15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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.EnumSet;
29 import java.util.Set;
30
31 import com.sun.tools.javac.code.*;
32 import com.sun.tools.javac.code.Scope.WriteableScope;
33 import com.sun.tools.javac.tree.*;
34 import com.sun.tools.javac.util.*;
35 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
36 import com.sun.tools.javac.util.JCDiagnostic.Error;
37 import com.sun.tools.javac.code.Source.Feature;
38
39 import com.sun.tools.javac.code.Symbol.*;
40 import com.sun.tools.javac.code.Type.*;
41 import com.sun.tools.javac.resources.CompilerProperties.Errors;
42 import com.sun.tools.javac.tree.JCTree.*;
43
44 import static com.sun.tools.javac.code.Flags.*;
45 import static com.sun.tools.javac.code.Kinds.*;
46 import static com.sun.tools.javac.code.Kinds.Kind.*;
47 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
48
49 /** Resolves field, method and constructor header, and constructs corresponding Symbols.
50 *
51 * <p><b>This is NOT part of any supported API.
52 * If you write code that depends on this, you do so at your own risk.
53 * This code and its internal interfaces are subject to change or
54 * deletion without notice.</b>
55 */
56 public class MemberEnter extends JCTree.Visitor {
57 protected static final Context.Key<MemberEnter> memberEnterKey = new Context.Key<>();
58
59 /** The Source language setting. */
60 private final Source source;
61 private final Enter enter;
62 private final Log log;
63 private final Check chk;
64 private final Attr attr;
65 private final Symtab syms;
66 private final Annotate annotate;
67 private final Types types;
68 private final Names names;
69
70 public static MemberEnter instance(Context context) {
71 MemberEnter instance = context.get(memberEnterKey);
72 if (instance == null)
73 instance = new MemberEnter(context);
74 return instance;
75 }
76
77 @SuppressWarnings("this-escape")
78 protected MemberEnter(Context context) {
79 context.put(memberEnterKey, this);
80 enter = Enter.instance(context);
81 log = Log.instance(context);
82 chk = Check.instance(context);
83 attr = Attr.instance(context);
84 syms = Symtab.instance(context);
85 annotate = Annotate.instance(context);
86 types = Types.instance(context);
87 source = Source.instance(context);
88 names = Names.instance(context);
89 }
90
91 /** Construct method type from method signature.
92 * @param typarams The method's type parameters.
93 * @param params The method's value parameters.
94 * @param res The method's result type,
95 * null if it is a constructor.
96 * @param recvparam The method's receiver parameter,
97 * null if none given; TODO: or already set here?
98 * @param thrown The method's thrown exceptions.
99 * @param env The method's (local) environment.
100 */
101 Type signature(MethodSymbol msym,
102 List<JCTypeParameter> typarams,
103 List<JCVariableDecl> params,
104 JCTree res,
105 JCVariableDecl recvparam,
106 List<JCExpression> thrown,
107 Env<AttrContext> env) {
108
276 tree.vartype.type = atype.makeVarargs();
277 }
278 WriteableScope enclScope = enter.enterScope(env);
279 Type vartype = switch (tree.declKind) {
280 case IMPLICIT -> tree.type;
281 case EXPLICIT -> tree.vartype.type;
282 case VAR -> tree.type != null ? tree.type
283 : env.info.scope.owner.kind == MTH ? Type.noType
284 : syms.errType;
285 };
286 Name name = tree.name;
287 VarSymbol v = new VarSymbol(0, name, vartype, enclScope.owner);
288 v.flags_field = chk.checkFlags(tree.mods.flags | tree.declKind.additionalSymbolFlags, v, tree);
289 tree.sym = v;
290 if (tree.init != null) {
291 v.flags_field |= HASINIT;
292 if ((v.flags_field & FINAL) != 0 &&
293 needsLazyConstValue(tree.init)) {
294 Env<AttrContext> initEnv = getInitEnv(tree, env);
295 initEnv.info.enclVar = v;
296 v.setLazyConstValue(initEnv(tree, initEnv), env, attr, tree);
297 }
298 }
299
300 if(!(Feature.UNNAMED_VARIABLES.allowedInSource(source) && tree.sym.isUnnamedVariable())) {
301 if (chk.checkUnique(tree.pos(), v, enclScope)) {
302 chk.checkTransparentVar(tree.pos(), v, enclScope);
303 enclScope.enter(v);
304 } else if (v.owner.kind == MTH || (v.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0) {
305 // if this is a parameter or a field obtained from a record component, enter it
306 enclScope.enter(v);
307 }
308 }
309
310 annotate.annotateLater(tree.mods.annotations, localEnv, v);
311 if (!tree.isImplicitlyTyped()) {
312 annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v);
313 }
314
315 v.pos = tree.pos;
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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.EnumSet;
29 import java.util.Set;
30
31 import com.sun.tools.javac.code.*;
32 import com.sun.tools.javac.code.Scope.WriteableScope;
33 import com.sun.tools.javac.tree.*;
34 import com.sun.tools.javac.util.*;
35 import com.sun.tools.javac.util.JCDiagnostic.Error;
36 import com.sun.tools.javac.code.Source.Feature;
37
38 import com.sun.tools.javac.code.Symbol.*;
39 import com.sun.tools.javac.code.Type.*;
40 import com.sun.tools.javac.resources.CompilerProperties.Errors;
41 import com.sun.tools.javac.tree.JCTree.*;
42
43 import static com.sun.tools.javac.code.Flags.*;
44 import static com.sun.tools.javac.code.Kinds.*;
45 import static com.sun.tools.javac.code.Kinds.Kind.*;
46 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
47
48 /** Resolves field, method and constructor header, and constructs corresponding Symbols.
49 *
50 * <p><b>This is NOT part of any supported API.
51 * If you write code that depends on this, you do so at your own risk.
52 * This code and its internal interfaces are subject to change or
53 * deletion without notice.</b>
54 */
55 public class MemberEnter extends JCTree.Visitor {
56 protected static final Context.Key<MemberEnter> memberEnterKey = new Context.Key<>();
57
58 /** The Source language setting. */
59 private final Source source;
60 private final Enter enter;
61 private final Log log;
62 private final Check chk;
63 private final Attr attr;
64 private final Symtab syms;
65 private final Annotate annotate;
66 private final Types types;
67 private final Names names;
68 private final Preview preview;
69 private final boolean allowValueClasses;
70
71 public static MemberEnter instance(Context context) {
72 MemberEnter instance = context.get(memberEnterKey);
73 if (instance == null)
74 instance = new MemberEnter(context);
75 return instance;
76 }
77
78 @SuppressWarnings("this-escape")
79 protected MemberEnter(Context context) {
80 context.put(memberEnterKey, this);
81 enter = Enter.instance(context);
82 log = Log.instance(context);
83 chk = Check.instance(context);
84 attr = Attr.instance(context);
85 syms = Symtab.instance(context);
86 annotate = Annotate.instance(context);
87 types = Types.instance(context);
88 source = Source.instance(context);
89 names = Names.instance(context);
90 preview = Preview.instance(context);
91 allowValueClasses = preview.isEnabled() && Feature.VALUE_CLASSES.allowedInSource(source);
92 }
93
94 /** Construct method type from method signature.
95 * @param typarams The method's type parameters.
96 * @param params The method's value parameters.
97 * @param res The method's result type,
98 * null if it is a constructor.
99 * @param recvparam The method's receiver parameter,
100 * null if none given; TODO: or already set here?
101 * @param thrown The method's thrown exceptions.
102 * @param env The method's (local) environment.
103 */
104 Type signature(MethodSymbol msym,
105 List<JCTypeParameter> typarams,
106 List<JCVariableDecl> params,
107 JCTree res,
108 JCVariableDecl recvparam,
109 List<JCExpression> thrown,
110 Env<AttrContext> env) {
111
279 tree.vartype.type = atype.makeVarargs();
280 }
281 WriteableScope enclScope = enter.enterScope(env);
282 Type vartype = switch (tree.declKind) {
283 case IMPLICIT -> tree.type;
284 case EXPLICIT -> tree.vartype.type;
285 case VAR -> tree.type != null ? tree.type
286 : env.info.scope.owner.kind == MTH ? Type.noType
287 : syms.errType;
288 };
289 Name name = tree.name;
290 VarSymbol v = new VarSymbol(0, name, vartype, enclScope.owner);
291 v.flags_field = chk.checkFlags(tree.mods.flags | tree.declKind.additionalSymbolFlags, v, tree);
292 tree.sym = v;
293 if (tree.init != null) {
294 v.flags_field |= HASINIT;
295 if ((v.flags_field & FINAL) != 0 &&
296 needsLazyConstValue(tree.init)) {
297 Env<AttrContext> initEnv = getInitEnv(tree, env);
298 initEnv.info.enclVar = v;
299 initEnv = initEnv(tree, initEnv);
300 if (v.isStrictInstance() && allowValueClasses) {
301 initEnv.info.earlyContext = EarlyConstructionContext.of((ClassSymbol)v.owner, false);
302 }
303 v.setLazyConstValue(initEnv(tree, initEnv), env, attr, tree);
304 }
305 }
306
307 if(!(Feature.UNNAMED_VARIABLES.allowedInSource(source) && tree.sym.isUnnamedVariable())) {
308 if (chk.checkUnique(tree.pos(), v, enclScope)) {
309 chk.checkTransparentVar(tree.pos(), v, enclScope);
310 enclScope.enter(v);
311 } else if (v.owner.kind == MTH || (v.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0) {
312 // if this is a parameter or a field obtained from a record component, enter it
313 enclScope.enter(v);
314 }
315 }
316
317 annotate.annotateLater(tree.mods.annotations, localEnv, v);
318 if (!tree.isImplicitlyTyped()) {
319 annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v);
320 }
321
322 v.pos = tree.pos;
|