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 protected Enter(Context context) {
118 context.put(enterKey, this);
119
120 log = Log.instance(context);
121 make = TreeMaker.instance(context);
122 syms = Symtab.instance(context);
123 chk = Check.instance(context);
124 typeEnter = TypeEnter.instance(context);
125 types = Types.instance(context);
126 annotate = Annotate.instance(context);
127 lint = Lint.instance(context);
128 names = Names.instance(context);
129 modules = Modules.instance(context);
130 diags = JCDiagnostic.Factory.instance(context);
131
132 predefClassDef = make.ClassDef(
133 make.Modifiers(PUBLIC),
134 syms.predefClass.name,
135 List.nil(),
136 null,
137 List.nil(),
138 List.nil());
139 predefClassDef.sym = syms.predefClass;
140 todo = Todo.instance(context);
141 fileManager = context.get(JavaFileManager.class);
142
143 Options options = Options.instance(context);
144 pkginfoOpt = PkgInfo.get(options);
145 typeEnvs = TypeEnvs.instance(context);
146 }
147
148 /** Accessor for typeEnvs
149 */
150 public Env<AttrContext> getEnv(TypeSymbol sym) {
151 return typeEnvs.get(sym);
152 }
153
154 public Iterable<Env<AttrContext>> getEnvs() {
155 return typeEnvs.values();
156 }
157
158 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
159 Env<AttrContext> localEnv = getEnv(sym);
160 if (localEnv == null) return null;
161 Env<AttrContext> lintEnv = localEnv;
162 while (lintEnv.info.lint == null)
163 lintEnv = lintEnv.next;
164 localEnv.info.lint = lintEnv.info.lint.augment(sym);
165 return localEnv;
486 tree.sym = (ClassSymbol)result.tsym;
487 return;
488 }
489 chk.putCompiled(c);
490 enclScope.enter(c);
491
492 // Set up an environment for class block and store in `typeEnvs'
493 // table, to be retrieved later in memberEnter and attribution.
494 Env<AttrContext> localEnv = classEnv(tree, env);
495 typeEnvs.put(c, localEnv);
496
497 // Fill out class fields.
498 c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
499 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
500 c.classfile = c.sourcefile = env.toplevel.sourcefile;
501 c.members_field = WriteableScope.create(c);
502 c.isPermittedExplicit = tree.permitting.nonEmpty();
503 c.clearAnnotationMetadata();
504
505 ClassType ct = (ClassType)c.type;
506 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
507 // We are seeing a local or inner class.
508 // Set outer_field of this class to closest enclosing class
509 // which contains this class in a non-static context
510 // (its "enclosing instance class"), provided such a class exists.
511 Symbol owner1 = owner;
512 while (owner1.kind.matches(KindSelector.VAL_MTH) &&
513 (owner1.flags_field & STATIC) == 0) {
514 owner1 = owner1.owner;
515 }
516 if (owner1.kind == TYP) {
517 ct.setEnclosingType(owner1.type);
518 }
519 }
520
521 // Enter type parameters.
522 ct.typarams_field = classEnter(tree.typarams, localEnv);
523 ct.allparams_field = null;
524
525 // install further completer for this type.
526 c.completer = typeEnter;
527
528 // Add non-local class to uncompleted, to make sure it will be
529 // completed later.
530 if (!c.isDirectlyOrIndirectlyLocal() && uncompleted != null) uncompleted.append(c);
531 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
532
533 // Recursively enter all member classes.
534 classEnter(tree.defs, localEnv);
535
536 // Assert.checkNonNull(c.modle, c.sourcefile.toString());
537
538 result = c.type;
539 }
540 //where
541 /** Does class have the same name as the file it appears in?
542 */
543 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 protected Enter(Context context) {
120 context.put(enterKey, this);
121
122 log = Log.instance(context);
123 make = TreeMaker.instance(context);
124 syms = Symtab.instance(context);
125 chk = Check.instance(context);
126 typeEnter = TypeEnter.instance(context);
127 types = Types.instance(context);
128 annotate = Annotate.instance(context);
129 lint = Lint.instance(context);
130 names = Names.instance(context);
131 modules = Modules.instance(context);
132 diags = JCDiagnostic.Factory.instance(context);
133
134 predefClassDef = make.ClassDef(
135 make.Modifiers(PUBLIC),
136 syms.predefClass.name,
137 List.nil(),
138 null,
139 List.nil(),
140 List.nil());
141 predefClassDef.sym = syms.predefClass;
142 todo = Todo.instance(context);
143 fileManager = context.get(JavaFileManager.class);
144
145 Options options = Options.instance(context);
146 pkginfoOpt = PkgInfo.get(options);
147 typeEnvs = TypeEnvs.instance(context);
148 Source source = Source.instance(context);
149 allowPrimitiveClasses = Source.Feature.PRIMITIVE_CLASSES.allowedInSource(source) && options.isSet("enablePrimitiveClasses");
150 }
151
152 /** Accessor for typeEnvs
153 */
154 public Env<AttrContext> getEnv(TypeSymbol sym) {
155 return typeEnvs.get(sym);
156 }
157
158 public Iterable<Env<AttrContext>> getEnvs() {
159 return typeEnvs.values();
160 }
161
162 public Env<AttrContext> getClassEnv(TypeSymbol sym) {
163 Env<AttrContext> localEnv = getEnv(sym);
164 if (localEnv == null) return null;
165 Env<AttrContext> lintEnv = localEnv;
166 while (lintEnv.info.lint == null)
167 lintEnv = lintEnv.next;
168 localEnv.info.lint = lintEnv.info.lint.augment(sym);
169 return localEnv;
490 tree.sym = (ClassSymbol)result.tsym;
491 return;
492 }
493 chk.putCompiled(c);
494 enclScope.enter(c);
495
496 // Set up an environment for class block and store in `typeEnvs'
497 // table, to be retrieved later in memberEnter and attribution.
498 Env<AttrContext> localEnv = classEnv(tree, env);
499 typeEnvs.put(c, localEnv);
500
501 // Fill out class fields.
502 c.completer = Completer.NULL_COMPLETER; // do not allow the initial completer linger on.
503 c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree) | FROM_SOURCE;
504 c.classfile = c.sourcefile = env.toplevel.sourcefile;
505 c.members_field = WriteableScope.create(c);
506 c.isPermittedExplicit = tree.permitting.nonEmpty();
507 c.clearAnnotationMetadata();
508
509 ClassType ct = (ClassType)c.type;
510 if (allowPrimitiveClasses) {
511 ct.flavor = ct.flavor.metamorphose((c.flags_field & PRIMITIVE_CLASS) != 0);
512 }
513
514 if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
515 // We are seeing a local or inner class.
516 // Set outer_field of this class to closest enclosing class
517 // which contains this class in a non-static context
518 // (its "enclosing instance class"), provided such a class exists.
519 Symbol owner1 = owner;
520 while (owner1.kind.matches(KindSelector.VAL_MTH) &&
521 (owner1.flags_field & STATIC) == 0) {
522 owner1 = owner1.owner;
523 }
524 if (owner1.kind == TYP) {
525 ct.setEnclosingType(owner1.type);
526 }
527 }
528
529 // Enter type parameters.
530 ct.typarams_field = classEnter(tree.typarams, localEnv);
531 ct.allparams_field = null;
532 if (allowPrimitiveClasses && ct.isPrimitiveClass()) {
533 if (ct.projection != null) {
534 ct.projection.typarams_field = ct.typarams_field;
535 ct.projection.allparams_field = ct.allparams_field;
536 }
537 }
538
539 // install further completer for this type.
540 c.completer = typeEnter;
541
542 // Add non-local class to uncompleted, to make sure it will be
543 // completed later.
544 if (!c.isDirectlyOrIndirectlyLocal() && uncompleted != null) uncompleted.append(c);
545 // System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
546
547 // Recursively enter all member classes.
548 classEnter(tree.defs, localEnv);
549
550 // Assert.checkNonNull(c.modle, c.sourcefile.toString());
551
552 result = c.type;
553 }
554 //where
555 /** Does class have the same name as the file it appears in?
556 */
557 private static boolean classNameMatchesFileName(ClassSymbol c,
|