langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
author vromero
Mon, 02 Sep 2013 22:38:36 +0100
changeset 19914 d86271bd430a
parent 19656 7f0afbdbf142
child 19916 5b5f188dbdd4
permissions -rw-r--r--
8016177: structural most specific and stuckness Reviewed-by: jjg, vromero Contributed-by: maurizio.cimadamore@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
     2
 * Copyright (c) 1999, 2013, 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
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    29
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    30
import javax.lang.model.element.ElementKind;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    31
import javax.lang.model.type.TypeKind;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    32
import javax.tools.JavaFileObject;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.source.tree.IdentifierTree;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
    35
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.source.tree.MemberSelectTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.source.tree.TreeVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.source.util.SimpleTreeVisitor;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    39
import com.sun.tools.javac.code.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    40
import com.sun.tools.javac.code.Lint.LintCategory;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    41
import com.sun.tools.javac.code.Symbol.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    42
import com.sun.tools.javac.code.Type.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    43
import com.sun.tools.javac.comp.Check.CheckContext;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    44
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    45
import com.sun.tools.javac.comp.Infer.InferenceContext;
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
    46
import com.sun.tools.javac.comp.Infer.FreeTypeListener;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    47
import com.sun.tools.javac.jvm.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    48
import com.sun.tools.javac.tree.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.*;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    51
import com.sun.tools.javac.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    52
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    53
import com.sun.tools.javac.util.List;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    55
import static com.sun.tools.javac.code.Flags.ANNOTATION;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    56
import static com.sun.tools.javac.code.Flags.BLOCK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import static com.sun.tools.javac.code.Kinds.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    58
import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
    59
import static com.sun.tools.javac.code.TypeTag.*;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
    60
import static com.sun.tools.javac.code.TypeTag.WILDCARD;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    61
import static com.sun.tools.javac.tree.JCTree.Tag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
/** This is the main context-dependent analysis phase in GJC. It
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  encompasses name resolution, type checking and constant folding as
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  subtasks. Some subtasks involve auxiliary classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  @see Check
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *  @see Resolve
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 *  @see ConstFold
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *  @see Infer
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    71
 *  <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
    72
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
public class Attr extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    protected static final Context.Key<Attr> attrKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        new Context.Key<Attr>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    80
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    final Resolve rs;
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
    84
    final Infer infer;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
    85
    final DeferredAttr deferredAttr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    final Check chk;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
    87
    final Flow flow;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    final MemberEnter memberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    final Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    final Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
    94
    final JCDiagnostic.Factory diags;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    final Annotate annotate;
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
    96
    final DeferredLintHandler deferredLintHandler;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public static Attr instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        Attr instance = context.get(attrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            instance = new Attr(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    protected Attr(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        context.put(attrKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
   108
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        rs = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        chk = Check.instance(context);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   113
        flow = Flow.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        memberEnter = MemberEnter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        enter = Enter.instance(context);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   117
        infer = Infer.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   118
        deferredAttr = DeferredAttr.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        target = Target.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
   122
        diags = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        annotate = Annotate.instance(context);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   124
        deferredLintHandler = DeferredLintHandler.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        allowGenerics = source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        allowVarargs = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        allowEnums = source.allowEnums();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        allowBoxing = source.allowBoxing();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        allowCovariantReturns = source.allowCovariantReturns();
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        allowAnonOuterThis = source.allowAnonOuterThis();
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   135
        allowStringsInSwitch = source.allowStringsInSwitch();
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   136
        allowPoly = source.allowPoly();
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   137
        allowTypeAnnos = source.allowTypeAnnotations();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   138
        allowLambda = source.allowLambda();
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   139
        allowDefaultMethods = source.allowDefaultMethods();
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   140
        sourceName = source.name;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6710
diff changeset
   141
        relax = (options.isSet("-retrofit") ||
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6710
diff changeset
   142
                 options.isSet("-relax"));
7081
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   143
        findDiamonds = options.get("findDiamond") != null &&
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   144
                 source.allowDiamond();
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6710
diff changeset
   145
        useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   146
        identifyLambdaCandidate = options.getBoolean("identifyLambdaCandidate", false);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   147
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   148
        statInfo = new ResultInfo(NIL, Type.noType);
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   149
        varInfo = new ResultInfo(VAR, Type.noType);
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   150
        unknownExprInfo = new ResultInfo(VAL, Type.noType);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   151
        unknownAnyPolyInfo = new ResultInfo(VAL, Infer.anyPoly);
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   152
        unknownTypeInfo = new ResultInfo(TYP, Type.noType);
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16972
diff changeset
   153
        unknownTypeExprInfo = new ResultInfo(Kinds.TYP | Kinds.VAL, Type.noType);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   154
        recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /** Switch: relax some constraints for retrofit mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    boolean relax;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   161
    /** Switch: support target-typing inference
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   162
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   163
    boolean allowPoly;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   164
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   165
    /** Switch: support type annotations.
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   166
     */
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   167
    boolean allowTypeAnnos;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   168
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** Switch: support generics?
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /** Switch: allow variable-arity methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    boolean allowVarargs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    /** Switch: support enums?
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    boolean allowEnums;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /** Switch: support boxing and unboxing?
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    boolean allowBoxing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    /** Switch: support covariant result types?
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    boolean allowCovariantReturns;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   189
    /** Switch: support lambda expressions ?
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   190
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   191
    boolean allowLambda;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   192
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   193
    /** Switch: support default methods ?
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   194
     */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   195
    boolean allowDefaultMethods;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   196
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    /** Switch: allow references to surrounding object from anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * objects during constructor call?
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    boolean allowAnonOuterThis;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
7081
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   202
    /** Switch: generates a warning if diamond can be safely applied
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   203
     *  to a given new expression
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   204
     */
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   205
    boolean findDiamonds;
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   206
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   207
    /**
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   208
     * Internally enables/disables diamond finder feature
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   209
     */
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   210
    static final boolean allowDiamondFinder = true;
