KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > FormalsPattern


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.crosscuts.ast;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.crosscuts.joinpoints.*;
29
30 import org.aspectj.compiler.base.JavaCompiler;
31
32 import java.util.*;
33
34 /**
35   * @grammar ([typename|.., ]*)
36   * @child GenTypeNames names
37   */

38 public class FormalsPattern extends ASTObject {
39     private boolean isEllipses(GenTypeName name) {
40         return name instanceof EllipsesFakeTypeName;
41     }
42     
43     
44     public JpPlan finishPlan(JpPlan plan, ASTObject nodes, JoinPoint point,
45                                 int namesIndex, int formalsIndex) {
46         if (namesIndex >= names.size()) {
47             if (formalsIndex >= nodes.getChildCount()) return plan;
48             else return JpPlan.NO_PLAN;
49         }
50         
51         GenTypeName name = names.get(namesIndex);
52         if (isEllipses(name)) {
53             //JpPlan plan1 = plan;
54
//JpPlan ret = JpPlan.NO_PLAN;
55
List plans = new LinkedList();
56             for (; formalsIndex <= nodes.getChildCount(); formalsIndex++) {
57                 JpPlan tmp = finishPlan(plan, nodes, point, namesIndex+1, formalsIndex);
58                 //??? bairly adequate ambiguity handling
59
if (tmp.isPossible()) { plans.add(tmp); }
60             }
61             JpPlan ret = JpPlan.NO_PLAN;
62             for (Iterator i = plans.iterator(); i.hasNext(); ) {
63                 JpPlan p = (JpPlan)i.next();
64                 ret = ret.or(p);
65             }
66             return ret;
67         } else {
68             if (formalsIndex >= nodes.getChildCount()) return JpPlan.NO_PLAN;
69             JpPlan paramPlan = name.makePlan(point, (Expr)nodes.getChildAt(formalsIndex));
70             if (paramPlan.isPossible()) {
71                 JpPlan nextPlan = finishPlan(plan, nodes, point, namesIndex+1, formalsIndex+1);
72                 return nextPlan.and(paramPlan);
73                 //return finishPlan(plan.and(paramPlan), nodes, point, namesIndex+1, formalsIndex+1);
74
} else {
75                 return JpPlan.NO_PLAN;
76             }
77         }
78                                     
79                                     
80 // if (namesIndex >= names.size()) {
81
// if (formalsIndex >= nodes.getChildCount()) return plan;
82
// else return JpPlan.NO_PLAN;
83
// }
84
//
85
// GenTypeName name = names.get(namesIndex);
86
// if (isEllipses(name)) {
87
// JpPlan plan1 = plan;
88
// JpPlan ret = JpPlan.NO_PLAN;
89
// for (; formalsIndex <= nodes.getChildCount(); formalsIndex++) {
90
// JpPlan tmp = finishPlan(plan1, nodes, point, namesIndex+1, formalsIndex);
91
// //??? bairly adequate ambiguity handling
92
// if (tmp.isPossible()) {
93
// ret = tmp;
94
// plan1 = tmp;
95
// }
96
// }
97
// return ret;
98
// } else {
99
// if (formalsIndex >= nodes.getChildCount()) return JpPlan.NO_PLAN;
100
// JpPlan paramPlan = name.makePlan(point, (Expr)nodes.getChildAt(formalsIndex));
101
// if (paramPlan.isPossible()) {
102
// return finishPlan(plan.and(paramPlan), nodes, point, namesIndex+1, formalsIndex+1);
103
// } else {
104
// return JpPlan.NO_PLAN;
105
// }
106
// }
107
}
108     
109     
110     public JpPlan finishPlan(JpPlan plan, Exprs exprs, JoinPoint point) {
111         return finishPlan(plan, exprs, point, 0, 0);
112     }
113
114     public JpPlan makePlan(Exprs exprs, JoinPoint jp) {
115         return finishPlan(JpPlan.ANY_PLAN, exprs, jp);
116     }
117     
118     
119     public boolean matches(Formals formals) {
120         return matches(formals, 0, 0);
121     }
122     
123     public Type extractType(ASTObject node) {
124         if (node instanceof FormalDec) {
125             return ((FormalDec)node).getType();
126         } else if (node instanceof Expr) {
127             return ((Expr)node).getType();
128         } else {
129             return null;
130         }
131     }
132     
133     public boolean matches(ASTObject nodes, int namesIndex, int formalsIndex) {
134         if (namesIndex >= names.size()) {
135             if (formalsIndex >= nodes.getChildCount()) return true;
136             else return false;
137         }
138         
139         GenTypeName name = names.get(namesIndex);
140         if (isEllipses(name)) {
141             for (; formalsIndex <= nodes.getChildCount(); formalsIndex++) {
142                 if (matches(nodes, namesIndex+1, formalsIndex)) return true;
143             }
144             return false;
145         } else {
146             if (formalsIndex >= nodes.getChildCount()) return false;
147             if (name.matches(extractType(nodes.getChildAt(formalsIndex)))) {
148                 return matches(nodes, namesIndex+1, formalsIndex+1);
149             } else {
150                 return false;
151             }
152         }
153     }
154     
155     
156     public String JavaDoc toShortString() {
157         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
158         buf.append("(");
159         final int N = names.size();
160         for(int i=0; i<=N; i++) {
161             if (i < N) {
162                buf.append(names.get(i).toShortString());
163             }
164             if (i < (N-1)) {
165                 buf.append(", ");
166             }
167         }
168         buf.append(")");
169         return buf.toString();
170     }
171     
172     //BEGIN: Generated from @child and @property
173
protected GenTypeNames names;
174     public GenTypeNames getNames() { return names; }
175     public void setNames(GenTypeNames _names) {
176         if (_names != null) _names.setParent(this);
177         names = _names;
178     }
179     
180     public FormalsPattern(SourceLocation location, GenTypeNames _names) {
181         super(location);
182         setNames(_names);
183     }
184     protected FormalsPattern(SourceLocation source) {
185         super(source);
186     }
187     
188     public ASTObject copyWalk(CopyWalker walker) {
189         FormalsPattern ret = new FormalsPattern(getSourceLocation());
190         ret.preCopy(walker, this);
191         if (names != null) ret.setNames( (GenTypeNames)walker.process(names) );
192         return ret;
193     }
194     
195     public ASTObject getChildAt(int childIndex) {
196         switch(childIndex) {
197         case 0: return names;
198         default: return super.getChildAt(childIndex);
199         }
200     }
201      public String JavaDoc getChildNameAt(int childIndex) {
202         switch(childIndex) {
203         case 0: return "names";
204         default: return super.getChildNameAt(childIndex);
205         }
206     }
207      public void setChildAt(int childIndex, ASTObject child) {
208         switch(childIndex) {
209         case 0: setNames((GenTypeNames)child); return;
210         default: super.setChildAt(childIndex, child); return;
211         }
212     }
213      public int getChildCount() {
214         return 1;
215     }
216     
217     public String JavaDoc getDefaultDisplayName() {
218         return "FormalsPattern()";
219     }
220     
221     //END: Generated from @child and @property
222
}
223
Popular Tags