KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > cst > Name


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.cst;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.*;
29 import java.util.*;
30
31 /**
32   * @grammar parent.id
33   * @child Name parentName
34   * @property String id
35   * @property boolean allowExpr
36   */

37 public class Name extends ASTObject {
38     boolean allowType = true;
39
40     public Name(SourceLocation location, Name parentName, String JavaDoc 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     /********************************************************************************
50      * New Scope-based design
51      */

52
53     private Object JavaDoc 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 JavaDoc 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 JavaDoc resolve1(String JavaDoc 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 JavaDoc 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         // some code to generate better error messages
113
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 JavaDoc 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 JavaDoc ret = internalResolve(scope, true);
145         if (ret instanceof Expr) {
146             return (Expr)ret;
147         } else if (ret instanceof Type) {
148             //??? is this really legal ???
149
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         //System.out.println("error in " + scope);
160
if (parentName == null) {
161             resolveSimple(scope, true);
162         } else {
163             showError("can't bind name: " + toString());
164         }
165     }
166
167
168     /**
169      * returns one of
170      * <ul>
171      * <li> Type if this is a type name
172      * <li> Expr (VarExpr or FieldAccessExpr) if this is a variable or field name
173      * <li> String if this is a package name
174      * <li> null if nothing matches
175      * </ul>
176      */

177     public Object JavaDoc resolve(Scope scope, boolean showError) {
178         return internalResolve(scope, showError);
179     }
180     
181     private final Object JavaDoc internalResolve(Scope scope, boolean showError) {
182         if (parentName == null) {
183             //!!! why can't this be showError???
184
return resolveSimple(scope, false);
185         } else {
186             Object JavaDoc 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 JavaDoc) {
192                 return resolve1((String JavaDoc)parentO, showError);
193             } else {
194                 return resolve1((Expr)parentO, showError);
195             }
196         }
197     }
198     
199     
200     
201     public String JavaDoc toString() {
202         if (parentName == null) {
203             return id;
204         } else {
205             return parentName.toString() + '.' + id;
206         }
207     }
208     
209     //BEGIN: Generated from @child and @property
210
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 JavaDoc id;
218     public String JavaDoc getId() { return id; }
219     public void setId(String JavaDoc _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 JavaDoc _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 JavaDoc 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 JavaDoc getDefaultDisplayName() {
267         return "Name(id: "+id+", "+"allowExpr: "+allowExpr+")";
268     }
269     
270     //END: Generated from @child and @property
271
}
272
Popular Tags