< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java

Print this page

200 
201         if (types.isSignaturePolymorphic(m)) {
202             m.flags_field |= SIGNATURE_POLYMORPHIC;
203         }
204 
205         // Set m.params
206         ListBuffer<VarSymbol> params = new ListBuffer<>();
207         JCVariableDecl lastParam = null;
208         for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
209             JCVariableDecl param = lastParam = l.head;
210             params.append(Assert.checkNonNull(param.sym));
211         }
212         m.params = params.toList();
213 
214         // mark the method varargs, if necessary
215         if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
216             m.flags_field |= Flags.VARARGS;
217 
218         localEnv.info.scope.leave();
219         if (chk.checkUnique(tree.pos(), m, enclScope)) {
220         enclScope.enter(m);
221         }
222 
223         annotate.annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
224         // Visit the signature of the method. Note that
225         // TypeAnnotate doesn't descend into the body.
226         annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, m, tree.pos());
227 
228         if (tree.defaultValue != null) {
229             m.defaultValue = annotate.unfinishedDefaultValue(); // set it to temporary sentinel for now
230             annotate.annotateDefaultValueLater(tree.defaultValue, localEnv, m, tree.pos());
231         }















232     }
233 
234     /** Create a fresh environment for method bodies.
235      *  @param tree     The method definition.
236      *  @param env      The environment current outside of the method definition.
237      */
238     Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
239         Env<AttrContext> localEnv =
240             env.dup(tree, env.info.dup(env.info.scope.dupUnshared(tree.sym)));
241         localEnv.enclMethod = tree;
242         if (tree.sym.type != null) {
243             //when this is called in the enter stage, there's no type to be set
244             localEnv.info.returnResult = attr.new ResultInfo(KindSelector.VAL,
245                                                              tree.sym.type.getReturnType());
246         }
247         if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
248         localEnv.info.yieldResult = null;
249         return localEnv;
250     }
251 






252     public void visitVarDef(JCVariableDecl tree) {
253         Env<AttrContext> localEnv = env;
254         if ((tree.mods.flags & STATIC) != 0 ||
255             (env.info.scope.owner.flags() & INTERFACE) != 0) {
256             localEnv = env.dup(tree, env.info.dup());
257             localEnv.info.staticLevel++;
258         }
259         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
260 
261         try {
262             if (TreeInfo.isEnumInit(tree)) {
263                 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
264             } else if (!tree.isImplicitlyTyped()) {
265                 attr.attribType(tree.vartype, localEnv);
266                 if (TreeInfo.isReceiverParam(tree))
267                     checkReceiver(tree, localEnv);
268             }
269         } finally {
270             deferredLintHandler.setPos(prevLintPos);
271         }
272 
273         if ((tree.mods.flags & VARARGS) != 0) {
274             //if we are entering a varargs parameter, we need to
275             //replace its type (a plain array type) with the more
276             //precise VarargsType --- we need to do it this way
277             //because varargs is represented in the tree as a
278             //modifier on the parameter declaration, and not as a
279             //distinct type of array node.
280             ArrayType atype = (ArrayType)tree.vartype.type;
281             tree.vartype.type = atype.makeVarargs();
282         }
283         WriteableScope enclScope = enter.enterScope(env);
284         Type vartype = tree.isImplicitlyTyped()
285                 ? env.info.scope.owner.kind == MTH ? Type.noType : syms.errType
286                 : tree.vartype.type;
287         VarSymbol v = new VarSymbol(0, tree.name, vartype, enclScope.owner);
288         v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, 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), attr, tree);
297             }
298         }
299         if (chk.checkUnique(tree.pos(), v, enclScope)) {
300             chk.checkTransparentVar(tree.pos(), v, enclScope);
301             enclScope.enter(v);
302         } else if (v.owner.kind == MTH || (v.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0) {
303             // if this is a parameter or a field obtained from a record component, enter it
304             enclScope.enter(v);
305         }
306 
307         annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
308         if (!tree.isImplicitlyTyped()) {
309             annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v, tree.pos());
310         }
311 
312         v.pos = tree.pos;

