root/trunk/ProjectFortress/src/com/sun/fortress/nodes_util/NodeUtil.java @ 2231

Revision 2231, 15.0 KB (checked in by sukyoungryu, 17 months ago)

[parser] Fixed parsing multifix operator declarations without varargs parameters.

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.nodes_util;
19
20import java.io.IOException;
21import java.util.List;
22import java.util.ArrayList;
23import java.util.Iterator;
24import edu.rice.cs.plt.tuple.Option;
25import edu.rice.cs.plt.tuple.OptionVisitor;
26import edu.rice.cs.plt.iter.IterUtil;
27
28import com.sun.fortress.nodes.*;
29import com.sun.fortress.useful.*;
30import com.sun.fortress.exceptions.InterpreterBug;
31import com.sun.fortress.interpreter.evaluator.values.Overload;
32import com.sun.fortress.interpreter.glue.NativeApp;
33import com.sun.fortress.interpreter.glue.WellKnownNames;
34
35import static com.sun.fortress.exceptions.InterpreterBug.bug;
36
37public class NodeUtil {
38
39    public static final String defaultSelfName = WellKnownNames.defaultSelfName;
40
41
42    public static Iterable<Id> getIds(final Id qName) {
43        return qName.getApi().apply(new OptionVisitor<APIName, Iterable<Id>>() {
44            public Iterable<Id> forSome(APIName apiName) {
45                return IterUtil.compose(apiName.getIds(), qName);
46            }
47            public Iterable<Id> forNone() {
48                return IterUtil.singleton(qName);
49            }
50        });
51    }
52
53    /* for HasAt ***********************************************************/
54    /**
55     * Returns the index of the 'self' parameter in the parameter list,
56     * or -1 if it does not appear.
57     * Only meaningful for method declarations.
58     */
59    public static int selfParameterIndex(Applicable d) {
60        int i = 0;
61        for (Param p : d.getParams()) {
62            Id name = p.getName();
63                if (WellKnownNames.defaultSelfName.equals(nameString(name))) {
64                return i;
65            }
66            i++;
67        }
68        return -1;
69    }
70
71    /* for Applicable ******************************************************/
72    public static String nameAsMethod(Applicable app) {
73        String name = nameString(app.getName());
74            int spi = selfParameterIndex(app);
75            if (spi >= 0)
76                return "rm$" + spi + "$" + name;
77            else
78                return name;
79
80    }
81
82    public static Option<Expr> getBody(Applicable def) {
83        if (def instanceof FnDef) { return Option.some(((FnDef)def).getBody()); }
84        else if (def instanceof FnExpr) { return Option.some(((FnExpr)def).getBody()); }
85        else { return Option.none(); }
86    }
87
88    /* for Param ***********************************************************/
89    public static boolean isMultifix(List<Param> params) {
90        for (Param p : params) {
91            if (p instanceof VarargsParam) return true;
92        }
93        return (params.size() > 2);
94    }
95
96    public static boolean isTransient(Param p) {
97        for (Modifier m : p.getMods()) {
98            if (m instanceof ModifierTransient) {
99                return true;
100            }
101        }
102        return false;
103    }
104
105    public static boolean isMutable(Param p) {
106        for (Modifier m : p.getMods()) {
107            if (m instanceof ModifierVar || m instanceof ModifierSettable) {
108                return true;
109            }
110        }
111        return false;
112    }
113
114    private final static NodeVisitor<String> nameGetter =
115        new NodeAbstractVisitor<String>() {
116
117        @Override public String forAPIName(APIName n) {
118            return nameString(n);
119            }
120        @Override public String forIdOrOpOrAnonymousName(IdOrOpOrAnonymousName n) {
121            return nameString(n);
122            }
123        public String forId(Id n) { return nameString(n); }
124        public String forOp(Op n) { return nameString(n); }
125        public String forEnclosing(Enclosing n) { return nameString(n); }
126        public String forAnonymousFnName(AnonymousFnName n) {
127            return nameString(n);
128        }
129        public String forConstructorFnName(ConstructorFnName n) {
130            // TODO Auto-generated method stub
131            return nameString(n);
132        }
133    };
134
135    /* nameString *************************************************************/
136    public static String nameString(Name n) {
137        return n.accept(nameGetter);
138    }
139
140    public static String nameString(IdOrOpOrAnonymousName n) {
141        return n.accept(nameGetter);
142    }
143
144    public static String nameString(APIName n) {
145        return n.getText();
146//        Iterable<String> ns = IterUtil.map(n.getIds(), IdToStringFn);
147//      return IterUtil.toString(ns, "", ".", "");
148    }
149
150    public static String dirString(APIName n) {
151        Iterable<String> ns = IterUtil.map(n.getIds(), IdToStringFn);
152        // NOT File.separator -- that is unnecessary and confusing.
153        return IterUtil.toString(ns, "", "/", "");
154    }
155
156    public static String nameString(Id n) {
157        final String last = n.getText();
158        Option<APIName> odn = n.getApi();
159        return odn.isSome() ? nameString(odn.unwrap()) + "." + last : last;
160    }
161
162    public static String nameString(Op n) {
163        return OprUtil.fixityDecorator(n.getFixity(), n.getText());
164    }
165
166    public static String nameString(Enclosing n) {
167        return n.getOpen().getText() + " " + n.getClose().getText();
168    }
169    public static String nameString(AnonymousFnName n) {
170        return n.getSpan().toString();
171    }
172    public static String nameString(ConstructorFnName n) {
173        // TODO Auto-generated method stub
174        return stringName(n.getDef());
175    }
176
177    /*
178    public static String nameString(IdOrOpOrAnonymousName n) {
179        final String last = n.accept(nameGetter);
180        Option<APIName> odn = n.getApi();
181        return odn.isSome() ? nameString(odn.unwrap()) + "." + last : last;
182    }
183    */
184
185    public static String namesString(Iterable<? extends Name> names) {
186        return IterUtil.toString(IterUtil.map(names, NameToStringFn), "", ", ", "");
187    }
188
189
190    /* getName *************************************************************/
191    public static String getName(StaticParam param) {
192        return param.accept(new NodeAbstractVisitor<String>() {
193            public String forBoolParam(BoolParam p) {
194                return p.getName().getText();
195            }
196            public String forDimParam(DimParam p) {
197                return p.getName().getText();
198            }
199            public String forIntParam(IntParam p) {
200                return p.getName().getText();
201            }
202            public String forNatParam(NatParam p) {
203                return p.getName().getText();
204            }
205            public String forOpParam(OpParam p) {
206                return nameString(p.getName());
207            }
208            public String forTypeParam(TypeParam p) {
209                return p.getName().getText();
210            }
211            public String forUnitParam(UnitParam p) {
212                return p.getName().getText();
213            }
214        });
215    }
216
217    private final static NodeAbstractVisitor<String> stringNameVisitor =
218        new NodeAbstractVisitor<String>() {
219        public String forDimDecl(DimDecl node) {
220            return nameString(node.getDim());
221        }
222        public String forUnitDecl(UnitDecl node) {
223            List<Id> ids = node.getUnits();
224            if (ids.size() < 1)
225                return bug("Unit declarations should have a name.");
226            else return nameString(ids.get(0));
227        }
228        public String forFnAbsDeclOrDecl(FnAbsDeclOrDecl node) {
229            return nameString(node.getName());
230        }
231        public String forIdOrOpOrAnonymousName(IdOrOpOrAnonymousName node) {
232            return nameString(node);
233        }
234        public String forObjectAbsDeclOrDecl(ObjectAbsDeclOrDecl node) {
235            return node.getName().getText();
236        }
237        public String for_RewriteObjectExpr(_RewriteObjectExpr node) {
238            return node.getGenSymName();
239        }
240        public String forTraitAbsDeclOrDecl(TraitAbsDeclOrDecl node) {
241            return node.getName().getText();
242        }
243        public String forTypeAlias(TypeAlias node) {
244            return node.getName().getText();
245        }
246        public String defaultCase(Node node) {
247            return node.getClass().getSimpleName();
248        }
249    };
250
251
252    /* stringName **********************************************************/
253    public static String stringName(Node the_node) {
254        return the_node.accept(stringNameVisitor);
255    }
256
257    /* stringNames *********************************************************/
258    public static IterableOnce<String> stringNames(LValue lv) {
259        return lv.accept(new NodeAbstractVisitor<IterableOnce<String>>() {
260            public IterableOnce<String> forLValueBind(LValueBind d) {
261                return new UnitIterable<String>(d.getName().getText());
262            }
263            public IterableOnce<String> forUnpastingBind(UnpastingBind d) {
264                return new UnitIterable<String>(d.getName().getText());
265            }
266            public IterableOnce<String> forUnpastingSplit(UnpastingSplit d) {
267                return new IterableOnceForLValueList(d.getElems());
268            }
269        });
270    }
271
272    public static IterableOnce<String> stringNames(AbsDeclOrDecl decl) {
273        return decl.accept(new NodeAbstractVisitor<IterableOnce<String>>() {
274            public IterableOnce<String> forAbsExternalSyntax(AbsExternalSyntax d) {
275                return new UnitIterable<String>(d.getName().getText());
276            }
277            public IterableOnce<String> forDimDecl(DimDecl d) {
278                return new UnitIterable<String>(d.getDim().getText());
279            }
280            public IterableOnce<String> forUnitDecl(UnitDecl d) {
281            List<Id> ids = d.getUnits();
282            if (ids.size() < 1)
283                return bug("Unit declarations should have a name.");
284            else return new UnitIterable<String>(nameString(ids.get(0)));
285            }
286            public IterableOnce<String> forExternalSyntax(ExternalSyntax d) {
287                return new UnitIterable<String>(d.getName().getText());
288            }
289            public IterableOnce<String> forFnExpr(FnExpr d) {
290                return new UnitIterable<String>(nameString(d.getName()));
291            }
292            public IterableOnce<String> forFnAbsDeclOrDecl(FnAbsDeclOrDecl d) {
293                return new UnitIterable<String>(nameString(d.getName()));
294            }
295            public IterableOnce<String> forGeneratedExpr(GeneratedExpr d) {
296                return new UnitIterable<String>("GeneratedExpr");
297            }
298            public IterableOnce<String> forLetFn(LetFn d) {
299                return new UnitIterable<String>(d.getClass().getSimpleName());
300            }
301            public IterableOnce<String> forLocalVarDecl(LocalVarDecl d) {
302                return new IterableOnceForLValueList(d.getLhs());
303            }
304            public IterableOnce<String> forObjectAbsDeclOrDecl(ObjectAbsDeclOrDecl d) {
305                return new UnitIterable<String>(d.getName().getText());
306            }
307            public IterableOnce<String> for_RewriteObjectExpr(_RewriteObjectExpr d) {
308                return new UnitIterable<String>(d.getGenSymName());
309            }
310            public IterableOnce<String> forPropertyDecl(PropertyDecl d) {
311                return d.getName().apply(new OptionVisitor<Id, IterableOnce<String>>() {
312                    public IterableOnce<String> forSome(Id name) {
313                        return new UnitIterable<String>(name.getText());
314                    }
315                    public IterableOnce<String> forNone() {
316                        return new UnitIterable<String>("_");
317                    }
318                });
319            }
320            public IterableOnce<String> forTestDecl(TestDecl d) {
321                return new UnitIterable<String>(d.getName().getText());
322            }
323            public IterableOnce<String> forTraitAbsDeclOrDecl(TraitAbsDeclOrDecl d) {
324                return new UnitIterable<String>(d.getName().getText());
325            }
326            public IterableOnce<String> forTypeAlias(TypeAlias d) {
327                return new UnitIterable<String>(d.getName().getText());
328            }
329            public IterableOnce<String> forVarAbsDeclOrDecl(VarAbsDeclOrDecl d) {
330                return new IterableOnceForLValueList(d.getLhs());
331            }
332            public IterableOnce<String> forGrammarDecl(GrammarDecl d) {
333                return new UnitIterable<String>(d.getName().getText());
334            }
335        });
336    }
337
338    /* dump ****************************************************************/
339    public static String dump(AbstractNode n) {
340        try {
341            StringBuffer sb = new StringBuffer();
342            dump(sb, n);
343            return sb.toString();
344        } catch (Throwable ex) {
345            return "Exception " + ex + " during dump";
346        }
347    }
348
349    /**
350     * @throws IOException
351     */
352    public static void dump(Appendable appendable, AbstractNode n) throws IOException {
353        Printer p = new Printer(true, true, true);
354        p.dump(n, appendable, 0);
355    }
356
357    public static <T> T NYI(String s) {
358        return (T)bug("AST." + s + " NYI");
359    }
360
361    /* function ************************************************************/
362    public static final Fn<Id, String> IdToStringFn = new Fn<Id, String>() {
363            public String apply(Id x) {
364                return stringName(x);
365            }
366        };
367
368    public static final Fn<Name, String> NameToStringFn = new Fn<Name, String>() {
369        public String apply(Name n) { return nameString(n); }
370    };
371
372    public static final Fn<String, Id> StringToIdFn = new Fn<String, Id>() {
373            public Id apply(String x) {
374                return new Id(new Span(), x);
375            }
376        };
377
378    /* for APIName ******************************************************/
379    public static List<String> toStrings(APIName n) {
380        return Useful.applyToAll(n.getIds(), IdToStringFn);
381    }
382
383    /* for TraitTypeWhere **************************************************/
384    public static List<BaseType> getTypes(List<TraitTypeWhere> l) {
385        List<BaseType> t = new ArrayList<BaseType>(l.size());
386        for (TraitTypeWhere tw : l) {
387            t.add(tw.getType());
388        }
389        return t;
390    }
391
392    /* for Type and StaticExpr **********************************************/
393    public static boolean isExponentiation(Type type) {
394        return (type instanceof ArrayType ||
395                type instanceof MatrixType ||
396                type instanceof ExponentType ||
397                type instanceof ExponentDim);
398    }
399    public static boolean isExponentiation(IntExpr staticExpr) {
400        return (staticExpr instanceof ExponentConstraint);
401    }
402}
Note: See TracBrowser for help on using the browser.