langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java
author jjg
Sun, 20 Oct 2013 12:46:12 -0700
changeset 21481 f0aeb1da97bd
parent 21480 9d08c2d0fafd
child 21499 203cfd174518
permissions -rw-r--r--
8026791: wrong type_path encoded for method_return on an inner class constructor Reviewed-by: jjg Contributed-by: wdietl@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     1
/*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     4
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    10
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    15
 * accompanied this code).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    16
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    20
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    23
 * questions.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    24
 */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    25
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.code;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    27
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    28
import javax.lang.model.element.Element;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    29
import javax.lang.model.element.ElementKind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    30
import javax.lang.model.type.TypeKind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    31
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    32
import javax.tools.JavaFileObject;
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    33
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.code.Attribute.TypeCompound;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.code.Type.AnnotatedType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.code.Type.ArrayType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.code.Type.CapturedType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    38
import com.sun.tools.javac.code.Type.ClassType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    39
import com.sun.tools.javac.code.Type.ErrorType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    40
import com.sun.tools.javac.code.Type.ForAll;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    41
import com.sun.tools.javac.code.Type.MethodType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    42
import com.sun.tools.javac.code.Type.PackageType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    43
import com.sun.tools.javac.code.Type.TypeVar;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    44
import com.sun.tools.javac.code.Type.UndetVar;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.code.Type.Visitor;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    46
import com.sun.tools.javac.code.Type.WildcardType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    47
import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    48
import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    49
import com.sun.tools.javac.code.Symbol.VarSymbol;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
    50
import com.sun.tools.javac.code.Symbol.MethodSymbol;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
    51
import com.sun.tools.javac.comp.Annotate;
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
    52
import com.sun.tools.javac.comp.Annotate.Worker;
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
    53
import com.sun.tools.javac.comp.Attr;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    54
import com.sun.tools.javac.comp.AttrContext;
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    55
import com.sun.tools.javac.comp.Env;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    56
import com.sun.tools.javac.tree.JCTree;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    57
import com.sun.tools.javac.tree.TreeInfo;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    58
import com.sun.tools.javac.tree.JCTree.JCBlock;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    59
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    60
import com.sun.tools.javac.tree.JCTree.JCExpression;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
    61
import com.sun.tools.javac.tree.JCTree.JCLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    62
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
    63
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
    64
import com.sun.tools.javac.tree.JCTree.JCNewClass;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    65
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    66
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    67
import com.sun.tools.javac.tree.TreeScanner;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    68
import com.sun.tools.javac.tree.JCTree.*;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    69
import com.sun.tools.javac.util.Assert;
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    70
import com.sun.tools.javac.util.Context;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    71
import com.sun.tools.javac.util.List;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    72
import com.sun.tools.javac.util.ListBuffer;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    73
import com.sun.tools.javac.util.Log;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    74
import com.sun.tools.javac.util.Names;
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
    75
import com.sun.tools.javac.util.Options;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    76
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    77
/**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    78
 * Contains operations specific to processing type annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    79
 * This class has two functions:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    80
 * separate declaration from type annotations and insert the type
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    81
 * annotations to their types;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    82
 * and determine the TypeAnnotationPositions for all type annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    83
 */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    84
