KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > Formals


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.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.cst.*;
29
30 import java.util.*;
31
32 /**
33   * @grammar (formal, )*;
34   * @children FormalDec formal
35   */

36
37 public class Formals extends ASTObject {
38
39     // stolen from CodeBody
40
public CodeDec getEnclosingCodeDec() {
41         if (getParent() instanceof CodeDec) return (CodeDec)getParent();
42         return super.getEnclosingCodeDec();
43     }
44     
45     public Set makeSet() {
46         Set ret = new HashSet();
47         for (int i = 0, len = size(); i < len; i++) {
48             ret.add(get(i));
49         }
50         return ret;
51     }
52
53     public Iterator iterator() {
54         return new Iterator() {
55                 private int i = 0;
56                 public boolean hasNext() { return i < size; }
57                 public Object JavaDoc next() { return children[i++]; }
58                 public void remove() {
59                     throw new UnsupportedOperationException JavaDoc();
60                 }
61             };
62     }
63
64     public boolean canBeCalledWith(Exprs parameters) {
65         if (this.size() != parameters.size()) return false;
66         for(int i=0; i<parameters.size(); i++) {
67             if (!parameters.get(i).getType().isMethodConvertableTo(this.get(i).getType())) {
68                 return false;
69             }
70         }
71         return true;
72     }
73
74     public boolean matches(Formals otherFormals) {
75 // FormalDec[] formals1 = children;
76
// FormalDec[] formals2 = otherFormals.children;
77

78         if (size() != otherFormals.size()) return false;
79         for(int i=0; i<size(); i++) {
80 // System.out.println(formals1[i] + ", " + formals2[i]);
81
if (!get(i).getType().isEquivalent(otherFormals.get(i).getType())) return false;
82         }
83         return true;
84     }
85
86     public FormalDec findName(String JavaDoc name) {
87         final int N = size();
88         for(int i=0; i<N; i++) {
89             if (get(i).getId().equals(name)) {
90                 return get(i);
91             }
92         }
93         return null;
94     }
95
96 // protected static VarDec makeTransformVar(FormalDec formal1, FormalDec formal2) {
97
// return new VarDec(formal1.getCompiler(),formal1.getTypeD(), formal1.getName(),
98
// new VarExpr(formal1.getCompiler(),formal2.getName()));
99
// }
100

101     public Map findTransform(Formals otherFormals) {
102         FormalDec[] formals1 = children;
103         FormalDec[] formals2 = otherFormals.children;
104         Map ft = new HashMap();
105
106         if (formals1.length != formals2.length) return null;
107         for(int i=0; i<formals1.length; i++) {
108             if (!formals1[i].getType().isEquivalent(formals2[i].getType())) return null;
109             ft.put(formals1[i].getName(), formals2[i]);
110         }
111         return ft;
112     }
113
114     public String JavaDoc toShortString() {
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116         buf.append("(");
117         final int N = size();
118         for(int i=0; i<N; i++) {
119             if (i > 0) buf.append(", ");
120             FormalDec child = get(i);
121             if (child == null) {
122                 this.showWarning("null child: "+i+" : "+children.length);
123                 //System.out.println("1st child: "+children[1]+", "+children[0]);
124
continue;
125             }
126             TypeD typeD = child.getTypeD();
127             if (typeD == null) child.showError("null typeD on :"+child.getId());
128             buf.append(typeD.toShortString());
129         }
130         buf.append(")");
131         return buf.toString();
132     }
133
134
135     public void unparse(CodeWriter writer) {
136         writer.openParen('(');
137         writer.writeChildrenWithCommas(this);
138         writer.closeParen(')');
139     }
140
141     public Exprs makeExprs() {
142         final AST ast = getAST();
143
144         final int N = size();
145         Exprs args = ast.makeExprs();
146         for(int i=0; i<N; i++) {
147             args.add(ast.makeVar(get(i)));
148         }
149         return args;
150     }
151
152     //BEGIN: Generated from @child and @property
153
protected int size;
154     public FormalDec[] children;
155
156     public Formals(SourceLocation location, FormalDec[] _children) {
157         super(location);
158         for(int i=0; i<_children.length; i++) {
159             if (_children[i] != null) _children[i].setParent(this);
160         }
161         children = _children;
162         size = _children.length;
163     }
164
165     public Formals(SourceLocation location) {
166         this(location, new FormalDec[] {});
167     }
168
169     public Formals(SourceLocation location, FormalDec child1) {
170         this(location, new FormalDec[] {child1});
171     }
172
173     public Formals(SourceLocation location, FormalDec child1, FormalDec child2) {
174         this(location, new FormalDec[] {child1, child2});
175     }
176
177     public Formals(SourceLocation location, FormalDec child1, FormalDec child2, FormalDec child3) {
178         this(location, new FormalDec[] {child1, child2, child3});
179     }
180
181     public ASTObject copyWalk(CopyWalker walker) {
182         final int N = size;
183         FormalDec[] copiedChildren = new FormalDec[N];
184         int newIndex = 0;
185         for(int oldIndex=0; oldIndex<N; oldIndex++) {
186             FormalDec newChild = (FormalDec)walker.process(children[oldIndex]);
187             if (newChild != null) copiedChildren[newIndex++] = newChild;
188         }
189         Formals ret = new Formals(getSourceLocation(),copiedChildren);
190         ret.size = newIndex;
191         ret.setSource(this);
192         return ret;
193     }
194
195     public ASTObject getChildAt(int childIndex) { return get(childIndex); }
196     public void setChildAt(int childIndex, ASTObject child) { set(childIndex, (FormalDec)child); }
197     public String JavaDoc getChildNameAt(int childIndex) { return "formal"+childIndex; }
198
199     public int getChildCount() { return size; }
200     public int size() { return size; }
201
202     public FormalDec get(int index) {
203         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
204         return children[index];
205     }
206
207     public void set(int index, FormalDec child) {
208         if (index >= size) throw new ArrayIndexOutOfBoundsException JavaDoc();
209         children[index] = child;
210         child.setParent(this);
211     }
212
213     public void resize(int newSize) {
214         if (newSize > children.length) {
215             FormalDec[] newChildren = new FormalDec[children.length*2 + 1];
216             System.arraycopy(children, 0, newChildren, 0, children.length);
217             children = newChildren;
218         }
219         size = newSize;
220     }
221
222     public void addAll(Formals collection) {
223         addAll(size, collection);
224     }
225
226     public void addAll(int index, Formals collection) {
227         for(int i=0; i<collection.size(); i++) {
228             add(index+i, collection.get(i));
229         }
230     }
231
232     public void add(FormalDec child) {
233         add(size, child);
234     }
235
236     public void add(int index, FormalDec child) {
237         if (child == null) return;
238
239         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
240
241         resize(size+1);
242
243         for(int moveIndex = size-1; moveIndex > index; moveIndex--) {
244             children[moveIndex] = children[moveIndex-1];
245         }
246
247         children[index] = child;
248         child.setParent(this);
249     }
250
251     public void remove(int index) {
252         if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException JavaDoc();
253
254         size -= 1;
255
256         for(int moveIndex = index; moveIndex < size; moveIndex++) {
257             children[moveIndex] = children[moveIndex+1];
258         }
259     }
260
261     public String JavaDoc getDefaultDisplayName() {
262         return "Formals()";
263     }
264
265     //END: Generated from @child and @property
266
}
267
Popular Tags