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