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