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