public class TypeAnnotations {
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    85
    protected static final Context.Key<TypeAnnotations> typeAnnosKey =
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    86
        new Context.Key<TypeAnnotations>();
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    87
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    88
    public static TypeAnnotations instance(Context context) {
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    89
        TypeAnnotations instance = context.get(typeAnnosKey);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    90
        if (instance == null)
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    91
            instance = new TypeAnnotations(context);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    92
        return instance;
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    93
    }
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    94
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    95
    final Log log;
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    96
    final Names names;
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    97
    final Symtab syms;
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
    98
    final Annotate annotate;
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
    99
    final Attr attr;
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   100
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   101
    protected TypeAnnotations(Context context) {
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   102
        context.put(typeAnnosKey, this);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   103
        names = Names.instance(context);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   104
        log = Log.instance(context);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   105
        syms = Symtab.instance(context);
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   106
        annotate = Annotate.instance(context);
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   107
        attr = Attr.instance(context);
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
   108
        Options options = Options.instance(context);
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   109
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   110
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   111
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   112
     * Separate type annotations from declaration annotations and
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   113
     * determine the correct positions for type annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   114
     * This version only visits types in signatures and should be
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   115
     * called from MemberEnter.
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   116
     * The method takes the Annotate object as parameter and
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   117
     * adds an Annotate.Worker to the correct Annotate queue for
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   118
     * later processing.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   119
     */
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   120
    public void organizeTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   121
        annotate.afterRepeated( new Worker() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   122
            @Override
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   123
            public void run() {
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   124
                JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   125
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   126
                try {
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   127
                    new TypeAnnotationPositions(true).scan(tree);
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   128
                } finally {
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   129
                    log.useSource(oldSource);
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   130
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   131
            }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   132
        } );
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   133
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   134
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   135
    public void validateTypeAnnotationsSignatures(final Env<AttrContext> env, final JCClassDecl tree) {
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   136
        annotate.validate(new Worker() { //validate annotations
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   137
            @Override
21040
3e32f68d2151 8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents: 21018
diff changeset
   138
            public void run() {
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   139
                JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   140
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   141
                try {
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   142
                    attr.validateTypeAnnotations(tree, true);
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   143
                } finally {
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   144
                    log.useSource(oldSource);
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   145
                }
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   146
            }
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   147
        } );
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   148
    }
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21010
diff changeset
   149
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   150
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   151
     * This version only visits types in bodies, that is, field initializers,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   152
     * top-level blocks, and method bodies, and should be called from Attr.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   153
     */
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   154
    public void organizeTypeAnnotationsBodies(JCClassDecl tree) {
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   155
        new TypeAnnotationPositions(false).scan(tree);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   156
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   157
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   158
    public enum AnnotationType { DECLARATION, TYPE, BOTH };
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   159
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   160
    /**
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   161
     * Determine whether an annotation is a declaration annotation,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   162
     * a type annotation, or both.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   163
     */
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   164
    public AnnotationType annotationType(Attribute.Compound a, Symbol s) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   165
        Attribute.Compound atTarget =
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   166
            a.type.tsym.attribute(syms.annotationTargetType.tsym);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   167
        if (atTarget == null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   168
            return inferTargetMetaInfo(a, s);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   169
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   170
        Attribute atValue = atTarget.member(names.value);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   171
        if (!(atValue instanceof Attribute.Array)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   172
            Assert.error("annotationType(): bad @Target argument " + atValue +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   173
                    " (" + atValue.getClass() + ")");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   174
            return AnnotationType.DECLARATION; // error recovery
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   175
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   176
        Attribute.Array arr = (Attribute.Array) atValue;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   177
        boolean isDecl = false, isType = false;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   178
        for (Attribute app : arr.values) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   179
            if (!(app instanceof Attribute.Enum)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   180
                Assert.error("annotationType(): unrecognized Attribute kind " + app +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   181
                        " (" + app.getClass() + ")");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   182
                isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   183
                continue;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   184
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   185
            Attribute.Enum e = (Attribute.Enum) app;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   186
            if (e.value.name == names.TYPE) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   187
                if (s.kind == Kinds.TYP)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   188
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   189
            } else if (e.value.name == names.FIELD) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   190
                if (s.kind == Kinds.VAR &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   191
                        s.owner.kind != Kinds.MTH)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   192
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   193
            } else if (e.value.name == names.METHOD) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   194
                if (s.kind == Kinds.MTH &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   195
                        !s.isConstructor())
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   196
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   197
            } else if (e.value.name == names.PARAMETER) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   198
                if (s.kind == Kinds.VAR &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   199
                        s.owner.kind == Kinds.MTH &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   200
                        (s.flags() & Flags.PARAMETER) != 0)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   201
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   202
            } else if (e.value.name == names.CONSTRUCTOR) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   203
                if (s.kind == Kinds.MTH &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   204
                        s.isConstructor())
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   205
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   206
            } else if (e.value.name == names.LOCAL_VARIABLE) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   207
                if (s.kind == Kinds.VAR &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   208
                        s.owner.kind == Kinds.MTH &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   209
                        (s.flags() & Flags.PARAMETER) == 0)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   210
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   211
            } else if (e.value.name == names.ANNOTATION_TYPE) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   212
                if (s.kind == Kinds.TYP &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   213
                        (s.flags() & Flags.ANNOTATION) != 0)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   214
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   215
            } else if (e.value.name == names.PACKAGE) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   216
                if (s.kind == Kinds.PCK)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   217
                    isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   218
            } else if (e.value.name == names.TYPE_USE) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   219
                if (s.kind == Kinds.TYP ||
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   220
                        s.kind == Kinds.VAR ||
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   221
                        (s.kind == Kinds.MTH && !s.isConstructor() &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   222
                        !s.type.getReturnType().hasTag(TypeTag.VOID)) ||
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   223
                        (s.kind == Kinds.MTH && s.isConstructor()))
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   224
                    isType = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   225
            } else if (e.value.name == names.TYPE_PARAMETER) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   226
                /* Irrelevant in this case */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   227
                // TYPE_PARAMETER doesn't aid in distinguishing between
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   228
                // Type annotations and declaration annotations on an
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   229
                // Element
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   230
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   231
                Assert.error("annotationType(): unrecognized Attribute name " + e.value.name +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   232
                        " (" + e.value.name.getClass() + ")");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   233
                isDecl = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   234
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   235
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   236
        if (isDecl && isType) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   237
            return AnnotationType.BOTH;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   238
        } else if (isType) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   239
            return AnnotationType.TYPE;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   240
        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   241
            return AnnotationType.DECLARATION;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   242
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   243
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   244
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   245
    /** Infer the target annotation kind, if none is give.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   246
     * We only infer declaration annotations.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   247
     */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   248
    private static AnnotationType inferTargetMetaInfo(Attribute.Compound a, Symbol s) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   249
        return AnnotationType.DECLARATION;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   250
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   251
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   252
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   253
    private class TypeAnnotationPositions extends TreeScanner {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   254
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   255
        private final boolean sigOnly;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   256
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   257
        TypeAnnotationPositions(boolean sigOnly) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   258
            this.sigOnly = sigOnly;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   259
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   260
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   261
        /*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   262
         * When traversing the AST we keep the "frames" of visited
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   263
         * trees in order to determine the position of annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   264
         */
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   265
        private ListBuffer<JCTree> frames = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   266
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   267
        protected void push(JCTree t) { frames = frames.prepend(t); }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   268
        protected JCTree pop() { return frames.next(); }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   269
        // could this be frames.elems.tail.head?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   270
        private JCTree peek2() { return frames.toList().tail.head; }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   271
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   272
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   273
        public void scan(JCTree tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   274
            push(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   275
            super.scan(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   276
            pop();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   277
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   278
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   279
        /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   280
         * Separates type annotations from declaration annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   281
         * This step is needed because in certain locations (where declaration
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   282
         * and type annotations can be mixed, e.g. the type of a field)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   283
         * we never build an JCAnnotatedType. This step finds these
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   284
         * annotations and marks them as if they were part of the type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   285
         */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   286
        private void separateAnnotationsKinds(JCTree typetree, Type type, Symbol sym,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   287
                TypeAnnotationPosition pos) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   288
            List<Attribute.Compound> annotations = sym.getRawAttributes();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   289
            ListBuffer<Attribute.Compound> declAnnos = new ListBuffer<Attribute.Compound>();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   290
            ListBuffer<Attribute.TypeCompound> typeAnnos = new ListBuffer<Attribute.TypeCompound>();
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   291
            ListBuffer<Attribute.TypeCompound> onlyTypeAnnos = new ListBuffer<Attribute.TypeCompound>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   292
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   293
            for (Attribute.Compound a : annotations) {
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   294
                switch (annotationType(a, sym)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   295
                case DECLARATION:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   296
                    declAnnos.append(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   297
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   298
                case BOTH: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   299
                    declAnnos.append(a);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   300
                    Attribute.TypeCompound ta = toTypeCompound(a, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   301
                    typeAnnos.append(ta);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   302
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   303
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   304
                case TYPE: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   305
                    Attribute.TypeCompound ta = toTypeCompound(a, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   306
                    typeAnnos.append(ta);
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   307
                    // Also keep track which annotations are only type annotations
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   308
                    onlyTypeAnnos.append(ta);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   309
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   310
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   311
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   312
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   313
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   314
            sym.resetAnnotations();
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   315
            sym.setDeclarationAttributes(declAnnos.toList());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   316
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   317
            if (typeAnnos.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   318
                return;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   319
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   320
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   321
            List<Attribute.TypeCompound> typeAnnotations = typeAnnos.toList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   322
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   323
            if (type == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   324
                // When type is null, put the type annotations to the symbol.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   325
                // This is used for constructor return annotations, for which
21481
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   326
                // we use the type of the enclosing class.
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   327
                type = sym.getEnclosingElement().asType();
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   328
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   329
                // Declaration annotations are always allowed on constructor returns.
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   330
                // Therefore, use typeAnnotations instead of onlyTypeAnnos.
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   331
                type = typeWithAnnotations(typetree, type, typeAnnotations, typeAnnotations);
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   332
                // Note that we don't use the result, the call to
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   333
                // typeWithAnnotations side-effects the type annotation positions.
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   334
                // This is important for constructors of nested classes.
f0aeb1da97bd 8026791: wrong type_path encoded for method_return on an inner class constructor
jjg
parents: 21480
diff changeset
   335
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   336
                sym.appendUniqueTypeAttributes(typeAnnotations);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   337
                return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   338
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   339
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   340
            // type is non-null and annotations are added to that type
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   341
            type = typeWithAnnotations(typetree, type, typeAnnotations, onlyTypeAnnos.toList());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   342
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   343
            if (sym.getKind() == ElementKind.METHOD) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   344
                sym.type.asMethodType().restype = type;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   345
            } else if (sym.getKind() == ElementKind.PARAMETER) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   346
                sym.type = type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   347
                if (sym.getQualifiedName().equals(names._this)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   348
                    sym.owner.type.asMethodType().recvtype = type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   349
                    // note that the typeAnnotations will also be added to the owner below.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   350
                } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   351
                    MethodType methType = sym.owner.type.asMethodType();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   352
                    List<VarSymbol> params = ((MethodSymbol)sym.owner).params;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   353
                    List<Type> oldArgs = methType.argtypes;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   354
                    ListBuffer<Type> newArgs = new ListBuffer<Type>();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   355
                    while (params.nonEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   356
                        if (params.head == sym) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   357
                            newArgs.add(type);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   358
                        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   359
                            newArgs.add(oldArgs.head);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   360
                        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   361
                        oldArgs = oldArgs.tail;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   362
                        params = params.tail;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   363
                    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   364
                    methType.argtypes = newArgs.toList();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   365
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   366
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   367
                sym.type = type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   368
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   369
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   370
            sym.appendUniqueTypeAttributes(typeAnnotations);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   371
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   372
            if (sym.getKind() == ElementKind.PARAMETER ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   373
                    sym.getKind() == ElementKind.LOCAL_VARIABLE ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   374
                    sym.getKind() == ElementKind.RESOURCE_VARIABLE ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   375
                    sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   376
                // Make sure all type annotations from the symbol are also
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   377
                // on the owner.
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   378
                sym.owner.appendUniqueTypeAttributes(sym.getRawTypeAttributes());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   379
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   380
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   381
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   382
        // This method has a similar purpose as
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   383
        // {@link com.sun.tools.javac.parser.JavacParser.insertAnnotationsToMostInner(JCExpression, List<JCTypeAnnotation>, boolean)}
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   384
        // We found a type annotation in a declaration annotation position,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   385
        // for example, on the return type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   386
        // Such an annotation is _not_ part of an JCAnnotatedType tree and we therefore
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   387
        // need to set its position explicitly.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   388
        // The method returns a copy of type that contains these annotations.
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   389
        //
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   390
        // As a side effect the method sets the type annotation position of "annotations".
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   391
        // Note that it is assumed that all annotations share the same position.
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   392
        private Type typeWithAnnotations(final JCTree typetree, final Type type,
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   393
                final List<Attribute.TypeCompound> annotations,
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   394
                final List<Attribute.TypeCompound> onlyTypeAnnotations) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   395
            // System.out.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s, onlyTypeAnnotations: %s)%n",
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   396
            //         typetree, type, annotations, onlyTypeAnnotations);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   397
            if (annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   398
                return type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   399
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   400
            if (type.hasTag(TypeTag.ARRAY)) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   401
                Type.ArrayType arType = (Type.ArrayType) type.unannotatedType();
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   402
                Type.ArrayType tomodify = new Type.ArrayType(null, arType.tsym);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   403
                Type toreturn;
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   404
                if (type.isAnnotated()) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   405
                    toreturn = tomodify.annotatedType(type.getAnnotationMirrors());
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   406
                } else {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   407
                    toreturn = tomodify;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   408
                }
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   409
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   410
                JCArrayTypeTree arTree = arrayTypeTree(typetree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   411
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   412
                ListBuffer<TypePathEntry> depth = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   413
                depth = depth.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   414
                while (arType.elemtype.hasTag(TypeTag.ARRAY)) {
16556
f4adc5bb4652 8008425: Remove interim new javax.lang.model API for type-annotations
jjg
parents: 15718
diff changeset
   415
                    if (arType.elemtype.isAnnotated()) {
21480
9d08c2d0fafd 8025109: Better encapsulation for AnnotatedType
jjg
parents: 21043
diff changeset
   416
                        Type aelemtype = arType.elemtype;
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   417
                        arType = (Type.ArrayType) aelemtype.unannotatedType();
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   418
                        ArrayType prevToMod = tomodify;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   419
                        tomodify = new Type.ArrayType(null, arType.tsym);
21480
9d08c2d0fafd 8025109: Better encapsulation for AnnotatedType
jjg
parents: 21043
diff changeset
   420
                        prevToMod.elemtype = tomodify.annotatedType(arType.elemtype.getAnnotationMirrors());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   421
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   422
                        arType = (Type.ArrayType) arType.elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   423
                        tomodify.elemtype = new Type.ArrayType(null, arType.tsym);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   424
                        tomodify = (Type.ArrayType) tomodify.elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   425
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   426
                    arTree = arrayTypeTree(arTree.elemtype);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   427
                    depth = depth.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   428
                }
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   429
                Type arelemType = typeWithAnnotations(arTree.elemtype, arType.elemtype, annotations, onlyTypeAnnotations);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   430
                tomodify.elemtype = arelemType;
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   431
                {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   432
                    // All annotations share the same position; modify the first one.
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   433
                    Attribute.TypeCompound a = annotations.get(0);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   434
                    TypeAnnotationPosition p = a.position;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   435
                    p.location = p.location.prependList(depth.toList());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   436
                }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   437
                typetree.type = toreturn;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   438
                return toreturn;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   439
            } else if (type.hasTag(TypeTag.TYPEVAR)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   440
                // Nothing to do for type variables.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   441
                return type;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   442
            } else if (type.getKind() == TypeKind.UNION) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   443
                // There is a TypeKind, but no TypeTag.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   444
                JCTypeUnion tutree = (JCTypeUnion) typetree;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   445
                JCExpression fst = tutree.alternatives.get(0);
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   446
                Type res = typeWithAnnotations(fst, fst.type, annotations, onlyTypeAnnotations);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   447
                fst.type = res;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   448
                // TODO: do we want to set res as first element in uct.alternatives?
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   449
                // UnionClassType uct = (com.sun.tools.javac.code.Type.UnionClassType)type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   450
                // Return the un-annotated union-type.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   451
                return type;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   452
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   453
                Type enclTy = type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   454
                Element enclEl = type.asElement();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   455
                JCTree enclTr = typetree;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   456
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   457
                while (enclEl != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   458
                        enclEl.getKind() != ElementKind.PACKAGE &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   459
                        enclTy != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   460
                        enclTy.getKind() != TypeKind.NONE &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   461
                        enclTy.getKind() != TypeKind.ERROR &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   462
                        (enclTr.getKind() == JCTree.Kind.MEMBER_SELECT ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   463
                         enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   464
                         enclTr.getKind() == JCTree.Kind.ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   465
                    // Iterate also over the type tree, not just the type: the type is already
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   466
                    // completely resolved and we cannot distinguish where the annotation
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   467
                    // belongs for a nested type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   468
                    if (enclTr.getKind() == JCTree.Kind.MEMBER_SELECT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   469
                        // only change encl in this case.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   470
                        enclTy = enclTy.getEnclosingType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   471
                        enclEl = enclEl.getEnclosingElement();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   472
                        enclTr = ((JCFieldAccess)enclTr).getExpression();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   473
                    } else if (enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   474
                        enclTr = ((JCTypeApply)enclTr).getType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   475
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   476
                        // only other option because of while condition
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   477
                        enclTr = ((JCAnnotatedType)enclTr).getUnderlyingType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   478
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   479
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   480
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   481
                /** We are trying to annotate some enclosing type,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   482
                 * but nothing more exists.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   483
                 */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   484
                if (enclTy != null &&
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   485
                        enclTy.hasTag(TypeTag.NONE)) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   486
                    switch (onlyTypeAnnotations.size()) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   487
                    case 0:
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   488
                        // Don't issue an error if all type annotations are
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   489
                        // also declaration annotations.
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   490
                        // If the annotations are also declaration annotations, they are
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   491
                        // illegal as type annotations but might be legal as declaration annotations.
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   492
                        // The normal declaration annotation checks make sure that the use is valid.
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   493
                        break;
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   494
                    case 1:
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   495
                        log.error(typetree.pos(), "cant.type.annotate.scoping.1",
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   496
                                onlyTypeAnnotations);
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   497
                        break;
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   498
                    default:
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   499
                        log.error(typetree.pos(), "cant.type.annotate.scoping",
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   500
                                onlyTypeAnnotations);
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   501
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   502
                    return type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   503
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   504
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   505
                // At this point we have visited the part of the nested
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   506
                // type that is written in the source code.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   507
                // Now count from here to the actual top-level class to determine
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   508
                // the correct nesting.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   509
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   510
                // The genericLocation for the annotation.
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   511
                ListBuffer<TypePathEntry> depth = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   512
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   513
                Type topTy = enclTy;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   514
                while (enclEl != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   515
                        enclEl.getKind() != ElementKind.PACKAGE &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   516
                        topTy != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   517
                        topTy.getKind() != TypeKind.NONE &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   518
                        topTy.getKind() != TypeKind.ERROR) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   519
                    topTy = topTy.getEnclosingType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   520
                    enclEl = enclEl.getEnclosingElement();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   521
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   522
                    if (topTy != null && topTy.getKind() != TypeKind.NONE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   523
                        // Only count enclosing types.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   524
                        depth = depth.append(TypePathEntry.INNER_TYPE);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   525
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   526
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   527
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   528
                if (depth.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   529
                    // Only need to change the annotation positions
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   530
                    // if they are on an enclosed type.
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   531
                    // All annotations share the same position; modify the first one.
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   532
                    Attribute.TypeCompound a = annotations.get(0);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   533
                    TypeAnnotationPosition p = a.position;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   534
                    p.location = p.location.appendList(depth.toList());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   535
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   536
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   537
                Type ret = typeWithAnnotations(type, enclTy, annotations);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   538
                typetree.type = ret;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   539
                return ret;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   540
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   541
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   542
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   543
        private JCArrayTypeTree arrayTypeTree(JCTree typetree) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   544
            if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   545
                return (JCArrayTypeTree) typetree;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   546
            } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   547
                return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   548
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   549
                Assert.error("Could not determine array type from type tree: " + typetree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   550
                return null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   551
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   552
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   553
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   554
        /** Return a copy of the first type that only differs by
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   555
         * inserting the annotations to the left-most/inner-most type
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   556
         * or the type given by stopAt.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   557
         *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   558
         * We need the stopAt parameter to know where on a type to
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   559
         * put the annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   560
         * If we have nested classes Outer > Middle > Inner, and we
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   561
         * have the source type "@A Middle.Inner", we will invoke
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   562
         * this method with type = Outer.Middle.Inner,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   563
         * stopAt = Middle.Inner, and annotations = @A.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   564
         *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   565
         * @param type The type to copy.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   566
         * @param stopAt The type to stop at.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   567
         * @param annotations The annotations to insert.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   568
         * @return A copy of type that contains the annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   569
         */
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   570
        private Type typeWithAnnotations(final Type type,
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   571
                final Type stopAt,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   572
                final List<Attribute.TypeCompound> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   573
            Visitor<Type, List<TypeCompound>> visitor =
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   574
                    new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   575
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   576
                public Type visitClassType(ClassType t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   577
                    // assert that t.constValue() == null?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   578
                    if (t == stopAt ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   579
                        t.getEnclosingType() == Type.noType) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   580
                        return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   581
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   582
                        ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   583
                                t.typarams_field, t.tsym);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   584
                        ret.all_interfaces_field = t.all_interfaces_field;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   585
                        ret.allparams_field = t.allparams_field;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   586
                        ret.interfaces_field = t.interfaces_field;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   587
                        ret.rank_field = t.rank_field;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   588
                        ret.supertype_field = t.supertype_field;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   589
                        return ret;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   590
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   591
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   592
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   593
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   594
                public Type visitAnnotatedType(AnnotatedType t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   595
                    return t.unannotatedType().accept(this, s).annotatedType(t.getAnnotationMirrors());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   596
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   597
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   598
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   599
                public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   600
                    return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   601
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   602
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   603
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   604
                public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   605
                    ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   606
                    return ret;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   607
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   608
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   609
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   610
                public Type visitMethodType(MethodType t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   611
                    // Impossible?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   612
                    return t;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   613
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   614
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   615
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   616
                public Type visitPackageType(PackageType t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   617
                    // Impossible?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   618
                    return t;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   619
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   620
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   621
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   622
                public Type visitTypeVar(TypeVar t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   623
                    return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   624
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   625
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   626
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   627
                public Type visitCapturedType(CapturedType t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   628
                    return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   629
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   630
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   631
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   632
                public Type visitForAll(ForAll t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   633
                    // Impossible?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   634
                    return t;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   635
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   636
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   637
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   638
                public Type visitUndetVar(UndetVar t, List<TypeCompound> s) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   639
                    // Impossible?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   640
                    return t;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   641
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   642
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   643
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   644
                public Type visitErrorType(ErrorType t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   645
                    return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   646
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   647
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   648
                @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   649
                public Type visitType(Type t, List<TypeCompound> s) {
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 21040
diff changeset
   650
                    return t.annotatedType(s);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   651
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   652
            };
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   653
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   654
            return type.accept(visitor, annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   655
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   656
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   657
        private Attribute.TypeCompound toTypeCompound(Attribute.Compound a, TypeAnnotationPosition p) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   658
            // It is safe to alias the position.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   659
            return new Attribute.TypeCompound(a, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   660
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   661
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   662
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   663
        /* This is the beginning of the second part of organizing
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   664
         * type annotations: determine the type annotation positions.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   665
         */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   666
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   667
        private void resolveFrame(JCTree tree, JCTree frame,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   668
                List<JCTree> path, TypeAnnotationPosition p) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   669
            /*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   670
            System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   671
            System.out.println("    Framing tree: " + frame + " kind: " + frame.getKind());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   672
            */
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   673
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   674
            // Note that p.offset is set in
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   675
            // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int)
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   676
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   677
            switch (frame.getKind()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   678
                case TYPE_CAST:
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   679
                    JCTypeCast frameTC = (JCTypeCast) frame;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   680
                    p.type = TargetType.CAST;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   681
                    if (frameTC.clazz.hasTag(Tag.TYPEINTERSECTION)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   682
                        // This case was already handled by INTERSECTION_TYPE
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   683
                    } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   684
                        p.type_index = 0;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   685
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   686
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   687
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   688
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   689
                case INSTANCE_OF:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   690
                    p.type = TargetType.INSTANCEOF;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   691
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   692
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   693
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   694
                case NEW_CLASS:
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   695
                    JCNewClass frameNewClass = (JCNewClass) frame;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   696
                    if (frameNewClass.def != null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   697
                        // Special handling for anonymous class instantiations
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   698
                        JCClassDecl frameClassDecl = frameNewClass.def;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   699
                        if (frameClassDecl.extending == tree) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   700
                            p.type = TargetType.CLASS_EXTENDS;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   701
                            p.type_index = -1;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   702
                        } else if (frameClassDecl.implementing.contains(tree)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   703
                            p.type = TargetType.CLASS_EXTENDS;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   704
                            p.type_index = frameClassDecl.implementing.indexOf(tree);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   705
                        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   706
                            // In contrast to CLASS below, typarams cannot occur here.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   707
                            Assert.error("Could not determine position of tree " + tree +
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   708
                                    " within frame " + frame);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   709
                        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   710
                    } else if (frameNewClass.typeargs.contains(tree)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   711
                        p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   712
                        p.type_index = frameNewClass.typeargs.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   713
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   714
                        p.type = TargetType.NEW;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   715
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   716
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   717
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   718
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   719
                case NEW_ARRAY:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   720
                    p.type = TargetType.NEW;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   721
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   722
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   723
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   724
                case ANNOTATION_TYPE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   725
                case CLASS:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   726
                case ENUM:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   727
                case INTERFACE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   728
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   729
                    if (((JCClassDecl)frame).extending == tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   730
                        p.type = TargetType.CLASS_EXTENDS;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   731
                        p.type_index = -1;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   732
                    } else if (((JCClassDecl)frame).implementing.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   733
                        p.type = TargetType.CLASS_EXTENDS;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   734
                        p.type_index = ((JCClassDecl)frame).implementing.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   735
                    } else if (((JCClassDecl)frame).typarams.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   736
                        p.type = TargetType.CLASS_TYPE_PARAMETER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   737
                        p.parameter_index = ((JCClassDecl)frame).typarams.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   738
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   739
                        Assert.error("Could not determine position of tree " + tree +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   740
                                " within frame " + frame);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   741
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   742
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   743
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   744
                case METHOD: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   745
                    JCMethodDecl frameMethod = (JCMethodDecl) frame;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   746
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   747
                    if (frameMethod.thrown.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   748
                        p.type = TargetType.THROWS;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   749
                        p.type_index = frameMethod.thrown.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   750
                    } else if (frameMethod.restype == tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   751
                        p.type = TargetType.METHOD_RETURN;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   752
                    } else if (frameMethod.typarams.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   753
                        p.type = TargetType.METHOD_TYPE_PARAMETER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   754
                        p.parameter_index = frameMethod.typarams.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   755
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   756
                        Assert.error("Could not determine position of tree " + tree +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   757
                                " within frame " + frame);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   758
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   759
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   760
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   761
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   762
                case PARAMETERIZED_TYPE: {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   763
                    List<JCTree> newPath = path.tail;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   764
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   765
                    if (((JCTypeApply)frame).clazz == tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   766
                        // generic: RAW; noop
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   767
                    } else if (((JCTypeApply)frame).arguments.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   768
                        JCTypeApply taframe = (JCTypeApply) frame;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   769
                        int arg = taframe.arguments.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   770
                        p.location = p.location.prepend(new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   771
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   772
                        Type typeToUse;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   773
                        if (newPath.tail != null && newPath.tail.head.hasTag(Tag.NEWCLASS)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   774
                            // If we are within an anonymous class instantiation, use its type,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   775
                            // because it contains a correctly nested type.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   776
                            typeToUse = newPath.tail.head.type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   777
                        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   778
                            typeToUse = taframe.type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   779
                        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   780
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   781
                        locateNestedTypes(typeToUse, p);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   782
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   783
                        Assert.error("Could not determine type argument position of tree " + tree +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   784
                                " within frame " + frame);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   785
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   786
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   787
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   788
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   789
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   790
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   791
                case MEMBER_REFERENCE: {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   792
                    JCMemberReference mrframe = (JCMemberReference) frame;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   793
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   794
                    if (mrframe.expr == tree) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   795
                        switch (mrframe.mode) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   796
                        case INVOKE:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   797
                            p.type = TargetType.METHOD_REFERENCE;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   798
                            break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   799
                        case NEW:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   800
                            p.type = TargetType.CONSTRUCTOR_REFERENCE;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   801
                            break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   802
                        default:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   803
                            Assert.error("Unknown method reference mode " + mrframe.mode +
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   804
                                    " for tree " + tree + " within frame " + frame);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   805
                        }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   806
                        p.pos = frame.pos;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   807
                    } else if (mrframe.typeargs != null &&
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   808
                            mrframe.typeargs.contains(tree)) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   809
                        int arg = mrframe.typeargs.indexOf(tree);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   810
                        p.type_index = arg;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   811
                        switch (mrframe.mode) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   812
                        case INVOKE:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   813
                            p.type = TargetType.METHOD_REFERENCE_TYPE_ARGUMENT;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   814
                            break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   815
                        case NEW:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   816
                            p.type = TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   817
                            break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   818
                        default:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   819
                            Assert.error("Unknown method reference mode " + mrframe.mode +
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   820
                                    " for tree " + tree + " within frame " + frame);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   821
                        }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   822
                        p.pos = frame.pos;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   823
                    } else {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   824
                        Assert.error("Could not determine type argument position of tree " + tree +
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   825
                                " within frame " + frame);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   826
                    }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   827
                    return;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   828
                }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   829
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   830
                case ARRAY_TYPE: {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   831
                    ListBuffer<TypePathEntry> index = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   832
                    index = index.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   833
                    List<JCTree> newPath = path.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   834
                    while (true) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   835
                        JCTree npHead = newPath.tail.head;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   836
                        if (npHead.hasTag(JCTree.Tag.TYPEARRAY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   837
                            newPath = newPath.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   838
                            index = index.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   839
                        } else if (npHead.hasTag(JCTree.Tag.ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   840
                            newPath = newPath.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   841
                        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   842
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   843
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   844
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   845
                    p.location = p.location.prependList(index.toList());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   846
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   847
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   848
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   849
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   850
                case TYPE_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   851
                    if (path.tail.tail.head.hasTag(JCTree.Tag.CLASSDEF)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   852
                        JCClassDecl clazz = (JCClassDecl)path.tail.tail.head;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   853
                        p.type = TargetType.CLASS_TYPE_PARAMETER_BOUND;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   854
                        p.parameter_index = clazz.typarams.indexOf(path.tail.head);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   855
                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   856
                        if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   857
                            // Account for an implicit Object as bound 0
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   858
                            p.bound_index += 1;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   859
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   860
                    } else if (path.tail.tail.head.hasTag(JCTree.Tag.METHODDEF)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   861
                        JCMethodDecl method = (JCMethodDecl)path.tail.tail.head;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   862
                        p.type = TargetType.METHOD_TYPE_PARAMETER_BOUND;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   863
                        p.parameter_index = method.typarams.indexOf(path.tail.head);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   864
                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   865
                        if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   866
                            // Account for an implicit Object as bound 0
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   867
                            p.bound_index += 1;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   868
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   869
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   870
                        Assert.error("Could not determine position of tree " + tree +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   871
                                " within frame " + frame);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   872
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   873
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   874
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   875
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   876
                case VARIABLE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   877
                    VarSymbol v = ((JCVariableDecl)frame).sym;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   878
                    p.pos = frame.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   879
                    switch (v.getKind()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   880
                        case LOCAL_VARIABLE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   881
                            p.type = TargetType.LOCAL_VARIABLE;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   882
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   883
                        case FIELD:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   884
                            p.type = TargetType.FIELD;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   885
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   886
                        case PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   887
                            if (v.getQualifiedName().equals(names._this)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   888
                                // TODO: Intro a separate ElementKind?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   889
                                p.type = TargetType.METHOD_RECEIVER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   890
                            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   891
                                p.type = TargetType.METHOD_FORMAL_PARAMETER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   892
                                p.parameter_index = methodParamIndex(path, frame);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   893
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   894
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   895
                        case EXCEPTION_PARAMETER:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   896
                            p.type = TargetType.EXCEPTION_PARAMETER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   897
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   898
                        case RESOURCE_VARIABLE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   899
                            p.type = TargetType.RESOURCE_VARIABLE;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   900
                            break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   901
                        default:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   902
                            Assert.error("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   903
                    }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   904
                    if (v.getKind() != ElementKind.FIELD) {
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17578
diff changeset
   905
                        v.owner.appendUniqueTypeAttributes(v.getRawTypeAttributes());
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   906
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   907
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   908
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   909
                case ANNOTATED_TYPE: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   910
                    if (frame == tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   911
                        // This is only true for the first annotated type we see.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   912
                        // For any other annotated types along the path, we do
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   913
                        // not care about inner types.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   914
                        JCAnnotatedType atypetree = (JCAnnotatedType) frame;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   915
                        final Type utype = atypetree.underlyingType.type;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   916
                        if (utype == null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   917
                            // This might happen during DeferredAttr;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   918
                            // we will be back later.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   919
                            return;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
   920
                        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   921
                        Symbol tsym = utype.tsym;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   922
                        if (tsym.getKind().equals(ElementKind.TYPE_PARAMETER) ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   923
                                utype.getKind().equals(TypeKind.WILDCARD) ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   924
                                utype.getKind().equals(TypeKind.ARRAY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   925
                            // Type parameters, wildcards, and arrays have the declaring
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   926
                            // class/method as enclosing elements.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   927
                            // There is actually nothing to do for them.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   928
                        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   929
                            locateNestedTypes(utype, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   930
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   931
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   932
                    List<JCTree> newPath = path.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   933
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   934
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   935
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   936
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   937
                case UNION_TYPE: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   938
                    List<JCTree> newPath = path.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   939
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   940
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   941
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   942
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   943
                case INTERSECTION_TYPE: {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   944
                    JCTypeIntersection isect = (JCTypeIntersection)frame;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   945
                    p.type_index = isect.bounds.indexOf(tree);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   946
                    List<JCTree> newPath = path.tail;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   947
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   948
                    return;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   949
                }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   950
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   951
                case METHOD_INVOCATION: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   952
                    JCMethodInvocation invocation = (JCMethodInvocation)frame;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   953
                    if (!invocation.typeargs.contains(tree)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   954
                        Assert.error("{" + tree + "} is not an argument in the invocation: " + invocation);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   955
                    }
19651
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   956
                    MethodSymbol exsym = (MethodSymbol) TreeInfo.symbol(invocation.getMethodSelect());
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   957
                    if (exsym == null) {
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   958
                        Assert.error("could not determine symbol for {" + invocation + "}");
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   959
                    } else if (exsym.isConstructor()) {
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   960
                        p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT;
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   961
                    } else {
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   962
                        p.type = TargetType.METHOD_INVOCATION_TYPE_ARGUMENT;
b1aa46cc2198 8023515: import type-annotations updates
jjg
parents: 18010
diff changeset
   963
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   964
                    p.pos = invocation.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   965
                    p.type_index = invocation.typeargs.indexOf(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   966
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   967
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   968
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   969
                case EXTENDS_WILDCARD:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   970
                case SUPER_WILDCARD: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   971
                    // Annotations in wildcard bounds
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   972
                    p.location = p.location.prepend(TypePathEntry.WILDCARD);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   973
                    List<JCTree> newPath = path.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   974
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   975
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   976
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   977
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   978
                case MEMBER_SELECT: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   979
                    List<JCTree> newPath = path.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   980
                    resolveFrame(newPath.head, newPath.tail.head, newPath, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   981
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   982
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   983
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   984
                default:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   985
                    Assert.error("Unresolved frame: " + frame + " of kind: " + frame.getKind() +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   986
                            "\n    Looking for tree: " + tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   987
                    return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   988
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   989
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   990
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
   991
        private void locateNestedTypes(Type type, TypeAnnotationPosition p) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   992
            // The number of "steps" to get from the full type to the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   993
            // left-most outer type.
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
   994
            ListBuffer<TypePathEntry> depth = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   995
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   996
            Type encl = type.getEnclosingType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   997
            while (encl != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   998
                    encl.getKind() != TypeKind.NONE &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   999
                    encl.getKind() != TypeKind.ERROR) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1000
                depth = depth.append(TypePathEntry.INNER_TYPE);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1001
                encl = encl.getEnclosingType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1002
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1003
            if (depth.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1004
                p.location = p.location.prependList(depth.toList());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1005
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1006
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1007
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
  1008
        private int methodParamIndex(List<JCTree> path, JCTree param) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1009
            List<JCTree> curr = path;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1010
            while (curr.head.getTag() != Tag.METHODDEF &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1011
                    curr.head.getTag() != Tag.LAMBDA) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1012
                curr = curr.tail;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1013
            }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1014
            if (curr.head.getTag() == Tag.METHODDEF) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1015
                JCMethodDecl method = (JCMethodDecl)curr.head;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1016
                return method.params.indexOf(param);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1017
            } else if (curr.head.getTag() == Tag.LAMBDA) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1018
                JCLambda lambda = (JCLambda)curr.head;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1019
                return lambda.params.indexOf(param);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1020
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1021
                Assert.error("methodParamIndex expected to find method or lambda for param: " + param);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1022
                return -1;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1023
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1024
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1025
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1026
        // Each class (including enclosed inner classes) is visited separately.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1027
        // This flag is used to prevent from visiting inner classes.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1028
        private boolean isInClass = false;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1029
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1030
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1031
        public void visitClassDef(JCClassDecl tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1032
            if (isInClass)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1033
                return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1034
            isInClass = true;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1035
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1036
            if (sigOnly) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1037
                scan(tree.mods);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1038
                scan(tree.typarams);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1039
                scan(tree.extending);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1040
                scan(tree.implementing);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1041
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1042
            scan(tree.defs);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1043
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1044
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1045
        /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1046
         * Resolve declaration vs. type annotations in methods and
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1047
         * then determine the positions.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1048
         */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1049
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1050
        public void visitMethodDef(final JCMethodDecl tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1051
            if (tree.sym == null) {
21043
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
  1052
                Assert.error("Visiting tree node before memberEnter");
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
  1053
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1054
            if (sigOnly) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1055
                if (!tree.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1056
                    // Nothing to do for separateAnnotationsKinds if
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1057
                    // there are no annotations of either kind.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1058
                    TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1059
                    pos.type = TargetType.METHOD_RETURN;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1060
                    if (tree.sym.isConstructor()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1061
                        pos.pos = tree.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1062
                        // Use null to mark that the annotations go with the symbol.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1063
                        separateAnnotationsKinds(tree, null, tree.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1064
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1065
                        pos.pos = tree.restype.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1066
                        separateAnnotationsKinds(tree.restype, tree.sym.type.getReturnType(),
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1067
                                tree.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1068
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1069
                }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1070
                if (tree.recvparam != null && tree.recvparam.sym != null &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1071
                        !tree.recvparam.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1072
                    // Nothing to do for separateAnnotationsKinds if
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1073
                    // there are no annotations of either kind.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1074
                    // TODO: make sure there are no declaration annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1075
                    TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1076
                    pos.type = TargetType.METHOD_RECEIVER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1077
                    pos.pos = tree.recvparam.vartype.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1078
                    separateAnnotationsKinds(tree.recvparam.vartype, tree.recvparam.sym.type,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1079
                            tree.recvparam.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1080
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1081
                int i = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1082
                for (JCVariableDecl param : tree.params) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1083
                    if (!param.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1084
                        // Nothing to do for separateAnnotationsKinds if
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1085
                        // there are no annotations of either kind.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1086
                        TypeAnnotationPosition pos = new TypeAnnotationPosition();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1087
                        pos.type = TargetType.METHOD_FORMAL_PARAMETER;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1088
                        pos.parameter_index = i;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1089
                        pos.pos = param.vartype.pos;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1090
                        separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1091
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1092
                    ++i;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1093
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1094
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1095
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1096
            push(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1097
            // super.visitMethodDef(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1098
            if (sigOnly) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1099
                scan(tree.mods);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1100
                scan(tree.restype);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1101
                scan(tree.typarams);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1102
                scan(tree.recvparam);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1103
                scan(tree.params);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1104
                scan(tree.thrown);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1105
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1106
                scan(tree.defaultValue);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1107
                scan(tree.body);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1108
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1109
            pop();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1110
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1111
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1112
        /* Store a reference to the current lambda expression, to
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1113
         * be used by all type annotations within this expression.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1114
         */
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1115
        private JCLambda currentLambda = null;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1116
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1117
        public void visitLambda(JCLambda tree) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1118
            JCLambda prevLambda = currentLambda;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1119
            try {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1120
                currentLambda = tree;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1121
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1122
                int i = 0;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1123
                for (JCVariableDecl param : tree.params) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1124
                    if (!param.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1125
                        // Nothing to do for separateAnnotationsKinds if
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1126
                        // there are no annotations of either kind.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1127
                        TypeAnnotationPosition pos = new TypeAnnotationPosition();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1128
                        pos.type = TargetType.METHOD_FORMAL_PARAMETER;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1129
                        pos.parameter_index = i;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1130
                        pos.pos = param.vartype.pos;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1131
                        pos.onLambda = tree;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1132
                        separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1133
                    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1134
                    ++i;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1135
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1136
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1137
                push(tree);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1138
                scan(tree.body);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1139
                scan(tree.params);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1140
                pop();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1141
            } finally {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1142
                currentLambda = prevLambda;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1143
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1144
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1145
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1146
        /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1147
         * Resolve declaration vs. type annotations in variable declarations and
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1148
         * then determine the positions.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1149
         */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1150
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1151
        public void visitVarDef(final JCVariableDecl tree) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1152
            if (tree.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1153
                // Nothing to do for separateAnnotationsKinds if
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1154
                // there are no annotations of either kind.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1155
            } else if (tree.sym == null) {
21043
3b000be15694 8026286: Improper locking of annotation queues causes assertion failures.
emc
parents: 21041
diff changeset
  1156
                Assert.error("Visiting tree node before memberEnter");
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
  1157
            } else if (tree.sym.getKind() == ElementKind.PARAMETER) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1158
                // Parameters are handled in visitMethodDef or visitLambda.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1159
            } else if (tree.sym.getKind() == ElementKind.FIELD) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1160
                if (sigOnly) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1161
                    TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1162
                    pos.type = TargetType.FIELD;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1163
                    pos.pos = tree.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1164
                    separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1165
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1166
            } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1167
                TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1168
                pos.type = TargetType.LOCAL_VARIABLE;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1169
                pos.pos = tree.pos;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1170
                pos.onLambda = currentLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1171
                separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1172
            } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1173
                TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1174
                pos.type = TargetType.EXCEPTION_PARAMETER;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1175
                pos.pos = tree.pos;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1176
                pos.onLambda = currentLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1177
                separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1178
            } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1179
                TypeAnnotationPosition pos = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1180
                pos.type = TargetType.RESOURCE_VARIABLE;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1181
                pos.pos = tree.pos;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1182
                pos.onLambda = currentLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1183
                separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
  1184
            } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
  1185
                // No type annotations can occur here.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1186
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1187
                // There is nothing else in a variable declaration that needs separation.
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
  1188
                Assert.error("Unhandled variable kind: " + tree + " of kind: " + tree.sym.getKind());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1189
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1190
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1191
            push(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1192
            // super.visitVarDef(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1193
            scan(tree.mods);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1194
            scan(tree.vartype);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1195
            if (!sigOnly) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1196
                scan(tree.init);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1197
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1198
            pop();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1199
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1200
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1201
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1202
        public void visitBlock(JCBlock tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1203
            // Do not descend into top-level blocks when only interested
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1204
            // in the signature.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1205
            if (!sigOnly) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1206
                scan(tree.stats);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1207
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1208
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1209
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1210
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1211
        public void visitAnnotatedType(JCAnnotatedType tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1212
            push(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1213
            findPosition(tree, tree, tree.annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1214
            pop();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1215
            super.visitAnnotatedType(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1216
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1217
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1218
        @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1219
        public void visitTypeParameter(JCTypeParameter tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1220
            findPosition(tree, peek2(), tree.annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1221
            super.visitTypeParameter(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1222
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1223
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1224
        @Override
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1225
        public void visitNewClass(JCNewClass tree) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1226
            if (tree.def != null &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1227
                    !tree.def.mods.annotations.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1228
                JCClassDecl classdecl = tree.def;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1229
                TypeAnnotationPosition pos = new TypeAnnotationPosition();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1230
                pos.type = TargetType.CLASS_EXTENDS;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1231
                pos.pos = tree.pos;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1232
                if (classdecl.extending == tree.clazz) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1233
                    pos.type_index = -1;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1234
                } else if (classdecl.implementing.contains(tree.clazz)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1235
                    pos.type_index = classdecl.implementing.indexOf(tree.clazz);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1236
                } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1237
                    // In contrast to CLASS elsewhere, typarams cannot occur here.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1238
                    Assert.error("Could not determine position of tree " + tree);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1239
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1240
                Type before = classdecl.sym.type;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1241
                separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1242
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1243
                // classdecl.sym.type now contains an annotated type, which
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1244
                // is not what we want there.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1245
                // TODO: should we put this type somewhere in the superclass/interface?
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1246
                classdecl.sym.type = before;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1247
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1248
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1249
            scan(tree.encl);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1250
            scan(tree.typeargs);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1251
            scan(tree.clazz);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1252
            scan(tree.args);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1253
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1254
            // The class body will already be scanned.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1255
            // scan(tree.def);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1256
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1257
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1258
        @Override
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1259
        public void visitNewArray(JCNewArray tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1260
            findPosition(tree, tree, tree.annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1261
            int dimAnnosCount = tree.dimAnnotations.size();
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19651
diff changeset
  1262
            ListBuffer<TypePathEntry> depth = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1263
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1264
            // handle annotations associated with dimensions
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1265
            for (int i = 0; i < dimAnnosCount; ++i) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1266
                TypeAnnotationPosition p = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1267
                p.pos = tree.pos;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1268
                p.onLambda = currentLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1269
                p.type = TargetType.NEW;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1270
                if (i != 0) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1271
                    depth = depth.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1272
                    p.location = p.location.appendList(depth.toList());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1273
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1274
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1275
                setTypeAnnotationPos(tree.dimAnnotations.get(i), p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1276
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1277
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1278
            // handle "free" annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1279
            // int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1280
            // TODO: is depth.size == i here?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1281
            JCExpression elemType = tree.elemtype;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1282
            depth = depth.append(TypePathEntry.ARRAY);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1283
            while (elemType != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1284
                if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1285
                    JCAnnotatedType at = (JCAnnotatedType)elemType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1286
                    TypeAnnotationPosition p = new TypeAnnotationPosition();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1287
                    p.type = TargetType.NEW;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1288
                    p.pos = tree.pos;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1289
                    p.onLambda = currentLambda;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1290
                    locateNestedTypes(elemType.type, p);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1291
                    p.location = p.location.prependList(depth.toList());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1292
                    setTypeAnnotationPos(at.annotations, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1293
                    elemType = at.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1294
                } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1295
                    depth = depth.append(TypePathEntry.ARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1296
                    elemType = ((JCArrayTypeTree)elemType).elemtype;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1297
                } else if (elemType.hasTag(JCTree.Tag.SELECT)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1298
                    elemType = ((JCFieldAccess)elemType).selected;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1299
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1300
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1301
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1302
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1303
            scan(tree.elems);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1304
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1305
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1306
        private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1307
            if (!annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1308
                /*
21010
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
  1309
                System.err.println("Finding pos for: " + annotations);
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
  1310
                System.err.println("    tree: " + tree + " kind: " + tree.getKind());
5ffe0d8a5e24 8008762: Type annotation on inner class in anonymous class show up as regular type annotations
emc
parents: 20258
diff changeset
  1311
                System.err.println("    frame: " + frame + " kind: " + frame.getKind());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1312
                */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1313
                TypeAnnotationPosition p = new TypeAnnotationPosition();
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1314
                p.onLambda = currentLambda;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1315
                resolveFrame(tree, frame, frames.toList(), p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1316
                setTypeAnnotationPos(annotations, p);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1317
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1318
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1319
20258
bdaa691b4da4 8025407: TypeAnnotations does not use Context
jjg
parents: 20249
diff changeset
  1320
        private void setTypeAnnotationPos(List<JCAnnotation> annotations,
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1321
                TypeAnnotationPosition position) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1322
            for (JCAnnotation anno : annotations) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1323
                // attribute might be null during DeferredAttr;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1324
                // we will be back later.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1325
                if (anno.attribute != null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1326
                    ((Attribute.TypeCompound) anno.attribute).position = position;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1327
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1328
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1329
        }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1330
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1331
        @Override
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1332
        public String toString() {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1333
            return super.toString() + ": sigOnly: " + sigOnly;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17278
diff changeset
  1334
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1335
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
  1336
}