493 |
493 |
494 // synthetic static final int[] $SwitchMap$Color = new int[Color.values().length]; |
494 // synthetic static final int[] $SwitchMap$Color = new int[Color.values().length]; |
495 MethodSymbol valuesMethod = lookupMethod(pos, |
495 MethodSymbol valuesMethod = lookupMethod(pos, |
496 names.values, |
496 names.values, |
497 forEnum.type, |
497 forEnum.type, |
498 List.<Type>nil()); |
498 List.nil()); |
499 JCExpression size = make // Color.values().length |
499 JCExpression size = make // Color.values().length |
500 .Select(make.App(make.QualIdent(valuesMethod)), |
500 .Select(make.App(make.QualIdent(valuesMethod)), |
501 syms.lengthVar); |
501 syms.lengthVar); |
502 JCExpression mapVarInit = make |
502 JCExpression mapVarInit = make |
503 .NewArray(make.Type(syms.intType), List.of(size), null) |
503 .NewArray(make.Type(syms.intType), List.of(size), null) |
506 // try { $SwitchMap$Color[red.ordinal()] = 1; } catch (java.lang.NoSuchFieldError ex) {} |
506 // try { $SwitchMap$Color[red.ordinal()] = 1; } catch (java.lang.NoSuchFieldError ex) {} |
507 ListBuffer<JCStatement> stmts = new ListBuffer<>(); |
507 ListBuffer<JCStatement> stmts = new ListBuffer<>(); |
508 Symbol ordinalMethod = lookupMethod(pos, |
508 Symbol ordinalMethod = lookupMethod(pos, |
509 names.ordinal, |
509 names.ordinal, |
510 forEnum.type, |
510 forEnum.type, |
511 List.<Type>nil()); |
511 List.nil()); |
512 List<JCCatch> catcher = List.<JCCatch>nil() |
512 List<JCCatch> catcher = List.<JCCatch>nil() |
513 .prepend(make.Catch(make.VarDef(new VarSymbol(PARAMETER, names.ex, |
513 .prepend(make.Catch(make.VarDef(new VarSymbol(PARAMETER, names.ex, |
514 syms.noSuchFieldErrorType, |
514 syms.noSuchFieldErrorType, |
515 syms.noSymbol), |
515 syms.noSymbol), |
516 null), |
516 null), |
517 make.Block(0, List.<JCStatement>nil()))); |
517 make.Block(0, List.nil()))); |
518 for (Map.Entry<VarSymbol,Integer> e : values.entrySet()) { |
518 for (Map.Entry<VarSymbol,Integer> e : values.entrySet()) { |
519 VarSymbol enumerator = e.getKey(); |
519 VarSymbol enumerator = e.getKey(); |
520 Integer mappedValue = e.getValue(); |
520 Integer mappedValue = e.getValue(); |
521 JCExpression assign = make |
521 JCExpression assign = make |
522 .Assign(make.Indexed(mapVar, |
522 .Assign(make.Indexed(mapVar, |
570 */ |
570 */ |
571 JCNewClass makeNewClass(Type ctype, List<JCExpression> args) { |
571 JCNewClass makeNewClass(Type ctype, List<JCExpression> args) { |
572 JCNewClass tree = make.NewClass(null, |
572 JCNewClass tree = make.NewClass(null, |
573 null, make.QualIdent(ctype.tsym), args, null); |
573 null, make.QualIdent(ctype.tsym), args, null); |
574 tree.constructor = rs.resolveConstructor( |
574 tree.constructor = rs.resolveConstructor( |
575 make_pos, attrEnv, ctype, TreeInfo.types(args), List.<Type>nil()); |
575 make_pos, attrEnv, ctype, TreeInfo.types(args), List.nil()); |
576 tree.type = ctype; |
576 tree.type = ctype; |
577 return tree; |
577 return tree; |
578 } |
578 } |
579 |
579 |
580 /** Make an attributed unary expression. |
580 /** Make an attributed unary expression. |
661 chk.putCompiled(c); |
661 chk.putCompiled(c); |
662 |
662 |
663 // Create class definition tree. |
663 // Create class definition tree. |
664 JCClassDecl cdef = make.ClassDef( |
664 JCClassDecl cdef = make.ClassDef( |
665 make.Modifiers(flags), names.empty, |
665 make.Modifiers(flags), names.empty, |
666 List.<JCTypeParameter>nil(), |
666 List.nil(), |
667 null, List.<JCExpression>nil(), List.<JCTree>nil()); |
667 null, List.nil(), List.nil()); |
668 cdef.sym = c; |
668 cdef.sym = c; |
669 cdef.type = c.type; |
669 cdef.type = c.type; |
670 |
670 |
671 // Append class definition tree to owner's definitions. |
671 // Append class definition tree to owner's definitions. |
672 if (addToDefs) odef.defs = odef.defs.prepend(cdef); |
672 if (addToDefs) odef.defs = odef.defs.prepend(cdef); |
755 } |
755 } |
756 |
756 |
757 /** Look up a method in a given scope. |
757 /** Look up a method in a given scope. |
758 */ |
758 */ |
759 private MethodSymbol lookupMethod(DiagnosticPosition pos, Name name, Type qual, List<Type> args) { |
759 private MethodSymbol lookupMethod(DiagnosticPosition pos, Name name, Type qual, List<Type> args) { |
760 return rs.resolveInternalMethod(pos, attrEnv, qual, name, args, List.<Type>nil()); |
760 return rs.resolveInternalMethod(pos, attrEnv, qual, name, args, List.nil()); |
761 } |
761 } |
762 |
762 |
763 /** Look up a constructor. |
763 /** Look up a constructor. |
764 */ |
764 */ |
765 private MethodSymbol lookupConstructor(DiagnosticPosition pos, Type qual, List<Type> args) { |
765 private MethodSymbol lookupConstructor(DiagnosticPosition pos, Type qual, List<Type> args) { |
1356 null); |
1356 null); |
1357 JCIdent callee = make.Ident(names._this); |
1357 JCIdent callee = make.Ident(names._this); |
1358 callee.sym = constr; |
1358 callee.sym = constr; |
1359 callee.type = constr.type; |
1359 callee.type = constr.type; |
1360 md.body = |
1360 md.body = |
1361 make.Block(0, List.<JCStatement>of( |
1361 make.Block(0, List.of( |
1362 make.Call( |
1362 make.Call( |
1363 make.App( |
1363 make.App( |
1364 callee, |
1364 callee, |
1365 make.Idents(md.params.reverse().tail.reverse()))))); |
1365 make.Idents(md.params.reverse().tail.reverse()))))); |
1366 return md; |
1366 return md; |
1611 syms.throwableType, |
1611 syms.throwableType, |
1612 currentMethodSym); |
1612 currentMethodSym); |
1613 JCVariableDecl paramTree = make.VarDef(param, null); |
1613 JCVariableDecl paramTree = make.VarDef(param, null); |
1614 JCStatement assign = make.Assignment(primaryException, make.Ident(param)); |
1614 JCStatement assign = make.Assignment(primaryException, make.Ident(param)); |
1615 JCStatement rethrowStat = make.Throw(make.Ident(param)); |
1615 JCStatement rethrowStat = make.Throw(make.Ident(param)); |
1616 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat)); |
1616 JCBlock catchBlock = make.Block(0L, List.of(assign, rethrowStat)); |
1617 JCCatch catchClause = make.Catch(paramTree, catchBlock); |
1617 JCCatch catchClause = make.Catch(paramTree, catchBlock); |
1618 |
1618 |
1619 int oldPos = make.pos; |
1619 int oldPos = make.pos; |
1620 make.at(TreeInfo.endPos(block)); |
1620 make.at(TreeInfo.endPos(block)); |
1621 JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr, resourceNonNull); |
1621 JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr, resourceNonNull); |
1622 make.at(oldPos); |
1622 make.at(oldPos); |
1623 JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, |
1623 JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, |
1624 finallyCanCompleteNormally, depth + 1), |
1624 finallyCanCompleteNormally, depth + 1), |
1625 List.<JCCatch>of(catchClause), |
1625 List.of(catchClause), |
1626 finallyClause); |
1626 finallyClause); |
1627 outerTry.finallyCanCompleteNormally = finallyCanCompleteNormally; |
1627 outerTry.finallyCanCompleteNormally = finallyCanCompleteNormally; |
1628 stats.add(outerTry); |
1628 stats.add(outerTry); |
1629 JCBlock newBlock = make.Block(0L, stats.toList()); |
1629 JCBlock newBlock = make.Block(0L, stats.toList()); |
1630 return newBlock; |
1630 return newBlock; |
1647 PRIVATE | STATIC | SYNTHETIC, |
1647 PRIVATE | STATIC | SYNTHETIC, |
1648 dollarCloseResource, |
1648 dollarCloseResource, |
1649 new MethodType( |
1649 new MethodType( |
1650 List.of(syms.throwableType, syms.autoCloseableType), |
1650 List.of(syms.throwableType, syms.autoCloseableType), |
1651 syms.voidType, |
1651 syms.voidType, |
1652 List.<Type>nil(), |
1652 List.nil(), |
1653 syms.methodClass), |
1653 syms.methodClass), |
1654 currentClass); |
1654 currentClass); |
1655 enterSynthetic(resource.pos(), closeResource, currentClass.members()); |
1655 enterSynthetic(resource.pos(), closeResource, currentClass.members()); |
1656 |
1656 |
1657 JCMethodDecl md = make.MethodDef(closeResource, null); |
1657 JCMethodDecl md = make.MethodDef(closeResource, null); |
1658 List<JCVariableDecl> params = md.getParameters(); |
1658 List<JCVariableDecl> params = md.getParameters(); |
1659 md.body = make.Block(0, List.<JCStatement>of(makeTwrCloseStatement(params.get(0).sym, |
1659 md.body = make.Block(0, List.of(makeTwrCloseStatement(params.get(0).sym, |
1660 make.Ident(params.get(1))))); |
1660 make.Ident(params.get(1))))); |
1661 |
1661 |
1662 JCClassDecl currentClassDecl = classDef(currentClass); |
1662 JCClassDecl currentClassDecl = classDef(currentClass); |
1663 currentClassDecl.defs = currentClassDecl.defs.prepend(md); |
1663 currentClassDecl.defs = currentClassDecl.defs.prepend(md); |
1664 } |
1664 } |
1665 |
1665 |
1666 JCStatement closeStatement; |
1666 JCStatement closeStatement; |
1667 |
1667 |
1668 if (closeResource != null) { |
1668 if (closeResource != null) { |
1669 //$closeResource(#primaryException, #resource) |
1669 //$closeResource(#primaryException, #resource) |
1670 closeStatement = make.Exec(make.Apply(List.<JCExpression>nil(), |
1670 closeStatement = make.Exec(make.Apply(List.nil(), |
1671 make.Ident(closeResource), |
1671 make.Ident(closeResource), |
1672 List.of(make.Ident(primaryException), |
1672 List.of(make.Ident(primaryException), |
1673 resource) |
1673 resource) |
1674 ).setType(syms.voidType)); |
1674 ).setType(syms.voidType)); |
1675 } else { |
1675 } else { |
1726 syms.throwableType, |
1726 syms.throwableType, |
1727 currentMethodSym); |
1727 currentMethodSym); |
1728 JCStatement addSuppressionStatement = |
1728 JCStatement addSuppressionStatement = |
1729 make.Exec(makeCall(make.Ident(primaryException), |
1729 make.Exec(makeCall(make.Ident(primaryException), |
1730 names.addSuppressed, |
1730 names.addSuppressed, |
1731 List.<JCExpression>of(make.Ident(catchException)))); |
1731 List.of(make.Ident(catchException)))); |
1732 |
1732 |
1733 // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); } |
1733 // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); } |
1734 JCBlock tryBlock = |
1734 JCBlock tryBlock = |
1735 make.Block(0L, List.<JCStatement>of(makeResourceCloseInvocation(resource))); |
1735 make.Block(0L, List.of(makeResourceCloseInvocation(resource))); |
1736 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null); |
1736 JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null); |
1737 JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(addSuppressionStatement)); |
1737 JCBlock catchBlock = make.Block(0L, List.of(addSuppressionStatement)); |
1738 List<JCCatch> catchClauses = List.<JCCatch>of(make.Catch(catchExceptionDecl, catchBlock)); |
1738 List<JCCatch> catchClauses = List.of(make.Catch(catchExceptionDecl, catchBlock)); |
1739 JCTry tryTree = make.Try(tryBlock, catchClauses, null); |
1739 JCTry tryTree = make.Try(tryBlock, catchClauses, null); |
1740 tryTree.finallyCanCompleteNormally = true; |
1740 tryTree.finallyCanCompleteNormally = true; |
1741 |
1741 |
1742 // if (primaryException != null) {try...} else resourceClose; |
1742 // if (primaryException != null) {try...} else resourceClose; |
1743 JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)), |
1743 JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)), |
1754 } |
1754 } |
1755 |
1755 |
1756 // create resource.close() method invocation |
1756 // create resource.close() method invocation |
1757 JCExpression resourceClose = makeCall(resource, |
1757 JCExpression resourceClose = makeCall(resource, |
1758 names.close, |
1758 names.close, |
1759 List.<JCExpression>nil()); |
1759 List.nil()); |
1760 return make.Exec(resourceClose); |
1760 return make.Exec(resourceClose); |
1761 } |
1761 } |
1762 |
1762 |
1763 private JCExpression makeNonNullCheck(JCExpression expression) { |
1763 private JCExpression makeNonNullCheck(JCExpression expression) { |
1764 return makeBinary(NE, expression, makeNull()); |
1764 return makeBinary(NE, expression, makeNull()); |
1929 STATIC | SYNTHETIC, |
1929 STATIC | SYNTHETIC, |
1930 classDollar, |
1930 classDollar, |
1931 new MethodType( |
1931 new MethodType( |
1932 List.of(syms.stringType), |
1932 List.of(syms.stringType), |
1933 types.erasure(syms.classType), |
1933 types.erasure(syms.classType), |
1934 List.<Type>nil(), |
1934 List.nil(), |
1935 syms.methodClass), |
1935 syms.methodClass), |
1936 outerCacheClass); |
1936 outerCacheClass); |
1937 enterSynthetic(pos, classDollarSym, outerCacheClass.members()); |
1937 enterSynthetic(pos, classDollarSym, outerCacheClass.members()); |
1938 |
1938 |
1939 JCMethodDecl md = make.MethodDef(classDollarSym, null); |
1939 JCMethodDecl md = make.MethodDef(classDollarSym, null); |
1940 try { |
1940 try { |
1941 md.body = classDollarSymBody(pos, md); |
1941 md.body = classDollarSymBody(pos, md); |
1942 } catch (CompletionFailure ex) { |
1942 } catch (CompletionFailure ex) { |
1943 md.body = make.Block(0, List.<JCStatement>nil()); |
1943 md.body = make.Block(0, List.nil()); |
1944 chk.completionError(pos, ex); |
1944 chk.completionError(pos, ex); |
1945 } |
1945 } |
1946 JCClassDecl outerCacheClassDef = classDef(outerCacheClass); |
1946 JCClassDecl outerCacheClassDef = classDef(outerCacheClass); |
1947 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(md); |
1947 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(md); |
1948 } |
1948 } |
1969 JCClassDecl outerCacheClassDef = classDef(outerCacheClass); |
1969 JCClassDecl outerCacheClassDef = classDef(outerCacheClass); |
1970 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef); |
1970 outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef); |
1971 |
1971 |
1972 // newcache := "new cache$1[0]" |
1972 // newcache := "new cache$1[0]" |
1973 JCNewArray newcache = make.NewArray(make.Type(outerCacheClass.type), |
1973 JCNewArray newcache = make.NewArray(make.Type(outerCacheClass.type), |
1974 List.<JCExpression>of(make.Literal(INT, 0).setType(syms.intType)), |
1974 List.of(make.Literal(INT, 0).setType(syms.intType)), |
1975 null); |
1975 null); |
1976 newcache.type = new ArrayType(types.erasure(outerCacheClass.type), |
1976 newcache.type = new ArrayType(types.erasure(outerCacheClass.type), |
1977 syms.arrayClass); |
1977 syms.arrayClass); |
1978 |
1978 |
1979 // forNameSym := java.lang.Class.forName( |
1979 // forNameSym := java.lang.Class.forName( |
1990 makeBinary(EQ, make.Ident(clsym), makeNull()), |
1990 makeBinary(EQ, make.Ident(clsym), makeNull()), |
1991 make.Assign(make.Ident(clsym), |
1991 make.Assign(make.Ident(clsym), |
1992 makeCall( |
1992 makeCall( |
1993 makeCall(makeCall(newcache, |
1993 makeCall(makeCall(newcache, |
1994 names.getClass, |
1994 names.getClass, |
1995 List.<JCExpression>nil()), |
1995 List.nil()), |
1996 names.getComponentType, |
1996 names.getComponentType, |
1997 List.<JCExpression>nil()), |
1997 List.nil()), |
1998 names.getClassLoader, |
1998 names.getClassLoader, |
1999 List.<JCExpression>nil())).setType(syms.classLoaderType), |
1999 List.nil())).setType(syms.classLoaderType), |
2000 make.Ident(clsym)).setType(syms.classLoaderType); |
2000 make.Ident(clsym)).setType(syms.classLoaderType); |
2001 |
2001 |
2002 // returnResult := "{ return Class.forName(param1, false, cl$); }" |
2002 // returnResult := "{ return Class.forName(param1, false, cl$); }" |
2003 List<JCExpression> args = List.of(make.Ident(md.params.head.sym), |
2003 List<JCExpression> args = List.of(make.Ident(md.params.head.sym), |
2004 makeLit(syms.booleanType, 0), |
2004 makeLit(syms.booleanType, 0), |
2005 clvalue); |
2005 clvalue); |
2006 returnResult = make.Block(0, List.<JCStatement>of(make.Call(make.App(make.Ident(forNameSym), args)))); |
2006 returnResult = make.Block(0, List.of(make.Call(make.App(make.Ident(forNameSym), args)))); |
2007 |
2007 |
2008 // catchParam := ClassNotFoundException e1 |
2008 // catchParam := ClassNotFoundException e1 |
2009 VarSymbol catchParam = |
2009 VarSymbol catchParam = |
2010 new VarSymbol(SYNTHETIC, make.paramName(1), |
2010 new VarSymbol(SYNTHETIC, make.paramName(1), |
2011 syms.classNotFoundExceptionType, |
2011 syms.classNotFoundExceptionType, |
2013 |
2013 |
2014 JCStatement rethrow; |
2014 JCStatement rethrow; |
2015 // rethrow = "throw new NoClassDefFoundError().initCause(e); |
2015 // rethrow = "throw new NoClassDefFoundError().initCause(e); |
2016 JCExpression throwExpr = |
2016 JCExpression throwExpr = |
2017 makeCall(makeNewClass(syms.noClassDefFoundErrorType, |
2017 makeCall(makeNewClass(syms.noClassDefFoundErrorType, |
2018 List.<JCExpression>nil()), |
2018 List.nil()), |
2019 names.initCause, |
2019 names.initCause, |
2020 List.<JCExpression>of(make.Ident(catchParam))); |
2020 List.of(make.Ident(catchParam))); |
2021 rethrow = make.Throw(throwExpr); |
2021 rethrow = make.Throw(throwExpr); |
2022 |
2022 |
2023 // rethrowStmt := "( $rethrow )" |
2023 // rethrowStmt := "( $rethrow )" |
2024 JCBlock rethrowStmt = make.Block(0, List.of(rethrow)); |
2024 JCBlock rethrowStmt = make.Block(0, List.of(rethrow)); |
2025 |
2025 |
2156 container); |
2156 container); |
2157 enterSynthetic(pos, assertDisabledSym, container.members()); |
2157 enterSynthetic(pos, assertDisabledSym, container.members()); |
2158 Symbol desiredAssertionStatusSym = lookupMethod(pos, |
2158 Symbol desiredAssertionStatusSym = lookupMethod(pos, |
2159 names.desiredAssertionStatus, |
2159 names.desiredAssertionStatus, |
2160 types.erasure(syms.classType), |
2160 types.erasure(syms.classType), |
2161 List.<Type>nil()); |
2161 List.nil()); |
2162 JCClassDecl containerDef = classDef(container); |
2162 JCClassDecl containerDef = classDef(container); |
2163 make_at(containerDef.pos()); |
2163 make_at(containerDef.pos()); |
2164 JCExpression notStatus = makeUnary(NOT, make.App(make.Select( |
2164 JCExpression notStatus = makeUnary(NOT, make.App(make.Select( |
2165 classOfType(types.erasure(outermostClass.type), |
2165 classOfType(types.erasure(outermostClass.type), |
2166 containerDef.pos()), |
2166 containerDef.pos()), |
2173 //need to load the assertions enabled/disabled state while |
2173 //need to load the assertions enabled/disabled state while |
2174 //initializing the interface: |
2174 //initializing the interface: |
2175 JCClassDecl currentClassDef = classDef(currentClass); |
2175 JCClassDecl currentClassDef = classDef(currentClass); |
2176 make_at(currentClassDef.pos()); |
2176 make_at(currentClassDef.pos()); |
2177 JCStatement dummy = make.If(make.QualIdent(assertDisabledSym), make.Skip(), null); |
2177 JCStatement dummy = make.If(make.QualIdent(assertDisabledSym), make.Skip(), null); |
2178 JCBlock clinit = make.Block(STATIC, List.<JCStatement>of(dummy)); |
2178 JCBlock clinit = make.Block(STATIC, List.of(dummy)); |
2179 currentClassDef.defs = currentClassDef.defs.prepend(clinit); |
2179 currentClassDef.defs = currentClassDef.defs.prepend(clinit); |
2180 } |
2180 } |
2181 } |
2181 } |
2182 make_at(pos); |
2182 make_at(pos); |
2183 return makeUnary(NOT, make.Ident(assertDisabledSym)); |
2183 return makeUnary(NOT, make.Ident(assertDisabledSym)); |
2373 public void visitModuleDef(JCModuleDecl tree) { |
2373 public void visitModuleDef(JCModuleDecl tree) { |
2374 ModuleSymbol msym = tree.sym; |
2374 ModuleSymbol msym = tree.sym; |
2375 ClassSymbol c = msym.module_info; |
2375 ClassSymbol c = msym.module_info; |
2376 c.setAttributes(msym); |
2376 c.setAttributes(msym); |
2377 c.flags_field |= Flags.MODULE; |
2377 c.flags_field |= Flags.MODULE; |
2378 createInfoClass(List.<JCAnnotation>nil(), tree.sym.module_info); |
2378 createInfoClass(List.nil(), tree.sym.module_info); |
2379 } |
2379 } |
2380 |
2380 |
2381 private void createInfoClass(List<JCAnnotation> annots, ClassSymbol c) { |
2381 private void createInfoClass(List<JCAnnotation> annots, ClassSymbol c) { |
2382 long flags = Flags.ABSTRACT | Flags.INTERFACE; |
2382 long flags = Flags.ABSTRACT | Flags.INTERFACE; |
2383 JCClassDecl infoClass = |
2383 JCClassDecl infoClass = |
2384 make.ClassDef(make.Modifiers(flags, annots), |
2384 make.ClassDef(make.Modifiers(flags, annots), |
2385 c.name, List.<JCTypeParameter>nil(), |
2385 c.name, List.nil(), |
2386 null, List.<JCExpression>nil(), List.<JCTree>nil()); |
2386 null, List.nil(), List.nil()); |
2387 infoClass.sym = c; |
2387 infoClass.sym = c; |
2388 translated.append(infoClass); |
2388 translated.append(infoClass); |
2389 } |
2389 } |
2390 |
2390 |
2391 public void visitClassDef(JCClassDecl tree) { |
2391 public void visitClassDef(JCClassDecl tree) { |
2473 attrEnv = prevEnv; |
2473 attrEnv = prevEnv; |
2474 currentClass = currentClassPrev; |
2474 currentClass = currentClassPrev; |
2475 currentMethodSym = currentMethodSymPrev; |
2475 currentMethodSym = currentMethodSymPrev; |
2476 |
2476 |
2477 // Return empty block {} as a placeholder for an inner class. |
2477 // Return empty block {} as a placeholder for an inner class. |
2478 result = make_at(tree.pos()).Block(SYNTHETIC, List.<JCStatement>nil()); |
2478 result = make_at(tree.pos()).Block(SYNTHETIC, List.nil()); |
2479 } |
2479 } |
2480 |
2480 |
2481 /** Translate an enum class. */ |
2481 /** Translate an enum class. */ |
2482 private void visitEnumDef(JCClassDecl tree) { |
2482 private void visitEnumDef(JCClassDecl tree) { |
2483 make_at(tree.pos()); |
2483 make_at(tree.pos()); |
2516 VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC, |
2516 VarSymbol valuesVar = new VarSymbol(PRIVATE|FINAL|STATIC|SYNTHETIC, |
2517 valuesName, |
2517 valuesName, |
2518 arrayType, |
2518 arrayType, |
2519 tree.type.tsym); |
2519 tree.type.tsym); |
2520 JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)), |
2520 JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)), |
2521 List.<JCExpression>nil(), |
2521 List.nil(), |
2522 values.toList()); |
2522 values.toList()); |
2523 newArray.type = arrayType; |
2523 newArray.type = arrayType; |
2524 enumDefs.append(make.VarDef(valuesVar, newArray)); |
2524 enumDefs.append(make.VarDef(valuesVar, newArray)); |
2525 tree.sym.members().enter(valuesVar); |
2525 tree.sym.members().enter(valuesVar); |
2526 |
2526 |
2527 Symbol valuesSym = lookupMethod(tree.pos(), names.values, |
2527 Symbol valuesSym = lookupMethod(tree.pos(), names.values, |
2528 tree.type, List.<Type>nil()); |
2528 tree.type, List.nil()); |
2529 List<JCStatement> valuesBody; |
2529 List<JCStatement> valuesBody; |
2530 if (useClone()) { |
2530 if (useClone()) { |
2531 // return (T[]) $VALUES.clone(); |
2531 // return (T[]) $VALUES.clone(); |
2532 JCTypeCast valuesResult = |
2532 JCTypeCast valuesResult = |
2533 make.TypeCast(valuesSym.type.getReturnType(), |
2533 make.TypeCast(valuesSym.type.getReturnType(), |
2534 make.App(make.Select(make.Ident(valuesVar), |
2534 make.App(make.Select(make.Ident(valuesVar), |
2535 syms.arrayCloneMethod))); |
2535 syms.arrayCloneMethod))); |
2536 valuesBody = List.<JCStatement>of(make.Return(valuesResult)); |
2536 valuesBody = List.of(make.Return(valuesResult)); |
2537 } else { |
2537 } else { |
2538 // template: T[] $result = new T[$values.length]; |
2538 // template: T[] $result = new T[$values.length]; |
2539 Name resultName = names.fromString(target.syntheticNameChar() + "result"); |
2539 Name resultName = names.fromString(target.syntheticNameChar() + "result"); |
2540 while (tree.sym.members().findFirst(resultName) != null) // avoid name clash |
2540 while (tree.sym.members().findFirst(resultName) != null) // avoid name clash |
2541 resultName = names.fromString(resultName + "" + target.syntheticNameChar()); |
2541 resultName = names.fromString(resultName + "" + target.syntheticNameChar()); |
2552 // template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length); |
2552 // template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length); |
2553 if (systemArraycopyMethod == null) { |
2553 if (systemArraycopyMethod == null) { |
2554 systemArraycopyMethod = |
2554 systemArraycopyMethod = |
2555 new MethodSymbol(PUBLIC | STATIC, |
2555 new MethodSymbol(PUBLIC | STATIC, |
2556 names.fromString("arraycopy"), |
2556 names.fromString("arraycopy"), |
2557 new MethodType(List.<Type>of(syms.objectType, |
2557 new MethodType(List.of(syms.objectType, |
2558 syms.intType, |
2558 syms.intType, |
2559 syms.objectType, |
2559 syms.objectType, |
2560 syms.intType, |
2560 syms.intType, |
2561 syms.intType), |
2561 syms.intType), |
2562 syms.voidType, |
2562 syms.voidType, |
2563 List.<Type>nil(), |
2563 List.nil(), |
2564 syms.methodClass), |
2564 syms.methodClass), |
2565 syms.systemType.tsym); |
2565 syms.systemType.tsym); |
2566 } |
2566 } |
2567 JCStatement copy = |
2567 JCStatement copy = |
2568 make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym), |
2568 make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym), |
2571 make.Ident(resultVar), make.Literal(0), |
2571 make.Ident(resultVar), make.Literal(0), |
2572 make.Select(make.Ident(valuesVar), syms.lengthVar)))); |
2572 make.Select(make.Ident(valuesVar), syms.lengthVar)))); |
2573 |
2573 |
2574 // template: return $result; |
2574 // template: return $result; |
2575 JCStatement ret = make.Return(make.Ident(resultVar)); |
2575 JCStatement ret = make.Return(make.Ident(resultVar)); |
2576 valuesBody = List.<JCStatement>of(decl, copy, ret); |
2576 valuesBody = List.of(decl, copy, ret); |
2577 } |
2577 } |
2578 |
2578 |
2579 JCMethodDecl valuesDef = |
2579 JCMethodDecl valuesDef = |
2580 make.MethodDef((MethodSymbol)valuesSym, make.Block(0, valuesBody)); |
2580 make.MethodDef((MethodSymbol)valuesSym, make.Block(0, valuesBody)); |
2581 |
2581 |
2952 DiagnosticPosition detailPos = (tree.detail == null) ? tree.pos() : tree.detail.pos(); |
2952 DiagnosticPosition detailPos = (tree.detail == null) ? tree.pos() : tree.detail.pos(); |
2953 tree.cond = translate(tree.cond, syms.booleanType); |
2953 tree.cond = translate(tree.cond, syms.booleanType); |
2954 if (!tree.cond.type.isTrue()) { |
2954 if (!tree.cond.type.isTrue()) { |
2955 JCExpression cond = assertFlagTest(tree.pos()); |
2955 JCExpression cond = assertFlagTest(tree.pos()); |
2956 List<JCExpression> exnArgs = (tree.detail == null) ? |
2956 List<JCExpression> exnArgs = (tree.detail == null) ? |
2957 List.<JCExpression>nil() : List.of(translate(tree.detail)); |
2957 List.nil() : List.of(translate(tree.detail)); |
2958 if (!tree.cond.type.isFalse()) { |
2958 if (!tree.cond.type.isFalse()) { |
2959 cond = makeBinary |
2959 cond = makeBinary |
2960 (AND, |
2960 (AND, |
2961 cond, |
2961 cond, |
2962 makeUnary(NOT, tree.cond)); |
2962 makeUnary(NOT, tree.cond)); |
3065 JCExpression arg = translate(args.head, varargsElement); |
3065 JCExpression arg = translate(args.head, varargsElement); |
3066 elems.append(arg); |
3066 elems.append(arg); |
3067 args = args.tail; |
3067 args = args.tail; |
3068 } |
3068 } |
3069 JCNewArray boxedArgs = make.NewArray(make.Type(varargsElement), |
3069 JCNewArray boxedArgs = make.NewArray(make.Type(varargsElement), |
3070 List.<JCExpression>nil(), |
3070 List.nil(), |
3071 elems.toList()); |
3071 elems.toList()); |
3072 boxedArgs.type = new ArrayType(varargsElement, syms.arrayClass); |
3072 boxedArgs.type = new ArrayType(varargsElement, syms.arrayClass); |
3073 result.append(boxedArgs); |
3073 result.append(boxedArgs); |
3074 } else { |
3074 } else { |
3075 if (args.length() != 1) throw new AssertionError(args); |
3075 if (args.length() != 1) throw new AssertionError(args); |
3134 } |
3134 } |
3135 make_at(tree.pos()); |
3135 make_at(tree.pos()); |
3136 Symbol valueSym = lookupMethod(tree.pos(), |
3136 Symbol valueSym = lookupMethod(tree.pos(), |
3137 unboxedType.tsym.name.append(names.Value), // x.intValue() |
3137 unboxedType.tsym.name.append(names.Value), // x.intValue() |
3138 tree.type, |
3138 tree.type, |
3139 List.<Type>nil()); |
3139 List.nil()); |
3140 return make.App(make.Select(tree, valueSym)); |
3140 return make.App(make.Select(tree, valueSym)); |
3141 } |
3141 } |
3142 |
3142 |
3143 /** Visitor method for parenthesized expressions. |
3143 /** Visitor method for parenthesized expressions. |
3144 * If the subexpression has changed, omit the parens. |
3144 * If the subexpression has changed, omit the parens. |
3483 if (eType.isCompound()) |
3483 if (eType.isCompound()) |
3484 tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr); |
3484 tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr); |
3485 Symbol iterator = lookupMethod(tree.expr.pos(), |
3485 Symbol iterator = lookupMethod(tree.expr.pos(), |
3486 names.iterator, |
3486 names.iterator, |
3487 eType, |
3487 eType, |
3488 List.<Type>nil()); |
3488 List.nil()); |
3489 VarSymbol itvar = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()), |
3489 VarSymbol itvar = new VarSymbol(SYNTHETIC, names.fromString("i" + target.syntheticNameChar()), |
3490 types.erasure(types.asSuper(iterator.type.getReturnType(), syms.iteratorType.tsym)), |
3490 types.erasure(types.asSuper(iterator.type.getReturnType(), syms.iteratorType.tsym)), |
3491 currentMethodSym); |
3491 currentMethodSym); |
3492 |
3492 |
3493 JCStatement init = make. |
3493 JCStatement init = make. |
3495 .setType(types.erasure(iterator.type)))); |
3495 .setType(types.erasure(iterator.type)))); |
3496 |
3496 |
3497 Symbol hasNext = lookupMethod(tree.expr.pos(), |
3497 Symbol hasNext = lookupMethod(tree.expr.pos(), |
3498 names.hasNext, |
3498 names.hasNext, |
3499 itvar.type, |
3499 itvar.type, |
3500 List.<Type>nil()); |
3500 List.nil()); |
3501 JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext)); |
3501 JCMethodInvocation cond = make.App(make.Select(make.Ident(itvar), hasNext)); |
3502 Symbol next = lookupMethod(tree.expr.pos(), |
3502 Symbol next = lookupMethod(tree.expr.pos(), |
3503 names.next, |
3503 names.next, |
3504 itvar.type, |
3504 itvar.type, |
3505 List.<Type>nil()); |
3505 List.nil()); |
3506 JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next)); |
3506 JCExpression vardefinit = make.App(make.Select(make.Ident(itvar), next)); |
3507 if (tree.var.type.isPrimitive()) |
3507 if (tree.var.type.isPrimitive()) |
3508 vardefinit = make.TypeCast(types.cvarUpperBound(iteratorTarget), vardefinit); |
3508 vardefinit = make.TypeCast(types.cvarUpperBound(iteratorTarget), vardefinit); |
3509 else |
3509 else |
3510 vardefinit = make.TypeCast(tree.var.type, vardefinit); |
3510 vardefinit = make.TypeCast(tree.var.type, vardefinit); |
3516 JCBlock body = make.Block(0, List.of(indexDef, tree.body)); |
3516 JCBlock body = make.Block(0, List.of(indexDef, tree.body)); |
3517 body.endpos = TreeInfo.endPos(tree.body); |
3517 body.endpos = TreeInfo.endPos(tree.body); |
3518 result = translate(make. |
3518 result = translate(make. |
3519 ForLoop(List.of(init), |
3519 ForLoop(List.of(init), |
3520 cond, |
3520 cond, |
3521 List.<JCExpressionStatement>nil(), |
3521 List.nil(), |
3522 body)); |
3522 body)); |
3523 patchTargets(body, tree, result); |
3523 patchTargets(body, tree, result); |
3524 } |
3524 } |
3525 |
3525 |
3526 public void visitVarDef(JCVariableDecl tree) { |
3526 public void visitVarDef(JCVariableDecl tree) { |
3605 EnumMapping map = mapForEnum(tree.pos(), enumSym); |
3605 EnumMapping map = mapForEnum(tree.pos(), enumSym); |
3606 make_at(tree.pos()); |
3606 make_at(tree.pos()); |
3607 Symbol ordinalMethod = lookupMethod(tree.pos(), |
3607 Symbol ordinalMethod = lookupMethod(tree.pos(), |
3608 names.ordinal, |
3608 names.ordinal, |
3609 tree.selector.type, |
3609 tree.selector.type, |
3610 List.<Type>nil()); |
3610 List.nil()); |
3611 JCArrayAccess selector = make.Indexed(map.mapVar, |
3611 JCArrayAccess selector = make.Indexed(map.mapVar, |
3612 make.App(make.Select(tree.selector, |
3612 make.App(make.Select(tree.selector, |
3613 ordinalMethod))); |
3613 ordinalMethod))); |
3614 ListBuffer<JCCase> cases = new ListBuffer<>(); |
3614 ListBuffer<JCCase> cases = new ListBuffer<>(); |
3615 for (JCCase c : tree.cases) { |
3615 for (JCCase c : tree.cases) { |
3744 stmtList.append(dollar_tmp_def); |
3744 stmtList.append(dollar_tmp_def); |
3745 ListBuffer<JCCase> caseBuffer = new ListBuffer<>(); |
3745 ListBuffer<JCCase> caseBuffer = new ListBuffer<>(); |
3746 // hashCode will trigger nullcheck on original switch expression |
3746 // hashCode will trigger nullcheck on original switch expression |
3747 JCMethodInvocation hashCodeCall = makeCall(make.Ident(dollar_s), |
3747 JCMethodInvocation hashCodeCall = makeCall(make.Ident(dollar_s), |
3748 names.hashCode, |
3748 names.hashCode, |
3749 List.<JCExpression>nil()).setType(syms.intType); |
3749 List.nil()).setType(syms.intType); |
3750 JCSwitch switch1 = make.Switch(hashCodeCall, |
3750 JCSwitch switch1 = make.Switch(hashCodeCall, |
3751 caseBuffer.toList()); |
3751 caseBuffer.toList()); |
3752 for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) { |
3752 for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) { |
3753 int hashCode = entry.getKey(); |
3753 int hashCode = entry.getKey(); |
3754 Set<String> stringsWithHashCode = entry.getValue(); |
3754 Set<String> stringsWithHashCode = entry.getValue(); |
3756 |
3756 |
3757 JCStatement elsepart = null; |
3757 JCStatement elsepart = null; |
3758 for(String caseLabel : stringsWithHashCode ) { |
3758 for(String caseLabel : stringsWithHashCode ) { |
3759 JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s), |
3759 JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s), |
3760 names.equals, |
3760 names.equals, |
3761 List.<JCExpression>of(make.Literal(caseLabel))); |
3761 List.of(make.Literal(caseLabel))); |
3762 elsepart = make.If(stringEqualsCall, |
3762 elsepart = make.If(stringEqualsCall, |
3763 make.Exec(make.Assign(make.Ident(dollar_tmp), |
3763 make.Exec(make.Assign(make.Ident(dollar_tmp), |
3764 make.Literal(caseLabelToPosition.get(caseLabel))). |
3764 make.Literal(caseLabelToPosition.get(caseLabel))). |
3765 setType(dollar_tmp.type)), |
3765 setType(dollar_tmp.type)), |
3766 elsepart); |
3766 elsepart); |