1 24 25 package org.aspectj.compiler.base; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.cst.*; 29 import org.aspectj.util.*; 30 31 import java.util.*; 32 33 public class NameHygienePass extends ScopeWalker implements CompilationUnitPass { 34 public NameHygienePass(JavaCompiler compiler) { 35 super(compiler); 36 } 37 38 public double getWorkEstimate() { return 1.0; } 39 40 public void transformWorld() { 41 if (getDisplayName() != null) getCompiler().beginSection(getDisplayName()); 42 for (Iterator i = getWorld().getCompilationUnits().iterator(); i.hasNext(); ) { 43 CompilationUnit cu = (CompilationUnit)i.next(); 44 transform(cu); 45 getCompiler().completedFile(this, cu); 46 } 47 } 48 49 public void enterCU(CompilationUnit cu) { 50 scope = new CUScope(getCompiler(), null, cu) { 53 protected Type handleAmbiguousImport(String id, Type type1, Type type2, ASTObject showWhere) { 54 return this.getTypeManager().TYPE_NOT_FOUND; 55 } 56 }; 57 methods = new HashMap(); 58 } 59 60 private boolean varDecCollision(VarDec varDec) { 61 Expr expr = scope.bindUnqualifiedName(varDec.getId(), varDec); 62 if (expr == null) return false; 63 if (! (expr instanceof VarExpr) ) return false; 64 return ((VarExpr)expr).getVarDec() != varDec; 65 } 71 72 static int globalId = 0; 74 75 public void addVarDec(VarDec varDec) { 76 int index = 0; 77 String id = varDec.getId(); 78 if (getCompiler().getOptions().torture) { 79 varDec.setId(id + "_" + globalId++); 80 } else { 81 while (varDecCollision(varDec)) { 82 varDec.setId(id + index++); 83 } 84 } 85 super.addVarDec(varDec); 86 } 87 88 public void addTypeDec(TypeDec typeDec) { 89 int index = 0; 91 String id = typeDec.getId(); 92 93 if (!Character.isJavaIdentifierStart(id.charAt(0))) { 95 id = "_" + id; 96 typeDec.setId(id); 97 } 98 99 if (typeDec.isLocallyDefined()) { 100 if (getCompiler().getOptions().torture) { 101 typeDec.setId(id + "_" + globalId++); 102 } else { 103 while (scope.findType(typeDec.getId(), typeDec) != null) { 104 typeDec.setId(id + index++); 106 } 107 } 108 } 109 super.addTypeDec(typeDec); 110 } 111 112 Map methods = new HashMap(); 113 114 public void addMethodDec(MethodDec methodDec) { 115 if (!methodDec.isPrivate() || methodDec.fromSource()) return; 116 117 int index = 0; 118 String id = methodDec.getId(); 119 while (scope.findMethodLookupType(methodDec.getId(), methodDec) != null || 120 methods.containsKey(methodDec.getId())) { 121 methodDec.setId(id + index++); 122 } 123 methods.put(methodDec.getId(), methodDec); 124 } 125 126 public boolean canUseUnqualifiedName(Type type, ASTObject fromWhere) { 127 String id = type.getId(); 128 129 fromWhere = fromWhere.getBytecodeTypeDec().getBody(); 132 133 134 if (scope.bindUnqualifiedName(id, fromWhere) != null) return false; 135 136 Type foundType = scope.findType(id, fromWhere); 137 if (foundType != type) return false; 138 139 Scope searchScope = scope; 141 142 while (searchScope.getStackParent() != null) { 145 searchScope = searchScope.getStackParent(); 146 Type outerFoundType = searchScope.findType(id, fromWhere); 147 if (outerFoundType == null) return true; 148 if (outerFoundType != type) return false; 149 } 150 return true; 151 } 152 153 public boolean mustUseNullTrick(Type type, ASTObject fromWhere) { 154 String id = type.getPackageName(); 155 if (id == null) { 156 id = type.getId(); 157 } else { 158 int dot = id.indexOf('.'); 159 if (dot != -1) id = id.substring(0, dot); 160 } 161 return scope.bindUnqualifiedName(id, fromWhere) != null; 162 } 163 164 public void transform(CompilationUnit cu) { 165 process(cu); 166 } 167 168 public boolean useJavaScopes() { return true; } 169 170 public String getDisplayName() { 171 return "name hygiene"; 172 } 173 } 174 | Popular Tags |