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