src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
author jlahoda
Mon, 21 Oct 2019 15:38:26 +0200
changeset 58713 ad69fd32778e
parent 58350 e23e560afbcb
child 58906 d58a21542c04
permissions -rw-r--r--
8226585: Improve javac messages for using a preview API Summary: Avoiding deprecation for removal for APIs associated with preview features, the features are marked with an annotation, and errors/warnings are produced for them based on the annotation. Reviewed-by: erikj, mcimadamore, alanb Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
53275
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
     2
 * Copyright (c) 1999, 2019, 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.*;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
    29
import java.util.function.BiConsumer;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
    30
import java.util.stream.Collectors;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    31
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    32
import javax.lang.model.element.ElementKind;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    33
import javax.tools.JavaFileObject;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 58350
diff changeset
    35
import com.sun.source.tree.CaseTree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.source.tree.IdentifierTree;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
    37
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.source.tree.MemberSelectTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.source.tree.TreeVisitor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.source.util.SimpleTreeVisitor;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    41
import com.sun.tools.javac.code.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    42
import com.sun.tools.javac.code.Lint.LintCategory;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
    43
import com.sun.tools.javac.code.Scope.WriteableScope;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
    44
import com.sun.tools.javac.code.Source.Feature;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    45
import com.sun.tools.javac.code.Symbol.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    46
import com.sun.tools.javac.code.Type.*;
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
    47
import com.sun.tools.javac.code.TypeMetadata.Annotations;
29553
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
    48
import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
    49
import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    50
import com.sun.tools.javac.comp.Check.CheckContext;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    51
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    52
import com.sun.tools.javac.jvm.*;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    53
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    54
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArg;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    55
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArgs;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    56
import com.sun.tools.javac.resources.CompilerProperties.Errors;
29553
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
    57
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
    58
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    59
import com.sun.tools.javac.tree.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    60
import com.sun.tools.javac.tree.JCTree.*;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
    61
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    62
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26107
diff changeset
    63
import com.sun.tools.javac.util.DefinedBy.Api;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    64
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
    65
import com.sun.tools.javac.util.JCDiagnostic.Error;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    66
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
    67
import com.sun.tools.javac.util.JCDiagnostic.Warning;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 14062
diff changeset
    68
import com.sun.tools.javac.util.List;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
    69
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    71
import static com.sun.tools.javac.code.Flags.ANNOTATION;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    72
import static com.sun.tools.javac.code.Flags.BLOCK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
import static com.sun.tools.javac.code.Kinds.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
    74
import static com.sun.tools.javac.code.Kinds.Kind.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
    75
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
    76
import static com.sun.tools.javac.code.TypeTag.WILDCARD;
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
    77
import com.sun.tools.javac.comp.Analyzer.AnalyzerMode;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
    78
import static com.sun.tools.javac.tree.JCTree.Tag.*;
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40313
diff changeset
    79
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
    80
import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
    81
import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
/** This is the main context-dependent analysis phase in GJC. It
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 *  encompasses name resolution, type checking and constant folding as
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 *  subtasks. Some subtasks involve auxiliary classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 *  @see Check
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 *  @see Resolve
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 *  @see ConstFold
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 *  @see Infer
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    91
 *  <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
    92
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
public class Attr extends JCTree.Visitor {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
    97
    protected static final Context.Key<Attr> attrKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
    99
    final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    final Resolve rs;
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
   103
    final Operators operators;
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   104
    final Infer infer;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27857
diff changeset
   105
    final Analyzer analyzer;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   106
    final DeferredAttr deferredAttr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    final Check chk;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   108
    final Flow flow;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    final MemberEnter memberEnter;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27856
diff changeset
   110
    final TypeEnter typeEnter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    final ConstFold cfolder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    final Target target;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    final Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
   116
    final JCDiagnostic.Factory diags;
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   117
    final TypeAnnotations typeAnnotations;
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   118
    final DeferredLintHandler deferredLintHandler;
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
   119
    final TypeEnvs typeEnvs;
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25445
diff changeset
   120
    final Dependencies dependencies;
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
   121
    final Annotate annotate;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   122
    final ArgumentAttr argumentAttr;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    public static Attr instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        Attr instance = context.get(attrKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            instance = new Attr(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    protected Attr(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        context.put(attrKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
   134
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        rs = Resolve.instance(context);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
   138
        operators = Operators.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        chk = Check.instance(context);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   140
        flow = Flow.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        memberEnter = MemberEnter.instance(context);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27856
diff changeset
   142
        typeEnter = TypeEnter.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        enter = Enter.instance(context);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   145
        infer = Infer.instance(context);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27857
diff changeset
   146
        analyzer = Analyzer.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   147
        deferredAttr = DeferredAttr.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        cfolder = ConstFold.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        target = Target.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 941
diff changeset
   151
        diags = JCDiagnostic.Factory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        annotate = Annotate.instance(context);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   153
        typeAnnotations = TypeAnnotations.instance(context);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   154
        deferredLintHandler = DeferredLintHandler.instance(context);
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
   155
        typeEnvs = TypeEnvs.instance(context);
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25445
diff changeset
   156
        dependencies = Dependencies.instance(context);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   157
        argumentAttr = ArgumentAttr.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        Source source = Source.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
   162
        allowPoly = Feature.POLY.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
   163
        allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
   164
        allowLambda = Feature.LAMBDA.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
   165
        allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
   166
        allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   167
        sourceName = source.name;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6710
diff changeset
   168
        useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   169
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   170
        statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
   171
        varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   172
        unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   173
        methodAttrInfo = new MethodAttrInfo();
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   174
        unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   175
        unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   176
        recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   179
    /** Switch: support target-typing inference
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   180
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   181
    boolean allowPoly;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   182
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   183
    /** Switch: support type annotations.
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   184
     */
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   185
    boolean allowTypeAnnos;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   186
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   187
    /** Switch: support lambda expressions ?
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   188
     */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   189
    boolean allowLambda;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   190
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   191
    /** Switch: support default methods ?
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   192
     */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   193
    boolean allowDefaultMethods;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   194
24901
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
   195
    /** Switch: static interface methods enabled?
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
   196
     */
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
   197
    boolean allowStaticInterfaceMethods;
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
   198
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * Switch: warn about use of variable before declaration?
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * RFE: 6425594
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    boolean useBeforeDeclarationWarning;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 3559
diff changeset
   205
    /**
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   206
     * Switch: name of source level; used for error reporting.
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   207
     */
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   208
    String sourceName;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents: 3765
