20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.comp;
27
28 import java.util.Map;
29 import java.util.Optional;
30
31 import javax.tools.JavaFileObject;
32 import javax.tools.JavaFileManager;
33
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.code.Kinds.KindName;
36 import com.sun.tools.javac.code.Kinds.KindSelector;
37 import com.sun.tools.javac.code.Scope.*;
38 import com.sun.tools.javac.code.Symbol.*;
39 import com.sun.tools.javac.code.Type.*;
40 import com.sun.tools.javac.main.Option.PkgInfo;
41 import com.sun.tools.javac.resources.CompilerProperties.Errors;
42 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
43 import com.sun.tools.javac.tree.*;
44 import com.sun.tools.javac.tree.JCTree.*;
45 import com.sun.tools.javac.util.*;
46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
47 import com.sun.tools.javac.util.List;
48
49 import static com.sun.tools.javac.code.Flags.*;
50 import static com.sun.tools.javac.code.Kinds.Kind.*;
51
52 /** This class enters symbols for all encountered definitions into
53 * the symbol table. The pass consists of high-level two phases,
54 * organized as follows:
55 *
56 * <p>In the first phase, all class symbols are entered into their
57 * enclosing scope, descending recursively down the tree for classes
58 * which are members of other classes. The class symbols are given a
59 * TypeEnter object as completer.
87 * This code and its internal interfaces are subject to change or
88 * deletion without notice.</b>
89 */
90 public class Enter extends JCTree.Visitor {
91 protected static final Context.Key<Enter> enterKey = new Context.Key<>();
92
93 Annotate annotate;
94 Log log;
95 Symtab syms;
96 Check chk;
97 TreeMaker make;
98 TypeEnter typeEnter;
99 Types types;
100 Lint lint;
101 Names names;
102 JavaFileManager fileManager;
103 PkgInfo pkginfoOpt;
104 TypeEnvs typeEnvs;
105 Modules modules;
106 JCDiagnostic.Factory diags;
107
108 private final Todo todo;
109
110 public static Enter instance(Context context) {
111 Enter instance = context.get(enterKey);
112 if (instance == null)
113 instance = new Enter(context);
114 return instance;
115 }
116
117 @SuppressWarnings("this-escape")
118 protected Enter(Context context) {
119 context.put(enterKey, this);
120
121 log = Log.instance(context);
122 make = TreeMaker.instance(context);
123 syms = Symtab.instance(context);
124 chk = Check.instance(context);
125 typeEnter = TypeEnter.instance(context);
126 types = Types.instance(context);
127 annotate = Annotate.instance(context);
128 lint = Lint.instance(context);
129 names = Names.instance(context);
130 modules = Modules.instance(context);
131 diags = JCDiagnostic.Factory.instance(context);
132
133 predefClassDef = make.ClassDef(
134 make.Modifiers(PUBLIC),
135 syms.predefClass.name,
136 List.nil(),
137 null,
138 List.nil(),
139 List.nil());
140 predefClassDef.sym = syms.predefClass;
141 todo = Todo.instance(context);
142 fileManager = context.get(JavaFileManager.class);
143
144 Options options = Options.instance(context);
145 pkginfoOpt = PkgInfo.get(options);
146 typeEnvs = TypeEnvs.instance(context);
147 }
148
149 /** Accessor for typeEnvs
150 */
151 public Env<AttrContext> getEnv(TypeSymbol sym) {
152 return typeEnvs.get(sym);
153 }
154
155 public Iterable<Env<AttrContext>> getEnvs() {
156 return typeEnvs.values();
157 }
158
159 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
160 Env<AttrContext> localEnv = getEnv(sym);
161 if (localEnv == null) return null;
162 Env<AttrContext> lintEnv = localEnv;
163 while (lintEnv.info.lint == null)
164 lintEnv = lintEnv.next;
165 localEnv.info.lint = lintEnv.info.lint.augment(sym);
166 return localEnv;
488 tree.sym = (ClassSymbol)result.tsym;
489 return;
490 }
491 chk.putCompiled(c);
492 enclScope.enter(c);
493
494 // Set up an environment for class block and store in `typeEnvs'
495 // table, to be retrieved later in memberEnter and attribution.
496 Env<AttrContext> localEnv = classEnv(tree, env);
497 typeEnvs.put(c, localEnv);
498
499 // Fill out class fields.
500 c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
501 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
502 c.classfile = c.sourcefile = env.toplevel.sourcefile;
503 c.members_field = WriteableScope.create(c);
504 c.isPermittedExplicit = tree.permitting.nonEmpty();
505 c.clearAnnotationMetadata();
506
507 ClassType ct = (ClassType)c.type;
508 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
509 // We are seeing a local or inner class.
510 // Set outer_field of this class to closest enclosing class
511 // which contains this class in a non-static context
512 // (its "enclosing instance class"), provided such a class exists.
513 Symbol owner1 = owner;
514 while (owner1.kind.matches(KindSelector.VAL_MTH) &&
515 (owner1.flags_field & STATIC) == 0) {
516 owner1 = owner1.owner;
517 }
518 if (owner1.kind == TYP) {
519 ct.setEnclosingType(owner1.type);
520 }
521 }
522
523 // Enter type parameters.
524 ct.typarams_field = classEnter(tree.typarams, localEnv);
525 ct.allparams_field = null;
526
527 // install further completer for this type.
528 c.completer = typeEnter;
529
530 // Add non-local class to uncompleted, to make sure it will be
531 // completed later.
532 if (!c.isDirectlyOrIndirectlyLocal() && uncompleted != null) uncompleted.append(c);
533 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
534
535 // Recursively enter all member classes.
536 classEnter(tree.defs, localEnv);
537
538 // Assert.checkNonNull(c.modle, c.sourcefile.toString());
539
540 result = c.type;
541 }
542 //where
543 /** Does class have the same name as the file it appears in?
544 */
545 private static boolean classNameMatchesFileName(ClassSymbol c,
|
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.comp;
27
28 import java.util.Map;
29 import java.util.Optional;
30
31 import javax.tools.JavaFileObject;
32 import javax.tools.JavaFileManager;
33
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.code.Kinds.KindName;
36 import com.sun.tools.javac.code.Kinds.KindSelector;
37 import com.sun.tools.javac.code.Scope.*;
38 import com.sun.tools.javac.code.Symbol.*;
39 import com.sun.tools.javac.code.Type.*;
40 import com.sun.tools.javac.code.Type.ClassType.Flavor;
41 import com.sun.tools.javac.main.Option.PkgInfo;
42 import com.sun.tools.javac.resources.CompilerProperties.Errors;
43 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
44 import com.sun.tools.javac.tree.*;
45 import com.sun.tools.javac.tree.JCTree.*;
46 import com.sun.tools.javac.util.*;
47 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
48 import com.sun.tools.javac.util.List;
49
50 import static com.sun.tools.javac.code.Flags.*;
51 import static com.sun.tools.javac.code.Kinds.Kind.*;
52
53 /** This class enters symbols for all encountered definitions into
54 * the symbol table. The pass consists of high-level two phases,
55 * organized as follows:
56 *
57 * <p>In the first phase, all class symbols are entered into their
58 * enclosing scope, descending recursively down the tree for classes
59 * which are members of other classes. The class symbols are given a
60 * TypeEnter object as completer.
88 * This code and its internal interfaces are subject to change or
89 * deletion without notice.</b>
90 */
91 public class Enter extends JCTree.Visitor {
92 protected static final Context.Key<Enter> enterKey = new Context.Key<>();
93
94 Annotate annotate;
95 Log log;
96 Symtab syms;
97 Check chk;
98 TreeMaker make;
99 TypeEnter typeEnter;
100 Types types;
101 Lint lint;
102 Names names;
103 JavaFileManager fileManager;
104 PkgInfo pkginfoOpt;
105 TypeEnvs typeEnvs;
106 Modules modules;
107 JCDiagnostic.Factory diags;
108 boolean allowPrimitiveClasses;
109
110 private final Todo todo;
111
112 public static Enter instance(Context context) {
113 Enter instance = context.get(enterKey);
114 if (instance == null)
115 instance = new Enter(context);
116 return instance;
117 }
118
119 @SuppressWarnings("this-escape")
120 protected Enter(Context context) {
121 context.put(enterKey, this);
122
123 log = Log.instance(context);
124 make = TreeMaker.instance(context);
125 syms = Symtab.instance(context);
126 chk = Check.instance(context);
127 typeEnter = TypeEnter.instance(context);
128 types = Types.instance(context);
129 annotate = Annotate.instance(context);
130 lint = Lint.instance(context);
131 names = Names.instance(context);
132 modules = Modules.instance(context);
133 diags = JCDiagnostic.Factory.instance(context);
134
135 predefClassDef = make.ClassDef(
136 make.Modifiers(PUBLIC),
137 syms.predefClass.name,
138 List.nil(),
139 null,
140 List.nil(),
141 List.nil());
142 predefClassDef.sym = syms.predefClass;
143 todo = Todo.instance(context);
144 fileManager = context.get(JavaFileManager.class);
145
146 Options options = Options.instance(context);
147 pkginfoOpt = PkgInfo.get(options);
148 typeEnvs = TypeEnvs.instance(context);
149 Source source = Source.instance(context);
150 allowPrimitiveClasses = Source.Feature.PRIMITIVE_CLASSES.allowedInSource(source) && options.isSet("enablePrimitiveClasses");
151 }
152
153 /** Accessor for typeEnvs
154 */
155 public Env<AttrContext> getEnv(TypeSymbol sym) {
156 return typeEnvs.get(sym);
157 }
158
159 public Iterable<Env<AttrContext>> getEnvs() {
160 return typeEnvs.values();
161 }
162
163 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
164 Env<AttrContext> localEnv = getEnv(sym);
165 if (localEnv == null) return null;
166 Env<AttrContext> lintEnv = localEnv;
167 while (lintEnv.info.lint == null)
168 lintEnv = lintEnv.next;
169 localEnv.info.lint = lintEnv.info.lint.augment(sym);
170 return localEnv;
492 tree.sym = (ClassSymbol)result.tsym;
493 return;
494 }
495 chk.putCompiled(c);
496 enclScope.enter(c);
497
498 // Set up an environment for class block and store in `typeEnvs'
499 // table, to be retrieved later in memberEnter and attribution.
500 Env<AttrContext> localEnv = classEnv(tree, env);
501 typeEnvs.put(c, localEnv);
502
503 // Fill out class fields.
504 c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
505 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
506 c.classfile = c.sourcefile = env.toplevel.sourcefile;
507 c.members_field = WriteableScope.create(c);
508 c.isPermittedExplicit = tree.permitting.nonEmpty();
509 c.clearAnnotationMetadata();
510
511 ClassType ct = (ClassType)c.type;
512 if (allowPrimitiveClasses) {
513 ct.flavor = ct.flavor.metamorphose((c.flags_field & PRIMITIVE_CLASS) != 0);
514 }
515
516 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
517 // We are seeing a local or inner class.
518 // Set outer_field of this class to closest enclosing class
519 // which contains this class in a non-static context
520 // (its "enclosing instance class"), provided such a class exists.
521 Symbol owner1 = owner;
522 while (owner1.kind.matches(KindSelector.VAL_MTH) &&
523 (owner1.flags_field & STATIC) == 0) {
524 owner1 = owner1.owner;
525 }
526 if (owner1.kind == TYP) {
527 ct.setEnclosingType(owner1.type);
528 }
529 }
530
531 // Enter type parameters.
532 ct.typarams_field = classEnter(tree.typarams, localEnv);
533 ct.allparams_field = null;
534 if (allowPrimitiveClasses && ct.isPrimitiveClass()) {
535 if (ct.projection != null) {
536 ct.projection.typarams_field = ct.typarams_field;
537 ct.projection.allparams_field = ct.allparams_field;
538 }
539 }
540
541 // install further completer for this type.
542 c.completer = typeEnter;
543
544 // Add non-local class to uncompleted, to make sure it will be
545 // completed later.
546 if (!c.isDirectlyOrIndirectlyLocal() && uncompleted != null) uncompleted.append(c);
547 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
548
549 // Recursively enter all member classes.
550 classEnter(tree.defs, localEnv);
551
552 // Assert.checkNonNull(c.modle, c.sourcefile.toString());
553
554 result = c.type;
555 }
556 //where
557 /** Does class have the same name as the file it appears in?
558 */
559 private static boolean classNameMatchesFileName(ClassSymbol c,
|