src/share/classes/com/sun/tools/javac/jvm/Gen.java

Print this page




 540             c.members().enter(clinit);
 541             List<JCStatement> clinitStats = clinitCode.toList();
 542             JCBlock block = make.at(clinitStats.head.pos()).Block(0, clinitStats);
 543             block.endpos = TreeInfo.endPos(clinitStats.last());
 544             methodDefs.append(make.MethodDef(clinit, block));
 545 
 546             if (!clinitTAs.isEmpty())
 547                 clinit.appendUniqueTypeAttributes(clinitTAs.toList());
 548             if (!c.getClassInitTypeAttributes().isEmpty())
 549                 clinit.appendUniqueTypeAttributes(c.getClassInitTypeAttributes());
 550         }
 551         // Return all method definitions.
 552         return methodDefs.toList();
 553     }
 554 
 555     private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
 556         List<TypeCompound> tas = sym.getRawTypeAttributes();
 557         ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<>();
 558         ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<>();
 559         for (TypeCompound ta : tas) {
 560             Assert.check(ta.getPosition().type != TargetType.UNKNOWN);
 561             if (ta.getPosition().type == TargetType.FIELD) {
 562                 fieldTAs.add(ta);
 563             } else {
 564                 nonfieldTAs.add(ta);
 565             }
 566         }
 567         sym.setTypeAttributes(fieldTAs.toList());
 568         return nonfieldTAs.toList();
 569     }
 570 
 571     /** Check a constant value and report if it is a string that is
 572      *  too large.
 573      */
 574     private void checkStringConstant(DiagnosticPosition pos, Object constValue) {
 575         if (nerrs != 0 || // only complain about a long string once
 576             constValue == null ||
 577             !(constValue instanceof String) ||
 578             ((String)constValue).length() < Pool.MAX_STRING_LENGTH)
 579             return;
 580         log.error(pos, "limit.string");


1915         }
1916         if (elseChain != null) {
1917             code.resolve(elseChain);
1918             int startpc = genCrt ? code.curCP() : 0;
1919             code.statBegin(tree.falsepart.pos);
1920             genExpr(tree.falsepart, pt).load();
1921             code.state.forceStackTop(tree.type);
1922             if (genCrt) code.crt.put(tree.falsepart, CRT_FLOW_TARGET,
1923                                      startpc, code.curCP());
1924         }
1925         code.resolve(thenExit);
1926         result = items.makeStackItem(pt);
1927     }
1928 
1929     private void setTypeAnnotationPositions(int treePos) {
1930         MethodSymbol meth = code.meth;
1931         boolean initOrClinit = code.meth.getKind() == javax.lang.model.element.ElementKind.CONSTRUCTOR
1932                 || code.meth.getKind() == javax.lang.model.element.ElementKind.STATIC_INIT;
1933 
1934         for (Attribute.TypeCompound ta : meth.getRawTypeAttributes()) {
1935             if (ta.hasUnknownPosition())
1936                 ta.tryFixPosition();
1937 
1938             if (ta.position.matchesPos(treePos))
1939                 ta.position.updatePosOffset(code.cp);
1940         }
1941 
1942         if (!initOrClinit)
1943             return;
1944 
1945         for (Attribute.TypeCompound ta : meth.owner.getRawTypeAttributes()) {
1946             if (ta.hasUnknownPosition())
1947                 ta.tryFixPosition();
1948 
1949             if (ta.position.matchesPos(treePos))
1950                 ta.position.updatePosOffset(code.cp);
1951         }
1952 
1953         ClassSymbol clazz = meth.enclClass();
1954         for (Symbol s : new com.sun.tools.javac.model.FilteredMemberList(clazz.members())) {
1955             if (!s.getKind().isField())
1956                 continue;
1957 
1958             for (Attribute.TypeCompound ta : s.getRawTypeAttributes()) {
1959                 if (ta.hasUnknownPosition())
1960                     ta.tryFixPosition();
1961 
1962                 if (ta.position.matchesPos(treePos))
1963                     ta.position.updatePosOffset(code.cp);
1964             }
1965         }
1966     }
1967 
1968     public void visitNewClass(JCNewClass tree) {
1969         // Enclosing instances or anonymous classes should have been eliminated
1970         // by now.
1971         Assert.check(tree.encl == null && tree.def == null);
1972         setTypeAnnotationPositions(tree.pos);
1973 
1974         code.emitop2(new_, makeRef(tree.pos(), tree.type));
1975         code.emitop0(dup);
1976 
1977         // Generate code for all arguments, where the expected types are
1978         // the parameters of the constructor's external type (that is,
1979         // any implicit outer instance appears as first parameter).
1980         genArgs(tree.args, tree.constructor.externalType(types).getParameterTypes());
1981 
1982         items.makeMemberItem(tree.constructor, true).invoke();


2314         }
2315 
2316     public void visitTypeCast(JCTypeCast tree) {
2317         setTypeAnnotationPositions(tree.pos);
2318         result = genExpr(tree.expr, tree.clazz.type).load();
2319         // Additional code is only needed if we cast to a reference type
2320         // which is not statically a supertype of the expression's type.
2321         // For basic types, the coerce(...) in genExpr(...) will do
2322         // the conversion.
2323         if (!tree.clazz.type.isPrimitive() &&
2324             types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) {
2325             code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type));
2326         }
2327     }
2328 
2329     public void visitWildcard(JCWildcard tree) {
2330         throw new AssertionError(this.getClass().getName());
2331     }
2332 
2333     public void visitTypeTest(JCInstanceOf tree) {
2334         setTypeAnnotationPositions(tree.pos);
2335         genExpr(tree.expr, tree.expr.type).load();

2336         code.emitop2(instanceof_, makeRef(tree.pos(), tree.clazz.type));
2337         result = items.makeStackItem(syms.booleanType);
2338     }
2339 
2340     public void visitIndexed(JCArrayAccess tree) {
2341         genExpr(tree.indexed, tree.indexed.type).load();
2342         genExpr(tree.index, syms.intType).load();
2343         result = items.makeIndexedItem(tree.type);
2344     }
2345 
2346     public void visitIdent(JCIdent tree) {
2347         Symbol sym = tree.sym;
2348         if (tree.name == names._this || tree.name == names._super) {
2349             Item res = tree.name == names._this
2350                 ? items.makeThisItem()
2351                 : items.makeSuperItem();
2352             if (sym.kind == MTH) {
2353                 // Generate code to address the constructor.
2354                 res.load();
2355                 res = items.makeMemberItem(sym, true);




 540             c.members().enter(clinit);
 541             List<JCStatement> clinitStats = clinitCode.toList();
 542             JCBlock block = make.at(clinitStats.head.pos()).Block(0, clinitStats);
 543             block.endpos = TreeInfo.endPos(clinitStats.last());
 544             methodDefs.append(make.MethodDef(clinit, block));
 545 
 546             if (!clinitTAs.isEmpty())
 547                 clinit.appendUniqueTypeAttributes(clinitTAs.toList());
 548             if (!c.getClassInitTypeAttributes().isEmpty())
 549                 clinit.appendUniqueTypeAttributes(c.getClassInitTypeAttributes());
 550         }
 551         // Return all method definitions.
 552         return methodDefs.toList();
 553     }
 554 
 555     private List<Attribute.TypeCompound> getAndRemoveNonFieldTAs(VarSymbol sym) {
 556         List<TypeCompound> tas = sym.getRawTypeAttributes();
 557         ListBuffer<Attribute.TypeCompound> fieldTAs = new ListBuffer<>();
 558         ListBuffer<Attribute.TypeCompound> nonfieldTAs = new ListBuffer<>();
 559         for (TypeCompound ta : tas) {

 560             if (ta.getPosition().type == TargetType.FIELD) {
 561                 fieldTAs.add(ta);
 562             } else {
 563                 nonfieldTAs.add(ta);
 564             }
 565         }
 566         sym.setTypeAttributes(fieldTAs.toList());
 567         return nonfieldTAs.toList();
 568     }
 569 
 570     /** Check a constant value and report if it is a string that is
 571      *  too large.
 572      */
 573     private void checkStringConstant(DiagnosticPosition pos, Object constValue) {
 574         if (nerrs != 0 || // only complain about a long string once
 575             constValue == null ||
 576             !(constValue instanceof String) ||
 577             ((String)constValue).length() < Pool.MAX_STRING_LENGTH)
 578             return;
 579         log.error(pos, "limit.string");


1914         }
1915         if (elseChain != null) {
1916             code.resolve(elseChain);
1917             int startpc = genCrt ? code.curCP() : 0;
1918             code.statBegin(tree.falsepart.pos);
1919             genExpr(tree.falsepart, pt).load();
1920             code.state.forceStackTop(tree.type);
1921             if (genCrt) code.crt.put(tree.falsepart, CRT_FLOW_TARGET,
1922                                      startpc, code.curCP());
1923         }
1924         code.resolve(thenExit);
1925         result = items.makeStackItem(pt);
1926     }
1927 
1928     private void setTypeAnnotationPositions(int treePos) {
1929         MethodSymbol meth = code.meth;
1930         boolean initOrClinit = code.meth.getKind() == javax.lang.model.element.ElementKind.CONSTRUCTOR
1931                 || code.meth.getKind() == javax.lang.model.element.ElementKind.STATIC_INIT;
1932 
1933         for (Attribute.TypeCompound ta : meth.getRawTypeAttributes()) {
1934             if (ta.position != null && ta.position.matchesPos(treePos))



1935                 ta.position.updatePosOffset(code.cp);
1936         }
1937 
1938         if (!initOrClinit)
1939             return;
1940 
1941         for (Attribute.TypeCompound ta : meth.owner.getRawTypeAttributes()) {
1942             if (ta.position != null && ta.position.matchesPos(treePos))



1943                 ta.position.updatePosOffset(code.cp);
1944         }
1945 
1946         ClassSymbol clazz = meth.enclClass();
1947         for (Symbol s : new com.sun.tools.javac.model.FilteredMemberList(clazz.members())) {
1948             if (!s.getKind().isField())
1949                 continue;
1950 
1951             for (Attribute.TypeCompound ta : s.getRawTypeAttributes()) {
1952                 if (ta.position != null && ta.position.matchesPos(treePos))



1953                     ta.position.updatePosOffset(code.cp);
1954             }
1955         }
1956     }
1957 
1958     public void visitNewClass(JCNewClass tree) {
1959         // Enclosing instances or anonymous classes should have been eliminated
1960         // by now.
1961         Assert.check(tree.encl == null && tree.def == null);
1962         setTypeAnnotationPositions(tree.pos);
1963 
1964         code.emitop2(new_, makeRef(tree.pos(), tree.type));
1965         code.emitop0(dup);
1966 
1967         // Generate code for all arguments, where the expected types are
1968         // the parameters of the constructor's external type (that is,
1969         // any implicit outer instance appears as first parameter).
1970         genArgs(tree.args, tree.constructor.externalType(types).getParameterTypes());
1971 
1972         items.makeMemberItem(tree.constructor, true).invoke();


2304         }
2305 
2306     public void visitTypeCast(JCTypeCast tree) {
2307         setTypeAnnotationPositions(tree.pos);
2308         result = genExpr(tree.expr, tree.clazz.type).load();
2309         // Additional code is only needed if we cast to a reference type
2310         // which is not statically a supertype of the expression's type.
2311         // For basic types, the coerce(...) in genExpr(...) will do
2312         // the conversion.
2313         if (!tree.clazz.type.isPrimitive() &&
2314             types.asSuper(tree.expr.type, tree.clazz.type.tsym) == null) {
2315             code.emitop2(checkcast, makeRef(tree.pos(), tree.clazz.type));
2316         }
2317     }
2318 
2319     public void visitWildcard(JCWildcard tree) {
2320         throw new AssertionError(this.getClass().getName());
2321     }
2322 
2323     public void visitTypeTest(JCInstanceOf tree) {

2324         genExpr(tree.expr, tree.expr.type).load();
2325         setTypeAnnotationPositions(tree.pos);
2326         code.emitop2(instanceof_, makeRef(tree.pos(), tree.clazz.type));
2327         result = items.makeStackItem(syms.booleanType);
2328     }
2329 
2330     public void visitIndexed(JCArrayAccess tree) {
2331         genExpr(tree.indexed, tree.indexed.type).load();
2332         genExpr(tree.index, syms.intType).load();
2333         result = items.makeIndexedItem(tree.type);
2334     }
2335 
2336     public void visitIdent(JCIdent tree) {
2337         Symbol sym = tree.sym;
2338         if (tree.name == names._this || tree.name == names._super) {
2339             Item res = tree.name == names._this
2340                 ? items.makeThisItem()
2341                 : items.makeSuperItem();
2342             if (sym.kind == MTH) {
2343                 // Generate code to address the constructor.
2344                 res.load();
2345                 res = items.makeMemberItem(sym, true);