langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
author mcimadamore
Tue, 28 Oct 2008 14:05:59 +0000
changeset 1538 3cbbc9424f43
parent 1534 e923a41e84cc
child 1791 d378f023c36d
permissions -rw-r--r--
6763518: Impossible to suppress raw-type warnings Summary: Check.validate(Type) should be invoked after -Xlint is augmented in Attr.visitVarDef Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 661
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.ElementKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
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.jvm.*;
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.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.jvm.Target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.code.Type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.source.tree.IdentifierTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.source.tree.MemberSelectTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.source.tree.TreeVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.source.util.SimpleTreeVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
/** This is the main context-dependent analysis phase in GJC. It
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  encompasses name resolution, type checking and constant folding as
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  subtasks. Some subtasks involve auxiliary classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  @see Check
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  @see Resolve
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *  @see ConstFold
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *  @see Infer
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
public class Attr extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    protected static final Context.Key<Attr> attrKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        new Context.Key<Attr>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    71
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    final Resolve rs;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    final MemberEnter memberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    final Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    final Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
    82
    final JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    final Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public static Attr instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        Attr instance = context.get(attrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            instance = new Attr(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    protected Attr(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        context.put(attrKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    95
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        memberEnter = MemberEnter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        target = Target.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
   106
        diags = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        allowGenerics = source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        allowVarargs = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        allowEnums = source.allowEnums();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        allowBoxing = source.allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        allowCovariantReturns = source.allowCovariantReturns();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        allowAnonOuterThis = source.allowAnonOuterThis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        relax = (options.get("-retrofit") != null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                 options.get("-relax") != null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /** Switch: relax some constraints for retrofit mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    boolean relax;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /** Switch: support generics?
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /** Switch: allow variable-arity methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    boolean allowVarargs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** Switch: support enums?
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    boolean allowEnums;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    /** Switch: support boxing and unboxing?
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    boolean allowBoxing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    /** Switch: support covariant result types?
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    boolean allowCovariantReturns;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** Switch: allow references to surrounding object from anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * objects during constructor call?
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    boolean allowAnonOuterThis;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * Switch: warn about use of variable before declaration?
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     * RFE: 6425594
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    boolean useBeforeDeclarationWarning;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /** Check kind and type of given tree against protokind and prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     *  If check succeeds, store type in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     *  If check fails, store errType in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     *  No checks are performed if the prototype is a method type.
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   162
     *  It is not necessary in this case since we know that kind and type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     *  are correct.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *  @param tree     The tree whose kind and type is checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     *  @param owntype  The computed type of the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     *  @param ownkind  The computed kind of the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     *  @param pkind    The expected kind (or: protokind) of the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     *  @param pt       The expected type (or: prototype) of the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            if ((ownkind & ~pkind) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                owntype = chk.checkType(tree.pos(), owntype, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                log.error(tree.pos(), "unexpected.type",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   177
                          kindNames(pkind),
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   178
                          kindName(ownkind));
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   179
                owntype = types.createErrorType(owntype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        tree.type = owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        return owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    /** Is given blank final variable assignable, i.e. in a scope where it
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     *  may be assigned to even though it is final?
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     *  @param v      The blank final variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        Symbol owner = env.info.scope.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
           // owner refers to the innermost variable, method or
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
           // initializer block declaration at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            v.owner == owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            ((owner.name == names.init ||    // i.e. we are in a constructor
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
              owner.kind == VAR ||           // i.e. we are in a variable initializer
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
              (owner.flags() & BLOCK) != 0)  // i.e. we are in an initializer block
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
             v.owner == owner.owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
             ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    /** Check that variable can be assigned to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     *  @param pos    The current source code position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     *  @param v      The assigned varaible
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     *  @param base   If the variable is referred to in a Select, the part
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     *                to the left of the `.', null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        if ((v.flags() & FINAL) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            ((v.flags() & HASINIT) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
             ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
             !((base == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
               (base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
               isAssignableAsBlankFinal(v, env)))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            log.error(pos, "cant.assign.val.to.final.var", v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    /** Does tree represent a static reference to an identifier?
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     *  It is assumed that tree is either a SELECT or an IDENT.
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     *  We have to weed out selects from non-type names here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     *  @param tree    The candidate tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    boolean isStaticReference(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        if (tree.getTag() == JCTree.SELECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            if (lsym == null || lsym.kind != TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    /** Is this symbol a type?
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    static boolean isType(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        return sym != null && sym.kind == TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /** The current `this' symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        return rs.resolveSelf(pos, env, env.enclClass.sym, names._this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    /** Attribute a parsed identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     * @param tree Parsed identifier name
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     * @param topLevel The toplevel to use
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        Env<AttrContext> localEnv = enter.topLevelEnv(topLevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        localEnv.enclClass = make.ClassDef(make.Modifiers(0),
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                                           syms.errSymbol.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                                           null, null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        localEnv.enclClass.sym = syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        return tree.accept(identAttributer, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                Symbol site = visit(node.getExpression(), env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                if (site.kind == ERR)
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                    return site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                Name name = (Name)node.getIdentifier();
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                if (site.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    env.toplevel.packge = (PackageSymbol)site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                    return rs.findIdentInPackage(env, (TypeSymbol)site, name, TYP | PCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                    env.enclClass.sym = (ClassSymbol)site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                return rs.findIdent(env, (Name)node.getName(), TYP | PCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    public Type coerce(Type etype, Type ttype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        return cfolder.coerce(etype, ttype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    public Type attribType(JCTree node, TypeSymbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        Env<AttrContext> env = enter.typeEnvs.get(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        Env<AttrContext> localEnv = env.dup(node, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        return attribTree(node, localEnv, Kinds.TYP, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        breakTree = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        JavaFileObject prev = log.useSource(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            attribExpr(expr, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            return b.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        breakTree = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        JavaFileObject prev = log.useSource(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            attribStat(stmt, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            return b.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    private JCTree breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    private static class BreakAttr extends RuntimeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        static final long serialVersionUID = -6924771130405446405L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        private Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        private BreakAttr(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
 * Visitor methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    /** Visitor argument: the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    /** Visitor argument: the currently expected proto-kind.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    int pkind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    /** Visitor argument: the currently expected proto-type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    Type pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    /** Visitor result: the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    Type result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
    /** Visitor method: attribute a tree, catching any completion failure
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     *  exceptions. Return the tree's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
     *  @param tree    The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
     *  @param env     The environment visitor argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
     *  @param pkind   The protokind visitor argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
     *  @param pt      The prototype visitor argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
    Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        Env<AttrContext> prevEnv = this.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        int prevPkind = this.pkind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        Type prevPt = this.pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            this.pkind = pkind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            this.pt = pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
            if (tree == breakTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                throw new BreakAttr(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            return chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
            this.env = prevEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            this.pkind = prevPkind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            this.pt = prevPt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    /** Derived visitor method: attribute an expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    /** Derived visitor method: attribute an expression tree with
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
     *  no constraints on the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    Type attribExpr(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        return attribTree(tree, env, VAL, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
    /** Derived visitor method: attribute a type tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    Type attribType(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        Type result = attribTree(tree, env, TYP, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    /** Derived visitor method: attribute a statement or definition tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
    public Type attribStat(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        return attribTree(tree, env, NIL, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    /** Attribute a list of expressions, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        ListBuffer<Type> ts = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            ts.append(attribExpr(l.head, env, pt));
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    /** Attribute a list of statements, returning nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        for (List<T> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
    /** Attribute the arguments in a method call, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    List<Type> attribArgs(List<JCExpression> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        ListBuffer<Type> argtypes = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            argtypes.append(chk.checkNonVoid(
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
                l.head.pos(), types.upperBound(attribTree(l.head, env, VAL, Infer.anyPoly))));
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        return argtypes.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    /** Attribute a type argument list, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        ListBuffer<Type> argtypes = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            argtypes.append(chk.checkRefType(l.head.pos(), attribType(l.head, env)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        return argtypes.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
     * Attribute type variables (of generic classes or methods).
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
     * Compound types are attributed later in attribBounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
     * @param typarams the type variables to enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
     * @param env      the current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        for (JCTypeParameter tvar : typarams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
            TypeVar a = (TypeVar)tvar.type;
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   459
            a.tsym.flags_field |= UNATTRIBUTED;
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   460
            a.bound = Type.noType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            if (!tvar.bounds.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                List<Type> bounds = List.of(attribType(tvar.bounds.head, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                for (JCExpression bound : tvar.bounds.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                    bounds = bounds.prepend(attribType(bound, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                types.setBounds(a, bounds.reverse());
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                // if no bounds are given, assume a single bound of
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
                // java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                types.setBounds(a, List.of(syms.objectType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            }
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   471
            a.tsym.flags_field &= ~UNATTRIBUTED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        for (JCTypeParameter tvar : typarams)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        attribStats(typarams, env);
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   476
    }
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   477
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   478
    void attribBounds(List<JCTypeParameter> typarams) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        for (JCTypeParameter typaram : typarams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            Type bound = typaram.type.getUpperBound();
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            if (bound != null && bound.tsym instanceof ClassSymbol) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                ClassSymbol c = (ClassSymbol)bound.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                if ((c.flags_field & COMPOUND) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                    assert (c.flags_field & UNATTRIBUTED) != 0 : c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                    attribClass(typaram.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
     * Attribute the type references in a list of annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    void attribAnnotationTypes(List<JCAnnotation> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                               Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            JCAnnotation a = al.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            attribType(a.annotationType, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    /** Attribute type reference in an `extends' or `implements' clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     *  @param tree              The tree making up the type reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
     *  @param env               The environment current at the reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
     *  @param classExpected     true if only a class is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
     *  @param interfaceExpected true if only an interface is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    Type attribBase(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                    Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                    boolean classExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                    boolean interfaceExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                    boolean checkExtensible) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        Type t = attribType(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible);
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    Type checkBase(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                   JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                   Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                   boolean classExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                   boolean interfaceExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                   boolean checkExtensible) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
            // check that type variable is already visible
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            if (t.getUpperBound() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                log.error(tree.pos(), "illegal.forward.ref");
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   527
                return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics);
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            log.error(tree.pos(), "intf.expected.here");
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            // return errType is necessary since otherwise there might
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            // be undetected cycles which cause attribution to loop
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   536
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        } else if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                   classExpected &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                   (t.tsym.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            log.error(tree.pos(), "no.intf.expected.here");
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   541
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
            ((t.tsym.flags() & FINAL) != 0)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                      "cant.inherit.from.final", t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        chk.checkNonCyclic(tree.pos(), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        // Local classes have not been entered yet, so we need to do it now:
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        if ((env.info.scope.owner.kind & (VAR | MTH)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            enter.classEnter(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        ClassSymbol c = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        if (c == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            // exit in case something drastic went wrong during enter.
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            // make sure class has been completed:
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            c.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            // If this class appears as an anonymous class
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
            // in a superclass constructor call where
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
            // no explicit outer instance is given,
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
            // disable implicit outer instance from being passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
            // (This would be an illegal access to "this before super").
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
            if (env.info.isSelfCall &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                env.tree.getTag() == JCTree.NEWCLASS &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                ((JCNewClass) env.tree).encl == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                c.flags_field |= NOOUTERTHIS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            attribClass(tree.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            result = tree.type = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        MethodSymbol m = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        Lint lint = env.info.lint.augment(m.attributes_field, m.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        Lint prevLint = chk.setLint(lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
            chk.checkDeprecatedAnnotation(tree.pos(), m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   589
            attribBounds(tree.typarams);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            // If we override any other methods, check that we do so properly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            // JLS ???
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            chk.checkOverride(tree, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            // Create a new environment with local scope
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            // for attributing the method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            localEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            // Enter all type parameters into the local method scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
                localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
            ClassSymbol owner = env.enclClass.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            if ((owner.flags() & ANNOTATION) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
                tree.params.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                log.error(tree.params.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                          "intf.annotation.members.cant.have.params");
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
            // Attribute all value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
            for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
                attribStat(l.head, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            // Check that type parameters are well-formed.
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
   617
            chk.validate(tree.typarams, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            if ((owner.flags() & ANNOTATION) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                tree.typarams.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                log.error(tree.typarams.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                          "intf.annotation.members.cant.have.type.params");
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            // Check that result type is well-formed.
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
   624
            chk.validate(tree.restype, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            if ((owner.flags() & ANNOTATION) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                chk.validateAnnotationType(tree.restype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            if ((owner.flags() & ANNOTATION) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
                chk.validateAnnotationMethod(tree.pos(), m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            // Check that all exceptions mentioned in the throws clause extend
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            // java.lang.Throwable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
            if ((owner.flags() & ANNOTATION) != 0 && tree.thrown.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                log.error(tree.thrown.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
                          "throws.not.allowed.in.intf.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            if (tree.body == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
                // Empty bodies are only allowed for
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                // abstract, native, or interface methods, or for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
                // in a retrofit signature class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                if ((owner.flags() & INTERFACE) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                    (tree.mods.flags & (ABSTRACT | NATIVE)) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                    !relax)
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
                    log.error(tree.pos(), "missing.meth.body.or.decl.abstract");
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
                if (tree.defaultValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                    if ((owner.flags() & ANNOTATION) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
                        log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
                                  "default.allowed.in.intf.annotation.member");
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            } else if ((owner.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                log.error(tree.body.pos(), "intf.meth.cant.have.body");
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
            } else if ((tree.mods.flags & ABSTRACT) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                log.error(tree.pos(), "abstract.meth.cant.have.body");
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
            } else if ((tree.mods.flags & NATIVE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
                log.error(tree.pos(), "native.meth.cant.have.body");
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                // Add an implicit super() call unless an explicit call to
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                // super(...) or this(...) is given
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                // or we are compiling class java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
                if (tree.name == names.init && owner.type != syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                    JCBlock body = tree.body;
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                    if (body.stats.isEmpty() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
                        !TreeInfo.isSelfCall(body.stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                        body.stats = body.stats.
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                            prepend(memberEnter.SuperCall(make.at(body.pos),
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
                                                          List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                                                          List.<JCVariableDecl>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                                                          false));
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
                    } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
                               (tree.mods.flags & GENERATEDCONSTR) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
                               TreeInfo.isSuperCall(body.stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
                        // enum constructors are not allowed to call super
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                        // directly, so make sure there aren't any super calls
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
                        // in enum constructors, except in the compiler
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
                        // generated one.
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
                        log.error(tree.body.stats.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
                                  "call.to.super.not.allowed.in.enum.ctor",
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
                                  env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
                // Attribute method body.
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
                attribStat(tree.body, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            localEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            result = tree.type = m.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
            chk.validateAnnotations(tree.mods.annotations, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
            chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
        // Local variables have not been entered yet, so we need to do it now:
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
        if (env.info.scope.owner.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            if (tree.sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
                // parameters have already been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
                env.info.scope.enter(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                memberEnter.memberEnter(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
                annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        VarSymbol v = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        Lint lint = env.info.lint.augment(v.attributes_field, v.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        Lint prevLint = chk.setLint(lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
1538
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
   713
        // Check that the variable's declared type is well-formed.
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
   714
        chk.validate(tree.vartype, env);
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
   715
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            chk.checkDeprecatedAnnotation(tree.pos(), v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
            if (tree.init != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                    // In this case, `v' is final.  Ensure that it's initializer is
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                    // evaluated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                    v.getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
                    // Attribute initializer in a new environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
                    // with the declared variable as owner.
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
                    // Check that initializer conforms to variable's declared type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
                    Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
                    initEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
                    // In order to catch self-references, we set the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
                    // declaration position to maximal possible value, effectively
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
                    // marking the variable as undefined.
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
   733
                    initEnv.info.enclVar = v;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
                    attribExpr(tree.init, initEnv, v.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
            result = tree.type = v.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            chk.validateAnnotations(tree.mods.annotations, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
            chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
    public void visitSkip(JCSkip tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
    public void visitBlock(JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        if (env.info.scope.owner.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
            // Block is a static or instance initializer;
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            // let the owner of the environment be a freshly
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
            // created BLOCK-method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
                env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            localEnv.info.scope.owner =
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
                new MethodSymbol(tree.flags | BLOCK, names.empty, null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
                                 env.info.scope.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
            if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            attribStats(tree.stats, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            // Create a new local environment with a local scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                env.dup(tree, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            attribStats(tree.stats, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            localEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
            env.dup(env.tree, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        attribStats(tree.init, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
        if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        loopEnv.tree = tree; // before, we were not in loop!
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
        attribStats(tree.step, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
        attribStat(tree.body, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        loopEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
            env.dup(env.tree, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
        attribStat(tree.var, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
        Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv));
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
        chk.checkNonVoid(tree.pos(), exprType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        if (elemtype == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            // or perhaps expr implements Iterable<T>?
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            Type base = types.asSuper(exprType, syms.iterableType.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
            if (base == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                log.error(tree.expr.pos(), "foreach.not.applicable.to.type");
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
   807
                elemtype = types.createErrorType(exprType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                List<Type> iterableParams = base.allparams();
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                elemtype = iterableParams.isEmpty()
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
                    ? syms.objectType
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
                    : types.upperBound(iterableParams.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        loopEnv.tree = tree; // before, we were not in loop!
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
        attribStat(tree.body, loopEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        loopEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
    public void visitLabelled(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
        // Check that label is not used in an enclosing statement
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
        Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
        while (env1 != null && env1.tree.getTag() != JCTree.CLASSDEF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
            if (env1.tree.getTag() == JCTree.LABELLED &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                ((JCLabeledStatement) env1.tree).label == tree.label) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                log.error(tree.pos(), "label.already.in.use",
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
                          tree.label);
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
    public void visitSwitch(JCSwitch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
        Type seltype = attribExpr(tree.selector, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
        Env<AttrContext> switchEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            env.dup(tree, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
        boolean enumSwitch =
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
            allowEnums &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            (seltype.tsym.flags() & Flags.ENUM) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        if (!enumSwitch)
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
            seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        // Attribute all cases and
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
        // check that there are no duplicate case labels or default clauses.
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        Set<Object> labels = new HashSet<Object>(); // The set of case labels.
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        boolean hasDefault = false;      // Is there a default label?
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
            JCCase c = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            Env<AttrContext> caseEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
                switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            if (c.pat != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                if (enumSwitch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                    Symbol sym = enumConstant(c.pat, seltype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                    if (sym == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                        log.error(c.pat.pos(), "enum.const.req");
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                    } else if (!labels.add(sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                        log.error(c.pos(), "duplicate.case.label");
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                    Type pattype = attribExpr(c.pat, switchEnv, seltype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                    if (pattype.tag != ERROR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
                        if (pattype.constValue() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
                            log.error(c.pat.pos(), "const.expr.req");
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
                        } else if (labels.contains(pattype.constValue())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
                            log.error(c.pos(), "duplicate.case.label");
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                            labels.add(pattype.constValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
            } else if (hasDefault) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                log.error(c.pos(), "duplicate.default.label");
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                hasDefault = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
            attribStats(c.stats, caseEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            caseEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
            addVars(c.stats, switchEnv.info.scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        switchEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
        /** Add any variables defined in stats to the switch scope. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
        private static void addVars(List<JCStatement> stats, Scope switchScope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            for (;stats.nonEmpty(); stats = stats.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
                JCTree stat = stats.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                if (stat.getTag() == JCTree.VARDEF)
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
                    switchScope.enter(((JCVariableDecl) stat).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
    /** Return the selected enumeration constant symbol, or null. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
    private Symbol enumConstant(JCTree tree, Type enumType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
        if (tree.getTag() != JCTree.IDENT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
            return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
        JCIdent ident = (JCIdent)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
        Name name = ident.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
        for (Scope.Entry e = enumType.tsym.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
             e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            if (e.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
                Symbol s = ident.sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                ident.type = s.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                return ((s.flags_field & Flags.ENUM) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                    ? null : s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
        chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
        attribStat(tree.body, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
    public void visitTry(JCTry tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        // Attribute body
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
        attribStat(tree.body, env.dup(tree, env.info.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
        // Attribute catch clauses
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
            JCCatch c = l.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
            Env<AttrContext> catchEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                env.dup(c, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
            Type ctype = attribStat(c.param, catchEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
            if (c.param.type.tsym.kind == Kinds.VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
                c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
            chk.checkType(c.param.vartype.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                          chk.checkClassType(c.param.vartype.pos(), ctype),
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                          syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
            attribStat(c.body, catchEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
            catchEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
        // Attribute finalizer
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
        if (tree.finalizer != null) attribStat(tree.finalizer, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
    public void visitConditional(JCConditional tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
        attribExpr(tree.truepart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
        attribExpr(tree.falsepart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        result = check(tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
                       capture(condType(tree.pos(), tree.cond.type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                                        tree.truepart.type, tree.falsepart.type)),
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                       VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
        /** Compute the type of a conditional expression, after
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
         *  checking that it exists. See Spec 15.25.
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
         *  @param pos      The source position to be used for
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
         *                  error diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
         *  @param condtype The type of the expression's condition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
         *  @param thentype The type of the expression's then-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
         *  @param elsetype The type of the expression's else-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
        private Type condType(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                              Type condtype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                              Type thentype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                              Type elsetype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
            Type ctype = condType1(pos, condtype, thentype, elsetype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
            // If condition and both arms are numeric constants,
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            // evaluate at compile-time.
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
            return ((condtype.constValue() != null) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
                    (thentype.constValue() != null) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                    (elsetype.constValue() != null))
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                ? cfolder.coerce(condtype.isTrue()?thentype:elsetype, ctype)
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                : ctype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
        /** Compute the type of a conditional expression, after
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
         *  checking that it exists.  Does not take into
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
         *  account the special case where condition and both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
         *  are constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
         *  @param pos      The source position to be used for error
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
         *                  diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
         *  @param condtype The type of the expression's condition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
         *  @param thentype The type of the expression's then-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
         *  @param elsetype The type of the expression's else-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
        private Type condType1(DiagnosticPosition pos, Type condtype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                               Type thentype, Type elsetype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
            // If same type, that is the result
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
            if (types.isSameType(thentype, elsetype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
            Type thenUnboxed = (!allowBoxing || thentype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                ? thentype : types.unboxedType(thentype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                ? elsetype : types.unboxedType(elsetype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
            // Otherwise, if both arms can be converted to a numeric
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
            // type, return the least numeric type that fits both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
            // (i.e. return larger of the two, or return int if one
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
            // arm is short, the other is char).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
            if (thenUnboxed.isPrimitive() && elseUnboxed.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                // If one arm has an integer subrange type (i.e., byte,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                // short, or char), and the other is an integer constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
                // that fits into the subrange, return the subrange type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
                if (thenUnboxed.tag < INT && elseUnboxed.tag == INT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
                    types.isAssignable(elseUnboxed, thenUnboxed))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                    return thenUnboxed.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
                if (elseUnboxed.tag < INT && thenUnboxed.tag == INT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                    types.isAssignable(thenUnboxed, elseUnboxed))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
                    return elseUnboxed.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
                for (int i = BYTE; i < VOID; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
                    Type candidate = syms.typeOfTag[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                    if (types.isSubtype(thenUnboxed, candidate) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                        types.isSubtype(elseUnboxed, candidate))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                        return candidate;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
            // Those were all the cases that could result in a primitive
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
            if (allowBoxing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                if (thentype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                    thentype = types.boxedClass(thentype).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                if (elsetype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                    elsetype = types.boxedClass(elsetype).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
            if (types.isSubtype(thentype, elsetype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                return elsetype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
            if (types.isSubtype(elsetype, thentype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
            if (!allowBoxing || thentype.tag == VOID || elsetype.tag == VOID) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                log.error(pos, "neither.conditional.subtype",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                          thentype, elsetype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
            // both are known to be reference types.  The result is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
            // lub(thentype,elsetype). This cannot fail, as it will
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
            // always be possible to infer "Object" if nothing better.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
            return types.lub(thentype.baseType(), elsetype.baseType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
    public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
        attribStat(tree.thenpart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
        if (tree.elsepart != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            attribStat(tree.elsepart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
        chk.checkEmptyIf(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
    public void visitExec(JCExpressionStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        attribExpr(tree.expr, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
    public void visitBreak(JCBreak tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
    public void visitContinue(JCContinue tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
        /** Return the target of a break or continue statement, if it exists,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
         *  report an error if not.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
         *  Note: The target of a labelled break or continue is the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
         *  (non-labelled) statement tree referred to by the label,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
         *  not the tree representing the labelled statement itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
         *  @param pos     The position to be used for error diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
         *  @param tag     The tag of the jump statement. This is either
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
         *                 Tree.BREAK or Tree.CONTINUE.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
         *  @param label   The label of the jump statement, or null if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
         *                 label is given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
         *  @param env     The environment current at the jump statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
        private JCTree findJumpTarget(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
                                    int tag,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
                                    Name label,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
                                    Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
            // Search environments outwards from the point of jump.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
            LOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
            while (env1 != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                switch (env1.tree.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                case JCTree.LABELLED:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
                    JCLabeledStatement labelled = (JCLabeledStatement)env1.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
                    if (label == labelled.label) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                        // If jump is a continue, check that target is a loop.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
                        if (tag == JCTree.CONTINUE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                            if (labelled.body.getTag() != JCTree.DOLOOP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
                                labelled.body.getTag() != JCTree.WHILELOOP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                                labelled.body.getTag() != JCTree.FORLOOP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
                                labelled.body.getTag() != JCTree.FOREACHLOOP)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                                log.error(pos, "not.loop.label", label);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                            // Found labelled statement target, now go inwards
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                            // to next non-labelled tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                            return TreeInfo.referencedStatement(labelled);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
                        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
                            return labelled;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
                case JCTree.DOLOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                case JCTree.WHILELOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                case JCTree.FORLOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                case JCTree.FOREACHLOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                    if (label == null) return env1.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
                case JCTree.SWITCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                    if (label == null && tag == JCTree.BREAK) return env1.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                case JCTree.METHODDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
                case JCTree.CLASSDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
                    break LOOP;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
                env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
            if (label != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                log.error(pos, "undef.label", label);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
            else if (tag == JCTree.CONTINUE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
                log.error(pos, "cont.outside.loop");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
                log.error(pos, "break.outside.switch.loop");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
        // Check that there is an enclosing method which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
        // nested within than the enclosing class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
        if (env.enclMethod == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
            env.enclMethod.sym.owner != env.enclClass.sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
            log.error(tree.pos(), "ret.outside.meth");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            // Attribute return expression, if it exists, and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            // it conforms to result type of enclosing method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
            Symbol m = env.enclMethod.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            if (m.type.getReturnType().tag == VOID) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
                if (tree.expr != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
                    log.error(tree.expr.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
                              "cant.ret.val.from.meth.decl.void");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            } else if (tree.expr == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
                log.error(tree.pos(), "missing.ret.val");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
                attribExpr(tree.expr, env, m.type.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
    public void visitThrow(JCThrow tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        attribExpr(tree.expr, env, syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
    public void visitAssert(JCAssert tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
        if (tree.detail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
            chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
     /** Visitor method for method invocations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
     *  NOTE: The method part of an application will have in its type field
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
     *        the return type of the method, not the method's type itself!
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
        // The local environment of a method application is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        // The types of the actual method arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
        List<Type> argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
        // The types of the actual method type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
        List<Type> typeargtypes = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
        Name methName = TreeInfo.name(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
        boolean isConstructorCall =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
            methName == names._this || methName == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
        if (isConstructorCall) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
            // We are seeing a ...this(...) or ...super(...) call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
            // Check that this is the first statement in a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            if (checkFirstConstructorStat(tree, env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
                // Record the fact
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                // that this is a constructor call (using isSelfCall).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                localEnv.info.isSelfCall = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
                // Attribute arguments, yielding list of argument types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
                argtypes = attribArgs(tree.args, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
                typeargtypes = attribTypes(tree.typeargs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
                // Variable `site' points to the class in which the called
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
                // constructor is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
                Type site = env.enclClass.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
                if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
                    if (site == syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
                        log.error(tree.meth.pos(), "no.superclass", site);
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1222
                        site = types.createErrorType(syms.objectType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
                        site = types.supertype(site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
                if (site.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
                    if (site.getEnclosingType().tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
                        // we are calling a nested class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
                        if (tree.meth.getTag() == JCTree.SELECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
                            JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
                            // We are seeing a prefixed call, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
                            //     <expr>.super(...).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
                            // Check that the prefix expression conforms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
                            // to the outer instance type of the class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
                            chk.checkRefType(qualifier.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
                                             attribExpr(qualifier, localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
                                                        site.getEnclosingType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
                        } else if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
                            // qualifier omitted; check for existence
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
                            // of an appropriate implicit qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
                            rs.resolveImplicitThis(tree.meth.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
                                                   localEnv, site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
                    } else if (tree.meth.getTag() == JCTree.SELECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
                        log.error(tree.meth.pos(), "illegal.qual.not.icls",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
                                  site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
                    // if we're calling a java.lang.Enum constructor,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
                    // prefix the implicit String and int parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
                    if (site.tsym == syms.enumSym && allowEnums)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
                        argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
                    // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
                    // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
                    // current instance (JLS ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
                    boolean selectSuperPrev = localEnv.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
                    localEnv.info.selectSuper = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
                    localEnv.info.varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
                    Symbol sym = rs.resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
                        tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
                    localEnv.info.selectSuper = selectSuperPrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
                    // Set method symbol to resolved constructor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
                    TreeInfo.setSymbol(tree.meth, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
                    // ...and check that it is legal in the current context.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
                    // (this will also set the tree's type)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
                    Type mpt = newMethTemplate(argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
                    checkId(tree.meth, site, sym, localEnv, MTH,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
                            mpt, tree.varargsElement != null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
                // Otherwise, `site' is an error type and we do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
            result = tree.type = syms.voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
            // Otherwise, we are seeing a regular method call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
            // Attribute the arguments, yielding list of argument types, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
            argtypes = attribArgs(tree.args, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
            typeargtypes = attribTypes(tree.typeargs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
            // ... and attribute the method using as a prototype a methodtype
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
            // whose formal argument types is exactly the list of actual
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
            // arguments (this will also set the method symbol).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
            Type mpt = newMethTemplate(argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
            localEnv.info.varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
            Type mtype = attribExpr(tree.meth, localEnv, mpt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
            if (localEnv.info.varArgs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
                assert mtype.isErroneous() || tree.varargsElement != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
            // Compute the result type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
            Type restype = mtype.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            assert restype.tag != WILDCARD : mtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
            // as a special case, array.clone() has a result that is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            // the same as static type of the array being cloned
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
            if (tree.meth.getTag() == JCTree.SELECT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
                allowCovariantReturns &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
                methName == names.clone &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
                types.isArray(((JCFieldAccess) tree.meth).selected.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
                restype = ((JCFieldAccess) tree.meth).selected.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
            // as a special case, x.getClass() has type Class<? extends |X|>
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
            if (allowGenerics &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
                methName == names.getClass && tree.args.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
                Type qualifier = (tree.meth.getTag() == JCTree.SELECT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
                    ? ((JCFieldAccess) tree.meth).selected.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
                    : env.enclClass.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
                restype = new
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
                    ClassType(restype.getEnclosingType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
                              List.<Type>of(new WildcardType(types.erasure(qualifier),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
                                                               BoundKind.EXTENDS,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
                                                               syms.boundClass)),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
                              restype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
            // Check that value of resulting type is admissible in the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
            // current context.  Also, capture the return type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
            result = check(tree, capture(restype), VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
        }
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1325
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
        /** Check that given application node appears as first statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
         *  in a constructor call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
         *  @param tree   The application node
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
         *  @param env    The environment current at the application.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
        boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
            JCMethodDecl enclMethod = env.enclMethod;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
            if (enclMethod != null && enclMethod.name == names.init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
                JCBlock body = enclMethod.body;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
                if (body.stats.head.getTag() == JCTree.EXEC &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
                    ((JCExpressionStatement) body.stats.head).expr == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
            log.error(tree.pos(),"call.must.be.first.stmt.in.ctor",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                      TreeInfo.name(tree.meth));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
        /** Obtain a method type with given argument types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
        Type newMethTemplate(List<Type> argtypes, List<Type> typeargtypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
            MethodType mt = new MethodType(argtypes, null, null, syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
            return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
    public void visitNewClass(JCNewClass tree) {
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1354
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
        // The local environment of a class creation is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        // The anonymous inner class definition of the new expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
        // if one is defined by it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
        JCClassDecl cdef = tree.def;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
        // If enclosing class is given, attribute it, and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
        // complete class name to be fully qualified
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
        JCExpression clazz = tree.clazz; // Class field following new
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
        JCExpression clazzid =          // Identifier in class field
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
            (clazz.getTag() == JCTree.TYPEAPPLY)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
            ? ((JCTypeApply) clazz).clazz
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
            : clazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
        JCExpression clazzid1 = clazzid; // The same in fully qualified form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
            // We are seeing a qualified new, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
            //    <expr>.new C <...> (...) ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
            // In this case, we let clazz stand for the name of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
            // allocated class C prefixed with the type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
            // expression, so that we can
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
            // resolve it with standard techniques later. I.e., if
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
            // <expr> has type T, then <expr>.new C <...> (...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            // yields a clazz T.C.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            Type encltype = chk.checkRefType(tree.encl.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                                             attribExpr(tree.encl, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
            clazzid1 = make.at(clazz.pos).Select(make.Type(encltype),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                                                 ((JCIdent) clazzid).name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
            if (clazz.getTag() == JCTree.TYPEAPPLY)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                clazz = make.at(tree.pos).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
                    TypeApply(clazzid1,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                              ((JCTypeApply) clazz).arguments);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
                clazz = clazzid1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
//          System.out.println(clazz + " generated.");//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
        // Attribute clazz expression and store
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
        // symbol + type back into the attributed tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
        Type clazztype = chk.checkClassType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
            tree.clazz.pos(), attribType(clazz, env), true);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1400
        chk.validate(clazz, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
            // We have to work in this case to store
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
            // symbol + type back into the attributed tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
            tree.clazz.type = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
            TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
            clazzid.type = ((JCIdent) clazzid).sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
            if (!clazztype.isErroneous()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                if (cdef != null && clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
                    log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
                } else if (clazztype.tsym.isStatic()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                    log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
        } else if (!clazztype.tsym.isInterface() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
                   clazztype.getEnclosingType().tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
            // Check for the existence of an apropos outer instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
            rs.resolveImplicitThis(tree.pos(), env, clazztype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
        // Attribute constructor arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
        List<Type> argtypes = attribArgs(tree.args, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
        List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
        // If we have made no mistakes in the class type...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
        if (clazztype.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
            // Enums may not be instantiated except implicitly
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
            if (allowEnums &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
                (clazztype.tsym.flags_field&Flags.ENUM) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
                (env.tree.getTag() != JCTree.VARDEF ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                 (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                 ((JCVariableDecl) env.tree).init != tree))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                log.error(tree.pos(), "enum.cant.be.instantiated");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
            // Check that class is not abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
            if (cdef == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
                (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
                log.error(tree.pos(), "abstract.cant.be.instantiated",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
                          clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
            } else if (cdef != null && clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
                // Check that no constructor arguments are given to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
                // anonymous classes implementing an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
                if (!argtypes.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
                    log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
                if (!typeargtypes.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                    log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
                // Error recovery: pretend no arguments were supplied.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
                argtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
                typeargtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
            // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
            // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
            // current instance (JLS ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
                localEnv.info.selectSuper = cdef != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
                localEnv.info.varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
                tree.constructor = rs.resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
                    tree.pos(), localEnv, clazztype, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
                Type ctorType = checkMethod(clazztype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
                                            tree.constructor,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
                                            localEnv,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
                                            tree.args,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
                                            argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
                                            typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
                                            localEnv.info.varArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
                if (localEnv.info.varArgs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
                    assert ctorType.isErroneous() || tree.varargsElement != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
            if (cdef != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
                // We are seeing an anonymous class instance creation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
                // In this case, the class instance creation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
                // expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
                //    E.new <typeargs1>C<typargs2>(args) { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
                // is represented internally as
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
                //    E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } )  .
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
                // This expression is then *transformed* as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                // (1) add a STATIC flag to the class definition
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                //     if the current environment is static
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                // (2) add an extends or implements clause
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                // (3) add a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                // For instance, if C is a class, and ET is the type of E,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                // the expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
                //    E.new <typeargs1>C<typargs2>(args) { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                // is translated to (where X is a fresh name and typarams is the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                // parameter list of the super constructor):
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                //   new <typeargs1>X(<*nullchk*>E, args) where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                //     X extends C<typargs2> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
                //       <typarams> X(ET e, args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                //         e.<typeargs1>super(args)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                //       }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                //       ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                //     }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                if (clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                    cdef.implementing = List.of(clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
                    cdef.extending = clazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                attribStat(cdef, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
                // If an outer instance is given,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                // prefix it to the constructor arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
                // and delete it from the new expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
                if (tree.encl != null && !clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
                    tree.args = tree.args.prepend(makeNullCheck(tree.encl));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
                    argtypes = argtypes.prepend(tree.encl.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
                    tree.encl = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
                // Reassign clazztype and recompute constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
                clazztype = cdef.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
                Symbol sym = rs.resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
                    tree.pos(), localEnv, clazztype, argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
                    typeargtypes, true, tree.varargsElement != null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
                assert sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
                tree.constructor = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
            if (tree.constructor != null && tree.constructor.kind == MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
                owntype = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
        result = check(tree, owntype, VAL, pkind, pt);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1536
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
    /** Make an attributed null check tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
    public JCExpression makeNullCheck(JCExpression arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
        // optimization: X.this is never null; skip null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
        Name name = TreeInfo.name(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
        if (name == names._this || name == names._super) return arg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
        int optag = JCTree.NULLCHK;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
        JCUnary tree = make.at(arg.pos).Unary(optag, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
        tree.operator = syms.nullcheck;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
        tree.type = arg.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
    public void visitNewArray(JCNewArray tree) {
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1554
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
        Type elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
        if (tree.elemtype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
            elemtype = attribType(tree.elemtype, env);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1558
            chk.validate(tree.elemtype, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
            owntype = elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
            for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
                attribExpr(l.head, env, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
                owntype = new ArrayType(owntype, syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
            // we are seeing an untyped aggregate { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
            // this is allowed only if the prototype is an array
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
            if (pt.tag == ARRAY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
                elemtype = types.elemtype(pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
                if (pt.tag != ERROR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
                    log.error(tree.pos(), "illegal.initializer.for.type",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
                              pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
                }
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1574
                elemtype = types.createErrorType(pt);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
        if (tree.elems != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
            attribExprs(tree.elems, env, elemtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
            owntype = new ArrayType(elemtype, syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
        if (!types.isReifiable(elemtype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
            log.error(tree.pos(), "generic.array.creation");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
        result = check(tree, owntype, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
    public void visitParens(JCParens tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
        Type owntype = attribTree(tree.expr, env, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
        result = check(tree, owntype, pkind, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
        if (sym != null && (sym.kind&(TYP|PCK)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
            log.error(tree.pos(), "illegal.start.of.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
    public void visitAssign(JCAssign tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
        Type owntype = attribTree(tree.lhs, env.dup(tree), VAR, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
        Type capturedType = capture(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
        attribExpr(tree.rhs, env, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
        result = check(tree, capturedType, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
        // Attribute arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
        Type owntype = attribTree(tree.lhs, env, VAR, Type.noType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
        Type operand = attribExpr(tree.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
        Symbol operator = tree.operator = rs.resolveBinaryOperator(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
            tree.pos(), tree.getTag() - JCTree.ASGOffset, env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
            owntype, operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        if (operator.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
            chk.checkOperator(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
                              (OperatorSymbol)operator,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
                              tree.getTag() - JCTree.ASGOffset,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
                              owntype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
                              operand);
166
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  1616
            chk.checkDivZero(tree.rhs.pos(), operator, operand);
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  1617
            chk.checkCastable(tree.rhs.pos(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  1618
                              operator.type.getReturnType(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  1619
                              owntype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
        result = check(tree, owntype, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
        // Attribute arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
        Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
            ? attribTree(tree.arg, env, VAR, Type.noType)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
            : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
        Symbol operator = tree.operator =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
            rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1634
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
        if (operator.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
            owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
                ? tree.arg.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
                : operator.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
            int opc = ((OperatorSymbol)operator).opcode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
            // If the argument is constant, fold it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
            if (argtype.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
                Type ctype = cfolder.fold1(opc, argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
                    // Remove constant types from arguments to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
                    // conserve space. The parser will fold concatenations
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
                    // of string literals; the code here also
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
                    // gets rid of intermediate results when some of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                    // operands are constant identifiers.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
                    if (tree.arg.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
                        tree.arg.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
        result = check(tree, owntype, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
        // Attribute arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
        Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
        Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
        Symbol operator = tree.operator =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
            rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1670
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
        if (operator.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
            owntype = operator.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
            int opc = chk.checkOperator(tree.lhs.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
                                        (OperatorSymbol)operator,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
                                        tree.getTag(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
                                        left,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
                                        right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
            // If both arguments are constants, fold them.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
            if (left.constValue() != null && right.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
                Type ctype = cfolder.fold2(opc, left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
                    // Remove constant types from arguments to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
                    // conserve space. The parser will fold concatenations
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
                    // of string literals; the code here also
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
                    // gets rid of intermediate results when some of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
                    // operands are constant identifiers.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
                    if (tree.lhs.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
                        tree.lhs.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
                    if (tree.rhs.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
                        tree.rhs.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
            // Check that argument types of a reference ==, != are
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
            // castable to each other, (JLS???).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
            if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
                if (!types.isCastable(left, right, new Warner(tree.pos()))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
                    log.error(tree.pos(), "incomparable.types", left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
            chk.checkDivZero(tree.rhs.pos(), operator, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        result = check(tree, owntype, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
    public void visitTypeCast(JCTypeCast tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
        Type clazztype = attribType(tree.clazz, env);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1714
        chk.validate(tree.clazz, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
        Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
        Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        if (exprtype.constValue() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
            owntype = cfolder.coerce(exprtype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
        result = check(tree, capture(owntype), VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
        Type exprtype = chk.checkNullOrRefType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
            tree.expr.pos(), attribExpr(tree.expr, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
        Type clazztype = chk.checkReifiableReferenceType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
            tree.clazz.pos(), attribType(tree.clazz, env));
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1727
        chk.validate(tree.clazz, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
        chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
        result = check(tree, syms.booleanType, VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
    public void visitIndexed(JCArrayAccess tree) {
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1733
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
        Type atype = attribExpr(tree.indexed, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
        attribExpr(tree.index, env, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
        if (types.isArray(atype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
            owntype = types.elemtype(atype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
        else if (atype.tag != ERROR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
            log.error(tree.pos(), "array.req.but.found", atype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
        if ((pkind & VAR) == 0) owntype = capture(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
        result = check(tree, owntype, VAR, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
        boolean varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
        // Find symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
  1749
        if (pt.tag == METHOD || pt.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
            // If we are looking for a method, the prototype `pt' will be a
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
            // method type with the type of the call's arguments as parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
            env.info.varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
            sym = rs.resolveMethod(tree.pos(), env, tree.name, pt.getParameterTypes(), pt.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
            varArgs = env.info.varArgs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
        } else if (tree.sym != null && tree.sym.kind != VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
            sym = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
            sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
        // (1) Also find the environment current for the class where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
        //     sym is defined (`symEnv').
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
        // Only for pre-tiger versions (1.4 and earlier):
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
        // (2) Also determine whether we access symbol out of an anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
        //     class in a this or super call.  This is illegal for instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
        //     members since such classes don't carry a this$n link.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
        //     (`noOuterThisPath').
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
        Env<AttrContext> symEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
        boolean noOuterThisPath = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
        if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
            (sym.kind & (VAR | MTH | TYP)) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
            tree.name != names._this && tree.name != names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
            // Find environment in which identifier is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
            while (symEnv.outer != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
                   !sym.isMemberOf(symEnv.enclClass.sym, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
                if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
                    noOuterThisPath = !allowAnonOuterThis;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
                symEnv = symEnv.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
        // If symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
            checkInit(tree, env, v, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
            // If symbol is a local variable accessed from an embedded
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
            // inner class check that it is final.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
            if (v.owner.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
                v.owner != env.info.scope.owner &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
                (v.flags_field & FINAL) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
                log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
                          "local.var.accessed.from.icls.needs.final",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
                          v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
            // that the variable is assignable in the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
            if (pkind == VAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
                checkAssignable(tree.pos(), v, null, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
        // In a constructor body,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
        // if symbol is a field or instance method, check that it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
        // not accessed before the supertype constructor is called.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
        if ((symEnv.info.isSelfCall || noOuterThisPath) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1813
            (sym.kind & (VAR | MTH)) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
            (sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
            chk.earlyRefError(tree.pos(), sym.kind == VAR ? sym : thisSym(tree.pos(), env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
        Env<AttrContext> env1 = env;
512
53e498fa5c0e 6657499: javac 1.6.0 fails to compile class with inner class
mcimadamore
parents: 511
diff changeset
  1819
        if (sym.kind != ERR && sym.kind != TYP && sym.owner != null && sym.owner != env1.enclClass.sym) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
            // If the found symbol is inaccessible, then it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
            // accessed through an enclosing instance.  Locate this
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
            // enclosing instance:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
            while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1824
                env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
        result = checkId(tree, env1.enclClass.sym.type, sym, env, pkind, pt, varArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
        // Determine the expected kind of the qualifier expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
        int skind = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
        if (tree.name == names._this || tree.name == names._super ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
            tree.name == names._class)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
            skind = TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
            if ((pkind & PCK) != 0) skind = skind | PCK;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
            if ((pkind & TYP) != 0) skind = skind | TYP | PCK;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
            if ((pkind & (VAL | MTH)) != 0) skind = skind | VAL | TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
        // Attribute the qualifier expression, and determine its symbol (if any).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
        Type site = attribTree(tree.selected, env, skind, Infer.anyPoly);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1844
        if ((pkind & (PCK | TYP)) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
            site = capture(site); // Capture field access
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
06bc494ca11e Initial load
duke
parents:
diff changeset
  1847
        // don't allow T.class T[].class, etc
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
        if (skind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
            Type elt = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
            while (elt.tag == ARRAY)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
                elt = ((ArrayType)elt).elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
            if (elt.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
                log.error(tree.pos(), "type.var.cant.be.deref");
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  1854
                result = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
        // If qualifier symbol is a type or `super', assert `selectSuper'
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
        // for the selection. This is relevant for determining whether
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
        // protected symbols are accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1862
        Symbol sitesym = TreeInfo.symbol(tree.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1863
        boolean selectSuperPrev = env.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
        env.info.selectSuper =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
            sitesym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
            sitesym.name == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
        // If selected expression is polymorphic, strip
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
        // type parameters and remember in env.info.tvars, so that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
        // they can be added later (in Attr.checkId and Infer.instantiateMethod).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
        if (tree.selected.type.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
            ForAll pstype = (ForAll)tree.selected.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
            env.info.tvars = pstype.tvars;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
            site = tree.selected.type = pstype.qtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
        // Determine the symbol represented by the selection.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
        env.info.varArgs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
        Symbol sym = selectSym(tree, site, env, pt, pkind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
        if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
            site = capture(site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
            sym = selectSym(tree, site, env, pt, pkind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
        boolean varArgs = env.info.varArgs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
511
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  1887
        if (site.tag == TYPEVAR && !isType(sym) && sym.kind != ERR) {
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  1888
            while (site.tag == TYPEVAR) site = site.getUpperBound();
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  1889
            site = capture(site);
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  1890
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
        // If that symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
            checkInit(tree, env, v, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
            // that the variable is assignable in the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
            if (pkind == VAR)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
                checkAssignable(tree.pos(), v, tree.selected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
        // Disallow selecting a type from an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
        if (isType(sym) && (sitesym==null || (sitesym.kind&(TYP|PCK)) == 0)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
            tree.type = check(tree.selected, pt,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
                              sitesym == null ? VAL : sitesym.kind, TYP|PCK, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
        if (isType(sitesym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
            if (sym.name == names._this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
                // If `C' is the currently compiled class, check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
                // C.this' does not appear in a call to a super(...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
                if (env.info.isSelfCall &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
                    site.tsym == env.enclClass.sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
                    chk.earlyRefError(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
                // Check if type-qualified fields or methods are static (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
                if ((sym.flags() & STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
                    sym.name != names._super &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
                    (sym.kind == VAR || sym.kind == MTH)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
                    rs.access(rs.new StaticError(sym),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
                              tree.pos(), site, sym.name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
        // If we are selecting an instance member via a `super', ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
        if (env.info.selectSuper && (sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
            // Check that super-qualified symbols are not abstract (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
            rs.checkNonAbstract(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
            if (site.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
                // Determine argument types for site.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
                Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
                if (site1 != null) site = site1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
        env.info.selectSuper = selectSuperPrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
        result = checkId(tree, site, sym, env, pkind, pt, varArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
        env.info.tvars = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
        /** Determine symbol referenced by a Select expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
         *  @param tree   The select tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
         *  @param site   The type of the selected expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
         *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
         *  @param pt     The current prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
         *  @param pkind  The expected kind(s) of the Select expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1957
        private Symbol selectSym(JCFieldAccess tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
                                 Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1959
                                 Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
                                 Type pt,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
                                 int pkind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
            DiagnosticPosition pos = tree.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
            Name name = tree.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
            switch (site.tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
            case PACKAGE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
                return rs.access(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
                    rs.findIdentInPackage(env, site.tsym, name, pkind),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
                    pos, site, name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
                if (pt.tag == METHOD || pt.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
                    return rs.resolveQualifiedMethod(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1974
                        pos, env, site, name, pt.getParameterTypes(), pt.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
                } else if (name == names._this || name == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
                    return rs.resolveSelf(pos, env, site.tsym, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
                } else if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
                    // In this case, we have already made sure in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
                    // visitSelect that qualifier expression is a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
                    Type t = syms.classType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
                    List<Type> typeargs = allowGenerics
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
                        ? List.of(types.erasure(site))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
                        : List.<Type>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
                    t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
                    return new VarSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
                        STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
                    // We are seeing a plain identifier as selector.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
                    Symbol sym = rs.findIdentInType(env, site, name, pkind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
                    if ((pkind & ERRONEOUS) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
                        sym = rs.access(sym, pos, site, name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
            case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
                throw new AssertionError(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
            case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
                // Normally, site.getUpperBound() shouldn't be null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
                // It should only happen during memberEnter/attribBase
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
                // when determining the super type which *must* be
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
                // done before attributing the type variables.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
                // other words, we are seeing this illegal program:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
                // class B<T> extends A<T.foo> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
                Symbol sym = (site.getUpperBound() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
                    ? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2005
                    : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
                if (sym == null || isType(sym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
                    log.error(pos, "type.var.cant.be.deref");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
                } else {
1528
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  2010
                    Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  2011
                        rs.new AccessError(env, site, sym) :
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  2012
                                sym;
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  2013
                    rs.access(sym2, pos, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
                // preserve identifier names through errors
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2018
                return types.createErrorType(name, site.tsym, site).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
                // The qualifier expression is of a primitive type -- only
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
                // .class is allowed for these.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
                if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
                    // In this case, we have already made sure in Select that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
                    // qualifier expression is a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
                    Type t = syms.classType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
                    Type arg = types.boxedClass(site).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
                    t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
                    return new VarSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
                        STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
                    log.error(pos, "cant.deref", site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
        /** Determine type of identifier or select expression and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
         *  (1) the referenced symbol is not deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
         *  (2) the symbol's type is safe (@see checkSafe)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
         *  (3) if symbol is a variable, check that its type and kind are
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
         *      compatible with the prototype and protokind.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
         *  (4) if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
         *      which is being assigned to, issue an unchecked warning if its
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
         *      type changes under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
         *  (5) if symbol is an instance method of a raw type, issue an
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
         *      unchecked warning if its argument types change under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
         *  If checks succeed:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
         *    If symbol is a constant, return its constant type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
         *    else if symbol is a method, return its result type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
         *    otherwise return its type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
         *  Otherwise return errType.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
         *  @param tree       The syntax tree representing the identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
         *  @param site       If this is a select, the type of the selected
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
         *                    expression, otherwise the type of the current class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
         *  @param sym        The symbol representing the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
         *  @param env        The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
         *  @param pkind      The set of expected kinds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
         *  @param pt         The expected type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
        Type checkId(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
                     Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
                     Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
                     int pkind,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
                     Type pt,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
                     boolean useVarargs) {
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2068
            if (pt.isErroneous()) return types.createErrorType(site);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
            Type owntype; // The computed type of this identifier occurrence.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
            switch (sym.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
            case TYP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
                // For types, the computed type equals the symbol's type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
                // except for two situations:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
                owntype = sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
                if (owntype.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
                    Type ownOuter = owntype.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
                    // (a) If the symbol's type is parameterized, erase it
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
                    // because no type parameters were given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
                    // We recover generic outer type later in visitTypeApply.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2081
                    if (owntype.tsym.type.getTypeArguments().nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2082
                        owntype = types.erasure(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
                    // (b) If the symbol's type is an inner class, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
                    // we have to interpret its outer type as a superclass
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
                    // of the site type. Example:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
                    // class Tree<A> { class Visitor { ... } }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
                    // class PointTree extends Tree<Point> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
                    // ...PointTree.Visitor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
                    // Then the type of the last expression above is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
                    // Tree<Point>.Visitor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
                    else if (ownOuter.tag == CLASS && site != ownOuter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
                        Type normOuter = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
                        if (normOuter.tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
                            normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
                        if (normOuter == null) // perhaps from an import
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
                            normOuter = types.erasure(ownOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
                        if (normOuter != ownOuter)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
                            owntype = new ClassType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
                                normOuter, List.<Type>nil(), owntype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
            case VAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
                VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
                // Test (4): if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
                // which is being assigned to, issue an unchecked warning if
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
                // its type changes under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
                if (allowGenerics &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
                    pkind == VAR &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
                    v.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
                    (v.flags() & STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
                    (site.tag == CLASS || site.tag == TYPEVAR)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
                    Type s = types.asOuterSuper(site, v.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
                    if (s != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
                        s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
                        !types.isSameType(v.type, v.erasure(types))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
                        chk.warnUnchecked(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
                                          "unchecked.assign.to.var",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
                                          v, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
                // The computed type of a variable is the type of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
                // variable symbol, taken as a member of the site type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
                owntype = (sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
                           sym.name != names._this && sym.name != names._super)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
                    ? types.memberType(site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
                    : sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
                if (env.info.tvars.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
                    Type owntype1 = new ForAll(env.info.tvars, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
                    for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
                        if (!owntype.contains(l.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
                            log.error(tree.pos(), "undetermined.type", owntype1);
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2138
                            owntype1 = types.createErrorType(owntype1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
                    owntype = owntype1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
                // If the variable is a constant, record constant value in
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
                // computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
                if (v.getConstValue() != null && isStaticReference(tree))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
                    owntype = owntype.constType(v.getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
                if (pkind == VAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
                    owntype = capture(owntype); // capture "names as expressions"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
            case MTH: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
                JCMethodInvocation app = (JCMethodInvocation)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
                owntype = checkMethod(site, sym, env, app.args,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
                                      pt.getParameterTypes(), pt.getTypeArguments(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
                                      env.info.varArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
            case PCK: case ERR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
                owntype = sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
                throw new AssertionError("unexpected kind: " + sym.kind +
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
                                         " in tree " + tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
            // Test (1): emit a `deprecation' warning if symbol is deprecated.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
            // (for constructors, the error was given when the constructor was
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
            // resolved)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
            if (sym.name != names.init &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
                (sym.flags() & DEPRECATED) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
                (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
                sym.outermostClass() != env.info.scope.owner.outermostClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
                chk.warnDeprecated(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
            if ((sym.flags() & PROPRIETARY) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
                log.strictWarning(tree.pos(), "sun.proprietary", sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
            // Test (3): if symbol is a variable, check that its type and
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
            // kind are compatible with the prototype and protokind.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
            return check(tree, owntype, sym.kind, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
        /** Check that variable is initialized and evaluate the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
         *  initializer, if not yet done. Also check that variable is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
         *  referenced before it is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2187
         *  @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
         *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
         *  @param v       The variable's symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
        private void checkInit(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
                               Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
                               VarSymbol v,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
                               boolean onlyWarning) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
//          System.err.println(v + " " + ((v.flags() & STATIC) != 0) + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
//                             tree.pos + " " + v.pos + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
//                             Resolve.isStatic(env));//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
            // A forward reference is diagnosed if the declaration position
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
            // of the variable is greater than the current tree position
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
            // and the tree and variable definition occur in the same class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
            // definition.  Note that writes don't count as references.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
            // This check applies only to class and instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  2204
            // variables.  Local variables follow different scope rules,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
            // and are subject to definite assignment checking.
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  2206
            if ((env.info.enclVar == v || v.pos > tree.pos) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
                v.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2208
                canOwnInitializer(env.info.scope.owner) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
                v.owner == env.info.scope.owner.enclClass() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
                ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
                (env.tree.getTag() != JCTree.ASSIGN ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
                 TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) {
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  2213
                String suffix = (env.info.enclVar == v) ?
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  2214
                                "self.ref" : "forward.ref";
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2215
                if (!onlyWarning || isStaticEnumField(v)) {
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  2216
                    log.error(tree.pos(), "illegal." + suffix);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
                } else if (useBeforeDeclarationWarning) {
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  2218
                    log.warning(tree.pos(), suffix, v);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
            v.getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
            checkEnumInitializer(tree, env, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
         * Check for illegal references to static members of enum.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
         * an enum type, constructors and initializers may not
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
         * reference its static members unless they are constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
         * @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
         * @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
         * @param v       The variable's symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
         * @see JLS 3rd Ed. (8.9 Enums)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
        private void checkEnumInitializer(JCTree tree, Env<AttrContext> env, VarSymbol v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
            // JLS 3rd Ed.:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
            //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
            // "It is a compile-time error to reference a static field
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
            // of an enum type that is not a compile-time constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
            // (15.28) from constructors, instance initializer blocks,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
            // or instance variable initializer expressions of that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
            // type. It is a compile-time error for the constructors,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
            // instance initializer blocks, or instance variable
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
            // initializer expressions of an enum constant e to refer
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
            // to itself or to an enum constant of the same type that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
            // is declared to the right of e."
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2249
            if (isStaticEnumField(v)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
                ClassSymbol enclClass = env.info.scope.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
                if (enclClass == null || enclClass.owner == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
                // See if the enclosing class is the enum (or a
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
                // subclass thereof) declaring v.  If not, this
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
                // reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
                if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
                // If the reference isn't from an initializer, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
                // the reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
                if (!Resolve.isInitializer(env))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
                log.error(tree.pos(), "illegal.enum.static.ref");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2270
        /** Is the given symbol a static, non-constant field of an Enum?
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2271
         *  Note: enum literals should not be regarded as such
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2272
         */
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2273
        private boolean isStaticEnumField(VarSymbol v) {
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2274
            return Flags.isEnum(v.owner) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2275
                   Flags.isStatic(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2276
                   !Flags.isConstant(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  2277
                   v.name != names._class;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
        /** Can the given symbol be the owner of code which forms part
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
         *  if class initialization? This is the case if the symbol is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
         *  a type or field, or if the symbol is the synthetic method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
         *  owning a block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
        private boolean canOwnInitializer(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
                (sym.kind & (VAR | TYP)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
                (sym.kind == MTH && (sym.flags() & BLOCK) != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
     * Check that method arguments conform to its instantation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
    public Type checkMethod(Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
                            Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
                            Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
                            final List<JCExpression> argtrees,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
                            List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
                            List<Type> typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
                            boolean useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
        // Test (5): if symbol is an instance method of a raw type, issue
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
        // an unchecked warning if its argument types change under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
        if (allowGenerics &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
            (sym.flags() & STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
            (site.tag == CLASS || site.tag == TYPEVAR)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
            Type s = types.asOuterSuper(site, sym.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
            if (s != null && s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
                !types.isSameTypes(sym.type.getParameterTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
                                   sym.erasure(types).getParameterTypes())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
                chk.warnUnchecked(env.tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
                                  "unchecked.call.mbr.of.raw.type",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
                                  sym, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
        // Compute the identifier's instantiated type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
        // For methods, we need to compute the instance type by
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
        // Resolve.instantiate from the symbol's type as well as
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
        // any type arguments and value arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
        noteWarner.warned = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
        Type owntype = rs.instantiate(env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
                                      site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2325
                                      sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2326
                                      argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
                                      typeargtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
                                      true,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2329
                                      useVarargs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
                                      noteWarner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
        boolean warned = noteWarner.warned;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
        // If this fails, something went wrong; we should not have
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
        // found the identifier in the first place.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
        if (owntype == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
            if (!pt.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
                log.error(env.tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
                          "internal.error.cant.instantiate",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
                          sym, site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
                          Type.toString(pt.getParameterTypes()));
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2341
            owntype = types.createErrorType(site);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2342
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2343
            // System.out.println("call   : " + env.tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
            // System.out.println("method : " + owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
            // System.out.println("actuals: " + argtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
            List<Type> formals = owntype.getParameterTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2347
            Type last = useVarargs ? formals.last() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2348
            if (sym.name==names.init &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
                sym.owner == syms.enumSym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2350
                formals = formals.tail.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
            List<JCExpression> args = argtrees;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
            while (formals.head != last) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
                JCTree arg = args.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
                Warner warn = chk.convertWarner(arg.pos(), arg.type, formals.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
                assertConvertible(arg, arg.type, formals.head, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
                warned |= warn.warned;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
                args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
                formals = formals.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
            if (useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
                Type varArg = types.elemtype(last);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
                while (args.tail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
                    JCTree arg = args.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
                    Warner warn = chk.convertWarner(arg.pos(), arg.type, varArg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
                    assertConvertible(arg, arg.type, varArg, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
                    warned |= warn.warned;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
                    args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
            } else if ((sym.flags() & VARARGS) != 0 && allowVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
                // non-varargs call to varargs method
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
                Type varParam = owntype.getParameterTypes().last();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
                Type lastArg = argtypes.last();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
                if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
                    !types.isSameType(types.erasure(varParam), types.erasure(lastArg)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
                    log.warning(argtrees.last().pos(), "inexact.non-varargs.call",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
                                types.elemtype(varParam),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
                                varParam);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
            if (warned && sym.type.tag == FORALL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
                chk.warnUnchecked(env.tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
                                  "unchecked.meth.invocation.applied",
1534
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2383
                                  kindName(sym),
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2384
                                  sym.name,
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2385
                                  rs.methodArguments(sym.type.getParameterTypes()),
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2386
                                  rs.methodArguments(argtypes),
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2387
                                  kindName(sym.location()),
e923a41e84cc 6758789: Some method resolution diagnostic should be improved
mcimadamore
parents: 1532
diff changeset
  2388
                                  sym.location());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
                owntype = new MethodType(owntype.getParameterTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
                                         types.erasure(owntype.getReturnType()),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2391
                                         owntype.getThrownTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2392
                                         syms.methodClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2393
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2394
            if (useVarargs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
                JCTree tree = env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
                Type argtype = owntype.getParameterTypes().last();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
                if (!types.isReifiable(argtype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
                    chk.warnUnchecked(env.tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
                                      "unchecked.generic.array.creation",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2400
                                      argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
                Type elemtype = types.elemtype(argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
                switch (tree.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
                case JCTree.APPLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2404
                    ((JCMethodInvocation) tree).varargsElement = elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2406
                case JCTree.NEWCLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
                    ((JCNewClass) tree).varargsElement = elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2410
                    throw new AssertionError(""+tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2412
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
        return owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
    private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2418
        if (types.isConvertible(actual, formal, warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2419
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2420
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
        if (formal.isCompound()
06bc494ca11e Initial load
duke
parents:
diff changeset
  2422
            && types.isSubtype(actual, types.supertype(formal))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2423
            && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2424
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2425
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
        if (false) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
            // TODO: make assertConvertible work
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
  2428
            chk.typeError(tree.pos(), diags.fragment("incompatible.types"), actual, formal);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
            throw new AssertionError("Tree: " + tree
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
                                     + " actual:" + actual
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
                                     + " formal: " + formal);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
    public void visitLiteral(JCLiteral tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
        result = check(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
            tree, litType(tree.typetag).constType(tree.value), VAL, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
    /** Return the type of a literal with given type tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2441
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2442
    Type litType(int tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2443
        return (tag == TypeTags.CLASS) ? syms.stringType : syms.typeOfTag[tag];
06bc494ca11e Initial load
duke
parents:
diff changeset
  2444
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2445
06bc494ca11e Initial load
duke
parents:
diff changeset
  2446
    public void visitTypeIdent(JCPrimitiveTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2447
        result = check(tree, syms.typeOfTag[tree.typetag], TYP, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2448
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
    public void visitTypeArray(JCArrayTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2451
        Type etype = attribType(tree.elemtype, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2452
        Type type = new ArrayType(etype, syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2453
        result = check(tree, type, TYP, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
    /** Visitor method for parameterized types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
     *  Bound checking is left until later, since types are attributed
06bc494ca11e Initial load
duke
parents:
diff changeset
  2458
     *  before supertype structure is completely known
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2460
    public void visitTypeApply(JCTypeApply tree) {
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2461
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
        // Attribute functor part of application and make sure it's a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
        Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
06bc494ca11e Initial load
duke
parents:
diff changeset
  2466
        // Attribute type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  2467
        List<Type> actuals = attribTypes(tree.arguments, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
        if (clazztype.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
            List<Type> formals = clazztype.tsym.type.getTypeArguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2471
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
            if (actuals.length() == formals.length()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
                List<Type> a = actuals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
                List<Type> f = formals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
                while (a.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2476
                    a.head = a.head.withTypeVar(f.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
                    a = a.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2478
                    f = f.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2479
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2480
                // Compute the proper generic outer
06bc494ca11e Initial load
duke
parents:
diff changeset
  2481
                Type clazzOuter = clazztype.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2482
                if (clazzOuter.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2483
                    Type site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2484
                    if (tree.clazz.getTag() == JCTree.IDENT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2485
                        site = env.enclClass.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2486
                    } else if (tree.clazz.getTag() == JCTree.SELECT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2487
                        site = ((JCFieldAccess) tree.clazz).selected.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2488
                    } else throw new AssertionError(""+tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2489
                    if (clazzOuter.tag == CLASS && site != clazzOuter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2490
                        if (site.tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2491
                            site = types.asOuterSuper(site, clazzOuter.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2492
                        if (site == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2493
                            site = types.erasure(clazzOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2494
                        clazzOuter = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2495
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2496
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2497
                owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2498
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2499
                if (formals.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2500
                    log.error(tree.pos(), "wrong.number.type.args",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2501
                              Integer.toString(formals.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2502
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2503
                    log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2504
                }
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1045
diff changeset
  2505
                owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2507
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2508
        result = check(tree, owntype, TYP, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2510
06bc494ca11e Initial load
duke
parents:
diff changeset
  2511
    public void visitTypeParameter(JCTypeParameter tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2512
        TypeVar a = (TypeVar)tree.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2513
        Set<Type> boundSet = new HashSet<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2514
        if (a.bound.isErroneous())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2515
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2516
        List<Type> bs = types.getBounds(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2517
        if (tree.bounds.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2518
            // accept class or interface or typevar as first bound.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2519
            Type b = checkBase(bs.head, tree.bounds.head, env, false, false, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2520
            boundSet.add(types.erasure(b));
1532
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2521
            if (b.isErroneous()) {
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2522
                a.bound = b;
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2523
            }
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2524
            else if (b.tag == TYPEVAR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2525
                // if first bound was a typevar, do not accept further bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2526
                if (tree.bounds.tail.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2527
                    log.error(tree.bounds.tail.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2528
                              "type.var.may.not.be.followed.by.other.bounds");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2529
                    tree.bounds = List.of(tree.bounds.head);
164
c1e219636b4e 6608214: Exception throw while analysing a file with error
mcimadamore
parents: 163
diff changeset
  2530
                    a.bound = bs.head;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2531
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2532
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2533
                // if first bound was a class or interface, accept only interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  2534
                // as further bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2535
                for (JCExpression bound : tree.bounds.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2536
                    bs = bs.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2537
                    Type i = checkBase(bs.head, bound, env, false, true, false);
1532
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2538
                    if (i.isErroneous())
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2539
                        a.bound = i;
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  2540
                    else if (i.tag == CLASS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2541
                        chk.checkNotRepeated(bound.pos(), types.erasure(i), boundSet);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2542
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2543
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2544
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2545
        bs = types.getBounds(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
        // in case of multiple bounds ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2548
        if (bs.length() > 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2549
            // ... the variable's bound is a class type flagged COMPOUND
06bc494ca11e Initial load
duke
parents:
diff changeset
  2550
            // (see comment for TypeVar.bound).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2551
            // In this case, generate a class tree that represents the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2552
            // bound class, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2553
            JCTree extending;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2554
            List<JCExpression> implementing;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2555
            if ((bs.head.tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2556
                extending = tree.bounds.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2557
                implementing = tree.bounds.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2558
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2559
                extending = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2560
                implementing = tree.bounds;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2561
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2562
            JCClassDecl cd = make.at(tree.pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2563
                make.Modifiers(PUBLIC | ABSTRACT),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2564
                tree.name, List.<JCTypeParameter>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2565
                extending, implementing, List.<JCTree>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2566
06bc494ca11e Initial load
duke
parents:
diff changeset
  2567
            ClassSymbol c = (ClassSymbol)a.getUpperBound().tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2568
            assert (c.flags() & COMPOUND) != 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2569
            cd.sym = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2570
            c.sourcefile = env.toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2571
06bc494ca11e Initial load
duke
parents:
diff changeset
  2572
            // ... and attribute the bound class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2573
            c.flags_field |= UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2574
            Env<AttrContext> cenv = enter.classEnv(cd, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2575
            enter.typeEnvs.put(c, cenv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2576
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2577
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2578
06bc494ca11e Initial load
duke
parents:
diff changeset
  2579
06bc494ca11e Initial load
duke
parents:
diff changeset
  2580
    public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2581
        //- System.err.println("visitWildcard("+tree+");");//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  2582
        Type type = (tree.kind.kind == BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2583
            ? syms.objectType
06bc494ca11e Initial load
duke
parents:
diff changeset
  2584
            : attribType(tree.inner, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2585
        result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2586
                                              tree.kind.kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2587
                                              syms.boundClass),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2588
                       TYP, pkind, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2589
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2590
06bc494ca11e Initial load
duke
parents:
diff changeset
  2591
    public void visitAnnotation(JCAnnotation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
        log.error(tree.pos(), "annotation.not.valid.for.type", pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
        result = tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
06bc494ca11e Initial load
duke
parents:
diff changeset
  2596
    public void visitErroneous(JCErroneous tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
        if (tree.errs != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2598
            for (JCTree err : tree.errs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
                attribTree(err, env, ERR, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
        result = tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2602
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
    /** Default visitor method for all other trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
        throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
    /** Main method: attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
     *  reporting completion failures at the given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
     *  @param pos The source position at which completion errors are to be
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
     *             reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
    public void attribClass(DiagnosticPosition pos, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
            annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
            attribClass(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
            chk.completionError(pos, ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2621
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2622
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
06bc494ca11e Initial load
duke
parents:
diff changeset
  2624
    /** Attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2625
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2626
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2627
    void attribClass(ClassSymbol c) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
        if (c.type.tag == ERROR) return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2629
06bc494ca11e Initial load
duke
parents:
diff changeset
  2630
        // Check for cycles in the inheritance graph, which can arise from
06bc494ca11e Initial load
duke
parents:
diff changeset
  2631
        // ill-formed class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
        chk.checkNonCyclic(null, c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
06bc494ca11e Initial load
duke
parents:
diff changeset
  2634
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
        if ((c.flags_field & Flags.COMPOUND) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
            // First, attribute superclass.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
            if (st.tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2638
                attribClass((ClassSymbol)st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
06bc494ca11e Initial load
duke
parents:
diff changeset
  2640
            // Next attribute owner, if it is a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
            if (c.owner.kind == TYP && c.owner.type.tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
                attribClass((ClassSymbol)c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
        // The previous operations might have attributed the current class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
        // if there was a cycle. So we test first whether the class is still
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
        // UNATTRIBUTED.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
        if ((c.flags_field & UNATTRIBUTED) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
            c.flags_field &= ~UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2650
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
            // Get environment current at the point of class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
            Env<AttrContext> env = enter.typeEnvs.get(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
            // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
            // because the annotations were not available at the time the env was created. Therefore,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
            // we look up the environment chain for the first enclosing environment for which the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
            // lint value is set. Typically, this is the parent env, but might be further if there
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
            // are any envs created as a result of TypeParameter nodes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
            Env<AttrContext> lintEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
            while (lintEnv.info.lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
                lintEnv = lintEnv.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
06bc494ca11e Initial load
duke
parents:
diff changeset
  2663
            // Having found the enclosing lint value, we can initialize the lint value for this class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2664
            env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2665
06bc494ca11e Initial load
duke
parents:
diff changeset
  2666
            Lint prevLint = chk.setLint(env.info.lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2667
            JavaFileObject prev = log.useSource(c.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2668
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
                // java.lang.Enum may not be subclassed by a non-enum
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
                if (st.tsym == syms.enumSym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2672
                    ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
                    log.error(env.tree.pos(), "enum.no.subclassing");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
                // Enums may not be extended by source-level classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  2676
                if (st.tsym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
                    ((st.tsym.flags_field & Flags.ENUM) != 0) &&
941
2589047a9c5d 6675483: Javac rejects multiple type-variable bound declarations starting with an enum type
mcimadamore
parents: 939
diff changeset
  2678
                    ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
                    !target.compilerBootstrap(c)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
                    log.error(env.tree.pos(), "enum.types.not.extensible");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2682
                attribClassBody(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2683
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
                chk.checkDeprecatedAnnotation(env.tree.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
                log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
                chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2692
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
    public void visitImport(JCImport tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2694
        // nothing to do
06bc494ca11e Initial load
duke
parents:
diff changeset
  2695
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2696
06bc494ca11e Initial load
duke
parents:
diff changeset
  2697
    /** Finish the attribution of a class. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
    private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
        JCClassDecl tree = (JCClassDecl)env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
        assert c == tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
        // Validate annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
        chk.validateAnnotations(tree.mods.annotations, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
06bc494ca11e Initial load
duke
parents:
diff changeset
  2705
        // Validate type parameters, supertype and interfaces.
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
  2706
        attribBounds(tree.typarams);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2707
        chk.validate(tree.typarams, env);
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2708
        chk.validate(tree.extending, env);
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2709
        chk.validate(tree.implementing, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
        // If this is a non-abstract class, check that it has no abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
        // methods or unimplemented methods of an implemented interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
        if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
            if (!relax)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
                chk.checkAllDefined(tree.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
        if ((c.flags() & ANNOTATION) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
            if (tree.implementing.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
                log.error(tree.implementing.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2721
                          "cant.extend.intf.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2722
            if (tree.typarams.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2723
                log.error(tree.typarams.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2724
                          "intf.annotation.cant.have.type.params");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2725
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2726
            // Check that all extended classes and interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  2727
            // are compatible (i.e. no two define methods with same arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  2728
            // yet different return types).  (JLS 8.4.6.3)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2729
            chk.checkCompatibleSupertypes(tree.pos(), c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2730
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
06bc494ca11e Initial load
duke
parents:
diff changeset
  2732
        // Check that class does not import the same parameterized interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
        // with two different argument lists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
        chk.checkClassBounds(tree.pos(), c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2735
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
        tree.type = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
        boolean assertsEnabled = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
        assert assertsEnabled = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
        if (assertsEnabled) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2741
            for (List<JCTypeParameter> l = tree.typarams;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
                 l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
                assert env.info.scope.lookup(l.head.name).scope != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
        // Check that a generic class doesn't extend Throwable
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
        if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
            log.error(tree.extending.pos(), "generic.throwable");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
        // Check that all methods which implement some
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
        // method conform to the method they implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
        chk.checkImplementations(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
            // Attribute declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
            // Check that declarations in inner classes are not static (JLS 8.1.2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
            // Make an exception for static constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
            if (c.owner.kind != PCK &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
                ((c.flags() & STATIC) == 0 || c.name == names.empty) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
                (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
                Symbol sym = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
                if (l.head.getTag() == JCTree.VARDEF) sym = ((JCVariableDecl) l.head).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
                if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
                    sym.kind != VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
                    ((VarSymbol) sym).getConstValue() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
                    log.error(l.head.pos(), "icls.cant.have.static.decl");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
        // Check for cycles among non-initial constructors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
        chk.checkCyclicConstructors(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
        // Check for cycles among annotation elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
        chk.checkNonCyclicElements(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
        // Check for proper use of serialVersionUID
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
        if (env.info.lint.isEnabled(Lint.LintCategory.SERIAL) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
            isSerializable(c) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
            (c.flags() & Flags.ENUM) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
            (c.flags() & ABSTRACT) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2782
            checkSerialVersionUID(tree, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2783
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2785
        // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
        /** check if a class is a subtype of Serializable, if that is available. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
        private boolean isSerializable(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
                syms.serializableType.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
            catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2792
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2794
            return types.isSubtype(c.type, syms.serializableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
06bc494ca11e Initial load
duke
parents:
diff changeset
  2797
        /** Check that an appropriate serialVersionUID member is defined. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2798
        private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
06bc494ca11e Initial load
duke
parents:
diff changeset
  2800
            // check for presence of serialVersionUID
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
            Scope.Entry e = c.members().lookup(names.serialVersionUID);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2802
            while (e.scope != null && e.sym.kind != VAR) e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2803
            if (e.scope == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2804
                log.warning(tree.pos(), "missing.SVUID", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2805
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2806
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2807
06bc494ca11e Initial load
duke
parents:
diff changeset
  2808
            // check that it is static final
06bc494ca11e Initial load
duke
parents:
diff changeset
  2809
            VarSymbol svuid = (VarSymbol)e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2810
            if ((svuid.flags() & (STATIC | FINAL)) !=
06bc494ca11e Initial load
duke
parents:
diff changeset
  2811
                (STATIC | FINAL))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
                log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
            // check that it is long
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
            else if (svuid.type.tag != TypeTags.LONG)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2816
                log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
            // check constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  2819
            else if (svuid.getConstValue() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2820
                log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2821
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2822
06bc494ca11e Initial load
duke
parents:
diff changeset
  2823
    private Type capture(Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
        return types.capture(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2825
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2826
}