Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
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 */
26 package com.sun.tools.javac.comp;
28 import java.util.*;
30 import com.sun.tools.javac.code.*;
31 import com.sun.tools.javac.jvm.*;
32 import com.sun.tools.javac.main.RecognizedOptions.PkgInfo;
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.List;
38 import com.sun.tools.javac.code.Symbol.*;
39 import com.sun.tools.javac.tree.JCTree.*;
40 import com.sun.tools.javac.code.Type.*;
42 import com.sun.tools.javac.jvm.Target;
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.TypeTags.*;
47 import static com.sun.tools.javac.jvm.ByteCodes.*;
49 /** This pass translates away some syntactic sugar: inner classes,
50 * class literals, assertions, foreach loops, etc.
51 *
52 * <p><b>This is NOT part of any supported API.
53 * If you write code that depends on this, you do so at your own risk.
54 * This code and its internal interfaces are subject to change or
55 * deletion without notice.</b>
56 */
57 public class Lower extends TreeTranslator {
58 protected static final Context.Key<Lower> lowerKey =
59 new Context.Key<Lower>();
61 public static Lower instance(Context context) {
62 Lower instance = context.get(lowerKey);
63 if (instance == null)
64 instance = new Lower(context);
65 return instance;
66 }
68 private Names names;
69 private Log log;
70 private Symtab syms;
71 private Resolve rs;
72 private Check chk;
73 private Attr attr;
74 private TreeMaker make;
75 private DiagnosticPosition make_pos;
76 private ClassWriter writer;
77 private ClassReader reader;
78 private ConstFold cfolder;
79 private Target target;
80 private Source source;
81 private boolean allowEnums;
82 private final Name dollarAssertionsDisabled;
83 private final Name classDollar;
84 private Types types;
85 private boolean debugLower;
86 private PkgInfo pkginfoOpt;
88 protected Lower(Context context) {
89 context.put(lowerKey, this);
90 names = Names.instance(context);
91 log = Log.instance(context);
92 syms = Symtab.instance(context);
93 rs = Resolve.instance(context);
94 chk = Check.instance(context);
95 attr = Attr.instance(context);
96 make = TreeMaker.instance(context);
97 writer = ClassWriter.instance(context);
98 reader = ClassReader.instance(context);
99 cfolder = ConstFold.instance(context);
100 target = Target.instance(context);
101 source = Source.instance(context);
102 allowEnums = source.allowEnums();
103 dollarAssertionsDisabled = names.
104 fromString(target.syntheticNameChar() + "assertionsDisabled");
105 classDollar = names.
106 fromString("class" + target.syntheticNameChar());
108 types = Types.instance(context);
109 Options options = Options.instance(context);
110 debugLower = options.isSet("debuglower");
111 pkginfoOpt = PkgInfo.get(options);
112 }
114 /** The currently enclosing class.
115 */
116 ClassSymbol currentClass;
118 /** A queue of all translated classes.
119 */
120 ListBuffer<JCTree> translated;
122 /** Environment for symbol lookup, set by translateTopLevelClass.
123 */
124 Env<AttrContext> attrEnv;
126 /** A hash table mapping syntax trees to their ending source positions.
127 */
128 Map<JCTree, Integer> endPositions;
130 /**************************************************************************
131 * Global mappings
132 *************************************************************************/
134 /** A hash table mapping local classes to their definitions.
135 */
136 Map<ClassSymbol, JCClassDecl> classdefs;
138 /** A hash table mapping virtual accessed symbols in outer subclasses
139 * to the actually referred symbol in superclasses.
140 */
141 Map<Symbol,Symbol> actualSymbols;
143 /** The current method definition.
144 */
145 JCMethodDecl currentMethodDef;
147 /** The current method symbol.
148 */
149 MethodSymbol currentMethodSym;
151 /** The currently enclosing outermost class definition.
152 */
153 JCClassDecl outermostClassDef;
155 /** The currently enclosing outermost member definition.
156 */
157 JCTree outermostMemberDef;
159 /** A navigator class for assembling a mapping from local class symbols
160 * to class definition trees.
161 * There is only one case; all other cases simply traverse down the tree.
162 */
163 class ClassMap extends TreeScanner {
165 /** All encountered class defs are entered into classdefs table.
166 */
167 public void visitClassDef(JCClassDecl tree) {
168 classdefs.put(tree.sym, tree);
169 super.visitClassDef(tree);
170 }
171 }
172 ClassMap classMap = new ClassMap();
174 /** Map a class symbol to its definition.
175 * @param c The class symbol of which we want to determine the definition.
176 */
177 JCClassDecl classDef(ClassSymbol c) {
178 // First lookup the class in the classdefs table.
179 JCClassDecl def = classdefs.get(c);
180 if (def == null && outermostMemberDef != null) {
181 // If this fails, traverse outermost member definition, entering all
182 // local classes into classdefs, and try again.
183 classMap.scan(outermostMemberDef);
184 def = classdefs.get(c);
185 }
186 if (def == null) {
187 // If this fails, traverse outermost class definition, entering all
188 // local classes into classdefs, and try again.
189 classMap.scan(outermostClassDef);
190 def = classdefs.get(c);
191 }
192 return def;
193 }
195 /** A hash table mapping class symbols to lists of free variables.
196 * accessed by them. Only free variables of the method immediately containing
197 * a class are associated with that class.
198 */
199 Map<ClassSymbol,List<VarSymbol>> freevarCache;
201 /** A navigator class for collecting the free variables accessed
202 * from a local class.
203 * There is only one case; all other cases simply traverse down the tree.
204 */
205 class FreeVarCollector extends TreeScanner {
207 /** The owner of the local class.
208 */
209 Symbol owner;
211 /** The local class.
212 */
213 ClassSymbol clazz;
215 /** The list of owner's variables accessed from within the local class,
216 * without any duplicates.
217 */
218 List<VarSymbol> fvs;
220 FreeVarCollector(ClassSymbol clazz) {
221 this.clazz = clazz;
222 this.owner = clazz.owner;
223 this.fvs = List.nil();
224 }
226 /** Add free variable to fvs list unless it is already there.
227 */
228 private void addFreeVar(VarSymbol v) {
229 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail)
230 if (l.head == v) return;
231 fvs = fvs.prepend(v);
232 }
234 /** Add all free variables of class c to fvs list
235 * unless they are already there.
236 */
237 private void addFreeVars(ClassSymbol c) {
238 List<VarSymbol> fvs = freevarCache.get(c);
239 if (fvs != null) {
240 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) {
241 addFreeVar(l.head);
242 }
243 }
244 }
246 /** If tree refers to a variable in owner of local class, add it to
247 * free variables list.
248 */
249 public void visitIdent(JCIdent tree) {
250 result = tree;
251 visitSymbol(tree.sym);
252 }
253 // where
254 private void visitSymbol(Symbol _sym) {
255 Symbol sym = _sym;
256 if (sym.kind == VAR || sym.kind == MTH) {
257 while (sym != null && sym.owner != owner)
258 sym = proxies.lookup(proxyName(sym.name)).sym;
259 if (sym != null && sym.owner == owner) {
260 VarSymbol v = (VarSymbol)sym;
261 if (v.getConstValue() == null) {
262 addFreeVar(v);
263 }
264 } else {
265 if (outerThisStack.head != null &&
266 outerThisStack.head != _sym)
267 visitSymbol(outerThisStack.head);
268 }
269 }
270 }
272 /** If tree refers to a class instance creation expression
273 * add all free variables of the freshly created class.
274 */
275 public void visitNewClass(JCNewClass tree) {
276 ClassSymbol c = (ClassSymbol)tree.constructor.owner;
277 addFreeVars(c);
278 if (tree.encl == null &&
279 c.hasOuterInstance() &&
280 outerThisStack.head != null)
281 visitSymbol(outerThisStack.head);
282 super.visitNewClass(tree);
283 }
285 /** If tree refers to a qualified this or super expression
286 * for anything but the current class, add the outer this
287 * stack as a free variable.
288 */
289 public void visitSelect(JCFieldAccess tree) {
290 if ((tree.name == names._this || tree.name == names._super) &&
291 tree.selected.type.tsym != clazz &&
292 outerThisStack.head != null)
293 visitSymbol(outerThisStack.head);
294 super.visitSelect(tree);
295 }
297 /** If tree refers to a superclass constructor call,
298 * add all free variables of the superclass.
299 */
300 public void visitApply(JCMethodInvocation tree) {
301 if (TreeInfo.name(tree.meth) == names._super) {
302 addFreeVars((ClassSymbol) TreeInfo.symbol(tree.meth).owner);
303 Symbol constructor = TreeInfo.symbol(tree.meth);
304 ClassSymbol c = (ClassSymbol)constructor.owner;
305 if (c.hasOuterInstance() &&
306 tree.meth.getTag() != JCTree.SELECT &&
307 outerThisStack.head != null)
308 visitSymbol(outerThisStack.head);
309 }
310 super.visitApply(tree);
311 }
312 }
314 /** Return the variables accessed from within a local class, which
315 * are declared in the local class' owner.
316 * (in reverse order of first access).
317 */
318 List<VarSymbol> freevars(ClassSymbol c) {
319 if ((c.owner.kind & (VAR | MTH)) != 0) {
320 List<VarSymbol> fvs = freevarCache.get(c);
321 if (fvs == null) {
322 FreeVarCollector collector = new FreeVarCollector(c);
323 collector.scan(classDef(c));
324 fvs = collector.fvs;
325 freevarCache.put(c, fvs);
326 }
327 return fvs;
328 } else {
329 return List.nil();
330 }
331 }
333 Map<TypeSymbol,EnumMapping> enumSwitchMap = new LinkedHashMap<TypeSymbol,EnumMapping>();
335 EnumMapping mapForEnum(DiagnosticPosition pos, TypeSymbol enumClass) {
336 EnumMapping map = enumSwitchMap.get(enumClass);
337 if (map == null)
338 enumSwitchMap.put(enumClass, map = new EnumMapping(pos, enumClass));
339 return map;
340 }
342 /** This map gives a translation table to be used for enum
343 * switches.
344 *
345 * <p>For each enum that appears as the type of a switch
346 * expression, we maintain an EnumMapping to assist in the
347 * translation, as exemplified by the following example:
348 *
349 * <p>we translate
350 * <pre>
351 * switch(colorExpression) {
352 * case red: stmt1;
353 * case green: stmt2;
354 * }
355 * </pre>
356 * into
357 * <pre>
358 * switch(Outer$0.$EnumMap$Color[colorExpression.ordinal()]) {
359 * case 1: stmt1;
360 * case 2: stmt2
361 * }
362 * </pre>
363 * with the auxiliary table initialized as follows:
364 * <pre>
365 * class Outer$0 {
366 * synthetic final int[] $EnumMap$Color = new int[Color.values().length];
367 * static {
368 * try { $EnumMap$Color[red.ordinal()] = 1; } catch (NoSuchFieldError ex) {}
369 * try { $EnumMap$Color[green.ordinal()] = 2; } catch (NoSuchFieldError ex) {}
370 * }
371 * }
372 * </pre>
373 * class EnumMapping provides mapping data and support methods for this translation.
374 */
375 class EnumMapping {
376 EnumMapping(DiagnosticPosition pos, TypeSymbol forEnum) {
377 this.forEnum = forEnum;
378 this.values = new LinkedHashMap<VarSymbol,Integer>();
379 this.pos = pos;
380 Name varName = names
381 .fromString(target.syntheticNameChar() +
382 "SwitchMap" +
383 target.syntheticNameChar() +
384 writer.xClassName(forEnum.type).toString()
385 .replace('/', '.')
386 .replace('.', target.syntheticNameChar()));
387 ClassSymbol outerCacheClass = outerCacheClass();
388 this.mapVar = new VarSymbol(STATIC | SYNTHETIC | FINAL,
389 varName,
390 new ArrayType(syms.intType, syms.arrayClass),
391 outerCacheClass);
392 enterSynthetic(pos, mapVar, outerCacheClass.members());
393 }
395 DiagnosticPosition pos = null;
397 // the next value to use
398 int next = 1; // 0 (unused map elements) go to the default label
400 // the enum for which this is a map
401 final TypeSymbol forEnum;
403 // the field containing the map
404 final VarSymbol mapVar;
406 // the mapped values
407 final Map<VarSymbol,Integer> values;
409 JCLiteral forConstant(VarSymbol v) {
410 Integer result = values.get(v);
411 if (result == null)
412 values.put(v, result = next++);
413 return make.Literal(result);
414 }
416 // generate the field initializer for the map
417 void translate() {
418 make.at(pos.getStartPosition());
419 JCClassDecl owner = classDef((ClassSymbol)mapVar.owner);
421 // synthetic static final int[] $SwitchMap$Color = new int[Color.values().length];
422 MethodSymbol valuesMethod = lookupMethod(pos,
423 names.values,
424 forEnum.type,
425 List.<Type>nil());
426 JCExpression size = make // Color.values().length
427 .Select(make.App(make.QualIdent(valuesMethod)),
428 syms.lengthVar);
429 JCExpression mapVarInit = make
430 .NewArray(make.Type(syms.intType), List.of(size), null)
431 .setType(new ArrayType(syms.intType, syms.arrayClass));
433 // try { $SwitchMap$Color[red.ordinal()] = 1; } catch (java.lang.NoSuchFieldError ex) {}
434 ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
435 Symbol ordinalMethod = lookupMethod(pos,
436 names.ordinal,
437 forEnum.type,
438 List.<Type>nil());
439 List<JCCatch> catcher = List.<JCCatch>nil()
440 .prepend(make.Catch(make.VarDef(new VarSymbol(PARAMETER, names.ex,
441 syms.noSuchFieldErrorType,
442 syms.noSymbol),
443 null),
444 make.Block(0, List.<JCStatement>nil())));
445 for (Map.Entry<VarSymbol,Integer> e : values.entrySet()) {
446 VarSymbol enumerator = e.getKey();
447 Integer mappedValue = e.getValue();
448 JCExpression assign = make
449 .Assign(make.Indexed(mapVar,
450 make.App(make.Select(make.QualIdent(enumerator),
451 ordinalMethod))),
452 make.Literal(mappedValue))
453 .setType(syms.intType);
454 JCStatement exec = make.Exec(assign);
455 JCStatement _try = make.Try(make.Block(0, List.of(exec)), catcher, null);
456 stmts.append(_try);
457 }
459 owner.defs = owner.defs
460 .prepend(make.Block(STATIC, stmts.toList()))
461 .prepend(make.VarDef(mapVar, mapVarInit));
462 }
463 }
466 /**************************************************************************
467 * Tree building blocks
468 *************************************************************************/
470 /** Equivalent to make.at(pos.getStartPosition()) with side effect of caching
471 * pos as make_pos, for use in diagnostics.
472 **/
473 TreeMaker make_at(DiagnosticPosition pos) {
474 make_pos = pos;
475 return make.at(pos);
476 }
478 /** Make an attributed tree representing a literal. This will be an
479 * Ident node in the case of boolean literals, a Literal node in all
480 * other cases.
481 * @param type The literal's type.
482 * @param value The literal's value.
483 */
484 JCExpression makeLit(Type type, Object value) {
485 return make.Literal(type.tag, value).setType(type.constType(value));
486 }
488 /** Make an attributed tree representing null.
489 */
490 JCExpression makeNull() {
491 return makeLit(syms.botType, null);
492 }
494 /** Make an attributed class instance creation expression.
495 * @param ctype The class type.
496 * @param args The constructor arguments.
497 */
498 JCNewClass makeNewClass(Type ctype, List<JCExpression> args) {
499 JCNewClass tree = make.NewClass(null,
500 null, make.QualIdent(ctype.tsym), args, null);
501 tree.constructor = rs.resolveConstructor(
502 make_pos, attrEnv, ctype, TreeInfo.types(args), null, false, false);
503 tree.type = ctype;
504 return tree;
505 }
507 /** Make an attributed unary expression.
508 * @param optag The operators tree tag.
509 * @param arg The operator's argument.
510 */
511 JCUnary makeUnary(int optag, JCExpression arg) {
512 JCUnary tree = make.Unary(optag, arg);
513 tree.operator = rs.resolveUnaryOperator(
514 make_pos, optag, attrEnv, arg.type);
515 tree.type = tree.operator.type.getReturnType();
516 return tree;
517 }
519 /** Make an attributed binary expression.
520 * @param optag The operators tree tag.
521 * @param lhs The operator's left argument.
522 * @param rhs The operator's right argument.
523 */
524 JCBinary makeBinary(int optag, JCExpression lhs, JCExpression rhs) {
525 JCBinary tree = make.Binary(optag, lhs, rhs);
526 tree.operator = rs.resolveBinaryOperator(
527 make_pos, optag, attrEnv, lhs.type, rhs.type);
528 tree.type = tree.operator.type.getReturnType();
529 return tree;
530 }
532 /** Make an attributed assignop expression.
533 * @param optag The operators tree tag.
534 * @param lhs The operator's left argument.
535 * @param rhs The operator's right argument.
536 */
537 JCAssignOp makeAssignop(int optag, JCTree lhs, JCTree rhs) {
538 JCAssignOp tree = make.Assignop(optag, lhs, rhs);
539 tree.operator = rs.resolveBinaryOperator(
540 make_pos, tree.getTag() - JCTree.ASGOffset, attrEnv, lhs.type, rhs.type);
541 tree.type = lhs.type;
542 return tree;
543 }
545 /** Convert tree into string object, unless it has already a
546 * reference type..
547 */
548 JCExpression makeString(JCExpression tree) {
549 if (tree.type.tag >= CLASS) {
550 return tree;
551 } else {
552 Symbol valueOfSym = lookupMethod(tree.pos(),
553 names.valueOf,
554 syms.stringType,
555 List.of(tree.type));
556 return make.App(make.QualIdent(valueOfSym), List.of(tree));
557 }
558 }
560 /** Create an empty anonymous class definition and enter and complete
561 * its symbol. Return the class definition's symbol.
562 * and create
563 * @param flags The class symbol's flags
564 * @param owner The class symbol's owner
565 */
566 ClassSymbol makeEmptyClass(long flags, ClassSymbol owner) {
567 // Create class symbol.
568 ClassSymbol c = reader.defineClass(names.empty, owner);
569 c.flatname = chk.localClassName(c);
570 c.sourcefile = owner.sourcefile;
571 c.completer = null;
572 c.members_field = new Scope(c);
573 c.flags_field = flags;
574 ClassType ctype = (ClassType) c.type;
575 ctype.supertype_field = syms.objectType;
576 ctype.interfaces_field = List.nil();
578 JCClassDecl odef = classDef(owner);
580 // Enter class symbol in owner scope and compiled table.
581 enterSynthetic(odef.pos(), c, owner.members());
582 chk.compiled.put(c.flatname, c);
584 // Create class definition tree.
585 JCClassDecl cdef = make.ClassDef(
586 make.Modifiers(flags), names.empty,
587 List.<JCTypeParameter>nil(),
588 null, List.<JCExpression>nil(), List.<JCTree>nil());
589 cdef.sym = c;
590 cdef.type = c.type;
592 // Append class definition tree to owner's definitions.
593 odef.defs = odef.defs.prepend(cdef);
595 return c;
596 }
598 /**************************************************************************
599 * Symbol manipulation utilities
600 *************************************************************************/
602 /** Enter a synthetic symbol in a given scope, but complain if there was already one there.
603 * @param pos Position for error reporting.
604 * @param sym The symbol.
605 * @param s The scope.
606 */
607 private void enterSynthetic(DiagnosticPosition pos, Symbol sym, Scope s) {
608 s.enter(sym);
609 }
611 /** Create a fresh synthetic name within a given scope - the unique name is
612 * obtained by appending '$' chars at the end of the name until no match
613 * is found.
614 *
615 * @param name base name
616 * @param s scope in which the name has to be unique
617 * @return fresh synthetic name
618 */
619 private Name makeSyntheticName(Name name, Scope s) {
620 do {
621 name = name.append(
622 target.syntheticNameChar(),
623 names.empty);
624 } while (lookupSynthetic(name, s) != null);
625 return name;
626 }
628 /** Check whether synthetic symbols generated during lowering conflict
629 * with user-defined symbols.
630 *
631 * @param translatedTrees lowered class trees
632 */
633 void checkConflicts(List<JCTree> translatedTrees) {
634 for (JCTree t : translatedTrees) {
635 t.accept(conflictsChecker);
636 }
637 }
639 JCTree.Visitor conflictsChecker = new TreeScanner() {
641 TypeSymbol currentClass;
643 @Override
644 public void visitMethodDef(JCMethodDecl that) {
645 chk.checkConflicts(that.pos(), that.sym, currentClass);
646 super.visitMethodDef(that);
647 }
649 @Override
650 public void visitVarDef(JCVariableDecl that) {
651 if (that.sym.owner.kind == TYP) {
652 chk.checkConflicts(that.pos(), that.sym, currentClass);
653 }
654 super.visitVarDef(that);
655 }
657 @Override
658 public void visitClassDef(JCClassDecl that) {
659 TypeSymbol prevCurrentClass = currentClass;
660 currentClass = that.sym;
661 try {
662 super.visitClassDef(that);
663 }
664 finally {
665 currentClass = prevCurrentClass;
666 }
667 }
668 };
670 /** Look up a synthetic name in a given scope.
671 * @param scope The scope.
672 * @param name The name.
673 */
674 private Symbol lookupSynthetic(Name name, Scope s) {
675 Symbol sym = s.lookup(name).sym;
676 return (sym==null || (sym.flags()&SYNTHETIC)==0) ? null : sym;
677 }
679 /** Look up a method in a given scope.
680 */
681 private MethodSymbol lookupMethod(DiagnosticPosition pos, Name name, Type qual, List<Type> args) {
682 return rs.resolveInternalMethod(pos, attrEnv, qual, name, args, null);
683 }
685 /** Look up a constructor.
686 */
687 private MethodSymbol lookupConstructor(DiagnosticPosition pos, Type qual, List<Type> args) {
688 return rs.resolveInternalConstructor(pos, attrEnv, qual, args, null);
689 }
691 /** Look up a field.
692 */
693 private VarSymbol lookupField(DiagnosticPosition pos, Type qual, Name name) {
694 return rs.resolveInternalField(pos, attrEnv, qual, name);
695 }
697 /** Anon inner classes are used as access constructor tags.
698 * accessConstructorTag will use an existing anon class if one is available,
699 * and synthethise a class (with makeEmptyClass) if one is not available.
700 * However, there is a small possibility that an existing class will not
701 * be generated as expected if it is inside a conditional with a constant
702 * expression. If that is found to be the case, create an empty class here.
703 */
704 private void checkAccessConstructorTags() {
705 for (List<ClassSymbol> l = accessConstrTags; l.nonEmpty(); l = l.tail) {
706 ClassSymbol c = l.head;
707 if (isTranslatedClassAvailable(c))
708 continue;
709 // Create class definition tree.
710 JCClassDecl cdef = make.ClassDef(
711 make.Modifiers(STATIC | SYNTHETIC), names.empty,
712 List.<JCTypeParameter>nil(),
713 null, List.<JCExpression>nil(), List.<JCTree>nil());
714 cdef.sym = c;
715 cdef.type = c.type;
716 // add it to the list of classes to be generated
717 translated.append(cdef);
718 }
719 }
720 // where
721 private boolean isTranslatedClassAvailable(ClassSymbol c) {
722 for (JCTree tree: translated) {
723 if (tree.getTag() == JCTree.CLASSDEF
724 && ((JCClassDecl) tree).sym == c) {
725 return true;
726 }
727 }
728 return false;
729 }
731 /**************************************************************************
732 * Access methods
733 *************************************************************************/
735 /** Access codes for dereferencing, assignment,
736 * and pre/post increment/decrement.
737 * Access codes for assignment operations are determined by method accessCode
738 * below.
739 *
740 * All access codes for accesses to the current class are even.
741 * If a member of the superclass should be accessed instead (because
742 * access was via a qualified super), add one to the corresponding code
743 * for the current class, making the number odd.
744 * This numbering scheme is used by the backend to decide whether
745 * to issue an invokevirtual or invokespecial call.
746 *
747 * @see Gen.visitSelect(Select tree)
748 */
749 private static final int
750 DEREFcode = 0,
751 ASSIGNcode = 2,
752 PREINCcode = 4,
753 PREDECcode = 6,
754 POSTINCcode = 8,
755 POSTDECcode = 10,
756 FIRSTASGOPcode = 12;
758 /** Number of access codes
759 */
760 private static final int NCODES = accessCode(ByteCodes.lushrl) + 2;
762 /** A mapping from symbols to their access numbers.
763 */
764 private Map<Symbol,Integer> accessNums;
766 /** A mapping from symbols to an array of access symbols, indexed by
767 * access code.
768 */
769 private Map<Symbol,MethodSymbol[]> accessSyms;
771 /** A mapping from (constructor) symbols to access constructor symbols.
772 */
773 private Map<Symbol,MethodSymbol> accessConstrs;
775 /** A list of all class symbols used for access constructor tags.
776 */
777 private List<ClassSymbol> accessConstrTags;
779 /** A queue for all accessed symbols.
780 */
781 private ListBuffer<Symbol> accessed;
783 /** Map bytecode of binary operation to access code of corresponding
784 * assignment operation. This is always an even number.
785 */
786 private static int accessCode(int bytecode) {
787 if (ByteCodes.iadd <= bytecode && bytecode <= ByteCodes.lxor)
788 return (bytecode - iadd) * 2 + FIRSTASGOPcode;
789 else if (bytecode == ByteCodes.string_add)
790 return (ByteCodes.lxor + 1 - iadd) * 2 + FIRSTASGOPcode;
791 else if (ByteCodes.ishll <= bytecode && bytecode <= ByteCodes.lushrl)
792 return (bytecode - ishll + ByteCodes.lxor + 2 - iadd) * 2 + FIRSTASGOPcode;
793 else
794 return -1;
795 }
797 /** return access code for identifier,
798 * @param tree The tree representing the identifier use.
799 * @param enclOp The closest enclosing operation node of tree,
800 * null if tree is not a subtree of an operation.
801 */
802 private static int accessCode(JCTree tree, JCTree enclOp) {
803 if (enclOp == null)
804 return DEREFcode;
805 else if (enclOp.getTag() == JCTree.ASSIGN &&
806 tree == TreeInfo.skipParens(((JCAssign) enclOp).lhs))
807 return ASSIGNcode;
808 else if (JCTree.PREINC <= enclOp.getTag() && enclOp.getTag() <= JCTree.POSTDEC &&
809 tree == TreeInfo.skipParens(((JCUnary) enclOp).arg))
810 return (enclOp.getTag() - JCTree.PREINC) * 2 + PREINCcode;
811 else if (JCTree.BITOR_ASG <= enclOp.getTag() && enclOp.getTag() <= JCTree.MOD_ASG &&
812 tree == TreeInfo.skipParens(((JCAssignOp) enclOp).lhs))
813 return accessCode(((OperatorSymbol) ((JCAssignOp) enclOp).operator).opcode);
814 else
815 return DEREFcode;
816 }
818 /** Return binary operator that corresponds to given access code.
819 */
820 private OperatorSymbol binaryAccessOperator(int acode) {
821 for (Scope.Entry e = syms.predefClass.members().elems;
822 e != null;
823 e = e.sibling) {
824 if (e.sym instanceof OperatorSymbol) {
825 OperatorSymbol op = (OperatorSymbol)e.sym;
826 if (accessCode(op.opcode) == acode) return op;
827 }
828 }
829 return null;
830 }
832 /** Return tree tag for assignment operation corresponding
833 * to given binary operator.
834 */
835 private static int treeTag(OperatorSymbol operator) {
836 switch (operator.opcode) {
837 case ByteCodes.ior: case ByteCodes.lor:
838 return JCTree.BITOR_ASG;
839 case ByteCodes.ixor: case ByteCodes.lxor:
840 return JCTree.BITXOR_ASG;
841 case ByteCodes.iand: case ByteCodes.land:
842 return JCTree.BITAND_ASG;
843 case ByteCodes.ishl: case ByteCodes.lshl:
844 case ByteCodes.ishll: case ByteCodes.lshll:
845 return JCTree.SL_ASG;
846 case ByteCodes.ishr: case ByteCodes.lshr:
847 case ByteCodes.ishrl: case ByteCodes.lshrl:
848 return JCTree.SR_ASG;
849 case ByteCodes.iushr: case ByteCodes.lushr:
850 case ByteCodes.iushrl: case ByteCodes.lushrl:
851 return JCTree.USR_ASG;
852 case ByteCodes.iadd: case ByteCodes.ladd:
853 case ByteCodes.fadd: case ByteCodes.dadd:
854 case ByteCodes.string_add:
855 return JCTree.PLUS_ASG;
856 case ByteCodes.isub: case ByteCodes.lsub:
857 case ByteCodes.fsub: case ByteCodes.dsub:
858 return JCTree.MINUS_ASG;
859 case ByteCodes.imul: case ByteCodes.lmul:
860 case ByteCodes.fmul: case ByteCodes.dmul:
861 return JCTree.MUL_ASG;
862 case ByteCodes.idiv: case ByteCodes.ldiv:
863 case ByteCodes.fdiv: case ByteCodes.ddiv:
864 return JCTree.DIV_ASG;
865 case ByteCodes.imod: case ByteCodes.lmod:
866 case ByteCodes.fmod: case ByteCodes.dmod:
867 return JCTree.MOD_ASG;
868 default:
869 throw new AssertionError();
870 }
871 }
873 /** The name of the access method with number `anum' and access code `acode'.
874 */
875 Name accessName(int anum, int acode) {
876 return names.fromString(
877 "access" + target.syntheticNameChar() + anum + acode / 10 + acode % 10);
878 }
880 /** Return access symbol for a private or protected symbol from an inner class.
881 * @param sym The accessed private symbol.
882 * @param tree The accessing tree.
883 * @param enclOp The closest enclosing operation node of tree,
884 * null if tree is not a subtree of an operation.
885 * @param protAccess Is access to a protected symbol in another
886 * package?
887 * @param refSuper Is access via a (qualified) C.super?
888 */
889 MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp,
890 boolean protAccess, boolean refSuper) {
891 ClassSymbol accOwner = refSuper && protAccess
892 // For access via qualified super (T.super.x), place the
893 // access symbol on T.
894 ? (ClassSymbol)((JCFieldAccess) tree).selected.type.tsym
895 // Otherwise pretend that the owner of an accessed
896 // protected symbol is the enclosing class of the current
897 // class which is a subclass of the symbol's owner.
898 : accessClass(sym, protAccess, tree);
900 Symbol vsym = sym;
901 if (sym.owner != accOwner) {
902 vsym = sym.clone(accOwner);
903 actualSymbols.put(vsym, sym);
904 }
906 Integer anum // The access number of the access method.
907 = accessNums.get(vsym);
908 if (anum == null) {
909 anum = accessed.length();
910 accessNums.put(vsym, anum);
911 accessSyms.put(vsym, new MethodSymbol[NCODES]);
912 accessed.append(vsym);
913 // System.out.println("accessing " + vsym + " in " + vsym.location());
914 }
916 int acode; // The access code of the access method.
917 List<Type> argtypes; // The argument types of the access method.
918 Type restype; // The result type of the access method.
919 List<Type> thrown; // The thrown exceptions of the access method.
920 switch (vsym.kind) {
921 case VAR:
922 acode = accessCode(tree, enclOp);
923 if (acode >= FIRSTASGOPcode) {
924 OperatorSymbol operator = binaryAccessOperator(acode);
925 if (operator.opcode == string_add)
926 argtypes = List.of(syms.objectType);
927 else
928 argtypes = operator.type.getParameterTypes().tail;
929 } else if (acode == ASSIGNcode)
930 argtypes = List.of(vsym.erasure(types));
931 else
932 argtypes = List.nil();
933 restype = vsym.erasure(types);
934 thrown = List.nil();
935 break;
936 case MTH:
937 acode = DEREFcode;
938 argtypes = vsym.erasure(types).getParameterTypes();
939 restype = vsym.erasure(types).getReturnType();
940 thrown = vsym.type.getThrownTypes();
941 break;
942 default:
943 throw new AssertionError();
944 }
946 // For references via qualified super, increment acode by one,
947 // making it odd.
948 if (protAccess && refSuper) acode++;
950 // Instance access methods get instance as first parameter.
951 // For protected symbols this needs to be the instance as a member
952 // of the type containing the accessed symbol, not the class
953 // containing the access method.
954 if ((vsym.flags() & STATIC) == 0) {
955 argtypes = argtypes.prepend(vsym.owner.erasure(types));
956 }
957 MethodSymbol[] accessors = accessSyms.get(vsym);
958 MethodSymbol accessor = accessors[acode];
959 if (accessor == null) {
960 accessor = new MethodSymbol(
961 STATIC | SYNTHETIC,
962 accessName(anum.intValue(), acode),
963 new MethodType(argtypes, restype, thrown, syms.methodClass),
964 accOwner);
965 enterSynthetic(tree.pos(), accessor, accOwner.members());
966 accessors[acode] = accessor;
967 }
968 return accessor;
969 }
971 /** The qualifier to be used for accessing a symbol in an outer class.
972 * This is either C.sym or C.this.sym, depending on whether or not
973 * sym is static.
974 * @param sym The accessed symbol.
975 */
976 JCExpression accessBase(DiagnosticPosition pos, Symbol sym) {
977 return (sym.flags() & STATIC) != 0
978 ? access(make.at(pos.getStartPosition()).QualIdent(sym.owner))
979 : makeOwnerThis(pos, sym, true);
980 }
982 /** Do we need an access method to reference private symbol?
983 */
984 boolean needsPrivateAccess(Symbol sym) {
985 if ((sym.flags() & PRIVATE) == 0 || sym.owner == currentClass) {
986 return false;
987 } else if (sym.name == names.init && (sym.owner.owner.kind & (VAR | MTH)) != 0) {
988 // private constructor in local class: relax protection
989 sym.flags_field &= ~PRIVATE;
990 return false;
991 } else {
992 return true;
993 }
994 }
996 /** Do we need an access method to reference symbol in other package?
997 */
998 boolean needsProtectedAccess(Symbol sym, JCTree tree) {
999 if ((sym.flags() & PROTECTED) == 0 ||
1000 sym.owner.owner == currentClass.owner || // fast special case
1001 sym.packge() == currentClass.packge())
1002 return false;
1003 if (!currentClass.isSubClass(sym.owner, types))
1004 return true;
1005 if ((sym.flags() & STATIC) != 0 ||
1006 tree.getTag() != JCTree.SELECT ||
1007 TreeInfo.name(((JCFieldAccess) tree).selected) == names._super)
1008 return false;
1009 return !((JCFieldAccess) tree).selected.type.tsym.isSubClass(currentClass, types);
1010 }
1012 /** The class in which an access method for given symbol goes.
1013 * @param sym The access symbol
1014 * @param protAccess Is access to a protected symbol in another
1015 * package?
1016 */
1017 ClassSymbol accessClass(Symbol sym, boolean protAccess, JCTree tree) {
1018 if (protAccess) {
1019 Symbol qualifier = null;
1020 ClassSymbol c = currentClass;
1021 if (tree.getTag() == JCTree.SELECT && (sym.flags() & STATIC) == 0) {
1022 qualifier = ((JCFieldAccess) tree).selected.type.tsym;
1023 while (!qualifier.isSubClass(c, types)) {
1024 c = c.owner.enclClass();
1025 }
1026 return c;
1027 } else {
1028 while (!c.isSubClass(sym.owner, types)) {
1029 c = c.owner.enclClass();
1030 }
1031 }
1032 return c;
1033 } else {
1034 // the symbol is private
1035 return sym.owner.enclClass();
1036 }
1037 }
1039 /** Ensure that identifier is accessible, return tree accessing the identifier.
1040 * @param sym The accessed symbol.
1041 * @param tree The tree referring to the symbol.
1042 * @param enclOp The closest enclosing operation node of tree,
1043 * null if tree is not a subtree of an operation.
1044 * @param refSuper Is access via a (qualified) C.super?
1045 */
1046 JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean refSuper) {
1047 // Access a free variable via its proxy, or its proxy's proxy
1048 while (sym.kind == VAR && sym.owner.kind == MTH &&
1049 sym.owner.enclClass() != currentClass) {
1050 // A constant is replaced by its constant value.
1051 Object cv = ((VarSymbol)sym).getConstValue();
1052 if (cv != null) {
1053 make.at(tree.pos);
1054 return makeLit(sym.type, cv);
1055 }
1056 // Otherwise replace the variable by its proxy.
1057 sym = proxies.lookup(proxyName(sym.name)).sym;
1058 Assert.check(sym != null && (sym.flags_field & FINAL) != 0);
1059 tree = make.at(tree.pos).Ident(sym);
1060 }
1061 JCExpression base = (tree.getTag() == JCTree.SELECT) ? ((JCFieldAccess) tree).selected : null;
1062 switch (sym.kind) {
1063 case TYP:
1064 if (sym.owner.kind != PCK) {
1065 // Convert type idents to
1066 // <flat name> or <package name> . <flat name>
1067 Name flatname = Convert.shortName(sym.flatName());
1068 while (base != null &&
1069 TreeInfo.symbol(base) != null &&
1070 TreeInfo.symbol(base).kind != PCK) {
1071 base = (base.getTag() == JCTree.SELECT)
1072 ? ((JCFieldAccess) base).selected
1073 : null;
1074 }
1075 if (tree.getTag() == JCTree.IDENT) {
1076 ((JCIdent) tree).name = flatname;
1077 } else if (base == null) {
1078 tree = make.at(tree.pos).Ident(sym);
1079 ((JCIdent) tree).name = flatname;
1080 } else {
1081 ((JCFieldAccess) tree).selected = base;
1082 ((JCFieldAccess) tree).name = flatname;
1083 }
1084 }
1085 break;
1086 case MTH: case VAR:
1087 if (sym.owner.kind == TYP) {
1089 // Access methods are required for
1090 // - private members,
1091 // - protected members in a superclass of an
1092 // enclosing class contained in another package.
1093 // - all non-private members accessed via a qualified super.
1094 boolean protAccess = refSuper && !needsPrivateAccess(sym)
1095 || needsProtectedAccess(sym, tree);
1096 boolean accReq = protAccess || needsPrivateAccess(sym);
1098 // A base has to be supplied for
1099 // - simple identifiers accessing variables in outer classes.
1100 boolean baseReq =
1101 base == null &&
1102 sym.owner != syms.predefClass &&
1103 !sym.isMemberOf(currentClass, types);
1105 if (accReq || baseReq) {
1106 make.at(tree.pos);
1108 // Constants are replaced by their constant value.
1109 if (sym.kind == VAR) {
1110 Object cv = ((VarSymbol)sym).getConstValue();
1111 if (cv != null) return makeLit(sym.type, cv);
1112 }
1114 // Private variables and methods are replaced by calls
1115 // to their access methods.
1116 if (accReq) {
1117 List<JCExpression> args = List.nil();
1118 if ((sym.flags() & STATIC) == 0) {
1119 // Instance access methods get instance
1120 // as first parameter.
1121 if (base == null)
1122 base = makeOwnerThis(tree.pos(), sym, true);
1123 args = args.prepend(base);
1124 base = null; // so we don't duplicate code
1125 }
1126 Symbol access = accessSymbol(sym, tree,
1127 enclOp, protAccess,
1128 refSuper);
1129 JCExpression receiver = make.Select(
1130 base != null ? base : make.QualIdent(access.owner),
1131 access);
1132 return make.App(receiver, args);
1134 // Other accesses to members of outer classes get a
1135 // qualifier.
1136 } else if (baseReq) {
1137 return make.at(tree.pos).Select(
1138 accessBase(tree.pos(), sym), sym).setType(tree.type);
1139 }
1140 }
1141 }
1142 }
1143 return tree;
1144 }
1146 /** Ensure that identifier is accessible, return tree accessing the identifier.
1147 * @param tree The identifier tree.
1148 */
1149 JCExpression access(JCExpression tree) {
1150 Symbol sym = TreeInfo.symbol(tree);
1151 return sym == null ? tree : access(sym, tree, null, false);
1152 }
1154 /** Return access constructor for a private constructor,
1155 * or the constructor itself, if no access constructor is needed.
1156 * @param pos The position to report diagnostics, if any.
1157 * @param constr The private constructor.
1158 */
1159 Symbol accessConstructor(DiagnosticPosition pos, Symbol constr) {
1160 if (needsPrivateAccess(constr)) {
1161 ClassSymbol accOwner = constr.owner.enclClass();
1162 MethodSymbol aconstr = accessConstrs.get(constr);
1163 if (aconstr == null) {
1164 List<Type> argtypes = constr.type.getParameterTypes();
1165 if ((accOwner.flags_field & ENUM) != 0)
1166 argtypes = argtypes
1167 .prepend(syms.intType)
1168 .prepend(syms.stringType);
1169 aconstr = new MethodSymbol(
1170 SYNTHETIC,
1171 names.init,
1172 new MethodType(
1173 argtypes.append(
1174 accessConstructorTag().erasure(types)),
1175 constr.type.getReturnType(),
1176 constr.type.getThrownTypes(),
1177 syms.methodClass),
1178 accOwner);
1179 enterSynthetic(pos, aconstr, accOwner.members());
1180 accessConstrs.put(constr, aconstr);
1181 accessed.append(constr);
1182 }
1183 return aconstr;
1184 } else {
1185 return constr;
1186 }
1187 }
1189 /** Return an anonymous class nested in this toplevel class.
1190 */
1191 ClassSymbol accessConstructorTag() {
1192 ClassSymbol topClass = currentClass.outermostClass();
1193 Name flatname = names.fromString("" + topClass.getQualifiedName() +
1194 target.syntheticNameChar() +
1195 "1");
1196 ClassSymbol ctag = chk.compiled.get(flatname);
1197 if (ctag == null)
1198 ctag = makeEmptyClass(STATIC | SYNTHETIC, topClass);
1199 // keep a record of all tags, to verify that all are generated as required
1200 accessConstrTags = accessConstrTags.prepend(ctag);
1201 return ctag;
1202 }
1204 /** Add all required access methods for a private symbol to enclosing class.
1205 * @param sym The symbol.
1206 */
1207 void makeAccessible(Symbol sym) {
1208 JCClassDecl cdef = classDef(sym.owner.enclClass());
1209 if (cdef == null) Assert.error("class def not found: " + sym + " in " + sym.owner);
1210 if (sym.name == names.init) {
1211 cdef.defs = cdef.defs.prepend(
1212 accessConstructorDef(cdef.pos, sym, accessConstrs.get(sym)));
1213 } else {
1214 MethodSymbol[] accessors = accessSyms.get(sym);
1215 for (int i = 0; i < NCODES; i++) {
1216 if (accessors[i] != null)
1217 cdef.defs = cdef.defs.prepend(
1218 accessDef(cdef.pos, sym, accessors[i], i));
1219 }
1220 }
1221 }
1223 /** Construct definition of an access method.
1224 * @param pos The source code position of the definition.
1225 * @param vsym The private or protected symbol.
1226 * @param accessor The access method for the symbol.
1227 * @param acode The access code.
1228 */
1229 JCTree accessDef(int pos, Symbol vsym, MethodSymbol accessor, int acode) {
1230 // System.err.println("access " + vsym + " with " + accessor);//DEBUG
1231 currentClass = vsym.owner.enclClass();
1232 make.at(pos);
1233 JCMethodDecl md = make.MethodDef(accessor, null);
1235 // Find actual symbol
1236 Symbol sym = actualSymbols.get(vsym);
1237 if (sym == null) sym = vsym;
1239 JCExpression ref; // The tree referencing the private symbol.
1240 List<JCExpression> args; // Any additional arguments to be passed along.
1241 if ((sym.flags() & STATIC) != 0) {
1242 ref = make.Ident(sym);
1243 args = make.Idents(md.params);
1244 } else {
1245 ref = make.Select(make.Ident(md.params.head), sym);
1246 args = make.Idents(md.params.tail);
1247 }
1248 JCStatement stat; // The statement accessing the private symbol.
1249 if (sym.kind == VAR) {
1250 // Normalize out all odd access codes by taking floor modulo 2:
1251 int acode1 = acode - (acode & 1);
1253 JCExpression expr; // The access method's return value.
1254 switch (acode1) {
1255 case DEREFcode:
1256 expr = ref;
1257 break;
1258 case ASSIGNcode:
1259 expr = make.Assign(ref, args.head);
1260 break;
1261 case PREINCcode: case POSTINCcode: case PREDECcode: case POSTDECcode:
1262 expr = makeUnary(
1263 ((acode1 - PREINCcode) >> 1) + JCTree.PREINC, ref);
1264 break;
1265 default:
1266 expr = make.Assignop(
1267 treeTag(binaryAccessOperator(acode1)), ref, args.head);
1268 ((JCAssignOp) expr).operator = binaryAccessOperator(acode1);
1269 }
1270 stat = make.Return(expr.setType(sym.type));
1271 } else {
1272 stat = make.Call(make.App(ref, args));
1273 }
1274 md.body = make.Block(0, List.of(stat));
1276 // Make sure all parameters, result types and thrown exceptions
1277 // are accessible.
1278 for (List<JCVariableDecl> l = md.params; l.nonEmpty(); l = l.tail)
1279 l.head.vartype = access(l.head.vartype);
1280 md.restype = access(md.restype);
1281 for (List<JCExpression> l = md.thrown; l.nonEmpty(); l = l.tail)
1282 l.head = access(l.head);
1284 return md;
1285 }
1287 /** Construct definition of an access constructor.
1288 * @param pos The source code position of the definition.
1289 * @param constr The private constructor.
1290 * @param accessor The access method for the constructor.
1291 */
1292 JCTree accessConstructorDef(int pos, Symbol constr, MethodSymbol accessor) {
1293 make.at(pos);
1294 JCMethodDecl md = make.MethodDef(accessor,
1295 accessor.externalType(types),
1296 null);
1297 JCIdent callee = make.Ident(names._this);
1298 callee.sym = constr;
1299 callee.type = constr.type;
1300 md.body =
1301 make.Block(0, List.<JCStatement>of(
1302 make.Call(
1303 make.App(
1304 callee,
1305 make.Idents(md.params.reverse().tail.reverse())))));
1306 return md;
1307 }
1309 /**************************************************************************
1310 * Free variables proxies and this$n
1311 *************************************************************************/
1313 /** A scope containing all free variable proxies for currently translated
1314 * class, as well as its this$n symbol (if needed).
1315 * Proxy scopes are nested in the same way classes are.
1316 * Inside a constructor, proxies and any this$n symbol are duplicated
1317 * in an additional innermost scope, where they represent the constructor
1318 * parameters.
1319 */
1320 Scope proxies;
1322 /** A scope containing all unnamed resource variables/saved
1323 * exception variables for translated TWR blocks
1324 */
1325 Scope twrVars;
1327 /** A stack containing the this$n field of the currently translated
1328 * classes (if needed) in innermost first order.
1329 * Inside a constructor, proxies and any this$n symbol are duplicated
1330 * in an additional innermost scope, where they represent the constructor
1331 * parameters.
1332 */
1333 List<VarSymbol> outerThisStack;
1335 /** The name of a free variable proxy.
1336 */
1337 Name proxyName(Name name) {
1338 return names.fromString("val" + target.syntheticNameChar() + name);
1339 }
1341 /** Proxy definitions for all free variables in given list, in reverse order.
1342 * @param pos The source code position of the definition.
1343 * @param freevars The free variables.
1344 * @param owner The class in which the definitions go.
1345 */
1346 List<JCVariableDecl> freevarDefs(int pos, List<VarSymbol> freevars, Symbol owner) {
1347 long flags = FINAL | SYNTHETIC;
1348 if (owner.kind == TYP &&
1349 target.usePrivateSyntheticFields())
1350 flags |= PRIVATE;
1351 List<JCVariableDecl> defs = List.nil();
1352 for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail) {
1353 VarSymbol v = l.head;
1354 VarSymbol proxy = new VarSymbol(
1355 flags, proxyName(v.name), v.erasure(types), owner);
1356 proxies.enter(proxy);
1357 JCVariableDecl vd = make.at(pos).VarDef(proxy, null);
1358 vd.vartype = access(vd.vartype);
1359 defs = defs.prepend(vd);
1360 }
1361 return defs;
1362 }
1364 /** The name of a this$n field
1365 * @param type The class referenced by the this$n field
1366 */
1367 Name outerThisName(Type type, Symbol owner) {
1368 Type t = type.getEnclosingType();
1369 int nestingLevel = 0;
1370 while (t.tag == CLASS) {
1371 t = t.getEnclosingType();
1372 nestingLevel++;
1373 }
1374 Name result = names.fromString("this" + target.syntheticNameChar() + nestingLevel);
1375 while (owner.kind == TYP && ((ClassSymbol)owner).members().lookup(result).scope != null)
1376 result = names.fromString(result.toString() + target.syntheticNameChar());
1377 return result;
1378 }
1380 /** Definition for this$n field.
1381 * @param pos The source code position of the definition.
1382 * @param owner The class in which the definition goes.
1383 */
1384 JCVariableDecl outerThisDef(int pos, Symbol owner) {
1385 long flags = FINAL | SYNTHETIC;
1386 if (owner.kind == TYP &&
1387 target.usePrivateSyntheticFields())
1388 flags |= PRIVATE;
1389 Type target = types.erasure(owner.enclClass().type.getEnclosingType());
1390 VarSymbol outerThis = new VarSymbol(
1391 flags, outerThisName(target, owner), target, owner);
1392 outerThisStack = outerThisStack.prepend(outerThis);
1393 JCVariableDecl vd = make.at(pos).VarDef(outerThis, null);
1394 vd.vartype = access(vd.vartype);
1395 return vd;
1396 }
1398 /** Return a list of trees that load the free variables in given list,
1399 * in reverse order.
1400 * @param pos The source code position to be used for the trees.
1401 * @param freevars The list of free variables.
1402 */
1403 List<JCExpression> loadFreevars(DiagnosticPosition pos, List<VarSymbol> freevars) {
1404 List<JCExpression> args = List.nil();
1405 for (List<VarSymbol> l = freevars; l.nonEmpty(); l = l.tail)
1406 args = args.prepend(loadFreevar(pos, l.head));
1407 return args;
1408 }
1409 //where
1410 JCExpression loadFreevar(DiagnosticPosition pos, VarSymbol v) {
1411 return access(v, make.at(pos).Ident(v), null, false);
1412 }
1414 /** Construct a tree simulating the expression <C.this>.
1415 * @param pos The source code position to be used for the tree.
1416 * @param c The qualifier class.
1417 */
1418 JCExpression makeThis(DiagnosticPosition pos, TypeSymbol c) {
1419 if (currentClass == c) {
1420 // in this case, `this' works fine
1421 return make.at(pos).This(c.erasure(types));
1422 } else {
1423 // need to go via this$n
1424 return makeOuterThis(pos, c);
1425 }
1426 }
1428 /**
1429 * Optionally replace a try statement with the desugaring of a
1430 * try-with-resources statement. The canonical desugaring of
1431 *
1432 * try ResourceSpecification
1433 * Block
1434 *
1435 * is
1436 *
1437 * {
1438 * final VariableModifiers_minus_final R #resource = Expression;
1439 * Throwable #primaryException = null;
1440 *
1441 * try ResourceSpecificationtail
1442 * Block
1443 * catch (Throwable #t) {
1444 * #primaryException = t;
1445 * throw #t;
1446 * } finally {
1447 * if (#resource != null) {
1448 * if (#primaryException != null) {
1449 * try {
1450 * #resource.close();
1451 * } catch(Throwable #suppressedException) {
1452 * #primaryException.addSuppressed(#suppressedException);
1453 * }
1454 * } else {
1455 * #resource.close();
1456 * }
1457 * }
1458 * }
1459 *
1460 * @param tree The try statement to inspect.
1461 * @return A a desugared try-with-resources tree, or the original
1462 * try block if there are no resources to manage.
1463 */
1464 JCTree makeTwrTry(JCTry tree) {
1465 make_at(tree.pos());
1466 twrVars = twrVars.dup();
1467 JCBlock twrBlock = makeTwrBlock(tree.resources, tree.body, 0);
1468 if (tree.catchers.isEmpty() && tree.finalizer == null)
1469 result = translate(twrBlock);
1470 else
1471 result = translate(make.Try(twrBlock, tree.catchers, tree.finalizer));
1472 twrVars = twrVars.leave();
1473 return result;
1474 }
1476 private JCBlock makeTwrBlock(List<JCTree> resources, JCBlock block, int depth) {
1477 if (resources.isEmpty())
1478 return block;
1480 // Add resource declaration or expression to block statements
1481 ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();
1482 JCTree resource = resources.head;
1483 JCExpression expr = null;
1484 if (resource instanceof JCVariableDecl) {
1485 JCVariableDecl var = (JCVariableDecl) resource;
1486 expr = make.Ident(var.sym).setType(resource.type);
1487 stats.add(var);
1488 } else {
1489 Assert.check(resource instanceof JCExpression);
1490 VarSymbol syntheticTwrVar =
1491 new VarSymbol(SYNTHETIC | FINAL,
1492 makeSyntheticName(names.fromString("twrVar" +
1493 depth), twrVars),
1494 (resource.type.tag == TypeTags.BOT) ?
1495 syms.autoCloseableType : resource.type,
1496 currentMethodSym);
1497 twrVars.enter(syntheticTwrVar);
1498 JCVariableDecl syntheticTwrVarDecl =
1499 make.VarDef(syntheticTwrVar, (JCExpression)resource);
1500 expr = (JCExpression)make.Ident(syntheticTwrVar);
1501 stats.add(syntheticTwrVarDecl);
1502 }
1504 // Add primaryException declaration
1505 VarSymbol primaryException =
1506 new VarSymbol(SYNTHETIC,
1507 makeSyntheticName(names.fromString("primaryException" +
1508 depth), twrVars),
1509 syms.throwableType,
1510 currentMethodSym);
1511 twrVars.enter(primaryException);
1512 JCVariableDecl primaryExceptionTreeDecl = make.VarDef(primaryException, makeNull());
1513 stats.add(primaryExceptionTreeDecl);
1515 // Create catch clause that saves exception and then rethrows it
1516 VarSymbol param =
1517 new VarSymbol(FINAL|SYNTHETIC,
1518 names.fromString("t" +
1519 target.syntheticNameChar()),
1520 syms.throwableType,
1521 currentMethodSym);
1522 JCVariableDecl paramTree = make.VarDef(param, null);
1523 JCStatement assign = make.Assignment(primaryException, make.Ident(param));
1524 JCStatement rethrowStat = make.Throw(make.Ident(param));
1525 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
1526 JCCatch catchClause = make.Catch(paramTree, catchBlock);
1528 int oldPos = make.pos;
1529 make.at(TreeInfo.endPos(block));
1530 JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr);
1531 make.at(oldPos);
1532 JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, depth + 1),
1533 List.<JCCatch>of(catchClause),
1534 finallyClause);
1535 stats.add(outerTry);
1536 return make.Block(0L, stats.toList());
1537 }
1539 private JCBlock makeTwrFinallyClause(Symbol primaryException, JCExpression resource) {
1540 // primaryException.addSuppressed(catchException);
1541 VarSymbol catchException =
1542 new VarSymbol(0, make.paramName(2),
1543 syms.throwableType,
1544 currentMethodSym);
1545 JCStatement addSuppressionStatement =
1546 make.Exec(makeCall(make.Ident(primaryException),
1547 names.addSuppressed,
1548 List.<JCExpression>of(make.Ident(catchException))));
1550 // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); }
1551 JCBlock tryBlock =
1552 make.Block(0L, List.<JCStatement>of(makeResourceCloseInvocation(resource)));
1553 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null);
1554 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement));
1555 List<JCCatch> catchClauses = List.<JCCatch>of(make.Catch(catchExceptionDecl, catchBlock));
1556 JCTry tryTree = make.Try(tryBlock, catchClauses, null);
1558 // if (primaryException != null) {try...} else resourceClose;
1559 JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)),
1560 tryTree,
1561 makeResourceCloseInvocation(resource));
1563 // if (#resource != null) { if (primaryException ... }
1564 return make.Block(0L,
1565 List.<JCStatement>of(make.If(makeNonNullCheck(resource),
1566 closeIfStatement,
1567 null)));
1568 }
1570 private JCStatement makeResourceCloseInvocation(JCExpression resource) {
1571 // create resource.close() method invocation
1572 JCExpression resourceClose = makeCall(resource,
1573 names.close,
1574 List.<JCExpression>nil());
1575 return make.Exec(resourceClose);
1576 }
1578 private JCExpression makeNonNullCheck(JCExpression expression) {
1579 return makeBinary(JCTree.NE, expression, makeNull());
1580 }
1582 /** Construct a tree that represents the outer instance
1583 * <C.this>. Never pick the current `this'.
1584 * @param pos The source code position to be used for the tree.
1585 * @param c The qualifier class.
1586 */
1587 JCExpression makeOuterThis(DiagnosticPosition pos, TypeSymbol c) {
1588 List<VarSymbol> ots = outerThisStack;
1589 if (ots.isEmpty()) {
1590 log.error(pos, "no.encl.instance.of.type.in.scope", c);
1591 Assert.error();
1592 return makeNull();
1593 }
1594 VarSymbol ot = ots.head;
1595 JCExpression tree = access(make.at(pos).Ident(ot));
1596 TypeSymbol otc = ot.type.tsym;
1597 while (otc != c) {
1598 do {
1599 ots = ots.tail;
1600 if (ots.isEmpty()) {
1601 log.error(pos,
1602 "no.encl.instance.of.type.in.scope",
1603 c);
1604 Assert.error(); // should have been caught in Attr
1605 return tree;
1606 }
1607 ot = ots.head;
1608 } while (ot.owner != otc);
1609 if (otc.owner.kind != PCK && !otc.hasOuterInstance()) {
1610 chk.earlyRefError(pos, c);
1611 Assert.error(); // should have been caught in Attr
1612 return makeNull();
1613 }
1614 tree = access(make.at(pos).Select(tree, ot));
1615 otc = ot.type.tsym;
1616 }
1617 return tree;
1618 }
1620 /** Construct a tree that represents the closest outer instance
1621 * <C.this> such that the given symbol is a member of C.
1622 * @param pos The source code position to be used for the tree.
1623 * @param sym The accessed symbol.
1624 * @param preciseMatch should we accept a type that is a subtype of
1625 * sym's owner, even if it doesn't contain sym
1626 * due to hiding, overriding, or non-inheritance
1627 * due to protection?
1628 */
1629 JCExpression makeOwnerThis(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
1630 Symbol c = sym.owner;
1631 if (preciseMatch ? sym.isMemberOf(currentClass, types)
1632 : currentClass.isSubClass(sym.owner, types)) {
1633 // in this case, `this' works fine
1634 return make.at(pos).This(c.erasure(types));
1635 } else {
1636 // need to go via this$n
1637 return makeOwnerThisN(pos, sym, preciseMatch);
1638 }
1639 }
1641 /**
1642 * Similar to makeOwnerThis but will never pick "this".
1643 */
1644 JCExpression makeOwnerThisN(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
1645 Symbol c = sym.owner;
1646 List<VarSymbol> ots = outerThisStack;
1647 if (ots.isEmpty()) {
1648 log.error(pos, "no.encl.instance.of.type.in.scope", c);
1649 Assert.error();
1650 return makeNull();
1651 }
1652 VarSymbol ot = ots.head;
1653 JCExpression tree = access(make.at(pos).Ident(ot));
1654 TypeSymbol otc = ot.type.tsym;
1655 while (!(preciseMatch ? sym.isMemberOf(otc, types) : otc.isSubClass(sym.owner, types))) {
1656 do {
1657 ots = ots.tail;
1658 if (ots.isEmpty()) {
1659 log.error(pos,
1660 "no.encl.instance.of.type.in.scope",
1661 c);
1662 Assert.error();
1663 return tree;
1664 }
1665 ot = ots.head;
1666 } while (ot.owner != otc);
1667 tree = access(make.at(pos).Select(tree, ot));
1668 otc = ot.type.tsym;
1669 }
1670 return tree;
1671 }
1673 /** Return tree simulating the assignment <this.name = name>, where
1674 * name is the name of a free variable.
1675 */
1676 JCStatement initField(int pos, Name name) {
1677 Scope.Entry e = proxies.lookup(name);
1678 Symbol rhs = e.sym;
1679 Assert.check(rhs.owner.kind == MTH);
1680 Symbol lhs = e.next().sym;
1681 Assert.check(rhs.owner.owner == lhs.owner);
1682 make.at(pos);
1683 return
1684 make.Exec(
1685 make.Assign(
1686 make.Select(make.This(lhs.owner.erasure(types)), lhs),
1687 make.Ident(rhs)).setType(lhs.erasure(types)));
1688 }
1690 /** Return tree simulating the assignment <this.this$n = this$n>.
1691 */
1692 JCStatement initOuterThis(int pos) {
1693 VarSymbol rhs = outerThisStack.head;
1694 Assert.check(rhs.owner.kind == MTH);
1695 VarSymbol lhs = outerThisStack.tail.head;
1696 Assert.check(rhs.owner.owner == lhs.owner);
1697 make.at(pos);
1698 return
1699 make.Exec(
1700 make.Assign(
1701 make.Select(make.This(lhs.owner.erasure(types)), lhs),
1702 make.Ident(rhs)).setType(lhs.erasure(types)));
1703 }
1705 /**************************************************************************
1706 * Code for .class
1707 *************************************************************************/
1709 /** Return the symbol of a class to contain a cache of
1710 * compiler-generated statics such as class$ and the
1711 * $assertionsDisabled flag. We create an anonymous nested class
1712 * (unless one already exists) and return its symbol. However,
1713 * for backward compatibility in 1.4 and earlier we use the
1714 * top-level class itself.
1715 */
1716 private ClassSymbol outerCacheClass() {
1717 ClassSymbol clazz = outermostClassDef.sym;
1718 if ((clazz.flags() & INTERFACE) == 0 &&
1719 !target.useInnerCacheClass()) return clazz;
1720 Scope s = clazz.members();
1721 for (Scope.Entry e = s.elems; e != null; e = e.sibling)
1722 if (e.sym.kind == TYP &&
1723 e.sym.name == names.empty &&
1724 (e.sym.flags() & INTERFACE) == 0) return (ClassSymbol) e.sym;
1725 return makeEmptyClass(STATIC | SYNTHETIC, clazz);
1726 }
1728 /** Return symbol for "class$" method. If there is no method definition
1729 * for class$, construct one as follows:
1730 *
1731 * class class$(String x0) {
1732 * try {
1733 * return Class.forName(x0);
1734 * } catch (ClassNotFoundException x1) {
1735 * throw new NoClassDefFoundError(x1.getMessage());
1736 * }
1737 * }
1738 */
1739 private MethodSymbol classDollarSym(DiagnosticPosition pos) {
1740 ClassSymbol outerCacheClass = outerCacheClass();
1741 MethodSymbol classDollarSym =
1742 (MethodSymbol)lookupSynthetic(classDollar,
1743 outerCacheClass.members());
1744 if (classDollarSym == null) {
1745 classDollarSym = new MethodSymbol(
1746 STATIC | SYNTHETIC,
1747 classDollar,
1748 new MethodType(
1749 List.of(syms.stringType),
1750 types.erasure(syms.classType),
1751 List.<Type>nil(),
1752 syms.methodClass),
1753 outerCacheClass);
1754 enterSynthetic(pos, classDollarSym, outerCacheClass.members());
1756 JCMethodDecl md = make.MethodDef(classDollarSym, null);
1757 try {
1758 md.body = classDollarSymBody(pos, md);
1759 } catch (CompletionFailure ex) {
1760 md.body = make.Block(0, List.<JCStatement>nil());
1761 chk.completionError(pos, ex);
1762 }
1763 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
1764 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(md);
1765 }
1766 return classDollarSym;
1767 }
1769 /** Generate code for class$(String name). */
1770 JCBlock classDollarSymBody(DiagnosticPosition pos, JCMethodDecl md) {
1771 MethodSymbol classDollarSym = md.sym;
1772 ClassSymbol outerCacheClass = (ClassSymbol)classDollarSym.owner;
1774 JCBlock returnResult;
1776 // in 1.4.2 and above, we use
1777 // Class.forName(String name, boolean init, ClassLoader loader);
1778 // which requires we cache the current loader in cl$
1779 if (target.classLiteralsNoInit()) {
1780 // clsym = "private static ClassLoader cl$"
1781 VarSymbol clsym = new VarSymbol(STATIC|SYNTHETIC,
1782 names.fromString("cl" + target.syntheticNameChar()),
1783 syms.classLoaderType,
1784 outerCacheClass);
1785 enterSynthetic(pos, clsym, outerCacheClass.members());
1787 // emit "private static ClassLoader cl$;"
1788 JCVariableDecl cldef = make.VarDef(clsym, null);
1789 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
1790 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef);
1792 // newcache := "new cache$1[0]"
1793 JCNewArray newcache = make.
1794 NewArray(make.Type(outerCacheClass.type),
1795 List.<JCExpression>of(make.Literal(INT, 0).setType(syms.intType)),
1796 null);
1797 newcache.type = new ArrayType(types.erasure(outerCacheClass.type),
1798 syms.arrayClass);
1800 // forNameSym := java.lang.Class.forName(
1801 // String s,boolean init,ClassLoader loader)
1802 Symbol forNameSym = lookupMethod(make_pos, names.forName,
1803 types.erasure(syms.classType),
1804 List.of(syms.stringType,
1805 syms.booleanType,
1806 syms.classLoaderType));
1807 // clvalue := "(cl$ == null) ?
1808 // $newcache.getClass().getComponentType().getClassLoader() : cl$"
1809 JCExpression clvalue =
1810 make.Conditional(
1811 makeBinary(JCTree.EQ, make.Ident(clsym), makeNull()),
1812 make.Assign(
1813 make.Ident(clsym),
1814 makeCall(
1815 makeCall(makeCall(newcache,
1816 names.getClass,
1817 List.<JCExpression>nil()),
1818 names.getComponentType,
1819 List.<JCExpression>nil()),
1820 names.getClassLoader,
1821 List.<JCExpression>nil())).setType(syms.classLoaderType),
1822 make.Ident(clsym)).setType(syms.classLoaderType);
1824 // returnResult := "{ return Class.forName(param1, false, cl$); }"
1825 List<JCExpression> args = List.of(make.Ident(md.params.head.sym),
1826 makeLit(syms.booleanType, 0),
1827 clvalue);
1828 returnResult = make.
1829 Block(0, List.<JCStatement>of(make.
1830 Call(make. // return
1831 App(make.
1832 Ident(forNameSym), args))));
1833 } else {
1834 // forNameSym := java.lang.Class.forName(String s)
1835 Symbol forNameSym = lookupMethod(make_pos,
1836 names.forName,
1837 types.erasure(syms.classType),
1838 List.of(syms.stringType));
1839 // returnResult := "{ return Class.forName(param1); }"
1840 returnResult = make.
1841 Block(0, List.of(make.
1842 Call(make. // return
1843 App(make.
1844 QualIdent(forNameSym),
1845 List.<JCExpression>of(make.
1846 Ident(md.params.
1847 head.sym))))));
1848 }
1850 // catchParam := ClassNotFoundException e1
1851 VarSymbol catchParam =
1852 new VarSymbol(0, make.paramName(1),
1853 syms.classNotFoundExceptionType,
1854 classDollarSym);
1856 JCStatement rethrow;
1857 if (target.hasInitCause()) {
1858 // rethrow = "throw new NoClassDefFoundError().initCause(e);
1859 JCTree throwExpr =
1860 makeCall(makeNewClass(syms.noClassDefFoundErrorType,
1861 List.<JCExpression>nil()),
1862 names.initCause,
1863 List.<JCExpression>of(make.Ident(catchParam)));
1864 rethrow = make.Throw(throwExpr);
1865 } else {
1866 // getMessageSym := ClassNotFoundException.getMessage()
1867 Symbol getMessageSym = lookupMethod(make_pos,
1868 names.getMessage,
1869 syms.classNotFoundExceptionType,
1870 List.<Type>nil());
1871 // rethrow = "throw new NoClassDefFoundError(e.getMessage());"
1872 rethrow = make.
1873 Throw(makeNewClass(syms.noClassDefFoundErrorType,
1874 List.<JCExpression>of(make.App(make.Select(make.Ident(catchParam),
1875 getMessageSym),
1876 List.<JCExpression>nil()))));
1877 }
1879 // rethrowStmt := "( $rethrow )"
1880 JCBlock rethrowStmt = make.Block(0, List.of(rethrow));
1882 // catchBlock := "catch ($catchParam) $rethrowStmt"
1883 JCCatch catchBlock = make.Catch(make.VarDef(catchParam, null),
1884 rethrowStmt);
1886 // tryCatch := "try $returnResult $catchBlock"
1887 JCStatement tryCatch = make.Try(returnResult,
1888 List.of(catchBlock), null);
1890 return make.Block(0, List.of(tryCatch));
1891 }
1892 // where
1893 /** Create an attributed tree of the form left.name(). */
1894 private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
1895 Assert.checkNonNull(left.type);
1896 Symbol funcsym = lookupMethod(make_pos, name, left.type,
1897 TreeInfo.types(args));
1898 return make.App(make.Select(left, funcsym), args);
1899 }
1901 /** The Name Of The variable to cache T.class values.
1902 * @param sig The signature of type T.
1903 */
1904 private Name cacheName(String sig) {
1905 StringBuffer buf = new StringBuffer();
1906 if (sig.startsWith("[")) {
1907 buf = buf.append("array");
1908 while (sig.startsWith("[")) {
1909 buf = buf.append(target.syntheticNameChar());
1910 sig = sig.substring(1);
1911 }
1912 if (sig.startsWith("L")) {
1913 sig = sig.substring(0, sig.length() - 1);
1914 }
1915 } else {
1916 buf = buf.append("class" + target.syntheticNameChar());
1917 }
1918 buf = buf.append(sig.replace('.', target.syntheticNameChar()));
1919 return names.fromString(buf.toString());
1920 }
1922 /** The variable symbol that caches T.class values.
1923 * If none exists yet, create a definition.
1924 * @param sig The signature of type T.
1925 * @param pos The position to report diagnostics, if any.
1926 */
1927 private VarSymbol cacheSym(DiagnosticPosition pos, String sig) {
1928 ClassSymbol outerCacheClass = outerCacheClass();
1929 Name cname = cacheName(sig);
1930 VarSymbol cacheSym =
1931 (VarSymbol)lookupSynthetic(cname, outerCacheClass.members());
1932 if (cacheSym == null) {
1933 cacheSym = new VarSymbol(
1934 STATIC | SYNTHETIC, cname, types.erasure(syms.classType), outerCacheClass);
1935 enterSynthetic(pos, cacheSym, outerCacheClass.members());
1937 JCVariableDecl cacheDef = make.VarDef(cacheSym, null);
1938 JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
1939 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cacheDef);
1940 }
1941 return cacheSym;
1942 }
1944 /** The tree simulating a T.class expression.
1945 * @param clazz The tree identifying type T.
1946 */
1947 private JCExpression classOf(JCTree clazz) {
1948 return classOfType(clazz.type, clazz.pos());
1949 }
1951 private JCExpression classOfType(Type type, DiagnosticPosition pos) {
1952 switch (type.tag) {
1953 case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
1954 case DOUBLE: case BOOLEAN: case VOID:
1955 // replace with <BoxedClass>.TYPE
1956 ClassSymbol c = types.boxedClass(type);
1957 Symbol typeSym =
1958 rs.access(
1959 rs.findIdentInType(attrEnv, c.type, names.TYPE, VAR),
1960 pos, c.type, names.TYPE, true);
1961 if (typeSym.kind == VAR)
1962 ((VarSymbol)typeSym).getConstValue(); // ensure initializer is evaluated
1963 return make.QualIdent(typeSym);
1964 case CLASS: case ARRAY:
1965 if (target.hasClassLiterals()) {
1966 VarSymbol sym = new VarSymbol(
1967 STATIC | PUBLIC | FINAL, names._class,
1968 syms.classType, type.tsym);
1969 return make_at(pos).Select(make.Type(type), sym);
1970 }
1971 // replace with <cache == null ? cache = class$(tsig) : cache>
1972 // where
1973 // - <tsig> is the type signature of T,
1974 // - <cache> is the cache variable for tsig.
1975 String sig =
1976 writer.xClassName(type).toString().replace('/', '.');
1977 Symbol cs = cacheSym(pos, sig);
1978 return make_at(pos).Conditional(
1979 makeBinary(JCTree.EQ, make.Ident(cs), makeNull()),
1980 make.Assign(
1981 make.Ident(cs),
1982 make.App(
1983 make.Ident(classDollarSym(pos)),
1984 List.<JCExpression>of(make.Literal(CLASS, sig)
1985 .setType(syms.stringType))))
1986 .setType(types.erasure(syms.classType)),
1987 make.Ident(cs)).setType(types.erasure(syms.classType));
1988 default:
1989 throw new AssertionError();
1990 }
1991 }
1993 /**************************************************************************
1994 * Code for enabling/disabling assertions.
1995 *************************************************************************/
1997 // This code is not particularly robust if the user has
1998 // previously declared a member named '$assertionsDisabled'.
1999 // The same faulty idiom also appears in the translation of
2000 // class literals above. We should report an error if a
2001 // previous declaration is not synthetic.
2003 private JCExpression assertFlagTest(DiagnosticPosition pos) {
2004 // Outermost class may be either true class or an interface.
2005 ClassSymbol outermostClass = outermostClassDef.sym;
2007 // note that this is a class, as an interface can't contain a statement.
2008 ClassSymbol container = currentClass;
2010 VarSymbol assertDisabledSym =
2011 (VarSymbol)lookupSynthetic(dollarAssertionsDisabled,
2012 container.members());
2013 if (assertDisabledSym == null) {
2014 assertDisabledSym =
2015 new VarSymbol(STATIC | FINAL | SYNTHETIC,
2016 dollarAssertionsDisabled,
2017 syms.booleanType,
2018 container);
2019 enterSynthetic(pos, assertDisabledSym, container.members());
2020 Symbol desiredAssertionStatusSym = lookupMethod(pos,
2021 names.desiredAssertionStatus,
2022 types.erasure(syms.classType),
2023 List.<Type>nil());
2024 JCClassDecl containerDef = classDef(container);
2025 make_at(containerDef.pos());
2026 JCExpression notStatus = makeUnary(JCTree.NOT, make.App(make.Select(
2027 classOfType(types.erasure(outermostClass.type),
2028 containerDef.pos()),
2029 desiredAssertionStatusSym)));
2030 JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym,
2031 notStatus);
2032 containerDef.defs = containerDef.defs.prepend(assertDisabledDef);
2033 }
2034 make_at(pos);
2035 return makeUnary(JCTree.NOT, make.Ident(assertDisabledSym));
2036 }
2039 /**************************************************************************
2040 * Building blocks for let expressions
2041 *************************************************************************/
2043 interface TreeBuilder {
2044 JCTree build(JCTree arg);
2045 }
2047 /** Construct an expression using the builder, with the given rval
2048 * expression as an argument to the builder. However, the rval
2049 * expression must be computed only once, even if used multiple
2050 * times in the result of the builder. We do that by
2051 * constructing a "let" expression that saves the rvalue into a
2052 * temporary variable and then uses the temporary variable in
2053 * place of the expression built by the builder. The complete
2054 * resulting expression is of the form
2055 * <pre>
2056 * (let <b>TYPE</b> <b>TEMP</b> = <b>RVAL</b>;
2057 * in (<b>BUILDER</b>(<b>TEMP</b>)))
2058 * </pre>
2059 * where <code><b>TEMP</b></code> is a newly declared variable
2060 * in the let expression.
2061 */
2062 JCTree abstractRval(JCTree rval, Type type, TreeBuilder builder) {
2063 rval = TreeInfo.skipParens(rval);
2064 switch (rval.getTag()) {
2065 case JCTree.LITERAL:
2066 return builder.build(rval);
2067 case JCTree.IDENT:
2068 JCIdent id = (JCIdent) rval;
2069 if ((id.sym.flags() & FINAL) != 0 && id.sym.owner.kind == MTH)
2070 return builder.build(rval);
2071 }
2072 VarSymbol var =
2073 new VarSymbol(FINAL|SYNTHETIC,
2074 names.fromString(
2075 target.syntheticNameChar()
2076 + "" + rval.hashCode()),
2077 type,
2078 currentMethodSym);
2079 rval = convert(rval,type);
2080 JCVariableDecl def = make.VarDef(var, (JCExpression)rval); // XXX cast
2081 JCTree built = builder.build(make.Ident(var));
2082 JCTree res = make.LetExpr(def, built);
2083 res.type = built.type;
2084 return res;
2085 }
2087 // same as above, with the type of the temporary variable computed
2088 JCTree abstractRval(JCTree rval, TreeBuilder builder) {
2089 return abstractRval(rval, rval.type, builder);
2090 }
2092 // same as above, but for an expression that may be used as either
2093 // an rvalue or an lvalue. This requires special handling for
2094 // Select expressions, where we place the left-hand-side of the
2095 // select in a temporary, and for Indexed expressions, where we
2096 // place both the indexed expression and the index value in temps.
2097 JCTree abstractLval(JCTree lval, final TreeBuilder builder) {
2098 lval = TreeInfo.skipParens(lval);
2099 switch (lval.getTag()) {
2100 case JCTree.IDENT:
2101 return builder.build(lval);
2102 case JCTree.SELECT: {
2103 final JCFieldAccess s = (JCFieldAccess)lval;
2104 JCTree selected = TreeInfo.skipParens(s.selected);
2105 Symbol lid = TreeInfo.symbol(s.selected);
2106 if (lid != null && lid.kind == TYP) return builder.build(lval);
2107 return abstractRval(s.selected, new TreeBuilder() {
2108 public JCTree build(final JCTree selected) {
2109 return builder.build(make.Select((JCExpression)selected, s.sym));
2110 }
2111 });
2112 }
2113 case JCTree.INDEXED: {
2114 final JCArrayAccess i = (JCArrayAccess)lval;
2115 return abstractRval(i.indexed, new TreeBuilder() {
2116 public JCTree build(final JCTree indexed) {
2117 return abstractRval(i.index, syms.intType, new TreeBuilder() {
2118 public JCTree build(final JCTree index) {
2119 JCTree newLval = make.Indexed((JCExpression)indexed,
2120 (JCExpression)index);
2121 newLval.setType(i.type);
2122 return builder.build(newLval);
2123 }
2124 });
2125 }
2126 });
2127 }
2128 case JCTree.TYPECAST: {
2129 return abstractLval(((JCTypeCast)lval).expr, builder);
2130 }
2131 }
2132 throw new AssertionError(lval);
2133 }
2135 // evaluate and discard the first expression, then evaluate the second.
2136 JCTree makeComma(final JCTree expr1, final JCTree expr2) {
2137 return abstractRval(expr1, new TreeBuilder() {
2138 public JCTree build(final JCTree discarded) {
2139 return expr2;
2140 }
2141 });
2142 }
2144 /**************************************************************************
2145 * Translation methods
2146 *************************************************************************/
2148 /** Visitor argument: enclosing operator node.
2149 */
2150 private JCExpression enclOp;
2152 /** Visitor method: Translate a single node.
2153 * Attach the source position from the old tree to its replacement tree.
2154 */
2155 public <T extends JCTree> T translate(T tree) {
2156 if (tree == null) {
2157 return null;
2158 } else {
2159 make_at(tree.pos());
2160 T result = super.translate(tree);
2161 if (endPositions != null && result != tree) {
2162 Integer endPos = endPositions.remove(tree);
2163 if (endPos != null) endPositions.put(result, endPos);
2164 }
2165 return result;
2166 }
2167 }
2169 /** Visitor method: Translate a single node, boxing or unboxing if needed.
2170 */
2171 public <T extends JCTree> T translate(T tree, Type type) {
2172 return (tree == null) ? null : boxIfNeeded(translate(tree), type);
2173 }
2175 /** Visitor method: Translate tree.
2176 */
2177 public <T extends JCTree> T translate(T tree, JCExpression enclOp) {
2178 JCExpression prevEnclOp = this.enclOp;
2179 this.enclOp = enclOp;
2180 T res = translate(tree);
2181 this.enclOp = prevEnclOp;
2182 return res;
2183 }
2185 /** Visitor method: Translate list of trees.
2186 */
2187 public <T extends JCTree> List<T> translate(List<T> trees, JCExpression enclOp) {
2188 JCExpression prevEnclOp = this.enclOp;
2189 this.enclOp = enclOp;
2190 List<T> res = translate(trees);
2191 this.enclOp = prevEnclOp;
2192 return res;
2193 }
2195 /** Visitor method: Translate list of trees.
2196 */
2197 public <T extends JCTree> List<T> translate(List<T> trees, Type type) {
2198 if (trees == null) return null;
2199 for (List<T> l = trees; l.nonEmpty(); l = l.tail)
2200 l.head = translate(l.head, type);
2201 return trees;
2202 }
2204 public void visitTopLevel(JCCompilationUnit tree) {
2205 if (needPackageInfoClass(tree)) {
2206 Name name = names.package_info;
2207 long flags = Flags.ABSTRACT | Flags.INTERFACE;
2208 if (target.isPackageInfoSynthetic())
2209 // package-info is marked SYNTHETIC in JDK 1.6 and later releases
2210 flags = flags | Flags.SYNTHETIC;
2211 JCClassDecl packageAnnotationsClass
2212 = make.ClassDef(make.Modifiers(flags,
2213 tree.packageAnnotations),
2214 name, List.<JCTypeParameter>nil(),
2215 null, List.<JCExpression>nil(), List.<JCTree>nil());
2216 ClassSymbol c = tree.packge.package_info;
2217 c.flags_field |= flags;
2218 c.attributes_field = tree.packge.attributes_field;
2219 ClassType ctype = (ClassType) c.type;
2220 ctype.supertype_field = syms.objectType;
2221 ctype.interfaces_field = List.nil();
2222 packageAnnotationsClass.sym = c;
2224 translated.append(packageAnnotationsClass);
2225 }
2226 }
2227 // where
2228 private boolean needPackageInfoClass(JCCompilationUnit tree) {
2229 switch (pkginfoOpt) {
2230 case ALWAYS:
2231 return true;
2232 case LEGACY:
2233 return tree.packageAnnotations.nonEmpty();
2234 case NONEMPTY:
2235 for (Attribute.Compound a: tree.packge.attributes_field) {
2236 Attribute.RetentionPolicy p = types.getRetention(a);
2237 if (p != Attribute.RetentionPolicy.SOURCE)
2238 return true;
2239 }
2240 return false;
2241 }
2242 throw new AssertionError();
2243 }
2245 public void visitClassDef(JCClassDecl tree) {
2246 ClassSymbol currentClassPrev = currentClass;
2247 MethodSymbol currentMethodSymPrev = currentMethodSym;
2248 currentClass = tree.sym;
2249 currentMethodSym = null;
2250 classdefs.put(currentClass, tree);
2252 proxies = proxies.dup(currentClass);
2253 List<VarSymbol> prevOuterThisStack = outerThisStack;
2255 // If this is an enum definition
2256 if ((tree.mods.flags & ENUM) != 0 &&
2257 (types.supertype(currentClass.type).tsym.flags() & ENUM) == 0)
2258 visitEnumDef(tree);
2260 // If this is a nested class, define a this$n field for
2261 // it and add to proxies.
2262 JCVariableDecl otdef = null;
2263 if (currentClass.hasOuterInstance())
2264 otdef = outerThisDef(tree.pos, currentClass);
2266 // If this is a local class, define proxies for all its free variables.
2267 List<JCVariableDecl> fvdefs = freevarDefs(
2268 tree.pos, freevars(currentClass), currentClass);
2270 // Recursively translate superclass, interfaces.
2271 tree.extending = translate(tree.extending);
2272 tree.implementing = translate(tree.implementing);
2274 if (currentClass.isLocal()) {
2275 ClassSymbol encl = currentClass.owner.enclClass();
2276 if (encl.trans_local == null) {
2277 encl.trans_local = List.nil();
2278 }
2279 encl.trans_local = encl.trans_local.prepend(currentClass);
2280 }
2282 // Recursively translate members, taking into account that new members
2283 // might be created during the translation and prepended to the member
2284 // list `tree.defs'.
2285 List<JCTree> seen = List.nil();
2286 while (tree.defs != seen) {
2287 List<JCTree> unseen = tree.defs;
2288 for (List<JCTree> l = unseen; l.nonEmpty() && l != seen; l = l.tail) {
2289 JCTree outermostMemberDefPrev = outermostMemberDef;
2290 if (outermostMemberDefPrev == null) outermostMemberDef = l.head;
2291 l.head = translate(l.head);
2292 outermostMemberDef = outermostMemberDefPrev;
2293 }
2294 seen = unseen;
2295 }
2297 // Convert a protected modifier to public, mask static modifier.
2298 if ((tree.mods.flags & PROTECTED) != 0) tree.mods.flags |= PUBLIC;
2299 tree.mods.flags &= ClassFlags;
2301 // Convert name to flat representation, replacing '.' by '$'.
2302 tree.name = Convert.shortName(currentClass.flatName());
2304 // Add this$n and free variables proxy definitions to class.
2305 for (List<JCVariableDecl> l = fvdefs; l.nonEmpty(); l = l.tail) {
2306 tree.defs = tree.defs.prepend(l.head);
2307 enterSynthetic(tree.pos(), l.head.sym, currentClass.members());
2308 }
2309 if (currentClass.hasOuterInstance()) {
2310 tree.defs = tree.defs.prepend(otdef);
2311 enterSynthetic(tree.pos(), otdef.sym, currentClass.members());
2312 }
2314 proxies = proxies.leave();
2315 outerThisStack = prevOuterThisStack;
2317 // Append translated tree to `translated' queue.
2318 translated.append(tree);
2320 currentClass = currentClassPrev;
2321 currentMethodSym = currentMethodSymPrev;
2323 // Return empty block {} as a placeholder for an inner class.
2324 result = make_at(tree.pos()).Block(0, List.<JCStatement>nil());
2325 }
2327 /** Translate an enum class. */
2328 private void visitEnumDef(JCClassDecl tree) {
2329 make_at(tree.pos());
2331 // add the supertype, if needed
2332 if (tree.extending == null)
2333 tree.extending = make.Type(types.supertype(tree.type));
2335 // classOfType adds a cache field to tree.defs unless
2336 // target.hasClassLiterals().
2337 JCExpression e_class = classOfType(tree.sym.type, tree.pos()).
2338 setType(types.erasure(syms.classType));
2340 // process each enumeration constant, adding implicit constructor parameters
2341 int nextOrdinal = 0;
2342 ListBuffer<JCExpression> values = new ListBuffer<JCExpression>();
2343 ListBuffer<JCTree> enumDefs = new ListBuffer<JCTree>();
2344 ListBuffer<JCTree> otherDefs = new ListBuffer<JCTree>();
2345 for (List<JCTree> defs = tree.defs;
2346 defs.nonEmpty();
2347 defs=defs.tail) {
2348 if (defs.head.getTag() == JCTree.VARDEF && (((JCVariableDecl) defs.head).mods.flags & ENUM) != 0) {
2349 JCVariableDecl var = (JCVariableDecl)defs.head;
2350 visitEnumConstantDef(var, nextOrdinal++);
2351 values.append(make.QualIdent(var.sym));
2352 enumDefs.append(var);
2353 } else {
2354 otherDefs.append(defs.head);
2355 }
2356 }
2358 // private static final T[] #VALUES = { a, b, c };
2359 Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES");
2360 while (tree.sym.members().lookup(valuesName).scope != null) // avoid name clash
2361 valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
2362 Type arrayType = new ArrayType(types.erasure(tree.type), syms.arrayClass);
2363 VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC,
2364 valuesName,
2365 arrayType,
2366 tree.type.tsym);
2367 JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)),
2368 List.<JCExpression>nil(),
2369 values.toList());
2370 newArray.type = arrayType;
2371 enumDefs.append(make.VarDef(valuesVar, newArray));
2372 tree.sym.members().enter(valuesVar);
2374 Symbol valuesSym = lookupMethod(tree.pos(), names.values,
2375 tree.type, List.<Type>nil());
2376 List<JCStatement> valuesBody;
2377 if (useClone()) {
2378 // return (T[]) $VALUES.clone();
2379 JCTypeCast valuesResult =
2380 make.TypeCast(valuesSym.type.getReturnType(),
2381 make.App(make.Select(make.Ident(valuesVar),
2382 syms.arrayCloneMethod)));
2383 valuesBody = List.<JCStatement>of(make.Return(valuesResult));
2384 } else {
2385 // template: T[] $result = new T[$values.length];
2386 Name resultName = names.fromString(target.syntheticNameChar() + "result");
2387 while (tree.sym.members().lookup(resultName).scope != null) // avoid name clash
2388 resultName = names.fromString(resultName + "" + target.syntheticNameChar());
2389 VarSymbol resultVar = new VarSymbol(FINAL|SYNTHETIC,
2390 resultName,
2391 arrayType,
2392 valuesSym);
2393 JCNewArray resultArray = make.NewArray(make.Type(types.erasure(tree.type)),
2394 List.of(make.Select(make.Ident(valuesVar), syms.lengthVar)),
2395 null);
2396 resultArray.type = arrayType;
2397 JCVariableDecl decl = make.VarDef(resultVar, resultArray);
2399 // template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length);
2400 if (systemArraycopyMethod == null) {
2401 systemArraycopyMethod =
2402 new MethodSymbol(PUBLIC | STATIC,
2403 names.fromString("arraycopy"),
2404 new MethodType(List.<Type>of(syms.objectType,
2405 syms.intType,
2406 syms.objectType,
2407 syms.intType,
2408 syms.intType),
2409 syms.voidType,
2410 List.<Type>nil(),
2411 syms.methodClass),
2412 syms.systemType.tsym);
2413 }
2414 JCStatement copy =
2415 make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym),
2416 systemArraycopyMethod),
2417 List.of(make.Ident(valuesVar), make.Literal(0),
2418 make.Ident(resultVar), make.Literal(0),
2419 make.Select(make.Ident(valuesVar), syms.lengthVar))));
2421 // template: return $result;
2422 JCStatement ret = make.Return(make.Ident(resultVar));
2423 valuesBody = List.<JCStatement>of(decl, copy, ret);
2424 }
2426 JCMethodDecl valuesDef =
2427 make.MethodDef((MethodSymbol)valuesSym, make.Block(0, valuesBody));
2429 enumDefs.append(valuesDef);
2431 if (debugLower)
2432 System.err.println(tree.sym + ".valuesDef = " + valuesDef);
2434 /** The template for the following code is:
2435 *
2436 * public static E valueOf(String name) {
2437 * return (E)Enum.valueOf(E.class, name);
2438 * }
2439 *
2440 * where E is tree.sym
2441 */
2442 MethodSymbol valueOfSym = lookupMethod(tree.pos(),
2443 names.valueOf,
2444 tree.sym.type,
2445 List.of(syms.stringType));
2446 Assert.check((valueOfSym.flags() & STATIC) != 0);
2447 VarSymbol nameArgSym = valueOfSym.params.head;
2448 JCIdent nameVal = make.Ident(nameArgSym);
2449 JCStatement enum_ValueOf =
2450 make.Return(make.TypeCast(tree.sym.type,
2451 makeCall(make.Ident(syms.enumSym),
2452 names.valueOf,
2453 List.of(e_class, nameVal))));
2454 JCMethodDecl valueOf = make.MethodDef(valueOfSym,
2455 make.Block(0, List.of(enum_ValueOf)));
2456 nameVal.sym = valueOf.params.head.sym;
2457 if (debugLower)
2458 System.err.println(tree.sym + ".valueOf = " + valueOf);
2459 enumDefs.append(valueOf);
2461 enumDefs.appendList(otherDefs.toList());
2462 tree.defs = enumDefs.toList();
2464 // Add the necessary members for the EnumCompatibleMode
2465 if (target.compilerBootstrap(tree.sym)) {
2466 addEnumCompatibleMembers(tree);
2467 }
2468 }
2469 // where
2470 private MethodSymbol systemArraycopyMethod;
2471 private boolean useClone() {
2472 try {
2473 Scope.Entry e = syms.objectType.tsym.members().lookup(names.clone);
2474 return (e.sym != null);
2475 }
2476 catch (CompletionFailure e) {
2477 return false;
2478 }
2479 }
2481 /** Translate an enumeration constant and its initializer. */
2482 private void visitEnumConstantDef(JCVariableDecl var, int ordinal) {
2483 JCNewClass varDef = (JCNewClass)var.init;
2484 varDef.args = varDef.args.
2485 prepend(makeLit(syms.intType, ordinal)).
2486 prepend(makeLit(syms.stringType, var.name.toString()));
2487 }
2489 public void visitMethodDef(JCMethodDecl tree) {
2490 if (tree.name == names.init && (currentClass.flags_field&ENUM) != 0) {
2491 // Add "String $enum$name, int $enum$ordinal" to the beginning of the
2492 // argument list for each constructor of an enum.
2493 JCVariableDecl nameParam = make_at(tree.pos()).
2494 Param(names.fromString(target.syntheticNameChar() +
2495 "enum" + target.syntheticNameChar() + "name"),
2496 syms.stringType, tree.sym);
2497 nameParam.mods.flags |= SYNTHETIC; nameParam.sym.flags_field |= SYNTHETIC;
2499 JCVariableDecl ordParam = make.
2500 Param(names.fromString(target.syntheticNameChar() +
2501 "enum" + target.syntheticNameChar() +
2502 "ordinal"),
2503 syms.intType, tree.sym);
2504 ordParam.mods.flags |= SYNTHETIC; ordParam.sym.flags_field |= SYNTHETIC;
2506 tree.params = tree.params.prepend(ordParam).prepend(nameParam);
2508 MethodSymbol m = tree.sym;
2509 Type olderasure = m.erasure(types);
2510 m.erasure_field = new MethodType(
2511 olderasure.getParameterTypes().prepend(syms.intType).prepend(syms.stringType),
2512 olderasure.getReturnType(),
2513 olderasure.getThrownTypes(),
2514 syms.methodClass);
2516 if (target.compilerBootstrap(m.owner)) {
2517 // Initialize synthetic name field
2518 Symbol nameVarSym = lookupSynthetic(names.fromString("$name"),
2519 tree.sym.owner.members());
2520 JCIdent nameIdent = make.Ident(nameParam.sym);
2521 JCIdent id1 = make.Ident(nameVarSym);
2522 JCAssign newAssign = make.Assign(id1, nameIdent);
2523 newAssign.type = id1.type;
2524 JCExpressionStatement nameAssign = make.Exec(newAssign);
2525 nameAssign.type = id1.type;
2526 tree.body.stats = tree.body.stats.prepend(nameAssign);
2528 // Initialize synthetic ordinal field
2529 Symbol ordinalVarSym = lookupSynthetic(names.fromString("$ordinal"),
2530 tree.sym.owner.members());
2531 JCIdent ordIdent = make.Ident(ordParam.sym);
2532 id1 = make.Ident(ordinalVarSym);
2533 newAssign = make.Assign(id1, ordIdent);
2534 newAssign.type = id1.type;
2535 JCExpressionStatement ordinalAssign = make.Exec(newAssign);
2536 ordinalAssign.type = id1.type;
2537 tree.body.stats = tree.body.stats.prepend(ordinalAssign);
2538 }
2539 }
2541 JCMethodDecl prevMethodDef = currentMethodDef;
2542 MethodSymbol prevMethodSym = currentMethodSym;
2543 try {
2544 currentMethodDef = tree;
2545 currentMethodSym = tree.sym;
2546 visitMethodDefInternal(tree);
2547 } finally {
2548 currentMethodDef = prevMethodDef;
2549 currentMethodSym = prevMethodSym;
2550 }
2551 }
2552 //where
2553 private void visitMethodDefInternal(JCMethodDecl tree) {
2554 if (tree.name == names.init &&
2555 (currentClass.isInner() ||
2556 (currentClass.owner.kind & (VAR | MTH)) != 0)) {
2557 // We are seeing a constructor of an inner class.
2558 MethodSymbol m = tree.sym;
2560 // Push a new proxy scope for constructor parameters.
2561 // and create definitions for any this$n and proxy parameters.
2562 proxies = proxies.dup(m);
2563 List<VarSymbol> prevOuterThisStack = outerThisStack;
2564 List<VarSymbol> fvs = freevars(currentClass);
2565 JCVariableDecl otdef = null;
2566 if (currentClass.hasOuterInstance())
2567 otdef = outerThisDef(tree.pos, m);
2568 List<JCVariableDecl> fvdefs = freevarDefs(tree.pos, fvs, m);
2570 // Recursively translate result type, parameters and thrown list.
2571 tree.restype = translate(tree.restype);
2572 tree.params = translateVarDefs(tree.params);
2573 tree.thrown = translate(tree.thrown);
2575 // when compiling stubs, don't process body
2576 if (tree.body == null) {
2577 result = tree;
2578 return;
2579 }
2581 // Add this$n (if needed) in front of and free variables behind
2582 // constructor parameter list.
2583 tree.params = tree.params.appendList(fvdefs);
2584 if (currentClass.hasOuterInstance())
2585 tree.params = tree.params.prepend(otdef);
2587 // If this is an initial constructor, i.e., it does not start with
2588 // this(...), insert initializers for this$n and proxies
2589 // before (pre-1.4, after) the call to superclass constructor.
2590 JCStatement selfCall = translate(tree.body.stats.head);
2592 List<JCStatement> added = List.nil();
2593 if (fvs.nonEmpty()) {
2594 List<Type> addedargtypes = List.nil();
2595 for (List<VarSymbol> l = fvs; l.nonEmpty(); l = l.tail) {
2596 if (TreeInfo.isInitialConstructor(tree))
2597 added = added.prepend(
2598 initField(tree.body.pos, proxyName(l.head.name)));
2599 addedargtypes = addedargtypes.prepend(l.head.erasure(types));
2600 }
2601 Type olderasure = m.erasure(types);
2602 m.erasure_field = new MethodType(
2603 olderasure.getParameterTypes().appendList(addedargtypes),
2604 olderasure.getReturnType(),
2605 olderasure.getThrownTypes(),
2606 syms.methodClass);
2607 }
2608 if (currentClass.hasOuterInstance() &&
2609 TreeInfo.isInitialConstructor(tree))
2610 {
2611 added = added.prepend(initOuterThis(tree.body.pos));
2612 }
2614 // pop local variables from proxy stack
2615 proxies = proxies.leave();
2617 // recursively translate following local statements and
2618 // combine with this- or super-call
2619 List<JCStatement> stats = translate(tree.body.stats.tail);
2620 if (target.initializeFieldsBeforeSuper())
2621 tree.body.stats = stats.prepend(selfCall).prependList(added);
2622 else
2623 tree.body.stats = stats.prependList(added).prepend(selfCall);
2625 outerThisStack = prevOuterThisStack;
2626 } else {
2627 super.visitMethodDef(tree);
2628 }
2629 result = tree;
2630 }
2632 public void visitTypeCast(JCTypeCast tree) {
2633 tree.clazz = translate(tree.clazz);
2634 if (tree.type.isPrimitive() != tree.expr.type.isPrimitive())
2635 tree.expr = translate(tree.expr, tree.type);
2636 else
2637 tree.expr = translate(tree.expr);
2638 result = tree;
2639 }
2641 public void visitNewClass(JCNewClass tree) {
2642 ClassSymbol c = (ClassSymbol)tree.constructor.owner;
2644 // Box arguments, if necessary
2645 boolean isEnum = (tree.constructor.owner.flags() & ENUM) != 0;
2646 List<Type> argTypes = tree.constructor.type.getParameterTypes();
2647 if (isEnum) argTypes = argTypes.prepend(syms.intType).prepend(syms.stringType);
2648 tree.args = boxArgs(argTypes, tree.args, tree.varargsElement);
2649 tree.varargsElement = null;
2651 // If created class is local, add free variables after
2652 // explicit constructor arguments.
2653 if ((c.owner.kind & (VAR | MTH)) != 0) {
2654 tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
2655 }
2657 // If an access constructor is used, append null as a last argument.
2658 Symbol constructor = accessConstructor(tree.pos(), tree.constructor);
2659 if (constructor != tree.constructor) {
2660 tree.args = tree.args.append(makeNull());
2661 tree.constructor = constructor;
2662 }
2664 // If created class has an outer instance, and new is qualified, pass
2665 // qualifier as first argument. If new is not qualified, pass the
2666 // correct outer instance as first argument.
2667 if (c.hasOuterInstance()) {
2668 JCExpression thisArg;
2669 if (tree.encl != null) {
2670 thisArg = attr.makeNullCheck(translate(tree.encl));
2671 thisArg.type = tree.encl.type;
2672 } else if ((c.owner.kind & (MTH | VAR)) != 0) {
2673 // local class
2674 thisArg = makeThis(tree.pos(), c.type.getEnclosingType().tsym);
2675 } else {
2676 // nested class
2677 thisArg = makeOwnerThis(tree.pos(), c, false);
2678 }
2679 tree.args = tree.args.prepend(thisArg);
2680 }
2681 tree.encl = null;
2683 // If we have an anonymous class, create its flat version, rather
2684 // than the class or interface following new.
2685 if (tree.def != null) {
2686 translate(tree.def);
2687 tree.clazz = access(make_at(tree.clazz.pos()).Ident(tree.def.sym));
2688 tree.def = null;
2689 } else {
2690 tree.clazz = access(c, tree.clazz, enclOp, false);
2691 }
2692 result = tree;
2693 }
2695 // Simplify conditionals with known constant controlling expressions.
2696 // This allows us to avoid generating supporting declarations for
2697 // the dead code, which will not be eliminated during code generation.
2698 // Note that Flow.isFalse and Flow.isTrue only return true
2699 // for constant expressions in the sense of JLS 15.27, which
2700 // are guaranteed to have no side-effects. More aggressive
2701 // constant propagation would require that we take care to
2702 // preserve possible side-effects in the condition expression.
2704 /** Visitor method for conditional expressions.
2705 */
2706 public void visitConditional(JCConditional tree) {
2707 JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
2708 if (cond.type.isTrue()) {
2709 result = convert(translate(tree.truepart, tree.type), tree.type);
2710 } else if (cond.type.isFalse()) {
2711 result = convert(translate(tree.falsepart, tree.type), tree.type);
2712 } else {
2713 // Condition is not a compile-time constant.
2714 tree.truepart = translate(tree.truepart, tree.type);
2715 tree.falsepart = translate(tree.falsepart, tree.type);
2716 result = tree;
2717 }
2718 }
2719 //where
2720 private JCTree convert(JCTree tree, Type pt) {
2721 if (tree.type == pt || tree.type.tag == TypeTags.BOT)
2722 return tree;
2723 JCTree result = make_at(tree.pos()).TypeCast(make.Type(pt), (JCExpression)tree);
2724 result.type = (tree.type.constValue() != null) ? cfolder.coerce(tree.type, pt)
2725 : pt;
2726 return result;
2727 }
2729 /** Visitor method for if statements.
2730 */
2731 public void visitIf(JCIf tree) {
2732 JCTree cond = tree.cond = translate(tree.cond, syms.booleanType);
2733 if (cond.type.isTrue()) {
2734 result = translate(tree.thenpart);
2735 } else if (cond.type.isFalse()) {
2736 if (tree.elsepart != null) {
2737 result = translate(tree.elsepart);
2738 } else {
2739 result = make.Skip();
2740 }
2741 } else {
2742 // Condition is not a compile-time constant.
2743 tree.thenpart = translate(tree.thenpart);
2744 tree.elsepart = translate(tree.elsepart);
2745 result = tree;
2746 }
2747 }
2749 /** Visitor method for assert statements. Translate them away.
2750 */
2751 public void visitAssert(JCAssert tree) {
2752 DiagnosticPosition detailPos = (tree.detail == null) ? tree.pos() : tree.detail.pos();
2753 tree.cond = translate(tree.cond, syms.booleanType);
2754 if (!tree.cond.type.isTrue()) {
2755 JCExpression cond = assertFlagTest(tree.pos());
2756 List<JCExpression> exnArgs = (tree.detail == null) ?
2757 List.<JCExpression>nil() : List.of(translate(tree.detail));
2758 if (!tree.cond.type.isFalse()) {
2759 cond = makeBinary
2760 (JCTree.AND,
2761 cond,
2762 makeUnary(JCTree.NOT, tree.cond));
2763 }
2764 result =
2765 make.If(cond,
2766 make_at(detailPos).
2767 Throw(makeNewClass(syms.assertionErrorType, exnArgs)),
2768 null);
2769 } else {
2770 result = make.Skip();
2771 }
2772 }
2774 public void visitApply(JCMethodInvocation tree) {
2775 Symbol meth = TreeInfo.symbol(tree.meth);
2776 List<Type> argtypes = meth.type.getParameterTypes();
2777 if (allowEnums &&
2778 meth.name==names.init &&
2779 meth.owner == syms.enumSym)
2780 argtypes = argtypes.tail.tail;
2781 tree.args = boxArgs(argtypes, tree.args, tree.varargsElement);
2782 tree.varargsElement = null;
2783 Name methName = TreeInfo.name(tree.meth);
2784 if (meth.name==names.init) {
2785 // We are seeing a this(...) or super(...) constructor call.
2786 // If an access constructor is used, append null as a last argument.
2787 Symbol constructor = accessConstructor(tree.pos(), meth);
2788 if (constructor != meth) {
2789 tree.args = tree.args.append(makeNull());
2790 TreeInfo.setSymbol(tree.meth, constructor);
2791 }
2793 // If we are calling a constructor of a local class, add
2794 // free variables after explicit constructor arguments.
2795 ClassSymbol c = (ClassSymbol)constructor.owner;
2796 if ((c.owner.kind & (VAR | MTH)) != 0) {
2797 tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
2798 }
2800 // If we are calling a constructor of an enum class, pass
2801 // along the name and ordinal arguments
2802 if ((c.flags_field&ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) {
2803 List<JCVariableDecl> params = currentMethodDef.params;
2804 if (currentMethodSym.owner.hasOuterInstance())
2805 params = params.tail; // drop this$n
2806 tree.args = tree.args
2807 .prepend(make_at(tree.pos()).Ident(params.tail.head.sym)) // ordinal
2808 .prepend(make.Ident(params.head.sym)); // name
2809 }
2811 // If we are calling a constructor of a class with an outer
2812 // instance, and the call
2813 // is qualified, pass qualifier as first argument in front of
2814 // the explicit constructor arguments. If the call
2815 // is not qualified, pass the correct outer instance as
2816 // first argument.
2817 if (c.hasOuterInstance()) {
2818 JCExpression thisArg;
2819 if (tree.meth.getTag() == JCTree.SELECT) {
2820 thisArg = attr.
2821 makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
2822 tree.meth = make.Ident(constructor);
2823 ((JCIdent) tree.meth).name = methName;
2824 } else if ((c.owner.kind & (MTH | VAR)) != 0 || methName == names._this){
2825 // local class or this() call
2826 thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
2827 } else {
2828 // super() call of nested class - never pick 'this'
2829 thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
2830 }
2831 tree.args = tree.args.prepend(thisArg);
2832 }
2833 } else {
2834 // We are seeing a normal method invocation; translate this as usual.
2835 tree.meth = translate(tree.meth);
2837 // If the translated method itself is an Apply tree, we are
2838 // seeing an access method invocation. In this case, append
2839 // the method arguments to the arguments of the access method.
2840 if (tree.meth.getTag() == JCTree.APPLY) {
2841 JCMethodInvocation app = (JCMethodInvocation)tree.meth;
2842 app.args = tree.args.prependList(app.args);
2843 result = app;
2844 return;
2845 }
2846 }
2847 result = tree;
2848 }
2850 List<JCExpression> boxArgs(List<Type> parameters, List<JCExpression> _args, Type varargsElement) {
2851 List<JCExpression> args = _args;
2852 if (parameters.isEmpty()) return args;
2853 boolean anyChanges = false;
2854 ListBuffer<JCExpression> result = new ListBuffer<JCExpression>();
2855 while (parameters.tail.nonEmpty()) {
2856 JCExpression arg = translate(args.head, parameters.head);
2857 anyChanges |= (arg != args.head);
2858 result.append(arg);
2859 args = args.tail;
2860 parameters = parameters.tail;
2861 }
2862 Type parameter = parameters.head;
2863 if (varargsElement != null) {
2864 anyChanges = true;
2865 ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
2866 while (args.nonEmpty()) {
2867 JCExpression arg = translate(args.head, varargsElement);
2868 elems.append(arg);
2869 args = args.tail;
2870 }
2871 JCNewArray boxedArgs = make.NewArray(make.Type(varargsElement),
2872 List.<JCExpression>nil(),
2873 elems.toList());
2874 boxedArgs.type = new ArrayType(varargsElement, syms.arrayClass);
2875 result.append(boxedArgs);
2876 } else {
2877 if (args.length() != 1) throw new AssertionError(args);
2878 JCExpression arg = translate(args.head, parameter);
2879 anyChanges |= (arg != args.head);
2880 result.append(arg);
2881 if (!anyChanges) return _args;
2882 }
2883 return result.toList();
2884 }
2886 /** Expand a boxing or unboxing conversion if needed. */
2887 @SuppressWarnings("unchecked") // XXX unchecked
2888 <T extends JCTree> T boxIfNeeded(T tree, Type type) {
2889 boolean havePrimitive = tree.type.isPrimitive();
2890 if (havePrimitive == type.isPrimitive())
2891 return tree;
2892 if (havePrimitive) {
2893 Type unboxedTarget = types.unboxedType(type);
2894 if (unboxedTarget.tag != NONE) {
2895 if (!types.isSubtype(tree.type, unboxedTarget)) //e.g. Character c = 89;
2896 tree.type = unboxedTarget.constType(tree.type.constValue());
2897 return (T)boxPrimitive((JCExpression)tree, type);
2898 } else {
2899 tree = (T)boxPrimitive((JCExpression)tree);
2900 }
2901 } else {
2902 tree = (T)unbox((JCExpression)tree, type);
2903 }
2904 return tree;
2905 }
2907 /** Box up a single primitive expression. */
2908 JCExpression boxPrimitive(JCExpression tree) {
2909 return boxPrimitive(tree, types.boxedClass(tree.type).type);
2910 }
2912 /** Box up a single primitive expression. */
2913 JCExpression boxPrimitive(JCExpression tree, Type box) {
2914 make_at(tree.pos());
2915 if (target.boxWithConstructors()) {
2916 Symbol ctor = lookupConstructor(tree.pos(),
2917 box,
2918 List.<Type>nil()
2919 .prepend(tree.type));
2920 return make.Create(ctor, List.of(tree));
2921 } else {
2922 Symbol valueOfSym = lookupMethod(tree.pos(),
2923 names.valueOf,
2924 box,
2925 List.<Type>nil()
2926 .prepend(tree.type));
2927 return make.App(make.QualIdent(valueOfSym), List.of(tree));
2928 }
2929 }
2931 /** Unbox an object to a primitive value. */
2932 JCExpression unbox(JCExpression tree, Type primitive) {
2933 Type unboxedType = types.unboxedType(tree.type);
2934 if (unboxedType.tag == NONE) {
2935 unboxedType = primitive;
2936 if (!unboxedType.isPrimitive())
2937 throw new AssertionError(unboxedType);
2938 make_at(tree.pos());
2939 tree = make.TypeCast(types.boxedClass(unboxedType).type, tree);
2940 } else {
2941 // There must be a conversion from unboxedType to primitive.
2942 if (!types.isSubtype(unboxedType, primitive))
2943 throw new AssertionError(tree);
2944 }
2945 make_at(tree.pos());
2946 Symbol valueSym = lookupMethod(tree.pos(),
2947 unboxedType.tsym.name.append(names.Value), // x.intValue()
2948 tree.type,
2949 List.<Type>nil());
2950 return make.App(make.Select(tree, valueSym));
2951 }
2953 /** Visitor method for parenthesized expressions.
2954 * If the subexpression has changed, omit the parens.
2955 */
2956 public void visitParens(JCParens tree) {
2957 JCTree expr = translate(tree.expr);
2958 result = ((expr == tree.expr) ? tree : expr);
2959 }
2961 public void visitIndexed(JCArrayAccess tree) {
2962 tree.indexed = translate(tree.indexed);
2963 tree.index = translate(tree.index, syms.intType);
2964 result = tree;
2965 }
2967 public void visitAssign(JCAssign tree) {
2968 tree.lhs = translate(tree.lhs, tree);
2969 tree.rhs = translate(tree.rhs, tree.lhs.type);
2971 // If translated left hand side is an Apply, we are
2972 // seeing an access method invocation. In this case, append
2973 // right hand side as last argument of the access method.
2974 if (tree.lhs.getTag() == JCTree.APPLY) {
2975 JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
2976 app.args = List.of(tree.rhs).prependList(app.args);
2977 result = app;
2978 } else {
2979 result = tree;
2980 }
2981 }
2983 public void visitAssignop(final JCAssignOp tree) {
2984 if (!tree.lhs.type.isPrimitive() &&
2985 tree.operator.type.getReturnType().isPrimitive()) {
2986 // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
2987 // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
2988 // (but without recomputing x)
2989 JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() {
2990 public JCTree build(final JCTree lhs) {
2991 int newTag = tree.getTag() - JCTree.ASGOffset;
2992 // Erasure (TransTypes) can change the type of
2993 // tree.lhs. However, we can still get the
2994 // unerased type of tree.lhs as it is stored
2995 // in tree.type in Attr.
2996 Symbol newOperator = rs.resolveBinaryOperator(tree.pos(),
2997 newTag,
2998 attrEnv,
2999 tree.type,
3000 tree.rhs.type);
3001 JCExpression expr = (JCExpression)lhs;
3002 if (expr.type != tree.type)
3003 expr = make.TypeCast(tree.type, expr);
3004 JCBinary opResult = make.Binary(newTag, expr, tree.rhs);
3005 opResult.operator = newOperator;
3006 opResult.type = newOperator.type.getReturnType();
3007 JCTypeCast newRhs = make.TypeCast(types.unboxedType(tree.type),
3008 opResult);
3009 return make.Assign((JCExpression)lhs, newRhs).setType(tree.type);
3010 }
3011 });
3012 result = translate(newTree);
3013 return;
3014 }
3015 tree.lhs = translate(tree.lhs, tree);
3016 tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
3018 // If translated left hand side is an Apply, we are
3019 // seeing an access method invocation. In this case, append
3020 // right hand side as last argument of the access method.
3021 if (tree.lhs.getTag() == JCTree.APPLY) {
3022 JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
3023 // if operation is a += on strings,
3024 // make sure to convert argument to string
3025 JCExpression rhs = (((OperatorSymbol)tree.operator).opcode == string_add)
3026 ? makeString(tree.rhs)
3027 : tree.rhs;
3028 app.args = List.of(rhs).prependList(app.args);
3029 result = app;
3030 } else {
3031 result = tree;
3032 }
3033 }
3035 /** Lower a tree of the form e++ or e-- where e is an object type */
3036 JCTree lowerBoxedPostop(final JCUnary tree) {
3037 // translate to tmp1=lval(e); tmp2=tmp1; tmp1 OP 1; tmp2
3038 // or
3039 // translate to tmp1=lval(e); tmp2=tmp1; (typeof tree)tmp1 OP 1; tmp2
3040 // where OP is += or -=
3041 final boolean cast = TreeInfo.skipParens(tree.arg).getTag() == JCTree.TYPECAST;
3042 return abstractLval(tree.arg, new TreeBuilder() {
3043 public JCTree build(final JCTree tmp1) {
3044 return abstractRval(tmp1, tree.arg.type, new TreeBuilder() {
3045 public JCTree build(final JCTree tmp2) {
3046 int opcode = (tree.getTag() == JCTree.POSTINC)
3047 ? JCTree.PLUS_ASG : JCTree.MINUS_ASG;
3048 JCTree lhs = cast
3049 ? make.TypeCast(tree.arg.type, (JCExpression)tmp1)
3050 : tmp1;
3051 JCTree update = makeAssignop(opcode,
3052 lhs,
3053 make.Literal(1));
3054 return makeComma(update, tmp2);
3055 }
3056 });
3057 }
3058 });
3059 }
3061 public void visitUnary(JCUnary tree) {
3062 boolean isUpdateOperator =
3063 JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC;
3064 if (isUpdateOperator && !tree.arg.type.isPrimitive()) {
3065 switch(tree.getTag()) {
3066 case JCTree.PREINC: // ++ e
3067 // translate to e += 1
3068 case JCTree.PREDEC: // -- e
3069 // translate to e -= 1
3070 {
3071 int opcode = (tree.getTag() == JCTree.PREINC)
3072 ? JCTree.PLUS_ASG : JCTree.MINUS_ASG;
3073 JCAssignOp newTree = makeAssignop(opcode,
3074 tree.arg,
3075 make.Literal(1));
3076 result = translate(newTree, tree.type);
3077 return;
3078 }
3079 case JCTree.POSTINC: // e ++
3080 case JCTree.POSTDEC: // e --
3081 {
3082 result = translate(lowerBoxedPostop(tree), tree.type);
3083 return;
3084 }
3085 }
3086 throw new AssertionError(tree);
3087 }
3089 tree.arg = boxIfNeeded(translate(tree.arg, tree), tree.type);
3091 if (tree.getTag() == JCTree.NOT && tree.arg.type.constValue() != null) {
3092 tree.type = cfolder.fold1(bool_not, tree.arg.type);
3093 }
3095 // If translated left hand side is an Apply, we are
3096 // seeing an access method invocation. In this case, return
3097 // that access method invocation as result.
3098 if (isUpdateOperator && tree.arg.getTag() == JCTree.APPLY) {
3099 result = tree.arg;
3100 } else {
3101 result = tree;
3102 }
3103 }
3105 public void visitBinary(JCBinary tree) {
3106 List<Type> formals = tree.operator.type.getParameterTypes();
3107 JCTree lhs = tree.lhs = translate(tree.lhs, formals.head);
3108 switch (tree.getTag()) {
3109 case JCTree.OR:
3110 if (lhs.type.isTrue()) {
3111 result = lhs;
3112 return;
3113 }
3114 if (lhs.type.isFalse()) {
3115 result = translate(tree.rhs, formals.tail.head);
3116 return;
3117 }
3118 break;
3119 case JCTree.AND:
3120 if (lhs.type.isFalse()) {
3121 result = lhs;
3122 return;
3123 }
3124 if (lhs.type.isTrue()) {
3125 result = translate(tree.rhs, formals.tail.head);
3126 return;
3127 }
3128 break;
3129 }
3130 tree.rhs = translate(tree.rhs, formals.tail.head);
3131 result = tree;
3132 }
3134 public void visitIdent(JCIdent tree) {
3135 result = access(tree.sym, tree, enclOp, false);
3136 }
3138 /** Translate away the foreach loop. */
3139 public void visitForeachLoop(JCEnhancedForLoop tree) {
3140 if (types.elemtype(tree.expr.type) == null)
3141 visitIterableForeachLoop(tree);
3142 else
3143 visitArrayForeachLoop(tree);
3144 }
3145 // where
3146 /**
3147 * A statement of the form
3148 *
3149 * <pre>
3150 * for ( T v : arrayexpr ) stmt;
3151 * </pre>
3152 *
3153 * (where arrayexpr is of an array type) gets translated to
3154 *
3155 * <pre>
3156 * for ( { arraytype #arr = arrayexpr;
3157 * int #len = array.length;
3158 * int #i = 0; };
3159 * #i < #len; i$++ ) {
3160 * T v = arr$[#i];
3161 * stmt;
3162 * }
3163 * </pre>
3164 *
3165 * where #arr, #len, and #i are freshly named synthetic local variables.
3166 */
3167 private void visitArrayForeachLoop(JCEnhancedForLoop tree) {
3168 make_at(tree.expr.pos());
3169 VarSymbol arraycache = new VarSymbol(0,
3170 names.fromString("arr" + target.syntheticNameChar()),
3171 tree.expr.type,
3172 currentMethodSym);
3173 JCStatement arraycachedef = make.VarDef(arraycache, tree.expr);
3174 VarSymbol lencache = new VarSymbol(0,
3175 names.fromString("len" + target.syntheticNameChar()),
3176 syms.intType,
3177 currentMethodSym);
3178 JCStatement lencachedef = make.
3179 VarDef(lencache, make.Select(make.Ident(arraycache), syms.lengthVar));
3180 VarSymbol index = new VarSymbol(0,
3181 names.fromString("i" + target.syntheticNameChar()),
3182 syms.intType,
3183 currentMethodSym);
3185 JCVariableDecl indexdef = make.VarDef(index, make.Literal(INT, 0));
3186 indexdef.init.type = indexdef.type = syms.intType.constType(0);
3188 List<JCStatement> loopinit = List.of(arraycachedef, lencachedef, indexdef);
3189 JCBinary cond = makeBinary(JCTree.LT, make.Ident(index), make.Ident(lencache));
3191 JCExpressionStatement step = make.Exec(makeUnary(JCTree.PREINC, make.Ident(index)));
3193 Type elemtype = types.elemtype(tree.expr.type);
3194 JCExpression loopvarinit = make.Indexed(make.Ident(arraycache),
3195 make.Ident(index)).setType(elemtype);
3196 JCVariableDecl loopvardef = (JCVariableDecl)make.VarDef(tree.var.mods,
3197 tree.var.name,
3198 tree.var.vartype,
3199 loopvarinit).setType(tree.var.type);
3200 loopvardef.sym = tree.var.sym;
3201 JCBlock body = make.
3202 Block(0, List.of(loopvardef, tree.body));
3204 result = translate(make.
3205 ForLoop(loopinit,
3206 cond,
3207 List.of(step),
3208 body));
3209 patchTargets(body, tree, result);
3210 }
3211 /** Patch up break and continue targets. */
3212 private void patchTargets(JCTree body, final JCTree src, final JCTree dest) {
3213 class Patcher extends TreeScanner {
3214 public void visitBreak(JCBreak tree) {
3215 if (tree.target == src)
3216 tree.target = dest;
3217 }
3218 public void visitContinue(JCContinue tree) {
3219 if (tree.target == src)
3220 tree.target = dest;
3221 }
3222 public void visitClassDef(JCClassDecl tree) {}
3223 }
3224 new Patcher().scan(body);
3225 }
3226 /**
3227 * A statement of the form
3228 *
3229 * <pre>
3230 * for ( T v : coll ) stmt ;
3231 * </pre>
3232 *
3233 * (where coll implements Iterable<? extends T>) gets translated to
3234 *
3235 * <pre>
3236 * for ( Iterator<? extends T> #i = coll.iterator(); #i.hasNext(); ) {
3237 * T v = (T) #i.next();
3238 * stmt;
3239 * }
3240 * </pre>
3241 *
3242 * where #i is a freshly named synthetic local variable.
3243 */
3244 private void visitIterableForeachLoop(JCEnhancedForLoop tree) {
3245 make_at(tree.expr.pos());
3246 Type iteratorTarget = syms.objectType;
3247 Type iterableType = types.asSuper(types.upperBound(tree.expr.type),
3248 syms.iterableType.tsym);
3249 if (iterableType.getTypeArguments().nonEmpty())
3250 iteratorTarget = types.erasure(iterableType.getTypeArguments().head);
3251 Type eType = tree.expr.type;
3252 tree.expr.type = types.erasure(eType);
3253 if (eType.tag == TYPEVAR && eType.getUpperBound().isCompound())
3254 tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr);
3255 Symbol iterator = lookupMethod(tree.expr.pos(),
3256 names.iterator,
3257 types.erasure(syms.iterableType),
3258 List.<Type>nil());
3259 VarSymbol itvar = new VarSymbol(0, names.fromString("i" + target.syntheticNameChar()),
3260 types.erasure(iterator.type.getReturnType()),
3261 currentMethodSym);
3262 JCStatement init = make.
3263 VarDef(itvar,
3264 make.App(make.Select(tree.expr, iterator)));
3265 Symbol hasNext = lookupMethod(tree.expr.pos(),
3266 names.hasNext,
3267 itvar.type,
3268 List.<Type>nil());
3269 JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext));
3270 Symbol next = lookupMethod(tree.expr.pos(),
3271 names.next,
3272 itvar.type,
3273 List.<Type>nil());
3274 JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next));
3275 if (tree.var.type.isPrimitive())
3276 vardefinit = make.TypeCast(types.upperBound(iteratorTarget), vardefinit);
3277 else
3278 vardefinit = make.TypeCast(tree.var.type, vardefinit);
3279 JCVariableDecl indexDef = (JCVariableDecl)make.VarDef(tree.var.mods,
3280 tree.var.name,
3281 tree.var.vartype,
3282 vardefinit).setType(tree.var.type);
3283 indexDef.sym = tree.var.sym;
3284 JCBlock body = make.Block(0, List.of(indexDef, tree.body));
3285 body.endpos = TreeInfo.endPos(tree.body);
3286 result = translate(make.
3287 ForLoop(List.of(init),
3288 cond,
3289 List.<JCExpressionStatement>nil(),
3290 body));
3291 patchTargets(body, tree, result);
3292 }
3294 public void visitVarDef(JCVariableDecl tree) {
3295 MethodSymbol oldMethodSym = currentMethodSym;
3296 tree.mods = translate(tree.mods);
3297 tree.vartype = translate(tree.vartype);
3298 if (currentMethodSym == null) {
3299 // A class or instance field initializer.
3300 currentMethodSym =
3301 new MethodSymbol((tree.mods.flags&STATIC) | BLOCK,
3302 names.empty, null,
3303 currentClass);
3304 }
3305 if (tree.init != null) tree.init = translate(tree.init, tree.type);
3306 result = tree;
3307 currentMethodSym = oldMethodSym;
3308 }
3310 public void visitBlock(JCBlock tree) {
3311 MethodSymbol oldMethodSym = currentMethodSym;
3312 if (currentMethodSym == null) {
3313 // Block is a static or instance initializer.
3314 currentMethodSym =
3315 new MethodSymbol(tree.flags | BLOCK,
3316 names.empty, null,
3317 currentClass);
3318 }
3319 super.visitBlock(tree);
3320 currentMethodSym = oldMethodSym;
3321 }
3323 public void visitDoLoop(JCDoWhileLoop tree) {
3324 tree.body = translate(tree.body);
3325 tree.cond = translate(tree.cond, syms.booleanType);
3326 result = tree;
3327 }
3329 public void visitWhileLoop(JCWhileLoop tree) {
3330 tree.cond = translate(tree.cond, syms.booleanType);
3331 tree.body = translate(tree.body);
3332 result = tree;
3333 }
3335 public void visitForLoop(JCForLoop tree) {
3336 tree.init = translate(tree.init);
3337 if (tree.cond != null)
3338 tree.cond = translate(tree.cond, syms.booleanType);
3339 tree.step = translate(tree.step);
3340 tree.body = translate(tree.body);
3341 result = tree;
3342 }
3344 public void visitReturn(JCReturn tree) {
3345 if (tree.expr != null)
3346 tree.expr = translate(tree.expr,
3347 types.erasure(currentMethodDef
3348 .restype.type));
3349 result = tree;
3350 }
3352 public void visitSwitch(JCSwitch tree) {
3353 Type selsuper = types.supertype(tree.selector.type);
3354 boolean enumSwitch = selsuper != null &&
3355 (tree.selector.type.tsym.flags() & ENUM) != 0;
3356 boolean stringSwitch = selsuper != null &&
3357 types.isSameType(tree.selector.type, syms.stringType);
3358 Type target = enumSwitch ? tree.selector.type :
3359 (stringSwitch? syms.stringType : syms.intType);
3360 tree.selector = translate(tree.selector, target);
3361 tree.cases = translateCases(tree.cases);
3362 if (enumSwitch) {
3363 result = visitEnumSwitch(tree);
3364 } else if (stringSwitch) {
3365 result = visitStringSwitch(tree);
3366 } else {
3367 result = tree;
3368 }
3369 }
3371 public JCTree visitEnumSwitch(JCSwitch tree) {
3372 TypeSymbol enumSym = tree.selector.type.tsym;
3373 EnumMapping map = mapForEnum(tree.pos(), enumSym);
3374 make_at(tree.pos());
3375 Symbol ordinalMethod = lookupMethod(tree.pos(),
3376 names.ordinal,
3377 tree.selector.type,
3378 List.<Type>nil());
3379 JCArrayAccess selector = make.Indexed(map.mapVar,
3380 make.App(make.Select(tree.selector,
3381 ordinalMethod)));
3382 ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
3383 for (JCCase c : tree.cases) {
3384 if (c.pat != null) {
3385 VarSymbol label = (VarSymbol)TreeInfo.symbol(c.pat);
3386 JCLiteral pat = map.forConstant(label);
3387 cases.append(make.Case(pat, c.stats));
3388 } else {
3389 cases.append(c);
3390 }
3391 }
3392 JCSwitch enumSwitch = make.Switch(selector, cases.toList());
3393 patchTargets(enumSwitch, tree, enumSwitch);
3394 return enumSwitch;
3395 }
3397 public JCTree visitStringSwitch(JCSwitch tree) {
3398 List<JCCase> caseList = tree.getCases();
3399 int alternatives = caseList.size();
3401 if (alternatives == 0) { // Strange but legal possibility
3402 return make.at(tree.pos()).Exec(attr.makeNullCheck(tree.getExpression()));
3403 } else {
3404 /*
3405 * The general approach used is to translate a single
3406 * string switch statement into a series of two chained
3407 * switch statements: the first a synthesized statement
3408 * switching on the argument string's hash value and
3409 * computing a string's position in the list of original
3410 * case labels, if any, followed by a second switch on the
3411 * computed integer value. The second switch has the same
3412 * code structure as the original string switch statement
3413 * except that the string case labels are replaced with
3414 * positional integer constants starting at 0.
3415 *
3416 * The first switch statement can be thought of as an
3417 * inlined map from strings to their position in the case
3418 * label list. An alternate implementation would use an
3419 * actual Map for this purpose, as done for enum switches.
3420 *
3421 * With some additional effort, it would be possible to
3422 * use a single switch statement on the hash code of the
3423 * argument, but care would need to be taken to preserve
3424 * the proper control flow in the presence of hash
3425 * collisions and other complications, such as
3426 * fallthroughs. Switch statements with one or two
3427 * alternatives could also be specially translated into
3428 * if-then statements to omit the computation of the hash
3429 * code.
3430 *
3431 * The generated code assumes that the hashing algorithm
3432 * of String is the same in the compilation environment as
3433 * in the environment the code will run in. The string
3434 * hashing algorithm in the SE JDK has been unchanged
3435 * since at least JDK 1.2. Since the algorithm has been
3436 * specified since that release as well, it is very
3437 * unlikely to be changed in the future.
3438 *
3439 * Different hashing algorithms, such as the length of the
3440 * strings or a perfect hashing algorithm over the
3441 * particular set of case labels, could potentially be
3442 * used instead of String.hashCode.
3443 */
3445 ListBuffer<JCStatement> stmtList = new ListBuffer<JCStatement>();
3447 // Map from String case labels to their original position in
3448 // the list of case labels.
3449 Map<String, Integer> caseLabelToPosition =
3450 new LinkedHashMap<String, Integer>(alternatives + 1, 1.0f);
3452 // Map of hash codes to the string case labels having that hashCode.
3453 Map<Integer, Set<String>> hashToString =
3454 new LinkedHashMap<Integer, Set<String>>(alternatives + 1, 1.0f);
3456 int casePosition = 0;
3457 for(JCCase oneCase : caseList) {
3458 JCExpression expression = oneCase.getExpression();
3460 if (expression != null) { // expression for a "default" case is null
3461 expression = TreeInfo.skipParens(expression);
3462 String labelExpr = (String) expression.type.constValue();
3463 Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
3464 Assert.checkNull(mapping);
3465 int hashCode = labelExpr.hashCode();
3467 Set<String> stringSet = hashToString.get(hashCode);
3468 if (stringSet == null) {
3469 stringSet = new LinkedHashSet<String>(1, 1.0f);
3470 stringSet.add(labelExpr);
3471 hashToString.put(hashCode, stringSet);
3472 } else {
3473 boolean added = stringSet.add(labelExpr);
3474 Assert.check(added);
3475 }
3476 }
3477 casePosition++;
3478 }
3480 // Synthesize a switch statement that has the effect of
3481 // mapping from a string to the integer position of that
3482 // string in the list of case labels. This is done by
3483 // switching on the hashCode of the string followed by an
3484 // if-then-else chain comparing the input for equality
3485 // with all the case labels having that hash value.
3487 /*
3488 * s$ = top of stack;
3489 * tmp$ = -1;
3490 * switch($s.hashCode()) {
3491 * case caseLabel.hashCode:
3492 * if (s$.equals("caseLabel_1")
3493 * tmp$ = caseLabelToPosition("caseLabel_1");
3494 * else if (s$.equals("caseLabel_2"))
3495 * tmp$ = caseLabelToPosition("caseLabel_2");
3496 * ...
3497 * break;
3498 * ...
3499 * }
3500 */
3502 VarSymbol dollar_s = new VarSymbol(FINAL|SYNTHETIC,
3503 names.fromString("s" + tree.pos + target.syntheticNameChar()),
3504 syms.stringType,
3505 currentMethodSym);
3506 stmtList.append(make.at(tree.pos()).VarDef(dollar_s, tree.getExpression()).setType(dollar_s.type));
3508 VarSymbol dollar_tmp = new VarSymbol(SYNTHETIC,
3509 names.fromString("tmp" + tree.pos + target.syntheticNameChar()),
3510 syms.intType,
3511 currentMethodSym);
3512 JCVariableDecl dollar_tmp_def =
3513 (JCVariableDecl)make.VarDef(dollar_tmp, make.Literal(INT, -1)).setType(dollar_tmp.type);
3514 dollar_tmp_def.init.type = dollar_tmp.type = syms.intType;
3515 stmtList.append(dollar_tmp_def);
3516 ListBuffer<JCCase> caseBuffer = ListBuffer.lb();
3517 // hashCode will trigger nullcheck on original switch expression
3518 JCMethodInvocation hashCodeCall = makeCall(make.Ident(dollar_s),
3519 names.hashCode,
3520 List.<JCExpression>nil()).setType(syms.intType);
3521 JCSwitch switch1 = make.Switch(hashCodeCall,
3522 caseBuffer.toList());
3523 for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) {
3524 int hashCode = entry.getKey();
3525 Set<String> stringsWithHashCode = entry.getValue();
3526 Assert.check(stringsWithHashCode.size() >= 1);
3528 JCStatement elsepart = null;
3529 for(String caseLabel : stringsWithHashCode ) {
3530 JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s),
3531 names.equals,
3532 List.<JCExpression>of(make.Literal(caseLabel)));
3533 elsepart = make.If(stringEqualsCall,
3534 make.Exec(make.Assign(make.Ident(dollar_tmp),
3535 make.Literal(caseLabelToPosition.get(caseLabel))).
3536 setType(dollar_tmp.type)),
3537 elsepart);
3538 }
3540 ListBuffer<JCStatement> lb = ListBuffer.lb();
3541 JCBreak breakStmt = make.Break(null);
3542 breakStmt.target = switch1;
3543 lb.append(elsepart).append(breakStmt);
3545 caseBuffer.append(make.Case(make.Literal(hashCode), lb.toList()));
3546 }
3548 switch1.cases = caseBuffer.toList();
3549 stmtList.append(switch1);
3551 // Make isomorphic switch tree replacing string labels
3552 // with corresponding integer ones from the label to
3553 // position map.
3555 ListBuffer<JCCase> lb = ListBuffer.lb();
3556 JCSwitch switch2 = make.Switch(make.Ident(dollar_tmp), lb.toList());
3557 for(JCCase oneCase : caseList ) {
3558 // Rewire up old unlabeled break statements to the
3559 // replacement switch being created.
3560 patchTargets(oneCase, tree, switch2);
3562 boolean isDefault = (oneCase.getExpression() == null);
3563 JCExpression caseExpr;
3564 if (isDefault)
3565 caseExpr = null;
3566 else {
3567 caseExpr = make.Literal(caseLabelToPosition.get((String)TreeInfo.skipParens(oneCase.
3568 getExpression()).
3569 type.constValue()));
3570 }
3572 lb.append(make.Case(caseExpr,
3573 oneCase.getStatements()));
3574 }
3576 switch2.cases = lb.toList();
3577 stmtList.append(switch2);
3579 return make.Block(0L, stmtList.toList());
3580 }
3581 }
3583 public void visitNewArray(JCNewArray tree) {
3584 tree.elemtype = translate(tree.elemtype);
3585 for (List<JCExpression> t = tree.dims; t.tail != null; t = t.tail)
3586 if (t.head != null) t.head = translate(t.head, syms.intType);
3587 tree.elems = translate(tree.elems, types.elemtype(tree.type));
3588 result = tree;
3589 }
3591 public void visitSelect(JCFieldAccess tree) {
3592 // need to special case-access of the form C.super.x
3593 // these will always need an access method.
3594 boolean qualifiedSuperAccess =
3595 tree.selected.getTag() == JCTree.SELECT &&
3596 TreeInfo.name(tree.selected) == names._super;
3597 tree.selected = translate(tree.selected);
3598 if (tree.name == names._class)
3599 result = classOf(tree.selected);
3600 else if (tree.name == names._this || tree.name == names._super)
3601 result = makeThis(tree.pos(), tree.selected.type.tsym);
3602 else
3603 result = access(tree.sym, tree, enclOp, qualifiedSuperAccess);
3604 }
3606 public void visitLetExpr(LetExpr tree) {
3607 tree.defs = translateVarDefs(tree.defs);
3608 tree.expr = translate(tree.expr, tree.type);
3609 result = tree;
3610 }
3612 // There ought to be nothing to rewrite here;
3613 // we don't generate code.
3614 public void visitAnnotation(JCAnnotation tree) {
3615 result = tree;
3616 }
3618 @Override
3619 public void visitTry(JCTry tree) {
3620 if (tree.resources.isEmpty()) {
3621 super.visitTry(tree);
3622 } else {
3623 result = makeTwrTry(tree);
3624 }
3625 }
3627 /**************************************************************************
3628 * main method
3629 *************************************************************************/
3631 /** Translate a toplevel class and return a list consisting of
3632 * the translated class and translated versions of all inner classes.
3633 * @param env The attribution environment current at the class definition.
3634 * We need this for resolving some additional symbols.
3635 * @param cdef The tree representing the class definition.
3636 */
3637 public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
3638 ListBuffer<JCTree> translated = null;
3639 try {
3640 attrEnv = env;
3641 this.make = make;
3642 endPositions = env.toplevel.endPositions;
3643 currentClass = null;
3644 currentMethodDef = null;
3645 outermostClassDef = (cdef.getTag() == JCTree.CLASSDEF) ? (JCClassDecl)cdef : null;
3646 outermostMemberDef = null;
3647 this.translated = new ListBuffer<JCTree>();
3648 classdefs = new HashMap<ClassSymbol,JCClassDecl>();
3649 actualSymbols = new HashMap<Symbol,Symbol>();
3650 freevarCache = new HashMap<ClassSymbol,List<VarSymbol>>();
3651 proxies = new Scope(syms.noSymbol);
3652 twrVars = new Scope(syms.noSymbol);
3653 outerThisStack = List.nil();
3654 accessNums = new HashMap<Symbol,Integer>();
3655 accessSyms = new HashMap<Symbol,MethodSymbol[]>();
3656 accessConstrs = new HashMap<Symbol,MethodSymbol>();
3657 accessConstrTags = List.nil();
3658 accessed = new ListBuffer<Symbol>();
3659 translate(cdef, (JCExpression)null);
3660 for (List<Symbol> l = accessed.toList(); l.nonEmpty(); l = l.tail)
3661 makeAccessible(l.head);
3662 for (EnumMapping map : enumSwitchMap.values())
3663 map.translate();
3664 checkConflicts(this.translated.toList());
3665 checkAccessConstructorTags();
3666 translated = this.translated;
3667 } finally {
3668 // note that recursive invocations of this method fail hard
3669 attrEnv = null;
3670 this.make = null;
3671 endPositions = null;
3672 currentClass = null;
3673 currentMethodDef = null;
3674 outermostClassDef = null;
3675 outermostMemberDef = null;
3676 this.translated = null;
3677 classdefs = null;
3678 actualSymbols = null;
3679 freevarCache = null;
3680 proxies = null;
3681 outerThisStack = null;
3682 accessNums = null;
3683 accessSyms = null;
3684 accessConstrs = null;
3685 accessConstrTags = null;
3686 accessed = null;
3687 enumSwitchMap.clear();
3688 }
3689 return translated.toList();
3690 }
3692 //////////////////////////////////////////////////////////////
3693 // The following contributed by Borland for bootstrapping purposes
3694 //////////////////////////////////////////////////////////////
3695 private void addEnumCompatibleMembers(JCClassDecl cdef) {
3696 make_at(null);
3698 // Add the special enum fields
3699 VarSymbol ordinalFieldSym = addEnumOrdinalField(cdef);
3700 VarSymbol nameFieldSym = addEnumNameField(cdef);
3702 // Add the accessor methods for name and ordinal
3703 MethodSymbol ordinalMethodSym = addEnumFieldOrdinalMethod(cdef, ordinalFieldSym);
3704 MethodSymbol nameMethodSym = addEnumFieldNameMethod(cdef, nameFieldSym);
3706 // Add the toString method
3707 addEnumToString(cdef, nameFieldSym);
3709 // Add the compareTo method
3710 addEnumCompareTo(cdef, ordinalFieldSym);
3711 }
3713 private VarSymbol addEnumOrdinalField(JCClassDecl cdef) {
3714 VarSymbol ordinal = new VarSymbol(PRIVATE|FINAL|SYNTHETIC,
3715 names.fromString("$ordinal"),
3716 syms.intType,
3717 cdef.sym);
3718 cdef.sym.members().enter(ordinal);
3719 cdef.defs = cdef.defs.prepend(make.VarDef(ordinal, null));
3720 return ordinal;
3721 }
3723 private VarSymbol addEnumNameField(JCClassDecl cdef) {
3724 VarSymbol name = new VarSymbol(PRIVATE|FINAL|SYNTHETIC,
3725 names.fromString("$name"),
3726 syms.stringType,
3727 cdef.sym);
3728 cdef.sym.members().enter(name);
3729 cdef.defs = cdef.defs.prepend(make.VarDef(name, null));
3730 return name;
3731 }
3733 private MethodSymbol addEnumFieldOrdinalMethod(JCClassDecl cdef, VarSymbol ordinalSymbol) {
3734 // Add the accessor methods for ordinal
3735 Symbol ordinalSym = lookupMethod(cdef.pos(),
3736 names.ordinal,
3737 cdef.type,
3738 List.<Type>nil());
3740 Assert.check(ordinalSym instanceof MethodSymbol);
3742 JCStatement ret = make.Return(make.Ident(ordinalSymbol));
3743 cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)ordinalSym,
3744 make.Block(0L, List.of(ret))));
3746 return (MethodSymbol)ordinalSym;
3747 }
3749 private MethodSymbol addEnumFieldNameMethod(JCClassDecl cdef, VarSymbol nameSymbol) {
3750 // Add the accessor methods for name
3751 Symbol nameSym = lookupMethod(cdef.pos(),
3752 names._name,
3753 cdef.type,
3754 List.<Type>nil());
3756 Assert.check(nameSym instanceof MethodSymbol);
3758 JCStatement ret = make.Return(make.Ident(nameSymbol));
3760 cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)nameSym,
3761 make.Block(0L, List.of(ret))));
3763 return (MethodSymbol)nameSym;
3764 }
3766 private MethodSymbol addEnumToString(JCClassDecl cdef,
3767 VarSymbol nameSymbol) {
3768 Symbol toStringSym = lookupMethod(cdef.pos(),
3769 names.toString,
3770 cdef.type,
3771 List.<Type>nil());
3773 JCTree toStringDecl = null;
3774 if (toStringSym != null)
3775 toStringDecl = TreeInfo.declarationFor(toStringSym, cdef);
3777 if (toStringDecl != null)
3778 return (MethodSymbol)toStringSym;
3780 JCStatement ret = make.Return(make.Ident(nameSymbol));
3782 JCTree resTypeTree = make.Type(syms.stringType);
3784 MethodType toStringType = new MethodType(List.<Type>nil(),
3785 syms.stringType,
3786 List.<Type>nil(),
3787 cdef.sym);
3788 toStringSym = new MethodSymbol(PUBLIC,
3789 names.toString,
3790 toStringType,
3791 cdef.type.tsym);
3792 toStringDecl = make.MethodDef((MethodSymbol)toStringSym,
3793 make.Block(0L, List.of(ret)));
3795 cdef.defs = cdef.defs.prepend(toStringDecl);
3796 cdef.sym.members().enter(toStringSym);
3798 return (MethodSymbol)toStringSym;
3799 }
3801 private MethodSymbol addEnumCompareTo(JCClassDecl cdef, VarSymbol ordinalSymbol) {
3802 Symbol compareToSym = lookupMethod(cdef.pos(),
3803 names.compareTo,
3804 cdef.type,
3805 List.of(cdef.sym.type));
3807 Assert.check(compareToSym instanceof MethodSymbol);
3809 JCMethodDecl compareToDecl = (JCMethodDecl) TreeInfo.declarationFor(compareToSym, cdef);
3811 ListBuffer<JCStatement> blockStatements = new ListBuffer<JCStatement>();
3813 JCModifiers mod1 = make.Modifiers(0L);
3814 Name oName = names.fromString("o");
3815 JCVariableDecl par1 = make.Param(oName, cdef.type, compareToSym);
3817 JCIdent paramId1 = make.Ident(names.java_lang_Object);
3818 paramId1.type = cdef.type;
3819 paramId1.sym = par1.sym;
3821 ((MethodSymbol)compareToSym).params = List.of(par1.sym);
3823 JCIdent par1UsageId = make.Ident(par1.sym);
3824 JCIdent castTargetIdent = make.Ident(cdef.sym);
3825 JCTypeCast cast = make.TypeCast(castTargetIdent, par1UsageId);
3826 cast.setType(castTargetIdent.type);
3828 Name otherName = names.fromString("other");
3830 VarSymbol otherVarSym = new VarSymbol(mod1.flags,
3831 otherName,
3832 cdef.type,
3833 compareToSym);
3834 JCVariableDecl otherVar = make.VarDef(otherVarSym, cast);
3835 blockStatements.append(otherVar);
3837 JCIdent id1 = make.Ident(ordinalSymbol);
3839 JCIdent fLocUsageId = make.Ident(otherVarSym);
3840 JCExpression sel = make.Select(fLocUsageId, ordinalSymbol);
3841 JCBinary bin = makeBinary(JCTree.MINUS, id1, sel);
3842 JCReturn ret = make.Return(bin);
3843 blockStatements.append(ret);
3844 JCMethodDecl compareToMethod = make.MethodDef((MethodSymbol)compareToSym,
3845 make.Block(0L,
3846 blockStatements.toList()));
3847 compareToMethod.params = List.of(par1);
3848 cdef.defs = cdef.defs.append(compareToMethod);
3850 return (MethodSymbol)compareToSym;
3851 }
3852 //////////////////////////////////////////////////////////////
3853 // The above contributed by Borland for bootstrapping purposes
3854 //////////////////////////////////////////////////////////////
3855 }