constructordecl.java
来自「JDK1.4编译器后端」· Java 代码 · 共 1,419 行 · 第 1/4 页
JAVA
1,419 行
package AST;
import java.util.HashSet;import java.util.LinkedHashSet;import java.io.FileNotFoundException;import java.io.File;import java.util.*;import beaver.*;import java.util.ArrayList;import java.util.zip.*;import java.io.*;public class ConstructorDecl extends BodyDecl implements Cloneable, ExceptionHolder {
public void flushCache() { super.flushCache(); accessibleFrom_TypeDecl_values = null; isDAafter_Variable_values = null; isDUafter_Variable_values = null; throwsException_TypeDecl_values = null; name_computed = false; name_value = null; signature_computed = false; signature_value = null; sameSignature_ConstructorDecl_values = null; moreSpecificThan_ConstructorDecl_values = null; parameterDeclaration_String_values = null; circularThisInvocation_ConstructorDecl_values = null; attributes_computed = false; attributes_value = null; descName_computed = false; descName_value = null; bytecodes_ConstantPool_values = null; flags_computed = false; localNumOfFirstParameter_computed = false; offsetFirstEnclosingVariable_computed = false; handlesException_TypeDecl_values = null; } @SuppressWarnings({"unchecked", "cast"}) public ConstructorDecl clone() throws CloneNotSupportedException { ConstructorDecl node = (ConstructorDecl)super.clone(); node.accessibleFrom_TypeDecl_values = null; node.isDAafter_Variable_values = null; node.isDUafter_Variable_values = null; node.throwsException_TypeDecl_values = null; node.name_computed = false; node.name_value = null; node.signature_computed = false; node.signature_value = null; node.sameSignature_ConstructorDecl_values = null; node.moreSpecificThan_ConstructorDecl_values = null; node.parameterDeclaration_String_values = null; node.circularThisInvocation_ConstructorDecl_values = null; node.attributes_computed = false; node.attributes_value = null; node.descName_computed = false; node.descName_value = null; node.bytecodes_ConstantPool_values = null; node.flags_computed = false; node.localNumOfFirstParameter_computed = false; node.offsetFirstEnclosingVariable_computed = false; node.handlesException_TypeDecl_values = null; node.in$Circle(false); node.is$Final(false); return node; } @SuppressWarnings({"unchecked", "cast"}) public ConstructorDecl copy() { try { ConstructorDecl node = (ConstructorDecl)clone(); if(children != null) node.children = (ASTNode[])children.clone(); return node; } catch (CloneNotSupportedException e) { } System.err.println("Error: Could not clone node of type " + getClass().getName() + "!"); return null; } @SuppressWarnings({"unchecked", "cast"}) public ConstructorDecl fullCopy() { ConstructorDecl res = (ConstructorDecl)copy(); for(int i = 0; i < getNumChildNoTransform(); i++) { ASTNode node = getChildNoTransform(i); if(node != null) node = node.fullCopy(); res.setChild(node, i); } return res; } // Declared in LookupConstructor.jrag at line 163 public boolean applicable(List argList) { if(getNumParameter() != argList.getNumChild()) return false; for(int i = 0; i < getNumParameter(); i++) { TypeDecl arg = ((Expr)argList.getChild(i)).type(); TypeDecl parameter = getParameter(i).type(); if(!arg.instanceOf(parameter)) { return false; } } return true; } // Declared in Modifiers.jrag at line 108 public void checkModifiers() { super.checkModifiers(); } // Declared in NameCheck.jrag at line 68 public void nameCheck() { super.nameCheck(); // 8.8 if(!hostType().name().equals(name())) error("constructor " + name() +" does not have the same name as the simple name of the host class " + hostType().name()); // 8.8.2 if(hostType().lookupConstructor(this) != this) error("constructor with signature " + signature() + " is multiply declared in type " + hostType().typeName()); if(circularThisInvocation(this)) error("The constructor " + signature() + " may not directly or indirectly invoke itself"); } // Declared in PrettyPrint.jadd at line 135 public void toString(StringBuffer s) { s.append(indent()); getModifiers().toString(s); s.append(name() + "("); if(getNumParameter() > 0) { getParameter(0).toString(s); for(int i = 1; i < getNumParameter(); i++) { s.append(", "); getParameter(i).toString(s); } } s.append(")"); if(getNumException() > 0) { s.append(" throws "); getException(0).toString(s); for(int i = 1; i < getNumException(); i++) { s.append(", "); getException(i).toString(s); } } s.append(" {\n"); indent++; if(hasConstructorInvocation()) { s.append(indent()); getConstructorInvocation().toString(s); } for(int i = 0; i < getBlock().getNumStmt(); i++) { s.append(indent()); getBlock().getStmt(i).toString(s); } indent--; s.append(indent()); s.append("}\n"); } // Declared in TypeCheck.jrag at line 424 public void typeCheck() { // 8.8.4 (8.4.4) TypeDecl exceptionType = typeThrowable(); for(int i = 0; i < getNumException(); i++) { TypeDecl typeDecl = getException(i).type(); if(!typeDecl.instanceOf(exceptionType)) error(signature() + " throws non throwable type " + typeDecl.fullName()); } } // Declared in CodeGeneration.jrag at line 917 public void emitInvokeConstructor(CodeGeneration gen) { int size = -1; for(int i = 0; i < getNumParameter(); i++) size -= getParameter(i).type().variableSize(); if(hostType().needsEnclosing()) size--; if(hostType().needsSuperEnclosing()) { size--; } String classname = hostType().constantPoolName(); String desc = descName(); String name = "<init>"; int index = gen.constantPool().addMethodref(classname, name, desc); gen.emit(Bytecode.INVOKESPECIAL, size).add2(index); } // Declared in CreateBCode.jrag at line 100 private void generateBytecodes(CodeGeneration gen) { int label = gen.variableScopeLabel(); gen.addLocalVariableEntryAtCurrentPC("this", hostType().typeDescriptor(), 0, label); for(int i = 0; i < getNumParameter(); i++) { ParameterDeclaration p = (ParameterDeclaration)getParameter(i); gen.addLocalVariableEntryAtCurrentPC( p.name(), p.type().typeDescriptor(), p.localNum(), label ); } createBCode(gen); gen.emitReturn(); gen.addVariableScopeLabel(label); } // Declared in CreateBCode.jrag at line 126 public void createBCode(CodeGeneration gen) { try { boolean needsInit = true; if(hasConstructorInvocation()) { getConstructorInvocation().createBCode(gen); Stmt stmt = getConstructorInvocation(); if(stmt instanceof ExprStmt) { ExprStmt exprStmt = (ExprStmt)stmt; Expr expr = exprStmt.getExpr(); if(!expr.isSuperConstructorAccess()) needsInit = false; } } if(needsEnclosing()) { gen.emitLoadReference(0); gen.emitLoadReference(1); String classname = hostType().constantPoolName(); String desc = enclosing().typeDescriptor(); String name = "this$0"; int index = gen.constantPool().addFieldref(classname, name, desc); gen.emit(Bytecode.PUTFIELD, -2).add2(index); } int localIndex = offsetFirstEnclosingVariable(); for(Iterator iter = hostType().enclosingVariables().iterator(); iter.hasNext(); ) { Variable v = (Variable)iter.next(); gen.emitLoadReference(0); v.type().emitLoadLocal(gen, localIndex); String classname = hostType().constantPoolName(); String desc = v.type().typeDescriptor(); String name = "val$" + v.name(); int index = gen.constantPool().addFieldref(classname, name, desc); gen.emit(Bytecode.PUTFIELD, -1 - v.type().variableSize()).add2(index); localIndex += v.type().variableSize(); } if(needsInit) { TypeDecl typeDecl = hostType(); for(int i = 0; i < typeDecl.getNumBodyDecl(); i++) { BodyDecl b = typeDecl.getBodyDecl(i); if(b instanceof FieldDeclaration && b.isBytecodeField() && b.generate()) { FieldDeclaration f = (FieldDeclaration)b; if(!f.isStatic() && f.hasInit()) { gen.emit(Bytecode.ALOAD_0); f.getInit().createBCode(gen); f.getInit().type().emitAssignConvTo(gen, f.type()); // AssignConversion f.emitStoreField(gen, hostType()); } } else if(b instanceof InstanceInitializer) { b.createBCode(gen); } } } gen.maxLocals = Math.max(gen.maxLocals, getBlock().localNum()); getBlock().createBCode(gen); } catch (Error e) { System.err.println(hostType().typeName() + ": " + this); throw e; } } // Declared in GenerateClassfile.jrag at line 252 public void generateMethod(DataOutputStream out, ConstantPool cp) throws IOException { out.writeChar(flags()); out.writeChar(cp.addUtf8("<init>")); out.writeChar(cp.addUtf8(descName())); out.writeChar(attributes().size()); for(Iterator itera = attributes().iterator(); itera.hasNext();) ((Attribute)itera.next()).emit(out); } // Declared in GenerateClassfile.jrag at line 268 public void touchMethod(ConstantPool cp) { cp.addUtf8("<init>"); cp.addUtf8(descName()); attributes(); } // Declared in GenerateClassfile.jrag at line 376 public boolean clear() { getBlock().clear(); setBlock(new Block(new List())); bytecodes_ConstantPool_values = null; return false; } // Declared in InnerClasses.jrag at line 427 // add val$name as parameters to the constructor protected boolean addEnclosingVariables = true; // Declared in InnerClasses.jrag at line 428 public void addEnclosingVariables() { if(!addEnclosingVariables) return; addEnclosingVariables = false; hostType().addEnclosingVariables(); for(Iterator iter = hostType().enclosingVariables().iterator(); iter.hasNext(); ) { Variable v = (Variable)iter.next(); getParameterList().add(new ParameterDeclaration(v.type(), "val$" + v.name())); } } // Declared in InnerClasses.jrag at line 462 public ConstructorDecl createAccessor() { ConstructorDecl c = (ConstructorDecl)hostType().getAccessor(this, "constructor"); if(c != null) return c; // make sure enclosing varibles are added as parameters prior to building accessor addEnclosingVariables(); Modifiers modifiers = new Modifiers(new List()); modifiers.addModifier(new Modifier("synthetic")); modifiers.addModifier(new Modifier("public")); List parameters = createAccessorParameters(); // add all parameters as arguments except for the dummy parameter List args = new List(); for(int i = 0; i < parameters.getNumChildNoTransform() - 1; i++) args.add(new VarAccess(((ParameterDeclaration)parameters.getChildNoTransform(i)).name())); ConstructorAccess access = new ConstructorAccess("this", args); access.addEnclosingVariables = false; c = new ConstructorDecl( modifiers, name(), parameters, (List)getExceptionList().fullCopy(), new Opt( new ExprStmt( access ) ), new Block( new List().add(new ReturnStmt(new Opt())) )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?