langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
author jjg
Fri, 07 Sep 2012 11:24:00 -0700
changeset 13840 a365c561f4de
parent 13689 4d519199a6aa
child 14054 7ae16dd77c90
permissions -rw-r--r--
7196774: javac cannot be built with JDK 6 after 7151010 Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    28
import java.util.Map;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.util.*;
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    31
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    37
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
    38
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/** Enter annotations on symbols.  Annotations accumulate in a queue,
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  which is processed at the top level of any set of recursive calls
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  requesting it be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    43
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
public class Annotate {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    protected static final Context.Key<Annotate> annotateKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        new Context.Key<Annotate>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    public static Annotate instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        Annotate instance = context.get(annotateKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            instance = new Annotate(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    final Symtab syms;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    63
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    final Resolve rs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    final Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    protected Annotate(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        context.put(annotateKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        syms = Symtab.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    75
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * Queue maintenance
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    private int enterCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    ListBuffer<Annotator> q = new ListBuffer<Annotator>();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    89
    ListBuffer<Annotator> repeatedQ = new ListBuffer<Annotator>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    91
    public void normal(Annotator a) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        q.append(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public void earlier(Annotator a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        q.prepend(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
    99
    public void repeated(Annotator a) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   100
        repeatedQ.append(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   101
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   102
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /** Called when the Enter phase starts. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public void enterStart() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    /** Called after the Enter phase completes. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    public void enterDone() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (enterCount != 0) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        enterCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            while (q.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                q.next().enterAnnotation();
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   120
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   121
            while (repeatedQ.nonEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   122
                repeatedQ.next().enterAnnotation();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   123
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            enterCount--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /** A client that has annotations to add registers an annotator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     *  the method it will use to add the annotation.  There are no
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *  parameters; any needed data should be captured by the
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     *  Annotator.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public interface Annotator {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        void enterAnnotation();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        String toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   139
    /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   140
     * This context contains all the information needed to synthesize new
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   141
     * annotations trees by the completer for repeating annotations.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   142
     */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   143
    public class AnnotateRepeatedContext {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   144
        public final Env<AttrContext> env;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   145
        public final Map<Symbol.TypeSymbol, ListBuffer<Attribute.Compound>> annotated;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   146
        public final Map<Attribute.Compound, JCDiagnostic.DiagnosticPosition> pos;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   147
        public final Log log;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   148
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   149
        public AnnotateRepeatedContext(Env<AttrContext> env,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   150
                                       Map<Symbol.TypeSymbol, ListBuffer<Attribute.Compound>> annotated,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   151
                                       Map<Attribute.Compound, JCDiagnostic.DiagnosticPosition> pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   152
                                       Log log) {
13840
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   153
            Assert.checkNonNull(env);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   154
            Assert.checkNonNull(annotated);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   155
            Assert.checkNonNull(pos);
a365c561f4de 7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents: 13689
diff changeset
   156
            Assert.checkNonNull(log);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   157
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   158
            this.env = env;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   159
            this.annotated = annotated;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   160
            this.pos = pos;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   161
            this.log = log;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   162
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   163
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   164
        /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   165
         * Process a list of repeating annotations returning a new
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   166
         * Attribute.Compound that is the attribute for the synthesized tree
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   167
         * for the container.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   168
         *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   169
         * @param repeatingAnnotations a List of repeating annotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   170
         * @return a new Attribute.Compound that is the container for the repeatingAnnotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   171
         */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   172
        public Attribute.Compound processRepeatedAnnotations(List<Attribute.Compound> repeatingAnnotations) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   173
            return Annotate.this.processRepeatedAnnotations(repeatingAnnotations, this);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   174
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   175
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   176
        /**
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   177
         * Queue the Annotator a on the repeating annotations queue of the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   178
         * Annotate instance this context belongs to.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   179
         *
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   180
         * @param a the Annotator to enqueue for repeating annotation annotating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   181
         */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   182
        public void annotateRepeated(Annotator a) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   183
            Annotate.this.repeated(a);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   184
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   185
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
 * Compute an attribute from its annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /** Process a single compound annotation, returning its
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     *  Attribute. Used from MemberEnter for attaching the attributes
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     *  to the annotated symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    Attribute.Compound enterAnnotation(JCAnnotation a,
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                                       Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                                       Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        // The annotation might have had its type attributed (but not checked)
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        // need to do it again.
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        Type at = (a.annotationType.type != null ? a.annotationType.type
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                  : attr.attribType(a.annotationType, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        a.type = chk.checkType(a.annotationType.pos(), at, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        if (a.type.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
            log.error(a.annotationType.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                      "not.annotation.type", a.type.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        List<JCExpression> args = a.args;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   212
        if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            // special case: elided "value=" assumed
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            args.head = make.at(args.head.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                Assign(make.Ident(names.value), args.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        ListBuffer<Pair<MethodSymbol,Attribute>> buf =
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            new ListBuffer<Pair<MethodSymbol,Attribute>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            JCExpression t = tl.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   221
            if (!t.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            JCAssign assign = (JCAssign)t;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   226
            if (!assign.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                log.error(t.pos(), "annotation.value.must.be.name.value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            JCIdent left = (JCIdent)assign.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            Symbol method = rs.resolveQualifiedMethod(left.pos(),
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   232
                                                          env,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   233
                                                          a.type,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   234
                                                          left.name,
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   235
                                                          List.<Type>nil(),
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   236
                                                          null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            left.sym = method;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            left.type = method.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            if (method.owner != a.type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                log.error(left.pos(), "no.annotation.member", left.name, a.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            Type result = method.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            Attribute value = enterAttributeValue(result, assign.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            if (!method.type.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                buf.append(new Pair<MethodSymbol,Attribute>
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                           ((MethodSymbol)method, value));
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   246
            t.type = result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        return new Attribute.Compound(a.type, buf.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    Attribute enterAttributeValue(Type expected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                                  JCExpression tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                                  Env<AttrContext> env) {
9603
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   254
        //first, try completing the attribution value sym - if a completion
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   255
        //error is thrown, we should recover gracefully, and display an
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   256
        //ordinary resolution diagnostic.
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   257
        try {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   258
            expected.tsym.complete();
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   259
        } catch(CompletionFailure e) {
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   260
            log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   261
            return new Attribute.Error(expected);
fa337b87574b 6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents: 7681
diff changeset
   262
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            if (result.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            if (result.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                log.error(tree.pos(), "attribute.value.must.be.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            result = cfolder.coerce(result, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            return new Attribute.Constant(expected, result.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        if (expected.tsym == syms.classType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            Type result = attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            if (result.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            if (TreeInfo.name(tree) != names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                log.error(tree.pos(), "annotation.value.must.be.class.literal");
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            return new Attribute.Class(types,
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                                       (((JCFieldAccess) tree).selected).type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   286
            if (!tree.hasTag(ANNOTATION)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                log.error(tree.pos(), "annotation.value.must.be.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                expected = syms.errorType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return enterAnnotation((JCAnnotation)tree, expected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9603
diff changeset
   293
            if (!tree.hasTag(NEWARRAY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                tree = make.at(tree.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            JCNewArray na = (JCNewArray)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            if (na.elemtype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                buf.append(enterAttributeValue(types.elemtype(expected),
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                                               l.head,
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                                               env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 5847
diff changeset
   308
            na.type = expected;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            return new Attribute.
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                Array(expected, buf.toArray(new Attribute[buf.length()]));
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        if (expected.tag == TypeTags.CLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            (expected.tsym.flags() & Flags.ENUM) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            attr.attribExpr(tree, env, expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                TreeInfo.nonstaticSelect(tree) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                sym.kind != Kinds.VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                (sym.flags() & Flags.ENUM) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                return new Attribute.Error(expected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            VarSymbol enumerator = (VarSymbol) sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            return new Attribute.Enum(expected, enumerator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        if (!expected.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            log.error(tree.pos(), "annotation.value.not.allowable.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        return new Attribute.Error(attr.attribExpr(tree, env, expected));
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   330
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   331
    /* *********************************
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   332
     * Support for repeating annotations
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   333
     ***********************************/
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   334
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   335
    /* Process repeated annotations. This method returns the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   336
     * synthesized container annotation or null IFF all repeating
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   337
     * annotation are invalid.  This method reports errors/warnings.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   338
     */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   339
    private Attribute.Compound processRepeatedAnnotations(List<Attribute.Compound> annotations,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   340
            AnnotateRepeatedContext ctx) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   341
        Attribute.Compound firstOccurrence = annotations.head;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   342
        List<Attribute> repeated = List.nil();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   343
        Type origAnnoType;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   344
        Type arrayOfOrigAnnoType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   345
        Type targetContainerType = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   346
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   347
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   348
        Assert.check(!annotations.isEmpty() &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   349
                     !annotations.tail.isEmpty()); // i.e. size() > 1
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   350
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   351
        for (List<Attribute.Compound> al = annotations;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   352
             !al.isEmpty();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   353
             al = al.tail)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   354
        {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   355
            Attribute.Compound currentAnno = al.head;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   356
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   357
            origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   358
            if (arrayOfOrigAnnoType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   359
                arrayOfOrigAnnoType = types.makeArrayType(origAnnoType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
}
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   361
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   362
            Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   363
            if (currentContainerType == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   364
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   365
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   366
            // Assert that the target Container is == for all repeated
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   367
            // annos of the same annotation type, the types should
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   368
            // come from the same Symbol, i.e. be '=='
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   369
            Assert.check(targetContainerType == null || currentContainerType == targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   370
            targetContainerType = currentContainerType;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   371
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   372
            containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   373
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   374
            if (containerValueSymbol == null) { // Check of CA type failed
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   375
                // errors are already reported
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   376
                continue;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   377
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   378
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   379
            repeated = repeated.prepend(currentAnno);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   380
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   381
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   382
        if (!repeated.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   383
            repeated = repeated.reverse();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   384
            JCAnnotation annoTree;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   385
            TreeMaker m = make.at(ctx.pos.get(firstOccurrence));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   386
            Pair<MethodSymbol, Attribute> p =
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   387
                    new Pair<MethodSymbol, Attribute>(containerValueSymbol,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   388
                                                      new Attribute.Array(arrayOfOrigAnnoType, repeated));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   389
            annoTree = m.Annotation(new Attribute.Compound(targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   390
                    List.of(p)));
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   391
            Attribute.Compound c = enterAnnotation(annoTree,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   392
                                                   targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   393
                                                   ctx.env);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   394
            return c;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   395
        } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   396
            return null; // errors should have been reported elsewhere
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   397
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   398
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   399
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   400
    /** Fetches the actual Type that should be the containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   401
    private Type getContainingType(Attribute.Compound currentAnno,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   402
            DiagnosticPosition pos)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   403
    {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   404
        Type origAnnoType = currentAnno.type;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   405
        TypeSymbol origAnnoDecl = origAnnoType.tsym;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   406
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   407
        // Fetch the ContainedBy annotation from the current
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   408
        // annotation's declaration, or null if it has none
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   409
        Attribute.Compound ca = origAnnoDecl.attribute(syms.containedByType.tsym);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   410
        if (ca == null) { // has no ContainedBy annotation
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   411
            log.error(pos, "duplicate.annotation.missing.container", origAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   412
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   413
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   414
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   415
        return filterSame(extractContainingType(ca, pos, origAnnoDecl),
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   416
                          origAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   417
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   418
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   419
    // returns null if t is same as 's', returns 't' otherwise
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   420
    private Type filterSame(Type t, Type s) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   421
        if (t == null || s == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   422
            return t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   423
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   424
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   425
        return types.isSameType(t, s) ? null : t;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   426
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   427
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   428
    /** Extract the actual Type to be used for a containing annotation. */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   429
    private Type extractContainingType(Attribute.Compound ca,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   430
            DiagnosticPosition pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   431
            TypeSymbol annoDecl)
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   432
    {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   433
        // The next three checks check that the ContainedBy annotation
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   434
        // on the declaration of the annotation type that is repeating is
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   435
        // valid.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   436
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   437
        // ContainedBy must have at least one element
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   438
        if (ca.values.isEmpty()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   439
            log.error(pos, "invalid.containedby.annotation", annoDecl);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   440
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   441
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   442
        Pair<MethodSymbol,Attribute> p = ca.values.head;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   443
        Name name = p.fst.name;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   444
        if (name != names.value) { // should contain only one element, named "value"
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   445
            log.error(pos, "invalid.containedby.annotation", annoDecl);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   446
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   447
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   448
        if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   449
            log.error(pos, "invalid.containedby.annotation", annoDecl);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   450
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   451
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   452
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   453
        return ((Attribute.Class)p.snd).getValue();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   454
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   455
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   456
    /* Validate that the suggested targetContainerType Type is a valid
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   457
     * container type for repeated instances of originalAnnoType
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   458
     * annotations. Return null and report errors if this is not the
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   459
     * case, return the MethodSymbol of the value element in
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   460
     * targetContainerType if it is suitable (this is needed to
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   461
     * synthesize the container). */
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   462
    private MethodSymbol validateContainer(Type targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   463
                                           Type originalAnnoType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   464
                                           DiagnosticPosition pos) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   465
        MethodSymbol containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   466
        boolean fatalError = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   467
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   468
        // Validate that there is a (and only 1) value method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   469
        Scope scope = targetContainerType.tsym.members();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   470
        int nr_value_elems = 0;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   471
        boolean error = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   472
        for(Symbol elm : scope.getElementsByName(names.value)) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   473
            nr_value_elems++;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   474
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   475
            if (nr_value_elems == 1 &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   476
                elm.kind == Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   477
                containerValueSymbol = (MethodSymbol)elm;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   478
            } else {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   479
                error = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   480
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   481
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   482
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   483
            log.error(pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   484
                      "invalid.containedby.annotation.multiple.values",
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   485
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   486
                      nr_value_elems);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   487
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   488
        } else if (nr_value_elems == 0) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   489
            log.error(pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   490
                      "invalid.containedby.annotation.no.value",
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   491
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   492
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   493
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   494
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   495
        // validate that the 'value' element is a method
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   496
        // probably "impossible" to fail this
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   497
        if (containerValueSymbol.kind != Kinds.MTH) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   498
            log.error(pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   499
                      "invalid.containedby.annotation.invalid.value",
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   500
                      targetContainerType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   501
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   502
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   503
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   504
        // validate that the 'value' element has the correct return type
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   505
        // i.e. array of original anno
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   506
        Type valueRetType = containerValueSymbol.type.getReturnType();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   507
        Type expectedType = types.makeArrayType(originalAnnoType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   508
        if (!(types.isArray(valueRetType) &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   509
              types.isSameType(expectedType, valueRetType))) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   510
            log.error(pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   511
                      "invalid.containedby.annotation.value.return",
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   512
                      targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   513
                      valueRetType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   514
                      expectedType);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   515
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   516
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   517
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   518
        // validate that all other elements of containing type has defaults
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   519
        scope = targetContainerType.tsym.members();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   520
        error = false;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   521
        for(Symbol elm : scope.getElements()) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   522
            if (elm.name != names.value &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   523
                elm.kind == Kinds.MTH &&
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   524
                ((MethodSymbol)elm).defaultValue == null) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   525
                log.error(pos,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   526
                          "invalid.containedby.annotation.elem.nondefault",
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   527
                          targetContainerType,
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   528
                          elm);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   529
                containerValueSymbol = null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   530
                error = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   531
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   532
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   533
        if (error) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   534
            fatalError = true;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   535
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   536
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   537
        // Explicitly no check for/validity of @ContainerFor. That is
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   538
        // done on declaration of the container, and at reflect time.
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   539
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   540
        // The rest of the conditions for a valid containing annotation are made
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   541
        // in Check.validateRepeatedAnnotaton();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   542
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   543
        return fatalError ? null : containerValueSymbol;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   544
    }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 10950
diff changeset
   545
}