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