diff changeset
   209
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** Check kind and type of given tree against protokind and prototype.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     *  If check succeeds, store type in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     *  If check fails, store errType in tree and return it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     *  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
   214
     *  It is not necessary in this case since we know that kind and type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     *  are correct.
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     *  @param tree     The tree whose kind and type is checked
28336
9a6ae3c3efb5 8066871: java.lang.VerifyError: Bad local variable type - local final String
mcimadamore
parents: 28148
diff changeset
   218
     *  @param found    The computed type of the tree
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     *  @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
   220
     *  @param resultInfo  The expected result of the tree
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     */
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   222
    Type check(final JCTree tree,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   223
               final Type found,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   224
               final KindSelector ownkind,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   225
               final ResultInfo resultInfo) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   226
        InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext();
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25273
diff changeset
   227
        Type owntype;
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   228
        boolean shouldCheck = !found.hasTag(ERROR) &&
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   229
                !resultInfo.pt.hasTag(METHOD) &&
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   230
                !resultInfo.pt.hasTag(FORALL);
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   231
        if (shouldCheck && !ownkind.subset(resultInfo.pkind)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   232
            log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   233
                      Errors.UnexpectedType(resultInfo.pkind.kindNames(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   234
                                            ownkind.kindNames()));
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   235
            owntype = types.createErrorType(found);
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   236
        } else if (allowPoly && inferenceContext.free(found)) {
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   237
            //delay the check if there are inference variables in the found type
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   238
            //this means we are dealing with a partially inferred poly expression
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   239
            owntype = shouldCheck ? resultInfo.pt : found;
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   240
            if (resultInfo.checkMode.installPostInferenceHook()) {
35423
36c7b4ec7a8b 8148213: Regression: nested unchecked call does not trigger erasure of return type
mcimadamore
parents: 35350
diff changeset
   241
                inferenceContext.addFreeTypeListener(List.of(found),
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   242
                        instantiatedContext -> {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   243
                            ResultInfo pendingResult =
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   244
                                    resultInfo.dup(inferenceContext.asInstType(resultInfo.pt));
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   245
                            check(tree, inferenceContext.asInstType(found), ownkind, pendingResult);
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   246
                        });
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   247
            }
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 25273
diff changeset
   248
        } else {
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   249
            owntype = shouldCheck ?
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   250
            resultInfo.check(tree, found) :
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   251
            found;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        }
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   253
        if (resultInfo.checkMode.updateTreeType()) {
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   254
            tree.type = owntype;
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
   255
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        return owntype;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /** Is given blank final variable assignable, i.e. in a scope where it
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     *  may be assigned to even though it is final?
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     *  @param v      The blank final variable.
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) {
25846
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
   265
        Symbol owner = env.info.scope.owner;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
           // owner refers to the innermost variable, method or
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
           // initializer block declaration at this point.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        return
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            v.owner == owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            ((owner.name == names.init ||    // i.e. we are in a constructor
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
              owner.kind == VAR ||           // i.e. we are in a variable initializer
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
              (owner.flags() & BLOCK) != 0)  // i.e. we are in an initializer block
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
             v.owner == owner.owner
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
             &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
             ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    /** Check that variable can be assigned to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     *  @param pos    The current source code position.
31751
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 31560
diff changeset
   282
     *  @param v      The assigned variable
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     *  @param base   If the variable is referred to in a Select, the part
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     *                to the left of the `.', null otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
36713
592302b6161b 8027999: Poorly worded error message when attempting to assign to this
alundblad
parents: 36526
diff changeset
   288
        if (v.name == names._this) {
592302b6161b 8027999: Poorly worded error message when attempting to assign to this
alundblad
parents: 36526
diff changeset
   289
            log.error(pos, Errors.CantAssignValToThis);
592302b6161b 8027999: Poorly worded error message when attempting to assign to this
alundblad
parents: 36526
diff changeset
   290
        } else if ((v.flags() & FINAL) != 0 &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            ((v.flags() & HASINIT) != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
             ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
             !((base == null ||
50468
4a5fd709e286 8204610: Compiler confused by parenthesized "this" in final fields assignments
mcimadamore
parents: 50250
diff changeset
   294
               TreeInfo.isThisQualifier(base)) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
               isAssignableAsBlankFinal(v, env)))) {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   296
            if (v.isResourceVariable()) { //TWR resource
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   297
                log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   298
            } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   299
                log.error(pos, Errors.CantAssignValToFinalVar(v));
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   300
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    /** Does tree represent a static reference to an identifier?
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     *  It is assumed that tree is either a SELECT or an IDENT.
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     *  We have to weed out selects from non-type names here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     *  @param tree    The candidate tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    boolean isStaticReference(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
   310
        if (tree.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            if (lsym == null || lsym.kind != TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    /** Is this symbol a type?
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    static boolean isType(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        return sym != null && sym.kind == TYP;
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    /** The current `this' symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     *  @param env    The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        return rs.resolveSelf(pos, env, env.enclClass.sym, names._this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    /** Attribute a parsed identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
     * @param tree Parsed identifier name
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     * @param topLevel The toplevel to use
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        Env<AttrContext> localEnv = enter.topLevelEnv(topLevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        localEnv.enclClass = make.ClassDef(make.Modifiers(0),
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                                           syms.errSymbol.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                                           null, null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        localEnv.enclClass.sym = syms.errSymbol;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   342
        return attribIdent(tree, localEnv);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   343
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   344
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   345
    /** Attribute a parsed identifier.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   346
     * @param tree Parsed identifier name
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   347
     * @param env The env to use
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   348
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   349
    public Symbol attribIdent(JCTree tree, Env<AttrContext> env) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
   350
        return tree.accept(identAttributer, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> {
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26107
diff changeset
   355
            @Override @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                Symbol site = visit(node.getExpression(), env);
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 42828
diff changeset
   358
                if (site.kind == ERR || site.kind == ABSENT_TYP || site.kind == HIDDEN)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                    return site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                Name name = (Name)node.getIdentifier();
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                if (site.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                    env.toplevel.packge = (PackageSymbol)site;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
   363
                    return rs.findIdentInPackage(null, env, (TypeSymbol)site, name,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   364
                            KindSelector.TYP_PCK);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                    env.enclClass.sym = (ClassSymbol)site;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                    return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site);
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26107
diff changeset
   371
            @Override @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
   373
                return rs.findIdent(null, env, (Name)node.getName(), KindSelector.TYP_PCK);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    public Type coerce(Type etype, Type ttype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        return cfolder.coerce(etype, ttype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    public Type attribType(JCTree node, TypeSymbol sym) {
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
   382
        Env<AttrContext> env = typeEnvs.get(sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        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
   384
        return attribTree(node, localEnv, unknownTypeInfo);
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   385
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   386
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   387
    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
   388
        // Attribute qualifying package or class.
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   389
        JCFieldAccess s = (JCFieldAccess)tree.qualid;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   390
        return attribTree(s.selected, env,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   391
                          new ResultInfo(tree.staticImport ?
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   392
                                         KindSelector.TYP : KindSelector.TYP_PCK,
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   393
                       Type.noType));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        breakTree = tree;
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 2723
diff changeset
   398
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   399
        EnumSet<AnalyzerMode> analyzerModes = EnumSet.copyOf(analyzer.analyzerModes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        try {
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   401
            analyzer.analyzerModes.clear();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            attribExpr(expr, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            return b.env;
6587
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   405
        } catch (AssertionError ae) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   406
            if (ae.getCause() instanceof BreakAttr) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   407
                return ((BreakAttr)(ae.getCause())).env;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   408
            } else {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   409
                throw ae;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   410
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            log.useSource(prev);
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   414
            analyzer.analyzerModes.addAll(analyzerModes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        return env;
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 Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        breakTree = tree;
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 2723
diff changeset
   421
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   422
        EnumSet<AnalyzerMode> analyzerModes = EnumSet.copyOf(analyzer.analyzerModes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        try {
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   424
            analyzer.analyzerModes.clear();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            attribStat(stmt, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        } catch (BreakAttr b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            return b.env;
6587
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   428
        } catch (AssertionError ae) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   429
            if (ae.getCause() instanceof BreakAttr) {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   430
                return ((BreakAttr)(ae.getCause())).env;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   431
            } else {
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   432
                throw ae;
344aa81a3b35 6956462: AssertionError exception thrown in the Compiler Tree API in JDK 7.
sundar
parents: 6582
diff changeset
   433
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
            breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            log.useSource(prev);
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
   437
            analyzer.analyzerModes.addAll(analyzerModes);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    private JCTree breakTree = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    private static class BreakAttr extends RuntimeException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        static final long serialVersionUID = -6924771130405446405L;
58350
e23e560afbcb 8231414: Mark various instance fields of javac internal types as transient
darcy
parents: 58018
diff changeset
   446
        private transient Env<AttrContext> env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        private BreakAttr(Env<AttrContext> env) {
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   448
            this.env = env;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   452
    /**
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   453
     * Mode controlling behavior of Attr.Check
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   454
     */
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   455
    enum CheckMode {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   456
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   457
        NORMAL,
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   458
44392
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   459
        /**
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   460
         * Mode signalling 'fake check' - skip tree update. A side-effect of this mode is
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   461
         * that the captured var cache in {@code InferenceContext} will be used in read-only
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   462
         * mode when performing inference checks.
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   463
         */
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   464
        NO_TREE_UPDATE {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   465
            @Override
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   466
            public boolean updateTreeType() {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   467
                return false;
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   468
            }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   469
        },
44392
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   470
        /**
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   471
         * Mode signalling that caller will manage free types in tree decorations.
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   472
         */
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
   473
        NO_INFERENCE_HOOK {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   474
            @Override
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   475
            public boolean installPostInferenceHook() {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   476
                return false;
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   477
            }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   478
        };
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   479
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   480
        public boolean updateTreeType() {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   481
            return true;
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   482
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   483
        public boolean installPostInferenceHook() {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   484
            return true;
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   485
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   486
    }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   487
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   488
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   489
    class ResultInfo {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   490
        final KindSelector pkind;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   491
        final Type pt;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   492
        final CheckContext checkContext;
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   493
        final CheckMode checkMode;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   494
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   495
        ResultInfo(KindSelector pkind, Type pt) {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   496
            this(pkind, pt, chk.basicHandler, CheckMode.NORMAL);
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   497
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   498
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   499
        ResultInfo(KindSelector pkind, Type pt, CheckMode checkMode) {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   500
            this(pkind, pt, chk.basicHandler, checkMode);
12334
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
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   503
        protected ResultInfo(KindSelector pkind,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   504
                             Type pt, CheckContext checkContext) {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   505
            this(pkind, pt, checkContext, CheckMode.NORMAL);
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   506
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   507
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   508
        protected ResultInfo(KindSelector pkind,
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   509
                             Type pt, CheckContext checkContext, CheckMode checkMode) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   510
            this.pkind = pkind;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   511
            this.pt = pt;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   512
            this.checkContext = checkContext;
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   513
            this.checkMode = checkMode;
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   514
        }
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   515
44571
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   516
        /**
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   517
         * Should {@link Attr#attribTree} use the {@ArgumentAttr} visitor instead of this one?
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   518
         * @param tree The tree to be type-checked.
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   519
         * @return true if {@ArgumentAttr} should be used.
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   520
         */
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   521
        protected boolean needsArgumentAttr(JCTree tree) { return false; }
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   522
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   523
        protected Type check(final DiagnosticPosition pos, final Type found) {
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
   524
            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
   525
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   526
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   527
        protected ResultInfo dup(Type newPt) {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   528
            return new ResultInfo(pkind, newPt, checkContext, checkMode);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   529
        }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   530
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   531
        protected ResultInfo dup(CheckContext newContext) {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   532
            return new ResultInfo(pkind, pt, newContext, checkMode);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   533
        }
21488
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   534
24401
0d793293862a 8028503: javac, for method references a primitive type can be added as a bound
vromero
parents: 24400
diff changeset
   535
        protected ResultInfo dup(Type newPt, CheckContext newContext) {
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   536
            return new ResultInfo(pkind, newPt, newContext, checkMode);
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   537
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   538
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   539
        protected ResultInfo dup(Type newPt, CheckContext newContext, CheckMode newMode) {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   540
            return new ResultInfo(pkind, newPt, newContext, newMode);
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   541
        }
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   542
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   543
        protected ResultInfo dup(CheckMode newMode) {
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
   544
            return new ResultInfo(pkind, pt, checkContext, newMode);
24401
0d793293862a 8028503: javac, for method references a primitive type can be added as a bound
vromero
parents: 24400
diff changeset
   545
        }
0d793293862a 8028503: javac, for method references a primitive type can be added as a bound
vromero
parents: 24400
diff changeset
   546
21488
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   547
        @Override
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   548
        public String toString() {
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   549
            if (pt != null) {
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   550
                return pt.toString();
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   551
            } else {
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   552
                return "";
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   553
            }
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 21480
diff changeset
   554
        }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   555
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   556
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   557
    class MethodAttrInfo extends ResultInfo {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   558
        public MethodAttrInfo() {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   559
            this(chk.basicHandler);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   560
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   561
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   562
        public MethodAttrInfo(CheckContext checkContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   563
            super(KindSelector.VAL, Infer.anyPoly, checkContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   564
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   565
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   566
        @Override
44571
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   567
        protected boolean needsArgumentAttr(JCTree tree) {
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   568
            return true;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   569
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   570
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   571
        protected ResultInfo dup(Type newPt) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   572
            throw new IllegalStateException();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   573
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   574
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   575
        protected ResultInfo dup(CheckContext newContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   576
            return new MethodAttrInfo(newContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   577
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   578
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   579
        protected ResultInfo dup(Type newPt, CheckContext newContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   580
            throw new IllegalStateException();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   581
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   582
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   583
        protected ResultInfo dup(Type newPt, CheckContext newContext, CheckMode newMode) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   584
            throw new IllegalStateException();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   585
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   586
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   587
        protected ResultInfo dup(CheckMode newMode) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   588
            throw new IllegalStateException();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   589
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   590
    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   591
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   592
    class RecoveryInfo extends ResultInfo {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   593
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   594
        public RecoveryInfo(final DeferredAttr.DeferredAttrContext deferredAttrContext) {
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   595
            this(deferredAttrContext, Type.recoveryType);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   596
        }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   597
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   598
        public RecoveryInfo(final DeferredAttr.DeferredAttrContext deferredAttrContext, Type pt) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   599
            super(KindSelector.VAL, pt, new Check.NestedCheckContext(chk.basicHandler) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   600
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   601
                public DeferredAttr.DeferredAttrContext deferredAttrContext() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   602
                    return deferredAttrContext;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   603
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   604
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   605
                public boolean compatible(Type found, Type req, Warner warn) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   606
                    return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   607
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   608
                @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   609
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   610
                    if (pt == Type.recoveryType) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   611
                        chk.basicHandler.report(pos, details);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   612
                    }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   613
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   614
            });
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   615
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   616
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   617
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   618
    final ResultInfo statInfo;
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
   619
    final ResultInfo varAssignmentInfo;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   620
    final ResultInfo methodAttrInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   621
    final ResultInfo unknownExprInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   622
    final ResultInfo unknownTypeInfo;
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16972
diff changeset
   623
    final ResultInfo unknownTypeExprInfo;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   624
    final ResultInfo recoveryInfo;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   625
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   626
    Type pt() {
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   627
        return resultInfo.pt;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   628
    }
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   629
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   630
    KindSelector pkind() {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   631
        return resultInfo.pkind;
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   632
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
 * Visitor methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
    /** Visitor argument: the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   642
    /** Visitor argument: the currently expected attribution result.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
     */
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   644
    ResultInfo resultInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
    /** Visitor result: the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
    Type result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    /** Visitor method: attribute a tree, catching any completion failure
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
     *  exceptions. Return the tree's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
     *  @param tree    The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
     *  @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
   655
     *  @param resultInfo   The result info visitor argument.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
     */
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   657
    Type attribTree(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        Env<AttrContext> prevEnv = this.env;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   659
        ResultInfo prevResult = this.resultInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
            this.env = env;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   662
            this.resultInfo = resultInfo;
44571
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   663
            if (resultInfo.needsArgumentAttr(tree)) {
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   664
                result = argumentAttr.attribArg(tree, env);
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   665
            } else {
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   666
                tree.accept(this);
1140b8dad6bb 8177933: Stackoverflow during compilation, starting jdk-9+163
mcimadamore
parents: 44393
diff changeset
   667
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   668
            if (tree == breakTree &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   669
                    resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   670
                breakTreeFound(copyEnv(env));
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   671
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
            tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            return chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
            this.env = prevEnv;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
   678
            this.resultInfo = prevResult;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   682
    protected void breakTreeFound(Env<AttrContext> env) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   683
        throw new BreakAttr(env);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   684
    }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
   685
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   686
    Env<AttrContext> copyEnv(Env<AttrContext> env) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   687
        Env<AttrContext> newEnv =
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   688
                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
   689
        if (newEnv.outer != null) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   690
            newEnv.outer = copyEnv(newEnv.outer);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   691
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   692
        return newEnv;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   693
    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   694
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
   695
    WriteableScope copyScope(WriteableScope sc) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
   696
        WriteableScope newScope = WriteableScope.create(sc.owner);
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   697
        List<Symbol> elemsList = List.nil();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
   698
        for (Symbol sym : sc.getSymbols()) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
   699
            elemsList = elemsList.prepend(sym);
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   700
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   701
        for (Symbol s : elemsList) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   702
            newScope.enter(s);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   703
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   704
        return newScope;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   705
    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
   706
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
    /** Derived visitor method: attribute an expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   710
        return attribTree(tree, env, new ResultInfo(KindSelector.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
   711
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
   712
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
    /** Derived visitor method: attribute an expression tree with
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
     *  no constraints on the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
     */
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14538
diff changeset
   716
    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
   717
        return attribTree(tree, env, unknownExprInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
    /** Derived visitor method: attribute a type tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
     */
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14538
diff changeset
   722
    public Type attribType(JCTree tree, Env<AttrContext> env) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   723
        Type result = attribType(tree, env, Type.noType);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   724
        return result;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   725
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   726
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   727
    /** Derived visitor method: attribute a type tree.
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   728
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   729
    Type attribType(JCTree tree, Env<AttrContext> env, Type pt) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
   730
        Type result = attribTree(tree, env, new ResultInfo(KindSelector.TYP, pt));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
    /** Derived visitor method: attribute a statement or definition tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
    public Type attribStat(JCTree tree, Env<AttrContext> env) {
45605
8d019eee3515 8181911: Lambda Analyzer causes compile-time error
jlahoda
parents: 45504
diff changeset
   737
        Env<AttrContext> analyzeEnv = analyzer.copyEnvIfNeeded(tree, env);
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55528
diff changeset
   738
        Type result = attribTree(tree, env, statInfo);
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55528
diff changeset
   739
        analyzer.analyzeIfNeeded(tree, analyzeEnv);
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55528
diff changeset
   740
        return result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    /** Attribute a list of expressions, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
    List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
   746
        ListBuffer<Type> ts = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
        for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            ts.append(attribExpr(l.head, env, pt));
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
    /** Attribute a list of statements, returning nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
    <T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        for (List<T> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   759
    /** Attribute the arguments in a method call, returning the method kind.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
     */
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
   761
    KindSelector attribArgs(KindSelector initialKind, List<JCExpression> trees, Env<AttrContext> env, ListBuffer<Type> argtypes) {
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
   762
        KindSelector kind = initialKind;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   763
        for (JCExpression arg : trees) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   764
            Type argtype = chk.checkNonVoid(arg, attribTree(arg, env, allowPoly ? methodAttrInfo : unknownExprInfo));
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
   765
            if (argtype.hasTag(DEFERRED)) {
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
   766
                kind = KindSelector.of(KindSelector.POLY, kind);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
   767
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   768
            argtypes.append(argtype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   769
        }
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
   770
        return kind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
    /** 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
   774
     *  Caller is responsible for calling checkRefTypes.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
     */
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   776
    List<Type> attribAnyTypes(List<JCExpression> trees, Env<AttrContext> env) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
   777
        ListBuffer<Type> argtypes = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
        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
   779
            argtypes.append(attribType(l.head, env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        return argtypes.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   783
    /** 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
   784
     *  Check that all the types are references.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   785
     */
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   786
    List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) {
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   787
        List<Type> types = attribAnyTypes(trees, env);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   788
        return chk.checkRefTypes(trees, types);
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
   789
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
     * Attribute type variables (of generic classes or methods).
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
     * Compound types are attributed later in attribBounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
     * @param typarams the type variables to enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
     * @param env      the current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
     */
53275
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
   797
    void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env, boolean checkCyclic) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
        for (JCTypeParameter tvar : typarams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
            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
   800
            a.tsym.flags_field |= UNATTRIBUTED;
53515
a772e65727c5 8193367: Annotated type variable bounds crash javac
bsrbnd
parents: 53275
diff changeset
   801
            a.setUpperBound(Type.noType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            if (!tvar.bounds.isEmpty()) {
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   803
                List<Type> bounds = List.of(attribType(tvar.bounds.head, env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
                for (JCExpression bound : tvar.bounds.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                    bounds = bounds.prepend(attribType(bound, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
                types.setBounds(a, bounds.reverse());
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
                // if no bounds are given, assume a single bound of
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
                // java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
                types.setBounds(a, List.of(syms.objectType));
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            }
661
9b2f1fe5c183 6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents: 512
diff changeset
   812
            a.tsym.flags_field &= ~UNATTRIBUTED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        }
53275
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
   814
        if (checkCyclic) {
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
   815
            for (JCTypeParameter tvar : typarams) {
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
   816
                chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type);
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52936
diff changeset
   817
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
     * Attribute the type references in a list of annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
    void attribAnnotationTypes(List<JCAnnotation> annotations,
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
                               Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
        for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
            JCAnnotation a = al.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
            attribType(a.annotationType, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   832
    /**
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   833
     * Attribute a "lazy constant value".
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   834
     *  @param env         The env for the const value
31751
ec251536a004 8080880: some docs cleanup for langtools
avstepan
parents: 31560
diff changeset
   835
     *  @param variable    The initializer for the const value
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   836
     *  @param type        The expected type, or null
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   837
     *  @see VarSymbol#setLazyConstValue
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   838
     */
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   839
    public Object attribLazyConstantValue(Env<AttrContext> env,
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
   840
                                      JCVariableDecl variable,
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   841
                                      Type type) {
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   842
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
   843
        DiagnosticPosition prevLintPos
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
   844
                = deferredLintHandler.setPos(variable.pos());
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   845
38607
eb4a0b7e67a1 8154052: Java compiler error displays line from the wrong file
sadayapalam
parents: 38538
diff changeset
   846
        final JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   847
        try {
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
   848
            Type itype = attribExpr(variable.init, env, type);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
   849
            if (variable.isImplicitlyTyped()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
   850
                //fixup local variable type
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
   851
                type = variable.type = variable.sym.type = chk.checkLocalVarType(variable, itype.baseType(), variable.name);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
   852
            }
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   853
            if (itype.constValue() != null) {
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   854
                return coerce(itype, type).constValue();
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   855
            } else {
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   856
                return null;
18657
2bd14bebdf57 8016099: Some @SuppressWarnings annotations ignored ( unchecked, rawtypes )
vromero
parents: 18644
diff changeset
   857
            }
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   858
        } finally {
38607
eb4a0b7e67a1 8154052: Java compiler error displays line from the wrong file
sadayapalam
parents: 38538
diff changeset
   859
            log.useSource(prevSource);
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
   860
            deferredLintHandler.setPos(prevLintPos);
8225
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   861
        }
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   862
    }
e9e5670e6a71 6554097: "final" confuses @SuppressWarnings
jjg
parents: 8046
diff changeset
   863
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
    /** Attribute type reference in an `extends' or `implements' clause.
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   865
     *  Supertypes of anonymous inner classes are usually already attributed.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
     *  @param tree              The tree making up the type reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
     *  @param env               The environment current at the reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
     *  @param classExpected     true if only a class is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
     *  @param interfaceExpected true if only an interface is expected here.
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
    Type attribBase(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
                    Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
                    boolean classExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                    boolean interfaceExpected,
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                    boolean checkExtensible) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   877
        Type t = tree.type != null ?
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   878
            tree.type :
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   879
            attribType(tree, env);
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   880
        return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
    Type checkBase(Type t,
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                   JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                   Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
                   boolean classExpected,
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   886
                   boolean interfaceExpected,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
                   boolean checkExtensible) {
33907
9ee2b1641949 8073616: Duplicate error message: cannot inherit from final (class) F
sadayapalam
parents: 33708
diff changeset
   888
        final DiagnosticPosition pos = tree.hasTag(TYPEAPPLY) ?
9ee2b1641949 8073616: Duplicate error message: cannot inherit from final (class) F
sadayapalam
parents: 33708
diff changeset
   889
                (((JCTypeApply) tree).clazz).pos() : tree.pos();
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents: 23803
diff changeset
   890
        if (t.tsym.isAnonymous()) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   891
            log.error(pos, Errors.CantInheritFromAnon);
23809
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents: 23803
diff changeset
   892
            return types.createErrorType(t);
9405883da95f 8023945: javac wrongly allows a subclass of an anonymous class
pgovereau
parents: 23803
diff changeset
   893
        }
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6351
diff changeset
   894
        if (t.isErroneous())
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6351
diff changeset
   895
            return t;
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   896
        if (t.hasTag(TYPEVAR) && !classExpected && !interfaceExpected) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
            // check that type variable is already visible
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
            if (t.getUpperBound() == null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   899
                log.error(pos, Errors.IllegalForwardRef);
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
   900
                return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
            }
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   902
        } else {
33907
9ee2b1641949 8073616: Duplicate error message: cannot inherit from final (class) F
sadayapalam
parents: 33708
diff changeset
   903
            t = chk.checkClassType(pos, t, checkExtensible);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
        }
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   905
        if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   906
            log.error(pos, Errors.IntfExpectedHere);
21009
b35973e2d42e 8019461: Clean up javac diagnostics
emc
parents: 20608
diff changeset
   907
            // return errType is necessary since otherwise there might
b35973e2d42e 8019461: Clean up javac diagnostics
emc
parents: 20608
diff changeset
   908
            // be undetected cycles which cause attribution to loop
b35973e2d42e 8019461: Clean up javac diagnostics
emc
parents: 20608
diff changeset
   909
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
        } else if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                   classExpected &&
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
   912
                   (t.tsym.flags() & INTERFACE) != 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   913
            log.error(pos, Errors.NoIntfExpectedHere);
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
   914
            return types.createErrorType(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
        if (checkExtensible &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
            ((t.tsym.flags() & FINAL) != 0)) {
33907
9ee2b1641949 8073616: Duplicate error message: cannot inherit from final (class) F
sadayapalam
parents: 33708
diff changeset
   918
            log.error(pos,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
   919
                      Errors.CantInheritFromFinal(t.tsym));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
        }
33907
9ee2b1641949 8073616: Duplicate error message: cannot inherit from final (class) F
sadayapalam
parents: 33708
diff changeset
   921
        chk.checkNonCyclic(pos, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   925
    Type attribIdentAsEnumType(Env<AttrContext> env, JCIdent id) {
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   926
        Assert.check((env.enclClass.sym.flags() & ENUM) != 0);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27857
diff changeset
   927
        id.type = env.info.scope.owner.enclClass().type;
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27857
diff changeset
   928
        id.sym = env.info.scope.owner.enclClass();
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   929
        return id.type;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   930
    }
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
   931
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
    public void visitClassDef(JCClassDecl tree) {
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   933
        Optional<ArgumentAttr.LocalCacheContext> localCacheContext =
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55528
diff changeset
   934
                Optional.ofNullable(env.info.attributionMode.isSpeculative ?
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   935
                        argumentAttr.withLocalCacheContext() : null);
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   936
        try {
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   937
            // Local and anonymous classes have not been entered yet, so we need to
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   938
            // do it now.
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   939
            if (env.info.scope.owner.kind.matches(KindSelector.VAL_MTH)) {
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   940
                enter.classEnter(tree, env);
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   941
            } else {
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   942
                // If this class declaration is part of a class level annotation,
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   943
                // as in @MyAnno(new Object() {}) class MyClass {}, enter it in
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   944
                // order to simplify later steps and allow for sensible error
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   945
                // messages.
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   946
                if (env.tree.hasTag(NEWCLASS) && TreeInfo.isInAnnotation(env, tree))
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   947
                    enter.classEnter(tree, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
            }
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   949
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   950
            ClassSymbol c = tree.sym;
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   951
            if (c == null) {
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   952
                // exit in case something drastic went wrong during enter.
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   953
                result = null;
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   954
            } else {
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   955
                // make sure class has been completed:
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   956
                c.complete();
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   957
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   958
                // If this class appears as an anonymous class
41154
ed8625a2f0f1 8166108: VerifyError passing anonymous inner class to supertype constructor
sadayapalam
parents: 40504
diff changeset
   959
                // in a superclass constructor call
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   960
                // disable implicit outer instance from being passed.
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   961
                // (This would be an illegal access to "this before super").
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   962
                if (env.info.isSelfCall &&
41154
ed8625a2f0f1 8166108: VerifyError passing anonymous inner class to supertype constructor
sadayapalam
parents: 40504
diff changeset
   963
                        env.tree.hasTag(NEWCLASS)) {
35350
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   964
                    c.flags_field |= NOOUTERTHIS;
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   965
                }
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   966
                attribClass(tree.pos(), c);
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   967
                result = tree.type = c.type;
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   968
            }
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   969
        } finally {
5ec1564e7b1f 8147546: regression when type-checking generic calls inside nested declarations occurring in method context
mcimadamore
parents: 35349
diff changeset
   970
            localCacheContext.ifPresent(LocalCacheContext::leave);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
        MethodSymbol m = tree.sym;
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14259
diff changeset
   976
        boolean isDefaultMethod = (m.flags() & DEFAULT) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
   978
        Lint lint = env.info.lint.augment(m);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
        Lint prevLint = chk.setLint(lint);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
   980
        MethodSymbol prevMethod = chk.setMethod(m);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
        try {
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   982
            deferredLintHandler.flush(tree.pos());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            chk.checkDeprecatedAnnotation(tree.pos(), m);
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   985
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   986
            // Create a new environment with local scope
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   987
            // for attributing the method.
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   988
            Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   989
            localEnv.info.lint = lint;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   990
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
   991
            attribStats(tree.typarams, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
            // If we override any other methods, check that we do so properly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
            // JLS ???
8242
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   995
            if (m.isStatic()) {
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   996
                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
   997
            } else {
3873b4aaf4a8 7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents: 8239
diff changeset
   998
                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
   999
            }
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  1000
            chk.checkOverride(env, tree, m);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1002
            if (isDefaultMethod && types.overridesObjectMethod(m.enclClass(), m)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1003
                log.error(tree, Errors.DefaultOverridesObjectMember(m.name, Kinds.kindName(m.location()), m.location()));
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  1004
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  1005
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
            // Enter all type parameters into the local method scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                localEnv.info.scope.enterIfAbsent(l.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
            ClassSymbol owner = env.enclClass.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
            if ((owner.flags() & ANNOTATION) != 0 &&
44194
c868c8c24e18 8175198: Javac incorrectly allows receiver parameters in annotation methods
vromero
parents: 44184
diff changeset
  1012
                    (tree.params.nonEmpty() ||
c868c8c24e18 8175198: Javac incorrectly allows receiver parameters in annotation methods
vromero
parents: 44184
diff changeset
  1013
                    tree.recvparam != null))
c868c8c24e18 8175198: Javac incorrectly allows receiver parameters in annotation methods
vromero
parents: 44184
diff changeset
  1014
                log.error(tree.params.nonEmpty() ?
c868c8c24e18 8175198: Javac incorrectly allows receiver parameters in annotation methods
vromero
parents: 44184
diff changeset
  1015
                        tree.params.head.pos() :
c868c8c24e18 8175198: Javac incorrectly allows receiver parameters in annotation methods
vromero
parents: 44184
diff changeset
  1016
                        tree.recvparam.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1017
                        Errors.IntfAnnotationMembersCantHaveParams);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            // Attribute all value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
            for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                attribStat(l.head, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  1024
            chk.checkVarargsMethodDecl(localEnv, tree);
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5654
diff changeset
  1025
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
            // Check that type parameters are well-formed.
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  1027
            chk.validate(tree.typarams, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            // Check that result type is well-formed.
21896
f39003ba351f 8026374: javac accepts void as a method parameter
jlahoda
parents: 21890
diff changeset
  1030
            if (tree.restype != null && !tree.restype.type.hasTag(VOID))
f39003ba351f 8026374: javac accepts void as a method parameter
jlahoda
parents: 21890
diff changeset
  1031
                chk.validate(tree.restype, localEnv);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1032
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1033
            // Check that receiver type is well-formed.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1034
            if (tree.recvparam != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1035
                // Use a new environment to check the receiver parameter.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1036
                // Otherwise I get "might not have been initialized" errors.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1037
                // Is there a better way?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1038
                Env<AttrContext> newEnv = memberEnter.methodEnv(tree, env);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1039
                attribType(tree.recvparam, newEnv);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1040
                chk.validate(tree.recvparam, newEnv);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1041
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1042
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1043
            // annotation method checks
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1044
            if ((owner.flags() & ANNOTATION) != 0) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1045
                // annotation method cannot have throws clause
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1046
                if (tree.thrown.nonEmpty()) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1047
                    log.error(tree.thrown.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1048
                              Errors.ThrowsNotAllowedInIntfAnnotation);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1049
                }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1050
                // annotation method cannot declare type-parameters
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1051
                if (tree.typarams.nonEmpty()) {
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1052
                    log.error(tree.typarams.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1053
                              Errors.IntfAnnotationMembersCantHaveTypeParams);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1054
                }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1055
                // validate annotation method's return type (could be an annotation type)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
                chk.validateAnnotationType(tree.restype);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1057
                // ensure that annotation method does not clash with members of Object/Annotation
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
                chk.validateAnnotationMethod(tree.pos(), m);
6342
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1059
            }
228b73431edb 6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents: 6154
diff changeset
  1060
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
                chk.checkType(l.head.pos(), l.head.type, syms.throwableType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
            if (tree.body == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                // Empty bodies are only allowed for
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                // abstract, native, or interface methods, or for methods
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                // in a retrofit signature class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
                if (tree.defaultValue != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                    if ((owner.flags() & ANNOTATION) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
                        log.error(tree.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1071
                                  Errors.DefaultAllowedInIntfAnnotationMember);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                }
36153
ed5063b304be 8149600: javac, remove unused options, step 2
vromero
parents: 35423
diff changeset
  1073
                if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1074
                    log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
30845
43ddd58a5a56 8080726: Redundant error message on private abstract interface method with body.
sadayapalam
parents: 30405
diff changeset
  1075
            } else if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  1076
                if ((owner.flags() & INTERFACE) != 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1077
                    log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  1078
                } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1079
                    log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  1080
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            } else if ((tree.mods.flags & NATIVE) != 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1082
                log.error(tree.pos(), Errors.NativeMethCantHaveBody);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
                // Add an implicit super() call unless an explicit call to
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
                // super(...) or this(...) is given
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
                // or we are compiling class java.lang.Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                if (tree.name == names.init && owner.type != syms.objectType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                    JCBlock body = tree.body;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
                    if (body.stats.isEmpty() ||
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1090
                            !TreeInfo.isSelfCall(body.stats.head)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                        body.stats = body.stats.
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27856
diff changeset
  1092
                                prepend(typeEnter.SuperCall(make.at(body.pos),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1093
                                        List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1094
                                        List.nil(),
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1095
                                        false));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
                    } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1097
                            (tree.mods.flags & GENERATEDCONSTR) == 0 &&
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1098
                            TreeInfo.isSuperCall(body.stats.head)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
                        // enum constructors are not allowed to call super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
                        // directly, so make sure there aren't any super calls
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
                        // in enum constructors, except in the compiler
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                        // generated one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                        log.error(tree.body.stats.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1104
                                  Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1108
                // Attribute all type annotations in the body
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  1109
                annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m, null);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1110
                annotate.flush();
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1111
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                // Attribute method body.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                attribStat(tree.body, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1115
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
            localEnv.info.scope.leave();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
            result = tree.type = m.type;
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1118
        } finally {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            chk.setLint(prevLint);
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  1120
            chk.setMethod(prevMethod);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1124
    public void visitVarDef(JCVariableDecl tree) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        // Local variables have not been entered yet, so we need to do it now:
52936
9745e4e36dd1 8214114: Switch expressions with try-catch statements
jlahoda
parents: 52697
diff changeset
  1126
        if (env.info.scope.owner.kind == MTH || env.info.scope.owner.kind == VAR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            if (tree.sym != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                // parameters have already been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
                env.info.scope.enter(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
            } else {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1131
                if (tree.isImplicitlyTyped() && (tree.getModifiers().flags & PARAMETER) == 0) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1132
                    if (tree.init == null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1133
                        //cannot use 'var' without initializer
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1134
                        log.error(tree, Errors.CantInferLocalVarType(tree.name, Fragments.LocalMissingInit));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1135
                        tree.vartype = make.Erroneous();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1136
                    } else {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1137
                        Fragment msg = canInferLocalVarType(tree);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1138
                        if (msg != null) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1139
                            //cannot use 'var' with initializer which require an explicit target
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1140
                            //(e.g. lambda, method reference, array initializer).
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1141
                            log.error(tree, Errors.CantInferLocalVarType(tree.name, msg));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1142
                            tree.vartype = make.Erroneous();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1143
                        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1144
                    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1145
                }
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
  1146
                try {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  1147
                    annotate.blockAnnotations();
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
  1148
                    memberEnter.memberEnter(tree, env);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
  1149
                } finally {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  1150
                    annotate.unblockAnnotations();
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
  1151
                }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1152
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1153
        } else {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1154
            if (tree.init != null) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1155
                // Field initializer expression need to be entered.
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  1156
                annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1157
                annotate.flush();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1158
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
        VarSymbol v = tree.sym;
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1162
        Lint lint = env.info.lint.augment(v);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        Lint prevLint = chk.setLint(lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
1538
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
  1165
        // 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
  1166
        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
  1167
                ((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
  1168
                (tree.sym.flags() & PARAMETER) != 0;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1169
        chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1538
3cbbc9424f43 6763518: Impossible to suppress raw-type warnings
mcimadamore
parents: 1534
diff changeset
  1170
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        try {
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  1172
            v.getConstValue(); // ensure compile-time constant initializer is evaluated
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  1173
            deferredLintHandler.flush(tree.pos());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
            chk.checkDeprecatedAnnotation(tree.pos(), v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
            if (tree.init != null) {
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  1177
                if ((v.flags_field & FINAL) == 0 ||
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  1178
                    !memberEnter.needsLazyConstValue(tree.init)) {
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  1179
                    // Not a compile-time constant
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
                    // Attribute initializer in a new environment
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
                    // with the declared variable as owner.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
                    // Check that initializer conforms to variable's declared type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                    Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                    initEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
                    // In order to catch self-references, we set the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                    // declaration position to maximal possible value, effectively
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
                    // marking the variable as undefined.
1045
56f6e84f7825 6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents: 1040
diff changeset
  1188
                    initEnv.info.enclVar = v;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
                    attribExpr(tree.init, initEnv, v.type);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1190
                    if (tree.isImplicitlyTyped()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1191
                        //fixup local variable type
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1192
                        v.type = chk.checkLocalVarType(tree, tree.init.type.baseType(), tree.name);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1193
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
                }
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  1195
                if (tree.isImplicitlyTyped()) {
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  1196
                    setSyntheticVariableType(tree, v.type);
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  1197
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
            result = tree.type = v.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
            chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1206
    Fragment canInferLocalVarType(JCVariableDecl tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1207
        LocalInitScanner lis = new LocalInitScanner();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1208
        lis.scan(tree.init);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1209
        return lis.badInferenceMsg;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1210
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1211
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1212
    static class LocalInitScanner extends TreeScanner {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1213
        Fragment badInferenceMsg = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1214
        boolean needsTarget = true;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1215
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1216
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1217
        public void visitNewArray(JCNewArray tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1218
            if (tree.elemtype == null && needsTarget) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1219
                badInferenceMsg = Fragments.LocalArrayMissingTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1220
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1221
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1222
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1223
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1224
        public void visitLambda(JCLambda tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1225
            if (needsTarget) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1226
                badInferenceMsg = Fragments.LocalLambdaMissingTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1227
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1228
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1229
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1230
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1231
        public void visitTypeCast(JCTypeCast tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1232
            boolean prevNeedsTarget = needsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1233
            try {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1234
                needsTarget = false;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1235
                super.visitTypeCast(tree);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1236
            } finally {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1237
                needsTarget = prevNeedsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1238
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1239
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1240
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1241
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1242
        public void visitReference(JCMemberReference tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1243
            if (needsTarget) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1244
                badInferenceMsg = Fragments.LocalMrefMissingTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1245
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1246
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1247
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1248
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1249
        public void visitNewClass(JCNewClass tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1250
            boolean prevNeedsTarget = needsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1251
            try {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1252
                needsTarget = false;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1253
                super.visitNewClass(tree);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1254
            } finally {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1255
                needsTarget = prevNeedsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1256
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1257
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1258
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1259
        @Override
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1260
        public void visitApply(JCMethodInvocation tree) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1261
            boolean prevNeedsTarget = needsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1262
            try {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1263
                needsTarget = false;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1264
                super.visitApply(tree);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1265
            } finally {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1266
                needsTarget = prevNeedsTarget;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1267
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1268
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1269
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1270
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
    public void visitSkip(JCSkip tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
    public void visitBlock(JCBlock tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
        if (env.info.scope.owner.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
            // Block is a static or instance initializer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
            // let the owner of the environment be a freshly
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
            // created BLOCK-method.
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  1280
            Symbol fakeOwner =
15710
5792a085da41 7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents: 15705
diff changeset
  1281
                new MethodSymbol(tree.flags | BLOCK |
5792a085da41 7166455: javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
vromero
parents: 15705
diff changeset
  1282
                    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
  1283
                    env.info.scope.owner);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  1284
            final Env<AttrContext> localEnv =
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  1285
                env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24794
diff changeset
  1286
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
            if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1288
            // Attribute all type annotations in the block
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  1289
            annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  1290
            annotate.flush();
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24794
diff changeset
  1291
            attribStats(tree.stats, localEnv);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1292
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1293
            {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1294
                // Store init and clinit type annotations with the ClassSymbol
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1295
                // to allow output in Gen.normalizeDefs.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1296
                ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1297
                List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1298
                if ((tree.flags & STATIC) != 0) {
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1299
                    cs.appendClassInitTypeAttributes(tas);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1300
                } else {
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17805
diff changeset
  1301
                    cs.appendInitTypeAttributes(tas);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1302
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 17557
diff changeset
  1303
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
            // Create a new local environment with a local scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
            Env<AttrContext> localEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
                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
  1308
            try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1309
                attribStats(tree.stats, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1310
            } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1311
                localEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1312
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
            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
  1332
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1333
            attribStats(tree.init, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1334
            if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1335
            loopEnv.tree = tree; // before, we were not in loop!
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1336
            attribStats(tree.step, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1337
            attribStat(tree.body, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1338
            result = null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1339
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1340
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1341
            loopEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1342
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
        Env<AttrContext> loopEnv =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
            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
  1348
        try {
18379
8dd20756448c 7139681: Enhanced for loop: local variable scope inconsistent with JLS
mcimadamore
parents: 18010
diff changeset
  1349
            //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
  1350
            //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
  1351
            //the for-each expression first (against original scope).
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24609
diff changeset
  1352
            Type exprType = types.cvarUpperBound(attribExpr(tree.expr, loopEnv));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1353
            chk.checkNonVoid(tree.pos(), exprType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1354
            Type elemtype = types.elemtype(exprType); // perhaps expr is an array?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1355
            if (elemtype == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1356
                // or perhaps expr implements Iterable<T>?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1357
                Type base = types.asSuper(exprType, syms.iterableType.tsym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1358
                if (base == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1359
                    log.error(tree.expr.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1360
                              Errors.ForeachNotApplicableToType(exprType,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1361
                                                                Fragments.TypeReqArrayOrIterable));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1362
                    elemtype = types.createErrorType(exprType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1363
                } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1364
                    List<Type> iterableParams = base.allparams();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1365
                    elemtype = iterableParams.isEmpty()
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1366
                        ? syms.objectType
24612
75dc732b45af 8042338: Refactor Types.upperBound to treat wildcards and variables separately
dlsmith
parents: 24609
diff changeset
  1367
                        : types.wildUpperBound(iterableParams.head);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1368
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
            }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1370
            if (tree.var.isImplicitlyTyped()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1371
                Type inferredType = chk.checkLocalVarType(tree.var, elemtype, tree.var.name);
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  1372
                setSyntheticVariableType(tree.var, inferredType);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1373
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  1374
            attribStat(tree.var, loopEnv);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1375
            chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1376
            loopEnv.tree = tree; // before, we were not in loop!
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1377
            attribStat(tree.body, loopEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1378
            result = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1380
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1381
            loopEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1382
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
    public void visitLabelled(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
        // Check that label is not used in an enclosing statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
        Env<AttrContext> env1 = env;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1388
        while (env1 != null && !env1.tree.hasTag(CLASSDEF)) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1389
            if (env1.tree.hasTag(LABELLED) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
                ((JCLabeledStatement) env1.tree).label == tree.label) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1391
                log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1392
                          Errors.LabelAlreadyInUse(tree.label));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
            env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
        attribStat(tree.body, env.dup(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
    public void visitSwitch(JCSwitch tree) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1403
        handleSwitch(tree, tree.selector, tree.cases, (c, caseEnv) -> {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1404
            attribStats(c.stats, caseEnv);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1405
        });
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1406
        result = null;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1407
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1408
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1409
    public void visitSwitchExpression(JCSwitchExpression tree) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1410
        tree.polyKind = (pt().hasTag(NONE) && pt() != Type.recoveryType && pt() != Infer.anyPoly) ?
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1411
                PolyKind.STANDALONE : PolyKind.POLY;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1412
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1413
        if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1414
            //this means we are returning a poly conditional from void-compatible lambda expression
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1415
            resultInfo.checkContext.report(tree, diags.fragment(Fragments.SwitchExpressionTargetCantBeVoid));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1416
            result = tree.type = types.createErrorType(resultInfo.pt);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1417
            return;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1418
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1419
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1420
        ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1421
                unknownExprInfo :
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1422
                resultInfo.dup(switchExpressionContext(resultInfo.checkContext));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1423
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1424
        ListBuffer<DiagnosticPosition> caseTypePositions = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1425
        ListBuffer<Type> caseTypes = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1426
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1427
        handleSwitch(tree, tree.selector, tree.cases, (c, caseEnv) -> {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1428
            caseEnv.info.yieldResult = condInfo;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1429
            attribStats(c.stats, caseEnv);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1430
            new TreeScanner() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1431
                @Override
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1432
                public void visitYield(JCYield brk) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1433
                    if (brk.target == tree) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1434
                        caseTypePositions.append(brk.value != null ? brk.value.pos() : brk.pos());
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1435
                        caseTypes.append(brk.value != null ? brk.value.type : syms.errType);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1436
                    }
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1437
                    super.visitYield(brk);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1438
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1439
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1440
                @Override public void visitClassDef(JCClassDecl tree) {}
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1441
                @Override public void visitLambda(JCLambda tree) {}
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1442
            }.scan(c.stats);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1443
        });
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1444
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1445
        if (tree.cases.isEmpty()) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1446
            log.error(tree.pos(),
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1447
                      Errors.SwitchExpressionEmpty);
55528
bad3754349aa 8226510: No compilation error when switch expression has no result expressions
jlahoda
parents: 55306
diff changeset
  1448
        } else if (caseTypes.isEmpty()) {
bad3754349aa 8226510: No compilation error when switch expression has no result expressions
jlahoda
parents: 55306
diff changeset
  1449
            log.error(tree.pos(),
bad3754349aa 8226510: No compilation error when switch expression has no result expressions
jlahoda
parents: 55306
diff changeset
  1450
                      Errors.SwitchExpressionNoResultExpressions);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1451
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1452
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1453
        Type owntype = (tree.polyKind == PolyKind.STANDALONE) ? condType(caseTypePositions.toList(), caseTypes.toList()) : pt();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1454
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1455
        result = tree.type = check(tree, owntype, KindSelector.VAL, resultInfo);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1456
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1457
    //where:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1458
        CheckContext switchExpressionContext(CheckContext checkContext) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1459
            return new Check.NestedCheckContext(checkContext) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1460
                //this will use enclosing check context to check compatibility of
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1461
                //subexpression against target type; if we are in a method check context,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1462
                //depending on whether boxing is allowed, we could have incompatibilities
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1463
                @Override
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1464
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1465
                    enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleTypeInSwitchExpression(details)));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1466
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1467
            };
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1468
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1469
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1470
    private void handleSwitch(JCTree switchTree,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1471
                              JCExpression selector,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1472
                              List<JCCase> cases,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1473
                              BiConsumer<JCCase, Env<AttrContext>> attribCase) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1474
        Type seltype = attribExpr(selector, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
        Env<AttrContext> switchEnv =
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1477
            env.dup(switchTree, env.info.dup(env.info.scope.dup()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1479
        try {
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  1480
            boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0;
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40313
diff changeset
  1481
            boolean stringSwitch = types.isSameType(seltype, syms.stringType);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1482
            if (!enumSwitch && !stringSwitch)
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1483
                seltype = chk.checkType(selector.pos(), seltype, syms.intType);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1484
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1485
            // Attribute all cases and
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1486
            // check that there are no duplicate case labels or default clauses.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
  1487
            Set<Object> labels = new HashSet<>(); // The set of case labels.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1488
            boolean hasDefault = false;      // Is there a default label?
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 58350
diff changeset
  1489
            @SuppressWarnings("preview")
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 58350
diff changeset
  1490
            CaseTree.CaseKind caseKind = null;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1491
            boolean wasError = false;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1492
            for (List<JCCase> l = cases; l.nonEmpty(); l = l.tail) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1493
                JCCase c = l.head;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1494
                if (caseKind == null) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1495
                    caseKind = c.caseKind;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1496
                } else if (caseKind != c.caseKind && !wasError) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1497
                    log.error(c.pos(),
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1498
                              Errors.SwitchMixingCaseTypes);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1499
                    wasError = true;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1500
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1501
                if (c.getExpressions().nonEmpty()) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1502
                    for (JCExpression pat : c.getExpressions()) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1503
                        if (TreeInfo.isNull(pat)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1504
                            log.error(pat.pos(),
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1505
                                      Errors.SwitchNullNotAllowed);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1506
                        } else if (enumSwitch) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1507
                            Symbol sym = enumConstant(pat, seltype);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1508
                            if (sym == null) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1509
                                log.error(pat.pos(), Errors.EnumLabelMustBeUnqualifiedEnum);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1510
                            } else if (!labels.add(sym)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1511
                                log.error(c.pos(), Errors.DuplicateCaseLabel);
27553
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1512
                            }
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1513
                        } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1514
                            Type pattype = attribExpr(pat, switchEnv, seltype);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1515
                            if (!pattype.hasTag(ERROR)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1516
                                if (pattype.constValue() == null) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1517
                                    log.error(pat.pos(),
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1518
                                              (stringSwitch ? Errors.StringConstReq : Errors.ConstExprReq));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1519
                                } else if (!labels.add(pattype.constValue())) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1520
                                    log.error(c.pos(), Errors.DuplicateCaseLabel);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1521
                                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1522
                            }
27553
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1523
                        }
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1524
                    }
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1525
                } else if (hasDefault) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1526
                    log.error(c.pos(), Errors.DuplicateDefaultLabel);
27553
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1527
                } else {
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1528
                    hasDefault = true;
75321debd020 8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents: 27551
diff changeset
  1529
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1530
                Env<AttrContext> caseEnv =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1531
                    switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1532
                try {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1533
                    attribCase.accept(c, caseEnv);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1534
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1535
                    caseEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1536
                    addVars(c.stats, switchEnv.info.scope);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1537
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1538
            }
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1539
        } finally {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1540
            switchEnv.info.scope.leave();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
        /** Add any variables defined in stats to the switch scope. */
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  1545
        private static void addVars(List<JCStatement> stats, WriteableScope switchScope) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
            for (;stats.nonEmpty(); stats = stats.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
                JCTree stat = stats.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1548
                if (stat.hasTag(VARDEF))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
                    switchScope.enter(((JCVariableDecl) stat).sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
    /** Return the selected enumeration constant symbol, or null. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
    private Symbol enumConstant(JCTree tree, Type enumType) {
29430
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1555
        if (tree.hasTag(IDENT)) {
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1556
            JCIdent ident = (JCIdent)tree;
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1557
            Name name = ident.name;
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1558
            for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1559
                if (sym.kind == VAR) {
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1560
                    Symbol s = ident.sym = sym;
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1561
                    ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1562
                    ident.type = s.type;
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1563
                    return ((s.flags_field & Flags.ENUM) == 0)
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1564
                        ? null : s;
0fc887a8c519 8050021: Improper "duplicate case label" error
sadayapalam
parents: 29292
diff changeset
  1565
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
        chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
        attribStat(tree.body, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
    public void visitTry(JCTry tree) {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1578
        // 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
  1579
        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
  1580
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1581
            boolean isTryWithResource = tree.resources.nonEmpty();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1582
            // 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
  1583
            Env<AttrContext> tryEnv = isTryWithResource ?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1584
                env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1585
                localEnv;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1586
            try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1587
                // Attribute resource declarations
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1588
                for (JCTree resource : tree.resources) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1589
                    CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1590
                        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1591
                        public void report(DiagnosticPosition pos, JCDiagnostic details) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1592
                            chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1593
                        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1594
                    };
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1595
                    ResultInfo twrResult =
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  1596
                        new ResultInfo(KindSelector.VAR,
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1597
                                       syms.autoCloseableType,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1598
                                       twrContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1599
                    if (resource.hasTag(VARDEF)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1600
                        attribStat(resource, tryEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1601
                        twrResult.check(resource, resource.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1602
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1603
                        //check that resource type cannot throw InterruptedException
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1604
                        checkAutoCloseable(resource.pos(), localEnv, resource.type);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1605
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
  1606
                        VarSymbol var = ((JCVariableDecl) resource).sym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1607
                        var.setData(ElementKind.RESOURCE_VARIABLE);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1608
                    } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1609
                        attribTree(resource, tryEnv, twrResult);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1610
                    }
12334
29e1bfdcba4e 7151492: Encapsulate check logic into Attr.ResultInfo
mcimadamore
parents: 12333
diff changeset
  1611
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1612
                // Attribute body
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1613
                attribStat(tree.body, tryEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1614
            } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1615
                if (isTryWithResource)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1616
                    tryEnv.info.scope.leave();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1617
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1618
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1619
            // Attribute catch clauses
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1620
            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
  1621
                JCCatch c = l.head;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1622
                Env<AttrContext> catchEnv =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1623
                    localEnv.dup(c, localEnv.info.dup(localEnv.info.scope.dup()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1624
                try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1625
                    Type ctype = attribStat(c.param, catchEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1626
                    if (TreeInfo.isMultiCatch(c)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1627
                        //multi-catch parameter is implicitly marked as final
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1628
                        c.param.sym.flags_field |= FINAL | UNION;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1629
                    }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1630
                    if (c.param.sym.kind == VAR) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1631
                        c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1632
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1633
                    chk.checkType(c.param.vartype.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1634
                                  chk.checkClassType(c.param.vartype.pos(), ctype),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1635
                                  syms.throwableType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1636
                    attribStat(c.body, catchEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1637
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1638
                    catchEnv.info.scope.leave();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1639
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1640
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1641
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1642
            // Attribute finalizer
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1643
            if (tree.finalizer != null) attribStat(tree.finalizer, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1644
            result = null;
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  1645
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1646
        finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1647
            localEnv.info.scope.leave();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1651
    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
  1652
        if (!resource.isErroneous() &&
12013
fdcc73079b81 7148025: javac should not warn about InterrupttedException on the declaration of AutoCloseable itself
darcy
parents: 11144
diff changeset
  1653
            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
  1654
            !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
  1655
            Symbol close = syms.noSymbol;
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14443
diff changeset
  1656
            Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log);
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1657
            try {
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1658
                close = rs.resolveQualifiedMethod(pos,
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1659
                        env,
41156
251471aec877 8163027: AssertionError while compiling a program that uses try with resources.
sadayapalam
parents: 41154
diff changeset
  1660
                        types.skipTypeVars(resource, false),
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1661
                        names.close,
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1662
                        List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1663
                        List.nil());
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1664
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1665
            finally {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14443
diff changeset
  1666
                log.popDiagnosticHandler(discardHandler);
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1667
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1668
            if (close.kind == MTH &&
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1669
                    close.overrides(syms.autoCloseableClose, resource.tsym, types, true) &&
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1670
                    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
  1671
                    env.info.lint.isEnabled(LintCategory.TRY)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1672
                log.warning(LintCategory.TRY, pos, Warnings.TryResourceThrowsInterruptedExc(resource));
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1673
            }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1674
        }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1675
    }
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  1676
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
    public void visitConditional(JCConditional tree) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1678
        Type condtype = attribExpr(tree.cond, env, syms.booleanType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1679
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1680
        tree.polyKind = (!allowPoly ||
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1681
                pt().hasTag(NONE) && pt() != Type.recoveryType && pt() != Infer.anyPoly ||
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1682
                isBooleanOrNumeric(env, tree)) ?
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1683
                PolyKind.STANDALONE : PolyKind.POLY;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1684
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1685
        if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1686
            //this means we are returning a poly conditional from void-compatible lambda expression
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1687
            resultInfo.checkContext.report(tree, diags.fragment(Fragments.ConditionalTargetCantBeVoid));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1688
            result = tree.type = types.createErrorType(resultInfo.pt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1689
            return;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1690
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1691
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1692
        ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1693
                unknownExprInfo :
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1694
                resultInfo.dup(conditionalContext(resultInfo.checkContext));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1695
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1696
        Type truetype = attribTree(tree.truepart, env, condInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1697
        Type falsetype = attribTree(tree.falsepart, env, condInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1698
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1699
        Type owntype = (tree.polyKind == PolyKind.STANDALONE) ?
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1700
                condType(List.of(tree.truepart.pos(), tree.falsepart.pos()),
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1701
                         List.of(truetype, falsetype)) : pt();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1702
        if (condtype.constValue() != null &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1703
                truetype.constValue() != null &&
14953
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14725
diff changeset
  1704
                falsetype.constValue() != null &&
89a34297b198 8004099: Bad compiler diagnostic generated when poly expression is passed to non-existent method
mcimadamore
parents: 14725
diff changeset
  1705
                !owntype.hasTag(NONE)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1706
            //constant folding
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1707
            owntype = cfolder.coerce(condtype.isTrue() ? truetype : falsetype, owntype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1708
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  1709
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1712
        private boolean isBooleanOrNumeric(Env<AttrContext> env, JCExpression tree) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1713
            switch (tree.getTag()) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1714
                case LITERAL: return ((JCLiteral)tree).typetag.isSubRangeOf(DOUBLE) ||
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1715
                              ((JCLiteral)tree).typetag == BOOLEAN ||
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  1716
                              ((JCLiteral)tree).typetag == BOT;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1717
                case LAMBDA: case REFERENCE: return false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1718
                case PARENS: return isBooleanOrNumeric(env, ((JCParens)tree).expr);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1719
                case CONDEXPR:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1720
                    JCConditional condTree = (JCConditional)tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1721
                    return isBooleanOrNumeric(env, condTree.truepart) &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1722
                            isBooleanOrNumeric(env, condTree.falsepart);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1723
                case APPLY:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1724
                    JCMethodInvocation speculativeMethodTree =
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1725
                            (JCMethodInvocation)deferredAttr.attribSpeculative(
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1726
                                    tree, env, unknownExprInfo,
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1727
                                    argumentAttr.withLocalCacheContext());
27551
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1728
                    Symbol msym = TreeInfo.symbol(speculativeMethodTree.meth);
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1729
                    Type receiverType = speculativeMethodTree.meth.hasTag(IDENT) ?
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1730
                            env.enclClass.type :
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1731
                            ((JCFieldAccess)speculativeMethodTree.meth).selected.type;
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1732
                    Type owntype = types.memberType(receiverType, msym).getReturnType();
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1733
                    return primitiveOrBoxed(owntype);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1734
                case NEWCLASS:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1735
                    JCExpression className =
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1736
                            removeClassParams.translate(((JCNewClass)tree).clazz);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1737
                    JCExpression speculativeNewClassTree =
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1738
                            (JCExpression)deferredAttr.attribSpeculative(
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1739
                                    className, env, unknownTypeInfo,
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1740
                                    argumentAttr.withLocalCacheContext());
27551
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1741
                    return primitiveOrBoxed(speculativeNewClassTree.type);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1742
                default:
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1743
                    Type speculativeType = deferredAttr.attribSpeculative(tree, env, unknownExprInfo,
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44392
diff changeset
  1744
                            argumentAttr.withLocalCacheContext()).type;
27551
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1745
                    return primitiveOrBoxed(speculativeType);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1746
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1747
        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1748
        //where
27551
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1749
            boolean primitiveOrBoxed(Type t) {
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1750
                return (!t.hasTag(TYPEVAR) && types.unboxedTypeOrType(t).isPrimitive());
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1751
            }
14a74a56c4a0 8064464: regression with type inference of conditional expression
mcimadamore
parents: 27231
diff changeset
  1752
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1753
            TreeTranslator removeClassParams = new TreeTranslator() {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1754
                @Override
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1755
                public void visitTypeApply(JCTypeApply tree) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1756
                    result = translate(tree.clazz);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1757
                }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  1758
            };
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1759
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1760
        CheckContext conditionalContext(CheckContext checkContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1761
            return new Check.NestedCheckContext(checkContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1762
                //this will use enclosing check context to check compatibility of
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1763
                //subexpression against target type; if we are in a method check context,
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1764
                //depending on whether boxing is allowed, we could have incompatibilities
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1765
                @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1766
                public void report(DiagnosticPosition pos, JCDiagnostic details) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1767
                    enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleTypeInConditional(details)));
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1768
                }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1769
            };
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1770
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  1771
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
        /** Compute the type of a conditional expression, after
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1773
         *  checking that it exists.  See JLS 15.25. Does not take into
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
         *  account the special case where condition and both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
         *  are constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
         *  @param pos      The source position to be used for error
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
         *                  diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
         *  @param thentype The type of the expression's then-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
         *  @param elsetype The type of the expression's else-part.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
         */
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1782
        Type condType(List<DiagnosticPosition> positions, List<Type> condTypes) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1783
            if (condTypes.isEmpty()) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1784
                return syms.objectType; //TODO: how to handle?
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1785
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1786
            Type first = condTypes.head;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
            // If same type, that is the result
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1788
            if (condTypes.tail.stream().allMatch(t -> types.isSameType(first, t)))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1789
                return first.baseType();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1790
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1791
            List<Type> unboxedTypes = condTypes.stream()
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1792
                                               .map(t -> t.isPrimitive() ? t : types.unboxedType(t))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1793
                                               .collect(List.collector());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
            // Otherwise, if both arms can be converted to a numeric
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
            // type, return the least numeric type that fits both arms
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
            // (i.e. return larger of the two, or return int if one
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
            // arm is short, the other is char).
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1799
            if (unboxedTypes.stream().allMatch(t -> t.isPrimitive())) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
                // If one arm has an integer subrange type (i.e., byte,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
                // short, or char), and the other is an integer constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
                // that fits into the subrange, return the subrange type.
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1803
                for (Type type : unboxedTypes) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1804
                    if (!type.getTag().isStrictSubRangeOf(INT)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1805
                        continue;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1806
                    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1807
                    if (unboxedTypes.stream().filter(t -> t != type).allMatch(t -> t.hasTag(INT) && types.isAssignable(t, type)))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1808
                        return type.baseType();
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1809
                }
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1810
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1811
                for (TypeTag tag : primitiveTags) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1812
                    Type candidate = syms.typeOfTag[tag.ordinal()];
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1813
                    if (unboxedTypes.stream().allMatch(t -> types.isSubtype(t, candidate))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
                        return candidate;
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1815
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
            // Those were all the cases that could result in a primitive
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1820
            condTypes = condTypes.stream()
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1821
                                 .map(t -> t.isPrimitive() ? types.boxedClass(t).type : t)
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1822
                                 .collect(List.collector());
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1823
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1824
            for (Type type : condTypes) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1825
                if (condTypes.stream().filter(t -> t != type).allMatch(t -> types.isAssignable(t, type)))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1826
                    return type.baseType();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1827
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1828
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1829
            Iterator<DiagnosticPosition> posIt = positions.iterator();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1830
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1831
            condTypes = condTypes.stream()
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1832
                                 .map(t -> chk.checkNonVoid(posIt.next(), t))
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1833
                                 .collect(List.collector());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
            // both are known to be reference types.  The result is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
            // lub(thentype,elsetype). This cannot fail, as it will
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
            // always be possible to infer "Object" if nothing better.
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1838
            return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1841
    final static TypeTag[] primitiveTags = new TypeTag[]{
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1842
        BYTE,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1843
        CHAR,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1844
        SHORT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1845
        INT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1846
        LONG,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1847
        FLOAT,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1848
        DOUBLE,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1849
        BOOLEAN,
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1850
    };
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18393
diff changeset
  1851
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
    public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
        attribStat(tree.thenpart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1855
        if (tree.elsepart != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
            attribStat(tree.elsepart, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1857
        chk.checkEmptyIf(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1858
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1860
06bc494ca11e Initial load
duke
parents:
diff changeset
  1861
    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
  1862
        //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
  1863
        //see Infer.instantiatePolymorphicSignatureInstance()
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6587
diff changeset
  1864
        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
  1865
        attribExpr(tree.expr, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
    public void visitBreak(JCBreak tree) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1870
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1871
        result = null;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1872
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1873
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1874
    public void visitYield(JCYield tree) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1875
        if (env.info.yieldResult != null) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1876
            attribTree(tree.value, env, env.info.yieldResult);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1877
            tree.target = findJumpTarget(tree.pos(), tree.getTag(), names.empty, env);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1878
        } else {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1879
            log.error(tree.pos(), tree.value.hasTag(PARENS)
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1880
                    ? Errors.NoSwitchExpressionQualify
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1881
                    : Errors.NoSwitchExpression);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1882
            attribTree(tree.value, env, unknownExprInfo);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1883
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
    public void visitContinue(JCContinue tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
        tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
    //where
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1892
        /** Return the target of a break, continue or yield statement,
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1893
         *  if it exists, report an error if not.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
         *  Note: The target of a labelled break or continue is the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
         *  (non-labelled) statement tree referred to by the label,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
         *  not the tree representing the labelled statement itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
         *  @param pos     The position to be used for error diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
         *  @param tag     The tag of the jump statement. This is either
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
         *                 Tree.BREAK or Tree.CONTINUE.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
         *  @param label   The label of the jump statement, or null if no
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
         *                 label is given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
         *  @param env     The environment current at the jump statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
        private JCTree findJumpTarget(DiagnosticPosition pos,
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1906
                                                   JCTree.Tag tag,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1907
                                                   Name label,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1908
                                                   Env<AttrContext> env) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1909
            Pair<JCTree, Error> jumpTarget = findJumpTargetNoError(tag, label, env);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1910
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1911
            if (jumpTarget.snd != null) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1912
                log.error(pos, jumpTarget.snd);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1913
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1914
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1915
            return jumpTarget.fst;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1916
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1917
        /** Return the target of a break or continue statement, if it exists,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1918
         *  report an error if not.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1919
         *  Note: The target of a labelled break or continue is the
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1920
         *  (non-labelled) statement tree referred to by the label,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1921
         *  not the tree representing the labelled statement itself.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1922
         *
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1923
         *  @param tag     The tag of the jump statement. This is either
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1924
         *                 Tree.BREAK or Tree.CONTINUE.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1925
         *  @param label   The label of the jump statement, or null if no
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1926
         *                 label is given.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1927
         *  @param env     The environment current at the jump statement.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1928
         */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1929
        private Pair<JCTree, JCDiagnostic.Error> findJumpTargetNoError(JCTree.Tag tag,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1930
                                                                       Name label,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1931
                                                                       Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
            // Search environments outwards from the point of jump.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
            Env<AttrContext> env1 = env;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1934
            JCDiagnostic.Error pendingError = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
            LOOP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
            while (env1 != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
                switch (env1.tree.getTag()) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1938
                    case LABELLED:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1939
                        JCLabeledStatement labelled = (JCLabeledStatement)env1.tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1940
                        if (label == labelled.label) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1941
                            // If jump is a continue, check that target is a loop.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1942
                            if (tag == CONTINUE) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1943
                                if (!labelled.body.hasTag(DOLOOP) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1944
                                        !labelled.body.hasTag(WHILELOOP) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1945
                                        !labelled.body.hasTag(FORLOOP) &&
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1946
                                        !labelled.body.hasTag(FOREACHLOOP)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1947
                                    pendingError = Errors.NotLoopLabel(label);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1948
                                }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1949
                                // Found labelled statement target, now go inwards
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1950
                                // to next non-labelled tree.
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1951
                                return Pair.of(TreeInfo.referencedStatement(labelled), pendingError);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1952
                            } else {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1953
                                return Pair.of(labelled, pendingError);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1954
                            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
                        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1956
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1957
                    case DOLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1958
                    case WHILELOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1959
                    case FORLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1960
                    case FOREACHLOOP:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1961
                        if (label == null) return Pair.of(env1.tree, pendingError);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1962
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1963
                    case SWITCH:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1964
                        if (label == null && tag == BREAK) return Pair.of(env1.tree, null);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1965
                        break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1966
                    case SWITCH_EXPRESSION:
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1967
                        if (tag == YIELD) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1968
                            return Pair.of(env1.tree, null);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1969
                        } else if (tag == BREAK) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1970
                            pendingError = Errors.BreakOutsideSwitchExpression;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1971
                        } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1972
                            pendingError = Errors.ContinueOutsideSwitchExpression;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1973
                        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1974
                        break;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1975
                    case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1976
                    case METHODDEF:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1977
                    case CLASSDEF:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1978
                        break LOOP;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1979
                    default:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1980
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
                env1 = env1.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
            if (label != null)
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1984
                return Pair.of(null, Errors.UndefLabel(label));
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1985
            else if (pendingError != null)
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1986
                return Pair.of(null, pendingError);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  1987
            else if (tag == CONTINUE)
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1988
                return Pair.of(null, Errors.ContOutsideLoop);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            else
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1990
                return Pair.of(null, Errors.BreakOutsideSwitchLoop);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
        // Check that there is an enclosing method which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
        // nested within than the enclosing class.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  1996
        if (env.info.returnResult == null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  1997
            log.error(tree.pos(), Errors.RetOutsideMeth);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  1998
        } else if (env.info.yieldResult != null) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51103
diff changeset
  1999
            log.error(tree.pos(), Errors.ReturnOutsideSwitchExpression);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
            // Attribute return expression, if it exists, and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
            // it conforms to result type of enclosing method.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2003
            if (tree.expr != null) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2004
                if (env.info.returnResult.pt.hasTag(VOID)) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2005
                    env.info.returnResult.checkContext.report(tree.expr.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2006
                              diags.fragment(Fragments.UnexpectedRetVal));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2007
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2008
                attribTree(tree.expr, env, env.info.returnResult);
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18899
diff changeset
  2009
            } else if (!env.info.returnResult.pt.hasTag(VOID) &&
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18899
diff changeset
  2010
                    !env.info.returnResult.pt.hasTag(NONE)) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2011
                env.info.returnResult.checkContext.report(tree.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2012
                              diags.fragment(Fragments.MissingRetVal(env.info.returnResult.pt)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2013
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2014
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2016
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
    public void visitThrow(JCThrow tree) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2019
        Type owntype = attribExpr(tree.expr, env, allowPoly ? Type.noType : syms.throwableType);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2020
        if (allowPoly) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2021
            chk.checkType(tree, owntype, syms.throwableType);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2022
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
    public void visitAssert(JCAssert tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
        attribExpr(tree.cond, env, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
        if (tree.detail != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
            chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2030
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
     /** Visitor method for method invocations.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
     *  NOTE: The method part of an application will have in its type field
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
     *        the return type of the method, not the method's type itself!
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
        // The local environment of a method application is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
        // The types of the actual method arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
        List<Type> argtypes;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
        // The types of the actual method type arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
        List<Type> typeargtypes = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
        Name methName = TreeInfo.name(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
        boolean isConstructorCall =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
            methName == names._this || methName == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  2054
        ListBuffer<Type> argtypesBuf = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
        if (isConstructorCall) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
            // We are seeing a ...this(...) or ...super(...) call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
            // Check that this is the first statement in a constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
            if (checkFirstConstructorStat(tree, env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
                // Record the fact
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
                // that this is a constructor call (using isSelfCall).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
                localEnv.info.isSelfCall = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
                // Attribute arguments, yielding list of argument types.
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
  2065
                KindSelector kind = attribArgs(KindSelector.MTH, tree.args, localEnv, argtypesBuf);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2066
                argtypes = argtypesBuf.toList();
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2067
                typeargtypes = attribTypes(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
                // Variable `site' points to the class in which the called
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
                // constructor is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
                Type site = env.enclClass.sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
                if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
                    if (site == syms.objectType) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2074
                        log.error(tree.meth.pos(), Errors.NoSuperclass(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
  2075
                        site = types.createErrorType(syms.objectType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
                        site = types.supertype(site);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2081
                if (site.hasTag(CLASS)) {
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  2082
                    Type encl = site.getEnclosingType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2083
                    while (encl != null && encl.hasTag(TYPEVAR))
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  2084
                        encl = encl.getUpperBound();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2085
                    if (encl.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
                        // we are calling a nested class
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2088
                        if (tree.meth.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
                            JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
                            // We are seeing a prefixed call, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
                            //     <expr>.super(...).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
                            // Check that the prefix expression conforms
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
                            // to the outer instance type of the class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
                            chk.checkRefType(qualifier.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
                                             attribExpr(qualifier, localEnv,
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  2097
                                                        encl));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
                        } else if (methName == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
                            // qualifier omitted; check for existence
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
                            // of an appropriate implicit qualifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
                            rs.resolveImplicitThis(tree.meth.pos(),
8622
4f032629a0fd 6541876: "Enclosing Instance" error new in 1.6
mcimadamore
parents: 8616
diff changeset
  2102
                                                   localEnv, site, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
                        }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2104
                    } else if (tree.meth.hasTag(SELECT)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2105
                        log.error(tree.meth.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2106
                                  Errors.IllegalQualNotIcls(site.tsym));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
06bc494ca11e Initial load
duke
parents:
diff changeset
  2109
                    // if we're calling a java.lang.Enum constructor,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
                    // prefix the implicit String and int parameters
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  2111
                    if (site.tsym == syms.enumSym)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
                        argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
                    // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
                    // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
                    // current instance (JLS ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
                    boolean selectSuperPrev = localEnv.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
                    localEnv.info.selectSuper = true;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2119
                    localEnv.info.pendingResolutionPhase = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
                    Symbol sym = rs.resolveConstructor(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
                        tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
                    localEnv.info.selectSuper = selectSuperPrev;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
                    // Set method symbol to resolved constructor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
                    TreeInfo.setSymbol(tree.meth, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
                    // ...and check that it is legal in the current context.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
                    // (this will also set the tree's type)
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  2129
                    Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2130
                    checkId(tree.meth, site, sym, localEnv,
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
  2131
                            new ResultInfo(kind, mpt));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
                // Otherwise, `site' is an error type and we do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
            result = tree.type = syms.voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
            // Otherwise, we are seeing a regular method call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
            // Attribute the arguments, yielding list of argument types, ...
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
  2139
            KindSelector kind = attribArgs(KindSelector.VAL, tree.args, localEnv, argtypesBuf);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2140
            argtypes = argtypesBuf.toList();
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2141
            typeargtypes = attribAnyTypes(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
            // ... and attribute the method using as a prototype a methodtype
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
            // whose formal argument types is exactly the list of actual
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
            // arguments (this will also set the method symbol).
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  2146
            Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2147
            localEnv.info.pendingResolutionPhase = null;
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2148
            Type mtype = attribTree(tree.meth, localEnv, new ResultInfo(kind, mpt, resultInfo.checkContext));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
            // Compute the result type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
            Type restype = mtype.getReturnType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2152
            if (restype.hasTag(WILDCARD))
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6594
diff changeset
  2153
                throw new AssertionError(mtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2155
            Type qualifier = (tree.meth.hasTag(SELECT))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
                    ? ((JCFieldAccess) tree.meth).selected.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
                    : env.enclClass.sym.type;
39810
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2158
            Symbol msym = TreeInfo.symbol(tree.meth);
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2159
            restype = adjustMethodReturnType(msym, qualifier, methName, argtypes, restype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 8032
diff changeset
  2161
            chk.checkRefTypes(tree.typeargs, typeargtypes);
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  2162
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
            // Check that value of resulting type is admissible in the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
            // current context.  Also, capture the return type
33708
99085a01b536 8141613: Compiler fails to infer generic type
mcimadamore
parents: 33366
diff changeset
  2165
            Type capturedRes = resultInfo.checkContext.inferenceContext().cachedCapture(tree, restype, true);
99085a01b536 8141613: Compiler fails to infer generic type
mcimadamore
parents: 33366
diff changeset
  2166
            result = check(tree, capturedRes, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
        }
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2168
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
    //where
39810
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2171
        Type adjustMethodReturnType(Symbol msym, Type qualifierType, Name methodName, List<Type> argtypes, Type restype) {
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2172
            if (msym != null &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2173
                    msym.owner == syms.objectType.tsym &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2174
                    methodName == names.getClass &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2175
                    argtypes.isEmpty()) {
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2176
                // as a special case, x.getClass() has type Class<? extends |X|>
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2177
                return new ClassType(restype.getEnclosingType(),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2178
                        List.of(new WildcardType(types.erasure(qualifierType),
39810
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2179
                                BoundKind.EXTENDS,
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2180
                                syms.boundClass)),
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2181
                        restype.tsym,
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2182
                        restype.getMetadata());
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2183
            } else if (msym != null &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2184
                    msym.owner == syms.arrayClass &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2185
                    methodName == names.clone &&
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  2186
                    types.isArray(qualifierType)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2187
                // as a special case, array.clone() has a result that is
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2188
                // the same as static type of the array being cloned
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2189
                return qualifierType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2190
            } else {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2191
                return restype;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2192
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2193
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2194
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
        /** Check that given application node appears as first statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
         *  in a constructor call.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
         *  @param tree   The application node
06bc494ca11e Initial load
duke
parents:
diff changeset
  2198
         *  @param env    The environment current at the application.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
        boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
            JCMethodDecl enclMethod = env.enclMethod;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
            if (enclMethod != null && enclMethod.name == names.init) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
                JCBlock body = enclMethod.body;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2204
                if (body.stats.head.hasTag(EXEC) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
                    ((JCExpressionStatement) body.stats.head).expr == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2206
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
            }
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2208
            log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2209
                      Errors.CallMustBeFirstStmtInCtor(TreeInfo.name(tree.meth)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
        /** Obtain a method type with given argument types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
         */
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  2215
        Type newMethodTemplate(Type restype, List<Type> argtypes, List<Type> typeargtypes) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2216
            MethodType mt = new MethodType(argtypes, restype, List.nil(), syms.methodClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2217
            return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2220
    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
  2221
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
        // The local environment of a class creation is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        // a new environment nested in the current one.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
        // The anonymous inner class definition of the new expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
        // if one is defined by it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
        JCClassDecl cdef = tree.def;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
        // If enclosing class is given, attribute it, and
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
        // complete class name to be fully qualified
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
        JCExpression clazz = tree.clazz; // Class field following new
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2234
        JCExpression clazzid;            // Identifier in class field
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2235
        JCAnnotatedType annoclazzid;     // Annotated type enclosing clazzid
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2236
        annoclazzid = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2237
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2238
        if (clazz.hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2239
            clazzid = ((JCTypeApply) clazz).clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2240
            if (clazzid.hasTag(ANNOTATED_TYPE)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2241
                annoclazzid = (JCAnnotatedType) clazzid;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2242
                clazzid = annoclazzid.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2243
            }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2244
        } else {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2245
            if (clazz.hasTag(ANNOTATED_TYPE)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2246
                annoclazzid = (JCAnnotatedType) clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2247
                clazzid = annoclazzid.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2248
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2249
                clazzid = clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2250
            }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2251
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
        JCExpression clazzid1 = clazzid; // The same in fully qualified form
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
            // We are seeing a qualified new, of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
            //    <expr>.new C <...> (...) ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  2258
            // In this case, we let clazz stand for the name of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2259
            // allocated class C prefixed with the type of the qualifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
            // expression, so that we can
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
            // resolve it with standard techniques later. I.e., if
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
            // <expr> has type T, then <expr>.new C <...> (...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
            // yields a clazz T.C.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
            Type encltype = chk.checkRefType(tree.encl.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
                                             attribExpr(tree.encl, env));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2266
            // 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
  2267
            // from expr to the combined type, or not? Yes, do this.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
            clazzid1 = make.at(clazz.pos).Select(make.Type(encltype),
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
                                                 ((JCIdent) clazzid).name);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2270
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 18920
diff changeset
  2271
            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
  2272
            endPosTable.storeEnd(clazzid1, tree.getEndPosition(endPosTable));
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2273
            if (clazz.hasTag(ANNOTATED_TYPE)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2274
                JCAnnotatedType annoType = (JCAnnotatedType) clazz;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2275
                List<JCAnnotation> annos = annoType.annotations;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2276
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2277
                if (annoType.underlyingType.hasTag(TYPEAPPLY)) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2278
                    clazzid1 = make.at(tree.pos).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2279
                        TypeApply(clazzid1,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2280
                                  ((JCTypeApply) clazz).arguments);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2281
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2282
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2283
                clazzid1 = make.at(tree.pos).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2284
                    AnnotatedType(annos, clazzid1);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2285
            } else if (clazz.hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2286
                clazzid1 = make.at(tree.pos).
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
                    TypeApply(clazzid1,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
                              ((JCTypeApply) clazz).arguments);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2289
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2290
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2291
            clazz = clazzid1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
        // Attribute clazz expression and store
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
        // symbol + type back into the attributed tree.
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2296
        Type clazztype;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2297
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2298
        try {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2299
            env.info.isNewClass = true;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2300
            clazztype = TreeInfo.isEnumInit(env.tree) ?
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2301
                attribIdentAsEnumType(env, (JCIdent)clazz) :
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2302
                attribType(clazz, env);
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2303
        } finally {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2304
            env.info.isNewClass = false;
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  2305
        }
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12915
diff changeset
  2306
8635
383a416a2bdf 7020044: Project Coin: diamond erroneous allowed on some anonymous inner classes
mcimadamore
parents: 8625
diff changeset
  2307
        clazztype = chk.checkDiamond(tree, clazztype);
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2308
        chk.validate(clazz, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
        if (tree.encl != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
            // We have to work in this case to store
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
            // symbol + type back into the attributed tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
            tree.clazz.type = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
            TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
            clazzid.type = ((JCIdent) clazzid).sym.type;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2315
            if (annoclazzid != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2316
                annoclazzid.type = clazzid.type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2317
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
            if (!clazztype.isErroneous()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
                if (cdef != null && clazztype.tsym.isInterface()) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2320
                    log.error(tree.encl.pos(), Errors.AnonClassImplIntfNoQualForNew);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
                } else if (clazztype.tsym.isStatic()) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2322
                    log.error(tree.encl.pos(), Errors.QualifiedNewOfStaticClass(clazztype.tsym));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2325
        } else if (!clazztype.tsym.isInterface() &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2326
                   clazztype.getEnclosingType().hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
            // Check for the existence of an apropos outer instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
            rs.resolveImplicitThis(tree.pos(), env, clazztype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2329
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
        // Attribute constructor arguments.
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  2332
        ListBuffer<Type> argtypesBuf = new ListBuffer<>();
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2333
        final KindSelector pkind =
27856
d4711a6931e2 8065986: Compiler fails to NullPointerException when calling super with Object<>()
mcimadamore
parents: 27851
diff changeset
  2334
            attribArgs(KindSelector.VAL, tree.args, localEnv, argtypesBuf);
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  2335
        List<Type> argtypes = argtypesBuf.toList();
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  2336
        List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
48931
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  2338
        if (clazztype.hasTag(CLASS) || clazztype.hasTag(ERROR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
            // Enums may not be instantiated except implicitly
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  2340
            if ((clazztype.tsym.flags_field & Flags.ENUM) != 0 &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2341
                (!env.tree.hasTag(VARDEF) ||
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  2342
                 (((JCVariableDecl) env.tree).mods.flags & Flags.ENUM) == 0 ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2343
                 ((JCVariableDecl) env.tree).init != tree))
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2344
                log.error(tree.pos(), Errors.EnumCantBeInstantiated);
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2345
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2346
            boolean isSpeculativeDiamondInferenceRound = TreeInfo.isDiamond(tree) &&
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2347
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2348
            boolean skipNonDiamondPath = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
            // Check that class is not abstract
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2350
            if (cdef == null && !isSpeculativeDiamondInferenceRound && // class body may be nulled out in speculative tree copy
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
                (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2352
                log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2353
                          Errors.AbstractCantBeInstantiated(clazztype.tsym));
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2354
                skipNonDiamondPath = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
            } else if (cdef != null && clazztype.tsym.isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
                // Check that no constructor arguments are given to
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
                // anonymous classes implementing an interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
                if (!argtypes.isEmpty())
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2359
                    log.error(tree.args.head.pos(), Errors.AnonClassImplIntfNoArgs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
                if (!typeargtypes.isEmpty())
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2362
                    log.error(tree.typeargs.head.pos(), Errors.AnonClassImplIntfNoTypeargs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
                // Error recovery: pretend no arguments were supplied.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
                argtypes = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
                typeargtypes = List.nil();
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2367
                skipNonDiamondPath = true;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2368
            }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2369
            if (TreeInfo.isDiamond(tree)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2370
                ClassType site = new ClassType(clazztype.getEnclosingType(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2371
                            clazztype.tsym.type.getTypeArguments(),
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24296
diff changeset
  2372
                                               clazztype.tsym,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  2373
                                               clazztype.getMetadata());
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2374
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2375
                Env<AttrContext> diamondEnv = localEnv.dup(tree);
52455
bbbc110cdffe 8210197: javac can't tell during speculative attribution if a diamond expression is creating an anonymous inner class or not
vromero
parents: 51897
diff changeset
  2376
                diamondEnv.info.selectSuper = cdef != null || tree.classDeclRemoved();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2377
                diamondEnv.info.pendingResolutionPhase = null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2378
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2379
                //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
  2380
                //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
  2381
                //of the resolved constructor will be a partially instantiated type
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2382
                Symbol constructor = rs.resolveDiamond(tree.pos(),
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2383
                            diamondEnv,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2384
                            site,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2385
                            argtypes,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2386
                            typeargtypes);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2387
                tree.constructor = constructor.baseSymbol();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2388
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2389
                final TypeSymbol csym = clazztype.tsym;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2390
                ResultInfo diamondResult = new ResultInfo(pkind, newMethodTemplate(resultInfo.pt, argtypes, typeargtypes),
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2391
                        diamondContext(tree, csym, resultInfo.checkContext), CheckMode.NO_TREE_UPDATE);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2392
                Type constructorType = tree.constructorType = types.createErrorType(clazztype);
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2393
                constructorType = checkId(tree, site,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2394
                        constructor,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2395
                        diamondEnv,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2396
                        diamondResult);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2397
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2398
                tree.clazz.type = types.createErrorType(clazztype);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2399
                if (!constructorType.isErroneous()) {
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2400
                    tree.clazz.type = clazz.type = constructorType.getReturnType();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2401
                    tree.constructorType = types.createMethodTypeWithReturn(constructorType, syms.voidType);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2402
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2403
                clazztype = chk.checkClassType(tree.clazz, tree.clazz.type, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2404
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
06bc494ca11e Initial load
duke
parents:
diff changeset
  2406
            // Resolve the called constructor under the assumption
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
            // that we are referring to a superclass instance of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
            // current instance (JLS ???).
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2409
            else if (!skipNonDiamondPath) {
9724
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2410
                //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
  2411
                //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
  2412
                //order to avoid downstream failures
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2413
                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
  2414
                rsEnv.info.selectSuper = cdef != null;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2415
                rsEnv.info.pendingResolutionPhase = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
                tree.constructor = rs.resolveConstructor(
9724
1eab738591a7 7043922: Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
mcimadamore
parents: 9599
diff changeset
  2417
                    tree.pos(), rsEnv, clazztype, argtypes, typeargtypes);
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2418
                if (cdef == null) { //do not check twice!
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2419
                    tree.constructorType = checkId(tree,
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2420
                            clazztype,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2421
                            tree.constructor,
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2422
                            rsEnv,
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2423
                            new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes), CheckMode.NO_TREE_UPDATE));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2424
                    if (rsEnv.info.lastResolveVarargs())
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2425
                        Assert.check(tree.constructorType.isErroneous() || tree.varargsElement != null);
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  2426
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
            if (cdef != null) {
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2430
                visitAnonymousClassDefinition(tree, clazz, clazztype, cdef, localEnv, argtypes, typeargtypes, pkind);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2431
                return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
            if (tree.constructor != null && tree.constructor.kind == MTH)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
                owntype = clazztype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2437
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2438
        InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext();
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2439
        if (tree.constructorType != null && inferenceContext.free(tree.constructorType)) {
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2440
            //we need to wait for inference to finish and then replace inference vars in the constructor type
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2441
            inferenceContext.addFreeTypeListener(List.of(tree.constructorType),
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2442
                    instantiatedContext -> {
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2443
                        tree.constructorType = instantiatedContext.asInstType(tree.constructorType);
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2444
                    });
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2445
        }
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 1260
diff changeset
  2446
        chk.validate(tree.typeargs, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2447
    }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2448
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2449
        // where
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2450
        private void visitAnonymousClassDefinition(JCNewClass tree, JCExpression clazz, Type clazztype,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2451
                                                   JCClassDecl cdef, Env<AttrContext> localEnv,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2452
                                                   List<Type> argtypes, List<Type> typeargtypes,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2453
                                                   KindSelector pkind) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2454
            // We are seeing an anonymous class instance creation.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2455
            // In this case, the class instance creation
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2456
            // expression
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2457
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2458
            //    E.new <typeargs1>C<typargs2>(args) { ... }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2459
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2460
            // is represented internally as
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2461
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2462
            //    E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } )  .
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2463
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2464
            // This expression is then *transformed* as follows:
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2465
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2466
            // (1) add an extends or implements clause
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2467
            // (2) add a constructor.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2468
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2469
            // For instance, if C is a class, and ET is the type of E,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2470
            // the expression
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2471
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2472
            //    E.new <typeargs1>C<typargs2>(args) { ... }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2473
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2474
            // is translated to (where X is a fresh name and typarams is the
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2475
            // parameter list of the super constructor):
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2476
            //
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2477
            //   new <typeargs1>X(<*nullchk*>E, args) where
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2478
            //     X extends C<typargs2> {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2479
            //       <typarams> X(ET e, args) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2480
            //         e.<typeargs1>super(args)
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2481
            //       }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2482
            //       ...
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2483
            //     }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2484
            InferenceContext inferenceContext = resultInfo.checkContext.inferenceContext();
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2485
            final boolean isDiamond = TreeInfo.isDiamond(tree);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2486
            if (isDiamond
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2487
                    && ((tree.constructorType != null && inferenceContext.free(tree.constructorType))
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2488
                    || (tree.clazz.type != null && inferenceContext.free(tree.clazz.type)))) {
31113
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2489
                final ResultInfo resultInfoForClassDefinition = this.resultInfo;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2490
                inferenceContext.addFreeTypeListener(List.of(tree.constructorType, tree.clazz.type),
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2491
                        instantiatedContext -> {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2492
                            tree.constructorType = instantiatedContext.asInstType(tree.constructorType);
32912
dd06b5fd9300 8133135: Compiler internall error (NPE) on anonymous class defined by qualified instance creation expression with diamond
sadayapalam
parents: 32709
diff changeset
  2493
                            tree.clazz.type = clazz.type = instantiatedContext.asInstType(clazz.type);
31113
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2494
                            ResultInfo prevResult = this.resultInfo;
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2495
                            try {
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2496
                                this.resultInfo = resultInfoForClassDefinition;
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2497
                                visitAnonymousClassDefinition(tree, clazz, clazz.type, cdef,
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2498
                                                            localEnv, argtypes, typeargtypes, pkind);
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2499
                            } finally {
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2500
                                this.resultInfo = prevResult;
646511cb5873 8081521: Compiler has trouble compiling nested diamond allocation constructs involving anonymous classes.
sadayapalam
parents: 30845
diff changeset
  2501
                            }
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2502
                        });
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2503
            } else {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2504
                if (isDiamond && clazztype.hasTag(CLASS)) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2505
                    List<Type> invalidDiamondArgs = chk.checkDiamondDenotable((ClassType)clazztype);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2506
                    if (!clazztype.isErroneous() && invalidDiamondArgs.nonEmpty()) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2507
                        // One or more types inferred in the previous steps is non-denotable.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2508
                        Fragment fragment = Diamond(clazztype.tsym);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2509
                        log.error(tree.clazz.pos(),
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2510
                                Errors.CantApplyDiamond1(
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2511
                                        fragment,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2512
                                        invalidDiamondArgs.size() > 1 ?
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2513
                                                DiamondInvalidArgs(invalidDiamondArgs, fragment) :
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2514
                                                DiamondInvalidArg(invalidDiamondArgs, fragment)));
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2515
                    }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2516
                    // For <>(){}, inferred types must also be accessible.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2517
                    for (Type t : clazztype.getTypeArguments()) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2518
                        rs.checkAccessibleType(env, t);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2519
                    }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2520
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2521
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2522
                // If we already errored, be careful to avoid a further avalanche. ErrorType answers
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2523
                // false for isInterface call even when the original type is an interface.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2524
                boolean implementing = clazztype.tsym.isInterface() ||
48931
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  2525
                        clazztype.isErroneous() && !clazztype.getOriginalType().hasTag(NONE) &&
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  2526
                        clazztype.getOriginalType().tsym.isInterface();
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2527
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2528
                if (implementing) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2529
                    cdef.implementing = List.of(clazz);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2530
                } else {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2531
                    cdef.extending = clazz;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2532
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2533
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2534
                if (resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK &&
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2535
                    isSerializable(clazztype)) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2536
                    localEnv.info.isSerializable = true;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2537
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2538
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2539
                attribStat(cdef, localEnv);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2540
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2541
                List<Type> finalargtypes;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2542
                // If an outer instance is given,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2543
                // prefix it to the constructor arguments
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2544
                // and delete it from the new expression
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2545
                if (tree.encl != null && !clazztype.tsym.isInterface()) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2546
                    finalargtypes = argtypes.prepend(tree.encl.type);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2547
                } else {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2548
                    finalargtypes = argtypes;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2549
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2550
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2551
                // Reassign clazztype and recompute constructor. As this necessarily involves
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2552
                // another attribution pass for deferred types in the case of <>, replicate
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2553
                // them. Original arguments have right decorations already.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2554
                if (isDiamond && pkind.contains(KindSelector.POLY)) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2555
                    finalargtypes = finalargtypes.map(deferredAttr.deferredCopier);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2556
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2557
48931
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  2558
                clazztype = clazztype.hasTag(ERROR) ? types.createErrorType(cdef.sym.type)
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  2559
                                                    : cdef.sym.type;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2560
                Symbol sym = tree.constructor = rs.resolveConstructor(
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2561
                        tree.pos(), localEnv, clazztype, finalargtypes, typeargtypes);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2562
                Assert.check(!sym.kind.isResolutionError());
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2563
                tree.constructor = sym;
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2564
                tree.constructorType = checkId(tree,
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2565
                        clazztype,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2566
                        tree.constructor,
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2567
                        localEnv,
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2568
                        new ResultInfo(pkind, newMethodTemplate(syms.voidType, finalargtypes, typeargtypes), CheckMode.NO_TREE_UPDATE));
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2569
            }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2570
            Type owntype = (tree.constructor != null && tree.constructor.kind == MTH) ?
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2571
                                clazztype : types.createErrorType(tree.type);
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  2572
            result = check(tree, owntype, KindSelector.VAL, resultInfo.dup(CheckMode.NO_INFERENCE_HOOK));
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2573
            chk.validate(tree.typeargs, localEnv);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2574
        }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
  2575
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2576
        CheckContext diamondContext(JCNewClass clazz, TypeSymbol tsym, CheckContext checkContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2577
            return new Check.NestedCheckContext(checkContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2578
                @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2579
                public void report(DiagnosticPosition _unused, JCDiagnostic details) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2580
                    enclosingContext.report(clazz.clazz,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2581
                            diags.fragment(Fragments.CantApplyDiamond1(Fragments.Diamond(tsym), details)));
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2582
                }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2583
            };
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2584
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2585
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2586
    /** Make an attributed null check tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2587
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2588
    public JCExpression makeNullCheck(JCExpression arg) {
41154
ed8625a2f0f1 8166108: VerifyError passing anonymous inner class to supertype constructor
sadayapalam
parents: 40504
diff changeset
  2589
        // optimization: new Outer() can never be null; skip null check
ed8625a2f0f1 8166108: VerifyError passing anonymous inner class to supertype constructor
sadayapalam
parents: 40504
diff changeset
  2590
        if (arg.getTag() == NEWCLASS)
ed8625a2f0f1 8166108: VerifyError passing anonymous inner class to supertype constructor
sadayapalam
parents: 40504
diff changeset
  2591
            return arg;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
        // optimization: X.this is never null; skip null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
        Name name = TreeInfo.name(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
        if (name == names._this || name == names._super) return arg;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  2596
        JCTree.Tag optag = NULLCHK;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
        JCUnary tree = make.at(arg.pos).Unary(optag, arg);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  2598
        tree.operator = operators.resolveUnary(arg, optag, arg.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
        tree.type = arg.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2602
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
    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
  2604
        Type owntype = types.createErrorType(tree.type);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2605
        Env<AttrContext> localEnv = env.dup(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
        Type elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
        if (tree.elemtype != null) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2608
            elemtype = attribType(tree.elemtype, localEnv);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2609
            chk.validate(tree.elemtype, localEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
            owntype = elemtype;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
            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
  2612
                attribExpr(l.head, localEnv, syms.intType);
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  2613
                owntype = new ArrayType(owntype, syms.arrayClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
            // we are seeing an untyped aggregate { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
            // 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
  2618
            if (pt().hasTag(ARRAY)) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2619
                elemtype = types.elemtype(pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
            } else {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  2621
                if (!pt().hasTag(ERROR) &&
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  2622
                        (env.info.enclVar == null || !env.info.enclVar.type.isErroneous())) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2623
                    log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2624
                              Errors.IllegalInitializerForType(pt()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2625
                }
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  2626
                elemtype = types.createErrorType(pt());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2627
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2629
        if (tree.elems != null) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  2630
            attribExprs(tree.elems, localEnv, elemtype);
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  2631
            owntype = new ArrayType(elemtype, syms.arrayClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
        if (!types.isReifiable(elemtype))
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2634
            log.error(tree.pos(), Errors.GenericArrayCreation);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2635
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2638
    /*
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2639
     * 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
  2640
     * 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
  2641
     * descriptor contains inference variables in argument position the lambda expression
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2642
     * is 'stuck' (see DeferredAttr).
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2643
     */
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 10950
diff changeset
  2644
    @Override
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2645
    public void visitLambda(final JCLambda that) {
51897
eb3e72f181af 8211102: Crash with -XDfind=lambda and -source 7
jlahoda
parents: 51563
diff changeset
  2646
        boolean wrongContext = false;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  2647
        if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  2648
            if (pt().hasTag(NONE) && (env.info.enclVar == null || !env.info.enclVar.type.isErroneous())) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2649
                //lambda only allowed in assignment or method invocation/cast context
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2650
                log.error(that.pos(), Errors.UnexpectedLambda);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2651
            }
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  2652
            resultInfo = recoveryInfo;
51897
eb3e72f181af 8211102: Crash with -XDfind=lambda and -source 7
jlahoda
parents: 51563
diff changeset
  2653
            wrongContext = true;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2654
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2655
        //create an environment for attribution of the lambda expression
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2656
        final Env<AttrContext> localEnv = lambdaEnv(that, env);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2657
        boolean needsRecovery =
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2658
                resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2659
        try {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2660
            if (needsRecovery && isSerializable(pt())) {
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  2661
                localEnv.info.isSerializable = true;
41629
9d203cde7d84 8026721: Enhance Lambda serialization
vromero
parents: 41444
diff changeset
  2662
                localEnv.info.isLambda = true;
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  2663
            }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2664
            List<Type> explicitParamTypes = null;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2665
            if (that.paramKind == JCLambda.ParameterKind.EXPLICIT) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2666
                //attribute lambda parameters
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2667
                attribStats(that.params, localEnv);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2668
                explicitParamTypes = TreeInfo.types(that.params);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2669
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2670
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2671
            TargetInfo targetInfo = getTargetInfo(that, resultInfo, explicitParamTypes);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2672
            Type currentTarget = targetInfo.target;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2673
            Type lambdaType = targetInfo.descriptor;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2674
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2675
            if (currentTarget.isErroneous()) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2676
                result = that.type = currentTarget;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2677
                return;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2678
            }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2679
19916
5b5f188dbdd4 8023389: Javac fails to infer type for lambda used with intersection type and wildcards
vromero
parents: 19914
diff changeset
  2680
            setFunctionalInfo(localEnv, that, pt(), lambdaType, currentTarget, resultInfo.checkContext);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2681
14723
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2682
            if (lambdaType.hasTag(FORALL)) {
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2683
                //lambda expression target desc cannot be a generic method
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2684
                Fragment msg = Fragments.InvalidGenericLambdaTarget(lambdaType,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2685
                                                                    kindName(currentTarget.tsym),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2686
                                                                    currentTarget.tsym);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2687
                resultInfo.checkContext.report(that, diags.fragment(msg));
14723
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2688
                result = that.type = types.createErrorType(pt());
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2689
                return;
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2690
            }
46aa71a5e4e0 8004102: Add support for generic functional descriptors
mcimadamore
parents: 14722
diff changeset
  2691
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2692
            if (that.paramKind == JCLambda.ParameterKind.IMPLICIT) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2693
                //add param type info in the AST
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2694
                List<Type> actuals = lambdaType.getParameterTypes();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2695
                List<JCVariableDecl> params = that.params;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2696
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2697
                boolean arityMismatch = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2698
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2699
                while (params.nonEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2700
                    if (actuals.isEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2701
                        //not enough actuals to perform lambda parameter inference
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2702
                        arityMismatch = true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2703
                    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2704
                    //reset previously set info
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2705
                    Type argType = arityMismatch ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2706
                            syms.errType :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2707
                            actuals.head;
50518
e775444c555e 8204674: Inconsistent lambda parameter span
jlahoda
parents: 50468
diff changeset
  2708
                    if (params.head.isImplicitlyTyped()) {
e775444c555e 8204674: Inconsistent lambda parameter span
jlahoda
parents: 50468
diff changeset
  2709
                        setSyntheticVariableType(params.head, argType);
e775444c555e 8204674: Inconsistent lambda parameter span
jlahoda
parents: 50468
diff changeset
  2710
                    }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2711
                    params.head.sym = null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2712
                    actuals = actuals.isEmpty() ?
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2713
                            actuals :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2714
                            actuals.tail;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2715
                    params = params.tail;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2716
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2717
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2718
                //attribute lambda parameters
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2719
                attribStats(that.params, localEnv);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2720
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2721
                if (arityMismatch) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2722
                    resultInfo.checkContext.report(that, diags.fragment(Fragments.IncompatibleArgTypesInLambda));
19916
5b5f188dbdd4 8023389: Javac fails to infer type for lambda used with intersection type and wildcards
vromero
parents: 19914
diff changeset
  2723
                        result = that.type = types.createErrorType(currentTarget);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2724
                        return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2725
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2726
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2727
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2728
            //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
  2729
            //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
  2730
            //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
  2731
            //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
  2732
            needsRecovery = false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2733
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2734
            ResultInfo bodyResultInfo = localEnv.info.returnResult =
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2735
                    lambdaBodyResult(that, lambdaType, resultInfo);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2736
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2737
            if (that.getBodyKind() == JCLambda.BodyKind.EXPRESSION) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2738
                attribTree(that.getBody(), localEnv, bodyResultInfo);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2739
            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2740
                JCBlock body = (JCBlock)that.body;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  2741
                if (body == breakTree &&
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  2742
                        resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  2743
                    breakTreeFound(copyEnv(localEnv));
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  2744
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  2745
                attribStats(body.stats, localEnv);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2746
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2747
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  2748
            result = check(that, currentTarget, KindSelector.VAL, resultInfo);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2749
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2750
            boolean isSpeculativeRound =
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2751
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2752
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2753
            preFlow(that);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2754
            flow.analyzeLambda(env, that, make, isSpeculativeRound);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2755
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  2756
            that.type = currentTarget; //avoids recovery at this stage
19933
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2757
            checkLambdaCompatible(that, lambdaType, resultInfo.checkContext);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2758
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2759
            if (!isSpeculativeRound) {
19933
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2760
                //add thrown types as bounds to the thrown types free variables if needed:
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2761
                if (resultInfo.checkContext.inferenceContext().free(lambdaType.getThrownTypes())) {
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2762
                    List<Type> inferredThrownTypes = flow.analyzeLambdaThrownTypes(env, that, make);
45756
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  2763
                    if(!checkExConstraints(inferredThrownTypes, lambdaType.getThrownTypes(), resultInfo.checkContext.inferenceContext())) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  2764
                        log.error(that, Errors.IncompatibleThrownTypesInMref(lambdaType.getThrownTypes()));
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  2765
                    }
19933
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2766
                }
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  2767
19916
5b5f188dbdd4 8023389: Javac fails to infer type for lambda used with intersection type and wildcards
vromero
parents: 19914
diff changeset
  2768
                checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), lambdaType, currentTarget);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2769
            }
51897
eb3e72f181af 8211102: Crash with -XDfind=lambda and -source 7
jlahoda
parents: 51563
diff changeset
  2770
            result = wrongContext ? that.type = types.createErrorType(pt())
eb3e72f181af 8211102: Crash with -XDfind=lambda and -source 7
jlahoda
parents: 51563
diff changeset
  2771
                                  : check(that, currentTarget, KindSelector.VAL, resultInfo);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2772
        } catch (Types.FunctionDescriptorLookupError ex) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2773
            JCDiagnostic cause = ex.getDiagnostic();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2774
            resultInfo.checkContext.report(that, cause);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2775
            result = that.type = types.createErrorType(pt());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2776
            return;
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
  2777
        } catch (CompletionFailure cf) {
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
  2778
            chk.completionError(that.pos(), cf);
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29959
diff changeset
  2779
        } catch (Throwable t) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29959
diff changeset
  2780
            //when an unexpected exception happens, avoid attempts to attribute the same tree again
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29959
diff changeset
  2781
            //as that would likely cause the same exception again.
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29959
diff changeset
  2782
            needsRecovery = false;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29959
diff changeset
  2783
            throw t;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2784
        } finally {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2785
            localEnv.info.scope.leave();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2786
            if (needsRecovery) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2787
                attribTree(that, env, recoveryInfo);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2788
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2789
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 10950
diff changeset
  2790
    }
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2791
    //where
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2792
        class TargetInfo {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2793
            Type target;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2794
            Type descriptor;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2795
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2796
            public TargetInfo(Type target, Type descriptor) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2797
                this.target = target;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2798
                this.descriptor = descriptor;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2799
            }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2800
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2801
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2802
        TargetInfo getTargetInfo(JCPolyExpression that, ResultInfo resultInfo, List<Type> explicitParamTypes) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2803
            Type lambdaType;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2804
            Type currentTarget = resultInfo.pt;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2805
            if (resultInfo.pt != Type.recoveryType) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2806
                /* We need to adjust the target. If the target is an
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2807
                 * intersection type, for example: SAM & I1 & I2 ...
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2808
                 * the target will be updated to SAM
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2809
                 */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2810
                currentTarget = targetChecker.visit(currentTarget, that);
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2811
                if (!currentTarget.isIntersection()) {
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2812
                    if (explicitParamTypes != null) {
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2813
                        currentTarget = infer.instantiateFunctionalInterface(that,
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2814
                                currentTarget, explicitParamTypes, resultInfo.checkContext);
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2815
                    }
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2816
                    currentTarget = types.removeWildcards(currentTarget);
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2817
                    lambdaType = types.findDescriptorType(currentTarget);
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2818
                } else {
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2819
                    IntersectionClassType ict = (IntersectionClassType)currentTarget;
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2820
                    ListBuffer<Type> components = new ListBuffer<>();
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2821
                    for (Type bound : ict.getExplicitComponents()) {
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2822
                        if (explicitParamTypes != null) {
50200
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2823
                            try {
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2824
                                bound = infer.instantiateFunctionalInterface(that,
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2825
                                        bound, explicitParamTypes, resultInfo.checkContext);
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2826
                            } catch (FunctionDescriptorLookupError t) {
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2827
                                // do nothing
9ce050c4711b 8203486: skip type inference for non functional interface components of intersection types
vromero
parents: 50181
diff changeset
  2828
                            }
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2829
                        }
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2830
                        bound = types.removeWildcards(bound);
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2831
                        components.add(bound);
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2832
                    }
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2833
                    currentTarget = types.makeIntersectionType(components.toList());
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2834
                    currentTarget.tsym.flags_field |= INTERFACE;
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2835
                    lambdaType = types.findDescriptorType(currentTarget);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2836
                }
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2837
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2838
            } else {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2839
                currentTarget = Type.recoveryType;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2840
                lambdaType = fallbackDescriptorType(that);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2841
            }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2842
            if (that.hasTag(LAMBDA) && lambdaType.hasTag(FORALL)) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2843
                //lambda expression target desc cannot be a generic method
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2844
                Fragment msg = Fragments.InvalidGenericLambdaTarget(lambdaType,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2845
                                                                    kindName(currentTarget.tsym),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2846
                                                                    currentTarget.tsym);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2847
                resultInfo.checkContext.report(that, diags.fragment(msg));
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2848
                currentTarget = types.createErrorType(pt());
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2849
            }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2850
            return new TargetInfo(currentTarget, lambdaType);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2851
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2852
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2853
        void preFlow(JCLambda tree) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2854
            new PostAttrAnalyzer() {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2855
                @Override
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2856
                public void scan(JCTree tree) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2857
                    if (tree == null ||
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2858
                            (tree.type != null &&
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2859
                            tree.type == Type.stuckType)) {
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2860
                        //don't touch stuck expressions!
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2861
                        return;
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2862
                    }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2863
                    super.scan(tree);
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2864
                }
52697
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2865
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2866
                @Override
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2867
                public void visitClassDef(JCClassDecl that) {
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2868
                    // or class declaration trees!
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2869
                }
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2870
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2871
                public void visitLambda(JCLambda that) {
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2872
                    // or lambda expressions!
605878cd4009 8203277: preflow visitor used during lambda attribution shouldn't visit class definitions inside the lambda body
vromero
parents: 52679
diff changeset
  2873
                }
54788
7f782ee493f7 8222251: preflow visitor is not visiting lambda expressions
vromero
parents: 53515
diff changeset
  2874
            }.scan(tree.body);
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2875
        }
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18911
diff changeset
  2876
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2877
        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
  2878
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2879
            @Override
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2880
            public Type visitClassType(ClassType t, DiagnosticPosition pos) {
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29051
diff changeset
  2881
                return t.isIntersection() ?
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2882
                        visitIntersectionClassType((IntersectionClassType)t, pos) : t;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2883
            }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2884
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2885
            public Type visitIntersectionClassType(IntersectionClassType ict, DiagnosticPosition pos) {
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2886
                types.findDescriptorSymbol(makeNotionalInterface(ict, pos));
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2887
                return ict;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  2888
            }
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2889
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  2890
            private TypeSymbol makeNotionalInterface(IntersectionClassType ict, DiagnosticPosition pos) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  2891
                ListBuffer<Type> targs = new ListBuffer<>();
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  2892
                ListBuffer<Type> supertypes = new ListBuffer<>();
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2893
                for (Type i : ict.interfaces_field) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2894
                    if (i.isParameterized()) {
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2895
                        targs.appendList(i.tsym.type.allparams());
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2896
                    }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2897
                    supertypes.append(i.tsym.type);
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2898
                }
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29051
diff changeset
  2899
                IntersectionClassType notionalIntf = types.makeIntersectionType(supertypes.toList());
16809
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2900
                notionalIntf.allparams_field = targs.toList();
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2901
                notionalIntf.tsym.flags_field |= INTERFACE;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2902
                return notionalIntf.tsym;
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2903
            }
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2904
        };
5acfcb821d65 8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents: 16808
diff changeset
  2905
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2906
        private Type fallbackDescriptorType(JCExpression tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2907
            switch (tree.getTag()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2908
                case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2909
                    JCLambda lambda = (JCLambda)tree;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2910
                    List<Type> argtypes = List.nil();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2911
                    for (JCVariableDecl param : lambda.params) {
51103
cecc2e10edf4 8207229: Trees.getScope crashes for broken lambda
jlahoda
parents: 51047
diff changeset
  2912
                        argtypes = param.vartype != null && param.vartype.type != null ?
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2913
                                argtypes.append(param.vartype.type) :
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2914
                                argtypes.append(syms.errType);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2915
                    }
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2916
                    return new MethodType(argtypes, Type.recoveryType,
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2917
                            List.of(syms.throwableType), syms.methodClass);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2918
                case REFERENCE:
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2919
                    return new MethodType(List.nil(), Type.recoveryType,
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2920
                            List.of(syms.throwableType), syms.methodClass);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2921
                default:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2922
                    Assert.error("Cannot get here!");
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2923
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2924
            return null;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2925
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2926
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2927
        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
  2928
                final InferenceContext inferenceContext, final Type... ts) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2929
            checkAccessibleTypes(pos, env, inferenceContext, List.from(ts));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2930
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2931
16972
b5e557f4d536 8011376: Spurious checked exception errors in nested method call
mcimadamore
parents: 16809
diff changeset
  2932
        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
  2933
                final InferenceContext inferenceContext, final List<Type> ts) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2934
            if (inferenceContext.free(ts)) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
  2935
                inferenceContext.addFreeTypeListener(ts,
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
  2936
                        solvedContext -> checkAccessibleTypes(pos, env, solvedContext, solvedContext.asInstTypes(ts)));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2937
            } else {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2938
                for (Type t : ts) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2939
                    rs.checkAccessibleType(env, t);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  2940
                }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2941
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2942
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2943
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2944
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2945
         * Lambda/method reference have a special check context that ensures
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2946
         * 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
  2947
         * type according to both the inherited context and the assignment
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2948
         * context.
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2949
         */
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2950
        class FunctionalReturnContext extends Check.NestedCheckContext {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2951
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2952
            FunctionalReturnContext(CheckContext enclosingContext) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2953
                super(enclosingContext);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2954
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2955
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2956
            @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2957
            public boolean compatible(Type found, Type req, Warner warn) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2958
                //return type must be compatible in both current context and assignment context
38538
8bdc63ff6961 8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents: 38508
diff changeset
  2959
                return chk.basicHandler.compatible(inferenceContext().asUndetVar(found), inferenceContext().asUndetVar(req), warn);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2960
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  2961
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2962
            @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2963
            public void report(DiagnosticPosition pos, JCDiagnostic details) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  2964
                enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleRetTypeInLambda(details)));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2965
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2966
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  2967
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2968
        class ExpressionLambdaReturnContext extends FunctionalReturnContext {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2969
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2970
            JCExpression expr;
38508
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2971
            boolean expStmtExpected;
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2972
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2973
            ExpressionLambdaReturnContext(JCExpression expr, CheckContext enclosingContext) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2974
                super(enclosingContext);
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2975
                this.expr = expr;
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2976
            }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2977
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2978
            @Override
38508
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2979
            public void report(DiagnosticPosition pos, JCDiagnostic details) {
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2980
                if (expStmtExpected) {
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2981
                    enclosingContext.report(pos, diags.fragment(Fragments.StatExprExpected));
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2982
                } else {
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2983
                    super.report(pos, details);
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2984
                }
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2985
            }
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2986
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2987
            @Override
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2988
            public boolean compatible(Type found, Type req, Warner warn) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2989
                //a void return is compatible with an expression statement lambda
38508
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2990
                if (req.hasTag(VOID)) {
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2991
                    expStmtExpected = true;
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2992
                    return TreeInfo.isExpressionStatement(expr);
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2993
                } else {
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2994
                    return super.compatible(found, req, warn);
03c87ceca5fd 8153884: Expression lambda erroneously compatible with void-returning descriptor
mcimadamore
parents: 37634
diff changeset
  2995
                }
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2996
            }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2997
        }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  2998
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  2999
        ResultInfo lambdaBodyResult(JCLambda that, Type descriptor, ResultInfo resultInfo) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3000
            FunctionalReturnContext funcContext = that.getBodyKind() == JCLambda.BodyKind.EXPRESSION ?
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3001
                    new ExpressionLambdaReturnContext((JCExpression)that.getBody(), resultInfo.checkContext) :
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3002
                    new FunctionalReturnContext(resultInfo.checkContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3003
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3004
            return descriptor.getReturnType() == Type.recoveryType ?
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3005
                    recoveryInfo :
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3006
                    new ResultInfo(KindSelector.VAL,
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3007
                            descriptor.getReturnType(), funcContext);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3008
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3009
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3010
        /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3011
        * Lambda compatibility. Check that given return types, thrown types, parameter types
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3012
        * are compatible with the expected functional interface descriptor. This means that:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3013
        * (i) parameter types must be identical to those of the target descriptor; (ii) return
19933
5182bc65845b 8019521: Enhanced rethrow disabled in lambdas
jlahoda
parents: 19918
diff changeset
  3014
        * types must be compatible with the return type of the expected descriptor.
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3015
        */
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3016
        void checkLambdaCompatible(JCLambda tree, Type descriptor, CheckContext checkContext) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23129
diff changeset
  3017
            Type returnType = checkContext.inferenceContext().asUndetVar(descriptor.getReturnType());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3018
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3019
            //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
  3020
            //values, we must ensure that void/value compatibility is correct;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3021
            //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
  3022
            //the descriptor's return type must be void
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3023
            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
  3024
                    !returnType.hasTag(VOID) && returnType != Type.recoveryType) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3025
                Fragment msg =
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3026
                        Fragments.IncompatibleRetTypeInLambda(Fragments.MissingRetVal(returnType));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3027
                checkContext.report(tree,
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3028
                                    diags.fragment(msg));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3029
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3030
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23129
diff changeset
  3031
            List<Type> argTypes = checkContext.inferenceContext().asUndetVars(descriptor.getParameterTypes());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3032
            if (!types.isSameTypes(argTypes, TreeInfo.types(tree.params))) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3033
                checkContext.report(tree, diags.fragment(Fragments.IncompatibleArgTypesInLambda));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3034
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3035
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3036
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3037
        /* Map to hold 'fake' clinit methods. If a lambda is used to initialize a
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3038
         * static field and that lambda has type annotations, these annotations will
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3039
         * also be stored at these fake clinit methods.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3040
         *
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3041
         * LambdaToMethod also use fake clinit methods so they can be reused.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3042
         * Also as LTM is a phase subsequent to attribution, the methods from
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3043
         * clinits can be safely removed by LTM to save memory.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3044
         */
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3045
        private Map<ClassSymbol, MethodSymbol> clinits = new HashMap<>();
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3046
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3047
        public MethodSymbol removeClinit(ClassSymbol sym) {
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3048
            return clinits.remove(sym);
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3049
        }
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3050
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3051
        /* This method returns an environment to be used to attribute a lambda
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3052
         * expression.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3053
         *
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3054
         * The owner of this environment is a method symbol. If the current owner
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3055
         * is not a method, for example if the lambda is used to initialize
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3056
         * a field, then if the field is:
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3057
         *
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3058
         * - an instance field, we use the first constructor.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3059
         * - a static field, we create a fake clinit method.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3060
         */
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 23969
diff changeset
  3061
        public Env<AttrContext> lambdaEnv(JCLambda that, Env<AttrContext> env) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3062
            Env<AttrContext> lambdaEnv;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3063
            Symbol owner = env.info.scope.owner;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3064
            if (owner.kind == VAR && owner.owner.kind == TYP) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3065
                //field initializer
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3066
                ClassSymbol enclClass = owner.enclClass();
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  3067
                Symbol newScopeOwner = env.info.scope.owner;
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3068
                /* if the field isn't static, then we can get the first constructor
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3069
                 * and use it as the owner of the environment. This is what
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3070
                 * LTM code is doing to look for type annotations so we are fine.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3071
                 */
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3072
                if ((owner.flags() & STATIC) == 0) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  3073
                    for (Symbol s : enclClass.members_field.getSymbolsByName(names.init)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  3074
                        newScopeOwner = s;
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3075
                        break;
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3076
                    }
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3077
                } else {
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3078
                    /* if the field is static then we need to create a fake clinit
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3079
                     * method, this method can later be reused by LTM.
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3080
                     */
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3081
                    MethodSymbol clinit = clinits.get(enclClass);
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3082
                    if (clinit == null) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3083
                        Type clinitType = new MethodType(List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3084
                                syms.voidType, List.nil(), syms.methodClass);
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3085
                        clinit = new MethodSymbol(STATIC | SYNTHETIC | PRIVATE,
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3086
                                names.clinit, clinitType, enclClass);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3087
                        clinit.params = List.nil();
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3088
                        clinits.put(enclClass, clinit);
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3089
                    }
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  3090
                    newScopeOwner = clinit;
22162
3b3e23e67329 8029721: javac crash for annotated parameter type of lambda in a field
vromero
parents: 22003
diff changeset
  3091
                }
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  3092
                lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dupUnshared(newScopeOwner)));
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3093
            } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3094
                lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dup()));
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3095
            }
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  3096
            lambdaEnv.info.yieldResult = null;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3097
            return lambdaEnv;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  3098
        }
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3099
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3100
    @Override
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3101
    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
  3102
        if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  3103
            if (pt().hasTag(NONE) && (env.info.enclVar == null || !env.info.enclVar.type.isErroneous())) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3104
                //method reference only allowed in assignment or method invocation/cast context
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3105
                log.error(that.pos(), Errors.UnexpectedMref);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3106
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3107
            result = that.type = types.createErrorType(pt());
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3108
            return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3109
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3110
        final Env<AttrContext> localEnv = env.dup(that);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3111
        try {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3112
            //attribute member reference qualifier - if this is a constructor
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3113
            //reference, the expected kind must be a type
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  3114
            Type exprType = attribTree(that.expr, env, memberReferenceQualifierResult(that));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3115
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3116
            if (that.getMode() == JCMemberReference.ReferenceMode.NEW) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3117
                exprType = chk.checkConstructorRefType(that.expr, exprType);
19918
3bdf0c6b869c 8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
vromero
parents: 19916
diff changeset
  3118
                if (!exprType.isErroneous() &&
3bdf0c6b869c 8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
vromero
parents: 19916
diff changeset
  3119
                    exprType.isRaw() &&
3bdf0c6b869c 8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
vromero
parents: 19916
diff changeset
  3120
                    that.typeargs != null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3121
                    log.error(that.expr.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3122
                              Errors.InvalidMref(Kinds.kindName(that.getMode()),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3123
                                                 Fragments.MrefInferAndExplicitParams));
19918
3bdf0c6b869c 8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
vromero
parents: 19916
diff changeset
  3124
                    exprType = types.createErrorType(exprType);
3bdf0c6b869c 8023549: Compiler emitting spurious errors when constructor reference type is inferred and explicit type arguments are supplied
vromero
parents: 19916
diff changeset
  3125
                }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3126
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3127
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3128
            if (exprType.isErroneous()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3129
                //if the qualifier expression contains problems,
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15717
diff changeset
  3130
                //give up attribution of method reference
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3131
                result = that.type = exprType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3132
                return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3133
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3134
17583
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3135
            if (TreeInfo.isStaticSelector(that.expr, names)) {
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3136
                //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
  3137
                //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
  3138
                //raw selector (because of inference)
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3139
                chk.validate(that.expr, env, false);
33363
f03a03067799 8139836: Can't use super::x method reference when x is protected
sadayapalam
parents: 32950
diff changeset
  3140
            } else {
f03a03067799 8139836: Can't use super::x method reference when x is protected
sadayapalam
parents: 32950
diff changeset
  3141
                Symbol lhsSym = TreeInfo.symbol(that.expr);
f03a03067799 8139836: Can't use super::x method reference when x is protected
sadayapalam
parents: 32950
diff changeset
  3142
                localEnv.info.selectSuper = lhsSym != null && lhsSym.name == names._super;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3143
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3144
            //attrib type-arguments
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3145
            List<Type> typeargtypes = List.nil();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3146
            if (that.typeargs != null) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3147
                typeargtypes = attribTypes(that.typeargs, localEnv);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3148
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3149
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3150
            boolean isTargetSerializable =
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3151
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK &&
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3152
                    isSerializable(pt());
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3153
            TargetInfo targetInfo = getTargetInfo(that, resultInfo, null);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3154
            Type currentTarget = targetInfo.target;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3155
            Type desc = targetInfo.descriptor;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3156
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3157
            setFunctionalInfo(localEnv, that, pt(), desc, currentTarget, resultInfo.checkContext);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3158
            List<Type> argtypes = desc.getParameterTypes();
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3159
            Resolve.MethodCheck referenceCheck = rs.resolveMethodCheck;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3160
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3161
            if (resultInfo.checkContext.inferenceContext().free(argtypes)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3162
                referenceCheck = rs.new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3163
            }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3164
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3165
            Pair<Symbol, Resolve.ReferenceLookupHelper> refResult = null;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3166
            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
  3167
            try {
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 21716
diff changeset
  3168
                refResult = rs.resolveMemberReference(localEnv, that, that.expr.type,
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 50200
diff changeset
  3169
                        that.name, argtypes, typeargtypes, targetInfo.descriptor, referenceCheck,
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28336
diff changeset
  3170
                        resultInfo.checkContext.inferenceContext(), rs.basicReferenceChooser);
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3171
            } finally {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3172
                resultInfo.checkContext.inferenceContext().rollback(saved_undet);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3173
            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3174
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3175
            Symbol refSym = refResult.fst;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3176
            Resolve.ReferenceLookupHelper lookupHelper = refResult.snd;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3177
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3178
            /** this switch will need to go away and be replaced by the new RESOLUTION_TARGET testing
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3179
             *  JDK-8075541
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3180
             */
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3181
            if (refSym.kind != MTH) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3182
                boolean targetError;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3183
                switch (refSym.kind) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3184
                    case ABSENT_MTH:
29774
9d438163db79 8075184: javac is mistakenly considering an missing enclosing instance error as an overload error
vromero
parents: 29554
diff changeset
  3185
                    case MISSING_ENCL:
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3186
                        targetError = false;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3187
                        break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3188
                    case WRONG_MTH:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3189
                    case WRONG_MTHS:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3190
                    case AMBIGUOUS:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3191
                    case HIDDEN:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3192
                    case STATICERR:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3193
                        targetError = true;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3194
                        break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3195
                    default:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3196
                        Assert.error("unexpected result kind " + refSym.kind);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3197
                        targetError = false;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3198
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3199
50086
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3200
                JCDiagnostic detailsDiag = ((Resolve.ResolveError)refSym.baseSymbol())
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3201
                        .getDiagnostic(JCDiagnostic.DiagnosticType.FRAGMENT,
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3202
                                that, exprType.tsym, exprType, that.name, argtypes, typeargtypes);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3203
50086
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3204
                JCDiagnostic diag = diags.create(log.currentSource(), that,
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3205
                        targetError ?
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3206
                            Fragments.InvalidMref(Kinds.kindName(that.getMode()), detailsDiag) :
66b0f0134aad 8194998: broken error message for subclass of interface with private method
vromero
parents: 50061
diff changeset
  3207
                            Errors.InvalidMref(Kinds.kindName(that.getMode()), detailsDiag));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3208
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3209
                if (targetError && currentTarget == Type.recoveryType) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3210
                    //a target error doesn't make sense during recovery stage
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3211
                    //as we don't know what actual parameter types are
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3212
                    result = that.type = currentTarget;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3213
                    return;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3214
                } else {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3215
                    if (targetError) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3216
                        resultInfo.checkContext.report(that, diag);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3217
                    } else {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3218
                        log.report(diag);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3219
                    }
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3220
                    result = that.type = types.createErrorType(currentTarget);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3221
                    return;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3222
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3223
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3224
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 50200
diff changeset
  3225
            that.sym = refSym.isConstructor() ? refSym.baseSymbol() : refSym;
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  3226
            that.kind = lookupHelper.referenceKind(that.sym);
16328
8b9eb42167f6 8009129: Illegal access error when calling method reference
mcimadamore
parents: 16323
diff changeset
  3227
            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
  3228
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  3229
            if (desc.getReturnType() == Type.recoveryType) {
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  3230
                // stop here
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3231
                result = that.type = currentTarget;
16323
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  3232
                return;
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  3233
            }
c4409c235642 8008537: Missing method reference lookup error when unbound search finds a static method
mcimadamore
parents: 16320
diff changeset
  3234
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55528
diff changeset
  3235
            if (!env.info.attributionMode.isSpeculative && that.getMode() == JCMemberReference.ReferenceMode.NEW) {
43582
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3236
                Type enclosingType = exprType.getEnclosingType();
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3237
                if (enclosingType != null && enclosingType.hasTag(CLASS)) {
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3238
                    // Check for the existence of an apropriate outer instance
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3239
                    rs.resolveImplicitThis(that.pos(), env, exprType);
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3240
                }
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3241
            }
a6c58d4cb05b 8173456: com.sun.tools.javac.util.Assert.error during code compilation
sadayapalam
parents: 43138
diff changeset
  3242
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3243
            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
  3244
17583
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3245
                if (that.getMode() == ReferenceMode.INVOKE &&
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3246
                        TreeInfo.isStaticSelector(that.expr, names) &&
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3247
                        that.kind.isUnbound() &&
47315
31f541df4187 8063054: Incorrect raw type warning for method reference
mcimadamore
parents: 47268
diff changeset
  3248
                        lookupHelper.site.isRaw()) {
17583
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3249
                    chk.checkRaw(that.expr, localEnv);
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3250
                }
91949b5f7651 8012685: Spurious raw types warning when using unbound method references
mcimadamore
parents: 17582
diff changeset
  3251
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  3252
                if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) &&
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3253
                        exprType.getTypeArguments().nonEmpty()) {
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3254
                    //static ref with class type-args
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3255
                    log.error(that.expr.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3256
                              Errors.InvalidMref(Kinds.kindName(that.getMode()),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3257
                                                 Fragments.StaticMrefWithTargs));
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3258
                    result = that.type = types.createErrorType(currentTarget);
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3259
                    return;
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3260
                }
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3261
16293
bf5e87940aee 8007285: AbstractMethodError instead of compile-time error when method reference with super and abstract
mcimadamore
parents: 15718
diff changeset
  3262
                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
  3263
                    // 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
  3264
                    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
  3265
                }
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3266
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3267
                if (isTargetSerializable) {
41629
9d203cde7d84 8026721: Enhance Lambda serialization
vromero
parents: 41444
diff changeset
  3268
                    chk.checkAccessFromSerializableElement(that, true);
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3269
                }
14724
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3270
            }
b542db73539a 8004101: Add checks for method reference well-formedness
mcimadamore
parents: 14723
diff changeset
  3271
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3272
            ResultInfo checkInfo =
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3273
                    resultInfo.dup(newMethodTemplate(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3274
                        desc.getReturnType().hasTag(VOID) ? Type.noType : desc.getReturnType(),
24401
0d793293862a 8028503: javac, for method references a primitive type can be added as a bound
vromero
parents: 24400
diff changeset
  3275
                        that.kind.isUnbound() ? argtypes.tail : argtypes, typeargtypes),
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  3276
                        new FunctionalReturnContext(resultInfo.checkContext), CheckMode.NO_TREE_UPDATE);
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  3277
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  3278
            Type refType = checkId(that, lookupHelper.site, refSym, localEnv, checkInfo);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3279
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3280
            if (that.kind.isUnbound() &&
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3281
                    resultInfo.checkContext.inferenceContext().free(argtypes.head)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3282
                //re-generate inference constraints for unbound receiver
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23129
diff changeset
  3283
                if (!types.isSubtype(resultInfo.checkContext.inferenceContext().asUndetVar(argtypes.head), exprType)) {
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3284
                    //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
  3285
                    //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
  3286
                    //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
  3287
                    Assert.error("Can't get here");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3288
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3289
            }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18903
diff changeset
  3290
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3291
            if (!refType.isErroneous()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3292
                refType = types.createMethodTypeWithReturn(refType,
39810
d290bef927c4 8161985: Spurious override of Object.getClass leads to NPE
mcimadamore
parents: 38829
diff changeset
  3293
                        adjustMethodReturnType(refSym, lookupHelper.site, that.name, checkInfo.pt.getParameterTypes(), refType.getReturnType()));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3294
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3295
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3296
            //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
  3297
            //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
  3298
            boolean isSpeculativeRound =
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3299
                    resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
28148
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  3300
6415a95b07fd 8066974: Compiler doesn't infer method's generic type information in lambda body
mcimadamore
parents: 28142
diff changeset
  3301
            that.type = currentTarget; //avoids recovery at this stage
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3302
            checkReferenceCompatible(that, desc, refType, resultInfo.checkContext, isSpeculativeRound);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3303
            if (!isSpeculativeRound) {
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3304
                checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), desc, currentTarget);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3305
            }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3306
            result = check(that, currentTarget, KindSelector.VAL, resultInfo);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3307
        } catch (Types.FunctionDescriptorLookupError ex) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3308
            JCDiagnostic cause = ex.getDiagnostic();
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3309
            resultInfo.checkContext.report(that, cause);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3310
            result = that.type = types.createErrorType(pt());
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3311
            return;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3312
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3313
    }
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  3314
    //where
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  3315
        ResultInfo memberReferenceQualifierResult(JCMemberReference tree) {
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  3316
            //if this is a constructor reference, the expected kind must be a type
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3317
            return new ResultInfo(tree.getMode() == ReferenceMode.INVOKE ?
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3318
                                  KindSelector.VAL_TYP : KindSelector.TYP,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3319
                                  Type.noType);
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  3320
        }
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 16293
diff changeset
  3321
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3322
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3323
    @SuppressWarnings("fallthrough")
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3324
    void checkReferenceCompatible(JCMemberReference tree, Type descriptor, Type refType, CheckContext checkContext, boolean speculativeAttr) {
28591
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3325
        InferenceContext inferenceContext = checkContext.inferenceContext();
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3326
        Type returnType = inferenceContext.asUndetVar(descriptor.getReturnType());
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3327
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3328
        Type resType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3329
        switch (tree.getMode()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3330
            case NEW:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3331
                if (!tree.expr.type.isRaw()) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3332
                    resType = tree.expr.type;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3333
                    break;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3334
                }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3335
            default:
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3336
                resType = refType.getReturnType();
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3337
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3338
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3339
        Type incompatibleReturnType = resType;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3340
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3341
        if (returnType.hasTag(VOID)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3342
            incompatibleReturnType = null;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3343
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3344
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3345
        if (!returnType.hasTag(VOID) && !resType.hasTag(VOID)) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3346
            if (resType.isErroneous() ||
44184
606c14ee20ed 8175317: javac does not issue unchecked warnings when checking method reference return types
mcimadamore
parents: 44060
diff changeset
  3347
                    new FunctionalReturnContext(checkContext).compatible(resType, returnType,
606c14ee20ed 8175317: javac does not issue unchecked warnings when checking method reference return types
mcimadamore
parents: 44060
diff changeset
  3348
                            checkContext.checkWarner(tree, resType, returnType))) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3349
                incompatibleReturnType = null;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3350
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3351
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3352
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3353
        if (incompatibleReturnType != null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3354
            Fragment msg =
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3355
                    Fragments.IncompatibleRetTypeInMref(Fragments.InconvertibleTypes(resType, descriptor.getReturnType()));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3356
            checkContext.report(tree, diags.fragment(msg));
28591
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3357
        } else {
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3358
            if (inferenceContext.free(refType)) {
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3359
                // we need to wait for inference to finish and then replace inference vars in the referent type
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3360
                inferenceContext.addFreeTypeListener(List.of(refType),
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3361
                        instantiatedContext -> {
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3362
                            tree.referentType = instantiatedContext.asInstType(refType);
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3363
                        });
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3364
            } else {
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3365
                tree.referentType = refType;
25f1384324ae 8046977: ClassCastException: typing information needed for method reference bridging not preserved
jfranck
parents: 28459
diff changeset
  3366
            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3367
        }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3368
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3369
        if (!speculativeAttr) {
45756
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3370
            if (!checkExConstraints(refType.getThrownTypes(), descriptor.getThrownTypes(), inferenceContext)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3371
                log.error(tree, Errors.IncompatibleThrownTypesInMref(refType.getThrownTypes()));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3372
            }
45756
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3373
        }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3374
    }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3375
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3376
    boolean checkExConstraints(
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3377
            List<Type> thrownByFuncExpr,
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3378
            List<Type> thrownAtFuncType,
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3379
            InferenceContext inferenceContext) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3380
        /** 18.2.5: Otherwise, let E1, ..., En be the types in the function type's throws clause that
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3381
         *  are not proper types
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3382
         */
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3383
        List<Type> nonProperList = thrownAtFuncType.stream()
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3384
                .filter(e -> inferenceContext.free(e)).collect(List.collector());
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3385
        List<Type> properList = thrownAtFuncType.diff(nonProperList);
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3386
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3387
        /** Let X1,...,Xm be the checked exception types that the lambda body can throw or
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3388
         *  in the throws clause of the invocation type of the method reference's compile-time
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3389
         *  declaration
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3390
         */
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3391
        List<Type> checkedList = thrownByFuncExpr.stream()
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3392
                .filter(e -> chk.isChecked(e)).collect(List.collector());
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3393
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3394
        /** If n = 0 (the function type's throws clause consists only of proper types), then
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3395
         *  if there exists some i (1 <= i <= m) such that Xi is not a subtype of any proper type
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3396
         *  in the throws clause, the constraint reduces to false; otherwise, the constraint
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3397
         *  reduces to true
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3398
         */
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3399
        ListBuffer<Type> uncaughtByProperTypes = new ListBuffer<>();
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3400
        for (Type checked : checkedList) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3401
            boolean isSubtype = false;
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3402
            for (Type proper : properList) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3403
                if (types.isSubtype(checked, proper)) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3404
                    isSubtype = true;
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3405
                    break;
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3406
                }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3407
            }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3408
            if (!isSubtype) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3409
                uncaughtByProperTypes.add(checked);
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3410
            }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3411
        }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3412
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3413
        if (nonProperList.isEmpty() && !uncaughtByProperTypes.isEmpty()) {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3414
            return false;
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3415
        }
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3416
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3417
        /** If n > 0, the constraint reduces to a set of subtyping constraints:
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3418
         *  for all i (1 <= i <= m), if Xi is not a subtype of any proper type in the
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3419
         *  throws clause, then the constraints include, for all j (1 <= j <= n), <Xi <: Ej>
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3420
         */
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3421
        List<Type> nonProperAsUndet = inferenceContext.asUndetVars(nonProperList);
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3422
        uncaughtByProperTypes.forEach(checkedEx -> {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3423
            nonProperAsUndet.forEach(nonProper -> {
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3424
                types.isSubtype(checkedEx, nonProper);
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3425
            });
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3426
        });
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3427
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3428
        /** In addition, for all j (1 <= j <= n), the constraint reduces to the bound throws Ej
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3429
         */
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3430
        nonProperAsUndet.stream()
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3431
                .filter(t -> t.hasTag(UNDETVAR))
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3432
                .forEach(t -> ((UndetVar)t).setThrow());
67f4f8f4d34a 8182047: javac compile error on type-parameter-exceptions in lambda expressions
vromero
parents: 45750
diff changeset
  3433
        return true;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3434
    }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  3435
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3436
    /**
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3437
     * 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
  3438
     * 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
  3439
     * current inference context.
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3440
     */
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  3441
    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
  3442
            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
  3443
        if (checkContext.inferenceContext().free(descriptorType)) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
  3444
            checkContext.inferenceContext().addFreeTypeListener(List.of(pt, descriptorType),
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
  3445
                    inferenceContext -> setFunctionalInfo(env, fExpr, pt, inferenceContext.asInstType(descriptorType),
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
  3446
                    inferenceContext.asInstType(primaryTarget), checkContext));
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3447
        } else {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3448
            if (pt.hasTag(CLASS)) {
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  3449
                fExpr.target = primaryTarget;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3450
            }
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  3451
            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
  3452
                    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
  3453
                //check that functional interface class is well-formed
25273
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3454
                try {
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3455
                    /* Types.makeFunctionalInterfaceClass() may throw an exception
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3456
                     * when it's executed post-inference. See the listener code
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3457
                     * above.
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3458
                     */
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3459
                    ClassSymbol csym = types.makeFunctionalInterfaceClass(env,
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  3460
                            names.empty, fExpr.target, ABSTRACT);
25273
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3461
                    if (csym != null) {
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3462
                        chk.checkImplementations(env.tree, csym, csym);
29553
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3463
                        try {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3464
                            //perform an additional functional interface check on the synthetic class,
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3465
                            //as there may be spurious errors for raw targets - because of existing issues
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3466
                            //with membership and inheritance (see JDK-8074570).
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3467
                            csym.flags_field |= INTERFACE;
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3468
                            types.findDescriptorType(csym.type);
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3469
                        } catch (FunctionDescriptorLookupError err) {
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3470
                            resultInfo.checkContext.report(fExpr,
50181
f854b76b6a0c 8148354: Errors targeting functional interface intersection types
vromero
parents: 50086
diff changeset
  3471
                                    diags.fragment(Fragments.NoSuitableFunctionalIntfInst(fExpr.target)));
29553
716d47f5a45e 8074381: java.lang.AssertionError during compiling
mcimadamore
parents: 29552
diff changeset
  3472
                        }
25273
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3473
                    }
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3474
                } catch (Types.FunctionDescriptorLookupError ex) {
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3475
                    JCDiagnostic cause = ex.getDiagnostic();
87443756f3ac 8038182: javac crash with FunctionDescriptorLookupError for invalid functional interface
vromero
parents: 25007
diff changeset
  3476
                    resultInfo.checkContext.report(env.tree, cause);
19127
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 19122
diff changeset
  3477
                }
18730
95354d510139 8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents: 18662
diff changeset
  3478
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3479
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3480
    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15356
diff changeset
  3481
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3482
    public void visitParens(JCParens tree) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3483
        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
  3484
        result = check(tree, owntype, pkind(), resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3485
        Symbol sym = TreeInfo.symbol(tree);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3486
        if (sym != null && sym.kind.matches(KindSelector.TYP_PCK))
45750
15404afd36ab 8058408: Compiler should emit a clearer message for invalid parenthesized expression
sadayapalam
parents: 45605
diff changeset
  3487
            log.error(tree.pos(), Errors.IllegalParenthesizedExpression);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3488
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3489
06bc494ca11e Initial load
duke
parents:
diff changeset
  3490
    public void visitAssign(JCAssign tree) {
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  3491
        Type owntype = attribTree(tree.lhs, env.dup(tree), varAssignmentInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
        Type capturedType = capture(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
        attribExpr(tree.rhs, env, owntype);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3494
        result = check(tree, capturedType, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3496
06bc494ca11e Initial load
duke
parents:
diff changeset
  3497
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3498
        // Attribute arguments.
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  3499
        Type owntype = attribTree(tree.lhs, env, varAssignmentInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3500
        Type operand = attribExpr(tree.rhs, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3501
        // Find operator.
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  3502
        Symbol operator = tree.operator = operators.resolveBinary(tree, tree.getTag().noAssignOp(), owntype, operand);
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39810
diff changeset
  3503
        if (operator != operators.noOpSymbol &&
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3504
                !owntype.isErroneous() &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3505
                !operand.isErroneous()) {
166
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  3506
            chk.checkDivZero(tree.rhs.pos(), operator, operand);
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  3507
            chk.checkCastable(tree.rhs.pos(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  3508
                              operator.type.getReturnType(),
9f5e9d551da1 4741726: allow Object += String
jjg
parents: 164
diff changeset
  3509
                              owntype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3510
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3511
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3512
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3513
06bc494ca11e Initial load
duke
parents:
diff changeset
  3514
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3515
        // Attribute arguments.
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  3516
        Type argtype = (tree.getTag().isIncOrDecUnaryOp())
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  3517
            ? attribTree(tree.arg, env, varAssignmentInfo)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3518
            : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3519
06bc494ca11e Initial load
duke
parents:
diff changeset
  3520
        // Find operator.
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  3521
        Symbol operator = tree.operator = operators.resolveUnary(tree, tree.getTag(), argtype);
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
  3522
        Type owntype = types.createErrorType(tree.type);
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39810
diff changeset
  3523
        if (operator != operators.noOpSymbol &&
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3524
                !argtype.isErroneous()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  3525
            owntype = (tree.getTag().isIncOrDecUnaryOp())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3526
                ? tree.arg.type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3527
                : operator.type.getReturnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3528
            int opc = ((OperatorSymbol)operator).opcode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3529
06bc494ca11e Initial load
duke
parents:
diff changeset
  3530
            // If the argument is constant, fold it.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3531
            if (argtype.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3532
                Type ctype = cfolder.fold1(opc, argtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3533
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3534
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3535
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3537
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3538
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3539
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3540
06bc494ca11e Initial load
duke
parents:
diff changeset
  3541
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3542
        // Attribute arguments.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3543
        Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env));
29552
690d0becaf32 8074148: Attr.visitBinary flags error at wrong position
sadayapalam
parents: 29430
diff changeset
  3544
        Type right = chk.checkNonVoid(tree.rhs.pos(), attribExpr(tree.rhs, env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3545
        // Find operator.
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  3546
        Symbol operator = tree.operator = operators.resolveBinary(tree, tree.getTag(), left, right);
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
  3547
        Type owntype = types.createErrorType(tree.type);
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39810
diff changeset
  3548
        if (operator != operators.noOpSymbol &&
8237
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3549
                !left.isErroneous() &&
d5ef8db7ad09 5017953: spurious cascaded diagnostics when name not found
mcimadamore
parents: 8236
diff changeset
  3550
                !right.isErroneous()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3551
            owntype = operator.type.getReturnType();
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  3552
            int opc = ((OperatorSymbol)operator).opcode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3553
            // If both arguments are constants, fold them.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3554
            if (left.constValue() != null && right.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3555
                Type ctype = cfolder.fold2(opc, left, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3556
                if (ctype != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3557
                    owntype = cfolder.coerce(ctype, owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3558
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3559
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3560
06bc494ca11e Initial load
duke
parents:
diff changeset
  3561
            // Check that argument types of a reference ==, != are
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3562
            // castable to each other, (JLS 15.21).  Note: unboxing
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 18657
diff changeset
  3563
            // comparisons will not have an acmp* opc at this point.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3564
            if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents: 28591
diff changeset
  3565
                if (!types.isCastable(left, right, new Warner(tree.pos()))) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3566
                    log.error(tree.pos(), Errors.IncomparableTypes(left, right));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3567
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3568
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3569
06bc494ca11e Initial load
duke
parents:
diff changeset
  3570
            chk.checkDivZero(tree.rhs.pos(), operator, right);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3571
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3572
        result = check(tree, owntype, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3573
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3574
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3575
    public void visitTypeCast(final JCTypeCast tree) {
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  3576
        Type clazztype = attribType(tree.clazz, env);
6351
84c44db80d06 6885255: Improve usability of raw warnings
mcimadamore
parents: 6347
diff changeset
  3577
        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
  3578
        //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
  3579
        //see Infer.instantiatePolymorphicSignatureInstance()
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6587
diff changeset
  3580
        Env<AttrContext> localEnv = env.dup(tree);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3581
        //should we propagate the target type?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3582
        final ResultInfo castInfo;
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16972
diff changeset
  3583
        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
  3584
        boolean isPoly = allowPoly && (expr.hasTag(LAMBDA) || expr.hasTag(REFERENCE));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3585
        if (isPoly) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3586
            //expression is a poly - we need to propagate target type info
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3587
            castInfo = new ResultInfo(KindSelector.VAL, clazztype,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3588
                                      new Check.NestedCheckContext(resultInfo.checkContext) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3589
                @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3590
                public boolean compatible(Type found, Type req, Warner warn) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3591
                    return types.isCastable(found, req, warn);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3592
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3593
            });
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3594
        } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3595
            //standalone cast - target-type info is not propagated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3596
            castInfo = unknownExprInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3597
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3598
        Type exprtype = attribTree(tree.expr, localEnv, castInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3599
        Type owntype = isPoly ? clazztype : chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3600
        if (exprtype.constValue() != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3601
            owntype = cfolder.coerce(exprtype, owntype);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3602
        result = check(tree, capture(owntype), KindSelector.VAL, resultInfo);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3603
        if (!isPoly)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3604
            chk.checkRedundantCast(localEnv, tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
06bc494ca11e Initial load
duke
parents:
diff changeset
  3607
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
        Type exprtype = chk.checkNullOrRefType(
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3609
                tree.expr.pos(), attribExpr(tree.expr, env));
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  3610
        Type clazztype = attribType(tree.clazz, env);
20251
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3611
        if (!clazztype.hasTag(TYPEVAR)) {
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3612
            clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype);
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3613
        }
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3614
        if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3615
            log.error(tree.clazz.pos(), Errors.IllegalGenericTypeForInstof);
20251
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3616
            clazztype = types.createErrorType(clazztype);
90a73be53aa0 4881267: improve diagnostic for "instanceof T" for type parameter T
kizune
parents: 20249
diff changeset
  3617
        }
6351
84c44db80d06 6885255: Improve usability of raw warnings
mcimadamore
parents: 6347
diff changeset
  3618
        chk.validate(tree.clazz, env, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3619
        chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3620
        result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3621
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3622
06bc494ca11e Initial load
duke
parents:
diff changeset
  3623
    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
  3624
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
        Type atype = attribExpr(tree.indexed, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3626
        attribExpr(tree.index, env, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3627
        if (types.isArray(atype))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3628
            owntype = types.elemtype(atype);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3629
        else if (!atype.hasTag(ERROR))
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3630
            log.error(tree.pos(), Errors.ArrayReqButFound(atype));
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3631
        if (!pkind().contains(KindSelector.VAL))
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3632
            owntype = capture(owntype);
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3633
        result = check(tree, owntype, KindSelector.VAR, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3634
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3635
06bc494ca11e Initial load
duke
parents:
diff changeset
  3636
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3637
        Symbol sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3638
06bc494ca11e Initial load
duke
parents:
diff changeset
  3639
        // Find symbol
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3640
        if (pt().hasTag(METHOD) || pt().hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3641
            // If we are looking for a method, the prototype `pt' will be a
06bc494ca11e Initial load
duke
parents:
diff changeset
  3642
            // 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
  3643
            env.info.pendingResolutionPhase = null;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3644
            sym = rs.resolveMethod(tree.pos(), env, tree.name, pt().getParameterTypes(), pt().getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3645
        } else if (tree.sym != null && tree.sym.kind != VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3646
            sym = tree.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3647
        } else {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3648
            sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3649
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3651
06bc494ca11e Initial load
duke
parents:
diff changeset
  3652
        // (1) Also find the environment current for the class where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3653
        //     sym is defined (`symEnv').
06bc494ca11e Initial load
duke
parents:
diff changeset
  3654
        // Only for pre-tiger versions (1.4 and earlier):
06bc494ca11e Initial load
duke
parents:
diff changeset
  3655
        // (2) Also determine whether we access symbol out of an anonymous
06bc494ca11e Initial load
duke
parents:
diff changeset
  3656
        //     class in a this or super call.  This is illegal for instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  3657
        //     members since such classes don't carry a this$n link.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3658
        //     (`noOuterThisPath').
06bc494ca11e Initial load
duke
parents:
diff changeset
  3659
        Env<AttrContext> symEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3660
        boolean noOuterThisPath = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3661
        if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3662
            sym.kind.matches(KindSelector.VAL_MTH) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3663
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3664
            tree.name != names._this && tree.name != names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3665
06bc494ca11e Initial load
duke
parents:
diff changeset
  3666
            // Find environment in which identifier is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3667
            while (symEnv.outer != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3668
                   !sym.isMemberOf(symEnv.enclClass.sym, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3669
                if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0)
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  3670
                    noOuterThisPath = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3671
                symEnv = symEnv.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3672
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3673
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3674
06bc494ca11e Initial load
duke
parents:
diff changeset
  3675
        // If symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3676
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3678
06bc494ca11e Initial load
duke
parents:
diff changeset
  3679
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  3680
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3681
            checkInit(tree, env, v, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
06bc494ca11e Initial load
duke
parents:
diff changeset
  3683
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  3684
            // that the variable is assignable in the current environment.
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  3685
            if (KindSelector.ASG.subset(pkind()))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3686
                checkAssignable(tree.pos(), v, null, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3687
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3688
06bc494ca11e Initial load
duke
parents:
diff changeset
  3689
        // In a constructor body,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3690
        // if symbol is a field or instance method, check that it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3691
        // not accessed before the supertype constructor is called.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3692
        if ((symEnv.info.isSelfCall || noOuterThisPath) &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3693
            sym.kind.matches(KindSelector.VAL_MTH) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3694
            sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3695
            (sym.flags() & STATIC) == 0) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3696
            chk.earlyRefError(tree.pos(), sym.kind == VAR ?
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3697
                                          sym : thisSym(tree.pos(), env));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3698
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
        Env<AttrContext> env1 = env;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3700
        if (sym.kind != ERR && sym.kind != TYP &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3701
            sym.owner != null && sym.owner != env1.enclClass.sym) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3702
            // If the found symbol is inaccessible, then it is
06bc494ca11e Initial load
duke
parents:
diff changeset
  3703
            // accessed through an enclosing instance.  Locate this
06bc494ca11e Initial load
duke
parents:
diff changeset
  3704
            // enclosing instance:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3705
            while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3706
                env1 = env1.outer;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3707
        }
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3708
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3709
        if (env.info.isSerializable) {
41629
9d203cde7d84 8026721: Enhance Lambda serialization
vromero
parents: 41444
diff changeset
  3710
            chk.checkAccessFromSerializableElement(tree, env.info.isLambda);
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3711
        }
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3712
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3713
        result = checkId(tree, env1.enclClass.sym.type, sym, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3714
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3715
06bc494ca11e Initial load
duke
parents:
diff changeset
  3716
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3717
        // Determine the expected kind of the qualifier expression.
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3718
        KindSelector skind = KindSelector.NIL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3719
        if (tree.name == names._this || tree.name == names._super ||
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3720
                tree.name == names._class)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
        {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3722
            skind = KindSelector.TYP;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3723
        } else {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3724
            if (pkind().contains(KindSelector.PCK))
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3725
                skind = KindSelector.of(skind, KindSelector.PCK);
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3726
            if (pkind().contains(KindSelector.TYP))
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3727
                skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3728
            if (pkind().contains(KindSelector.VAL_MTH))
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3729
                skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3730
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3731
06bc494ca11e Initial load
duke
parents:
diff changeset
  3732
        // Attribute the qualifier expression, and determine its symbol (if any).
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  3733
        Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3734
        if (!pkind().contains(KindSelector.TYP_PCK))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3735
            site = capture(site); // Capture field access
06bc494ca11e Initial load
duke
parents:
diff changeset
  3736
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  3737
        // don't allow T.class T[].class, etc
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3738
        if (skind == KindSelector.TYP) {
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24794
diff changeset
  3739
            Type elt = site;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3740
            while (elt.hasTag(ARRAY))
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24296
diff changeset
  3741
                elt = ((ArrayType)elt).elemtype;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3742
            if (elt.hasTag(TYPEVAR)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3743
                log.error(tree.pos(), Errors.TypeVarCantBeDeref);
26780
82b0aaba473d 8057794: Compiler Error when obtaining .class property
jlahoda
parents: 26532
diff changeset
  3744
                result = tree.type = types.createErrorType(tree.name, site.tsym, site);
82b0aaba473d 8057794: Compiler Error when obtaining .class property
jlahoda
parents: 26532
diff changeset
  3745
                tree.sym = tree.type.tsym;
82b0aaba473d 8057794: Compiler Error when obtaining .class property
jlahoda
parents: 26532
diff changeset
  3746
                return ;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3747
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3748
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3749
06bc494ca11e Initial load
duke
parents:
diff changeset
  3750
        // If qualifier symbol is a type or `super', assert `selectSuper'
06bc494ca11e Initial load
duke
parents:
diff changeset
  3751
        // for the selection. This is relevant for determining whether
06bc494ca11e Initial load
duke
parents:
diff changeset
  3752
        // protected symbols are accessible.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3753
        Symbol sitesym = TreeInfo.symbol(tree.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3754
        boolean selectSuperPrev = env.info.selectSuper;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3755
        env.info.selectSuper =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3756
            sitesym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3757
            sitesym.name == names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3758
06bc494ca11e Initial load
duke
parents:
diff changeset
  3759
        // Determine the symbol represented by the selection.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3760
        env.info.pendingResolutionPhase = null;
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3761
        Symbol sym = selectSym(tree, sitesym, site, env, resultInfo);
27845
afce9de2de10 8059921: Missing compile error in Java 8 mode for Interface.super.field access
vromero
parents: 27844
diff changeset
  3762
        if (sym.kind == VAR && sym.name != names._super && env.info.defaultSuperCallSite != null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3763
            log.error(tree.selected.pos(), Errors.NotEnclClass(site.tsym));
27845
afce9de2de10 8059921: Missing compile error in Java 8 mode for Interface.super.field access
vromero
parents: 27844
diff changeset
  3764
            sym = syms.errSymbol;
afce9de2de10 8059921: Missing compile error in Java 8 mode for Interface.super.field access
vromero
parents: 27844
diff changeset
  3765
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3766
        if (sym.exists() && !isType(sym) && pkind().contains(KindSelector.TYP_PCK)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3767
            site = capture(site);
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3768
            sym = selectSym(tree, sitesym, site, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3769
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3770
        boolean varArgs = env.info.lastResolveVarargs();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3771
        tree.sym = sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3772
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3773
        if (site.hasTag(TYPEVAR) && !isType(sym) && sym.kind != ERR) {
29292
c10d63c667cd 8073842: Invalid method reference when referencing a method on a wildcard type
mcimadamore
parents: 29147
diff changeset
  3774
            site = types.skipTypeVars(site, true);
511
b3b5eadd2bca 6450290: Capture of nested wildcards causes type error
mcimadamore
parents: 325
diff changeset
  3775
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3776
06bc494ca11e Initial load
duke
parents:
diff changeset
  3777
        // If that symbol is a variable, ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3778
        if (sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3779
            VarSymbol v = (VarSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3780
06bc494ca11e Initial load
duke
parents:
diff changeset
  3781
            // ..., evaluate its initializer, if it has one, and check for
06bc494ca11e Initial load
duke
parents:
diff changeset
  3782
            // illegal forward reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3783
            checkInit(tree, env, v, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3784
06bc494ca11e Initial load
duke
parents:
diff changeset
  3785
            // If we are expecting a variable (as opposed to a value), check
06bc494ca11e Initial load
duke
parents:
diff changeset
  3786
            // that the variable is assignable in the current environment.
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  3787
            if (KindSelector.ASG.subset(pkind()))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3788
                checkAssignable(tree.pos(), v, tree.selected, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3789
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3790
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3791
        if (sitesym != null &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3792
                sitesym.kind == VAR &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3793
                ((VarSymbol)sitesym).isResourceVariable() &&
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3794
                sym.kind == MTH &&
9079
e512a7712be3 7032633: javac -Xlint:all warns about flush() within try on an auto-closeable resource
mcimadamore
parents: 9076
diff changeset
  3795
                sym.name.equals(names.close) &&
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3796
                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
  3797
                env.info.lint.isEnabled(LintCategory.TRY)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3798
            log.warning(LintCategory.TRY, tree, Warnings.TryExplicitCloseCall);
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3799
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5857
diff changeset
  3800
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3801
        // Disallow selecting a type from an expression
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3802
        if (isType(sym) && (sitesym == null || !sitesym.kind.matches(KindSelector.TYP_PCK))) {
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3803
            tree.type = check(tree.selected, pt(),
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3804
                              sitesym == null ?
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3805
                                      KindSelector.VAL : sitesym.kind.toSelector(),
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3806
                              new ResultInfo(KindSelector.TYP_PCK, pt()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3807
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3808
06bc494ca11e Initial load
duke
parents:
diff changeset
  3809
        if (isType(sitesym)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3810
            if (sym.name == names._this) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3811
                // If `C' is the currently compiled class, check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3812
                // C.this' does not appear in a call to a super(...)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3813
                if (env.info.isSelfCall &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3814
                    site.tsym == env.enclClass.sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3815
                    chk.earlyRefError(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3817
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3818
                // Check if type-qualified fields or methods are static (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3819
                if ((sym.flags() & STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3820
                    sym.name != names._super &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3821
                    (sym.kind == VAR || sym.kind == MTH)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3822
                    rs.accessBase(rs.new StaticError(sym),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3823
                              tree.pos(), site, sym.name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3824
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
            }
24901
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
  3826
            if (!allowStaticInterfaceMethods && sitesym.isInterface() &&
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
  3827
                    sym.isStatic() && sym.kind == MTH) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47318
diff changeset
  3828
                log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(), Feature.STATIC_INTERFACE_METHODS_INVOKE.error(sourceName));
24901
73bacb7d064e 8037385: constant pool errors with -target 1.7 and static default methods
dlsmith
parents: 24900
diff changeset
  3829
            }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3830
        } else if (sym.kind != ERR &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3831
                   (sym.flags() & STATIC) != 0 &&
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3832
                   sym.name != names._class) {
5002
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 5001
diff changeset
  3833
            // 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
  3834
            // a warning. Make allowance for the class of an array type e.g. Object[].class)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  3835
            chk.warnStatic(tree, Warnings.StaticNotQualifiedByType(sym.kind.kindName(), sym.owner));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3836
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3837
06bc494ca11e Initial load
duke
parents:
diff changeset
  3838
        // If we are selecting an instance member via a `super', ...
06bc494ca11e Initial load
duke
parents:
diff changeset
  3839
        if (env.info.selectSuper && (sym.flags() & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3840
06bc494ca11e Initial load
duke
parents:
diff changeset
  3841
            // Check that super-qualified symbols are not abstract (JLS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
            rs.checkNonAbstract(tree.pos(), sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
06bc494ca11e Initial load
duke
parents:
diff changeset
  3844
            if (site.isRaw()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3845
                // Determine argument types for site.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3846
                Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3847
                if (site1 != null) site = site1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3848
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3849
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3850
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3851
        if (env.info.isSerializable) {
41629
9d203cde7d84 8026721: Enhance Lambda serialization
vromero
parents: 41444
diff changeset
  3852
            chk.checkAccessFromSerializableElement(tree, env.info.isLambda);
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3853
        }
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  3854
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3855
        env.info.selectSuper = selectSuperPrev;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3856
        result = checkId(tree, site, sym, env, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3858
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
        /** Determine symbol referenced by a Select expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3860
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3861
         *  @param tree   The select tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3862
         *  @param site   The type of the selected expression,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3863
         *  @param env    The current environment.
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3864
         *  @param resultInfo The current result.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3865
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3866
        private Symbol selectSym(JCFieldAccess tree,
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3867
                                 Symbol location,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3868
                                 Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3869
                                 Env<AttrContext> env,
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3870
                                 ResultInfo resultInfo) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3871
            DiagnosticPosition pos = tree.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3872
            Name name = tree.name;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3873
            switch (site.getTag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3874
            case PACKAGE:
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3875
                return rs.accessBase(
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  3876
                    rs.findIdentInPackage(pos, env, site.tsym, name, resultInfo.pkind),
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3877
                    pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3878
            case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
            case CLASS:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3880
                if (resultInfo.pt.hasTag(METHOD) || resultInfo.pt.hasTag(FORALL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3881
                    return rs.resolveQualifiedMethod(
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3882
                        pos, env, location, site, name, resultInfo.pt.getParameterTypes(), resultInfo.pt.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3883
                } else if (name == names._this || name == names._super) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3884
                    return rs.resolveSelf(pos, env, site.tsym, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3885
                } else if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3886
                    // In this case, we have already made sure in
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
                    // visitSelect that qualifier expression is a type.
49887
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents: 48931
diff changeset
  3888
                    return syms.getClassField(site, types);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
                    // We are seeing a plain identifier as selector.
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54952
diff changeset
  3891
                    Symbol sym = rs.findIdentInType(pos, env, site, name, resultInfo.pkind);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3892
                        sym = rs.accessBase(sym, pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3894
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3895
            case WILDCARD:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
                throw new AssertionError(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3897
            case TYPEVAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
                // Normally, site.getUpperBound() shouldn't be null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
                // It should only happen during memberEnter/attribBase
8045
df2ca0edfbaa 6968793: issues with diagnostics
mcimadamore
parents: 8036
diff changeset
  3900
                // when determining the super type which *must* beac
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3901
                // done before attributing the type variables.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  3902
                // other words, we are seeing this illegal program:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3903
                // class B<T> extends A<T.foo> {}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
                Symbol sym = (site.getUpperBound() != null)
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3905
                    ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3906
                    : null;
3559
58cfcc0f1aa9 6569404: Cannot instantiate an inner class of a type variable
mcimadamore
parents: 3556
diff changeset
  3907
                if (sym == null) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3908
                    log.error(pos, Errors.TypeVarCantBeDeref);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3909
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3910
                } else {
1528
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  3911
                    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
  3912
                        rs.new AccessError(env, site, sym) :
441d4ec466de 6711619: javac doesn't allow access to protected members in intersection types
mcimadamore
parents: 1358
diff changeset
  3913
                                sym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3914
                    rs.accessBase(sym2, pos, location, site, name, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3915
                    return sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3916
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3917
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3918
                // 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
  3919
                return types.createErrorType(name, site.tsym, site).tsym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3920
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3921
                // The qualifier expression is of a primitive type -- only
06bc494ca11e Initial load
duke
parents:
diff changeset
  3922
                // .class is allowed for these.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3923
                if (name == names._class) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3924
                    // In this case, we have already made sure in Select that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3925
                    // qualifier expression is a type.
49887
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents: 48931
diff changeset
  3926
                    return syms.getClassField(site, types);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3927
                } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  3928
                    log.error(pos, Errors.CantDeref(site));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3929
                    return syms.errSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3930
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3931
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3932
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3933
06bc494ca11e Initial load
duke
parents:
diff changeset
  3934
        /** Determine type of identifier or select expression and check that
06bc494ca11e Initial load
duke
parents:
diff changeset
  3935
         *  (1) the referenced symbol is not deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  3936
         *  (2) the symbol's type is safe (@see checkSafe)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3937
         *  (3) if symbol is a variable, check that its type and kind are
06bc494ca11e Initial load
duke
parents:
diff changeset
  3938
         *      compatible with the prototype and protokind.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3939
         *  (4) if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3940
         *      which is being assigned to, issue an unchecked warning if its
06bc494ca11e Initial load
duke
parents:
diff changeset
  3941
         *      type changes under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3942
         *  (5) if symbol is an instance method of a raw type, issue an
06bc494ca11e Initial load
duke
parents:
diff changeset
  3943
         *      unchecked warning if its argument types change under erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3944
         *  If checks succeed:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3945
         *    If symbol is a constant, return its constant type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3946
         *    else if symbol is a method, return its result type
06bc494ca11e Initial load
duke
parents:
diff changeset
  3947
         *    otherwise return its type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3948
         *  Otherwise return errType.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3949
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3950
         *  @param tree       The syntax tree representing the identifier
06bc494ca11e Initial load
duke
parents:
diff changeset
  3951
         *  @param site       If this is a select, the type of the selected
06bc494ca11e Initial load
duke
parents:
diff changeset
  3952
         *                    expression, otherwise the type of the current class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3953
         *  @param sym        The symbol representing the identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3954
         *  @param env        The current environment.
12081
42f541476d14 7133238: Merge proto-kind and proto-type into a single result class
mcimadamore
parents: 12080
diff changeset
  3955
         *  @param resultInfo    The expected result
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3956
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3957
        Type checkId(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3958
                     Type site,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3959
                     Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3960
                     Env<AttrContext> env,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3961
                     ResultInfo resultInfo) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3962
            return (resultInfo.pt.hasTag(FORALL) || resultInfo.pt.hasTag(METHOD)) ?
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 50200
diff changeset
  3963
                    checkMethodIdInternal(tree, site, sym, env, resultInfo) :
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3964
                    checkIdInternal(tree, site, sym, resultInfo.pt, env, resultInfo);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3965
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3966
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3967
        Type checkMethodIdInternal(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3968
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3969
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3970
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3971
                     ResultInfo resultInfo) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  3972
            if (resultInfo.pkind.contains(KindSelector.POLY)) {
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3973
                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
  3974
                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
  3975
                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
  3976
                return owntype;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3977
            } else {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18412
diff changeset
  3978
                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
  3979
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3980
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3981
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3982
        Type checkIdInternal(JCTree tree,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3983
                     Type site,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3984
                     Symbol sym,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3985
                     Type pt,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3986
                     Env<AttrContext> env,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  3987
                     ResultInfo resultInfo) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3988
            if (pt.isErroneous()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3989
                return types.createErrorType(site);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  3990
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3991
            Type owntype; // The computed type of this identifier occurrence.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3992
            switch (sym.kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3993
            case TYP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3994
                // For types, the computed type equals the symbol's type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3995
                // except for two situations:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3996
                owntype = sym.type;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3997
                if (owntype.hasTag(CLASS)) {
14369
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 14366
diff changeset
  3998
                    chk.checkForBadAuxiliaryClassAccess(tree.pos(), env, (ClassSymbol)sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3999
                    Type ownOuter = owntype.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4000
06bc494ca11e Initial load
duke
parents:
diff changeset
  4001
                    // (a) If the symbol's type is parameterized, erase it
06bc494ca11e Initial load
duke
parents:
diff changeset
  4002
                    // because no type parameters were given.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4003
                    // We recover generic outer type later in visitTypeApply.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4004
                    if (owntype.tsym.type.getTypeArguments().nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4005
                        owntype = types.erasure(owntype);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4006
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4007
06bc494ca11e Initial load
duke
parents:
diff changeset
  4008
                    // (b) If the symbol's type is an inner class, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  4009
                    // we have to interpret its outer type as a superclass
06bc494ca11e Initial load
duke
parents:
diff changeset
  4010
                    // of the site type. Example:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4011
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  4012
                    // class Tree<A> { class Visitor { ... } }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4013
                    // class PointTree extends Tree<Point> { ... }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4014
                    // ...PointTree.Visitor...
06bc494ca11e Initial load
duke
parents:
diff changeset
  4015
                    //
06bc494ca11e Initial load
duke
parents:
diff changeset
  4016
                    // Then the type of the last expression above is
06bc494ca11e Initial load
duke
parents:
diff changeset
  4017
                    // Tree<Point>.Visitor.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4018
                    else if (ownOuter.hasTag(CLASS) && site != ownOuter) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4019
                        Type normOuter = site;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4020
                        if (normOuter.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4021
                            normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4022
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4023
                        if (normOuter == null) // perhaps from an import
06bc494ca11e Initial load
duke
parents:
diff changeset
  4024
                            normOuter = types.erasure(ownOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4025
                        if (normOuter != ownOuter)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4026
                            owntype = new ClassType(
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  4027
                                normOuter, List.nil(), owntype.tsym,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  4028
                                owntype.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4029
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4030
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4031
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4032
            case VAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4033
                VarSymbol v = (VarSymbol)sym;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4034
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4035
                if (env.info.enclVar != null
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4036
                        && v.type.hasTag(NONE)) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4037
                    //self reference to implicitly typed variable declaration
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4038
                    log.error(TreeInfo.positionFor(v, env.enclClass), Errors.CantInferLocalVarType(v.name, Fragments.LocalSelfRef));
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4039
                    return v.type = types.createErrorType(v.type);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4040
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  4041
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4042
                // Test (4): if symbol is an instance field of a raw type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4043
                // which is being assigned to, issue an unchecked warning if
06bc494ca11e Initial load
duke
parents:
diff changeset
  4044
                // its type changes under erasure.
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 27553
diff changeset
  4045
                if (KindSelector.ASG.subset(pkind()) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4046
                    v.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4047
                    (v.flags() & STATIC) == 0 &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4048
                    (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4049
                    Type s = types.asOuterSuper(site, v.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4050
                    if (s != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4051
                        s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4052
                        !types.isSameType(v.type, v.erasure(types))) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4053
                        chk.warnUnchecked(tree.pos(), Warnings.UncheckedAssignToVar(v, s));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4054
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4055
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4056
                // The computed type of a variable is the type of the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4057
                // variable symbol, taken as a member of the site type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4058
                owntype = (sym.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4059
                           sym.name != names._this && sym.name != names._super)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4060
                    ? types.memberType(site, sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4061
                    : sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4062
06bc494ca11e Initial load
duke
parents:
diff changeset
  4063
                // If the variable is a constant, record constant value in
06bc494ca11e Initial load
duke
parents:
diff changeset
  4064
                // computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4065
                if (v.getConstValue() != null && isStaticReference(tree))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4066
                    owntype = owntype.constType(v.getConstValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  4067
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4068
                if (resultInfo.pkind == KindSelector.VAL) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4069
                    owntype = capture(owntype); // capture "names as expressions"
06bc494ca11e Initial load
duke
parents:
diff changeset
  4070
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4071
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4072
            case MTH: {
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  4073
                owntype = checkMethod(site, sym,
44392
e21b18355690 8177097: Generic method reference returning wildcard parameterized type does not compile
mcimadamore
parents: 44194
diff changeset
  4074
                        new ResultInfo(resultInfo.pkind, resultInfo.pt.getReturnType(), resultInfo.checkContext, resultInfo.checkMode),
14051
9097cec96212 7188968: New instance creation expression using diamond is checked twice
mcimadamore
parents: 13689
diff changeset
  4075
                        env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(),
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4076
                        resultInfo.pt.getTypeArguments());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4077
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4078
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4079
            case PCK: case ERR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4080
                owntype = sym.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4081
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4082
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  4083
                throw new AssertionError("unexpected kind: " + sym.kind +
06bc494ca11e Initial load
duke
parents:
diff changeset
  4084
                                         " in tree " + tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4085
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4086
32950
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4087
            // Emit a `deprecation' warning if symbol is deprecated.
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4088
            // (for constructors (but not for constructor references), the error
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4089
            // was given when the constructor was resolved)
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4090
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4091
            if (sym.name != names.init || tree.hasTag(REFERENCE)) {
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
  4092
                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
  4093
                chk.checkSunAPI(tree.pos(), sym);
15724
3063fb01c8a1 8004182: Add support for profiles in javac
jjg
parents: 14953
diff changeset
  4094
                chk.checkProfile(tree.pos(), sym);
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 58350
diff changeset
  4095
                chk.checkPreview(tree.pos(), sym);
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 3559
diff changeset
  4096
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4097
32950
22315d47485b 8065219: Deprecated warning in method reference are missing in some cases.
sadayapalam
parents: 32912
diff changeset
  4098
            // If symbol is a variable, check that its type and
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4099
            // kind are compatible with the prototype and protokind.
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4100
            return check(tree, owntype, sym.kind.toSelector(), resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4102
06bc494ca11e Initial load
duke
parents:
diff changeset
  4103
        /** Check that variable is initialized and evaluate the variable's
06bc494ca11e Initial load
duke
parents:
diff changeset
  4104
         *  initializer, if not yet done. Also check that variable is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  4105
         *  referenced before it is defined.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4106
         *  @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4107
         *  @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4108
         *  @param v       The variable's symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4109
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4110
        private void checkInit(JCTree tree,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4111
                               Env<AttrContext> env,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4112
                               VarSymbol v,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4113
                               boolean onlyWarning) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4114
            // A forward reference is diagnosed if the declaration position
06bc494ca11e Initial load
duke
parents:
diff changeset
  4115
            // of the variable is greater than the current tree position
06bc494ca11e Initial load
duke
parents:
diff changeset
  4116
            // and the tree and variable definition occur in the same class
06bc494ca11e Initial load
duke
parents:
diff changeset
  4117
            // definition.  Note that writes don't count as references.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4118
            // This check applies only to class and instance
06bc494ca11e Initial load
duke
parents:
diff changeset
  4119
            // variables.  Local variables follow different scope rules,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4120
            // and are subject to definite assignment checking.
32164
6c65d5b414bc 8053906: javac is accepting a self-referencing variable initializer inside a lambda expression
vromero
parents: 31937
diff changeset
  4121
            Env<AttrContext> initEnv = enclosingInitEnv(env);
6c65d5b414bc 8053906: javac is accepting a self-referencing variable initializer inside a lambda expression
vromero
parents: 31937
diff changeset
  4122
            if (initEnv != null &&
6c65d5b414bc 8053906: javac is accepting a self-referencing variable initializer inside a lambda expression
vromero
parents: 31937
diff changeset
  4123
                (initEnv.info.enclVar == v || v.pos > tree.pos) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4124
                v.owner.kind == TYP &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4125
                v.owner == env.info.scope.owner.enclClass() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4126
                ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4127
                (!env.tree.hasTag(ASSIGN) ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4128
                 TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) {
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4129
                if (!onlyWarning || isStaticEnumField(v)) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4130
                    Error errkey = (initEnv.info.enclVar == v) ?
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4131
                                Errors.IllegalSelfRef : Errors.IllegalForwardRef;
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4132
                    log.error(tree.pos(), errkey);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4133
                } else if (useBeforeDeclarationWarning) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4134
                    Warning warnkey = (initEnv.info.enclVar == v) ?
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4135
                                Warnings.SelfRef(v) : Warnings.ForwardRef(v);
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4136
                    log.warning(tree.pos(), warnkey);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4137
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4138
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4139
06bc494ca11e Initial load
duke
parents:
diff changeset
  4140
            v.getConstValue(); // ensure initializer is evaluated
06bc494ca11e Initial load
duke
parents:
diff changeset
  4141
06bc494ca11e Initial load
duke
parents:
diff changeset
  4142
            checkEnumInitializer(tree, env, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4144
06bc494ca11e Initial load
duke
parents:
diff changeset
  4145
        /**
25846
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4146
         * Returns the enclosing init environment associated with this env (if any). An init env
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4147
         * can be either a field declaration env or a static/instance initializer env.
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4148
         */
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4149
        Env<AttrContext> enclosingInitEnv(Env<AttrContext> env) {
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4150
            while (true) {
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4151
                switch (env.tree.getTag()) {
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4152
                    case VARDEF:
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4153
                        JCVariableDecl vdecl = (JCVariableDecl)env.tree;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4154
                        if (vdecl.sym.owner.kind == TYP) {
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4155
                            //field
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4156
                            return env;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4157
                        }
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4158
                        break;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4159
                    case BLOCK:
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4160
                        if (env.next.tree.hasTag(CLASSDEF)) {
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4161
                            //instance/static initializer
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4162
                            return env;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4163
                        }
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4164
                        break;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4165
                    case METHODDEF:
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4166
                    case CLASSDEF:
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4167
                    case TOPLEVEL:
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4168
                        return null;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4169
                }
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4170
                Assert.checkNonNull(env.next);
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4171
                env = env.next;
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4172
            }
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4173
        }
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4174
e7124d0dc984 8051958: Cannot assign a value to final variable in lambda
mcimadamore
parents: 25844
diff changeset
  4175
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4176
         * Check for illegal references to static members of enum.  In
06bc494ca11e Initial load
duke
parents:
diff changeset
  4177
         * an enum type, constructors and initializers may not
06bc494ca11e Initial load
duke
parents:
diff changeset
  4178
         * reference its static members unless they are constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4179
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
  4180
         * @param tree    The tree making up the variable reference.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4181
         * @param env     The current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4182
         * @param v       The variable's symbol.
54952
a978d86ac389 8224175: Fix inconsistencies in @jls and @jvms tags
darcy
parents: 54788
diff changeset
  4183
         * @jls 8.9 Enum Types
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4184
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4185
        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
  4186
            // JLS:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4187
            //
06bc494ca11e Initial load
duke
parents:
diff changeset
  4188
            // "It is a compile-time error to reference a static field
06bc494ca11e Initial load
duke
parents:
diff changeset
  4189
            // of an enum type that is not a compile-time constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  4190
            // (15.28) from constructors, instance initializer blocks,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4191
            // or instance variable initializer expressions of that
06bc494ca11e Initial load
duke
parents:
diff changeset
  4192
            // type. It is a compile-time error for the constructors,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4193
            // instance initializer blocks, or instance variable
06bc494ca11e Initial load
duke
parents:
diff changeset
  4194
            // initializer expressions of an enum constant e to refer
06bc494ca11e Initial load
duke
parents:
diff changeset
  4195
            // to itself or to an enum constant of the same type that
06bc494ca11e Initial load
duke
parents:
diff changeset
  4196
            // is declared to the right of e."
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4197
            if (isStaticEnumField(v)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4198
                ClassSymbol enclClass = env.info.scope.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4199
06bc494ca11e Initial load
duke
parents:
diff changeset
  4200
                if (enclClass == null || enclClass.owner == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4201
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4202
06bc494ca11e Initial load
duke
parents:
diff changeset
  4203
                // See if the enclosing class is the enum (or a
06bc494ca11e Initial load
duke
parents:
diff changeset
  4204
                // subclass thereof) declaring v.  If not, this
06bc494ca11e Initial load
duke
parents:
diff changeset
  4205
                // reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4206
                if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4207
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4208
06bc494ca11e Initial load
duke
parents:
diff changeset
  4209
                // If the reference isn't from an initializer, then
06bc494ca11e Initial load
duke
parents:
diff changeset
  4210
                // the reference is OK.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4211
                if (!Resolve.isInitializer(env))
06bc494ca11e Initial load
duke
parents:
diff changeset
  4212
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4213
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4214
                log.error(tree.pos(), Errors.IllegalEnumStaticRef);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4215
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4216
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4217
325
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4218
        /** 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
  4219
         *  Note: enum literals should not be regarded as such
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4220
         */
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4221
        private boolean isStaticEnumField(VarSymbol v) {
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4222
            return Flags.isEnum(v.owner) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4223
                   Flags.isStatic(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4224
                   !Flags.isConstant(v) &&
44df0e9643b4 6509042: javac rejects class literals in enum constructors
mcimadamore
parents: 166
diff changeset
  4225
                   v.name != names._class;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4227
06bc494ca11e Initial load
duke
parents:
diff changeset
  4228
    /**
12080
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  4229
     * Check that method arguments conform to its instantiation.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4230
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
  4231
    public Type checkMethod(Type site,
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4232
                            final Symbol sym,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  4233
                            ResultInfo resultInfo,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  4234
                            Env<AttrContext> env,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  4235
                            final List<JCExpression> argtrees,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12334
diff changeset
  4236
                            List<Type> argtypes,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4237
                            List<Type> typeargtypes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4238
        // Test (5): if symbol is an instance method of a raw type, issue
06bc494ca11e Initial load
duke
parents:
diff changeset
  4239
        // an unchecked warning if its argument types change under erasure.
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25443
diff changeset
  4240
        if ((sym.flags() & STATIC) == 0 &&
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4241
            (site.hasTag(CLASS) || site.hasTag(TYPEVAR))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4242
            Type s = types.asOuterSuper(site, sym.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4243
            if (s != null && s.isRaw() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4244
                !types.isSameTypes(sym.type.getParameterTypes(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  4245
                                   sym.erasure(types).getParameterTypes())) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4246
                chk.warnUnchecked(env.tree.pos(), Warnings.UncheckedCallMbrOfRawType(sym, s));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4249
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4250
        if (env.info.defaultSuperCallSite != null) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4251
            for (Type sup : types.interfaces(env.enclClass.type).prepend(types.supertype((env.enclClass.type)))) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4252
                if (!sup.tsym.isSubClass(sym.enclClass(), types) ||
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4253
                        types.isSameType(sup, env.info.defaultSuperCallSite)) continue;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4254
                List<MethodSymbol> icand_sup =
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4255
                        types.interfaceCandidates(sup, (MethodSymbol)sym);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4256
                if (icand_sup.nonEmpty() &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4257
                        icand_sup.head != sym &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4258
                        icand_sup.head.overrides(sym, icand_sup.head.enclClass(), types, true)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4259
                    log.error(env.tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4260
                              Errors.IllegalDefaultSuperCall(env.info.defaultSuperCallSite, Fragments.OverriddenDefault(sym, sup)));
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4261
                    break;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4262
                }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4263
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
  4264
            env.info.defaultSuperCallSite = null;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4265
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4266
16330
66e277848a69 8009299: Javac crashes when compiling method reference to static interface method
mcimadamore
parents: 16328
diff changeset
  4267
        if (sym.isStatic() && site.isInterface() && env.tree.hasTag(APPLY)) {
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4268
            JCMethodInvocation app = (JCMethodInvocation)env.tree;
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4269
            if (app.meth.hasTag(SELECT) &&
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4270
                    !TreeInfo.isStaticSelector(((JCFieldAccess)app.meth).selected, names)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4271
                log.error(env.tree.pos(), Errors.IllegalStaticIntfMethCall(site));
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4272
            }
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4273
        }
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15374
diff changeset
  4274
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4275
        // Compute the identifier's instantiated type.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4276
        // For methods, we need to compute the instance type by
06bc494ca11e Initial load
duke
parents:
diff changeset
  4277
        // Resolve.instantiate from the symbol's type as well as
06bc494ca11e Initial load
duke
parents:
diff changeset
  4278
        // any type arguments and value arguments.
35349
4a13a361309a 8147493: regression when type-checking unchecked method calls
mcimadamore
parents: 33907
diff changeset
  4279
        Warner noteWarner = new Warner();
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4280
        try {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4281
            Type owntype = rs.checkMethod(
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4282
                    env,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4283
                    site,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4284
                    sym,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4285
                    resultInfo,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4286
                    argtypes,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4287
                    typeargtypes,
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4288
                    noteWarner);
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4289
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  4290
            DeferredAttr.DeferredTypeMap<Void> checkDeferredMap =
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50518
diff changeset
  4291
                deferredAttr.new DeferredTypeMap<>(DeferredAttr.AttrMode.CHECK, sym, env.info.pendingResolutionPhase);
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4292
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29553
diff changeset
  4293
            argtypes = argtypes.map(checkDeferredMap);
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4294
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4295
            if (noteWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4296
                chk.warnUnchecked(env.tree.pos(), Warnings.UncheckedMethInvocationApplied(kindName(sym),
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4297
                        sym.name,
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4298
                        rs.methodArguments(sym.type.getParameterTypes()),
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29553
diff changeset
  4299
                        rs.methodArguments(argtypes.map(checkDeferredMap)),
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4300
                        kindName(sym.location()),
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48449
diff changeset
  4301
                        sym.location()));
43761
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4302
                if (resultInfo.pt != Infer.anyPoly ||
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4303
                        !owntype.hasTag(METHOD) ||
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4304
                        !owntype.isPartial()) {
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4305
                    //if this is not a partially inferred method type, erase return type. Otherwise,
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4306
                    //erasure is carried out in PartiallyInferredMethodType.check().
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4307
                    owntype = new MethodType(owntype.getParameterTypes(),
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4308
                            types.erasure(owntype.getReturnType()),
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4309
                            types.erasure(owntype.getThrownTypes()),
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4310
                            syms.methodClass);
f50592785b30 8174249: Regression in generic method unchecked calls
mcimadamore
parents: 43582
diff changeset
  4311
                }
18381
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4312
            }
56a7f54f4166 8015432: javac crashes with stack overflow when method called recursively from nested generic call
mcimadamore
parents: 18379
diff changeset
  4313
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4314
            PolyKind pkind = (sym.type.hasTag(FORALL) &&
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4315
                 sym.type.getReturnType().containsAny(((ForAll)sym.type).tvars)) ?
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4316
                 PolyKind.POLY : PolyKind.STANDALONE;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4317
            TreeInfo.setPolyKind(env.tree, pkind);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4318
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4319
            return (resultInfo.pt == Infer.anyPoly) ?
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4320
                    owntype :
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4321
                    chk.checkMethod(owntype, sym, env, argtrees, argtypes, env.info.lastResolveVarargs(),
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 32164
diff changeset
  4322
                            resultInfo.checkContext.inferenceContext());
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4323
        } catch (Infer.InferenceException ex) {
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4324
            //invalid target type - propagate exception outwards or report error
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4325
            //depending on the current check context
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4326
            resultInfo.checkContext.report(env.tree.pos(), ex.getDiagnostic());
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4327
            return types.createErrorType(site);
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4328
        } catch (Resolve.InapplicableMethodException ex) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4329
            final JCDiagnostic diag = ex.getDiagnostic();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4330
            Resolve.InapplicableSymbolError errSym = rs.new InapplicableSymbolError(null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4331
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4332
                protected Pair<Symbol, JCDiagnostic> errCandidate() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
  4333
                    return new Pair<>(sym, diag);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4334
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4335
            };
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29553
diff changeset
  4336
            List<Type> argtypes2 = argtypes.map(
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4337
                    rs.new ResolveDeferredRecoveryMap(AttrMode.CHECK, sym, env.info.pendingResolutionPhase));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4338
            JCDiagnostic errDiag = errSym.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4339
                    env.tree, sym, site, sym.name, argtypes2, typeargtypes);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4340
            log.report(errDiag);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19656
diff changeset
  4341
            return types.createErrorType(site);
13438
83729994273a 7175911: Simplify error reporting API in Check.CheckContext interface
mcimadamore
parents: 12916
diff changeset
  4342
        }
12080
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  4343
    }
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 12078
diff changeset
  4344
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4345
    public void visitLiteral(JCLiteral tree) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4346
        result = check(tree, litType(tree.typetag).constType(tree.value),
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4347
                KindSelector.VAL, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4348
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4349
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
  4350
    /** Return the type of a literal with given type tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4351
     */
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4352
    Type litType(TypeTag tag) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4353
        return (tag == CLASS) ? syms.stringType : syms.typeOfTag[tag.ordinal()];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4354
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4355
06bc494ca11e Initial load
duke
parents:
diff changeset
  4356
    public void visitTypeIdent(JCPrimitiveTypeTree tree) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4357
        result = check(tree, syms.typeOfTag[tree.typetag.ordinal()], KindSelector.TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4358
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4359
06bc494ca11e Initial load
duke
parents:
diff changeset
  4360
    public void visitTypeArray(JCArrayTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4361
        Type etype = attribType(tree.elemtype, env);
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  4362
        Type type = new ArrayType(etype, syms.arrayClass);
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4363
        result = check(tree, type, KindSelector.TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4364
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4365
06bc494ca11e Initial load
duke
parents:
diff changeset
  4366
    /** Visitor method for parameterized types.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4367
     *  Bound checking is left until later, since types are attributed
06bc494ca11e Initial load
duke
parents:
diff changeset
  4368
     *  before supertype structure is completely known
06bc494ca11e Initial load
duke
parents:
diff changeset
  4369
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4370
    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
  4371
        Type owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4372
06bc494ca11e Initial load
duke
parents:
diff changeset
  4373
        // Attribute functor part of application and make sure it's a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4374
        Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4375
06bc494ca11e Initial load
duke
parents:
diff changeset
  4376
        // Attribute type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
  4377
        List<Type> actuals = attribTypes(tree.arguments, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4378
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4379
        if (clazztype.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4380
            List<Type> formals = clazztype.tsym.type.getTypeArguments();
10198
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  4381
            if (actuals.isEmpty()) //diamond
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  4382
                actuals = formals;
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  4383
84698d93792c 7046778: Project Coin: problem with diamond and member inner classes
mcimadamore
parents: 10187
diff changeset
  4384
            if (actuals.length() == formals.length()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4385
                List<Type> a = actuals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4386
                List<Type> f = formals;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4387
                while (a.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4388
                    a.head = a.head.withTypeVar(f.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4389
                    a = a.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4390
                    f = f.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4391
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4392
                // Compute the proper generic outer
06bc494ca11e Initial load
duke
parents:
diff changeset
  4393
                Type clazzOuter = clazztype.getEnclosingType();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4394
                if (clazzOuter.hasTag(CLASS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4395
                    Type site;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  4396
                    JCExpression clazz = TreeInfo.typeIn(tree.clazz);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4397
                    if (clazz.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4398
                        site = env.enclClass.sym.type;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4399
                    } else if (clazz.hasTag(SELECT)) {
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  4400
                        site = ((JCFieldAccess) clazz).selected.type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4401
                    } else throw new AssertionError(""+tree);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4402
                    if (clazzOuter.hasTag(CLASS) && site != clazzOuter) {
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4403
                        if (site.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4404
                            site = types.asOuterSuper(site, clazzOuter.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4405
                        if (site == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4406
                            site = types.erasure(clazzOuter);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4407
                        clazzOuter = site;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4408
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4409
                }
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24296
diff changeset
  4410
                owntype = new ClassType(clazzOuter, actuals, clazztype.tsym,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  4411
                                        clazztype.getMetadata());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4412
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4413
                if (formals.length() != 0) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4414
                    log.error(tree.pos(),
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4415
                              Errors.WrongNumberTypeArgs(Integer.toString(formals.length())));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4416
                } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4417
                    log.error(tree.pos(), Errors.TypeDoesntTakeParams(clazztype.tsym));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4418
                }
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
  4419
                owntype = types.createErrorType(tree.type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4420
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4421
        }
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4422
        result = check(tree, owntype, KindSelector.TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4423
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4424
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9079
diff changeset
  4425
    public void visitTypeUnion(JCTypeUnion tree) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  4426
        ListBuffer<Type> multicatchTypes = new ListBuffer<>();
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4427
        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
  4428
        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
  4429
            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
  4430
            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
  4431
                          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
  4432
                          syms.throwableType);
9074
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4433
            if (!ctype.isErroneous()) {
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9079
diff changeset
  4434
                //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
  4435
                //unrelated w.r.t. subtyping
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4436
                if (chk.intersects(ctype,  multicatchTypes.toList())) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4437
                    for (Type t : multicatchTypes) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4438
                        boolean sub = types.isSubtype(ctype, t);
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4439
                        boolean sup = types.isSubtype(t, ctype);
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4440
                        if (sub || sup) {
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4441
                            //assume 'a' <: 'b'
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4442
                            Type a = sub ? ctype : t;
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4443
                            Type b = sub ? t : ctype;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4444
                            log.error(typeTree.pos(), Errors.MulticatchTypesMustBeDisjoint(a, b));
9074
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4445
                        }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4446
                    }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4447
                }
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4448
                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
  4449
                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
  4450
                    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
  4451
            } else {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4452
                if (all_multicatchTypes == null) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19942
diff changeset
  4453
                    all_multicatchTypes = new ListBuffer<>();
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4454
                    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
  4455
                }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4456
                all_multicatchTypes.append(ctype);
9074
76b505d19026 7030606: Project-coin: multi-catch types should be pairwise disjoint
mcimadamore
parents: 8845
diff changeset
  4457
            }
7622
ee16e77d07f0 7002070: If catch clause has an incompatible type, error pointer points to first exception type in list
mcimadamore
parents: 7618
diff changeset
  4458
        }
30405
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  4459
        Type t = check(tree, types.lub(multicatchTypes.toList()),
3da6595b205f 8076279: Refactor Attr.check* methods to receive/handle a CheckMode enumeration
sadayapalam
parents: 30064
diff changeset
  4460
                KindSelector.TYP, resultInfo.dup(CheckMode.NO_TREE_UPDATE));
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4461
        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
  4462
            List<Type> alternatives =
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4463
                ((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
  4464
            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
  4465
        }
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 9303
diff changeset
  4466
        tree.type = result = t;
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5489
diff changeset
  4467
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5489
diff changeset
  4468
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4469
    public void visitTypeIntersection(JCTypeIntersection tree) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4470
        attribTypes(tree.bounds, env);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4471
        tree.type = result = checkIntersection(tree, tree.bounds);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4472
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4473
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4474
    public void visitTypeParameter(JCTypeParameter tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4475
        TypeVar typeVar = (TypeVar) tree.type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4476
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4477
        if (tree.annotations != null && tree.annotations.nonEmpty()) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4478
            annotate.annotateTypeParameterSecondStage(tree, tree.annotations);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4479
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4480
53515
a772e65727c5 8193367: Annotated type variable bounds crash javac
bsrbnd
parents: 53275
diff changeset
  4481
        if (!typeVar.getUpperBound().isErroneous()) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4482
            //fixup type-parameter bound computed in 'attribTypeVariables'
53515
a772e65727c5 8193367: Annotated type variable bounds crash javac
bsrbnd
parents: 53275
diff changeset
  4483
            typeVar.setUpperBound(checkIntersection(tree, tree.bounds));
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4484
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4485
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4486
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4487
    Type checkIntersection(JCTree tree, List<JCExpression> bounds) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22162
diff changeset
  4488
        Set<Type> boundSet = new HashSet<>();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4489
        if (bounds.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4490
            // accept class or interface or typevar as first bound.
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
  4491
            bounds.head.type = checkBase(bounds.head.type, bounds.head, env, false, false, false);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4492
            boundSet.add(types.erasure(bounds.head.type));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4493
            if (bounds.head.type.isErroneous()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4494
                return bounds.head.type;
1532
ae41c47516e5 6680106: StackOverFlowError for Cyclic inheritance in TypeParameters with ArrayType Bounds
mcimadamore
parents: 1528
diff changeset
  4495
            }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4496
            else if (bounds.head.type.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4497
                // 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
  4498
                if (bounds.tail.nonEmpty()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4499
                    log.error(bounds.tail.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4500
                              Errors.TypeVarMayNotBeFollowedByOtherBounds);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4501
                    return bounds.head.type;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4502
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4503
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4504
                // if first bound was a class or interface, accept only interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  4505
                // as further bounds.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4506
                for (JCExpression bound : bounds.tail) {
21712
d363f367d4c0 8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents: 21704
diff changeset
  4507
                    bound.type = checkBase(bound.type, bound, env, false, true, false);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4508
                    if (bound.type.isErroneous()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4509
                        bounds = List.of(bound);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4510
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4511
                    else if (bound.type.hasTag(CLASS)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4512
                        chk.checkNotRepeated(bound.pos(), types.erasure(bound.type), boundSet);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4513
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4514
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4515
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4516
        }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4517
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4518
        if (bounds.length() == 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4519
            return syms.objectType;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4520
        } else if (bounds.length() == 1) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4521
            return bounds.head.type;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4522
        } else {
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 29051
diff changeset
  4523
            Type owntype = types.makeIntersectionType(TreeInfo.types(bounds));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4524
            // ... the variable's bound is a class type flagged COMPOUND
06bc494ca11e Initial load
duke
parents:
diff changeset
  4525
            // (see comment for TypeVar.bound).
06bc494ca11e Initial load
duke
parents:
diff changeset
  4526
            // In this case, generate a class tree that represents the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4527
            // bound class, ...
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8622
diff changeset
  4528
            JCExpression extending;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4529
            List<JCExpression> implementing;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4530
            if (!bounds.head.type.isInterface()) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4531
                extending = bounds.head;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4532
                implementing = bounds.tail;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4533
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4534
                extending = null;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4535
                implementing = bounds;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4536
            }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4537
            JCClassDecl cd = make.at(tree).ClassDef(
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4538
                make.Modifiers(PUBLIC | ABSTRACT),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  4539
                names.empty, List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  4540
                extending, implementing, List.nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4541
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4542
            ClassSymbol c = (ClassSymbol)owntype.tsym;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4543
            Assert.check((c.flags() & COMPOUND) != 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4544
            cd.sym = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4545
            c.sourcefile = env.toplevel.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4546
06bc494ca11e Initial load
duke
parents:
diff changeset
  4547
            // ... and attribute the bound class
06bc494ca11e Initial load
duke
parents:
diff changeset
  4548
            c.flags_field |= UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4549
            Env<AttrContext> cenv = enter.classEnv(cd, env);
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
  4550
            typeEnvs.put(c, cenv);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4551
            attribClass(c);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4552
            return owntype;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4553
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4554
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4555
06bc494ca11e Initial load
duke
parents:
diff changeset
  4556
    public void visitWildcard(JCWildcard tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4557
        //- System.err.println("visitWildcard("+tree+");");//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
  4558
        Type type = (tree.kind.kind == BoundKind.UNBOUND)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4559
            ? syms.objectType
06bc494ca11e Initial load
duke
parents:
diff changeset
  4560
            : attribType(tree.inner, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4561
        result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type),
06bc494ca11e Initial load
duke
parents:
diff changeset
  4562
                                              tree.kind.kind,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26780
diff changeset
  4563
                                              syms.boundClass),
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4564
                KindSelector.TYP, resultInfo);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4565
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4566
06bc494ca11e Initial load
duke
parents:
diff changeset
  4567
    public void visitAnnotation(JCAnnotation tree) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4568
        Assert.error("should be handled in annotate");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4569
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4570
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4571
    public void visitAnnotatedType(JCAnnotatedType tree) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4572
        attribAnnotationTypes(tree.annotations, env);
31560
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  4573
        Type underlyingType = attribType(tree.underlyingType, env);
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4574
        Type annotatedType = underlyingType.annotatedType(Annotations.TO_BE_SET);
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4575
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4576
        if (!env.info.isNewClass)
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4577
            annotate.annotateTypeSecondStage(tree, tree.annotations, annotatedType);
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4578
        result = tree.type = annotatedType;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4579
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4580
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4581
    public void visitErroneous(JCErroneous tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4582
        if (tree.errs != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4583
            for (JCTree err : tree.errs)
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26991
diff changeset
  4584
                attribTree(err, env, new ResultInfo(KindSelector.ERR, pt()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4585
        result = tree.type = syms.errType;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4586
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4587
06bc494ca11e Initial load
duke
parents:
diff changeset
  4588
    /** Default visitor method for all other trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4589
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4590
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4591
        throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4592
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4593
8845
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4594
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4595
     * Attribute an env for either a top level tree or class or module declaration.
8845
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4596
     */
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4597
    public void attrib(Env<AttrContext> env) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4598
        switch (env.tree.getTag()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4599
            case MODULEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4600
                attribModule(env.tree.pos(), ((JCModuleDecl)env.tree).sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4601
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4602
            case TOPLEVEL:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4603
                attribTopLevel(env);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4604
                break;
40313
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4605
            case PACKAGEDEF:
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4606
                attribPackage(env.tree.pos(), ((JCPackageDecl) env.tree).packge);
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4607
                break;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4608
            default:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4609
                attribClass(env.tree.pos(), env.enclClass.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4610
        }
8845
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4611
    }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4612
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4613
    /**
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4614
     * Attribute a top level tree. These trees are encountered when the
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4615
     * package declaration has annotations.
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4616
     */
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4617
    public void attribTopLevel(Env<AttrContext> env) {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4618
        JCCompilationUnit toplevel = env.toplevel;
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4619
        try {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4620
            annotate.flush();
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4621
        } catch (CompletionFailure ex) {
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4622
            chk.completionError(toplevel.pos(), ex);
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4623
        }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4624
    }
42fb82dfbf52 6993311: annotations on packages are not validated
jjg
parents: 8635
diff changeset
  4625
40313
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4626
    public void attribPackage(DiagnosticPosition pos, PackageSymbol p) {
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4627
        try {
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4628
            annotate.flush();
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4629
            attribPackage(p);
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4630
        } catch (CompletionFailure ex) {
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4631
            chk.completionError(pos, ex);
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4632
        }
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4633
    }
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4634
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4635
    void attribPackage(PackageSymbol p) {
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4636
        Env<AttrContext> env = typeEnvs.get(p);
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4637
        chk.checkDeprecatedAnnotation(((JCPackageDecl) env.tree).pid.pos(), p);
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4638
    }
a85f92c9a8ab 8068626: Add javac lint warning when the @Deprecated annotation is used where it is a no-op
sadayapalam
parents: 39920
diff changeset
  4639
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4640
    public void attribModule(DiagnosticPosition pos, ModuleSymbol m) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4641
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4642
            annotate.flush();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4643
            attribModule(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4644
        } catch (CompletionFailure ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4645
            chk.completionError(pos, ex);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4646
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4647
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4648
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4649
    void attribModule(ModuleSymbol m) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4650
        // Get environment current at the point of module definition.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4651
        Env<AttrContext> env = enter.typeEnvs.get(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4652
        attribStat(env.tree, env);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4653
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4654
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4655
    /** Main method: attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4656
     *  reporting completion failures at the given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4657
     *  @param pos The source position at which completion errors are to be
06bc494ca11e Initial load
duke
parents:
diff changeset
  4658
     *             reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4659
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4660
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4661
    public void attribClass(DiagnosticPosition pos, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4662
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4663
            annotate.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4664
            attribClass(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4665
        } catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4666
            chk.completionError(pos, ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4667
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4668
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4669
06bc494ca11e Initial load
duke
parents:
diff changeset
  4670
    /** Attribute class definition associated with given class symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4671
     *  @param c   The class symbol whose definition will be attributed.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4672
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4673
    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
  4674
        if (c.type.hasTag(ERROR)) return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4675
06bc494ca11e Initial load
duke
parents:
diff changeset
  4676
        // Check for cycles in the inheritance graph, which can arise from
06bc494ca11e Initial load
duke
parents:
diff changeset
  4677
        // ill-formed class files.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4678
        chk.checkNonCyclic(null, c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4679
06bc494ca11e Initial load
duke
parents:
diff changeset
  4680
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4681
        if ((c.flags_field & Flags.COMPOUND) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4682
            // First, attribute superclass.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4683
            if (st.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4684
                attribClass((ClassSymbol)st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4685
06bc494ca11e Initial load
duke
parents:
diff changeset
  4686
            // 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
  4687
            if (c.owner.kind == TYP && c.owner.type.hasTag(CLASS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4688
                attribClass((ClassSymbol)c.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4689
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4690
06bc494ca11e Initial load
duke
parents:
diff changeset
  4691
        // The previous operations might have attributed the current class
06bc494ca11e Initial load
duke
parents:
diff changeset
  4692
        // if there was a cycle. So we test first whether the class is still
06bc494ca11e Initial load
duke
parents:
diff changeset
  4693
        // UNATTRIBUTED.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4694
        if ((c.flags_field & UNATTRIBUTED) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4695
            c.flags_field &= ~UNATTRIBUTED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4696
06bc494ca11e Initial load
duke
parents:
diff changeset
  4697
            // Get environment current at the point of class definition.
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
  4698
            Env<AttrContext> env = typeEnvs.get(c);
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
  4699
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24901
diff changeset
  4700
            // The info.lint field in the envs stored in typeEnvs is deliberately uninitialized,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4701
            // because the annotations were not available at the time the env was created. Therefore,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4702
            // we look up the environment chain for the first enclosing environment for which the
06bc494ca11e Initial load
duke
parents:
diff changeset
  4703
            // lint value is set. Typically, this is the parent env, but might be further if there
06bc494ca11e Initial load
duke
parents:
diff changeset
  4704
            // are any envs created as a result of TypeParameter nodes.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4705
            Env<AttrContext> lintEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4706
            while (lintEnv.info.lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4707
                lintEnv = lintEnv.next;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4708
06bc494ca11e Initial load
duke
parents:
diff changeset
  4709
            // 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
  4710
            env.info.lint = lintEnv.info.lint.augment(c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4711
06bc494ca11e Initial load
duke
parents:
diff changeset
  4712
            Lint prevLint = chk.setLint(env.info.lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4713
            JavaFileObject prev = log.useSource(c.sourcefile);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4714
            ResultInfo prevReturnRes = env.info.returnResult;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4715
06bc494ca11e Initial load
duke
parents:
diff changeset
  4716
            try {
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19933
diff changeset
  4717
                deferredLintHandler.flush(env.tree);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4718
                env.info.returnResult = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4719
                // java.lang.Enum may not be subclassed by a non-enum
06bc494ca11e Initial load
duke
parents:
diff changeset
  4720
                if (st.tsym == syms.enumSym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4721
                    ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4722
                    log.error(env.tree.pos(), Errors.EnumNoSubclassing);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4723
06bc494ca11e Initial load
duke
parents:
diff changeset
  4724
                // Enums may not be extended by source-level classes
06bc494ca11e Initial load
duke
parents:
diff changeset
  4725
                if (st.tsym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4726
                    ((st.tsym.flags_field & Flags.ENUM) != 0) &&
16558
07c08ad4a700 8010179: Remove transitional target values from javac
darcy
parents: 16557
diff changeset
  4727
                    ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4728
                    log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4729
                }
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4730
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4731
                if (isSerializable(c.type)) {
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4732
                    env.info.isSerializable = true;
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4733
                }
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4734
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4735
                attribClassBody(env, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4736
06bc494ca11e Initial load
duke
parents:
diff changeset
  4737
                chk.checkDeprecatedAnnotation(env.tree.pos(), c);
16333
a6e1ded87200 8009138: javac, equals-hashCode warning tuning
vromero
parents: 16331
diff changeset
  4738
                chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
21018
95d225149128 8014016: javac is too late detecting invalid annotation usage
jlahoda
parents: 21009
diff changeset
  4739
                chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
41444
b61c805c6173 8153362: Add javac -Xlint warning to list exposed types which are not accessible
jlahoda
parents: 41156
diff changeset
  4740
                chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4741
            } finally {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
  4742
                env.info.returnResult = prevReturnRes;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4743
                log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4744
                chk.setLint(prevLint);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4745
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4746
06bc494ca11e Initial load
duke
parents:
diff changeset
  4747
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4748
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4749
06bc494ca11e Initial load
duke
parents:
diff changeset
  4750
    public void visitImport(JCImport tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4751
        // nothing to do
06bc494ca11e Initial load
duke
parents:
diff changeset
  4752
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4753
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4754
    public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4755
        tree.sym.completeUsesProvides();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4756
        ModuleSymbol msym = tree.sym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4757
        Lint lint = env.outer.info.lint = env.outer.info.lint.augment(msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4758
        Lint prevLint = chk.setLint(lint);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42416
diff changeset
  4759
        chk.checkModuleName(tree);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4760
        chk.checkDeprecatedAnnotation(tree, msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4761
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4762
        try {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4763
            deferredLintHandler.flush(tree.pos());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4764
        } finally {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4765
            chk.setLint(prevLint);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  4766
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4767
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  4768
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4769
    /** Finish the attribution of a class. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4770
    private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4771
        JCClassDecl tree = (JCClassDecl)env.tree;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4772
        Assert.check(c == tree.sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4773
06bc494ca11e Initial load
duke
parents:
diff changeset
  4774
        // Validate type parameters, supertype and interfaces.
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14724
diff changeset
  4775
        attribStats(tree.typarams, env);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4776
        if (!c.isAnonymous()) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4777
            //already checked if anonymous
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4778
            chk.validate(tree.typarams, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4779
            chk.validate(tree.extending, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4780
            chk.validate(tree.implementing, env);
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  4781
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4782
28459
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28456
diff changeset
  4783
        c.markAbstractIfNeeded(types);
48a68a485760 8068517: Compiler may generate wrong InnerClasses attribute for static enum reference
jlahoda
parents: 28456
diff changeset
  4784
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4785
        // If this is a non-abstract class, check that it has no abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
  4786
        // methods or unimplemented methods of an implemented interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4787
        if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) {
36153
ed5063b304be 8149600: javac, remove unused options, step 2
vromero
parents: 35423
diff changeset
  4788
            chk.checkAllDefined(tree.pos(), c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4789
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4790
06bc494ca11e Initial load
duke
parents:
diff changeset
  4791
        if ((c.flags() & ANNOTATION) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4792
            if (tree.implementing.nonEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  4793
                log.error(tree.implementing.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4794
                          Errors.CantExtendIntfAnnotation);
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4795
            if (tree.typarams.nonEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4796
                log.error(tree.typarams.head.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4797
                          Errors.IntfAnnotationCantHaveTypeParams(c));
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4798
            }
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4799
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4800
            // If this annotation type has a @Repeatable, validate
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4801
            Attribute.Compound repeatable = c.getAnnotationTypeMetadata().getRepeatable();
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  4802
            // If this annotation type has a @Repeatable, validate
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4803
            if (repeatable != null) {
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4804
                // get diagnostic position for error reporting
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4805
                DiagnosticPosition cbPos = getDiagnosticPosition(tree, repeatable.type);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4806
                Assert.checkNonNull(cbPos);
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4807
15356
cf312dc54c60 8006119: update javac to follow latest spec for repeatable annotations
jjg
parents: 14953
diff changeset
  4808
                chk.validateRepeatable(c, repeatable, cbPos);
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4809
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4810
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4811
            // Check that all extended classes and interfaces
06bc494ca11e Initial load
duke
parents:
diff changeset
  4812
            // are compatible (i.e. no two define methods with same arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  4813
            // yet different return types).  (JLS 8.4.6.3)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4814
            chk.checkCompatibleSupertypes(tree.pos(), c.type);
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4815
            if (allowDefaultMethods) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4816
                chk.checkDefaultMethodClashes(tree.pos(), c.type);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
  4817
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4818
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4819
06bc494ca11e Initial load
duke
parents:
diff changeset
  4820
        // Check that class does not import the same parameterized interface
06bc494ca11e Initial load
duke
parents:
diff changeset
  4821
        // with two different argument lists.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4822
        chk.checkClassBounds(tree.pos(), c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4823
06bc494ca11e Initial load
duke
parents:
diff changeset
  4824
        tree.type = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4825
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4826
        for (List<JCTypeParameter> l = tree.typarams;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
  4827
             l.nonEmpty(); l = l.tail) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4828
             Assert.checkNonNull(env.info.scope.findFirst(l.head.name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4829
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4830
06bc494ca11e Initial load
duke
parents:
diff changeset
  4831
        // Check that a generic class doesn't extend Throwable
06bc494ca11e Initial load
duke
parents:
diff changeset
  4832
        if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType))
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4833
            log.error(tree.extending.pos(), Errors.GenericThrowable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4834
06bc494ca11e Initial load
duke
parents:
diff changeset
  4835
        // Check that all methods which implement some
06bc494ca11e Initial load
duke
parents:
diff changeset
  4836
        // method conform to the method they implement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4837
        chk.checkImplementations(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4838
9076
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  4839
        //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
  4840
        checkAutoCloseable(tree.pos(), env, c.type);
45c73da050e9 7027157: Project Coin: javac warnings for AutoCloseable.close throwing InterruptedException
mcimadamore
parents: 9075
diff changeset
  4841
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4842
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4843
            // Attribute declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  4844
            attribStat(l.head, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4845
            // Check that declarations in inner classes are not static (JLS 8.1.2)
06bc494ca11e Initial load
duke
parents:
diff changeset
  4846
            // Make an exception for static constants.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4847
            if (c.owner.kind != PCK &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4848
                ((c.flags() & STATIC) == 0 || c.name == names.empty) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  4849
                (TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4850
                Symbol sym = null;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10459
diff changeset
  4851
                if (l.head.hasTag(VARDEF)) sym = ((JCVariableDecl) l.head).sym;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4852
                if (sym == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  4853
                    sym.kind != VAR ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  4854
                    ((VarSymbol) sym).getConstValue() == null)
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4855
                    log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4856
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4857
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4858
06bc494ca11e Initial load
duke
parents:
diff changeset
  4859
        // Check for cycles among non-initial constructors.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4860
        chk.checkCyclicConstructors(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4861
06bc494ca11e Initial load
duke
parents:
diff changeset
  4862
        // Check for cycles among annotation elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4863
        chk.checkNonCyclicElements(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4864
06bc494ca11e Initial load
duke
parents:
diff changeset
  4865
        // Check for proper use of serialVersionUID
37634
a5f740ef93f2 7152104: javac should not warn about missing serialVersionUID for anonymous inner classes
alundblad
parents: 36713
diff changeset
  4866
        if (env.info.lint.isEnabled(LintCategory.SERIAL)
a5f740ef93f2 7152104: javac should not warn about missing serialVersionUID for anonymous inner classes
alundblad
parents: 36713
diff changeset
  4867
                && isSerializable(c.type)
48449
e2b8009bf42c 8191637: Interface with defaults invalid compiler warning for Serializable
jjg
parents: 48246
diff changeset
  4868
                && (c.flags() & (Flags.ENUM | Flags.INTERFACE)) == 0
e2b8009bf42c 8191637: Interface with defaults invalid compiler warning for Serializable
jjg
parents: 48246
diff changeset
  4869
                && !c.isAnonymous()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4870
            checkSerialVersionUID(tree, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4871
        }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4872
        if (allowTypeAnnos) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4873
            // Correctly organize the postions of the type annotations
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4874
            typeAnnotations.organizeTypeAnnotationsBodies(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4875
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4876
            // Check type annotations applicability rules
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4877
            validateTypeAnnotations(tree, false);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4878
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4879
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4880
        // where
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4881
        /** 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
  4882
        private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4883
            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
  4884
                if (types.isSameType(al.head.annotationType.type, t))
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4885
                    return al.head.pos();
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4886
            }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4887
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4888
            return null;
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4889
        }
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 13439
diff changeset
  4890
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4891
        /** check if a type is a subtype of Serializable, if that is available. */
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4892
        boolean isSerializable(Type t) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4893
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4894
                syms.serializableType.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4895
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4896
            catch (CompletionFailure e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4897
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4898
            }
24219
e7dc661cafae 8029102: Enhance compiler warnings for Lambda
vromero
parents: 24066
diff changeset
  4899
            return types.isSubtype(t, syms.serializableType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4900
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4901
06bc494ca11e Initial load
duke
parents:
diff changeset
  4902
        /** Check that an appropriate serialVersionUID member is defined. */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4903
        private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4904
06bc494ca11e Initial load
duke
parents:
diff changeset
  4905
            // check for presence of serialVersionUID
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4906
            VarSymbol svuid = null;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4907
            for (Symbol sym : c.members().getSymbolsByName(names.serialVersionUID)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4908
                if (sym.kind == VAR) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4909
                    svuid = (VarSymbol)sym;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4910
                    break;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4911
                }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4912
            }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4913
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25279
diff changeset
  4914
            if (svuid == null) {
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4915
                log.warning(LintCategory.SERIAL,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4916
                        tree.pos(), Warnings.MissingSVUID(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4917
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4918
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4919
06bc494ca11e Initial load
duke
parents:
diff changeset
  4920
            // check that it is static final
06bc494ca11e Initial load
duke
parents:
diff changeset
  4921
            if ((svuid.flags() & (STATIC | FINAL)) !=
06bc494ca11e Initial load
duke
parents:
diff changeset
  4922
                (STATIC | FINAL))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4923
                log.warning(LintCategory.SERIAL,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4924
                        TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.ImproperSVUID(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4925
06bc494ca11e Initial load
duke
parents:
diff changeset
  4926
            // check that it is long
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4927
            else if (!svuid.type.hasTag(LONG))
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4928
                log.warning(LintCategory.SERIAL,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4929
                        TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.LongSVUID(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4930
06bc494ca11e Initial load
duke
parents:
diff changeset
  4931
            // check constant
06bc494ca11e Initial load
duke
parents:
diff changeset
  4932
            else if (svuid.getConstValue() == null)
7643
a067a0cda531 6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents: 7628
diff changeset
  4933
                log.warning(LintCategory.SERIAL,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  4934
                        TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.ConstantSVUID(c));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4935
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4936
06bc494ca11e Initial load
duke
parents:
diff changeset
  4937
    private Type capture(Type type) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  4938
        return types.capture(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4939
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 3144
diff changeset
  4940
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4941
    private void setSyntheticVariableType(JCVariableDecl tree, Type type) {
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4942
        if (type.isErroneous()) {
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4943
            tree.vartype = make.at(Position.NOPOS).Erroneous();
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4944
        } else {
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4945
            tree.vartype = make.at(Position.NOPOS).Type(type);
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4946
        }
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4947
    }
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  4948
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4949
    public void validateTypeAnnotations(JCTree tree, boolean sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4950
        tree.accept(new TypeAnnotationsValidator(sigOnly));
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4951
    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4952
    //where
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4953
    private final class TypeAnnotationsValidator extends TreeScanner {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4954
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4955
        private final boolean sigOnly;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4956
        public TypeAnnotationsValidator(boolean sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4957
            this.sigOnly = sigOnly;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4958
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4959
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4960
        public void visitAnnotation(JCAnnotation tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4961
            chk.validateTypeAnnotation(tree, false);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4962
            super.visitAnnotation(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4963
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4964
        public void visitAnnotatedType(JCAnnotatedType tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4965
            if (!tree.underlyingType.type.isErroneous()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4966
                super.visitAnnotatedType(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4967
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4968
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4969
        public void visitTypeParameter(JCTypeParameter tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4970
            chk.validateTypeAnnotations(tree.annotations, true);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4971
            scan(tree.bounds);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4972
            // Don't call super.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4973
            // This is needed because above we call validateTypeAnnotation with
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4974
            // false, which would forbid annotations on type parameters.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4975
            // super.visitTypeParameter(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4976
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4977
        public void visitMethodDef(JCMethodDecl tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4978
            if (tree.recvparam != null &&
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4979
                    !tree.recvparam.vartype.type.isErroneous()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4980
                checkForDeclarationAnnotations(tree.recvparam.mods.annotations,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4981
                        tree.recvparam.vartype.type.tsym);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4982
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4983
            if (tree.restype != null && tree.restype.type != null) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4984
                validateAnnotatedType(tree.restype, tree.restype.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4985
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4986
            if (sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4987
                scan(tree.mods);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4988
                scan(tree.restype);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4989
                scan(tree.typarams);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4990
                scan(tree.recvparam);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4991
                scan(tree.params);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4992
                scan(tree.thrown);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4993
            } else {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4994
                scan(tree.defaultValue);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4995
                scan(tree.body);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4996
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4997
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4998
        public void visitVarDef(final JCVariableDecl tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  4999
            //System.err.println("validateTypeAnnotations.visitVarDef " + tree);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  5000
            if (tree.sym != null && tree.sym.type != null && !tree.isImplicitlyTyped())
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5001
                validateAnnotatedType(tree.vartype, tree.sym.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5002
            scan(tree.mods);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5003
            scan(tree.vartype);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5004
            if (!sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5005
                scan(tree.init);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5006
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5007
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5008
        public void visitTypeCast(JCTypeCast tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5009
            if (tree.clazz != null && tree.clazz.type != null)
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5010
                validateAnnotatedType(tree.clazz, tree.clazz.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5011
            super.visitTypeCast(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5012
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5013
        public void visitTypeTest(JCInstanceOf tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5014
            if (tree.clazz != null && tree.clazz.type != null)
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5015
                validateAnnotatedType(tree.clazz, tree.clazz.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5016
            super.visitTypeTest(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5017
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5018
        public void visitNewClass(JCNewClass tree) {
27851
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5019
            if (tree.clazz != null && tree.clazz.type != null) {
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5020
                if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5021
                    checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5022
                            tree.clazz.type.tsym);
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5023
                }
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5024
                if (tree.def != null) {
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5025
                    checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5026
                }
5f28577d20f6 8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
jfranck
parents: 27845
diff changeset
  5027
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5028
                validateAnnotatedType(tree.clazz, tree.clazz.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5029
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5030
            super.visitNewClass(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5031
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5032
        public void visitNewArray(JCNewArray tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5033
            if (tree.elemtype != null && tree.elemtype.type != null) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5034
                if (tree.elemtype.hasTag(ANNOTATED_TYPE)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5035
                    checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5036
                            tree.elemtype.type.tsym);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5037
                }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5038
                validateAnnotatedType(tree.elemtype, tree.elemtype.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5039
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5040
            super.visitNewArray(tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5041
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5042
        public void visitClassDef(JCClassDecl tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5043
            //System.err.println("validateTypeAnnotations.visitClassDef " + tree);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5044
            if (sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5045
                scan(tree.mods);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5046
                scan(tree.typarams);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5047
                scan(tree.extending);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5048
                scan(tree.implementing);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5049
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5050
            for (JCTree member : tree.defs) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5051
                if (member.hasTag(Tag.CLASSDEF)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5052
                    continue;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5053
                }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5054
                scan(member);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5055
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5056
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5057
        public void visitBlock(JCBlock tree) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5058
            if (!sigOnly) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5059
                scan(tree.stats);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5060
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5061
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5062
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5063
        /* I would want to model this after
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5064
         * com.sun.tools.javac.comp.Check.Validator.visitSelectInternal(JCFieldAccess)
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5065
         * and override visitSelect and visitTypeApply.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5066
         * However, we only set the annotated type in the top-level type
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5067
         * of the symbol.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5068
         * Therefore, we need to override each individual location where a type
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5069
         * can occur.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5070
         */
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5071
        private void validateAnnotatedType(final JCTree errtree, final Type type) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5072
            //System.err.println("Attr.validateAnnotatedType: " + errtree + " type: " + type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5073
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5074
            if (type.isPrimitiveOrVoid()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5075
                return;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5076
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5077
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5078
            JCTree enclTr = errtree;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5079
            Type enclTy = type;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5080
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5081
            boolean repeat = true;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5082
            while (repeat) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5083
                if (enclTr.hasTag(TYPEAPPLY)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5084
                    List<Type> tyargs = enclTy.getTypeArguments();
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5085
                    List<JCExpression> trargs = ((JCTypeApply)enclTr).getTypeArguments();
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5086
                    if (trargs.length() > 0) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5087
                        // Nothing to do for diamonds
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5088
                        if (tyargs.length() == trargs.length()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5089
                            for (int i = 0; i < tyargs.length(); ++i) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5090
                                validateAnnotatedType(trargs.get(i), tyargs.get(i));
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5091
                            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5092
                        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5093
                        // If the lengths don't match, it's either a diamond
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5094
                        // or some nested type that redundantly provides
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5095
                        // type arguments in the tree.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5096
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5097
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5098
                    // Look at the clazz part of a generic type
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5099
                    enclTr = ((JCTree.JCTypeApply)enclTr).clazz;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5100
                }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5101
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5102
                if (enclTr.hasTag(SELECT)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5103
                    enclTr = ((JCTree.JCFieldAccess)enclTr).getExpression();
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5104
                    if (enclTy != null &&
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5105
                            !enclTy.hasTag(NONE)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5106
                        enclTy = enclTy.getEnclosingType();
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5107
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5108
                } else if (enclTr.hasTag(ANNOTATED_TYPE)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5109
                    JCAnnotatedType at = (JCTree.JCAnnotatedType) enclTr;
31560
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5110
                    if (enclTy == null || enclTy.hasTag(NONE)) {
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5111
                        if (at.getAnnotations().size() == 1) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  5112
                            log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping1(at.getAnnotations().head.attribute));
31560
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5113
                        } else {
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5114
                            ListBuffer<Attribute.Compound> comps = new ListBuffer<>();
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5115
                            for (JCAnnotation an : at.getAnnotations()) {
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5116
                                comps.add(an.attribute);
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5117
                            }
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44571
diff changeset
  5118
                            log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping(comps.toList()));
31560
3a71b9271010 8130745: Revert fix pushed for JDK-8074346
sadayapalam
parents: 31212
diff changeset
  5119
                        }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5120
                        repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5121
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5122
                    enclTr = at.underlyingType;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5123
                    // enclTy doesn't need to be changed
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5124
                } else if (enclTr.hasTag(IDENT)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5125
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5126
                } else if (enclTr.hasTag(JCTree.Tag.WILDCARD)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5127
                    JCWildcard wc = (JCWildcard) enclTr;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  5128
                    if (wc.getKind() == JCTree.Kind.EXTENDS_WILDCARD ||
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  5129
                            wc.getKind() == JCTree.Kind.SUPER_WILDCARD) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  5130
                        validateAnnotatedType(wc.getBound(), wc.getBound().type);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5131
                    } else {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5132
                        // Nothing to do for UNBOUND
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5133
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5134
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5135
                } else if (enclTr.hasTag(TYPEARRAY)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5136
                    JCArrayTypeTree art = (JCArrayTypeTree) enclTr;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47248
diff changeset
  5137
                    validateAnnotatedType(art.getType(), art.elemtype.type);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5138
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5139
                } else if (enclTr.hasTag(TYPEUNION)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5140
                    JCTypeUnion ut = (JCTypeUnion) enclTr;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5141
                    for (JCTree t : ut.getTypeAlternatives()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5142
                        validateAnnotatedType(t, t.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5143
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5144
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5145
                } else if (enclTr.hasTag(TYPEINTERSECTION)) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5146
                    JCTypeIntersection it = (JCTypeIntersection) enclTr;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5147
                    for (JCTree t : it.getBounds()) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5148
                        validateAnnotatedType(t, t.type);
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5149
                    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5150
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5151
                } else if (enclTr.getKind() == JCTree.Kind.PRIMITIVE_TYPE ||
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5152
                           enclTr.getKind() == JCTree.Kind.ERRONEOUS) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5153
                    repeat = false;
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5154
                } else {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5155
                    Assert.error("Unexpected tree: " + enclTr + " with kind: " + enclTr.getKind() +
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5156
                            " within: "+ errtree + " with kind: " + errtree.getKind());
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5157
                }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5158
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5159
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5160
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5161
        private void checkForDeclarationAnnotations(List<? extends JCAnnotation> annotations,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5162
                Symbol sym) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5163
            // Ensure that no declaration annotations are present.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5164
            // Note that a tree type might be an AnnotatedType with
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5165
            // empty annotations, if only declaration annotations were given.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5166
            // This method will raise an error for such a type.
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5167
            for (JCAnnotation ai : annotations) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5168
                if (!ai.type.isErroneous() &&
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
  5169
                        typeAnnotations.annotationTargetType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) {
29959
61bfc66866da 8073534: Confusing / incorrect error message regarding annotations on non-declarations
alundblad
parents: 29842
diff changeset
  5170
                    log.error(ai.pos(), Errors.AnnotationTypeNotApplicableToType(ai.type));
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5171
                }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5172
            }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5173
        }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5174
    }
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
  5175
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5176
    // <editor-fold desc="post-attribution visitor">
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5177
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5178
    /**
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5179
     * 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
  5180
     * 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
  5181
     * 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
  5182
     * 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
  5183
     * prevents NPE to be progagated during subsequent compilation steps.
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5184
     */
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  5185
    public void postAttr(JCTree tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  5186
        new PostAttrAnalyzer().scan(tree);
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5187
    }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5188
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5189
    class PostAttrAnalyzer extends TreeScanner {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5190
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5191
        private void initTypeIfNeeded(JCTree that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5192
            if (that.type == null) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5193
                if (that.hasTag(METHODDEF)) {
24609
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5194
                    that.type = dummyMethodType((JCMethodDecl)that);
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5195
                } else {
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5196
                    that.type = syms.unknownType;
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5197
                }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5198
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5199
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5200
24609
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5201
        /* Construct a dummy method type. If we have a method declaration,
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5202
         * and the declared return type is void, then use that return type
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5203
         * instead of UNKNOWN to avoid spurious error messages in lambda
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5204
         * bodies (see:JDK-8041704).
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5205
         */
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5206
        private Type dummyMethodType(JCMethodDecl md) {
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5207
            Type restype = syms.unknownType;
48931
b25eb74ec283 8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
jlahoda
parents: 48721
diff changeset
  5208
            if (md != null && md.restype != null && md.restype.hasTag(TYPEIDENT)) {
24609
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5209
                JCPrimitiveTypeTree prim = (JCPrimitiveTypeTree)md.restype;
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5210
                if (prim.typetag == VOID)
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5211
                    restype = syms.voidType;
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5212
            }
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  5213
            return new MethodType(List.nil(), restype,
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  5214
                                  List.nil(), syms.methodClass);
24609
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5215
        }
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5216
        private Type dummyMethodType() {
24609
5139d892e270 8041704: wrong error message when mixing lambda expression and inner class
pgovereau
parents: 24401
diff changeset
  5217
            return dummyMethodType(null);
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5218
        }
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5219
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5220
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5221
        public void scan(JCTree tree) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5222
            if (tree == null) return;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5223
            if (tree instanceof JCExpression) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5224
                initTypeIfNeeded(tree);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5225
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5226
            super.scan(tree);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5227
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5228
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5229
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5230
        public void visitIdent(JCIdent that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5231
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5232
                that.sym = syms.unknownSymbol;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5233
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5234
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5235
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5236
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5237
        public void visitSelect(JCFieldAccess that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5238
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5239
                that.sym = syms.unknownSymbol;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5240
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5241
            super.visitSelect(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5242
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5243
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5244
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5245
        public void visitClassDef(JCClassDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5246
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5247
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5248
                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
  5249
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5250
            super.visitClassDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5251
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5252
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5253
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5254
        public void visitMethodDef(JCMethodDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5255
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5256
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5257
                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
  5258
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5259
            super.visitMethodDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5260
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5261
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5262
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5263
        public void visitVarDef(JCVariableDecl that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5264
            initTypeIfNeeded(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5265
            if (that.sym == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5266
                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
  5267
                that.sym.adr = 0;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5268
            }
44060
f420de7e26fa 8175790: field JCVariableDecl.vartype can't be null after post attribution analysis
vromero
parents: 43761
diff changeset
  5269
            if (that.vartype == null) {
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47315
diff changeset
  5270
                that.vartype = make.at(Position.NOPOS).Erroneous();
44060
f420de7e26fa 8175790: field JCVariableDecl.vartype can't be null after post attribution analysis
vromero
parents: 43761
diff changeset
  5271
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5272
            super.visitVarDef(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5273
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5274
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5275
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5276
        public void visitNewClass(JCNewClass that) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5277
            if (that.constructor == null) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5278
                that.constructor = new MethodSymbol(0, names.init,
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5279
                        dummyMethodType(), syms.noSymbol);
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5280
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5281
            if (that.constructorType == null) {
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5282
                that.constructorType = syms.unknownType;
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5283
            }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5284
            super.visitNewClass(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5285
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5286
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5287
        @Override
10187
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  5288
        public void visitAssignop(JCAssignOp that) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5289
            if (that.operator == null) {
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5290
                that.operator = new OperatorSymbol(names.empty, dummyMethodType(),
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5291
                        -1, syms.noSymbol);
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5292
            }
10187
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  5293
            super.visitAssignop(that);
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  5294
        }
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  5295
983f0e987540 7060926: Attr.PostAttrAnalyzer misses a case
jjg
parents: 9724
diff changeset
  5296
        @Override
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5297
        public void visitBinary(JCBinary that) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5298
            if (that.operator == null) {
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5299
                that.operator = new OperatorSymbol(names.empty, dummyMethodType(),
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5300
                        -1, syms.noSymbol);
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5301
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5302
            super.visitBinary(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5303
        }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5304
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5305
        @Override
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5306
        public void visitUnary(JCUnary that) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5307
            if (that.operator == null) {
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5308
                that.operator = new OperatorSymbol(names.empty, dummyMethodType(),
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5309
                        -1, syms.noSymbol);
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5310
            }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5311
            super.visitUnary(that);
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5312
        }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5313
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5314
        @Override
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5315
        public void visitReference(JCMemberReference that) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5316
            super.visitReference(that);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5317
            if (that.sym == null) {
22685
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5318
                that.sym = new MethodSymbol(0, names.empty, dummyMethodType(),
c8d0807378b6 8030816: javac crashes when mixing lambdas and inner classes
vromero
parents: 22440
diff changeset
  5319
                        syms.noSymbol);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5320
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  5321
        }
6594
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5322
    }
d43f068fba19 6970584: Flow.java should be more error-friendly
mcimadamore
parents: 6592
diff changeset
  5323
    // </editor-fold>
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5324
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5325
    public void setPackageSymbols(JCExpression pid, Symbol pkg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5326
        new TreeScanner() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5327
            Symbol packge = pkg;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5328
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5329
            public void visitIdent(JCIdent that) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5330
                that.sym = packge;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5331
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5332
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5333
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5334
            public void visitSelect(JCFieldAccess that) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5335
                that.sym = packge;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5336
                packge = packge.owner;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5337
                super.visitSelect(that);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5338
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5339
        }.scan(pid);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5340
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36153
diff changeset
  5341
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  5342
}