94cfc5b65bed 6939780: add a warning to detect diamond sites
mcimadamore
parents: 7074
diff changeset
   211
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     * Switch: warn about use of variable before declaration?
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     * RFE: 6425594
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    boolean useBeforeDeclarationWarning;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 3559
diff changeset
   218
    /**
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   219
     * Switch: generate warnings whenever an anonymous inner class that is convertible
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   220
     * to a lambda expression is found
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   221
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   222
    boolean identifyLambdaCandidate;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   223
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   224
    /**
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   225
     * Switch: allow strings in switch?
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   226
     */
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   227
    boolean allowStringsInSwitch;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   228
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   229
    /**
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   230
     * Switch: name of source level; used for error reporting.
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   231
     */
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   232
    String sourceName;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   233
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    /** Check kind and type of given tree against protokind and prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     *  If check succeeds, store type in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     *  If check fails, store errType in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     *  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
   238
     *  It is not necessary in this case since we know that kind and type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     *  are correct.
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     *  @param tree     The tree whose kind and type is checked
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     *  @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
   243
     *  @param resultInfo  The expected result of the tree
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   245
    Type check(final JCTree tree, final Type found, final int ownkind, final ResultInfo resultInfo) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   246
        InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   247
        Type owntype = found;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   248
        if (!owntype.hasTag(ERROR) && !resultInfo.pt.hasTag(METHOD) && !resultInfo.pt.hasTag(FORALL)) {
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   249
            if (allowPoly && inferenceContext.free(found)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   250
                inferenceContext.addFreeTypeListener(List.of(found, resultInfo.pt), new FreeTypeListener() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   251
                    @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   252
                    public void typesInferred(InferenceContext inferenceContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   253
                        ResultInfo pendingResult =
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   254
                                    resultInfo.dup(inferenceContext.asInstType(resultInfo.pt));
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
   255
                        check(tree, inferenceContext.asInstType(found), ownkind, pendingResult);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   256
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   257
                });
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   258
                return tree.type = resultInfo.pt;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            } else {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   260
                if ((ownkind & ~resultInfo.pkind) == 0) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   261
                    owntype = resultInfo.check(tree, owntype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   262
                } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   263
                    log.error(tree.pos(), "unexpected.type",
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   264
                            kindNames(resultInfo.pkind),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   265
                            kindName(ownkind));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   266
                    owntype = types.createErrorType(owntype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   267
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        tree.type = owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        return owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    /** Is given blank final variable assignable, i.e. in a scope where it
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     *  may be assigned to even though it is final?
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     *  @param v      The blank final variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) {
13439
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   280
        Symbol owner = owner(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
           // owner refers to the innermost variable, method or
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
           // initializer block declaration at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            v.owner == owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            ((owner.name == names.init ||    // i.e. we are in a constructor
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
              owner.kind == VAR ||           // i.e. we are in a variable initializer
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
              (owner.flags() & BLOCK) != 0)  // i.e. we are in an initializer block
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
             v.owner == owner.owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
             ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
13439
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   295
    /**
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   296
     * 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
   297
     */
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   298
    Symbol owner(Env<AttrContext> env) {
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   299
        while (true) {
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   300
            switch (env.tree.getTag()) {
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   301
                case VARDEF:
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   302
                    //a field can be owner
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   303
                    VarSymbol vsym = ((JCVariableDecl)env.tree).sym;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   304
                    if (vsym.owner.kind == TYP) {
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   305
                        return vsym;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   306
                    }
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   307
                    break;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   308
                case METHODDEF:
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   309
                    //method def is always an owner
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   310
                    return ((JCMethodDecl)env.tree).sym;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   311
                case CLASSDEF:
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   312
                    //class def is always an owner
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   313
                    return ((JCClassDecl)env.tree).sym;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   314
                case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   315
                    //a lambda is an owner - return a fresh synthetic method symbol
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   316
                    return new MethodSymbol(0, names.empty, null, syms.methodClass);
13439
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   317
                case BLOCK:
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   318
                    //static/instance init blocks are owner
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   319
                    Symbol blockSym = env.info.scope.owner;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   320
                    if ((blockSym.flags() & BLOCK) != 0) {
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   321
                        return blockSym;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   322
                    }
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   323
                    break;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   324
                case TOPLEVEL:
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   325
                    //toplevel is always an owner (for pkge decls)
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   326
                    return env.info.scope.owner;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   327
            }
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   328
            Assert.checkNonNull(env.next);
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   329
            env = env.next;
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   330
        }
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   331
    }
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
   332
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    /** Check that variable can be assigned to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     *  @param pos    The current source code position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     *  @param v      The assigned varaible
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     *  @param base   If the variable is referred to in a Select, the part
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     *                to the left of the `.', null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        if ((v.flags() & FINAL) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            ((v.flags() & HASINIT) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
             ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
             !((base == null ||
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
   345
               (base.hasTag(IDENT) && TreeInfo.name(base) == names._this)) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
               isAssignableAsBlankFinal(v, env)))) {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   347
            if (v.isResourceVariable()) { //TWR resource
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7208
diff changeset
   348
                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
   349
            } else {
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   350
                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
   351
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
    /** Does tree represent a static reference to an identifier?
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
     *  It is assumed that tree is either a SELECT or an IDENT.
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
     *  We have to weed out selects from non-type names here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     *  @param tree    The candidate tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    boolean isStaticReference(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
   361
        if (tree.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            if (lsym == null || lsym.kind != TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    /** Is this symbol a type?
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
    static boolean isType(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        return sym != null && sym.kind == TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    /** The current `this' symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
    Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        return rs.resolveSelf(pos, env, env.enclClass.sym, names._this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
    /** Attribute a parsed identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
     * @param tree Parsed identifier name
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
     * @param topLevel The toplevel to use
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
    public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        Env<AttrContext> localEnv = enter.topLevelEnv(topLevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        localEnv.enclClass = make.ClassDef(make.Modifiers(0),
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                                           syms.errSymbol.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                                           null, null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        localEnv.enclClass.sym = syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        return tree.accept(identAttributer, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                Symbol site = visit(node.getExpression(), env);
19505
df0bd8ca483c 7071377: Exception when javac -processor is given a class name with invalid postfix
ksrini
parents: 19502
diff changeset
   401
                if (site.kind == ERR || site.kind == ABSENT_TYP)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                    return site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                Name name = (Name)node.getIdentifier();
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                if (site.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                    env.toplevel.packge = (PackageSymbol)site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                    return rs.findIdentInPackage(env, (TypeSymbol)site, name, TYP | PCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                    env.enclClass.sym = (ClassSymbol)site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                    return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site);
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                return rs.findIdent(env, (Name)node.getName(), TYP | PCK);
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
    public Type coerce(Type etype, Type ttype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        return cfolder.coerce(etype, ttype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    public Type attribType(JCTree node, TypeSymbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        Env<AttrContext> env = enter.typeEnvs.get(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        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
   426
        return attribTree(node, localEnv, unknownTypeInfo);
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   427
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   428
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   429
    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
   430
        // Attribute qualifying package or class.
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   431
        JCFieldAccess s = (JCFieldAccess)tree.qualid;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   432
        return attribTree(s.selected,
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   433
                       env,
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   434
                       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
   435
                       Type.noType));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
    public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        breakTree = tree;
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 2723
diff changeset
   440
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            attribExpr(expr, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            return b.env;
6587
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   445
        } catch (AssertionError ae) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   446
            if (ae.getCause() instanceof BreakAttr) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   447
                return ((BreakAttr)(ae.getCause())).env;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   448
            } else {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   449
                throw ae;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   450
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        breakTree = tree;
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 2723
diff changeset
   460
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            attribStat(stmt, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            return b.env;
6587
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   465
        } catch (AssertionError ae) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   466
            if (ae.getCause() instanceof BreakAttr) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   467
                return ((BreakAttr)(ae.getCause())).env;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   468
            } else {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   469
                throw ae;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   470
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    private JCTree breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    private static class BreakAttr extends RuntimeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        static final long serialVersionUID = -6924771130405446405L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        private Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        private BreakAttr(Env<AttrContext> env) {
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   484
            this.env = env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   488
    class ResultInfo {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   489
        final int pkind;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   490
        final Type pt;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   491
        final CheckContext checkContext;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   492
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   493
        ResultInfo(int pkind, Type pt) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   494
            this(pkind, pt, chk.basicHandler);
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   495
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   496
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   497
        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
   498
            this.pkind = pkind;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   499
            this.pt = pt;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   500
            this.checkContext = checkContext;
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   501
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   502
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   503
        protected Type check(final DiagnosticPosition pos, final Type found) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   504
            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
   505
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   506
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   507
        protected ResultInfo dup(Type newPt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   508
            return new ResultInfo(pkind, newPt, checkContext);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   509
        }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   510
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   511
        protected ResultInfo dup(CheckContext newContext) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   512
            return new ResultInfo(pkind, pt, newContext);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   513
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   514
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   515
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   516
    class RecoveryInfo extends ResultInfo {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   517
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   518
        public RecoveryInfo(final DeferredAttr.DeferredAttrContext deferredAttrContext) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   519
            super(Kinds.VAL, Type.recoveryType, new Check.NestedCheckContext(chk.basicHandler) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   520
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   521
                public DeferredAttr.DeferredAttrContext deferredAttrContext() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   522
                    return deferredAttrContext;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   523
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   524
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   525
                public boolean compatible(Type found, Type req, Warner warn) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   526
                    return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   527
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   528
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   529
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   530
                    chk.basicHandler.report(pos, details);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   531
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   532
            });
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   533
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   534
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   535
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   536
    final ResultInfo statInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   537
    final ResultInfo varInfo;
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   538
    final ResultInfo unknownAnyPolyInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   539
    final ResultInfo unknownExprInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   540
    final ResultInfo unknownTypeInfo;
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16972
diff changeset
   541
    final ResultInfo unknownTypeExprInfo;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   542
    final ResultInfo recoveryInfo;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   543
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   544
    Type pt() {
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   545
        return resultInfo.pt;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   546
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   547
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   548
    int pkind() {
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   549
        return resultInfo.pkind;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   550
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
 * Visitor methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    /** Visitor argument: the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
    Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   560
    /** Visitor argument: the currently expected attribution result.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
     */
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   562
    ResultInfo resultInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    /** Visitor result: the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    Type result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
    /** Visitor method: attribute a tree, catching any completion failure
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
     *  exceptions. Return the tree's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
     *  @param tree    The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
     *  @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
   573
     *  @param resultInfo   The result info visitor argument.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   575
    Type attribTree(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        Env<AttrContext> prevEnv = this.env;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   577
        ResultInfo prevResult = this.resultInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            this.env = env;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   580
            this.resultInfo = resultInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            tree.accept(this);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   582
            if (tree == breakTree &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   583
                    resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   584
                throw new BreakAttr(copyEnv(env));
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   585
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            return chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            this.env = prevEnv;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   592
            this.resultInfo = prevResult;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   596
    Env<AttrContext> copyEnv(Env<AttrContext> env) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   597
        Env<AttrContext> newEnv =
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   598
                env.dup(env.tree, env.info.dup(copyScope(env.info.scope)));
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   599
        if (newEnv.outer != null) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   600
            newEnv.outer = copyEnv(newEnv.outer);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   601
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   602
        return newEnv;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   603
    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   604
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   605
    Scope copyScope(Scope sc) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   606
        Scope newScope = new Scope(sc.owner);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   607
        List<Symbol> elemsList = List.nil();
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   608
        while (sc != null) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   609
            for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   610
                elemsList = elemsList.prepend(e.sym);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   611
            }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   612
            sc = sc.next;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   613
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   614
        for (Symbol s : elemsList) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   615
            newScope.enter(s);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   616
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   617
        return newScope;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   618
    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   619
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
    /** Derived visitor method: attribute an expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
    public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   623
        return attribTree(tree, env, new ResultInfo(VAL, !pt.hasTag(ERROR) ? pt : Type.noType));
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   624
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   625
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    /** Derived visitor method: attribute an expression tree with
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
     *  no constraints on the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
     */
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14538
diff changeset
   629
    public 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
   630
        return attribTree(tree, env, unknownExprInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    /** Derived visitor method: attribute a type tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     */
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14538
diff changeset
   635
    public Type attribType(JCTree tree, Env<AttrContext> env) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   636
        Type result = attribType(tree, env, Type.noType);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   637
        return result;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   638
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   639
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   640
    /** Derived visitor method: attribute a type tree.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   641
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   642
    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
   643
        Type result = attribTree(tree, env, new ResultInfo(TYP, pt));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
    /** Derived visitor method: attribute a statement or definition tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
    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
   650
        return attribTree(tree, env, statInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
    /** Attribute a list of expressions, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
    List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
        ListBuffer<Type> ts = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
            ts.append(attribExpr(l.head, env, pt));
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    /** Attribute a list of statements, returning nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
    <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        for (List<T> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   669
    /** Attribute the arguments in a method call, returning the method kind.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
     */
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   671
    int attribArgs(List<JCExpression> trees, Env<AttrContext> env, ListBuffer<Type> argtypes) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   672
        int kind = VAL;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   673
        for (JCExpression arg : trees) {
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   674
            Type argtype;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   675
            if (allowPoly && deferredAttr.isDeferred(env, arg)) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   676
                argtype = deferredAttr.new DeferredType(arg, env);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   677
                kind |= POLY;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   678
            } else {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   679
                argtype = chk.checkNonVoid(arg, attribTree(arg, env, unknownAnyPolyInfo));
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   680
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   681
            argtypes.append(argtype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   682
        }
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   683
        return kind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
    /** 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
   687
     *  Caller is responsible for calling checkRefTypes.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
     */
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   689
    List<Type> attribAnyTypes(List<JCExpression> trees, Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        ListBuffer<Type> argtypes = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        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
   692
            argtypes.append(attribType(l.head, env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
        return argtypes.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   696
    /** 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
   697
     *  Check that all the types are references.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   698
     */
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   699
    List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   700
        List<Type> types = attribAnyTypes(trees, env);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   701
        return chk.checkRefTypes(trees, types);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   702
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
     * Attribute type variables (of generic classes or methods).
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
     * Compound types are attributed later in attribBounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
     * @param typarams the type variables to enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
     * @param env      the current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
    void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        for (JCTypeParameter tvar : typarams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            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
   713
            a.tsym.flags_field |= UNATTRIBUTED;
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   714
            a.bound = Type.noType;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            if (!tvar.bounds.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                List<Type> bounds = List.of(attribType(tvar.bounds.head, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                for (JCExpression bound : tvar.bounds.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                    bounds = bounds.prepend(attribType(bound, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                types.setBounds(a, bounds.reverse());
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                // if no bounds are given, assume a single bound of
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                // java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                types.setBounds(a, List.of(syms.objectType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
            }
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   725
            a.tsym.flags_field &= ~UNATTRIBUTED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   727
        for (JCTypeParameter tvar : typarams) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
            chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
     * Attribute the type references in a list of annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    void attribAnnotationTypes(List<JCAnnotation> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
                               Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            JCAnnotation a = al.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
            attribType(a.annotationType, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   743
    /**
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   744
     * Attribute a "lazy constant value".
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   745
     *  @param env         The env for the const value
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   746
     *  @param initializer The initializer for the const value
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   747
     *  @param type        The expected type, or null
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   748
     *  @see VarSymbol#setLazyConstValue
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   749
     */
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   750
    public Object attribLazyConstantValue(Env<AttrContext> env,
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   751
                                      JCTree.JCExpression initializer,
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   752
                                      Type type) {
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   753
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   754
        /*  When this env was created, it didn't have the correct lint nor had
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   755
         *  annotations has been processed.
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   756
         *  But now at this phase we have already processed annotations and the
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   757
         *  correct lint must have been set in chk, so we should use that one to
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   758
         *  attribute the initializer.
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   759
         */
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   760
        Lint prevLint = env.info.lint;
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   761
        env.info.lint = chk.getLint();
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   762
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   763
        JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   764
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   765
        try {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   766
            // Use null as symbol to not attach the type annotation to any symbol.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   767
            // The initializer will later also be visited and then we'll attach
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   768
            // to the symbol.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   769
            // This prevents having multiple type annotations, just because of
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   770
            // lazy constant value evaluation.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
   771
            memberEnter.typeAnnotate(initializer, env, null);
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15717
diff changeset
   772
            annotate.flush();
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   773
            Type itype = attribExpr(initializer, env, type);
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   774
            if (itype.constValue() != null) {
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   775
                return coerce(itype, type).constValue();
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   776
            } else {
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   777
                return null;
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   778
            }
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   779
        } finally {
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   780
            env.info.lint = prevLint;
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   781
            log.useSource(prevSource);
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   782
        }
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   783
    }
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   784
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    /** Attribute type reference in an `extends' or `implements' clause.
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   786
     *  Supertypes of anonymous inner classes are usually already attributed.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
     *  @param tree              The tree making up the type reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
     *  @param env               The environment current at the reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
     *  @param classExpected     true if only a class is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
     *  @param interfaceExpected true if only an interface is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
    Type attribBase(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
                    Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
                    boolean classExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
                    boolean interfaceExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                    boolean checkExtensible) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   798
        Type t = tree.type != null ?
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   799
            tree.type :
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   800
            attribType(tree, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible);
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
    Type checkBase(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                   JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                   Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                   boolean classExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
                   boolean interfaceExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                   boolean checkExtensible) {
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6351
diff changeset
   809
        if (t.isErroneous())
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6351
diff changeset
   810
            return t;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   811
        if (t.hasTag(TYPEVAR) && !classExpected && !interfaceExpected) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
            // check that type variable is already visible
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
            if (t.getUpperBound() == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
                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
   815
                return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
            t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics);
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
            log.error(tree.pos(), "intf.expected.here");
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
            // return errType is necessary since otherwise there might
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            // 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
   824
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
        } else if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                   classExpected &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                   (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
   828
                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
   829
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
            ((t.tsym.flags() & FINAL) != 0)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                      "cant.inherit.from.final", t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        chk.checkNonCyclic(tree.pos(), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   840
    Type attribIdentAsEnumType(Env<AttrContext> env, JCIdent id) {
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   841
        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
   842
        id.type = env.info.scope.owner.type;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   843
        id.sym = env.info.scope.owner;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   844
        return id.type;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   845
    }
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   846
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
    public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        // Local classes have not been entered yet, so we need to do it now:
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
        if ((env.info.scope.owner.kind & (VAR | MTH)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
            enter.classEnter(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
        ClassSymbol c = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        if (c == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
            // exit in case something drastic went wrong during enter.
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
            result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            // make sure class has been completed:
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
            c.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
            // If this class appears as an anonymous class
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
            // in a superclass constructor call where
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
            // no explicit outer instance is given,
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            // disable implicit outer instance from being passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            // (This would be an illegal access to "this before super").
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
            if (env.info.isSelfCall &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
   866
                env.tree.hasTag(NEWCLASS) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                ((JCNewClass) env.tree).encl == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
            {
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                c.flags_field |= NOOUTERTHIS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            attribClass(tree.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            result = tree.type = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
        MethodSymbol m = tree.sym;
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14259
diff changeset
   878
        boolean isDefaultMethod = (m.flags() & DEFAULT) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
   880
        Lint lint = env.info.lint.augment(m);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
        Lint prevLint = chk.setLint(lint);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
   882
        MethodSymbol prevMethod = chk.setMethod(m);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
        try {
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   884
            deferredLintHandler.flush(tree.pos());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            chk.checkDeprecatedAnnotation(tree.pos(), m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   887
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   888
            // Create a new environment with local scope
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   889
            // for attributing the method.
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   890
            Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   891
            localEnv.info.lint = lint;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   892
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   893
            attribStats(tree.typarams, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            // If we override any other methods, check that we do so properly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
            // JLS ???
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   897
            if (m.isStatic()) {
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   898
                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
   899
            } else {
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   900
                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
   901
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
            chk.checkOverride(tree, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   904
            if (isDefaultMethod && types.overridesObjectMethod(m.enclClass(), m)) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   905
                log.error(tree, "default.overrides.object.member", m.name, Kinds.kindName(m.location()), m.location());
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   906
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   907
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            // Enter all type parameters into the local method scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
            for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
                localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            ClassSymbol owner = env.enclClass.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            if ((owner.flags() & ANNOTATION) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                tree.params.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                log.error(tree.params.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                          "intf.annotation.members.cant.have.params");
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            // Attribute all value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
            for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
                attribStat(l.head, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
   923
            chk.checkVarargsMethodDecl(localEnv, tree);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5654
diff changeset
   924
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
            // Check that type parameters are well-formed.
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
   926
            chk.validate(tree.typarams, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
            // Check that result type is well-formed.
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
   929
            chk.validate(tree.restype, localEnv);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   930
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   931
            // Check that receiver type is well-formed.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   932
            if (tree.recvparam != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   933
                // Use a new environment to check the receiver parameter.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   934
                // Otherwise I get "might not have been initialized" errors.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   935
                // Is there a better way?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   936
                Env<AttrContext> newEnv = memberEnter.methodEnv(tree, env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   937
                attribType(tree.recvparam, newEnv);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   938
                chk.validate(tree.recvparam, newEnv);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   939
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   940
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   941
            // annotation method checks
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   942
            if ((owner.flags() & ANNOTATION) != 0) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   943
                // annotation method cannot have throws clause
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   944
                if (tree.thrown.nonEmpty()) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   945
                    log.error(tree.thrown.head.pos(),
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   946
                            "throws.not.allowed.in.intf.annotation");
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   947
                }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   948
                // annotation method cannot declare type-parameters
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   949
                if (tree.typarams.nonEmpty()) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   950
                    log.error(tree.typarams.head.pos(),
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   951
                            "intf.annotation.members.cant.have.type.params");
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   952
                }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   953
                // validate annotation method's return type (could be an annotation type)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
                chk.validateAnnotationType(tree.restype);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   955
                // ensure that annotation method does not clash with members of Object/Annotation
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
                chk.validateAnnotationMethod(tree.pos(), m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
6347
947437d52cc1 6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents: 6344
diff changeset
   958
                if (tree.defaultValue != null) {
947437d52cc1 6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents: 6344
diff changeset
   959
                    // 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
   960
                    // 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
   961
                    chk.validateAnnotationTree(tree.defaultValue);
947437d52cc1 6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents: 6344
diff changeset
   962
                }
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   963
            }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
   964
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
            for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            if (tree.body == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                // Empty bodies are only allowed for
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                // abstract, native, or interface methods, or for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                // in a retrofit signature class.
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
   972
                if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0 &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                    !relax)
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                    log.error(tree.pos(), "missing.meth.body.or.decl.abstract");
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                if (tree.defaultValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                    if ((owner.flags() & ANNOTATION) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
                        log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
                                  "default.allowed.in.intf.annotation.member");
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
                }
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   980
            } else if ((tree.sym.flags() & ABSTRACT) != 0 && !isDefaultMethod) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   981
                if ((owner.flags() & INTERFACE) != 0) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   982
                    log.error(tree.body.pos(), "intf.meth.cant.have.body");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   983
                } else {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   984
                    log.error(tree.pos(), "abstract.meth.cant.have.body");
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   985
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
            } else if ((tree.mods.flags & NATIVE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                log.error(tree.pos(), "native.meth.cant.have.body");
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
                // Add an implicit super() call unless an explicit call to
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                // super(...) or this(...) is given
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                // or we are compiling class java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                if (tree.name == names.init && owner.type != syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                    JCBlock body = tree.body;
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                    if (body.stats.isEmpty() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                        !TreeInfo.isSelfCall(body.stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                        body.stats = body.stats.
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                            prepend(memberEnter.SuperCall(make.at(body.pos),
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                                                          List.<Type>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                                                          List.<JCVariableDecl>nil(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
                                                          false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                    } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
                               (tree.mods.flags & GENERATEDCONSTR) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
                               TreeInfo.isSuperCall(body.stats.head)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
                        // enum constructors are not allowed to call super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                        // directly, so make sure there aren't any super calls
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                        // in enum constructors, except in the compiler
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                        // generated one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                        log.error(tree.body.stats.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
                                  "call.to.super.not.allowed.in.enum.ctor",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
                                  env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1014
                // Attribute all type annotations in the body
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1015
                memberEnter.typeAnnotate(tree.body, localEnv, m);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1016
                annotate.flush();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1017
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
                // Attribute method body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                attribStat(tree.body, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1021
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
            localEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
            result = tree.type = m.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
            chk.validateAnnotations(tree.mods.annotations, m);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
            chk.setLint(prevLint);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  1028
            chk.setMethod(prevMethod);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
        // Local variables have not been entered yet, so we need to do it now:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
        if (env.info.scope.owner.kind == MTH) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
            if (tree.sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                // parameters have already been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                env.info.scope.enter(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                memberEnter.memberEnter(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1042
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1043
            if (tree.init != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1044
                // Field initializer expression need to be entered.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1045
                memberEnter.typeAnnotate(tree.init, env, tree.sym);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1046
                annotate.flush();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1047
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
        VarSymbol v = tree.sym;
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1051
        Lint lint = env.info.lint.augment(v);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
        Lint prevLint = chk.setLint(lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
1538
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
  1054
        // Check that the variable's declared type is well-formed.
17584
3c4dfe34c2ec 8013222: Javac issues spurious raw type warnings when lambda has implicit parameter types
mcimadamore
parents: 17583
diff changeset
  1055
        boolean isImplicitLambdaParameter = env.tree.hasTag(LAMBDA) &&
3c4dfe34c2ec 8013222: Javac issues spurious raw type warnings when lambda has implicit parameter types
mcimadamore
parents: 17583
diff changeset
  1056
                ((JCLambda)env.tree).paramKind == JCLambda.ParameterKind.IMPLICIT &&
3c4dfe34c2ec 8013222: Javac issues spurious raw type warnings when lambda has implicit parameter types
mcimadamore
parents: 17583
diff changeset
  1057
                (tree.sym.flags() & PARAMETER) != 0;
3c4dfe34c2ec 8013222: Javac issues spurious raw type warnings when lambda has implicit parameter types
mcimadamore
parents: 17583
diff changeset
  1058
        chk.validate(tree.vartype, env, !isImplicitLambdaParameter);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
  1059
        deferredLintHandler.flush(tree.pos());
1538
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
  1060
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
            chk.checkDeprecatedAnnotation(tree.pos(), v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
            if (tree.init != null) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1065
                if ((v.flags_field & FINAL) != 0 &&
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19505
diff changeset
  1066
                    memberEnter.needsLazyConstValue(tree.init)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                    // In this case, `v' is final.  Ensure that it's initializer is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
                    // evaluated.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                    v.getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                    // Attribute initializer in a new environment
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                    // with the declared variable as owner.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
                    // Check that initializer conforms to variable's declared type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                    Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
                    initEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
                    // In order to catch self-references, we set the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
                    // declaration position to maximal possible value, effectively
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
                    // marking the variable as undefined.
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  1079
                    initEnv.info.enclVar = v;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
                    attribExpr(tree.init, initEnv, v.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
            result = tree.type = v.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
            chk.validateAnnotations(tree.mods.annotations, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
            chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
    public void visitSkip(JCSkip tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
    public void visitBlock(JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
        if (env.info.scope.owner.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
            // Block is a static or instance initializer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
            // let the owner of the environment be a freshly
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            // created BLOCK-method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
            Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
                env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
            localEnv.info.scope.owner =
15710
5792a085da41 7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents: 15705
diff changeset
  1103
                new MethodSymbol(tree.flags | BLOCK |
5792a085da41 7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents: 15705
diff changeset
  1104
                    env.info.scope.owner.flags() & STRICTFP, names.empty, null,
5792a085da41 7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents: 15705
diff changeset
  1105
                    env.info.scope.owner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
            if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1107
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1108
            // Attribute all type annotations in the block
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1109
            memberEnter.typeAnnotate(tree, localEnv, localEnv.info.scope.owner);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1110
            annotate.flush();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1111
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1112
            {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1113
                // Store init and clinit type annotations with the ClassSymbol
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1114
                // to allow output in Gen.normalizeDefs.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1115
                ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1116
                List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1117
                if ((tree.flags & STATIC) != 0) {
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1118
                    cs.appendClassInitTypeAttributes(tas);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1119
                } else {
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1120
                    cs.appendInitTypeAttributes(tas);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1121
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1122
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1123
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
            attribStats(tree.stats, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
            // Create a new local environment with a local scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                env.dup(tree, env.info.dup(env.info.scope.dup()));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1129
            try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1130
                attribStats(tree.stats, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1131
            } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1132
                localEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1133
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
            env.dup(env.tree, env.info.dup(env.info.scope.dup()));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1153
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1154
            attribStats(tree.init, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1155
            if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1156
            loopEnv.tree = tree; // before, we were not in loop!
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1157
            attribStats(tree.step, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1158
            attribStat(tree.body, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1159
            result = null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1160
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1161
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1162
            loopEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1163
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
            env.dup(env.tree, env.info.dup(env.info.scope.dup()));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1169
        try {
18379
8dd20756448c 7139681: Enhanced for loop: local variable scope inconsistent with JLS
mcimadamore
parents: 18010
diff changeset
  1170
            //the Formal Parameter of a for-each loop is not in the scope when
8dd20756448c 7139681: Enhanced for loop: local variable scope inconsistent with JLS
mcimadamore
parents: 18010
diff changeset
  1171
            //attributing the for-each expression; we mimick this by attributing
8dd20756448c 7139681: Enhanced for loop: local variable scope inconsistent with JLS
mcimadamore
parents: 18010
diff changeset
  1172
            //the for-each expression first (against original scope).
8dd20756448c 7139681: Enhanced for loop: local variable scope inconsistent with JLS
mcimadamore
parents: 18010
diff changeset
  1173
            Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1174
            attribStat(tree.var, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1175
            chk.checkNonVoid(tree.pos(), exprType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1176
            Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1177
            if (elemtype == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1178
                // or perhaps expr implements Iterable<T>?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1179
                Type base = types.asSuper(exprType, syms.iterableType.tsym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1180
                if (base == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1181
                    log.error(tree.expr.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1182
                            "foreach.not.applicable.to.type",
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1183
                            exprType,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1184
                            diags.fragment("type.req.array.or.iterable"));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1185
                    elemtype = types.createErrorType(exprType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1186
                } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1187
                    List<Type> iterableParams = base.allparams();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1188
                    elemtype = iterableParams.isEmpty()
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1189
                        ? syms.objectType
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1190
                        : types.upperBound(iterableParams.head);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1191
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1193
            chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1194
            loopEnv.tree = tree; // before, we were not in loop!
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1195
            attribStat(tree.body, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1196
            result = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1198
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1199
            loopEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1200
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
    public void visitLabelled(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
        // Check that label is not used in an enclosing statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        Env<AttrContext> env1 = env;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1206
        while (env1 != null && !env1.tree.hasTag(CLASSDEF)) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1207
            if (env1.tree.hasTag(LABELLED) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
                ((JCLabeledStatement) env1.tree).label == tree.label) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
                log.error(tree.pos(), "label.already.in.use",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
                          tree.label);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
            env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
        attribStat(tree.body, env.dup(tree));
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
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
    public void visitSwitch(JCSwitch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
        Type seltype = attribExpr(tree.selector, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
        Env<AttrContext> switchEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            env.dup(tree, env.info.dup(env.info.scope.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1226
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1227
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1228
            boolean enumSwitch =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1229
                allowEnums &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1230
                (seltype.tsym.flags() & Flags.ENUM) != 0;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1231
            boolean stringSwitch = false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1232
            if (types.isSameType(seltype, syms.stringType)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1233
                if (allowStringsInSwitch) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1234
                    stringSwitch = true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1235
                } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1236
                    log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1237
                }
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
  1238
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1239
            if (!enumSwitch && !stringSwitch)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1240
                seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1241
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1242
            // Attribute all cases and
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1243
            // check that there are no duplicate case labels or default clauses.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1244
            Set<Object> labels = new HashSet<Object>(); // The set of case labels.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1245
            boolean hasDefault = false;      // Is there a default label?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1246
            for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1247
                JCCase c = l.head;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1248
                Env<AttrContext> caseEnv =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1249
                    switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1250
                try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1251
                    if (c.pat != null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1252
                        if (enumSwitch) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1253
                            Symbol sym = enumConstant(c.pat, seltype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1254
                            if (sym == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1255
                                log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1256
                            } else if (!labels.add(sym)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1257
                                log.error(c.pos(), "duplicate.case.label");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1258
                            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1259
                        } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1260
                            Type pattype = attribExpr(c.pat, switchEnv, seltype);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1261
                            if (!pattype.hasTag(ERROR)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1262
                                if (pattype.constValue() == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1263
                                    log.error(c.pat.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1264
                                              (stringSwitch ? "string.const.req" : "const.expr.req"));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1265
                                } else if (labels.contains(pattype.constValue())) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1266
                                    log.error(c.pos(), "duplicate.case.label");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1267
                                } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1268
                                    labels.add(pattype.constValue());
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1269
                                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1270
                            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1271
                        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1272
                    } else if (hasDefault) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1273
                        log.error(c.pos(), "duplicate.default.label");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1274
                    } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1275
                        hasDefault = true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1276
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1277
                    attribStats(c.stats, caseEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1278
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1279
                    caseEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1280
                    addVars(c.stats, switchEnv.info.scope);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1281
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1282
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1283
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1284
            result = null;
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
  1285
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1286
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1287
            switchEnv.info.scope.leave();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
        /** Add any variables defined in stats to the switch scope. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
        private static void addVars(List<JCStatement> stats, Scope switchScope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
            for (;stats.nonEmpty(); stats = stats.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                JCTree stat = stats.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1295
                if (stat.hasTag(VARDEF))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
                    switchScope.enter(((JCVariableDecl) stat).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
    /** Return the selected enumeration constant symbol, or null. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
    private Symbol enumConstant(JCTree tree, Type enumType) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1302
        if (!tree.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
            log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
            return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
        JCIdent ident = (JCIdent)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
        Name name = ident.name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
        for (Scope.Entry e = enumType.tsym.members().lookup(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
             e.scope != null; e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
            if (e.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
                Symbol s = ident.sym = e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
                ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
                ident.type = s.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
                return ((s.flags_field & Flags.ENUM) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
                    ? null : s;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
        chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
        attribStat(tree.body, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    public void visitTry(JCTry tree) {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1328
        // 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
  1329
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1330
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1331
            boolean isTryWithResource = tree.resources.nonEmpty();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1332
            // Create a nested environment for attributing the try block if needed
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1333
            Env<AttrContext> tryEnv = isTryWithResource ?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1334
                env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1335
                localEnv;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1336
            try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1337
                // Attribute resource declarations
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1338
                for (JCTree resource : tree.resources) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1339
                    CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1340
                        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1341
                        public void report(DiagnosticPosition pos, JCDiagnostic details) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1342
                            chk.basicHandler.report(pos, diags.fragment("try.not.applicable.to.type", details));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1343
                        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1344
                    };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1345
                    ResultInfo twrResult = new ResultInfo(VAL, syms.autoCloseableType, twrContext);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1346
                    if (resource.hasTag(VARDEF)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1347
                        attribStat(resource, tryEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1348
                        twrResult.check(resource, resource.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1349
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1350
                        //check that resource type cannot throw InterruptedException
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1351
                        checkAutoCloseable(resource.pos(), localEnv, resource.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1352
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
  1353
                        VarSymbol var = ((JCVariableDecl) resource).sym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1354
                        var.setData(ElementKind.RESOURCE_VARIABLE);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1355
                    } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1356
                        attribTree(resource, tryEnv, twrResult);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1357
                    }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
  1358
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1359
                // Attribute body
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1360
                attribStat(tree.body, tryEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1361
            } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1362
                if (isTryWithResource)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1363
                    tryEnv.info.scope.leave();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1364
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1365
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1366
            // Attribute catch clauses
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1367
            for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1368
                JCCatch c = l.head;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1369
                Env<AttrContext> catchEnv =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1370
                    localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1371
                try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1372
                    Type ctype = attribStat(c.param, catchEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1373
                    if (TreeInfo.isMultiCatch(c)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1374
                        //multi-catch parameter is implicitly marked as final
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1375
                        c.param.sym.flags_field |= FINAL | UNION;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1376
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1377
                    if (c.param.sym.kind == Kinds.VAR) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1378
                        c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1379
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1380
                    chk.checkType(c.param.vartype.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1381
                                  chk.checkClassType(c.param.vartype.pos(), ctype),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1382
                                  syms.throwableType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1383
                    attribStat(c.body, catchEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1384
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1385
                    catchEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1386
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1387
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1388
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1389
            // Attribute finalizer
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1390
            if (tree.finalizer != null) attribStat(tree.finalizer, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1391
            result = null;
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1392
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1393
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1394
            localEnv.info.scope.leave();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1398
    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
  1399
        if (!resource.isErroneous() &&
12013
fdcc73079b81 7148025: javac should not warn about InterrupttedException on the declaration of AutoCloseable itself
darcy
parents: 11144
diff changeset
  1400
            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
  1401
            !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
  1402
            Symbol close = syms.noSymbol;
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14443
diff changeset
  1403
            Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log);
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1404
            try {
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1405
                close = rs.resolveQualifiedMethod(pos,
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1406
                        env,
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1407
                        resource,
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1408
                        names.close,
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1409
                        List.<Type>nil(),
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1410
                        List.<Type>nil());
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1411
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1412
            finally {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14443
diff changeset
  1413
                log.popDiagnosticHandler(discardHandler);
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1414
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1415
            if (close.kind == MTH &&
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1416
                    close.overrides(syms.autoCloseableClose, resource.tsym, types, true) &&
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1417
                    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
  1418
                    env.info.lint.isEnabled(LintCategory.TRY)) {
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1419
                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
  1420
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1421
        }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1422
    }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1423
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
    public void visitConditional(JCConditional tree) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1425
        Type condtype = attribExpr(tree.cond, env, syms.booleanType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1426
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1427
        tree.polyKind = (!allowPoly ||
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1428
                pt().hasTag(NONE) && pt() != Type.recoveryType ||
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1429
                isBooleanOrNumeric(env, tree)) ?
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1430
                PolyKind.STANDALONE : PolyKind.POLY;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1431
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1432
        if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1433
            //cannot get here (i.e. it means we are returning from void method - which is already an error)
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1434
            resultInfo.checkContext.report(tree, diags.fragment("conditional.target.cant.be.void"));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1435
            result = tree.type = types.createErrorType(resultInfo.pt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1436
            return;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1437
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1438
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1439
        ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1440
                unknownExprInfo :
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1441
                resultInfo.dup(new Check.NestedCheckContext(resultInfo.checkContext) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1442
                    //this will use enclosing check context to check compatibility of
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1443
                    //subexpression against target type; if we are in a method check context,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1444
                    //depending on whether boxing is allowed, we could have incompatibilities
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1445
                    @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1446
                    public void report(DiagnosticPosition pos, JCDiagnostic details) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1447
                        enclosingContext.report(pos, diags.fragment("incompatible.type.in.conditional", details));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1448
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1449
                });
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1450
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1451
        Type truetype = attribTree(tree.truepart, env, condInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1452
        Type falsetype = attribTree(tree.falsepart, env, condInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1453
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1454
        Type owntype = (tree.polyKind == PolyKind.STANDALONE) ? condType(tree, truetype, falsetype) : pt();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1455
        if (condtype.constValue() != null &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1456
                truetype.constValue() != null &&
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14725
diff changeset
  1457
                falsetype.constValue() != null &&
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14725
diff changeset
  1458
                !owntype.hasTag(NONE)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1459
            //constant folding
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1460
            owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1461
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1462
        result = check(tree, owntype, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1465
        private boolean isBooleanOrNumeric(Env<AttrContext> env, JCExpression tree) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1466
            switch (tree.getTag()) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1467
                case LITERAL: return ((JCLiteral)tree).typetag.isSubRangeOf(DOUBLE) ||
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1468
                              ((JCLiteral)tree).typetag == BOOLEAN ||
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1469
                              ((JCLiteral)tree).typetag == BOT;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1470
                case LAMBDA: case REFERENCE: return false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1471
                case PARENS: return isBooleanOrNumeric(env, ((JCParens)tree).expr);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1472
                case CONDEXPR:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1473
                    JCConditional condTree = (JCConditional)tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1474
                    return isBooleanOrNumeric(env, condTree.truepart) &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1475
                            isBooleanOrNumeric(env, condTree.falsepart);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1476
                case APPLY:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1477
                    JCMethodInvocation speculativeMethodTree =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1478
                            (JCMethodInvocation)deferredAttr.attribSpeculative(tree, env, unknownExprInfo);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1479
                    Type owntype = TreeInfo.symbol(speculativeMethodTree.meth).type.getReturnType();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1480
                    return types.unboxedTypeOrType(owntype).isPrimitive();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1481
                case NEWCLASS:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1482
                    JCExpression className =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1483
                            removeClassParams.translate(((JCNewClass)tree).clazz);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1484
                    JCExpression speculativeNewClassTree =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1485
                            (JCExpression)deferredAttr.attribSpeculative(className, env, unknownTypeInfo);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1486
                    return types.unboxedTypeOrType(speculativeNewClassTree.type).isPrimitive();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1487
                default:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1488
                    Type speculativeType = deferredAttr.attribSpeculative(tree, env, unknownExprInfo).type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1489
                    speculativeType = types.unboxedTypeOrType(speculativeType);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1490
                    return speculativeType.isPrimitive();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1491
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1492
        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1493
        //where
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1494
            TreeTranslator removeClassParams = new TreeTranslator() {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1495
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1496
                public void visitTypeApply(JCTypeApply tree) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1497
                    result = translate(tree.clazz);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1498
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1499
            };
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1500
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
        /** Compute the type of a conditional expression, after
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1502
         *  checking that it exists.  See JLS 15.25. Does not take into
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
         *  account the special case where condition and both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
         *  are constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
         *  @param pos      The source position to be used for error
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
         *                  diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
         *  @param thentype The type of the expression's then-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
         *  @param elsetype The type of the expression's else-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
         */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1511
        private Type condType(DiagnosticPosition pos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
                               Type thentype, Type elsetype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
            // If same type, that is the result
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
            if (types.isSameType(thentype, elsetype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            Type thenUnboxed = (!allowBoxing || thentype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
                ? thentype : types.unboxedType(thentype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
            Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
                ? elsetype : types.unboxedType(elsetype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
            // Otherwise, if both arms can be converted to a numeric
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
            // type, return the least numeric type that fits both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
            // (i.e. return larger of the two, or return int if one
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
            // arm is short, the other is char).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
            if (thenUnboxed.isPrimitive() && elseUnboxed.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
                // If one arm has an integer subrange type (i.e., byte,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
                // short, or char), and the other is an integer constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
                // that fits into the subrange, return the subrange type.
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1530
                if (thenUnboxed.getTag().isStrictSubRangeOf(INT) &&
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1531
                    elseUnboxed.hasTag(INT) &&
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1532
                    types.isAssignable(elseUnboxed, thenUnboxed)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
                    return thenUnboxed.baseType();
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1534
                }
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1535
                if (elseUnboxed.getTag().isStrictSubRangeOf(INT) &&
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1536
                    thenUnboxed.hasTag(INT) &&
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1537
                    types.isAssignable(thenUnboxed, elseUnboxed)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
                    return elseUnboxed.baseType();
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1539
                }
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1540
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1541
                for (TypeTag tag : primitiveTags) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1542
                    Type candidate = syms.typeOfTag[tag.ordinal()];
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1543
                    if (types.isSubtype(thenUnboxed, candidate) &&
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1544
                        types.isSubtype(elseUnboxed, candidate)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
                        return candidate;
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1546
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
            // Those were all the cases that could result in a primitive
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
            if (allowBoxing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
                if (thentype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
                    thentype = types.boxedClass(thentype).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
                if (elsetype.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
                    elsetype = types.boxedClass(elsetype).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
            if (types.isSubtype(thentype, elsetype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
                return elsetype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
            if (types.isSubtype(elsetype, thentype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1563
            if (!allowBoxing || thentype.hasTag(VOID) || elsetype.hasTag(VOID)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
                log.error(pos, "neither.conditional.subtype",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
                          thentype, elsetype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
                return thentype.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            // both are known to be reference types.  The result is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            // lub(thentype,elsetype). This cannot fail, as it will
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
            // always be possible to infer "Object" if nothing better.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
            return types.lub(thentype.baseType(), elsetype.baseType());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1575
    final static TypeTag[] primitiveTags = new TypeTag[]{
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1576
        BYTE,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1577
        CHAR,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1578
        SHORT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1579
        INT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1580
        LONG,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1581
        FLOAT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1582
        DOUBLE,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1583
        BOOLEAN,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1584
    };
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1585
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
    public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
        attribStat(tree.thenpart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        if (tree.elsepart != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
            attribStat(tree.elsepart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
        chk.checkEmptyIf(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
    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
  1596
        //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
  1597
        //see Infer.instantiatePolymorphicSignatureInstance()
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6587
diff changeset
  1598
        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
  1599
        attribExpr(tree.expr, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
    public void visitBreak(JCBreak tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
    public void visitContinue(JCContinue tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
        /** Return the target of a break or continue statement, if it exists,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
         *  report an error if not.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
         *  Note: The target of a labelled break or continue is the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
         *  (non-labelled) statement tree referred to by the label,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
         *  not the tree representing the labelled statement itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
         *  @param pos     The position to be used for error diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
         *  @param tag     The tag of the jump statement. This is either
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
         *                 Tree.BREAK or Tree.CONTINUE.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
         *  @param label   The label of the jump statement, or null if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
         *                 label is given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
         *  @param env     The environment current at the jump statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
        private JCTree findJumpTarget(DiagnosticPosition pos,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1627
                                    JCTree.Tag tag,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
                                    Name label,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
                                    Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
            // Search environments outwards from the point of jump.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
            Env<AttrContext> env1 = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
            LOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
            while (env1 != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
                switch (env1.tree.getTag()) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1635
                    case LABELLED:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1636
                        JCLabeledStatement labelled = (JCLabeledStatement)env1.tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1637
                        if (label == labelled.label) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1638
                            // If jump is a continue, check that target is a loop.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1639
                            if (tag == CONTINUE) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1640
                                if (!labelled.body.hasTag(DOLOOP) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1641
                                        !labelled.body.hasTag(WHILELOOP) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1642
                                        !labelled.body.hasTag(FORLOOP) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1643
                                        !labelled.body.hasTag(FOREACHLOOP))
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1644
                                    log.error(pos, "not.loop.label", label);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1645
                                // Found labelled statement target, now go inwards
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1646
                                // to next non-labelled tree.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1647
                                return TreeInfo.referencedStatement(labelled);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1648
                            } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1649
                                return labelled;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1650
                            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
                        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1652
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1653
                    case DOLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1654
                    case WHILELOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1655
                    case FORLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1656
                    case FOREACHLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1657
                        if (label == null) return env1.tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1658
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1659
                    case SWITCH:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1660
                        if (label == null && tag == BREAK) return env1.tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1661
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1662
                    case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1663
                    case METHODDEF:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1664
                    case CLASSDEF:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1665
                        break LOOP;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1666
                    default:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
                env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
            if (label != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
                log.error(pos, "undef.label", label);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1672
            else if (tag == CONTINUE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
                log.error(pos, "cont.outside.loop");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
                log.error(pos, "break.outside.switch.loop");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
        // Check that there is an enclosing method which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
        // nested within than the enclosing class.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1682
        if (env.info.returnResult == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
            log.error(tree.pos(), "ret.outside.meth");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
            // Attribute return expression, if it exists, and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
            // it conforms to result type of enclosing method.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1687
            if (tree.expr != null) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1688
                if (env.info.returnResult.pt.hasTag(VOID)) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1689
                    env.info.returnResult.checkContext.report(tree.expr.pos(),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1690
                              diags.fragment("unexpected.ret.val"));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1691
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1692
                attribTree(tree.expr, env, env.info.returnResult);
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18899
diff changeset
  1693
            } else if (!env.info.returnResult.pt.hasTag(VOID) &&
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18899
diff changeset
  1694
                    !env.info.returnResult.pt.hasTag(NONE)) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1695
                env.info.returnResult.checkContext.report(tree.pos(),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1696
                              diags.fragment("missing.ret.val"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
    public void visitThrow(JCThrow tree) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1703
        Type owntype = attribExpr(tree.expr, env, allowPoly ? Type.noType : syms.throwableType);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1704
        if (allowPoly) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1705
            chk.checkType(tree, owntype, syms.throwableType);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1706
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
    public void visitAssert(JCAssert tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
        if (tree.detail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
     /** Visitor method for method invocations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
     *  NOTE: The method part of an application will have in its type field
06bc494ca11e Initial load
duke
parents:
diff changeset
  1720
     *        the return type of the method, not the method's type itself!
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
        // The local environment of a method application is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
        // The types of the actual method arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
        List<Type> argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
        // The types of the actual method type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
        List<Type> typeargtypes = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1732
06bc494ca11e Initial load
duke
parents:
diff changeset
  1733
        Name methName = TreeInfo.name(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1734
06bc494ca11e Initial load
duke
parents:
diff changeset
  1735
        boolean isConstructorCall =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
            methName == names._this || methName == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1738
        ListBuffer<Type> argtypesBuf = ListBuffer.lb();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
        if (isConstructorCall) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
            // We are seeing a ...this(...) or ...super(...) call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
            // Check that this is the first statement in a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
            if (checkFirstConstructorStat(tree, env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
                // Record the fact
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
                // that this is a constructor call (using isSelfCall).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
                localEnv.info.isSelfCall = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
                // Attribute arguments, yielding list of argument types.
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1749
                attribArgs(tree.args, localEnv, argtypesBuf);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1750
                argtypes = argtypesBuf.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
                typeargtypes = attribTypes(tree.typeargs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
                // Variable `site' points to the class in which the called
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
                // constructor is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
                Type site = env.enclClass.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
                if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
                    if (site == syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
                        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
  1759
                        site = types.createErrorType(syms.objectType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
                        site = types.supertype(site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1765
                if (site.hasTag(CLASS)) {
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  1766
                    Type encl = site.getEnclosingType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1767
                    while (encl != null && encl.hasTag(TYPEVAR))
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  1768
                        encl = encl.getUpperBound();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1769
                    if (encl.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
                        // we are calling a nested class
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1772
                        if (tree.meth.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
                            JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
                            // We are seeing a prefixed call, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
                            //     <expr>.super(...).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
                            // Check that the prefix expression conforms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
                            // to the outer instance type of the class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
                            chk.checkRefType(qualifier.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
                                             attribExpr(qualifier, localEnv,
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  1781
                                                        encl));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
                        } else if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
                            // qualifier omitted; check for existence
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
                            // of an appropriate implicit qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
                            rs.resolveImplicitThis(tree.meth.pos(),
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8616
diff changeset
  1786
                                                   localEnv, site, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
                        }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1788
                    } else if (tree.meth.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
                        log.error(tree.meth.pos(), "illegal.qual.not.icls",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
                                  site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
                    // if we're calling a java.lang.Enum constructor,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
                    // prefix the implicit String and int parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
                    if (site.tsym == syms.enumSym && allowEnums)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
                        argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
                    // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
                    // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
                    // current instance (JLS ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
                    boolean selectSuperPrev = localEnv.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
                    localEnv.info.selectSuper = true;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1803
                    localEnv.info.pendingResolutionPhase = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
                    Symbol sym = rs.resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
                        tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
                    localEnv.info.selectSuper = selectSuperPrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
                    // Set method symbol to resolved constructor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1809
                    TreeInfo.setSymbol(tree.meth, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
                    // ...and check that it is legal in the current context.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
                    // (this will also set the tree's type)
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  1813
                    Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1814
                    checkId(tree.meth, site, sym, localEnv, new ResultInfo(MTH, mpt));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
                // Otherwise, `site' is an error type and we do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
            result = tree.type = syms.voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
            // Otherwise, we are seeing a regular method call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
            // Attribute the arguments, yielding list of argument types, ...
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1822
            int kind = attribArgs(tree.args, localEnv, argtypesBuf);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1823
            argtypes = argtypesBuf.toList();
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1824
            typeargtypes = attribAnyTypes(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
            // ... and attribute the method using as a prototype a methodtype
06bc494ca11e Initial load
duke
parents:
diff changeset
  1827
            // whose formal argument types is exactly the list of actual
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
            // arguments (this will also set the method symbol).
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  1829
            Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1830
            localEnv.info.pendingResolutionPhase = null;
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  1831
            Type mtype = attribTree(tree.meth, localEnv, new ResultInfo(kind, mpt, resultInfo.checkContext));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
            // Compute the result type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
            Type restype = mtype.getReturnType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1835
            if (restype.hasTag(WILDCARD))
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6594
diff changeset
  1836
                throw new AssertionError(mtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1838
            Type qualifier = (tree.meth.hasTag(SELECT))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
                    ? ((JCFieldAccess) tree.meth).selected.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
                    : env.enclClass.sym.type;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1841
            restype = adjustMethodReturnType(qualifier, methName, argtypes, restype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
  1843
            chk.checkRefTypes(tree.typeargs, typeargtypes);
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1844
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1845
            // Check that value of resulting type is admissible in the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1846
            // 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
  1847
            result = check(tree, capture(restype), VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1848
        }
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1849
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
    //where
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1852
        Type adjustMethodReturnType(Type qualifierType, Name methodName, List<Type> argtypes, Type restype) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1853
            if (allowCovariantReturns &&
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1854
                    methodName == names.clone &&
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1855
                types.isArray(qualifierType)) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1856
                // as a special case, array.clone() has a result that is
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1857
                // the same as static type of the array being cloned
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1858
                return qualifierType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1859
            } else if (allowGenerics &&
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1860
                    methodName == names.getClass &&
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1861
                    argtypes.isEmpty()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1862
                // as a special case, x.getClass() has type Class<? extends |X|>
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1863
                return new ClassType(restype.getEnclosingType(),
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1864
                              List.<Type>of(new WildcardType(types.erasure(qualifierType),
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1865
                                                               BoundKind.EXTENDS,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1866
                                                               syms.boundClass)),
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1867
                              restype.tsym);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1868
            } else {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1869
                return restype;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1870
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1871
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1872
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
        /** Check that given application node appears as first statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
         *  in a constructor call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
         *  @param tree   The application node
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
         *  @param env    The environment current at the application.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1877
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
        boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
            JCMethodDecl enclMethod = env.enclMethod;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
            if (enclMethod != null && enclMethod.name == names.init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                JCBlock body = enclMethod.body;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1882
                if (body.stats.head.hasTag(EXEC) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                    ((JCExpressionStatement) body.stats.head).expr == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
            log.error(tree.pos(),"call.must.be.first.stmt.in.ctor",
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
                      TreeInfo.name(tree.meth));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
        /** Obtain a method type with given argument types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
         */
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  1893
        Type newMethodTemplate(Type restype, List<Type> argtypes, List<Type> typeargtypes) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1894
            MethodType mt = new MethodType(argtypes, restype, List.<Type>nil(), syms.methodClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
            return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1898
    public void visitNewClass(final 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
  1899
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
        // The local environment of a class creation is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
        // The anonymous inner class definition of the new expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
        // if one is defined by it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
        JCClassDecl cdef = tree.def;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
        // If enclosing class is given, attribute it, and
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
        // complete class name to be fully qualified
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
        JCExpression clazz = tree.clazz; // Class field following new
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1912
        JCExpression clazzid;            // Identifier in class field
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1913
        JCAnnotatedType annoclazzid;     // Annotated type enclosing clazzid
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1914
        annoclazzid = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1915
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1916
        if (clazz.hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1917
            clazzid = ((JCTypeApply) clazz).clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1918
            if (clazzid.hasTag(ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1919
                annoclazzid = (JCAnnotatedType) clazzid;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1920
                clazzid = annoclazzid.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1921
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1922
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1923
            if (clazz.hasTag(ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1924
                annoclazzid = (JCAnnotatedType) clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1925
                clazzid = annoclazzid.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1926
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1927
                clazzid = clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1928
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1929
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
        JCExpression clazzid1 = clazzid; // The same in fully qualified form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
            // We are seeing a qualified new, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
            //    <expr>.new C <...> (...) ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
            // In this case, we let clazz stand for the name of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
            // allocated class C prefixed with the type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
            // expression, so that we can
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
            // resolve it with standard techniques later. I.e., if
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
            // <expr> has type T, then <expr>.new C <...> (...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
            // yields a clazz T.C.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
            Type encltype = chk.checkRefType(tree.encl.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
                                             attribExpr(tree.encl, env));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1944
            // TODO 308: in <expr>.new C, do we also want to add the type annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1945
            // from expr to the combined type, or not? Yes, do this.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
            clazzid1 = make.at(clazz.pos).Select(make.Type(encltype),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
                                                 ((JCIdent) clazzid).name);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1948
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 18920
diff changeset
  1949
            EndPosTable endPosTable = this.env.toplevel.endPositions;
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 18920
diff changeset
  1950
            endPosTable.storeEnd(clazzid1, tree.getEndPosition(endPosTable));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1951
            if (clazz.hasTag(ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1952
                JCAnnotatedType annoType = (JCAnnotatedType) clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1953
                List<JCAnnotation> annos = annoType.annotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1954
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1955
                if (annoType.underlyingType.hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1956
                    clazzid1 = make.at(tree.pos).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1957
                        TypeApply(clazzid1,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1958
                                  ((JCTypeApply) clazz).arguments);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1959
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1960
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1961
                clazzid1 = make.at(tree.pos).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1962
                    AnnotatedType(annos, clazzid1);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1963
            } else if (clazz.hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1964
                clazzid1 = make.at(tree.pos).
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
                    TypeApply(clazzid1,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
                              ((JCTypeApply) clazz).arguments);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1967
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1968
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1969
            clazz = clazzid1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1971
06bc494ca11e Initial load
duke
parents:
diff changeset
  1972
        // Attribute clazz expression and store
06bc494ca11e Initial load
duke
parents:
diff changeset
  1973
        // 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
  1974
        Type clazztype = TreeInfo.isEnumInit(env.tree) ?
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
  1975
            attribIdentAsEnumType(env, (JCIdent)clazz) :
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
  1976
            attribType(clazz, env);
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
  1977
8635
383a416a2bdf 7020044: Project Coin: diamond erroneous allowed on some anonymous inner classes
mcimadamore
parents: 8625
diff changeset
  1978
        clazztype = chk.checkDiamond(tree, clazztype);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1979
        chk.validate(clazz, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
            // We have to work in this case to store
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
            // symbol + type back into the attributed tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
            tree.clazz.type = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
            TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
            clazzid.type = ((JCIdent) clazzid).sym.type;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1986
            if (annoclazzid != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1987
                annoclazzid.type = clazzid.type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1988
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            if (!clazztype.isErroneous()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
                if (cdef != null && clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
                    log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
                } else if (clazztype.tsym.isStatic()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
                    log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
        } else if (!clazztype.tsym.isInterface() &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1997
                   clazztype.getEnclosingType().hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
            // Check for the existence of an apropos outer instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
            rs.resolveImplicitThis(tree.pos(), env, clazztype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
        // Attribute constructor arguments.
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2003
        ListBuffer<Type> argtypesBuf = ListBuffer.lb();
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2004
        int pkind = attribArgs(tree.args, localEnv, argtypesBuf);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2005
        List<Type> argtypes = argtypesBuf.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
        List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
        // If we have made no mistakes in the class type...
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2009
        if (clazztype.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
            // Enums may not be instantiated except implicitly
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
            if (allowEnums &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2012
                (clazztype.tsym.flags_field&Flags.ENUM) != 0 &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2013
                (!env.tree.hasTag(VARDEF) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
                 (((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
                 ((JCVariableDecl) env.tree).init != tree))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
                log.error(tree.pos(), "enum.cant.be.instantiated");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
            // Check that class is not abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
            if (cdef == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
                (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
                log.error(tree.pos(), "abstract.cant.be.instantiated",
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
                          clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
            } else if (cdef != null && clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
                // Check that no constructor arguments are given to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
                // anonymous classes implementing an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
                if (!argtypes.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
                    log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
                if (!typeargtypes.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
                    log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
                // Error recovery: pretend no arguments were supplied.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
                argtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
                typeargtypes = List.nil();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2034
            } else if (TreeInfo.isDiamond(tree)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2035
                ClassType site = new ClassType(clazztype.getEnclosingType(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2036
                            clazztype.tsym.type.getTypeArguments(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2037
                            clazztype.tsym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2038
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2039
                Env<AttrContext> diamondEnv = localEnv.dup(tree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2040
                diamondEnv.info.selectSuper = cdef != null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2041
                diamondEnv.info.pendingResolutionPhase = null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2042
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2043
                //if the type of the instance creation expression is a class type
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2044
                //apply method resolution inference (JLS 15.12.2.7). The return type
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2045
                //of the resolved constructor will be a partially instantiated type
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2046
                Symbol constructor = rs.resolveDiamond(tree.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2047
                            diamondEnv,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2048
                            site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2049
                            argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2050
                            typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2051
                tree.constructor = constructor.baseSymbol();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2052
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2053
                final TypeSymbol csym = clazztype.tsym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2054
                ResultInfo diamondResult = new ResultInfo(MTH, newMethodTemplate(resultInfo.pt, argtypes, typeargtypes), new Check.NestedCheckContext(resultInfo.checkContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2055
                    @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2056
                    public void report(DiagnosticPosition _unused, JCDiagnostic details) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2057
                        enclosingContext.report(tree.clazz,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2058
                                diags.fragment("cant.apply.diamond.1", diags.fragment("diamond", csym), details));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2059
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2060
                });
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2061
                Type constructorType = tree.constructorType = types.createErrorType(clazztype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2062
                constructorType = checkId(tree, site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2063
                        constructor,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2064
                        diamondEnv,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2065
                        diamondResult);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2066
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2067
                tree.clazz.type = types.createErrorType(clazztype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2068
                if (!constructorType.isErroneous()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2069
                    tree.clazz.type = clazztype = constructorType.getReturnType();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2070
                    tree.constructorType = types.createMethodTypeWithReturn(constructorType, syms.voidType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2071
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2072
                clazztype = chk.checkClassType(tree.clazz, tree.clazz.type, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
            // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
            // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
            // current instance (JLS ???).
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2078
            else {
9724
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2079
                //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
  2080
                //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
  2081
                //order to avoid downstream failures
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2082
                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
  2083
                rsEnv.info.selectSuper = cdef != null;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2084
                rsEnv.info.pendingResolutionPhase = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
                tree.constructor = rs.resolveConstructor(
9724
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2086
                    tree.pos(), rsEnv, clazztype, argtypes, typeargtypes);
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2087
                if (cdef == null) { //do not check twice!
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2088
                    tree.constructorType = checkId(tree,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2089
                            clazztype,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2090
                            tree.constructor,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2091
                            rsEnv,
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2092
                            new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes)));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2093
                    if (rsEnv.info.lastResolveVarargs())
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2094
                        Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null);
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2095
                }
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2096
                if (cdef == null &&
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2097
                        !clazztype.isErroneous() &&
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2098
                        clazztype.getTypeArguments().nonEmpty() &&
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2099
                        findDiamonds) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2100
                    findDiamond(localEnv, tree, clazztype);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2101
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
            if (cdef != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
                // We are seeing an anonymous class instance creation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
                // In this case, the class instance creation
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
                // expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
                //    E.new <typeargs1>C<typargs2>(args) { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
                // is represented internally as
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
                //    E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } )  .
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
                // This expression is then *transformed* as follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
                // (1) add a STATIC flag to the class definition
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
                //     if the current environment is static
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
                // (2) add an extends or implements clause
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
                // (3) add a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
                // For instance, if C is a class, and ET is the type of E,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
                // the expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
                //    E.new <typeargs1>C<typargs2>(args) { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
                // is translated to (where X is a fresh name and typarams is the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
                // parameter list of the super constructor):
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
                //
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
                //   new <typeargs1>X(<*nullchk*>E, args) where
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
                //     X extends C<typargs2> {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
                //       <typarams> X(ET e, args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
                //         e.<typeargs1>super(args)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
                //       }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
                //       ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
                //     }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
                if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;
5320
e2aaa958b02d 6939618: Revert 'simple' diamond implementation
mcimadamore
parents: 5002
diff changeset
  2138
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
                if (clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
                    cdef.implementing = List.of(clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
                    cdef.extending = clazz;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
                attribStat(cdef, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2147
                checkLambdaCandidate(tree, cdef.sym, clazztype);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2148
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
                // If an outer instance is given,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                // prefix it to the constructor arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
                // and delete it from the new expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
                if (tree.encl != null && !clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
                    tree.args = tree.args.prepend(makeNullCheck(tree.encl));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
                    argtypes = argtypes.prepend(tree.encl.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
                    tree.encl = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
                // Reassign clazztype and recompute constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
                clazztype = cdef.sym.type;
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2160
                Symbol sym = tree.constructor = rs.resolveConstructor(
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2161
                    tree.pos(), localEnv, clazztype, argtypes, typeargtypes);
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2162
                Assert.check(sym.kind < AMBIGUOUS);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
                tree.constructor = sym;
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2164
                tree.constructorType = checkId(tree,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2165
                    clazztype,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2166
                    tree.constructor,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2167
                    localEnv,
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2168
                    new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes)));
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2169
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2170
                if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2171
                    checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2172
                            tree.clazz.type.tsym);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2173
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
            if (tree.constructor != null && tree.constructor.kind == MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
                owntype = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2179
        result = check(tree, owntype, VAL, resultInfo);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2180
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2182
    //where
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2183
        void findDiamond(Env<AttrContext> env, JCNewClass tree, Type clazztype) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2184
            JCTypeApply ta = (JCTypeApply)tree.clazz;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2185
            List<JCExpression> prevTypeargs = ta.arguments;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2186
            try {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2187
                //create a 'fake' diamond AST node by removing type-argument trees
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2188
                ta.arguments = List.nil();
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2189
                ResultInfo findDiamondResult = new ResultInfo(VAL,
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2190
                        resultInfo.checkContext.inferenceContext().free(resultInfo.pt) ? Type.noType : pt());
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2191
                Type inferred = deferredAttr.attribSpeculative(tree, env, findDiamondResult).type;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2192
                Type polyPt = allowPoly ?
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2193
                        syms.objectType :
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2194
                        clazztype;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2195
                if (!inferred.isErroneous() &&
19132
e2349c075315 8021338: Diamond finder may mark a required type argument as unnecessary
jlahoda
parents: 19127
diff changeset
  2196
                    (allowPoly && pt() == Infer.anyPoly ?
e2349c075315 8021338: Diamond finder may mark a required type argument as unnecessary
jlahoda
parents: 19127
diff changeset
  2197
                        types.isSameType(inferred, clazztype) :
e2349c075315 8021338: Diamond finder may mark a required type argument as unnecessary
jlahoda
parents: 19127
diff changeset
  2198
                        types.isAssignable(inferred, pt().hasTag(NONE) ? polyPt : pt(), types.noWarnings))) {
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2199
                    String key = types.isSameType(clazztype, inferred) ?
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2200
                        "diamond.redundant.args" :
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2201
                        "diamond.redundant.args.1";
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2202
                    log.warning(tree.clazz.pos(), key, clazztype, inferred);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2203
                }
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2204
            } finally {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2205
                ta.arguments = prevTypeargs;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2206
            }
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2207
        }
9075
cba34854a40e 7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents: 9074
diff changeset
  2208
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2209
            private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2210
                if (allowLambda &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2211
                        identifyLambdaCandidate &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2212
                        clazztype.hasTag(CLASS) &&
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2213
                        !pt().hasTag(NONE) &&
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2214
                        types.isFunctionalInterface(clazztype.tsym)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2215
                    Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2216
                    int count = 0;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2217
                    boolean found = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2218
                    for (Symbol sym : csym.members().getElements()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2219
                        if ((sym.flags() & SYNTHETIC) != 0 ||
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2220
                                sym.isConstructor()) continue;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2221
                        count++;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2222
                        if (sym.kind != MTH ||
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2223
                                !sym.name.equals(descriptor.name)) continue;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2224
                        Type mtype = types.memberType(clazztype, sym);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2225
                        if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2226
                            found = true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2227
                        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2228
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2229
                    if (found && count == 1) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2230
                        log.note(tree.def, "potential.lambda.found");
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2231
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2232
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2233
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2234
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2235
    private void checkForDeclarationAnnotations(List<? extends JCAnnotation> annotations,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2236
            Symbol sym) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2237
        // Ensure that no declaration annotations are present.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2238
        // Note that a tree type might be an AnnotatedType with
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2239
        // empty annotations, if only declaration annotations were given.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2240
        // This method will raise an error for such a type.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2241
        for (JCAnnotation ai : annotations) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2242
            if (TypeAnnotations.annotationType(syms, names, ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2243
                log.error(ai.pos(), "annotation.type.not.applicable");
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2244
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2245
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2246
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2247
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2248
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
    /** Make an attributed null check tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
    public JCExpression makeNullCheck(JCExpression arg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
        // optimization: X.this is never null; skip null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
        Name name = TreeInfo.name(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
        if (name == names._this || name == names._super) return arg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2256
        JCTree.Tag optag = NULLCHK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
        JCUnary tree = make.at(arg.pos).Unary(optag, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
        tree.operator = syms.nullcheck;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
        tree.type = arg.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
    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
  2264
        Type owntype = types.createErrorType(tree.type);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2265
        Env<AttrContext> localEnv = env.dup(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
        Type elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
        if (tree.elemtype != null) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2268
            elemtype = attribType(tree.elemtype, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2269
            chk.validate(tree.elemtype, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
            owntype = elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
            for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2272
                attribExpr(l.head, localEnv, syms.intType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
                owntype = new ArrayType(owntype, syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
            }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2275
            if (tree.elemtype.hasTag(ANNOTATED_TYPE)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2276
                checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2277
                        tree.elemtype.type.tsym);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  2278
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
            // we are seeing an untyped aggregate { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
            // this is allowed only if the prototype is an array
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2282
            if (pt().hasTag(ARRAY)) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2283
                elemtype = types.elemtype(pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
            } else {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2285
                if (!pt().hasTag(ERROR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
                    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
  2287
                              pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
                }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2289
                elemtype = types.createErrorType(pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
        if (tree.elems != null) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2293
            attribExprs(tree.elems, localEnv, elemtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
            owntype = new ArrayType(elemtype, syms.arrayClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
        if (!types.isReifiable(elemtype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
            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
  2298
        result = check(tree, owntype, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2301
    /*
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2302
     * A lambda expression can only be attributed when a target-type is available.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2303
     * In addition, if the target-type is that of a functional interface whose
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2304
     * descriptor contains inference variables in argument position the lambda expression
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2305
     * is 'stuck' (see DeferredAttr).
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2306
     */
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 10950
diff changeset
  2307
    @Override
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2308
    public void visitLambda(final JCLambda that) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2309
        if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2310
            if (pt().hasTag(NONE)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2311
                //lambda only allowed in assignment or method invocation/cast context
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2312
                log.error(that.pos(), "unexpected.lambda");
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2313
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2314
            result = that.type = types.createErrorType(pt());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2315
            return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2316
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2317
        //create an environment for attribution of the lambda expression
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2318
        final Env<AttrContext> localEnv = lambdaEnv(that, env);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2319
        boolean needsRecovery =
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2320
                resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2321
        try {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2322
            Type target = pt();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2323
            List<Type> explicitParamTypes = null;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2324
            if (that.paramKind == JCLambda.ParameterKind.EXPLICIT) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2325
                //attribute lambda parameters
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2326
                attribStats(that.params, localEnv);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2327
                explicitParamTypes = TreeInfo.types(that.params);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2328
                target = infer.instantiateFunctionalInterface(that, target, explicitParamTypes, resultInfo.checkContext);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2329
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2330
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2331
            Type lambdaType;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2332
            if (pt() != Type.recoveryType) {
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2333
                target = targetChecker.visit(target, that);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2334
                lambdaType = types.findDescriptorType(target);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2335
            } else {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2336
                target = Type.recoveryType;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2337
                lambdaType = fallbackDescriptorType(that);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2338
            }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2339
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2340
            setFunctionalInfo(localEnv, that, pt(), lambdaType, target, resultInfo.checkContext);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2341
14723
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2342
            if (lambdaType.hasTag(FORALL)) {
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2343
                //lambda expression target desc cannot be a generic method
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2344
                resultInfo.checkContext.report(that, diags.fragment("invalid.generic.lambda.target",
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2345
                        lambdaType, kindName(target.tsym), target.tsym));
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2346
                result = that.type = types.createErrorType(pt());
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2347
                return;
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2348
            }
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2349
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2350
            if (that.paramKind == JCLambda.ParameterKind.IMPLICIT) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2351
                //add param type info in the AST
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2352
                List<Type> actuals = lambdaType.getParameterTypes();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2353
                List<JCVariableDecl> params = that.params;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2354
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2355
                boolean arityMismatch = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2356
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2357
                while (params.nonEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2358
                    if (actuals.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2359
                        //not enough actuals to perform lambda parameter inference
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2360
                        arityMismatch = true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2361
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2362
                    //reset previously set info
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2363
                    Type argType = arityMismatch ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2364
                            syms.errType :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2365
                            actuals.head;
17584
3c4dfe34c2ec 8013222: Javac issues spurious raw type warnings when lambda has implicit parameter types
mcimadamore
parents: 17583
diff changeset
  2366
                    params.head.vartype = make.at(params.head).Type(argType);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2367
                    params.head.sym = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2368
                    actuals = actuals.isEmpty() ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2369
                            actuals :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2370
                            actuals.tail;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2371
                    params = params.tail;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2372
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2373
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2374
                //attribute lambda parameters
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2375
                attribStats(that.params, localEnv);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2376
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2377
                if (arityMismatch) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2378
                    resultInfo.checkContext.report(that, diags.fragment("incompatible.arg.types.in.lambda"));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2379
                        result = that.type = types.createErrorType(target);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2380
                        return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2381
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2382
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2383
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2384
            //from this point on, no recovery is needed; if we are in assignment context
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2385
            //we will be able to attribute the whole lambda body, regardless of errors;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2386
            //if we are in a 'check' method context, and the lambda is not compatible
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2387
            //with the target-type, it will be recovered anyway in Attr.checkId
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2388
            needsRecovery = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2389
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2390
            FunctionalReturnContext funcContext = that.getBodyKind() == JCLambda.BodyKind.EXPRESSION ?
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2391
                    new ExpressionLambdaReturnContext((JCExpression)that.getBody(), resultInfo.checkContext) :
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2392
                    new FunctionalReturnContext(resultInfo.checkContext);
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2393
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2394
            ResultInfo bodyResultInfo = lambdaType.getReturnType() == Type.recoveryType ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2395
                recoveryInfo :
19493
f2028bc02f0c 8021567: Javac doesn't report \"java: reference to method is ambiguous\" any more
mcimadamore
parents: 19132
diff changeset
  2396
                new ResultInfo(VAL, lambdaType.getReturnType(), funcContext);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2397
            localEnv.info.returnResult = bodyResultInfo;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2398
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2399
            if (that.getBodyKind() == JCLambda.BodyKind.EXPRESSION) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2400
                attribTree(that.getBody(), localEnv, bodyResultInfo);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2401
            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2402
                JCBlock body = (JCBlock)that.body;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2403
                attribStats(body.stats, localEnv);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2404
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2405
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2406
            result = check(that, target, VAL, resultInfo);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2407
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2408
            boolean isSpeculativeRound =
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2409
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2410
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2411
            preFlow(that);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2412
            flow.analyzeLambda(env, that, make, isSpeculativeRound);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2413
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2414
            checkLambdaCompatible(that, lambdaType, resultInfo.checkContext, isSpeculativeRound);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2415
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2416
            if (!isSpeculativeRound) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2417
                checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), lambdaType, target);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2418
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2419
            result = check(that, target, VAL, resultInfo);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2420
        } catch (Types.FunctionDescriptorLookupError ex) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2421
            JCDiagnostic cause = ex.getDiagnostic();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2422
            resultInfo.checkContext.report(that, cause);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2423
            result = that.type = types.createErrorType(pt());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2424
            return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2425
        } finally {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2426
            localEnv.info.scope.leave();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2427
            if (needsRecovery) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2428
                attribTree(that, env, recoveryInfo);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2429
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2430
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 10950
diff changeset
  2431
    }
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2432
    //where
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2433
        void preFlow(JCLambda tree) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2434
            new PostAttrAnalyzer() {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2435
                @Override
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2436
                public void scan(JCTree tree) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2437
                    if (tree == null ||
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2438
                            (tree.type != null &&
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2439
                            tree.type == Type.stuckType)) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2440
                        //don't touch stuck expressions!
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2441
                        return;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2442
                    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2443
                    super.scan(tree);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2444
                }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2445
            }.scan(tree);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2446
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2447
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2448
        Types.MapVisitor<DiagnosticPosition> targetChecker = new Types.MapVisitor<DiagnosticPosition>() {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2449
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2450
            @Override
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2451
            public Type visitClassType(ClassType t, DiagnosticPosition pos) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2452
                return t.isCompound() ?
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2453
                        visitIntersectionClassType((IntersectionClassType)t, pos) : t;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2454
            }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2455
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2456
            public Type visitIntersectionClassType(IntersectionClassType ict, DiagnosticPosition pos) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2457
                Symbol desc = types.findDescriptorSymbol(makeNotionalInterface(ict));
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2458
                Type target = null;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2459
                for (Type bound : ict.getExplicitComponents()) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2460
                    TypeSymbol boundSym = bound.tsym;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2461
                    if (types.isFunctionalInterface(boundSym) &&
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2462
                            types.findDescriptorSymbol(boundSym) == desc) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2463
                        target = bound;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2464
                    } else if (!boundSym.isInterface() || (boundSym.flags() & ANNOTATION) != 0) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2465
                        //bound must be an interface
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2466
                        reportIntersectionError(pos, "not.an.intf.component", boundSym);
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2467
                    }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  2468
                }
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2469
                return target != null ?
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2470
                        target :
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2471
                        ict.getExplicitComponents().head; //error recovery
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  2472
            }
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2473
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2474
            private TypeSymbol makeNotionalInterface(IntersectionClassType ict) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2475
                ListBuffer<Type> targs = ListBuffer.lb();
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2476
                ListBuffer<Type> supertypes = ListBuffer.lb();
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2477
                for (Type i : ict.interfaces_field) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2478
                    if (i.isParameterized()) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2479
                        targs.appendList(i.tsym.type.allparams());
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2480
                    }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2481
                    supertypes.append(i.tsym.type);
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2482
                }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2483
                IntersectionClassType notionalIntf =
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2484
                        (IntersectionClassType)types.makeCompoundType(supertypes.toList());
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2485
                notionalIntf.allparams_field = targs.toList();
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2486
                notionalIntf.tsym.flags_field |= INTERFACE;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2487
                return notionalIntf.tsym;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2488
            }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2489
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2490
            private void reportIntersectionError(DiagnosticPosition pos, String key, Object... args) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2491
                resultInfo.checkContext.report(pos, diags.fragment("bad.intersection.target.for.functional.expr",
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2492
                        diags.fragment(key, args)));
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2493
            }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2494
        };
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2495
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2496
        private Type fallbackDescriptorType(JCExpression tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2497
            switch (tree.getTag()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2498
                case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2499
                    JCLambda lambda = (JCLambda)tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2500
                    List<Type> argtypes = List.nil();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2501
                    for (JCVariableDecl param : lambda.params) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2502
                        argtypes = param.vartype != null ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2503
                                argtypes.append(param.vartype.type) :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2504
                                argtypes.append(syms.errType);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2505
                    }
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2506
                    return new MethodType(argtypes, Type.recoveryType,
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2507
                            List.of(syms.throwableType), syms.methodClass);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2508
                case REFERENCE:
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2509
                    return new MethodType(List.<Type>nil(), Type.recoveryType,
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2510
                            List.of(syms.throwableType), syms.methodClass);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2511
                default:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2512
                    Assert.error("Cannot get here!");
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2513
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2514
            return null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2515
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2516
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2517
        private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2518
                final InferenceContext inferenceContext, final Type... ts) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2519
            checkAccessibleTypes(pos, env, inferenceContext, List.from(ts));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2520
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2521
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2522
        private void checkAccessibleTypes(final DiagnosticPosition pos, final Env<AttrContext> env,
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2523
                final InferenceContext inferenceContext, final List<Type> ts) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2524
            if (inferenceContext.free(ts)) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2525
                inferenceContext.addFreeTypeListener(ts, new FreeTypeListener() {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2526
                    @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2527
                    public void typesInferred(InferenceContext inferenceContext) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2528
                        checkAccessibleTypes(pos, env, inferenceContext, inferenceContext.asInstTypes(ts));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2529
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2530
                });
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2531
            } else {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2532
                for (Type t : ts) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2533
                    rs.checkAccessibleType(env, t);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2534
                }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2535
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2536
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2537
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2538
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2539
         * Lambda/method reference have a special check context that ensures
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2540
         * that i.e. a lambda return type is compatible with the expected
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2541
         * type according to both the inherited context and the assignment
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2542
         * context.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2543
         */
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2544
        class FunctionalReturnContext extends Check.NestedCheckContext {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2545
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2546
            FunctionalReturnContext(CheckContext enclosingContext) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2547
                super(enclosingContext);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2548
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2549
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2550
            @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2551
            public boolean compatible(Type found, Type req, Warner warn) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2552
                //return type must be compatible in both current context and assignment context
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2553
                return chk.basicHandler.compatible(found, inferenceContext().asFree(req), warn);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2554
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2555
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2556
            @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2557
            public void report(DiagnosticPosition pos, JCDiagnostic details) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2558
                enclosingContext.report(pos, diags.fragment("incompatible.ret.type.in.lambda", details));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2559
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2560
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2561
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2562
        class ExpressionLambdaReturnContext extends FunctionalReturnContext {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2563
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2564
            JCExpression expr;
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2565
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2566
            ExpressionLambdaReturnContext(JCExpression expr, CheckContext enclosingContext) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2567
                super(enclosingContext);
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2568
                this.expr = expr;
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2569
            }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2570
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2571
            @Override
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2572
            public boolean compatible(Type found, Type req, Warner warn) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2573
                //a void return is compatible with an expression statement lambda
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2574
                return TreeInfo.isExpressionStatement(expr) && req.hasTag(VOID) ||
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2575
                        super.compatible(found, req, warn);
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2576
            }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2577
        }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2578
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2579
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2580
        * Lambda compatibility. Check that given return types, thrown types, parameter types
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2581
        * are compatible with the expected functional interface descriptor. This means that:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2582
        * (i) parameter types must be identical to those of the target descriptor; (ii) return
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2583
        * types must be compatible with the return type of the expected descriptor;
19502
f30b3ef165ea 8015809: More user friendly compile-time errors for uncaught exceptions in lambda expression
jlahoda
parents: 19493
diff changeset
  2584
        * (iii) finish inference of thrown types if required.
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2585
        */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2586
        private void checkLambdaCompatible(JCLambda tree, Type descriptor, CheckContext checkContext, boolean speculativeAttr) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2587
            Type returnType = checkContext.inferenceContext().asFree(descriptor.getReturnType());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2588
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2589
            //return values have already been checked - but if lambda has no return
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2590
            //values, we must ensure that void/value compatibility is correct;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2591
            //this amounts at checking that, if a lambda body can complete normally,
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2592
            //the descriptor's return type must be void
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2593
            if (tree.getBodyKind() == JCLambda.BodyKind.STATEMENT && tree.canCompleteNormally &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2594
                    !returnType.hasTag(VOID) && returnType != Type.recoveryType) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2595
                checkContext.report(tree, diags.fragment("incompatible.ret.type.in.lambda",
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2596
                        diags.fragment("missing.ret.val", returnType)));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2597
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2598
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2599
            List<Type> argTypes = checkContext.inferenceContext().asFree(descriptor.getParameterTypes());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2600
            if (!types.isSameTypes(argTypes, TreeInfo.types(tree.params))) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2601
                checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda"));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2602
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2603
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2604
            if (!speculativeAttr) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2605
                List<Type> thrownTypes = checkContext.inferenceContext().asFree(descriptor.getThrownTypes());
19502
f30b3ef165ea 8015809: More user friendly compile-time errors for uncaught exceptions in lambda expression
jlahoda
parents: 19493
diff changeset
  2606
                chk.unhandled(tree.inferredThrownTypes == null ? List.<Type>nil() : tree.inferredThrownTypes, thrownTypes);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2607
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2608
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2609
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2610
        private Env<AttrContext> lambdaEnv(JCLambda that, Env<AttrContext> env) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2611
            Env<AttrContext> lambdaEnv;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2612
            Symbol owner = env.info.scope.owner;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2613
            if (owner.kind == VAR && owner.owner.kind == TYP) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2614
                //field initializer
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2615
                lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dupUnshared()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2616
                lambdaEnv.info.scope.owner =
18382
26a906ea9787 8015648: Duplicate variable in lambda causes javac crash
mcimadamore
parents: 18381
diff changeset
  2617
                    new MethodSymbol((owner.flags() & STATIC) | BLOCK, names.empty, null,
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2618
                                     env.info.scope.owner);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2619
            } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2620
                lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dup()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2621
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2622
            return lambdaEnv;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2623
        }
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2624
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2625
    @Override
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2626
    public void visitReference(final JCMemberReference that) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2627
        if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2628
            if (pt().hasTag(NONE)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2629
                //method reference only allowed in assignment or method invocation/cast context
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2630
                log.error(that.pos(), "unexpected.mref");
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2631
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2632
            result = that.type = types.createErrorType(pt());
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2633
            return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2634
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2635
        final Env<AttrContext> localEnv = env.dup(that);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2636
        try {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2637
            //attribute member reference qualifier - if this is a constructor
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2638
            //reference, the expected kind must be a type
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2639
            Type exprType = attribTree(that.expr, env, memberReferenceQualifierResult(that));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2640
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2641
            if (that.getMode() == JCMemberReference.ReferenceMode.NEW) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2642
                exprType = chk.checkConstructorRefType(that.expr, exprType);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2643
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2644
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2645
            if (exprType.isErroneous()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2646
                //if the qualifier expression contains problems,
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15717
diff changeset
  2647
                //give up attribution of method reference
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2648
                result = that.type = exprType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2649
                return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2650
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2651
17583
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2652
            if (TreeInfo.isStaticSelector(that.expr, names)) {
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2653
                //if the qualifier is a type, validate it; raw warning check is
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2654
                //omitted as we don't know at this stage as to whether this is a
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2655
                //raw selector (because of inference)
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2656
                chk.validate(that.expr, env, false);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2657
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2658
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2659
            //attrib type-arguments
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2660
            List<Type> typeargtypes = List.nil();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2661
            if (that.typeargs != null) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2662
                typeargtypes = attribTypes(that.typeargs, localEnv);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2663
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2664
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2665
            Type target;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2666
            Type desc;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2667
            if (pt() != Type.recoveryType) {
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2668
                target = targetChecker.visit(pt(), that);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2669
                desc = types.findDescriptorType(target);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2670
            } else {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2671
                target = Type.recoveryType;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2672
                desc = fallbackDescriptorType(that);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2673
            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2674
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2675
            setFunctionalInfo(localEnv, that, pt(), desc, target, resultInfo.checkContext);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2676
            List<Type> argtypes = desc.getParameterTypes();
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2677
            Resolve.MethodCheck referenceCheck = rs.resolveMethodCheck;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2678
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2679
            if (resultInfo.checkContext.inferenceContext().free(argtypes)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2680
                referenceCheck = rs.new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2681
            }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2682
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2683
            Pair<Symbol, Resolve.ReferenceLookupHelper> refResult = null;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2684
            List<Type> saved_undet = resultInfo.checkContext.inferenceContext().save();
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2685
            try {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2686
                refResult = rs.resolveMemberReference(that.pos(), localEnv, that, that.expr.type,
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2687
                        that.name, argtypes, typeargtypes, true, referenceCheck,
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2688
                        resultInfo.checkContext.inferenceContext());
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2689
            } finally {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2690
                resultInfo.checkContext.inferenceContext().rollback(saved_undet);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2691
            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2692
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2693
            Symbol refSym = refResult.fst;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2694
            Resolve.ReferenceLookupHelper lookupHelper = refResult.snd;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2695
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2696
            if (refSym.kind != MTH) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2697
                boolean targetError;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2698
                switch (refSym.kind) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2699
                    case ABSENT_MTH:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2700
                        targetError = false;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2701
                        break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2702
                    case WRONG_MTH:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2703
                    case WRONG_MTHS:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2704
                    case AMBIGUOUS:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2705
                    case HIDDEN:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2706
                    case STATICERR:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2707
                    case MISSING_ENCL:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2708
                        targetError = true;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2709
                        break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2710
                    default:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2711
                        Assert.error("unexpected result kind " + refSym.kind);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2712
                        targetError = false;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2713
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2714
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2715
                JCDiagnostic detailsDiag = ((Resolve.ResolveError)refSym).getDiagnostic(JCDiagnostic.DiagnosticType.FRAGMENT,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2716
                                that, exprType.tsym, exprType, that.name, argtypes, typeargtypes);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2717
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2718
                JCDiagnostic.DiagnosticType diagKind = targetError ?
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2719
                        JCDiagnostic.DiagnosticType.FRAGMENT : JCDiagnostic.DiagnosticType.ERROR;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2720
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2721
                JCDiagnostic diag = diags.create(diagKind, log.currentSource(), that,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2722
                        "invalid.mref", Kinds.kindName(that.getMode()), detailsDiag);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2723
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2724
                if (targetError && target == Type.recoveryType) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2725
                    //a target error doesn't make sense during recovery stage
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2726
                    //as we don't know what actual parameter types are
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2727
                    result = that.type = target;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2728
                    return;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2729
                } else {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2730
                    if (targetError) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2731
                        resultInfo.checkContext.report(that, diag);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2732
                    } else {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2733
                        log.report(diag);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2734
                    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2735
                    result = that.type = types.createErrorType(target);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2736
                    return;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2737
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2738
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2739
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2740
            that.sym = refSym.baseSymbol();
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2741
            that.kind = lookupHelper.referenceKind(that.sym);
16328
8b9eb42167f6 8009129: Illegal access error when calling method reference
mcimadamore
parents: 16323
diff changeset
  2742
            that.ownerAccessible = rs.isAccessible(localEnv, that.sym.enclClass());
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2743
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2744
            if (desc.getReturnType() == Type.recoveryType) {
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2745
                // stop here
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2746
                result = that.type = target;
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2747
                return;
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2748
            }
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2749
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2750
            if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2751
17583
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2752
                if (that.getMode() == ReferenceMode.INVOKE &&
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2753
                        TreeInfo.isStaticSelector(that.expr, names) &&
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2754
                        that.kind.isUnbound() &&
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2755
                        !desc.getParameterTypes().head.isParameterized()) {
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2756
                    chk.checkRaw(that.expr, localEnv);
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2757
                }
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  2758
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2759
                if (!that.kind.isUnbound() &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2760
                        that.getMode() == ReferenceMode.INVOKE &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2761
                        TreeInfo.isStaticSelector(that.expr, names) &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2762
                        !that.sym.isStatic()) {
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2763
                    log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()),
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2764
                            diags.fragment("non-static.cant.be.ref", Kinds.kindName(refSym), refSym));
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2765
                    result = that.type = types.createErrorType(target);
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2766
                    return;
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2767
                }
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2768
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2769
                if (that.kind.isUnbound() &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2770
                        that.getMode() == ReferenceMode.INVOKE &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2771
                        TreeInfo.isStaticSelector(that.expr, names) &&
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2772
                        that.sym.isStatic()) {
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2773
                    log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()),
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2774
                            diags.fragment("static.method.in.unbound.lookup", Kinds.kindName(refSym), refSym));
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2775
                    result = that.type = types.createErrorType(target);
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2776
                    return;
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2777
                }
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  2778
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2779
                if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) &&
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2780
                        exprType.getTypeArguments().nonEmpty()) {
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2781
                    //static ref with class type-args
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2782
                    log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()),
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2783
                            diags.fragment("static.mref.with.targs"));
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2784
                    result = that.type = types.createErrorType(target);
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2785
                    return;
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2786
                }
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2787
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2788
                if (that.sym.isStatic() && !TreeInfo.isStaticSelector(that.expr, names) &&
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2789
                        !that.kind.isUnbound()) {
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2790
                    //no static bound mrefs
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2791
                    log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()),
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2792
                            diags.fragment("static.bound.mref"));
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2793
                    result = that.type = types.createErrorType(target);
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2794
                    return;
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2795
                }
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2796
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2797
                if (!refSym.isStatic() && that.kind == JCMemberReference.ReferenceKind.SUPER) {
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2798
                    // Check that super-qualified symbols are not abstract (JLS)
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2799
                    rs.checkNonAbstract(that.pos(), that.sym);
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  2800
                }
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2801
            }
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  2802
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2803
            ResultInfo checkInfo =
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2804
                    resultInfo.dup(newMethodTemplate(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2805
                        desc.getReturnType().hasTag(VOID) ? Type.noType : desc.getReturnType(),
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2806
                        that.kind.isUnbound() ? argtypes.tail : argtypes, typeargtypes));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2807
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2808
            Type refType = checkId(that, lookupHelper.site, refSym, localEnv, checkInfo);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2809
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2810
            if (that.kind.isUnbound() &&
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2811
                    resultInfo.checkContext.inferenceContext().free(argtypes.head)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2812
                //re-generate inference constraints for unbound receiver
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2813
                if (!types.isSubtype(resultInfo.checkContext.inferenceContext().asFree(argtypes.head), exprType)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2814
                    //cannot happen as this has already been checked - we just need
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2815
                    //to regenerate the inference constraints, as that has been lost
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2816
                    //as a result of the call to inferenceContext.save()
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2817
                    Assert.error("Can't get here");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2818
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2819
            }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  2820
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2821
            if (!refType.isErroneous()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2822
                refType = types.createMethodTypeWithReturn(refType,
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2823
                        adjustMethodReturnType(lookupHelper.site, that.name, checkInfo.pt.getParameterTypes(), refType.getReturnType()));
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2824
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2825
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2826
            //go ahead with standard method reference compatibility check - note that param check
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2827
            //is a no-op (as this has been taken care during method applicability)
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2828
            boolean isSpeculativeRound =
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2829
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2830
            checkReferenceCompatible(that, desc, refType, resultInfo.checkContext, isSpeculativeRound);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2831
            if (!isSpeculativeRound) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2832
                checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), desc, target);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2833
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2834
            result = check(that, target, VAL, resultInfo);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2835
        } catch (Types.FunctionDescriptorLookupError ex) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2836
            JCDiagnostic cause = ex.getDiagnostic();
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2837
            resultInfo.checkContext.report(that, cause);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2838
            result = that.type = types.createErrorType(pt());
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2839
            return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2840
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2841
    }
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2842
    //where
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2843
        ResultInfo memberReferenceQualifierResult(JCMemberReference tree) {
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2844
            //if this is a constructor reference, the expected kind must be a type
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2845
            return new ResultInfo(tree.getMode() == ReferenceMode.INVOKE ? VAL | TYP : TYP, Type.noType);
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2846
        }
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  2847
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2848
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2849
    @SuppressWarnings("fallthrough")
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2850
    void checkReferenceCompatible(JCMemberReference tree, Type descriptor, Type refType, CheckContext checkContext, boolean speculativeAttr) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2851
        Type returnType = checkContext.inferenceContext().asFree(descriptor.getReturnType());
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2852
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2853
        Type resType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2854
        switch (tree.getMode()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2855
            case NEW:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2856
                if (!tree.expr.type.isRaw()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2857
                    resType = tree.expr.type;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2858
                    break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2859
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2860
            default:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2861
                resType = refType.getReturnType();
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2862
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2863
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2864
        Type incompatibleReturnType = resType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2865
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2866
        if (returnType.hasTag(VOID)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2867
            incompatibleReturnType = null;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2868
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2869
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2870
        if (!returnType.hasTag(VOID) && !resType.hasTag(VOID)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2871
            if (resType.isErroneous() ||
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2872
                    new FunctionalReturnContext(checkContext).compatible(resType, returnType, types.noWarnings)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2873
                incompatibleReturnType = null;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2874
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2875
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2876
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2877
        if (incompatibleReturnType != null) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2878
            checkContext.report(tree, diags.fragment("incompatible.ret.type.in.mref",
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2879
                    diags.fragment("inconvertible.types", resType, descriptor.getReturnType())));
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2880
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2881
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2882
        if (!speculativeAttr) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15385
diff changeset
  2883
            List<Type> thrownTypes = checkContext.inferenceContext().asFree(descriptor.getThrownTypes());
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2884
            if (chk.unhandled(refType.getThrownTypes(), thrownTypes).nonEmpty()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2885
                log.error(tree, "incompatible.thrown.types.in.mref", refType.getThrownTypes());
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2886
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2887
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2888
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2889
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2890
    /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2891
     * Set functional type info on the underlying AST. Note: as the target descriptor
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2892
     * might contain inference variables, we might need to register an hook in the
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2893
     * current inference context.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2894
     */
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2895
    private void setFunctionalInfo(final Env<AttrContext> env, final JCFunctionalExpression fExpr,
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2896
            final Type pt, final Type descriptorType, final Type primaryTarget, final CheckContext checkContext) {
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2897
        if (checkContext.inferenceContext().free(descriptorType)) {
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2898
            checkContext.inferenceContext().addFreeTypeListener(List.of(pt, descriptorType), new FreeTypeListener() {
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2899
                public void typesInferred(InferenceContext inferenceContext) {
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2900
                    setFunctionalInfo(env, fExpr, pt, inferenceContext.asInstType(descriptorType),
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2901
                            inferenceContext.asInstType(primaryTarget), checkContext);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2902
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2903
            });
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2904
        } else {
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2905
            ListBuffer<Type> targets = ListBuffer.lb();
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2906
            if (pt.hasTag(CLASS)) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2907
                if (pt.isCompound()) {
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2908
                    targets.append(types.removeWildcards(primaryTarget)); //this goes first
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2909
                    for (Type t : ((IntersectionClassType)pt()).interfaces_field) {
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2910
                        if (t != primaryTarget) {
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2911
                            targets.append(types.removeWildcards(t));
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2912
                        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2913
                    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2914
                } else {
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2915
                    targets.append(types.removeWildcards(primaryTarget));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2916
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2917
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2918
            fExpr.targets = targets.toList();
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2919
            if (checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK &&
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2920
                    pt != Type.recoveryType) {
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2921
                //check that functional interface class is well-formed
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2922
                ClassSymbol csym = types.makeFunctionalInterfaceClass(env,
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2923
                        names.empty, List.of(fExpr.targets.head), ABSTRACT);
19127
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19122
diff changeset
  2924
                if (csym != null) {
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19122
diff changeset
  2925
                    chk.checkImplementations(env.tree, csym, csym);
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19122
diff changeset
  2926
                }
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  2927
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2928
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2929
    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2930
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2931
    public void visitParens(JCParens tree) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2932
        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
  2933
        result = check(tree, owntype, pkind(), resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2934
        Symbol sym = TreeInfo.symbol(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2935
        if (sym != null && (sym.kind&(TYP|PCK)) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2936
            log.error(tree.pos(), "illegal.start.of.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
  2937
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2938
06bc494ca11e Initial load
duke
parents:
diff changeset
  2939
    public void visitAssign(JCAssign tree) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2940
        Type owntype = attribTree(tree.lhs, env.dup(tree), varInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2941
        Type capturedType = capture(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2942
        attribExpr(tree.rhs, env, owntype);
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2943
        result = check(tree, capturedType, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2944
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2945
06bc494ca11e Initial load
duke
parents:
diff changeset
  2946
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2947
        // Attribute arguments.
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2948
        Type owntype = attribTree(tree.lhs, env, varInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2949
        Type operand = attribExpr(tree.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2950
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2951
        Symbol operator = tree.operator = rs.resolveBinaryOperator(
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2952
            tree.pos(), tree.getTag().noAssignOp(), env,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2953
            owntype, operand);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2954
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2955
        if (operator.kind == MTH &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2956
                !owntype.isErroneous() &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2957
                !operand.isErroneous()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2958
            chk.checkOperator(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2959
                              (OperatorSymbol)operator,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2960
                              tree.getTag().noAssignOp(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2961
                              owntype,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2962
                              operand);
166
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  2963
            chk.checkDivZero(tree.rhs.pos(), operator, operand);
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  2964
            chk.checkCastable(tree.rhs.pos(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  2965
                              operator.type.getReturnType(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  2966
                              owntype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2967
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2968
        result = check(tree, owntype, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2969
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2970
06bc494ca11e Initial load
duke
parents:
diff changeset
  2971
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2972
        // Attribute arguments.
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2973
        Type argtype = (tree.getTag().isIncOrDecUnaryOp())
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2974
            ? attribTree(tree.arg, env, varInfo)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2975
            : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2976
06bc494ca11e Initial load
duke
parents:
diff changeset
  2977
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2978
        Symbol operator = tree.operator =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2979
            rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2980
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
  2981
        Type owntype = types.createErrorType(tree.type);
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2982
        if (operator.kind == MTH &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  2983
                !argtype.isErroneous()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2984
            owntype = (tree.getTag().isIncOrDecUnaryOp())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2985
                ? tree.arg.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2986
                : operator.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2987
            int opc = ((OperatorSymbol)operator).opcode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2988
06bc494ca11e Initial load
duke
parents:
diff changeset
  2989
            // If the argument is constant, fold it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2990
            if (argtype.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2991
                Type ctype = cfolder.fold1(opc, argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2992
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2993
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2994
06bc494ca11e Initial load
duke
parents:
diff changeset
  2995
                    // Remove constant types from arguments to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2996
                    // conserve space. The parser will fold concatenations
06bc494ca11e Initial load
duke
parents:
diff changeset
  2997
                    // of string literals; the code here also
06bc494ca11e Initial load
duke
parents:
diff changeset
  2998
                    // gets rid of intermediate results when some of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2999
                    // operands are constant identifiers.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3000
                    if (tree.arg.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3001
                        tree.arg.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3002
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3003
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3004
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3005
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3006
        result = check(tree, owntype, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3007
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3008
06bc494ca11e Initial load
duke
parents:
diff changeset
  3009
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3010
        // Attribute arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3011
        Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3012
        Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3013
06bc494ca11e Initial load
duke
parents:
diff changeset
  3014
        // Find operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3015
        Symbol operator = tree.operator =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3016
            rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3017
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
  3018
        Type owntype = types.createErrorType(tree.type);
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3019
        if (operator.kind == MTH &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3020
                !left.isErroneous() &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3021
                !right.isErroneous()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3022
            owntype = operator.type.getReturnType();
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3023
            // This will figure out when unboxing can happen and
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3024
            // choose the right comparison operator.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3025
            int opc = chk.checkOperator(tree.lhs.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3026
                                        (OperatorSymbol)operator,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3027
                                        tree.getTag(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3028
                                        left,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3029
                                        right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3030
06bc494ca11e Initial load
duke
parents:
diff changeset
  3031
            // If both arguments are constants, fold them.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3032
            if (left.constValue() != null && right.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3033
                Type ctype = cfolder.fold2(opc, left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3034
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3035
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3036
06bc494ca11e Initial load
duke
parents:
diff changeset
  3037
                    // Remove constant types from arguments to
06bc494ca11e Initial load
duke
parents:
diff changeset
  3038
                    // conserve space. The parser will fold concatenations
06bc494ca11e Initial load
duke
parents:
diff changeset
  3039
                    // of string literals; the code here also
06bc494ca11e Initial load
duke
parents:
diff changeset
  3040
                    // gets rid of intermediate results when some of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3041
                    // operands are constant identifiers.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3042
                    if (tree.lhs.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3043
                        tree.lhs.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3044
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3045
                    if (tree.rhs.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3046
                        tree.rhs.type = syms.stringType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3047
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3048
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3049
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3050
06bc494ca11e Initial load
duke
parents:
diff changeset
  3051
            // Check that argument types of a reference ==, != are
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3052
            // castable to each other, (JLS 15.21).  Note: unboxing
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3053
            // comparisons will not have an acmp* opc at this point.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3054
            if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3055
                if (!types.isEqualityComparable(left, right,
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3056
                                                new Warner(tree.pos()))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3057
                    log.error(tree.pos(), "incomparable.types", left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3058
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3059
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3060
06bc494ca11e Initial load
duke
parents:
diff changeset
  3061
            chk.checkDivZero(tree.rhs.pos(), operator, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3062
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3063
        result = check(tree, owntype, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3064
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3065
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3066
    public void visitTypeCast(final JCTypeCast tree) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3067
        Type clazztype = attribType(tree.clazz, env);
6351
84c44db80d06 6885255: Improve usability of raw warnings
mcimadamore
parents: 6347
diff changeset
  3068
        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
  3069
        //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
  3070
        //see Infer.instantiatePolymorphicSignatureInstance()
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6587
diff changeset
  3071
        Env<AttrContext> localEnv = env.dup(tree);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3072
        //should we propagate the target type?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3073
        final ResultInfo castInfo;
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16972
diff changeset
  3074
        JCExpression expr = TreeInfo.skipParens(tree.expr);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3075
        boolean isPoly = allowPoly && (expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3076
        if (isPoly) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3077
            //expression is a poly - we need to propagate target type info
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3078
            castInfo = new ResultInfo(VAL, clazztype, new Check.NestedCheckContext(resultInfo.checkContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3079
                @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3080
                public boolean compatible(Type found, Type req, Warner warn) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3081
                    return types.isCastable(found, req, warn);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3082
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3083
            });
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3084
        } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3085
            //standalone cast - target-type info is not propagated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3086
            castInfo = unknownExprInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3087
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3088
        Type exprtype = attribTree(tree.expr, localEnv, castInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3089
        Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3090
        if (exprtype.constValue() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3091
            owntype = cfolder.coerce(exprtype, owntype);
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3092
        result = check(tree, capture(owntype), VAL, resultInfo);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3093
        if (!isPoly)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3094
            chk.checkRedundantCast(localEnv, tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3095
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3096
06bc494ca11e Initial load
duke
parents:
diff changeset
  3097
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3098
        Type exprtype = chk.checkNullOrRefType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3099
            tree.expr.pos(), attribExpr(tree.expr, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3100
        Type clazztype = chk.checkReifiableReferenceType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3101
            tree.clazz.pos(), attribType(tree.clazz, env));
6351
84c44db80d06 6885255: Improve usability of raw warnings
mcimadamore
parents: 6347
diff changeset
  3102
        chk.validate(tree.clazz, env, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3103
        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
  3104
        result = check(tree, syms.booleanType, VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3106
06bc494ca11e Initial load
duke
parents:
diff changeset
  3107
    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
  3108
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3109
        Type atype = attribExpr(tree.indexed, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3110
        attribExpr(tree.index, env, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3111
        if (types.isArray(atype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3112
            owntype = types.elemtype(atype);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3113
        else if (!atype.hasTag(ERROR))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3114
            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
  3115
        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
  3116
        result = check(tree, owntype, VAR, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3118
06bc494ca11e Initial load
duke
parents:
diff changeset
  3119
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3120
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3121
06bc494ca11e Initial load
duke
parents:
diff changeset
  3122
        // Find symbol
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3123
        if (pt().hasTag(METHOD) || pt().hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3124
            // If we are looking for a method, the prototype `pt' will be a
06bc494ca11e Initial load
duke
parents:
diff changeset
  3125
            // method type with the type of the call's arguments as parameters.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3126
            env.info.pendingResolutionPhase = null;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3127
            sym = rs.resolveMethod(tree.pos(), env, tree.name, pt().getParameterTypes(), pt().getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3128
        } else if (tree.sym != null && tree.sym.kind != VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3129
            sym = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3130
        } else {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3131
            sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3133
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3134
06bc494ca11e Initial load
duke
parents:
diff changeset
  3135
        // (1) Also find the environment current for the class where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3136
        //     sym is defined (`symEnv').
06bc494ca11e Initial load
duke
parents:
diff changeset
  3137
        // Only for pre-tiger versions (1.4 and earlier):
06bc494ca11e Initial load
duke
parents:
diff changeset
  3138
        // (2) Also determine whether we access symbol out of an anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
  3139
        //     class in a this or super call.  This is illegal for instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  3140
        //     members since such classes don't carry a this$n link.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3141
        //     (`noOuterThisPath').
06bc494ca11e Initial load
duke
parents:
diff changeset
  3142
        Env<AttrContext> symEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3143
        boolean noOuterThisPath = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3144
        if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class
06bc494ca11e Initial load
duke
parents:
diff changeset
  3145
            (sym.kind & (VAR | MTH | TYP)) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3146
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3147
            tree.name != names._this && tree.name != names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3148
06bc494ca11e Initial load
duke
parents:
diff changeset
  3149
            // Find environment in which identifier is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3150
            while (symEnv.outer != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3151
                   !sym.isMemberOf(symEnv.enclClass.sym, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3152
                if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3153
                    noOuterThisPath = !allowAnonOuterThis;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3154
                symEnv = symEnv.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3155
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3156
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3157
06bc494ca11e Initial load
duke
parents:
diff changeset
  3158
        // If symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3159
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3160
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3161
06bc494ca11e Initial load
duke
parents:
diff changeset
  3162
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  3163
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3164
            checkInit(tree, env, v, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3165
06bc494ca11e Initial load
duke
parents:
diff changeset
  3166
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  3167
            // 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
  3168
            if (pkind() == VAR)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3169
                checkAssignable(tree.pos(), v, null, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3171
06bc494ca11e Initial load
duke
parents:
diff changeset
  3172
        // In a constructor body,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3173
        // if symbol is a field or instance method, check that it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3174
        // not accessed before the supertype constructor is called.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3175
        if ((symEnv.info.isSelfCall || noOuterThisPath) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3176
            (sym.kind & (VAR | MTH)) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3177
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3178
            (sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3179
            chk.earlyRefError(tree.pos(), sym.kind == VAR ? sym : thisSym(tree.pos(), env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3181
        Env<AttrContext> env1 = env;
512
53e498fa5c0e 6657499: javac 1.6.0 fails to compile class with inner class
mcimadamore
parents: 511
diff changeset
  3182
        if (sym.kind != ERR && sym.kind != TYP && sym.owner != null && sym.owner != env1.enclClass.sym) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3183
            // If the found symbol is inaccessible, then it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3184
            // accessed through an enclosing instance.  Locate this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3185
            // enclosing instance:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3186
            while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3187
                env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3188
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3189
        result = checkId(tree, env1.enclClass.sym.type, sym, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3191
06bc494ca11e Initial load
duke
parents:
diff changeset
  3192
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3193
        // Determine the expected kind of the qualifier expression.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3194
        int skind = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3195
        if (tree.name == names._this || tree.name == names._super ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  3196
            tree.name == names._class)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3197
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3198
            skind = TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3199
        } else {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3200
            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
  3201
            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
  3202
            if ((pkind() & (VAL | MTH)) != 0) skind = skind | VAL | TYP;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3203
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3204
06bc494ca11e Initial load
duke
parents:
diff changeset
  3205
        // 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
  3206
        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
  3207
        if ((pkind() & (PCK | TYP)) == 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3208
            site = capture(site); // Capture field access
06bc494ca11e Initial load
duke
parents:
diff changeset
  3209
06bc494ca11e Initial load
duke
parents:
diff changeset
  3210
        // don't allow T.class T[].class, etc
06bc494ca11e Initial load
duke
parents:
diff changeset
  3211
        if (skind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3212
            Type elt = site;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3213
            while (elt.hasTag(ARRAY))
18644
02f65c63159c 8007546: ClassCastException on JSR308 tests
emc
parents: 18643
diff changeset
  3214
                elt = ((ArrayType)elt.unannotatedType()).elemtype;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3215
            if (elt.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3216
                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
  3217
                result = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3218
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3219
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3220
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3221
06bc494ca11e Initial load
duke
parents:
diff changeset
  3222
        // If qualifier symbol is a type or `super', assert `selectSuper'
06bc494ca11e Initial load
duke
parents:
diff changeset
  3223
        // for the selection. This is relevant for determining whether
06bc494ca11e Initial load
duke
parents:
diff changeset
  3224
        // protected symbols are accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3225
        Symbol sitesym = TreeInfo.symbol(tree.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3226
        boolean selectSuperPrev = env.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3227
        env.info.selectSuper =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3228
            sitesym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3229
            sitesym.name == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3230
06bc494ca11e Initial load
duke
parents:
diff changeset
  3231
        // Determine the symbol represented by the selection.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3232
        env.info.pendingResolutionPhase = null;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3233
        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
  3234
        if (sym.exists() && !isType(sym) && (pkind() & (PCK | TYP)) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3235
            site = capture(site);
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3236
            sym = selectSym(tree, sitesym, site, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3237
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3238
        boolean varArgs = env.info.lastResolveVarargs();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3239
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3240
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3241
        if (site.hasTag(TYPEVAR) && !isType(sym) && sym.kind != ERR) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3242
            while (site.hasTag(TYPEVAR)) site = site.getUpperBound();
511
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  3243
            site = capture(site);
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  3244
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3245
06bc494ca11e Initial load
duke
parents:
diff changeset
  3246
        // If that symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3247
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3248
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3249
06bc494ca11e Initial load
duke
parents:
diff changeset
  3250
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  3251
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3252
            checkInit(tree, env, v, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3253
06bc494ca11e Initial load
duke
parents:
diff changeset
  3254
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  3255
            // 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
  3256
            if (pkind() == VAR)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3257
                checkAssignable(tree.pos(), v, tree.selected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3259
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3260
        if (sitesym != null &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3261
                sitesym.kind == VAR &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3262
                ((VarSymbol)sitesym).isResourceVariable() &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3263
                sym.kind == MTH &&
9079
e512a7712be3 7032633: javac -Xlint:all warns about flush() within try on an auto-closeable resource
mcimadamore
parents: 9076
diff changeset
  3264
                sym.name.equals(names.close) &&
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3265
                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
  3266
                env.info.lint.isEnabled(LintCategory.TRY)) {
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  3267
            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
  3268
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3269
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3270
        // Disallow selecting a type from an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  3271
        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
  3272
            tree.type = check(tree.selected, pt(),
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3273
                              sitesym == null ? VAL : sitesym.kind, new ResultInfo(TYP|PCK, pt()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3275
06bc494ca11e Initial load
duke
parents:
diff changeset
  3276
        if (isType(sitesym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3277
            if (sym.name == names._this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3278
                // If `C' is the currently compiled class, check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3279
                // C.this' does not appear in a call to a super(...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3280
                if (env.info.isSelfCall &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3281
                    site.tsym == env.enclClass.sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3282
                    chk.earlyRefError(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3283
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3284
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3285
                // Check if type-qualified fields or methods are static (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3286
                if ((sym.flags() & STATIC) == 0 &&
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3287
                    !env.next.tree.hasTag(REFERENCE) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3288
                    sym.name != names._super &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3289
                    (sym.kind == VAR || sym.kind == MTH)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3290
                    rs.accessBase(rs.new StaticError(sym),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3291
                              tree.pos(), site, sym.name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3292
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3293
            }
5002
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 5001
diff changeset
  3294
        } 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
  3295
            // 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
  3296
            // 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
  3297
            chk.warnStatic(tree, "static.not.qualified.by.type", Kinds.kindName(sym.kind), sym.owner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3299
06bc494ca11e Initial load
duke
parents:
diff changeset
  3300
        // If we are selecting an instance member via a `super', ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3301
        if (env.info.selectSuper && (sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3302
06bc494ca11e Initial load
duke
parents:
diff changeset
  3303
            // Check that super-qualified symbols are not abstract (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3304
            rs.checkNonAbstract(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
            if (site.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3307
                // Determine argument types for site.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3308
                Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3309
                if (site1 != null) site = site1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3310
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3311
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3312
06bc494ca11e Initial load
duke
parents:
diff changeset
  3313
        env.info.selectSuper = selectSuperPrev;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3314
        result = checkId(tree, site, sym, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3316
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3317
        /** Determine symbol referenced by a Select expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3318
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3319
         *  @param tree   The select tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3320
         *  @param site   The type of the selected expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3321
         *  @param env    The current environment.
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3322
         *  @param resultInfo The current result.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3323
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3324
        private Symbol selectSym(JCFieldAccess tree,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3325
                                 Symbol location,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3326
                                 Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3327
                                 Env<AttrContext> env,
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3328
                                 ResultInfo resultInfo) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3329
            DiagnosticPosition pos = tree.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3330
            Name name = tree.name;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3331
            switch (site.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3332
            case PACKAGE:
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3333
                return rs.accessBase(
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3334
                    rs.findIdentInPackage(env, site.tsym, name, resultInfo.pkind),
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3335
                    pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3336
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3337
            case CLASS:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3338
                if (resultInfo.pt.hasTag(METHOD) || resultInfo.pt.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3339
                    return rs.resolveQualifiedMethod(
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3340
                        pos, env, location, site, name, resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3341
                } else if (name == names._this || name == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3342
                    return rs.resolveSelf(pos, env, site.tsym, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3343
                } else if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3344
                    // In this case, we have already made sure in
06bc494ca11e Initial load
duke
parents:
diff changeset
  3345
                    // visitSelect that qualifier expression is a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3346
                    Type t = syms.classType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3347
                    List<Type> typeargs = allowGenerics
06bc494ca11e Initial load
duke
parents:
diff changeset
  3348
                        ? List.of(types.erasure(site))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3349
                        : List.<Type>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3350
                    t = new ClassType(t.getEnclosingType(), typeargs, t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3351
                    return new VarSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3352
                        STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3353
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3354
                    // 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
  3355
                    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
  3356
                    if ((resultInfo.pkind & ERRONEOUS) == 0)
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3357
                        sym = rs.accessBase(sym, pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3359
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3360
            case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3361
                throw new AssertionError(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3362
            case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3363
                // Normally, site.getUpperBound() shouldn't be null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3364
                // It should only happen during memberEnter/attribBase
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3365
                // when determining the super type which *must* beac
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3366
                // done before attributing the type variables.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  3367
                // other words, we are seeing this illegal program:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3368
                // class B<T> extends A<T.foo> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3369
                Symbol sym = (site.getUpperBound() != null)
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3370
                    ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3371
                    : null;
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  3372
                if (sym == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3373
                    log.error(pos, "type.var.cant.be.deref");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3374
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3375
                } else {
1528
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  3376
                    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
  3377
                        rs.new AccessError(env, site, sym) :
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  3378
                                sym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3379
                    rs.accessBase(sym2, pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3380
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3381
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3382
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3383
                // 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
  3384
                return types.createErrorType(name, site.tsym, site).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3385
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3386
                // The qualifier expression is of a primitive type -- only
06bc494ca11e Initial load
duke
parents:
diff changeset
  3387
                // .class is allowed for these.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3388
                if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3389
                    // In this case, we have already made sure in Select that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3390
                    // qualifier expression is a type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3391
                    Type t = syms.classType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3392
                    Type arg = types.boxedClass(site).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3393
                    t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3394
                    return new VarSymbol(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3395
                        STATIC | PUBLIC | FINAL, names._class, t, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3396
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3397
                    log.error(pos, "cant.deref", site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3398
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3399
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3400
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3401
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3402
06bc494ca11e Initial load
duke
parents:
diff changeset
  3403
        /** Determine type of identifier or select expression and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3404
         *  (1) the referenced symbol is not deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  3405
         *  (2) the symbol's type is safe (@see checkSafe)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3406
         *  (3) if symbol is a variable, check that its type and kind are
06bc494ca11e Initial load
duke
parents:
diff changeset
  3407
         *      compatible with the prototype and protokind.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3408
         *  (4) if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3409
         *      which is being assigned to, issue an unchecked warning if its
06bc494ca11e Initial load
duke
parents:
diff changeset
  3410
         *      type changes under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3411
         *  (5) if symbol is an instance method of a raw type, issue an
06bc494ca11e Initial load
duke
parents:
diff changeset
  3412
         *      unchecked warning if its argument types change under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3413
         *  If checks succeed:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3414
         *    If symbol is a constant, return its constant type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3415
         *    else if symbol is a method, return its result type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3416
         *    otherwise return its type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3417
         *  Otherwise return errType.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3418
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3419
         *  @param tree       The syntax tree representing the identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  3420
         *  @param site       If this is a select, the type of the selected
06bc494ca11e Initial load
duke
parents:
diff changeset
  3421
         *                    expression, otherwise the type of the current class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3422
         *  @param sym        The symbol representing the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3423
         *  @param env        The current environment.
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3424
         *  @param resultInfo    The expected result
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3425
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3426
        Type checkId(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3427
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3428
                     Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3429
                     Env<AttrContext> env,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3430
                     ResultInfo resultInfo) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3431
            return (resultInfo.pt.hasTag(FORALL) || resultInfo.pt.hasTag(METHOD)) ?
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3432
                    checkMethodId(tree, site, sym, env, resultInfo) :
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3433
                    checkIdInternal(tree, site, sym, resultInfo.pt, env, resultInfo);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3434
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3435
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3436
        Type checkMethodId(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3437
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3438
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3439
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3440
                     ResultInfo resultInfo) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3441
            boolean isPolymorhicSignature =
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 18382
diff changeset
  3442
                (sym.baseSymbol().flags() & SIGNATURE_POLYMORPHIC) != 0;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3443
            return isPolymorhicSignature ?
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3444
                    checkSigPolyMethodId(tree, site, sym, env, resultInfo) :
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3445
                    checkMethodIdInternal(tree, site, sym, env, resultInfo);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3446
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3447
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3448
        Type checkSigPolyMethodId(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3449
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3450
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3451
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3452
                     ResultInfo resultInfo) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3453
            //recover original symbol for signature polymorphic methods
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3454
            checkMethodIdInternal(tree, site, sym.baseSymbol(), env, resultInfo);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3455
            env.info.pendingResolutionPhase = Resolve.MethodResolutionPhase.BASIC;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3456
            return sym.type;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3457
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3458
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3459
        Type checkMethodIdInternal(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3460
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3461
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3462
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3463
                     ResultInfo resultInfo) {
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3464
            if ((resultInfo.pkind & POLY) != 0) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3465
                Type pt = resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, sym, env.info.pendingResolutionPhase));
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3466
                Type owntype = checkIdInternal(tree, site, sym, pt, env, resultInfo);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3467
                resultInfo.pt.map(deferredAttr.new RecoveryDeferredTypeMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase));
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3468
                return owntype;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3469
            } else {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3470
                return checkIdInternal(tree, site, sym, resultInfo.pt, env, resultInfo);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3471
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3472
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3473
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3474
        Type checkIdInternal(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3475
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3476
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3477
                     Type pt,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3478
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3479
                     ResultInfo resultInfo) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3480
            if (pt.isErroneous()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3481
                return types.createErrorType(site);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3482
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3483
            Type owntype; // The computed type of this identifier occurrence.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3484
            switch (sym.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3485
            case TYP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3486
                // For types, the computed type equals the symbol's type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3487
                // except for two situations:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3488
                owntype = sym.type;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3489
                if (owntype.hasTag(CLASS)) {
14369
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 14366
diff changeset
  3490
                    chk.checkForBadAuxiliaryClassAccess(tree.pos(), env, (ClassSymbol)sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3491
                    Type ownOuter = owntype.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
                    // (a) If the symbol's type is parameterized, erase it
06bc494ca11e Initial load
duke
parents:
diff changeset
  3494
                    // because no type parameters were given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3495
                    // We recover generic outer type later in visitTypeApply.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3496
                    if (owntype.tsym.type.getTypeArguments().nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3497
                        owntype = types.erasure(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3498
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3499
06bc494ca11e Initial load
duke
parents:
diff changeset
  3500
                    // (b) If the symbol's type is an inner class, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  3501
                    // we have to interpret its outer type as a superclass
06bc494ca11e Initial load
duke
parents:
diff changeset
  3502
                    // of the site type. Example:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3503
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  3504
                    // class Tree<A> { class Visitor { ... } }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3505
                    // class PointTree extends Tree<Point> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3506
                    // ...PointTree.Visitor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3507
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  3508
                    // Then the type of the last expression above is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3509
                    // Tree<Point>.Visitor.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3510
                    else if (ownOuter.hasTag(CLASS) && site != ownOuter) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3511
                        Type normOuter = site;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3512
                        if (normOuter.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3513
                            normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
16556
f4adc5bb4652 8008425: Remove interim new javax.lang.model API for type-annotations
jjg
parents: 16333
diff changeset
  3514
                            if (site.isAnnotated()) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3515
                                // Propagate any type annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3516
                                // TODO: should asEnclosingSuper do this?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3517
                                // Note that the type annotations in site will be updated
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3518
                                // by annotateType. Therefore, modify site instead
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3519
                                // of creating a new AnnotatedType.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3520
                                ((AnnotatedType)site).underlyingType = normOuter;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3521
                                normOuter = site;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3522
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3523
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3524
                        if (normOuter == null) // perhaps from an import
06bc494ca11e Initial load
duke
parents:
diff changeset
  3525
                            normOuter = types.erasure(ownOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3526
                        if (normOuter != ownOuter)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3527
                            owntype = new ClassType(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3528
                                normOuter, List.<Type>nil(), owntype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3529
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3530
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3531
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3532
            case VAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3533
                VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3534
                // Test (4): if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3535
                // which is being assigned to, issue an unchecked warning if
06bc494ca11e Initial load
duke
parents:
diff changeset
  3536
                // its type changes under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3537
                if (allowGenerics &&
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3538
                    resultInfo.pkind == VAR &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3539
                    v.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3540
                    (v.flags() & STATIC) == 0 &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3541
                    (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3542
                    Type s = types.asOuterSuper(site, v.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3543
                    if (s != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3544
                        s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3545
                        !types.isSameType(v.type, v.erasure(types))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3546
                        chk.warnUnchecked(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3547
                                          "unchecked.assign.to.var",
06bc494ca11e Initial load
duke
parents:
diff changeset
  3548
                                          v, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3549
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3550
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3551
                // The computed type of a variable is the type of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  3552
                // variable symbol, taken as a member of the site type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3553
                owntype = (sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3554
                           sym.name != names._this && sym.name != names._super)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3555
                    ? types.memberType(site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3556
                    : sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3557
06bc494ca11e Initial load
duke
parents:
diff changeset
  3558
                // If the variable is a constant, record constant value in
06bc494ca11e Initial load
duke
parents:
diff changeset
  3559
                // computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3560
                if (v.getConstValue() != null && isStaticReference(tree))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3561
                    owntype = owntype.constType(v.getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3562
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3563
                if (resultInfo.pkind == VAL) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3564
                    owntype = capture(owntype); // capture "names as expressions"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3565
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3566
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3567
            case MTH: {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  3568
                owntype = checkMethod(site, sym,
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3569
                        new ResultInfo(resultInfo.pkind, resultInfo.pt.getReturnType(), resultInfo.checkContext),
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  3570
                        env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(),
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3571
                        resultInfo.pt.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3572
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3573
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3574
            case PCK: case ERR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3575
                owntype = sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3576
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3577
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3578
                throw new AssertionError("unexpected kind: " + sym.kind +
06bc494ca11e Initial load
duke
parents:
diff changeset
  3579
                                         " in tree " + tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3581
06bc494ca11e Initial load
duke
parents:
diff changeset
  3582
            // Test (1): emit a `deprecation' warning if symbol is deprecated.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3583
            // (for constructors, the error was given when the constructor was
06bc494ca11e Initial load
duke
parents:
diff changeset
  3584
            // resolved)
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
  3585
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
  3586
            if (sym.name != names.init) {
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
  3587
                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
  3588
                chk.checkSunAPI(tree.pos(), sym);
15724
3063fb01c8a1 8004182: Add support for profiles in javac
jjg
parents: 14953
diff changeset
  3589
                chk.checkProfile(tree.pos(), sym);
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 3559
diff changeset
  3590
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3591
06bc494ca11e Initial load
duke
parents:
diff changeset
  3592
            // Test (3): if symbol is a variable, check that its type and
06bc494ca11e Initial load
duke
parents:
diff changeset
  3593
            // 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
  3594
            return check(tree, owntype, sym.kind, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3595
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3596
06bc494ca11e Initial load
duke
parents:
diff changeset
  3597
        /** Check that variable is initialized and evaluate the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
  3598
         *  initializer, if not yet done. Also check that variable is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  3599
         *  referenced before it is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3600
         *  @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3601
         *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3602
         *  @param v       The variable's symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3603
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3604
        private void checkInit(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3605
                               Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
                               VarSymbol v,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3607
                               boolean onlyWarning) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
//          System.err.println(v + " " + ((v.flags() & STATIC) != 0) + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  3609
//                             tree.pos + " " + v.pos + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
  3610
//                             Resolve.isStatic(env));//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  3611
06bc494ca11e Initial load
duke
parents:
diff changeset
  3612
            // A forward reference is diagnosed if the declaration position
06bc494ca11e Initial load
duke
parents:
diff changeset
  3613
            // of the variable is greater than the current tree position
06bc494ca11e Initial load
duke
parents:
diff changeset
  3614
            // and the tree and variable definition occur in the same class
06bc494ca11e Initial load
duke
parents:
diff changeset
  3615
            // definition.  Note that writes don't count as references.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3616
            // This check applies only to class and instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  3617
            // variables.  Local variables follow different scope rules,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3618
            // 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
  3619
            if ((env.info.enclVar == v || v.pos > tree.pos) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3620
                v.owner.kind == TYP &&
13439
3025d6ac1401 7175538: Integrate efectively final check with DA/DU analysis
mcimadamore
parents: 13438
diff changeset
  3621
                canOwnInitializer(owner(env)) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3622
                v.owner == env.info.scope.owner.enclClass() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3623
                ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  3624
                (!env.tree.hasTag(ASSIGN) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
                 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
  3626
                String suffix = (env.info.enclVar == v) ?
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  3627
                                "self.ref" : "forward.ref";
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3628
                if (!onlyWarning || isStaticEnumField(v)) {
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  3629
                    log.error(tree.pos(), "illegal." + suffix);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3630
                } else if (useBeforeDeclarationWarning) {
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  3631
                    log.warning(tree.pos(), suffix, v);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3632
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3633
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3634
06bc494ca11e Initial load
duke
parents:
diff changeset
  3635
            v.getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
  3636
06bc494ca11e Initial load
duke
parents:
diff changeset
  3637
            checkEnumInitializer(tree, env, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3638
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3639
06bc494ca11e Initial load
duke
parents:
diff changeset
  3640
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  3641
         * Check for illegal references to static members of enum.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  3642
         * an enum type, constructors and initializers may not
06bc494ca11e Initial load
duke
parents:
diff changeset
  3643
         * reference its static members unless they are constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3644
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3645
         * @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3646
         * @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3647
         * @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
  3648
         * @jls  section 8.9 Enums
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3649
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
        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
  3651
            // JLS:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3652
            //
06bc494ca11e Initial load
duke
parents:
diff changeset
  3653
            // "It is a compile-time error to reference a static field
06bc494ca11e Initial load
duke
parents:
diff changeset
  3654
            // of an enum type that is not a compile-time constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  3655
            // (15.28) from constructors, instance initializer blocks,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3656
            // or instance variable initializer expressions of that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3657
            // type. It is a compile-time error for the constructors,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3658
            // instance initializer blocks, or instance variable
06bc494ca11e Initial load
duke
parents:
diff changeset
  3659
            // initializer expressions of an enum constant e to refer
06bc494ca11e Initial load
duke
parents:
diff changeset
  3660
            // to itself or to an enum constant of the same type that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3661
            // is declared to the right of e."
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3662
            if (isStaticEnumField(v)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3663
                ClassSymbol enclClass = env.info.scope.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3664
06bc494ca11e Initial load
duke
parents:
diff changeset
  3665
                if (enclClass == null || enclClass.owner == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3666
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3667
06bc494ca11e Initial load
duke
parents:
diff changeset
  3668
                // See if the enclosing class is the enum (or a
06bc494ca11e Initial load
duke
parents:
diff changeset
  3669
                // subclass thereof) declaring v.  If not, this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3670
                // reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3671
                if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3672
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3673
06bc494ca11e Initial load
duke
parents:
diff changeset
  3674
                // If the reference isn't from an initializer, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  3675
                // the reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3676
                if (!Resolve.isInitializer(env))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3678
06bc494ca11e Initial load
duke
parents:
diff changeset
  3679
                log.error(tree.pos(), "illegal.enum.static.ref");
06bc494ca11e Initial load
duke
parents:
diff changeset
  3680
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3681
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3683
        /** 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
  3684
         *  Note: enum literals should not be regarded as such
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3685
         */
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3686
        private boolean isStaticEnumField(VarSymbol v) {
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3687
            return Flags.isEnum(v.owner) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3688
                   Flags.isStatic(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3689
                   !Flags.isConstant(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  3690
                   v.name != names._class;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3691
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3692
06bc494ca11e Initial load
duke
parents:
diff changeset
  3693
        /** Can the given symbol be the owner of code which forms part
06bc494ca11e Initial load
duke
parents:
diff changeset
  3694
         *  if class initialization? This is the case if the symbol is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3695
         *  a type or field, or if the symbol is the synthetic method.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3696
         *  owning a block.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3697
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3698
        private boolean canOwnInitializer(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
            return
06bc494ca11e Initial load
duke
parents:
diff changeset
  3700
                (sym.kind & (VAR | TYP)) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  3701
                (sym.kind == MTH && (sym.flags() & BLOCK) != 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3702
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3703
06bc494ca11e Initial load
duke
parents:
diff changeset
  3704
    Warner noteWarner = new Warner();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3705
06bc494ca11e Initial load
duke
parents:
diff changeset
  3706
    /**
12080
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  3707
     * Check that method arguments conform to its instantiation.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3708
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
  3709
    public Type checkMethod(Type site,
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3710
                            final Symbol sym,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  3711
                            ResultInfo resultInfo,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  3712
                            Env<AttrContext> env,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  3713
                            final List<JCExpression> argtrees,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  3714
                            List<Type> argtypes,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3715
                            List<Type> typeargtypes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3716
        // Test (5): if symbol is an instance method of a raw type, issue
06bc494ca11e Initial load
duke
parents:
diff changeset
  3717
        // an unchecked warning if its argument types change under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3718
        if (allowGenerics &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3719
            (sym.flags() & STATIC) == 0 &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3720
            (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
            Type s = types.asOuterSuper(site, sym.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3722
            if (s != null && s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3723
                !types.isSameTypes(sym.type.getParameterTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3724
                                   sym.erasure(types).getParameterTypes())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3725
                chk.warnUnchecked(env.tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  3726
                                  "unchecked.call.mbr.of.raw.type",
06bc494ca11e Initial load
duke
parents:
diff changeset
  3727
                                  sym, s);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3728
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3729
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3730
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3731
        if (env.info.defaultSuperCallSite != null) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3732
            for (Type sup : types.interfaces(env.enclClass.type).prepend(types.supertype((env.enclClass.type)))) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3733
                if (!sup.tsym.isSubClass(sym.enclClass(), types) ||
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3734
                        types.isSameType(sup, env.info.defaultSuperCallSite)) continue;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3735
                List<MethodSymbol> icand_sup =
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3736
                        types.interfaceCandidates(sup, (MethodSymbol)sym);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3737
                if (icand_sup.nonEmpty() &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3738
                        icand_sup.head != sym &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3739
                        icand_sup.head.overrides(sym, icand_sup.head.enclClass(), types, true)) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3740
                    log.error(env.tree.pos(), "illegal.default.super.call", env.info.defaultSuperCallSite,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3741
                        diags.fragment("overridden.default", sym, sup));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3742
                    break;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  3743
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  3744
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3745
            env.info.defaultSuperCallSite = null;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  3746
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  3747
16330
66e277848a69 8009299: Javac crashes when compiling method reference to static interface method
mcimadamore
parents: 16328
diff changeset
  3748
        if (sym.isStatic() && site.isInterface() && env.tree.hasTag(APPLY)) {
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3749
            JCMethodInvocation app = (JCMethodInvocation)env.tree;
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3750
            if (app.meth.hasTag(SELECT) &&
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3751
                    !TreeInfo.isStaticSelector(((JCFieldAccess)app.meth).selected, names)) {
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3752
                log.error(env.tree.pos(), "illegal.static.intf.meth.call", site);
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3753
            }
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3754
        }
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  3755
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3756
        // Compute the identifier's instantiated type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3757
        // For methods, we need to compute the instance type by
06bc494ca11e Initial load
duke
parents:
diff changeset
  3758
        // Resolve.instantiate from the symbol's type as well as
06bc494ca11e Initial load
duke
parents:
diff changeset
  3759
        // any type arguments and value arguments.
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  3760
        noteWarner.clear();
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3761
        try {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3762
            Type owntype = rs.checkMethod(
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3763
                    env,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3764
                    site,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3765
                    sym,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3766
                    resultInfo,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3767
                    argtypes,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3768
                    typeargtypes,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3769
                    noteWarner);
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3770
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3771
            DeferredAttr.DeferredTypeMap checkDeferredMap =
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3772
                deferredAttr.new DeferredTypeMap(DeferredAttr.AttrMode.CHECK, sym, env.info.pendingResolutionPhase);
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3773
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3774
            argtypes = Type.map(argtypes, checkDeferredMap);
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3775
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3776
            if (noteWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3777
                chk.warnUnchecked(env.tree.pos(),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3778
                        "unchecked.meth.invocation.applied",
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3779
                        kindName(sym),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3780
                        sym.name,
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3781
                        rs.methodArguments(sym.type.getParameterTypes()),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3782
                        rs.methodArguments(Type.map(argtypes, checkDeferredMap)),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3783
                        kindName(sym.location()),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3784
                        sym.location());
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3785
               owntype = new MethodType(owntype.getParameterTypes(),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3786
                       types.erasure(owntype.getReturnType()),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3787
                       types.erasure(owntype.getThrownTypes()),
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3788
                       syms.methodClass);
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3789
            }
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3790
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3791
            return chk.checkMethod(owntype, sym, env, argtrees, argtypes, env.info.lastResolveVarargs(),
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  3792
                    resultInfo.checkContext.inferenceContext());
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3793
        } catch (Infer.InferenceException ex) {
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3794
            //invalid target type - propagate exception outwards or report error
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3795
            //depending on the current check context
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3796
            resultInfo.checkContext.report(env.tree.pos(), ex.getDiagnostic());
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3797
            return types.createErrorType(site);
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3798
        } catch (Resolve.InapplicableMethodException ex) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3799
            final JCDiagnostic diag = ex.getDiagnostic();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3800
            Resolve.InapplicableSymbolError errSym = rs.new InapplicableSymbolError(null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3801
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3802
                protected Pair<Symbol, JCDiagnostic> errCandidate() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3803
                    return new Pair<Symbol, JCDiagnostic>(sym, diag);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3804
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3805
            };
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3806
            List<Type> argtypes2 = Type.map(argtypes,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3807
                    rs.new ResolveDeferredRecoveryMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3808
            JCDiagnostic errDiag = errSym.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3809
                    env.tree, sym, site, sym.name, argtypes2, typeargtypes);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3810
            log.report(errDiag);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  3811
            return types.createErrorType(site);
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  3812
        }
12080
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  3813
    }
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  3814
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3815
    public void visitLiteral(JCLiteral tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
        result = check(
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3817
            tree, litType(tree.typetag).constType(tree.value), VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3818
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3819
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3820
    /** Return the type of a literal with given type tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3821
     */
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3822
    Type litType(TypeTag tag) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3823
        return (tag == CLASS) ? syms.stringType : syms.typeOfTag[tag.ordinal()];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3824
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
06bc494ca11e Initial load
duke
parents:
diff changeset
  3826
    public void visitTypeIdent(JCPrimitiveTypeTree tree) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3827
        result = check(tree, syms.typeOfTag[tree.typetag.ordinal()], TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3828
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3829
06bc494ca11e Initial load
duke
parents:
diff changeset
  3830
    public void visitTypeArray(JCArrayTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3831
        Type etype = attribType(tree.elemtype, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3832
        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
  3833
        result = check(tree, type, TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3834
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3835
06bc494ca11e Initial load
duke
parents:
diff changeset
  3836
    /** Visitor method for parameterized types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3837
     *  Bound checking is left until later, since types are attributed
06bc494ca11e Initial load
duke
parents:
diff changeset
  3838
     *  before supertype structure is completely known
06bc494ca11e Initial load
duke
parents:
diff changeset
  3839
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3840
    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
  3841
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
        // Attribute functor part of application and make sure it's a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3844
        Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3845
06bc494ca11e Initial load
duke
parents:
diff changeset
  3846
        // Attribute type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  3847
        List<Type> actuals = attribTypes(tree.arguments, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3848
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3849
        if (clazztype.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3850
            List<Type> formals = clazztype.tsym.type.getTypeArguments();
10198
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  3851
            if (actuals.isEmpty()) //diamond
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  3852
                actuals = formals;
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  3853
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  3854
            if (actuals.length() == formals.length()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3855
                List<Type> a = actuals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3856
                List<Type> f = formals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3857
                while (a.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3858
                    a.head = a.head.withTypeVar(f.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
                    a = a.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3860
                    f = f.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3861
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3862
                // Compute the proper generic outer
06bc494ca11e Initial load
duke
parents:
diff changeset
  3863
                Type clazzOuter = clazztype.getEnclosingType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3864
                if (clazzOuter.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3865
                    Type site;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  3866
                    JCExpression clazz = TreeInfo.typeIn(tree.clazz);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  3867
                    if (clazz.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3868
                        site = env.enclClass.sym.type;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  3869
                    } else if (clazz.hasTag(SELECT)) {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  3870
                        site = ((JCFieldAccess) clazz).selected.type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3871
                    } else throw new AssertionError(""+tree);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3872
                    if (clazzOuter.hasTag(CLASS) && site != clazzOuter) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3873
                        if (site.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3874
                            site = types.asOuterSuper(site, clazzOuter.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
                        if (site == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3876
                            site = types.erasure(clazzOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3877
                        clazzOuter = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3878
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
                }
5320
e2aaa958b02d 6939618: Revert 'simple' diamond implementation
mcimadamore
parents: 5002
diff changeset
  3880
                owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3881
                if (clazztype.isAnnotated()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3882
                    // Use the same AnnotatedType, because it will have
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3883
                    // its annotations set later.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3884
                    ((AnnotatedType)clazztype).underlyingType = owntype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3885
                    owntype = clazztype;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  3886
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3888
                if (formals.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
                    log.error(tree.pos(), "wrong.number.type.args",
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
                              Integer.toString(formals.length()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3891
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3892
                    log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
                }
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
  3894
                owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3895
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3897
        result = check(tree, owntype, TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9079
diff changeset
  3900
    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
  3901
        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
  3902
        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
  3903
        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
  3904
            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
  3905
            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
  3906
                          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
  3907
                          syms.throwableType);
9074
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3908
            if (!ctype.isErroneous()) {
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9079
diff changeset
  3909
                //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
  3910
                //unrelated w.r.t. subtyping
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3911
                if (chk.intersects(ctype,  multicatchTypes.toList())) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3912
                    for (Type t : multicatchTypes) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3913
                        boolean sub = types.isSubtype(ctype, t);
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3914
                        boolean sup = types.isSubtype(t, ctype);
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3915
                        if (sub || sup) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3916
                            //assume 'a' <: 'b'
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3917
                            Type a = sub ? ctype : t;
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3918
                            Type b = sub ? t : ctype;
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3919
                            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
  3920
                        }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3921
                    }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3922
                }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3923
                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
  3924
                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
  3925
                    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
  3926
            } else {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  3927
                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
  3928
                    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
  3929
                    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
  3930
                }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  3931
                all_multicatchTypes.append(ctype);
9074
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  3932
            }
7622
ee16e77d07f0 7002070: If catch clause has an incompatible type, error pointer points to first exception type in list
mcimadamore
parents: 7618
diff changeset
  3933
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3934
        Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, resultInfo);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3935
        if (t.hasTag(CLASS)) {
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  3936
            List<Type> alternatives =
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  3937
                ((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
  3938
            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
  3939
        }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  3940
        tree.type = result = t;
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5489
diff changeset
  3941
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5489
diff changeset
  3942
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3943
    public void visitTypeIntersection(JCTypeIntersection tree) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3944
        attribTypes(tree.bounds, env);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3945
        tree.type = result = checkIntersection(tree, tree.bounds);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3946
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3947
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3948
    public void visitTypeParameter(JCTypeParameter tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3949
        TypeVar typeVar = (TypeVar) tree.type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3950
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3951
        if (tree.annotations != null && tree.annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3952
            AnnotatedType antype = new AnnotatedType(typeVar);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3953
            annotateType(antype, tree.annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3954
            tree.type = antype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3955
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3956
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3957
        if (!typeVar.bound.isErroneous()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3958
            //fixup type-parameter bound computed in 'attribTypeVariables'
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3959
            typeVar.bound = checkIntersection(tree, tree.bounds);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3960
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3961
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3962
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3963
    Type checkIntersection(JCTree tree, List<JCExpression> bounds) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3964
        Set<Type> boundSet = new HashSet<Type>();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3965
        if (bounds.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3966
            // accept class or interface or typevar as first bound.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3967
            bounds.head.type = checkBase(bounds.head.type, bounds.head, env, false, false, false);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3968
            boundSet.add(types.erasure(bounds.head.type));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3969
            if (bounds.head.type.isErroneous()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3970
                return bounds.head.type;
1532
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  3971
            }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3972
            else if (bounds.head.type.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3973
                // if first bound was a typevar, do not accept further bounds.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3974
                if (bounds.tail.nonEmpty()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3975
                    log.error(bounds.tail.head.pos(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3976
                              "type.var.may.not.be.followed.by.other.bounds");
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3977
                    return bounds.head.type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3978
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3979
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3980
                // if first bound was a class or interface, accept only interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  3981
                // as further bounds.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3982
                for (JCExpression bound : bounds.tail) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3983
                    bound.type = checkBase(bound.type, bound, env, false, true, false);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3984
                    if (bound.type.isErroneous()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3985
                        bounds = List.of(bound);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3986
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3987
                    else if (bound.type.hasTag(CLASS)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3988
                        chk.checkNotRepeated(bound.pos(), types.erasure(bound.type), boundSet);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3989
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3990
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3991
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3992
        }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3993
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3994
        if (bounds.length() == 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3995
            return syms.objectType;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3996
        } else if (bounds.length() == 1) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3997
            return bounds.head.type;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3998
        } else {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  3999
            Type owntype = types.makeCompoundType(TreeInfo.types(bounds));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4000
            if (tree.hasTag(TYPEINTERSECTION)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4001
                ((IntersectionClassType)owntype).intersectionKind =
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4002
                        IntersectionClassType.IntersectionKind.EXPLICIT;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4003
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4004
            // ... the variable's bound is a class type flagged COMPOUND
06bc494ca11e Initial load
duke
parents:
diff changeset
  4005
            // (see comment for TypeVar.bound).
06bc494ca11e Initial load
duke
parents:
diff changeset
  4006
            // In this case, generate a class tree that represents the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4007
            // bound class, ...
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8622
diff changeset
  4008
            JCExpression extending;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4009
            List<JCExpression> implementing;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4010
            if (!bounds.head.type.isInterface()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4011
                extending = bounds.head;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4012
                implementing = bounds.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4013
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4014
                extending = null;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4015
                implementing = bounds;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4016
            }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4017
            JCClassDecl cd = make.at(tree).ClassDef(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4018
                make.Modifiers(PUBLIC | ABSTRACT),
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4019
                names.empty, List.<JCTypeParameter>nil(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4020
                extending, implementing, List.<JCTree>nil());
06bc494ca11e Initial load
duke
parents:
diff changeset
  4021
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4022
            ClassSymbol c = (ClassSymbol)owntype.tsym;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4023
            Assert.check((c.flags() & COMPOUND) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4024
            cd.sym = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4025
            c.sourcefile = env.toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4026
06bc494ca11e Initial load
duke
parents:
diff changeset
  4027
            // ... and attribute the bound class
06bc494ca11e Initial load
duke
parents:
diff changeset
  4028
            c.flags_field |= UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4029
            Env<AttrContext> cenv = enter.classEnv(cd, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4030
            enter.typeEnvs.put(c, cenv);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4031
            attribClass(c);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4032
            return owntype;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4033
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4034
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4035
06bc494ca11e Initial load
duke
parents:
diff changeset
  4036
    public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4037
        //- System.err.println("visitWildcard("+tree+");");//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  4038
        Type type = (tree.kind.kind == BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4039
            ? syms.objectType
06bc494ca11e Initial load
duke
parents:
diff changeset
  4040
            : attribType(tree.inner, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4041
        result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type),
06bc494ca11e Initial load
duke
parents:
diff changeset
  4042
                                              tree.kind.kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4043
                                              syms.boundClass),
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  4044
                       TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4045
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4046
06bc494ca11e Initial load
duke
parents:
diff changeset
  4047
    public void visitAnnotation(JCAnnotation tree) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  4048
        log.error(tree.pos(), "annotation.not.valid.for.type", pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4049
        result = tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4050
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4051
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4052
    public void visitAnnotatedType(JCAnnotatedType tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4053
        Type underlyingType = attribType(tree.getUnderlyingType(), env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4054
        this.attribAnnotationTypes(tree.annotations, env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4055
        AnnotatedType antype = new AnnotatedType(underlyingType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4056
        annotateType(antype, tree.annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4057
        result = tree.type = antype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4058
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4059
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4060
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4061
     * Apply the annotations to the particular type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4062
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4063
    public void annotateType(final AnnotatedType type, final List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4064
        if (annotations.isEmpty())
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4065
            return;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4066
        annotate.typeAnnotation(new Annotate.Annotator() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4067
            @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4068
            public String toString() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4069
                return "annotate " + annotations + " onto " + type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4070
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4071
            @Override
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4072
            public void enterAnnotation() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4073
                List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4074
                type.typeAnnotations = compounds;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4075
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4076
        });
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4077
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4078
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4079
    private static List<Attribute.TypeCompound> fromAnnotations(List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4080
        if (annotations.isEmpty())
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4081
            return List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4082
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4083
        ListBuffer<Attribute.TypeCompound> buf = ListBuffer.lb();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4084
        for (JCAnnotation anno : annotations) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4085
            if (anno.attribute != null) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4086
                // TODO: this null-check is only needed for an obscure
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4087
                // ordering issue, where annotate.flush is called when
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4088
                // the attribute is not set yet. For an example failure
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4089
                // try the referenceinfos/NestedTypes.java test.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4090
                // Any better solutions?
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4091
                buf.append((Attribute.TypeCompound) anno.attribute);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4092
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4093
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4094
        return buf.toList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4095
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4096
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4097
    public void visitErroneous(JCErroneous tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4098
        if (tree.errs != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4099
            for (JCTree err : tree.errs)
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  4100
                attribTree(err, env, new ResultInfo(ERR, pt()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4101
        result = tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4103
06bc494ca11e Initial load
duke
parents:
diff changeset
  4104
    /** Default visitor method for all other trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4106
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4107
        throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4109
8845
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4110
    /**
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4111
     * 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
  4112
     */
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4113
    public void attrib(Env<AttrContext> env) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4114
        if (env.tree.hasTag(TOPLEVEL))
8845
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4115
            attribTopLevel(env);
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4116
        else
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4117
            attribClass(env.tree.pos(), env.enclClass.sym);
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4118
    }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4119
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4120
    /**
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4121
     * Attribute a top level tree. These trees are encountered when the
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4122
     * package declaration has annotations.
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4123
     */
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4124
    public void attribTopLevel(Env<AttrContext> env) {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4125
        JCCompilationUnit toplevel = env.toplevel;
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4126
        try {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4127
            annotate.flush();
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4128
            chk.validateAnnotations(toplevel.packageAnnotations, toplevel.packge);
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4129
        } catch (CompletionFailure ex) {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4130
            chk.completionError(toplevel.pos(), ex);
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4131
        }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4132
    }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4133
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4134
    /** Main method: attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4135
     *  reporting completion failures at the given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4136
     *  @param pos The source position at which completion errors are to be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4137
     *             reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4138
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4140
    public void attribClass(DiagnosticPosition pos, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4141
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4142
            annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4143
            attribClass(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4144
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4145
            chk.completionError(pos, ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4147
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4148
06bc494ca11e Initial load
duke
parents:
diff changeset
  4149
    /** Attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4150
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4152
    void attribClass(ClassSymbol c) throws CompletionFailure {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4153
        if (c.type.hasTag(ERROR)) return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4154
06bc494ca11e Initial load
duke
parents:
diff changeset
  4155
        // Check for cycles in the inheritance graph, which can arise from
06bc494ca11e Initial load
duke
parents:
diff changeset
  4156
        // ill-formed class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4157
        chk.checkNonCyclic(null, c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4158
06bc494ca11e Initial load
duke
parents:
diff changeset
  4159
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4160
        if ((c.flags_field & Flags.COMPOUND) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4161
            // First, attribute superclass.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4162
            if (st.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4163
                attribClass((ClassSymbol)st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4164
06bc494ca11e Initial load
duke
parents:
diff changeset
  4165
            // Next attribute owner, if it is a class.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4166
            if (c.owner.kind == TYP && c.owner.type.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4167
                attribClass((ClassSymbol)c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4169
06bc494ca11e Initial load
duke
parents:
diff changeset
  4170
        // The previous operations might have attributed the current class
06bc494ca11e Initial load
duke
parents:
diff changeset
  4171
        // if there was a cycle. So we test first whether the class is still
06bc494ca11e Initial load
duke
parents:
diff changeset
  4172
        // UNATTRIBUTED.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4173
        if ((c.flags_field & UNATTRIBUTED) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4174
            c.flags_field &= ~UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4175
06bc494ca11e Initial load
duke
parents:
diff changeset
  4176
            // Get environment current at the point of class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4177
            Env<AttrContext> env = enter.typeEnvs.get(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4178
06bc494ca11e Initial load
duke
parents:
diff changeset
  4179
            // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4180
            // because the annotations were not available at the time the env was created. Therefore,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4181
            // we look up the environment chain for the first enclosing environment for which the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4182
            // lint value is set. Typically, this is the parent env, but might be further if there
06bc494ca11e Initial load
duke
parents:
diff changeset
  4183
            // are any envs created as a result of TypeParameter nodes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4184
            Env<AttrContext> lintEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4185
            while (lintEnv.info.lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4186
                lintEnv = lintEnv.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4187
06bc494ca11e Initial load
duke
parents:
diff changeset
  4188
            // Having found the enclosing lint value, we can initialize the lint value for this class
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  4189
            env.info.lint = lintEnv.info.lint.augment(c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4190
06bc494ca11e Initial load
duke
parents:
diff changeset
  4191
            Lint prevLint = chk.setLint(env.info.lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4192
            JavaFileObject prev = log.useSource(c.sourcefile);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4193
            ResultInfo prevReturnRes = env.info.returnResult;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4194
06bc494ca11e Initial load
duke
parents:
diff changeset
  4195
            try {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4196
                env.info.returnResult = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4197
                // java.lang.Enum may not be subclassed by a non-enum
06bc494ca11e Initial load
duke
parents:
diff changeset
  4198
                if (st.tsym == syms.enumSym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4199
                    ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4200
                    log.error(env.tree.pos(), "enum.no.subclassing");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4201
06bc494ca11e Initial load
duke
parents:
diff changeset
  4202
                // Enums may not be extended by source-level classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  4203
                if (st.tsym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4204
                    ((st.tsym.flags_field & Flags.ENUM) != 0) &&
16558
07c08ad4a700 8010179: Remove transitional target values from javac
darcy
parents: 16557
diff changeset
  4205
                    ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4206
                    log.error(env.tree.pos(), "enum.types.not.extensible");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4207
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4208
                attribClassBody(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4209
06bc494ca11e Initial load
duke
parents:
diff changeset
  4210
                chk.checkDeprecatedAnnotation(env.tree.pos(), c);
16333
a6e1ded87200 8009138: javac, equals-hashCode warning tuning
vromero
parents: 16331
diff changeset
  4211
                chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4212
            } finally {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4213
                env.info.returnResult = prevReturnRes;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4214
                log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4215
                chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4216
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4217
06bc494ca11e Initial load
duke
parents:
diff changeset
  4218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4220
06bc494ca11e Initial load
duke
parents:
diff changeset
  4221
    public void visitImport(JCImport tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4222
        // nothing to do
06bc494ca11e Initial load
duke
parents:
diff changeset
  4223
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4224
06bc494ca11e Initial load
duke
parents:
diff changeset
  4225
    /** Finish the attribution of a class. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4226
    private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4227
        JCClassDecl tree = (JCClassDecl)env.tree;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4228
        Assert.check(c == tree.sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4229
06bc494ca11e Initial load
duke
parents:
diff changeset
  4230
        // Validate annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  4231
        chk.validateAnnotations(tree.mods.annotations, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4232
06bc494ca11e Initial load
duke
parents:
diff changeset
  4233
        // Validate type parameters, supertype and interfaces.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4234
        attribStats(tree.typarams, env);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4235
        if (!c.isAnonymous()) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4236
            //already checked if anonymous
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4237
            chk.validate(tree.typarams, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4238
            chk.validate(tree.extending, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4239
            chk.validate(tree.implementing, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4240
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4241
06bc494ca11e Initial load
duke
parents:
diff changeset
  4242
        // If this is a non-abstract class, check that it has no abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
  4243
        // methods or unimplemented methods of an implemented interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4244
        if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4245
            if (!relax)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4246
                chk.checkAllDefined(tree.pos(), c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4247
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4248
06bc494ca11e Initial load
duke
parents:
diff changeset
  4249
        if ((c.flags() & ANNOTATION) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4250
            if (tree.implementing.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  4251
                log.error(tree.implementing.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  4252
                          "cant.extend.intf.annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4253
            if (tree.typarams.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  4254
                log.error(tree.typarams.head.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  4255
                          "intf.annotation.cant.have.type.params");
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4256
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4257
            // If this annotation has a @Repeatable, validate
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4258
            Attribute.Compound repeatable = c.attribute(syms.repeatableType.tsym);
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4259
            if (repeatable != null) {
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4260
                // get diagnostic position for error reporting
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4261
                DiagnosticPosition cbPos = getDiagnosticPosition(tree, repeatable.type);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4262
                Assert.checkNonNull(cbPos);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4263
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4264
                chk.validateRepeatable(c, repeatable, cbPos);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4265
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4266
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4267
            // Check that all extended classes and interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  4268
            // are compatible (i.e. no two define methods with same arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  4269
            // yet different return types).  (JLS 8.4.6.3)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4270
            chk.checkCompatibleSupertypes(tree.pos(), c.type);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4271
            if (allowDefaultMethods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4272
                chk.checkDefaultMethodClashes(tree.pos(), c.type);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4273
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4275
06bc494ca11e Initial load
duke
parents:
diff changeset
  4276
        // Check that class does not import the same parameterized interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  4277
        // with two different argument lists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4278
        chk.checkClassBounds(tree.pos(), c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4279
06bc494ca11e Initial load
duke
parents:
diff changeset
  4280
        tree.type = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4281
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4282
        for (List<JCTypeParameter> l = tree.typarams;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4283
             l.nonEmpty(); l = l.tail) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4284
             Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4285
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4286
06bc494ca11e Initial load
duke
parents:
diff changeset
  4287
        // Check that a generic class doesn't extend Throwable
06bc494ca11e Initial load
duke
parents:
diff changeset
  4288
        if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4289
            log.error(tree.extending.pos(), "generic.throwable");
06bc494ca11e Initial load
duke
parents:
diff changeset
  4290
06bc494ca11e Initial load
duke
parents:
diff changeset
  4291
        // Check that all methods which implement some
06bc494ca11e Initial load
duke
parents:
diff changeset
  4292
        // method conform to the method they implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4293
        chk.checkImplementations(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4294
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  4295
        //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
  4296
        checkAutoCloseable(tree.pos(), env, c.type);
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  4297
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4298
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4299
            // Attribute declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  4300
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4301
            // Check that declarations in inner classes are not static (JLS 8.1.2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4302
            // Make an exception for static constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4303
            if (c.owner.kind != PCK &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4304
                ((c.flags() & STATIC) == 0 || c.name == names.empty) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4305
                (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4306
                Symbol sym = null;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4307
                if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4308
                if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  4309
                    sym.kind != VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  4310
                    ((VarSymbol) sym).getConstValue() == null)
8239
d2c934e951e2 7014715: javac returns different error code for certain failure(s)
mcimadamore
parents: 8237
diff changeset
  4311
                    log.error(l.head.pos(), "icls.cant.have.static.decl", c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4312
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4314
06bc494ca11e Initial load
duke
parents:
diff changeset
  4315
        // Check for cycles among non-initial constructors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4316
        chk.checkCyclicConstructors(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4317
06bc494ca11e Initial load
duke
parents:
diff changeset
  4318
        // Check for cycles among annotation elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4319
        chk.checkNonCyclicElements(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4320
06bc494ca11e Initial load
duke
parents:
diff changeset
  4321
        // Check for proper use of serialVersionUID
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4322
        if (env.info.lint.isEnabled(LintCategory.SERIAL) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4323
            isSerializable(c) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4324
            (c.flags() & Flags.ENUM) == 0 &&
18899
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4325
            checkForSerial(c)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4326
            checkSerialVersionUID(tree, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4327
        }
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4328
        if (allowTypeAnnos) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4329
            // Correctly organize the postions of the type annotations
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4330
            TypeAnnotations.organizeTypeAnnotationsBodies(this.syms, this.names, this.log, tree);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4331
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4332
            // Check type annotations applicability rules
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4333
            validateTypeAnnotations(tree);
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  4334
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4335
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4336
        // where
18899
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4337
        boolean checkForSerial(ClassSymbol c) {
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4338
            if ((c.flags() & ABSTRACT) == 0) {
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4339
                return true;
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4340
            } else {
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4341
                return c.members().anyMatch(anyNonAbstractOrDefaultMethod);
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4342
            }
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4343
        }
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4344
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4345
        public static final Filter<Symbol> anyNonAbstractOrDefaultMethod = new Filter<Symbol>() {
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4346
            @Override
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4347
            public boolean accepts(Symbol s) {
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4348
                return s.kind == Kinds.MTH &&
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4349
                       (s.flags() & (DEFAULT | ABSTRACT)) != ABSTRACT;
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4350
            }
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4351
        };
2557b27d1f1c 6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
vromero
parents: 18662
diff changeset
  4352
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4353
        /** 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
  4354
        private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4355
            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
  4356
                if (types.isSameType(al.head.annotationType.type, t))
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4357
                    return al.head.pos();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4358
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4359
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4360
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4361
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4362
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4363
        /** check if a class is a subtype of Serializable, if that is available. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4364
        private boolean isSerializable(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4365
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4366
                syms.serializableType.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4368
            catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4369
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4370
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4371
            return types.isSubtype(c.type, syms.serializableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4372
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4373
06bc494ca11e Initial load
duke
parents:
diff changeset
  4374
        /** Check that an appropriate serialVersionUID member is defined. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4375
        private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4376
06bc494ca11e Initial load
duke
parents:
diff changeset
  4377
            // check for presence of serialVersionUID
06bc494ca11e Initial load
duke
parents:
diff changeset
  4378
            Scope.Entry e = c.members().lookup(names.serialVersionUID);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4379
            while (e.scope != null && e.sym.kind != VAR) e = e.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4380
            if (e.scope == null) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4381
                log.warning(LintCategory.SERIAL,
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 6148
diff changeset
  4382
                        tree.pos(), "missing.SVUID", c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4383
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4384
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4385
06bc494ca11e Initial load
duke
parents:
diff changeset
  4386
            // check that it is static final
06bc494ca11e Initial load
duke
parents:
diff changeset
  4387
            VarSymbol svuid = (VarSymbol)e.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4388
            if ((svuid.flags() & (STATIC | FINAL)) !=
06bc494ca11e Initial load
duke
parents:
diff changeset
  4389
                (STATIC | FINAL))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4390
                log.warning(LintCategory.SERIAL,
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 6148
diff changeset
  4391
                        TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4392
06bc494ca11e Initial load
duke
parents:
diff changeset
  4393
            // check that it is long
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4394
            else if (!svuid.type.hasTag(LONG))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4395
                log.warning(LintCategory.SERIAL,
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 6148
diff changeset
  4396
                        TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4397
06bc494ca11e Initial load
duke
parents:
diff changeset
  4398
            // check constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  4399
            else if (svuid.getConstValue() == null)
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4400
                log.warning(LintCategory.SERIAL,
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 6148
diff changeset
  4401
                        TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4402
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4403
06bc494ca11e Initial load
duke
parents:
diff changeset
  4404
    private Type capture(Type type) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  4405
        return types.capture(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4406
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  4407
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4408
    private void validateTypeAnnotations(JCTree tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4409
        tree.accept(typeAnnotationsValidator);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4410
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4411
    //where
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4412
    private final JCTree.Visitor typeAnnotationsValidator = new TreeScanner() {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4413
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4414
        private boolean checkAllAnnotations = false;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4415
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4416
        public void visitAnnotation(JCAnnotation tree) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4417
            if (tree.hasTag(TYPE_ANNOTATION) || checkAllAnnotations) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4418
                chk.validateTypeAnnotation(tree, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4419
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4420
            super.visitAnnotation(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4421
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4422
        public void visitTypeParameter(JCTypeParameter tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4423
            chk.validateTypeAnnotations(tree.annotations, true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4424
            scan(tree.bounds);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4425
            // Don't call super.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4426
            // This is needed because above we call validateTypeAnnotation with
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4427
            // false, which would forbid annotations on type parameters.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4428
            // super.visitTypeParameter(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4429
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4430
        public void visitMethodDef(JCMethodDecl tree) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4431
            if (tree.recvparam != null &&
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4432
                    tree.recvparam.vartype.type.getKind() != TypeKind.ERROR) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4433
                checkForDeclarationAnnotations(tree.recvparam.mods.annotations,
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4434
                        tree.recvparam.vartype.type.tsym);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4435
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4436
            if (tree.restype != null && tree.restype.type != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4437
                validateAnnotatedType(tree.restype, tree.restype.type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4438
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4439
            super.visitMethodDef(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4440
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4441
        public void visitVarDef(final JCVariableDecl tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4442
            if (tree.sym != null && tree.sym.type != null)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4443
                validateAnnotatedType(tree, tree.sym.type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4444
            super.visitVarDef(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4445
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4446
        public void visitTypeCast(JCTypeCast tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4447
            if (tree.clazz != null && tree.clazz.type != null)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4448
                validateAnnotatedType(tree.clazz, tree.clazz.type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4449
            super.visitTypeCast(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4450
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4451
        public void visitTypeTest(JCInstanceOf tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4452
            if (tree.clazz != null && tree.clazz.type != null)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4453
                validateAnnotatedType(tree.clazz, tree.clazz.type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4454
            super.visitTypeTest(tree);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4455
        }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4456
        public void visitNewClass(JCNewClass tree) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4457
            if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4458
                boolean prevCheck = this.checkAllAnnotations;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4459
                try {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4460
                    this.checkAllAnnotations = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4461
                    scan(((JCAnnotatedType)tree.clazz).annotations);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4462
                } finally {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4463
                    this.checkAllAnnotations = prevCheck;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4464
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4465
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4466
            super.visitNewClass(tree);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4467
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4468
        public void visitNewArray(JCNewArray tree) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4469
            if (tree.elemtype != null && tree.elemtype.hasTag(ANNOTATED_TYPE)) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4470
                boolean prevCheck = this.checkAllAnnotations;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4471
                try {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4472
                    this.checkAllAnnotations = true;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4473
                    scan(((JCAnnotatedType)tree.elemtype).annotations);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4474
                } finally {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4475
                    this.checkAllAnnotations = prevCheck;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4476
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4477
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4478
            super.visitNewArray(tree);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  4479
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4480
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4481
        /* I would want to model this after
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4482
         * com.sun.tools.javac.comp.Check.Validator.visitSelectInternal(JCFieldAccess)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4483
         * and override visitSelect and visitTypeApply.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4484
         * However, we only set the annotated type in the top-level type
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4485
         * of the symbol.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4486
         * Therefore, we need to override each individual location where a type
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4487
         * can occur.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4488
         */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4489
        private void validateAnnotatedType(final JCTree errtree, final Type type) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4490
            if (type.getEnclosingType() != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4491
                    type != type.getEnclosingType()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4492
                validateEnclosingAnnotatedType(errtree, type.getEnclosingType());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4493
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4494
            for (Type targ : type.getTypeArguments()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4495
                validateAnnotatedType(errtree, targ);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4496
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4497
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4498
        private void validateEnclosingAnnotatedType(final JCTree errtree, final Type type) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4499
            validateAnnotatedType(errtree, type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4500
            if (type.tsym != null &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4501
                    type.tsym.isStatic() &&
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents: 16556
diff changeset
  4502
                    type.getAnnotationMirrors().nonEmpty()) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4503
                    // Enclosing static classes cannot have type annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4504
                log.error(errtree.pos(), "cant.annotate.static.class");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4505
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4506
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4507
    };
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4508
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4509
    // <editor-fold desc="post-attribution visitor">
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4510
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4511
    /**
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4512
     * 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
  4513
     * 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
  4514
     * 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
  4515
     * 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
  4516
     * prevents NPE to be progagated during subsequent compilation steps.
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4517
     */
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  4518
    public void postAttr(JCTree tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  4519
        new PostAttrAnalyzer().scan(tree);
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4520
    }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4521
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4522
    class PostAttrAnalyzer extends TreeScanner {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4523
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4524
        private void initTypeIfNeeded(JCTree that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4525
            if (that.type == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4526
                that.type = syms.unknownType;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4527
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4528
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4529
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4530
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4531
        public void scan(JCTree tree) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4532
            if (tree == null) return;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4533
            if (tree instanceof JCExpression) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4534
                initTypeIfNeeded(tree);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4535
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4536
            super.scan(tree);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4537
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4538
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4539
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4540
        public void visitIdent(JCIdent that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4541
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4542
                that.sym = syms.unknownSymbol;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4543
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4544
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4545
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4546
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4547
        public void visitSelect(JCFieldAccess that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4548
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4549
                that.sym = syms.unknownSymbol;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4550
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4551
            super.visitSelect(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4552
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4553
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4554
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4555
        public void visitClassDef(JCClassDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4556
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4557
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4558
                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
  4559
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4560
            super.visitClassDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4561
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4562
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4563
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4564
        public void visitMethodDef(JCMethodDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4565
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4566
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4567
                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
  4568
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4569
            super.visitMethodDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4570
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4571
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4572
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4573
        public void visitVarDef(JCVariableDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4574
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4575
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4576
                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
  4577
                that.sym.adr = 0;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4578
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4579
            super.visitVarDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4580
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4581
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4582
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4583
        public void visitNewClass(JCNewClass that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4584
            if (that.constructor == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4585
                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
  4586
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4587
            if (that.constructorType == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4588
                that.constructorType = syms.unknownType;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4589
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4590
            super.visitNewClass(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4591
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4592
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4593
        @Override
10187
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4594
        public void visitAssignop(JCAssignOp that) {
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4595
            if (that.operator == null)
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4596
                that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol);
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4597
            super.visitAssignop(that);
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4598
        }
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4599
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  4600
        @Override
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4601
        public void visitBinary(JCBinary that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4602
            if (that.operator == null)
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4603
                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
  4604
            super.visitBinary(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4605
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4606
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4607
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4608
        public void visitUnary(JCUnary that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4609
            if (that.operator == null)
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4610
                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
  4611
            super.visitUnary(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4612
        }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4613
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4614
        @Override
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4615
        public void visitLambda(JCLambda that) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4616
            super.visitLambda(that);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4617
            if (that.targets == null) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4618
                that.targets = List.nil();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4619
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4620
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4621
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4622
        @Override
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4623
        public void visitReference(JCMemberReference that) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4624
            super.visitReference(that);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4625
            if (that.sym == null) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4626
                that.sym = new MethodSymbol(0, names.empty, syms.unknownType, syms.noSymbol);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4627
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4628
            if (that.targets == null) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4629
                that.targets = List.nil();
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  4630
            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  4631
        }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4632
    }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  4633
    // </editor-fold>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4634
}