1 package polyglot.ext.jl.ast; 2 3 import java.util.List ; 4 5 import polyglot.ast.*; 6 import polyglot.types.*; 7 import polyglot.util.CodeWriter; 8 import polyglot.util.Position; 9 import polyglot.visit.*; 10 11 17 public class Special_c extends Expr_c implements Special 18 { 19 protected Special.Kind kind; 20 protected TypeNode qualifier; 21 22 public Special_c(Position pos, Special.Kind kind, TypeNode qualifier) { 23 super(pos); 24 this.kind = kind; 25 this.qualifier = qualifier; 26 } 27 28 29 public Precedence precedence() { 30 return Precedence.LITERAL; 31 } 32 33 34 public Special.Kind kind() { 35 return this.kind; 36 } 37 38 39 public Special kind(Special.Kind kind) { 40 Special_c n = (Special_c) copy(); 41 n.kind = kind; 42 return n; 43 } 44 45 46 public TypeNode qualifier() { 47 return this.qualifier; 48 } 49 50 51 public Special qualifier(TypeNode qualifier) { 52 Special_c n = (Special_c) copy(); 53 n.qualifier = qualifier; 54 return n; 55 } 56 57 58 protected Special_c reconstruct(TypeNode qualifier) { 59 if (qualifier != this.qualifier) { 60 Special_c n = (Special_c) copy(); 61 n.qualifier = qualifier; 62 return n; 63 } 64 65 return this; 66 } 67 68 69 public Node visitChildren(NodeVisitor v) { 70 TypeNode qualifier = (TypeNode) visitChild(this.qualifier, v); 71 return reconstruct(qualifier); 72 } 73 74 75 public Node typeCheck(TypeChecker tc) throws SemanticException { 76 TypeSystem ts = tc.typeSystem(); 77 Context c = tc.context(); 78 79 ClassType t; 80 81 if (qualifier == null) { 82 t = c.currentClass(); 84 } 85 else { 86 if (! qualifier.type().isClass()) { 87 throw new SemanticException("Qualified " + kind + 88 " expression must be of a class type", 89 qualifier.position()); 90 } 91 92 t = qualifier.type().toClass(); 93 94 if (!c.currentClass().hasEnclosingInstance(t)) { 95 throw new SemanticException("The nested class \"" + 96 c.currentClass() + "\" does not have " + 97 "an enclosing instance of type \"" + 98 t + "\".", qualifier.position()); 99 } 100 } 101 102 if (c.inStaticContext() && ts.equals(t, c.currentClass())) { 103 throw new SemanticException("Cannot access a non-static " + 105 "field or method, or refer to \"this\" or \"super\" " + 106 "from a static context.", this.position()); 107 } 108 109 if (kind == THIS) { 110 return type(t); 111 } 112 else if (kind == SUPER) { 113 return type(t.superType()); 114 } 115 return this; 116 } 117 118 122 public Term entry() { 123 return this; 124 } 125 126 129 public List acceptCFG(CFGBuilder v, List succs) { 130 return succs; 131 } 132 133 public String toString() { 134 return (qualifier != null ? qualifier + "." : "") + kind; 135 } 136 137 138 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 139 if (qualifier != null) { 140 print(qualifier, w, tr); 141 w.write("."); 142 } 143 144 w.write(kind.toString()); 145 } 146 147 public void dump(CodeWriter w) { 148 super.dump(w); 149 150 if (kind != null) { 151 w.allowBreak(4, " "); 152 w.begin(0); 153 w.write("(kind " + kind + ")"); 154 w.end(); 155 } 156 } 157 } 158 | Popular Tags |