src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java
author mcimadamore
Mon, 04 Dec 2017 17:54:49 +0000
changeset 48054 702043a4cdeb
parent 47350 d65c3b21081c
child 50374 2d0647b9ac18
permissions -rw-r--r--
8189749: Devise strategy for making source level checks more uniform Summary: Create a 'feature' enum which is responsible for handling source version checks and related diagnostic generation Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43137
9a96f02a6293 8171332: NPE in MembersPhase.finishClass
vromero
parents: 42828
diff changeset
     2
 * Copyright (c) 2003, 2017, 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: 4877
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: 4877
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: 4877
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
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
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
    28
import java.util.HashSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Set;
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
    30
import java.util.function.BiConsumer;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
    31
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.*;
27854
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
    35
import com.sun.tools.javac.code.Lint.LintCategory;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
    36
import com.sun.tools.javac.code.Scope.ImportFilter;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
    37
import com.sun.tools.javac.code.Scope.NamedImportScope;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
    38
import com.sun.tools.javac.code.Scope.StarImportScope;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
    39
import com.sun.tools.javac.code.Scope.WriteableScope;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47350
diff changeset
    40
import com.sun.tools.javac.code.Source.Feature;
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
    41
import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    44
import com.sun.tools.javac.util.DefinedBy.Api;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24604
diff changeset
    46
import com.sun.tools.javac.code.Symbol.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.code.Type.*;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
    48
import com.sun.tools.javac.resources.CompilerProperties.Errors;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9082
diff changeset
    52
import static com.sun.tools.javac.code.Flags.ANNOTATION;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
    53
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
    54
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: 14259
diff changeset
    55
import static com.sun.tools.javac.code.TypeTag.CLASS;
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
    56
import static com.sun.tools.javac.code.TypeTag.ERROR;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
    57
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9082
diff changeset
    58
