?? classinstanceexpr.java
字號:
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 ClassInstanceExpr extends Access implements Cloneable {
public void flushCache() { super.flushCache(); isDAafterInstance_Variable_values = null; computeDAbefore_int_Variable_values = null; computeDUbefore_int_Variable_values = null; decls_computed = false; decls_value = null; decl_computed = false; decl_value = null; localLookupType_String_values = null; type_computed = false; type_value = null; } @SuppressWarnings({"unchecked", "cast"}) public ClassInstanceExpr clone() throws CloneNotSupportedException { ClassInstanceExpr node = (ClassInstanceExpr)super.clone(); node.isDAafterInstance_Variable_values = null; node.computeDAbefore_int_Variable_values = null; node.computeDUbefore_int_Variable_values = null; node.decls_computed = false; node.decls_value = null; node.decl_computed = false; node.decl_value = null; node.localLookupType_String_values = null; node.type_computed = false; node.type_value = null; node.in$Circle(false); node.is$Final(false); return node; } @SuppressWarnings({"unchecked", "cast"}) public ClassInstanceExpr copy() { try { ClassInstanceExpr node = (ClassInstanceExpr)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 ClassInstanceExpr fullCopy() { ClassInstanceExpr res = (ClassInstanceExpr)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 AccessControl.jrag at line 139 public void accessControl() { super.accessControl(); if(type().isAbstract()) error("Can not instantiate abstract class " + type().fullName()); if(!decl().accessibleFrom(hostType())) error("constructor " + decl().signature() + " is not accessible"); } // Declared in ExceptionHandling.jrag at line 253 protected boolean reachedException(TypeDecl catchType) { ConstructorDecl decl = decl(); for(int i = 0; i < decl.getNumException(); i++) { TypeDecl exceptionType = decl.getException(i).type(); if(catchType.mayCatch(exceptionType)) return true; } return super.reachedException(catchType); } // Declared in LookupType.jrag at line 326 public SimpleSet keepInnerClasses(SimpleSet c) { SimpleSet newSet = SimpleSet.emptySet; for(Iterator iter = c.iterator(); iter.hasNext(); ) { TypeDecl t = (TypeDecl)iter.next(); if(t.isInnerType() && t.isClassDecl()) { newSet = newSet.add(c); } } return newSet; } // Declared in NameCheck.jrag at line 137 public void nameCheck() { super.nameCheck(); if(decls().isEmpty()) error("can not instantiate " + type().typeName() + " no matching constructor found in " + type().typeName()); else if(decls().size() > 1 && validArgs()) { error("several most specific constructors found"); for(Iterator iter = decls().iterator(); iter.hasNext(); ) { error(" " + ((ConstructorDecl)iter.next()).signature()); } } } // Declared in NodeConstructors.jrag at line 82 public ClassInstanceExpr(Access type, List args) { this(type, args, new Opt()); } // Declared in PrettyPrint.jadd at line 345 public void toString(StringBuffer s) { s.append("new "); getAccess().toString(s); s.append("("); if(getNumArg() > 0) { getArg(0).toString(s); for(int i = 1; i < getNumArg(); i++) { s.append(", "); getArg(i).toString(s); } } s.append(")"); if(hasTypeDecl()) { TypeDecl decl = getTypeDecl(); s.append(" {\n"); indent++; for(int i = 0; i < decl.getNumBodyDecl(); i++) { if(!(decl.getBodyDecl(i) instanceof ConstructorDecl)) decl.getBodyDecl(i).toString(s); } indent--; s.append(indent()); s.append("}"); } } // Declared in TypeCheck.jrag at line 434 public void typeCheck() { if(isQualified() && qualifier().isTypeAccess() && !qualifier().type().isUnknown()) error("*** The expression in a qualified class instance expr must not be a type name"); // 15.9 if(isQualified() && !type().isInnerClass() && !((ClassDecl)type()).superclass().isInnerClass() && !type().isUnknown()) { error("*** Qualified class instance creation can only instantiate inner classes and their anonymous subclasses"); } if(!type().isClassDecl()) { error("*** Can only instantiate classes, which " + type().typeName() + " is not"); } typeCheckEnclosingInstance(); typeCheckAnonymousSuperclassEnclosingInstance(); } // Declared in TypeCheck.jrag at line 448 public void typeCheckEnclosingInstance() { TypeDecl C = type(); if(!C.isInnerClass()) return; TypeDecl enclosing = null; if(C.isAnonymous()) { if(noEnclosingInstance()) { enclosing = null; } else { enclosing = hostType(); } } else if(C.isLocalClass()) { if(C.inStaticContext()) { enclosing = null; } else if(noEnclosingInstance()) { enclosing = unknownType(); } else { TypeDecl nest = hostType(); while(nest != null && !nest.instanceOf(C.enclosingType())) nest = nest.enclosingType(); enclosing = nest; } } else if(C.isMemberType()) { if(!isQualified()) { if(noEnclosingInstance()) { error("No enclosing instance to initialize " + C.typeName() + " with"); //System.err.println("ClassInstanceExpr: Non qualified MemberType " + C.typeName() + " is in a static context when instantiated in " + this); enclosing = unknownType(); } else { TypeDecl nest = hostType(); while(nest != null && !nest.instanceOf(C.enclosingType())) nest = nest.enclosingType(); enclosing = nest == null ? unknownType() : nest; } } else { enclosing = enclosingInstance(); } } if(enclosing != null && !enclosing.instanceOf(type().enclosingType())) { String msg = enclosing == null ? "None" : enclosing.typeName(); error("*** Can not instantiate " + type().typeName() + " with the enclosing instance " + msg + " due to incorrect enclosing instance"); } else if(!isQualified() && C.isMemberType() && inExplicitConstructorInvocation() && enclosing == hostType()) { error("*** The innermost enclosing instance of type " + enclosing.typeName() + " is this which is not yet initialized here."); } } // Declared in TypeCheck.jrag at line 521 public void typeCheckAnonymousSuperclassEnclosingInstance() { if(type().isAnonymous() && ((ClassDecl)type()).superclass().isInnerType()) { TypeDecl S = ((ClassDecl)type()).superclass(); if(S.isLocalClass()) { if(S.inStaticContext()) { } else if(noEnclosingInstance()) { error("*** No enclosing instance to class " + type().typeName() + " due to static context"); } else if(inExplicitConstructorInvocation()) error("*** No enclosing instance to superclass " + S.typeName() + " of " + type().typeName() + " since this is not initialized yet"); } else if(S.isMemberType()) { if(!isQualified()) { // 15.9.2 2nd paragraph if(noEnclosingInstance()) { error("*** No enclosing instance to class " + type().typeName() + " due to static context"); } else { TypeDecl nest = hostType(); while(nest != null && !nest.instanceOf(S.enclosingType())) nest = nest.enclosingType(); if(nest == null) { error("*** No enclosing instance to superclass " + S.typeName() + " of " + type().typeName()); } else if(inExplicitConstructorInvocation()) { error("*** No enclosing instance to superclass " + S.typeName() + " of " + type().typeName() + " since this is not initialized yet"); } } } } } } // Declared in CreateBCode.jrag at line 649 // 15.9.2 private void emitLocalEnclosing(CodeGeneration gen, TypeDecl localClass) { if(!localClass.inStaticContext()) { emitThis(gen, localClass.enclosingType()); } } // Declared in CreateBCode.jrag at line 654 private void emitInnerMemberEnclosing(CodeGeneration gen, TypeDecl innerClass) { if(hasPrevExpr()) { prevExpr().createBCode(gen); gen.emitDup(); int index = gen.constantPool().addMethodref("java/lang/Object", "getClass", "()Ljava/lang/Class;"); gen.emit(Bytecode.INVOKEVIRTUAL, 0).add2(index); gen.emitPop(); } else { TypeDecl enclosing = hostType(); while(enclosing != null && !enclosing.hasType(innerClass.name())) enclosing = enclosing.enclosingType(); if(enclosing == null) throw new Error(errorPrefix() + "Could not find enclosing for " + this); else emitThis(gen, enclosing); } } // Declared in CreateBCode.jrag at line 672 public void createBCode(CodeGeneration gen) { type().emitNew(gen); type().emitDup(gen); // 15.9.2 first part if(type().isAnonymous()) { if(type().isAnonymousInNonStaticContext()) { if(type().inExplicitConstructorInvocation()) gen.emit(Bytecode.ALOAD_1); else gen.emit(Bytecode.ALOAD_0); } // 15.9.2 second part ClassDecl C = (ClassDecl)type(); TypeDecl S = C.superclass(); if(S.isLocalClass()) { if(!type().inStaticContext()) emitLocalEnclosing(gen, S); } else if(S.isInnerType()) { emitInnerMemberEnclosing(gen, S); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -