1 24 25 package org.aspectj.compiler.base.cst; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.*; 29 import java.util.*; 30 31 37 public class Name extends ASTObject { 38 boolean allowType = true; 39 40 public Name(SourceLocation location, Name parentName, String id) { 41 this(location, parentName, id, true); 42 } 43 44 public void forceType() { 45 allowExpr = false; 46 if (parentName != null) parentName.forceType(); 47 } 48 49 52 53 private Object resolveSimple(Scope scope, boolean showError) { 54 if (allowExpr) { 55 Expr expr = scope.bindUnqualifiedName(id, this); 56 57 if (expr != null) { 58 expr.setSourceLocation(getSourceLocation()); 59 return expr; 60 } else if (showError) { 61 this.showError("can't bind name: " + id); 62 } 63 } 64 if (allowType) { 65 Type type = scope.findType(id, this); 66 if (type != null) return type; 67 68 if (type == null && showError && !allowExpr) { 69 this.showError("type name not found: " + id); 70 } 71 } 72 73 return id; 74 } 75 76 private Object resolve1(Expr parentExpr, boolean showError) { 77 Field field = parentExpr.getType().getField(id, this, showError); 78 ASTObject ret = getAST().makeGet(parentExpr, field); 79 ret.setSourceLocation(getSourceLocation()); 80 return ret; 81 } 82 83 private Object resolve1(String parentPackage, boolean showError) { 84 Type type = getTypeManager().findType(parentPackage, id); 85 if (type == null) { 86 return parentPackage + '.' + id; 87 } else { 88 return type; 89 } 90 } 91 92 private Object resolve1(Type parentType, boolean showError) { 93 if (allowExpr) { 94 Field field = parentType.getField(id, this, false); 95 if (field != null) { 96 TypeExpr typeExpr = getAST().makeTypeExpr(parentType); 97 typeExpr.setSourceLocation(parentName.getSourceLocation()); 98 ASTObject ret = getAST().makeGet(typeExpr, field); 99 ret.setSourceLocation(getSourceLocation()); 100 return ret; 101 } 102 } 103 if (allowType) { 104 Type type = parentType.getInnerType(id, this, false); 105 if (type != getTypeManager().TYPE_NOT_FOUND) { 106 return type; 107 } 108 } 109 110 if (!showError) return null; 111 112 if (allowExpr && allowType) { 114 return getAST().makeGet(null, parentType.getField(id, this, true)); 115 } else if (allowType && !allowExpr) { 116 if (parentType.getField(id, this, false) != null) { 117 this.showError("type required, but field found: " + id); 118 return getTypeManager().TYPE_NOT_FOUND; 119 } else { 120 return parentType.getInnerType(id, this, true); 121 } 122 } else { 123 this.showError("name not found: " + id); 124 return null; 125 } 126 } 127 128 129 public Type resolveType(Scope scope) { 130 return resolveType(scope, true); 131 } 132 133 public Type resolveType(Scope scope, boolean showError) { 134 forceType(); 135 Object ret = internalResolve(scope, showError); 136 if (ret instanceof Type) { 137 return (Type)ret; 138 } 139 if (showError) showNiceError(scope); 140 return getTypeManager().TYPE_NOT_FOUND; 141 } 142 143 public Expr resolveExpr(Scope scope) { 144 Object ret = internalResolve(scope, true); 145 if (ret instanceof Expr) { 146 return (Expr)ret; 147 } else if (ret instanceof Type) { 148 Expr expr = getAST().makeTypeExpr((Type)ret); 150 expr.setSourceLocation(getSourceLocation()); 151 return expr; 152 } 153 154 showNiceError(scope); 155 return getAST().makeNotFoundExpr(); 156 } 157 158 private void showNiceError(Scope scope) { 159 if (parentName == null) { 161 resolveSimple(scope, true); 162 } else { 163 showError("can't bind name: " + toString()); 164 } 165 } 166 167 168 177 public Object resolve(Scope scope, boolean showError) { 178 return internalResolve(scope, showError); 179 } 180 181 private final Object internalResolve(Scope scope, boolean showError) { 182 if (parentName == null) { 183 return resolveSimple(scope, false); 185 } else { 186 Object parentO = parentName.internalResolve(scope, showError); 187 if (parentO == null) { 188 return null; 189 } else if (parentO instanceof Type) { 190 return resolve1((Type)parentO, showError); 191 } else if (parentO instanceof String ) { 192 return resolve1((String )parentO, showError); 193 } else { 194 return resolve1((Expr)parentO, showError); 195 } 196 } 197 } 198 199 200 201 public String toString() { 202 if (parentName == null) { 203 return id; 204 } else { 205 return parentName.toString() + '.' + id; 206 } 207 } 208 209 protected Name parentName; 211 public Name getParentName() { return parentName; } 212 public void setParentName(Name _parentName) { 213 if (_parentName != null) _parentName.setParent(this); 214 parentName = _parentName; 215 } 216 217 protected String id; 218 public String getId() { return id; } 219 public void setId(String _id) { id = _id; } 220 221 protected boolean allowExpr; 222 public boolean getAllowExpr() { return allowExpr; } 223 public void setAllowExpr(boolean _allowExpr) { allowExpr = _allowExpr; } 224 225 public Name(SourceLocation location, Name _parentName, String _id, boolean _allowExpr) { 226 super(location); 227 setParentName(_parentName); 228 setId(_id); 229 setAllowExpr(_allowExpr); 230 } 231 protected Name(SourceLocation source) { 232 super(source); 233 } 234 235 public ASTObject copyWalk(CopyWalker walker) { 236 Name ret = new Name(getSourceLocation()); 237 ret.preCopy(walker, this); 238 if (parentName != null) ret.setParentName( (Name)walker.process(parentName) ); 239 ret.id = id; 240 ret.allowExpr = allowExpr; 241 return ret; 242 } 243 244 public ASTObject getChildAt(int childIndex) { 245 switch(childIndex) { 246 case 0: return parentName; 247 default: return super.getChildAt(childIndex); 248 } 249 } 250 public String getChildNameAt(int childIndex) { 251 switch(childIndex) { 252 case 0: return "parentName"; 253 default: return super.getChildNameAt(childIndex); 254 } 255 } 256 public void setChildAt(int childIndex, ASTObject child) { 257 switch(childIndex) { 258 case 0: setParentName((Name)child); return; 259 default: super.setChildAt(childIndex, child); return; 260 } 261 } 262 public int getChildCount() { 263 return 1; 264 } 265 266 public String getDefaultDisplayName() { 267 return "Name(id: "+id+", "+"allowExpr: "+allowExpr+")"; 268 } 269 270 } 272 | Popular Tags |