import static com.sun.tools.javac.tree.JCTree.Tag.*;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24396
diff changeset
    59
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    60
import com.sun.tools.javac.util.Dependencies.CompletionCause;
11709
0d56d3fc22e6 7129225: javac fails to run annotation processors when star import of package of gensrc
jjh
parents: 10950
diff changeset
    61
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
/** This is the second phase of Enter, in which classes are completed
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    65
 *  by resolving their headers and entering their members in the into
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    66
 *  the class scope. See Enter for an overall overview.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    67
 *
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    68
 *  This class uses internal phases to process the classes. When a phase
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    69
 *  processes classes, the lower phases are not invoked until all classes
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    70
 *  pass through the current phase. Note that it is possible that upper phases
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    71
 *  are run due to recursive completion. The internal phases are:
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    72
 *  - ImportPhase: shallow pass through imports, adds information about imports
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    73
 *                 the NamedImportScope and StarImportScope, but avoids queries
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    74
 *                 about class hierarchy.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    75
 *  - HierarchyPhase: resolves the supertypes of the given class. Does not handle
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    76
 *                    type parameters of the class or type argument of the supertypes.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    77
 *  - HeaderPhase: finishes analysis of the header of the given class by resolving
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    78
 *                 type parameters, attributing supertypes including type arguments
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    79
 *                 and scheduling full annotation attribution. This phase also adds
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    80
 *                 a synthetic default constructor if needed and synthetic "this" field.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    81
 *  - MembersPhase: resolves headers for fields, methods and constructors in the given class.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    82
 *                  Also generates synthetic enum members.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    84
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    85
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 */
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    89
public class TypeEnter implements Completer {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    90
    protected static final Context.Key<TypeEnter> typeEnterKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    /** A switch to determine whether we check for package/class conflicts
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    final static boolean checkClash = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
    96
    private final Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    private final Enter enter;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    98
    private final MemberEnter memberEnter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    private final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    private final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    private final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    private final TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private final Todo todo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    private final Annotate annotate;
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   106
    private final TypeAnnotations typeAnnotations;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    private final Types types;
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   108
    private final JCDiagnostic.Factory diags;
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   109
    private final DeferredLintHandler deferredLintHandler;
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19941
diff changeset
   110
    private final Lint lint;
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24903
diff changeset
   111
    private final TypeEnvs typeEnvs;
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25443
diff changeset
   112
    private final Dependencies dependencies;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   114
    public static TypeEnter instance(Context context) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   115
        TypeEnter instance = context.get(typeEnterKey);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        if (instance == null)
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   117
            instance = new TypeEnter(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   121
    protected TypeEnter(Context context) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   122
        context.put(typeEnterKey, this);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1045
diff changeset
   123
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        enter = Enter.instance(context);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   125
        memberEnter = MemberEnter.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        todo = Todo.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        annotate = Annotate.instance(context);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   133
        typeAnnotations = TypeAnnotations.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        types = Types.instance(context);
1040
c0f5acfd9d15 6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents: 939
diff changeset
   135
        diags = JCDiagnostic.Factory.instance(context);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   136
        deferredLintHandler = DeferredLintHandler.instance(context);
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19941
diff changeset
   137
        lint = Lint.instance(context);
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24903
diff changeset
   138
        typeEnvs = TypeEnvs.instance(context);
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25443
diff changeset
   139
        dependencies = Dependencies.instance(context);
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   140
        Source source = Source.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47350
diff changeset
   141
        allowTypeAnnos = Feature.TYPE_ANNOTATIONS.allowedInSource(source);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47350
diff changeset
   142
        allowDeprecationOnImport = Feature.DEPRECATION_ON_IMPORT.allowedInSource(source);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18389
diff changeset
   145
    /** Switch: support type annotations.
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18389
diff changeset
   146
     */
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18389
diff changeset
   147
    boolean allowTypeAnnos;
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 18389
diff changeset
   148
27854
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
   149
    /**
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
   150
     * Switch: should deprecation warnings be issued on import
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
   151
     */
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
   152
    boolean allowDeprecationOnImport;
22b4bfc4e22f 8032211: Don't issue deprecation warnings on import statements
jlahoda
parents: 27231
diff changeset
   153
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    /** A flag to disable completion from time to time during member
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     *  enter, as we only need to look up types.  This avoids
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     *  unnecessarily deep recursion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    boolean completionEnabled = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   160
    /* Verify Imports:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   162
    protected void ensureImportsChecked(List<JCCompilationUnit> trees) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   163
        // if there remain any unimported toplevels (these must have
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   164
        // no classes at all), process their import statements as well.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   165
        for (JCCompilationUnit tree : trees) {
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   166
            if (!tree.starImportScope.isFilled()) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   167
                Env<AttrContext> topEnv = enter.topLevelEnv(tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   168
                finishImports(tree, () -> { completeClass.resolveImports(tree, topEnv); });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            }
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   170
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
 * Source completer
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    /** Complete entering a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     *  @param sym         The symbol of the class to be completed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
41637
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 39371
diff changeset
   180
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public void complete(Symbol sym) throws CompletionFailure {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        // Suppress some (recursive) MemberEnter invocations
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        if (!completionEnabled) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            // Re-install same completer for next time around and return.
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   185
            Assert.check((sym.flags() & Flags.COMPOUND) == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            sym.completer = this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   190
        try {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   191
            annotate.blockAnnotations();
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   192
            sym.flags_field |= UNATTRIBUTED;
1043
11ea4773b0af 6695838: javac does not detect cyclic inheritance involving static inner classes after import clause
mcimadamore
parents: 1040
diff changeset
   193
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   194
            List<Env<AttrContext>> queue;
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   195
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   196
            dependencies.push((ClassSymbol) sym, CompletionCause.MEMBER_ENTER);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   197
            try {
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   198
                queue = completeClass.completeEnvs(List.of(typeEnvs.get((ClassSymbol) sym)));
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   199
            } finally {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   200
                dependencies.pop();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   203
            if (!queue.isEmpty()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   204
                Set<JCCompilationUnit> seen = new HashSet<>();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   205
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   206
                for (Env<AttrContext> env : queue) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   207
                    if (env.toplevel.defs.contains(env.enclClass) && seen.add(env.toplevel)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   208
                        finishImports(env.toplevel, () -> {});
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                }
14369
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 14359
diff changeset
   211
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        } finally {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   213
            annotate.unblockAnnotations();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   214
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   215
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   217
    void finishImports(JCCompilationUnit toplevel, Runnable resolve) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   218
        JavaFileObject prev = log.useSource(toplevel.sourcefile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        try {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   220
            resolve.run();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   221
            chk.checkImportsUnique(toplevel);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   222
            chk.checkImportsResolvable(toplevel);
31299
81ce5febcae8 4869999: Error on import statement naming package containing no class files
sadayapalam
parents: 30066
diff changeset
   223
            chk.checkImportedPackagesObservable(toplevel);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   224
            toplevel.namedImportScope.finalizeScope();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   225
            toplevel.starImportScope.finalizeScope();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   231
    abstract class Phase {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   232
        private final ListBuffer<Env<AttrContext>> queue = new ListBuffer<>();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   233
        private final Phase next;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   234
        private final CompletionCause phaseName;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   235
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   236
        Phase(CompletionCause phaseName, Phase next) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   237
            this.phaseName = phaseName;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   238
            this.next = next;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   239
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   240
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   241
        public final List<Env<AttrContext>> completeEnvs(List<Env<AttrContext>> envs) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   242
            boolean firstToComplete = queue.isEmpty();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   243
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   244
            Phase prevTopLevelPhase = topLevelPhase;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   245
            boolean success = false;
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   246
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   247
            try {
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   248
                topLevelPhase = this;
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   249
                doCompleteEnvs(envs);
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   250
                success = true;
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   251
            } finally {
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   252
                topLevelPhase = prevTopLevelPhase;
47350
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   253
                if (!success && firstToComplete) {
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   254
                    //an exception was thrown, e.g. BreakAttr:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   255
                    //the queue would become stale, clear it:
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   256
                    queue.clear();
d65c3b21081c 8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents: 47248
diff changeset
   257
                }
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   258
            }
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   259
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   260
            if (firstToComplete) {
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   261
                List<Env<AttrContext>> out = queue.toList();
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   262
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   263
                queue.clear();
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   264
                return next != null ? next.completeEnvs(out) : out;
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   265
            } else {
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   266
                return List.nil();
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   267
            }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   268
        }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   269
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   270
        protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   271
            for (Env<AttrContext> env : envs) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   272
                JCClassDecl tree = (JCClassDecl)env.tree;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   273
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   274
                queue.add(env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   275
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   276
                JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   277
                DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   278
                try {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   279
                    dependencies.push(env.enclClass.sym, phaseName);
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   280
                    runPhase(env);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   281
                } catch (CompletionFailure ex) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   282
                    chk.completionError(tree.pos(), ex);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   283
                } finally {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   284
                    dependencies.pop();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   285
                    deferredLintHandler.setPos(prevLintPos);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   286
                    log.useSource(prev);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   287
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   288
            }
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   289
        }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   290
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   291
        protected abstract void runPhase(Env<AttrContext> env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   294
    private final ImportsPhase completeClass = new ImportsPhase();
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   295
    private Phase topLevelPhase;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   296
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   297
    /**Analyze import clauses.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   298
     */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   299
    private final class ImportsPhase extends Phase {
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   300
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   301
        public ImportsPhase() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   302
            super(CompletionCause.IMPORTS_PHASE, new HierarchyPhase());
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   303
        }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   304
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   305
        Env<AttrContext> env;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   306
        ImportFilter staticImportFilter;
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   307
        ImportFilter typeImportFilter;
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   308
        BiConsumer<JCImport, CompletionFailure> cfHandler =
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   309
                (imp, cf) -> chk.completionError(imp.pos(), cf);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   310
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   311
        @Override
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   312
        protected void runPhase(Env<AttrContext> env) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   313
            JCClassDecl tree = env.enclClass;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   314
            ClassSymbol sym = tree.sym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   315
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   316
            // If sym is a toplevel-class, make sure any import
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   317
            // clauses in its source file have been seen.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   318
            if (sym.owner.kind == PCK) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   319
                resolveImports(env.toplevel, env.enclosing(TOPLEVEL));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   320
                todo.append(env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   321
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   322
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   323
            if (sym.owner.kind == TYP)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   324
                sym.owner.complete();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   325
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   326
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   327
        private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) {
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   328
            if (tree.starImportScope.isFilled()) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   329
                // we must have already processed this toplevel
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   330
                return;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   331
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   332
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   333
            ImportFilter prevStaticImportFilter = staticImportFilter;
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   334
            ImportFilter prevTypeImportFilter = typeImportFilter;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   335
            DiagnosticPosition prevLintPos = deferredLintHandler.immediate();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   336
            Lint prevLint = chk.setLint(lint);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   337
            Env<AttrContext> prevEnv = this.env;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   338
            try {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   339
                this.env = env;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   340
                final PackageSymbol packge = env.toplevel.packge;
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   341
                this.staticImportFilter =
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   342
                        (origin, sym) -> sym.isStatic() &&
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   343
                                         chk.importAccessible(sym, packge) &&
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   344
                                         sym.isMemberOf((TypeSymbol) origin.owner, types);
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   345
                this.typeImportFilter =
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   346
                        (origin, sym) -> sym.kind == TYP &&
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   347
                                         chk.importAccessible(sym, packge);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   348
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   349
                // Import-on-demand java.lang.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   350
                PackageSymbol javaLang = syms.enterPackage(syms.java_base, names.java_lang);
31299
81ce5febcae8 4869999: Error on import statement naming package containing no class files
sadayapalam
parents: 30066
diff changeset
   351
                if (javaLang.members().isEmpty() && !javaLang.exists())
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
   352
                    throw new FatalError(diags.fragment(Fragments.FatalErrNoJavaLang));
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   353
                importAll(make.at(tree.pos()).Import(make.QualIdent(javaLang), false), javaLang, env);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   354
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   355
                JCModuleDecl decl = tree.getModuleDecl();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   356
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   357
                // Process the package def and all import clauses.
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   358
                if (tree.getPackage() != null && decl == null)
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   359
                    checkClassPackageClash(tree.getPackage());
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   360
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   361
                for (JCImport imp : tree.getImports()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   362
                    doImport(imp);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   363
                }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   364
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   365
                if (decl != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   366
                    //check @Deprecated:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   367
                    markDeprecated(decl.sym, decl.mods.annotations, env);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   368
                    // process module annotations
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   369
                    annotate.annotateLater(decl.mods.annotations, env, env.toplevel.modle, null);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   370
                }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   371
            } finally {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   372
                this.env = prevEnv;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   373
                chk.setLint(prevLint);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   374
                deferredLintHandler.setPos(prevLintPos);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   375
                this.staticImportFilter = prevStaticImportFilter;
29149
3fa94aad0264 8067886: Inaccessible nested classes can be incorrectly imported
jlahoda
parents: 27857
diff changeset
   376
                this.typeImportFilter = prevTypeImportFilter;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   377
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   378
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   379
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   380
        private void checkClassPackageClash(JCPackageDecl tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   381
            // check that no class exists with same fully qualified name as
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   382
            // toplevel package
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   383
            if (checkClash && tree.pid != null) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   384
                Symbol p = env.toplevel.packge;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   385
                while (p.owner != syms.rootPackage) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   386
                    p.owner.complete(); // enter all class members of p
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   387
                    //need to lookup the owning module/package:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   388
                    PackageSymbol pack = syms.lookupPackage(env.toplevel.modle, p.owner.getQualifiedName());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   389
                    if (syms.getClass(pack.modle, p.getQualifiedName()) != null) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   390
                        log.error(tree.pos,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
   391
                                  Errors.PkgClashesWithClassOfSameName(p));
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   392
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   393
                    p = p.owner;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   394
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   395
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   396
            // process package annotations
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   397
            annotate.annotateLater(tree.annotations, env, env.toplevel.packge, null);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   398
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   399
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   400
        private void doImport(JCImport tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   401
            JCFieldAccess imp = (JCFieldAccess)tree.qualid;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   402
            Name name = TreeInfo.name(imp);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   403
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   404
            // Create a local environment pointing to this tree to disable
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   405
            // effects of other imports in Resolve.findGlobalType
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   406
            Env<AttrContext> localEnv = env.dup(tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   407
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   408
            TypeSymbol p = attr.attribImportQualifier(tree, localEnv).tsym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   409
            if (name == names.asterisk) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   410
                // Import on demand.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   411
                chk.checkCanonical(imp.selected);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   412
                if (tree.staticImport)
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   413
                    importStaticAll(tree, p, env);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   414
                else
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   415
                    importAll(tree, p, env);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   416
            } else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   417
                // Named type import.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   418
                if (tree.staticImport) {
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   419
                    importNamedStatic(tree, p, name, localEnv);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   420
                    chk.checkCanonical(imp.selected);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   421
                } else {
43141
7ea6578a9110 8172668: NPE in jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.importNamed(
jlahoda
parents: 43138
diff changeset
   422
                    Type importedType = attribImportType(imp, localEnv);
7ea6578a9110 8172668: NPE in jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.importNamed(
jlahoda
parents: 43138
diff changeset
   423
                    Type originalType = importedType.getOriginalType();
7ea6578a9110 8172668: NPE in jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.importNamed(
jlahoda
parents: 43138
diff changeset
   424
                    TypeSymbol c = originalType.hasTag(CLASS) ? originalType.tsym : importedType.tsym;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   425
                    chk.checkCanonical(imp);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   426
                    importNamed(tree.pos(), c, env, tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   427
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   428
            }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   429
        }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   430
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   431
        Type attribImportType(JCTree tree, Env<AttrContext> env) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   432
            Assert.check(completionEnabled);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   433
            Lint prevLint = chk.setLint(allowDeprecationOnImport ?
41637
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 39371
diff changeset
   434
                    lint : lint.suppress(LintCategory.DEPRECATION, LintCategory.REMOVAL));
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   435
            try {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   436
                // To prevent deep recursion, suppress completion of some
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   437
                // types.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   438
                completionEnabled = false;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   439
                return attr.attribType(tree, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   440
            } finally {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   441
                completionEnabled = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   442
                chk.setLint(prevLint);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   443
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   444
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   445
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   446
        /** Import all classes of a class or package on demand.
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   447
         *  @param imp           The import that is being handled.
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   448
         *  @param tsym          The class or package the members of which are imported.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   449
         *  @param env           The env in which the imported classes will be entered.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   450
         */
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   451
        private void importAll(JCImport imp,
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   452
                               final TypeSymbol tsym,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   453
                               Env<AttrContext> env) {
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   454
            env.toplevel.starImportScope.importAll(types, tsym.members(), typeImportFilter, imp, cfHandler);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   455
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   456
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   457
        /** Import all static members of a class or package on demand.
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   458
         *  @param imp           The import that is being handled.
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   459
         *  @param tsym          The class or package the members of which are imported.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   460
         *  @param env           The env in which the imported classes will be entered.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   461
         */
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   462
        private void importStaticAll(JCImport imp,
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   463
                                     final TypeSymbol tsym,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   464
                                     Env<AttrContext> env) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   465
            final StarImportScope toScope = env.toplevel.starImportScope;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   466
            final TypeSymbol origin = tsym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   467
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   468
            toScope.importAll(types, origin.members(), staticImportFilter, imp, cfHandler);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   469
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   470
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   471
        /** Import statics types of a given name.  Non-types are handled in Attr.
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   472
         *  @param imp           The import that is being handled.
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   473
         *  @param tsym          The class from which the name is imported.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   474
         *  @param name          The (simple) name being imported.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   475
         *  @param env           The environment containing the named import
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   476
         *                  scope to add to.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   477
         */
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   478
        private void importNamedStatic(final JCImport imp,
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   479
                                       final TypeSymbol tsym,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   480
                                       final Name name,
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   481
                                       final Env<AttrContext> env) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   482
            if (tsym.kind != TYP) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
   483
                log.error(DiagnosticFlag.RECOVERABLE, imp.pos(), Errors.StaticImpOnlyClassesAndInterfaces);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   484
                return;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   485
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   486
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   487
            final NamedImportScope toScope = env.toplevel.namedImportScope;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   488
            final Scope originMembers = tsym.members();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   489
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 31299
diff changeset
   490
            imp.importScope = toScope.importByName(types, originMembers, name, staticImportFilter, imp, cfHandler);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   491
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   492
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   493
        /** Import given class.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   494
         *  @param pos           Position to be used for error reporting.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   495
         *  @param tsym          The class to be imported.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   496
         *  @param env           The environment containing the named import
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   497
         *                  scope to add to.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   498
         */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   499
        private void importNamed(DiagnosticPosition pos, final Symbol tsym, Env<AttrContext> env, JCImport imp) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   500
            if (tsym.kind == TYP)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   501
                imp.importScope = env.toplevel.namedImportScope.importType(tsym.owner.members(), tsym.owner.members(), tsym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   502
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   503
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   504
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   505
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   506
    /**Defines common utility methods used by the HierarchyPhase and HeaderPhase.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   507
     */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   508
    private abstract class AbstractHeaderPhase extends Phase {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   509
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   510
        public AbstractHeaderPhase(CompletionCause phaseName, Phase next) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   511
            super(phaseName, next);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   512
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   513
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   514
        protected Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   515
            WriteableScope baseScope = WriteableScope.create(tree.sym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   516
            //import already entered local classes into base scope
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   517
            for (Symbol sym : env.outer.info.scope.getSymbols(NON_RECURSIVE)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   518
                if (sym.isLocal()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   519
                    baseScope.enter(sym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   520
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   521
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   522
            //import current type-parameters into base scope
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   523
            if (tree.typarams != null)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   524
                for (List<JCTypeParameter> typarams = tree.typarams;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   525
                     typarams.nonEmpty();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   526
                     typarams = typarams.tail)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   527
                    baseScope.enter(typarams.head.type.tsym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   528
            Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   529
            Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   530
            localEnv.baseClause = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   531
            localEnv.outer = outer;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   532
            localEnv.info.isSelfCall = false;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   533
            return localEnv;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   534
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   535
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   536
        /** Generate a base clause for an enum type.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   537
         *  @param pos              The position for trees and diagnostics, if any
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   538
         *  @param c                The class symbol of the enum
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   539
         */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   540
        protected  JCExpression enumBase(int pos, ClassSymbol c) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   541
            JCExpression result = make.at(pos).
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   542
                TypeApply(make.QualIdent(syms.enumSym),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   543
                          List.of(make.Type(c.type)));
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   544
            return result;
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   545
        }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   546
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   547
        protected Type modelMissingTypes(Env<AttrContext> env, Type t, final JCExpression tree, final boolean interfaceExpected) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   548
            if (!t.hasTag(ERROR))
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   549
                return t;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   550
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   551
            return new ErrorType(t.getOriginalType(), t.tsym) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   552
                private Type modelType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   553
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   554
                @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   555
                public Type getModelType() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   556
                    if (modelType == null)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   557
                        modelType = new Synthesizer(env.toplevel.modle, getOriginalType(), interfaceExpected).visit(tree);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   558
                    return modelType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   559
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   560
            };
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   561
        }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   562
            // where:
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   563
            private class Synthesizer extends JCTree.Visitor {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   564
                ModuleSymbol msym;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   565
                Type originalType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   566
                boolean interfaceExpected;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   567
                List<ClassSymbol> synthesizedSymbols = List.nil();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   568
                Type result;
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   569
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   570
                Synthesizer(ModuleSymbol msym, Type originalType, boolean interfaceExpected) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   571
                    this.msym = msym;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   572
                    this.originalType = originalType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   573
                    this.interfaceExpected = interfaceExpected;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   574
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   575
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   576
                Type visit(JCTree tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   577
                    tree.accept(this);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   578
                    return result;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   579
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   580
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   581
                List<Type> visit(List<? extends JCTree> trees) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   582
                    ListBuffer<Type> lb = new ListBuffer<>();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   583
                    for (JCTree t: trees)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   584
                        lb.append(visit(t));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   585
                    return lb.toList();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   586
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   587
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   588
                @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   589
                public void visitTree(JCTree tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   590
                    result = syms.errType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   591
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   592
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   593
                @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   594
                public void visitIdent(JCIdent tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   595
                    if (!tree.type.hasTag(ERROR)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   596
                        result = tree.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   597
                    } else {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   598
                        result = synthesizeClass(tree.name, msym.unnamedPackage).type;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   599
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   600
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   601
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   602
                @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   603
                public void visitSelect(JCFieldAccess tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   604
                    if (!tree.type.hasTag(ERROR)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   605
                        result = tree.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   606
                    } else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   607
                        Type selectedType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   608
                        boolean prev = interfaceExpected;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   609
                        try {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   610
                            interfaceExpected = false;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   611
                            selectedType = visit(tree.selected);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   612
                        } finally {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   613
                            interfaceExpected = prev;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   614
                        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   615
                        ClassSymbol c = synthesizeClass(tree.name, selectedType.tsym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   616
                        result = c.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   617
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   618
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   619
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   620
                @Override
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   621
                public void visitTypeApply(JCTypeApply tree) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   622
                    if (!tree.type.hasTag(ERROR)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   623
                        result = tree.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   624
                    } else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   625
                        ClassType clazzType = (ClassType) visit(tree.clazz);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   626
                        if (synthesizedSymbols.contains(clazzType.tsym))
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   627
                            synthesizeTyparams((ClassSymbol) clazzType.tsym, tree.arguments.size());
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   628
                        final List<Type> actuals = visit(tree.arguments);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   629
                        result = new ErrorType(tree.type, clazzType.tsym) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   630
                            @Override @DefinedBy(Api.LANGUAGE_MODEL)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   631
                            public List<Type> getTypeArguments() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   632
                                return actuals;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   633
                            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   634
                        };
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   635
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   636
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   637
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   638
                ClassSymbol synthesizeClass(Name name, Symbol owner) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   639
                    int flags = interfaceExpected ? INTERFACE : 0;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   640
                    ClassSymbol c = new ClassSymbol(flags, name, owner);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   641
                    c.members_field = new Scope.ErrorScope(c);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   642
                    c.type = new ErrorType(originalType, c) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   643
                        @Override @DefinedBy(Api.LANGUAGE_MODEL)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   644
                        public List<Type> getTypeArguments() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   645
                            return typarams_field;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   646
                        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   647
                    };
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   648
                    synthesizedSymbols = synthesizedSymbols.prepend(c);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   649
                    return c;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   650
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   651
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   652
                void synthesizeTyparams(ClassSymbol sym, int n) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   653
                    ClassType ct = (ClassType) sym.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   654
                    Assert.check(ct.typarams_field.isEmpty());
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   655
                    if (n == 1) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   656
                        TypeVar v = new TypeVar(names.fromString("T"), sym, syms.botType);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   657
                        ct.typarams_field = ct.typarams_field.prepend(v);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   658
                    } else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   659
                        for (int i = n; i > 0; i--) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   660
                            TypeVar v = new TypeVar(names.fromString("T" + i), sym,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   661
                                                    syms.botType);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   662
                            ct.typarams_field = ct.typarams_field.prepend(v);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   663
                        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   664
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   665
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   666
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   667
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   668
        protected void attribSuperTypes(Env<AttrContext> env, Env<AttrContext> baseEnv) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   669
            JCClassDecl tree = env.enclClass;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   670
            ClassSymbol sym = tree.sym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   671
            ClassType ct = (ClassType)sym.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   672
            // Determine supertype.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   673
            Type supertype;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   674
            JCExpression extending;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   675
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   676
            if (tree.extending != null) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   677
                extending = clearTypeParams(tree.extending);
33366
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   678
                supertype = attr.attribBase(extending, baseEnv, true, false, true);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   679
            } else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   680
                extending = null;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   681
                supertype = ((tree.mods.flags & Flags.ENUM) != 0)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   682
                ? attr.attribBase(enumBase(tree.pos, sym), baseEnv,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   683
                                  true, false, false)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   684
                : (sym.fullname == names.java_lang_Object)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   685
                ? Type.noType
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   686
                : syms.objectType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   687
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   688
            ct.supertype_field = modelMissingTypes(baseEnv, supertype, extending, false);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   689
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   690
            // Determine interfaces.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   691
            ListBuffer<Type> interfaces = new ListBuffer<>();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   692
            ListBuffer<Type> all_interfaces = null; // lazy init
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   693
            List<JCExpression> interfaceTrees = tree.implementing;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   694
            for (JCExpression iface : interfaceTrees) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   695
                iface = clearTypeParams(iface);
33366
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   696
                Type it = attr.attribBase(iface, baseEnv, false, true, true);
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   697
                if (it.hasTag(CLASS)) {
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   698
                    interfaces.append(it);
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   699
                    if (all_interfaces != null) all_interfaces.append(it);
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   700
                } else {
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   701
                    if (all_interfaces == null)
a113e08cc061 8087349: Test tools/sjavac/IncCompInheritance.java is failing
alundblad
parents: 32913
diff changeset
   702
                        all_interfaces = new ListBuffer<Type>().appendList(interfaces);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   703
                    all_interfaces.append(modelMissingTypes(baseEnv, it, iface, true));
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   704
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   705
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   706
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   707
            if ((sym.flags_field & ANNOTATION) != 0) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   708
                ct.interfaces_field = List.of(syms.annotationType);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   709
                ct.all_interfaces_field = ct.interfaces_field;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   710
            }  else {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   711
                ct.interfaces_field = interfaces.toList();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   712
                ct.all_interfaces_field = (all_interfaces == null)
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   713
                        ? ct.interfaces_field : all_interfaces.toList();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   714
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   715
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   716
            //where:
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   717
            protected JCExpression clearTypeParams(JCExpression superType) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   718
                return superType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   719
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   720
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   721
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   722
    private final class HierarchyPhase extends AbstractHeaderPhase implements Completer {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   723
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   724
        public HierarchyPhase() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   725
            super(CompletionCause.HIERARCHY_PHASE, new HeaderPhase());
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   726
        }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   727
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   728
        @Override
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   729
        protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   730
            //The ClassSymbols in the envs list may not be in the dependency order.
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   731
            //To get proper results, for every class or interface C, the supertypes of
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   732
            //C must be processed by the HierarchyPhase phase before C.
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   733
            //To achieve that, the HierarchyPhase is registered as the Completer for
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   734
            //all the classes first, and then all the classes are completed.
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   735
            for (Env<AttrContext> env : envs) {
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   736
                env.enclClass.sym.completer = this;
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   737
            }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   738
            for (Env<AttrContext> env : envs) {
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   739
                env.enclClass.sym.complete();
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   740
            }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   741
        }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   742
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   743
        @Override
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   744
        protected void runPhase(Env<AttrContext> env) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   745
            JCClassDecl tree = env.enclClass;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   746
            ClassSymbol sym = tree.sym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   747
            ClassType ct = (ClassType)sym.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   748
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   749
            Env<AttrContext> baseEnv = baseEnv(tree, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   750
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   751
            attribSuperTypes(env, baseEnv);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   752
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   753
            if (sym.fullname == names.java_lang_Object) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   754
                if (tree.extending != null) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   755
                    chk.checkNonCyclic(tree.extending.pos(),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   756
                                       ct.supertype_field);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   757
                    ct.supertype_field = Type.noType;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   758
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   759
                else if (tree.implementing.nonEmpty()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   760
                    chk.checkNonCyclic(tree.implementing.head.pos(),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   761
                                       ct.interfaces_field.head);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   762
                    ct.interfaces_field = List.nil();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   763
                }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   764
            }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   765
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
   766
            markDeprecated(sym, tree.mods.annotations, baseEnv);
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   767
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   768
            chk.checkNonCyclicDecl(tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   769
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   770
            //where:
41637
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 39371
diff changeset
   771
            @Override
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   772
            protected JCExpression clearTypeParams(JCExpression superType) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   773
                switch (superType.getTag()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   774
                    case TYPEAPPLY:
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   775
                        return ((JCTypeApply) superType).clazz;
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   776
                }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   777
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   778
                return superType;
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   779
            }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   780
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   781
        @Override
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   782
        public void complete(Symbol sym) throws CompletionFailure {
39371
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   783
            Assert.check((topLevelPhase instanceof ImportsPhase) ||
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   784
                         (topLevelPhase == this));
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   785
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   786
            if (topLevelPhase != this) {
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   787
                //only do the processing based on dependencies in the HierarchyPhase:
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   788
                sym.completer = this;
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   789
                return ;
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   790
            }
ac04aefa4ba6 8148131: compilation result depends on order of sources
jlahoda
parents: 37942
diff changeset
   791
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   792
            Env<AttrContext> env = typeEnvs.get((ClassSymbol) sym);
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   793
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   794
            super.doCompleteEnvs(List.of(env));
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   795
        }
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   796
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   797
    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   798
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   799
    private final class HeaderPhase extends AbstractHeaderPhase {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   800
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   801
        public HeaderPhase() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   802
            super(CompletionCause.HEADER_PHASE, new MembersPhase());
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   803
        }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   804
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   805
        @Override
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   806
        protected void runPhase(Env<AttrContext> env) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   807
            JCClassDecl tree = env.enclClass;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   808
            ClassSymbol sym = tree.sym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   809
            ClassType ct = (ClassType)sym.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   810
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   811
            // create an environment for evaluating the base clauses
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   812
            Env<AttrContext> baseEnv = baseEnv(tree, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   813
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   814
            if (tree.extending != null)
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   815
                annotate.queueScanTreeAndTypeAnnotate(tree.extending, baseEnv, sym, tree.pos());
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   816
            for (JCExpression impl : tree.implementing)
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   817
                annotate.queueScanTreeAndTypeAnnotate(impl, baseEnv, sym, tree.pos());
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   818
            annotate.flush();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   819
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   820
            attribSuperTypes(env, baseEnv);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   821
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   822
            Set<Type> interfaceSet = new HashSet<>();
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   823
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   824
            for (JCExpression iface : tree.implementing) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   825
                Type it = iface.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   826
                if (it.hasTag(CLASS))
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   827
                    chk.checkNotRepeated(iface.pos(), types.erasure(it), interfaceSet);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   828
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   829
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   830
            annotate.annotateLater(tree.mods.annotations, baseEnv,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   831
                        sym, tree.pos());
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   832
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   833
            attr.attribTypeVariables(tree.typarams, baseEnv);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   834
            for (JCTypeParameter tp : tree.typarams)
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   835
                annotate.queueScanTreeAndTypeAnnotate(tp, baseEnv, sym, tree.pos());
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   836
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   837
            // check that no package exists with same fully qualified name,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   838
            // but admit classes in the unnamed package which have the same
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   839
            // name as a top-level package.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   840
            if (checkClash &&
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   841
                sym.owner.kind == PCK && sym.owner != env.toplevel.modle.unnamedPackage &&
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33366
diff changeset
   842
                syms.packageExists(env.toplevel.modle, sym.fullname)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43141
diff changeset
   843
                log.error(tree.pos, Errors.ClashWithPkgOfSameName(Kinds.kindName(sym),sym));
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   844
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   845
            if (sym.owner.kind == PCK && (sym.flags_field & PUBLIC) == 0 &&
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   846
                !env.toplevel.sourcefile.isNameCompatible(sym.name.toString(),JavaFileObject.Kind.SOURCE)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   847
                sym.flags_field |= AUXILIARY;
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   848
            }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   849
        }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   850
    }
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   851
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   852
    /** Enter member fields and methods of a class
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   853
     */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   854
    private final class MembersPhase extends Phase {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   855
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   856
        public MembersPhase() {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   857
            super(CompletionCause.MEMBERS_PHASE, null);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   858
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   859
37942
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   860
        private boolean completing;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   861
        private List<Env<AttrContext>> todo = List.nil();
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   862
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   863
        @Override
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   864
        protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   865
            todo = todo.prependList(envs);
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   866
            if (completing) {
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   867
                return ; //the top-level invocation will handle all envs
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   868
            }
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   869
            boolean prevCompleting = completing;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   870
            completing = true;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   871
            try {
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   872
                while (todo.nonEmpty()) {
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   873
                    Env<AttrContext> head = todo.head;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   874
                    todo = todo.tail;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   875
                    super.doCompleteEnvs(List.of(head));
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   876
                }
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   877
            } finally {
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   878
                completing = prevCompleting;
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   879
            }
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   880
        }
b0e834553947 8156097: docs build fails with StackOverflowError on Solaris
jlahoda
parents: 36526
diff changeset
   881
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   882
        @Override
32913
586e23a1b116 8075274: Compilation still depends on the order of imports
jlahoda
parents: 32059
diff changeset
   883
        protected void runPhase(Env<AttrContext> env) {
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   884
            JCClassDecl tree = env.enclClass;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   885
            ClassSymbol sym = tree.sym;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   886
            ClassType ct = (ClassType)sym.type;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   887
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   888
            // Add default constructor if needed.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   889
            if ((sym.flags() & INTERFACE) == 0 &&
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   890
                !TreeInfo.hasConstructors(tree.defs)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   891
                List<Type> argtypes = List.nil();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   892
                List<Type> typarams = List.nil();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   893
                List<Type> thrown = List.nil();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   894
                long ctorFlags = 0;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   895
                boolean based = false;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   896
                boolean addConstructor = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   897
                JCNewClass nc = null;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   898
                if (sym.name.isEmpty()) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   899
                    nc = (JCNewClass)env.next.tree;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   900
                    if (nc.constructor != null) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   901
                        addConstructor = nc.constructor.kind != ERR;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   902
                        Type superConstrType = types.memberType(sym.type,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   903
                                                                nc.constructor);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   904
                        argtypes = superConstrType.getParameterTypes();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   905
                        typarams = superConstrType.getTypeArguments();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   906
                        ctorFlags = nc.constructor.flags() & VARARGS;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   907
                        if (nc.encl != null) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   908
                            argtypes = argtypes.prepend(nc.encl.type);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   909
                            based = true;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   910
                        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   911
                        thrown = superConstrType.getThrownTypes();
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   912
                    }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   913
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   914
                if (addConstructor) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   915
                    MethodSymbol basedConstructor = nc != null ?
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   916
                            (MethodSymbol)nc.constructor : null;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   917
                    JCTree constrDef = DefaultConstructor(make.at(tree.pos), sym,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   918
                                                        basedConstructor,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   919
                                                        typarams, argtypes, thrown,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   920
                                                        ctorFlags, based);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   921
                    tree.defs = tree.defs.prepend(constrDef);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   922
                }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   923
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   924
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   925
            // enter symbols for 'this' into current scope.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   926
            VarSymbol thisSym =
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   927
                new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   928
            thisSym.pos = Position.FIRSTPOS;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   929
            env.info.scope.enter(thisSym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   930
            // if this is a class, enter symbol for 'super' into current scope.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   931
            if ((sym.flags_field & INTERFACE) == 0 &&
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   932
                    ct.supertype_field.hasTag(CLASS)) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   933
                VarSymbol superSym =
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   934
                    new VarSymbol(FINAL | HASINIT, names._super,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   935
                                  ct.supertype_field, sym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   936
                superSym.pos = Position.FIRSTPOS;
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   937
                env.info.scope.enter(superSym);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   938
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   939
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   940
            finishClass(tree, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   941
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   942
            if (allowTypeAnnos) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   943
                typeAnnotations.organizeTypeAnnotationsSignatures(env, (JCClassDecl)env.tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   944
                typeAnnotations.validateTypeAnnotationsSignatures(env, (JCClassDecl)env.tree);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   945
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   946
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   947
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   948
        /** Enter members for a class.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   949
         */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   950
        void finishClass(JCClassDecl tree, Env<AttrContext> env) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   951
            if ((tree.mods.flags & Flags.ENUM) != 0 &&
43137
9a96f02a6293 8171332: NPE in MembersPhase.finishClass
vromero
parents: 42828
diff changeset
   952
                !tree.sym.type.hasTag(ERROR) &&
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   953
                (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   954
                addEnumMembers(tree, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   955
            }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   956
            memberEnter.memberEnter(tree.defs, env);
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   957
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   958
            if (tree.sym.isAnnotationType()) {
30066
d74c06a92bd8 8078600: Infinite loop when compiling annotations with -XDcompletionDeps
alundblad
parents: 29842
diff changeset
   959
                Assert.check(tree.sym.isCompleted());
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   960
                tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29149
diff changeset
   961
            }
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   962
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   963
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   964
        /** Add the implicit members for an enum type
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   965
         *  to the symbol table.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   966
         */
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   967
        private void addEnumMembers(JCClassDecl tree, Env<AttrContext> env) {
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   968
            JCExpression valuesType = make.Type(new ArrayType(tree.sym.type, syms.arrayClass));
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   969
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   970
            // public static T[] values() { return ???; }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   971
            JCMethodDecl values = make.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   972
                MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   973
                          names.values,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   974
                          valuesType,
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   975
                          List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   976
                          List.nil(),
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   977
                          List.nil(), // thrown
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   978
                          null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   979
                          null);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   980
            memberEnter.memberEnter(values, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   981
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   982
            // public static T valueOf(String name) { return ???; }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   983
            JCMethodDecl valueOf = make.
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   984
                MethodDef(make.Modifiers(Flags.PUBLIC|Flags.STATIC),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   985
                          names.valueOf,
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   986
                          make.Type(tree.sym.type),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   987
                          List.nil(),
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   988
                          List.of(make.VarDef(make.Modifiers(Flags.PARAMETER |
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   989
                                                             Flags.MANDATED),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   990
                                                names.fromString("name"),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   991
                                                make.Type(syms.stringType), null)),
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
   992
                          List.nil(), // thrown
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   993
                          null, //make.Block(0, Tree.emptyList.prepend(make.Return(make.Ident(names._null)))),
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   994
                          null);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   995
            memberEnter.memberEnter(valueOf, env);
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   996
        }
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   997
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
   998
    }
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8242
diff changeset
   999
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
/* ***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
 * tree building
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
 ****************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
    /** Generate default constructor for given class. For classes different
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
     *  from java.lang.Object, this is:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
     *      super(x_0, ..., x_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
     *    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
     *  or, if based == true:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
     *    c(argtype_0 x_0, ..., argtype_n x_n) throws thrown {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
     *      x_0.super(x_1, ..., x_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
     *    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
     *  @param make     The tree factory.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
     *  @param c        The class owning the default constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
     *  @param argtypes The parameter types of the constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
     *  @param thrown   The thrown exceptions of the constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
     *  @param based    Is first parameter a this$n?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
    JCTree DefaultConstructor(TreeMaker make,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
                            ClassSymbol c,
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1025
                            MethodSymbol baseInit,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                            List<Type> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                            List<Type> argtypes,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                            List<Type> thrown,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
                            long flags,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                            boolean based) {
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1031
        JCTree result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
        if ((c.flags() & ENUM) != 0 &&
16558
07c08ad4a700 8010179: Remove transitional target values from javac
darcy
parents: 15720
diff changeset
  1033
            (types.supertype(c.type).tsym == syms.enumSym)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
            // constructors of true enums are private
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
            flags = (flags & ~AccessFlags) | PRIVATE | GENERATEDCONSTR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
            flags |= (c.flags() & AccessFlags) | GENERATEDCONSTR;
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1038
        if (c.name.isEmpty()) {
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1039
            flags |= ANONCONSTR;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1040
        }
47248
22d665f53a4a 8044853: Attr synthesizes nullcheck tree nodes
jlahoda
parents: 47216
diff changeset
  1041
        if (based) {
22d665f53a4a 8044853: Attr synthesizes nullcheck tree nodes
jlahoda
parents: 47216
diff changeset
  1042
            flags |= ANONCONSTR_BASED;
22d665f53a4a 8044853: Attr synthesizes nullcheck tree nodes
jlahoda
parents: 47216
diff changeset
  1043
        }
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1044
        Type mType = new MethodType(argtypes, null, thrown, c);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1045
        Type initType = typarams.nonEmpty() ?
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24069
diff changeset
  1046
            new ForAll(typarams, mType) :
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24069
diff changeset
  1047
            mType;
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1048
        MethodSymbol init = new MethodSymbol(flags, names.init,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1049
                initType, c);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1050
        init.params = createDefaultConstructorParams(make, baseInit, init,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1051
                argtypes, based);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1052
        List<JCVariableDecl> params = make.Params(argtypes, init);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1053
        List<JCStatement> stats = List.nil();
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1054
        if (c.type != syms.objectType) {
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1055
            stats = stats.prepend(SuperCall(make, typarams, params, based));
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1056
        }
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1057
        result = make.MethodDef(init, make.Block(0, stats));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1061
    private List<VarSymbol> createDefaultConstructorParams(
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1062
            TreeMaker make,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1063
            MethodSymbol baseInit,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1064
            MethodSymbol init,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1065
            List<Type> argtypes,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1066
            boolean based) {
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1067
        List<VarSymbol> initParams = null;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1068
        List<Type> argTypesList = argtypes;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1069
        if (based) {
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1070
            /*  In this case argtypes will have an extra type, compared to baseInit,
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1071
             *  corresponding to the type of the enclosing instance i.e.:
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1072
             *
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1073
             *  Inner i = outer.new Inner(1){}
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1074
             *
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1075
             *  in the above example argtypes will be (Outer, int) and baseInit
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1076
             *  will have parameter's types (int). So in this case we have to add
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1077
             *  first the extra type in argtypes and then get the names of the
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1078
             *  parameters from baseInit.
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1079
             */
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1080
            initParams = List.nil();
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 19656
diff changeset
  1081
            VarSymbol param = new VarSymbol(PARAMETER, make.paramName(0), argtypes.head, init);
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1082
            initParams = initParams.append(param);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1083
            argTypesList = argTypesList.tail;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1084
        }
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1085
        if (baseInit != null && baseInit.params != null &&
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1086
            baseInit.params.nonEmpty() && argTypesList.nonEmpty()) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42407
diff changeset
  1087
            initParams = (initParams == null) ? List.nil() : initParams;
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1088
            List<VarSymbol> baseInitParams = baseInit.params;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1089
            while (baseInitParams.nonEmpty() && argTypesList.nonEmpty()) {
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents: 19656
diff changeset
  1090
                VarSymbol param = new VarSymbol(baseInitParams.head.flags() | PARAMETER,
17999
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1091
                        baseInitParams.head.name, argTypesList.head, init);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1092
                initParams = initParams.append(param);
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1093
                baseInitParams = baseInitParams.tail;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1094
                argTypesList = argTypesList.tail;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1095
            }
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1096
        }
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1097
        return initParams;
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1098
    }
42ae6fe53718 8010737: javac, known parameter's names should be copied to automatically generated constructors for inner classes
vromero
parents: 17578
diff changeset
  1099
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
    /** Generate call to superclass constructor. This is:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
     *    super(id_0, ..., id_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
     * or, if based == true
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
     *    id_0.super(id_1,...,id_n)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
     *  where id_0, ..., id_n are the names of the given parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
     *  @param make    The tree factory
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
     *  @param params  The parameters that need to be passed to super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
     *  @param typarams  The type parameters that need to be passed to super
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
     *  @param based   Is first parameter a this$n?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
    JCExpressionStatement SuperCall(TreeMaker make,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
                   List<Type> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
                   List<JCVariableDecl> params,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                   boolean based) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
        JCExpression meth;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
        if (based) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
            meth = make.Select(make.Ident(params.head), names._super);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
            params = params.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
            meth = make.Ident(names._super);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
        List<JCExpression> typeargs = typarams.nonEmpty() ? make.Types(typarams) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
        return make.Exec(make.Apply(typeargs, meth, make.Idents(params)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
    }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1129
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1130
    /**
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1131
     * Mark sym deprecated if annotations contain @Deprecated annotation.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1132
     */
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1133
    public void markDeprecated(Symbol sym, List<JCAnnotation> annotations, Env<AttrContext> env) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1134
        // In general, we cannot fully process annotations yet,  but we
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1135
        // can attribute the annotation types and then check to see if the
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1136
        // @Deprecated annotation is present.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1137
        attr.attribAnnotationTypes(annotations, env);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1138
        handleDeprecatedAnnotations(annotations, sym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1139
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1140
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1141
    /**
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1142
     * If a list of annotations contains a reference to java.lang.Deprecated,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1143
     * set the DEPRECATED flag.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1144
     * If the annotation is marked forRemoval=true, also set DEPRECATED_REMOVAL.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1145
     **/
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1146
    private void handleDeprecatedAnnotations(List<JCAnnotation> annotations, Symbol sym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1147
        for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1148
            JCAnnotation a = al.head;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1149
            if (a.annotationType.type == syms.deprecatedType) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1150
                sym.flags_field |= (Flags.DEPRECATED | Flags.DEPRECATED_ANNOTATION);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1151
                a.args.stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1152
                        .filter(e -> e.hasTag(ASSIGN))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1153
                        .map(e -> (JCAssign) e)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1154
                        .filter(assign -> TreeInfo.name(assign.lhs) == names.forRemoval)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1155
                        .findFirst()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1156
                        .ifPresent(assign -> {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1157
                            JCExpression rhs = TreeInfo.skipParens(assign.rhs);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1158
                            if (rhs.hasTag(LITERAL)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1159
                                    && Boolean.TRUE.equals(((JCLiteral) rhs).getValue())) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1160
                                sym.flags_field |= DEPRECATED_REMOVAL;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1161
                            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1162
                        });
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1163
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1164
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41932
diff changeset
  1165
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
}