200 
201         if (types.isSignaturePolymorphic(m)) {
202             m.flags_field |= SIGNATURE_POLYMORPHIC;
203         }
204 
205         // Set m.params
206         ListBuffer<VarSymbol> params = new ListBuffer<>();
207         JCVariableDecl lastParam = null;
208         for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
209             JCVariableDecl param = lastParam = l.head;
210             params.append(Assert.checkNonNull(param.sym));
211         }
212         m.params = params.toList();
213 
214         // mark the method varargs, if necessary
215         if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
216             m.flags_field |= Flags.VARARGS;
217 
218         localEnv.info.scope.leave();
219         if (chk.checkUnique(tree.pos(), m, enclScope)) {
220             enclScope.enter(m);
221         }
222 
223         annotate.annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
224         // Visit the signature of the method. Note that
225         // TypeAnnotate doesn't descend into the body.
226         annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, m, tree.pos());
227 
228         if (tree.defaultValue != null) {
229             m.defaultValue = annotate.unfinishedDefaultValue(); // set it to temporary sentinel for now
230             annotate.annotateDefaultValueLater(tree.defaultValue, localEnv, m, tree.pos());
231         }
232 
233         if (m.isConstructor() && m.type.getParameterTypes().size() == 0) {
234             int statsSize = tree.body.stats.size();
235             if (statsSize == 0) {
236                 m.flags_field |= EMPTYNOARGCONSTR;
237             } else if (statsSize == 1 && TreeInfo.isSuperCall(tree.body.stats.head)) {
238                 JCExpressionStatement exec = (JCExpressionStatement) tree.body.stats.head;
239                 JCMethodInvocation meth = (JCMethodInvocation)exec.expr;
240                 if (meth.args.size() == 0) {
241                     // Deem a constructor "empty" even if it contains a 'super' call,
242                     // as long as it has no argument expressions (to respect common coding style).
243                     m.flags_field |= EMPTYNOARGCONSTR;
244                 }
245             }
246         }
247     }
248 
249     /** Create a fresh environment for method bodies.
250      *  @param tree     The method definition.
251      *  @param env      The environment current outside of the method definition.
252      */
253     Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
254         Env<AttrContext> localEnv =
255             env.dup(tree, env.info.dup(env.info.scope.dupUnshared(tree.sym)));
256         localEnv.enclMethod = tree;
257         if (tree.sym.type != null) {
258             //when this is called in the enter stage, there's no type to be set
259             localEnv.info.returnResult = attr.new ResultInfo(KindSelector.VAL,
260                                                              tree.sym.type.getReturnType());
261         }
262         if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
263         localEnv.info.yieldResult = null;
264         return localEnv;
265     }
266 
267     @Override
268     public void visitBlock(JCBlock tree) {
269         if ((tree.flags & STATIC) == 0 && tree.stats.size() > 0)
270             env.info.scope.owner.flags_field |= HASINITBLOCK;
271     }
272 
273     public void visitVarDef(JCVariableDecl tree) {
274         Env<AttrContext> localEnv = env;
275         if ((tree.mods.flags & STATIC) != 0 ||
276             (env.info.scope.owner.flags() & INTERFACE) != 0) {
277             localEnv = env.dup(tree, env.info.dup());
278             localEnv.info.staticLevel++;
279         }
280         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
281 
282         try {
283             if (TreeInfo.isEnumInit(tree)) {
284                 attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
285             } else if (!tree.isImplicitlyTyped()) {
286                 attr.attribType(tree.vartype, localEnv);
287                 if (TreeInfo.isReceiverParam(tree))
288                     checkReceiver(tree, localEnv);
289             }
290         } finally {
291             deferredLintHandler.setPos(prevLintPos);
292         }
293 
294         if ((tree.mods.flags & VARARGS) != 0) {
295             //if we are entering a varargs parameter, we need to
296             //replace its type (a plain array type) with the more
297             //precise VarargsType --- we need to do it this way
298             //because varargs is represented in the tree as a
299             //modifier on the parameter declaration, and not as a
300             //distinct type of array node.
301             ArrayType atype = (ArrayType)tree.vartype.type;
302             tree.vartype.type = atype.makeVarargs();
303         }
304         WriteableScope enclScope = enter.enterScope(env);
305         Type vartype = tree.isImplicitlyTyped()
306                 ? env.info.scope.owner.kind == MTH ? Type.noType : syms.errType
307                 : tree.vartype.type;
308         VarSymbol v = new VarSymbol(0, tree.name, vartype, enclScope.owner);
309         v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
310         tree.sym = v;
311         /* Don't want constant propagation/folding for instance fields of primitive classes,
312            as these can undergo updates via copy on write.
313         */
314         if (tree.init != null) {
315             v.flags_field |= HASINIT;
316             if ((v.flags_field & FINAL) != 0 && ((v.flags_field & STATIC) != 0 || !v.owner.isValueClass()) &&
317                 needsLazyConstValue(tree.init)) {
318                 Env<AttrContext> initEnv = getInitEnv(tree, env);
319                 initEnv.info.enclVar = v;
320                 v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
321             }
322         }
323         if (chk.checkUnique(tree.pos(), v, enclScope)) {
324             chk.checkTransparentVar(tree.pos(), v, enclScope);
325             enclScope.enter(v);
326         } else if (v.owner.kind == MTH || (v.flags_field & (Flags.PRIVATE | Flags.FINAL | Flags.GENERATED_MEMBER | Flags.RECORD)) != 0) {
327             // if this is a parameter or a field obtained from a record component, enter it
328             enclScope.enter(v);
329         }
330 
331         annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
332         if (!tree.isImplicitlyTyped()) {
333             annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v, tree.pos());
334         }
335 
336         v.pos = tree.pos;
< prev index next >