KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > NameHygienePass


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

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         // don't print error messages for ambiguous imports,
51
// we will fix them later
52
scope = new CUScope(getCompiler(), null, cu) {
53             protected Type handleAmbiguousImport(String JavaDoc 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 // if (foundVarDec.isField()) {
66
// return varDec.isField() &&
67
// (foundVarDec.getBytecodeTypeDec() == varDec.getBytecodeTypeDec());
68
// }
69
// return true;
70
}
71     
72     // used by mangler to validate name binding better
73
static int globalId = 0;
74     
75     public void addVarDec(VarDec varDec) {
76         int index = 0;
77         String JavaDoc 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         //System.out.println("adding typeDec: " + typeDec);
90
int index = 0;
91         String JavaDoc id = typeDec.getId();
92         
93         // make sure that the name of the typeDec is legal (generated inners are often not)
94
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                     //XXX check for a true collision
105
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 JavaDoc 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 JavaDoc id = type.getId();
128         
129         //fromWhere.showWarning("can use unqualified name? " + id);
130
//System.out.println("can use unqualified name? " + scope);
131
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         //XXX this is a hack to deal with some inner naming issues
140
Scope searchScope = scope;
141         
142         // getStackParent() and getParent() should always return the same thing
143
// because useJavaScopes() returns true
144
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 JavaDoc 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 JavaDoc getDisplayName() {
171         return "name hygiene";
172     }
173 }
174
Popular Tags