1 ///////////////////////////////////////////////////////////////////////////////
3 // This file contains the abstract syntax tree definitions for the
4 // Prop language. For simplicity, C++ statements are currently not
5 // handled directly in the AST.
7 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef abstract_syntax_tree_h
9 #define abstract_syntax_tree_h
14 ///////////////////////////////////////////////////////////////////////////////
16 // Options to the pattern matching compiler.
18 ///////////////////////////////////////////////////////////////////////////////
19 enum { MATCHnone = 0, // no option (select first matching rule)
20 MATCHall = 1<<0, // select all matching rules
21 MATCHwhile = 1<<1, // loop back
22 MATCHnocheck = 1<<2, // no checking
23 MATCHwithcost = 1<<3, // with cost minimization
24 MATCHwithintcost = 1<<4, // with integer costs
25 MATCHwithexpcost = 1<<5, // with expression costs
26 MATCHwithtreecost = 1<<6, // use cumulative cost
27 MATCHrefutable = 1<<7, // pattern is refutable
28 MATCHscanner = 1<<8, // lexical scanner
29 MATCHcaseinsensitive = 1<<9, // insensitive case during string matching
30 MATCHlexemepat = 1<<10, // with lexeme pattern
31 MATCHnotrace = 1<<11, // no tracing
32 MATCHunification = 1<<12 // unification mode
35 ///////////////////////////////////////////////////////////////////////////////
37 // Forward type declarations.
38 // These are defined in other places.
40 ///////////////////////////////////////////////////////////////////////////////
41 datatype Ty // type expression
43 and Cons // constructor info
44 and Literal // literals
45 and GramExp // grammar expressions
46 and FieldDef // bitfield definition
47 and FieldLaw // bitfield law
48 and Stmt // statements
49 and Def // definitions
50 and Cost // cost expression
51 and ConstraintSet // logical constraint rule set
52 and GraphRewritingRule // a graph rewriting rule
55 class ClassDefinition;
56 class RewriteIndexing;
58 ///////////////////////////////////////////////////////////////////////////////
60 // Information on a match rule.
62 ///////////////////////////////////////////////////////////////////////////////
63 class MatchRuleInfo : public Loc
64 { MatchRuleInfo (const MatchRuleInfo&); // no copy constructor
65 void operator = (const MatchRuleInfo&); // no assignment
67 Bool used; // is the rule being used?
68 Ty ty; // type of pattern
69 int rule_number; // rule number
70 Bool negated; // negation in inference?
71 Bool rewriting; // A rewriting pattern?
72 Bool is_chain_rule; // Chain rule?
74 { BEFORE, PREORDER, POSTORDER, TOPDOWN, BOTTOMUP, LAST_REWRITING_MODE
75 } mode; // rewriting mode
77 { NO_OPTIONS = 0, CUTREWRITE = 1, REPLACEMENT = 2, FAILREWRITE = 4 };
78 typedef int RewritingOption;
79 RewritingOption option;
84 ///////////////////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////////////////
89 datatype Exp : public MEM =
90 ////////////////////////////////////////////////////////////////////////////
92 // Basic expressions. These are mostly generated by the parser.
94 ////////////////////////////////////////////////////////////////////////////
95 NOexp // no expression.
96 | LITERALexp (Literal) // literal expression.
97 | IDexp (Id) // identifier
98 | RELexp int // nth relation during inference
99 | DOTexp (Exp, Id) // e . l
100 | SELECTORexp(Exp, Cons, Ty) // selector from a constructor
101 | DEREFexp (Exp) // * e
102 | ARROWexp (Exp, Id) // e -> l
103 | INDEXexp (Exp, Exp) // e1 [ e2 ]
104 | BINOPexp (Id, Exp, Exp) // e1 op e2
105 | PREFIXexp (Id, Exp) // op e
106 | POSTFIXexp (Id, Exp) // e op
107 | APPexp (Exp, Exp) // e1(e2)
108 | ASSIGNexp (Exp, Exp) // e1 := e2
109 | IFexp (Exp, Exp, Exp) // e1 ? e2 : e3
110 | TUPLEexp (List<Exp>) // (e,...,e)
111 | EXTUPLEexp (List<Exp>) // .(e,...,e)
112 | RECORDexp (List<LabExp>) // { x = e1, y = e2, ... z = en }
113 | LISTexp (Cons, Cons, List<Exp>, Exp) // #[1,2,3,4,5]
114 | VECTORexp (Cons, List<Exp>) // [| 1, 2, 3, 4, 5 |]
115 | CONSexp (Cons, List<Exp>, Exp) // cons(e)
116 | CASTexp (Ty,Exp) // casts
117 | QUALexp (Ty, Id) // qualified expression ty :: id
119 ////////////////////////////////////////////////////////////////////////////
121 // Extensions to deal with pattern matching, rewriting and inference.
122 // Most of these are generated internally.
124 ////////////////////////////////////////////////////////////////////////////
125 | EQexp (Ty, Exp, Exp) // equality expression
126 | UNIFYexp (Ty, Exp, Exp) // unification expression
127 | LTexp (Ty, Exp, Exp) // less than expression
128 | HASHexp (Ty, Exp) // hashing expression
129 | THISCOSTexp() // current cost
130 | COSTexp (ChildNo) // cost attribute of child
131 | THISSYNexp (RuleNo,Ty,Bool) // current synthesized attribue
132 | SYNexp (ChildNo, RuleNo,Ty,Bool) // synthesized attribute of child
133 | SENDexp (Id, List<Exp>) // [| email sendTo: recipient |]
135 ////////////////////////////////////////////////////////////////////////////
137 // Extensions to deal with the SETL-like sublanguage.
139 ////////////////////////////////////////////////////////////////////////////
140 | SETLexp (SETLOp, Exps) // SETL-like operator expression.
141 | LISTCOMPexp { exp : Exp, // list comprehension
142 generators : List<Generator>,
145 | FORALLexp (Id, Exp) // universal quantifier
146 | EXISTSexp (Id, Exp) // existential quantifier
148 ////////////////////////////////////////////////////////////////////////////
150 // Miscellaneous expressions.
152 ////////////////////////////////////////////////////////////////////////////
153 | MARKEDexp (Loc, Exp) // expressions marked with the location.
159 ///////////////////////////////////////////////////////////////////////////////
161 // SETL-like operators
163 ///////////////////////////////////////////////////////////////////////////////
165 ARBop // extract arbitrary element
168 | CARDop // cardinality
170 | WITHASSIGNop // with :=
172 | LESSASSIGNop // less :=
174 ///////////////////////////////////////////////////////////////////////////////
176 // A generator expression: pat | guard <- exp
178 ///////////////////////////////////////////////////////////////////////////////
179 and Generator : public Loc =
180 GENERATOR { pat : Pat, // bindings
181 guard : Exp, // guard expression, if any
182 exp : Exp // a collection object
185 ///////////////////////////////////////////////////////////////////////////////
187 // Pattern matching expressions.
189 ///////////////////////////////////////////////////////////////////////////////
190 and MatchExp : public Loc = MATCHexp (Exp, Id)
192 ///////////////////////////////////////////////////////////////////////////////
194 // Pattern matching rules.
196 ///////////////////////////////////////////////////////////////////////////////
197 and MatchRule : public MatchRuleInfo =
198 MATCHrule (Id, Pat, Exp, Cost, List<Decl>)
199 // nonterminal -> pat | guard: action
201 ///////////////////////////////////////////////////////////////////////////////
203 // Prop declarations/specifications
205 ///////////////////////////////////////////////////////////////////////////////
206 and Decl : public MEM =
208 | OPAQUEdecl (const char *)
210 // datatype and instantiation declarations
211 | DATATYPEdecl (DatatypeDefs, ViewDefs, LawDefs, TyDefs)
212 | INSTANTIATEdecl (Bool, Tys)
214 // special class definition
215 | CLASSDEFdecl (ClassDefinition *)
217 // inference rules declarations
218 | INFERENCEdecl (Id, InferenceRules)
220 // rewriting rule declarations
221 | REWRITEdecl (Id, RewriteIndexings, MatchRules)
222 | REWRITINGdecl (Protocols, Exp, Exp, RewriteIndexings,
224 | REPLACEMENTdecl (Exp, MatchRuleInfo::RewritingMode)
225 | CUTREWRITEdecl (Exp, MatchRuleInfo::RewritingMode)
226 | FAILREWRITEdecl (MatchRuleInfo::RewritingMode)
227 | INJECTdecl { node_number : int,
228 direction : EntryDirection
233 // constraint class and constraint declarations
234 | CONSTRAINTdecl (Id, ConstraintSet)
236 // syntax class and grammar declarations
237 | SYNTAXdecl (Id, GramExp)
238 | ATTRIBUTEGRAMMARdecl(Id, GramExp)
240 // functions and pattern matching definitions
242 | MATCHdecl (MatchExps, MatchRules, MatchOptions, Ty)
244 // bitfield declarations
245 | BITFIELDdecl { name : Id,
247 field_names : FieldDefs,
251 // SETL-style statements and definitions
252 | SETLSTMTdecl (Stmt)
255 // graphrewrite declarations
256 | GRAPHREWRITEdecl { name : Id,
258 rules : GraphRewritingRules
261 // dataflow declarations
262 | DATAFLOWdecl { name : Id
267 | EXPdecl { exp : Exp,
268 prefix : const char * = 0,
269 suffix : const char * = 0
273 | MARKEDdecl (Loc, Decl)
275 ///////////////////////////////////////////////////////////////////////////////
277 // Direction of entry into the RETE network
279 ///////////////////////////////////////////////////////////////////////////////
280 and EntryDirection = LEFTdirection | RIGHTdirection
282 ///////////////////////////////////////////////////////////////////////////////
284 // Protocol of a rewrite type defines the set inherited and synthesized
285 // attributes for a rewrite type.
287 ///////////////////////////////////////////////////////////////////////////////
288 and Protocol : public Loc = PROTOCOL { ty : Ty, inh : Ty, syn : Ty }
290 ///////////////////////////////////////////////////////////////////////////////
292 // Datatype definitions.
294 ///////////////////////////////////////////////////////////////////////////////
295 and DatatypeDef : public Loc =
296 DATATYPEdef (Id, TyVars, Inherits, TyQual, TermDefs, Decls)
298 ///////////////////////////////////////////////////////////////////////////////
300 // Definition of a constructor term.
302 ///////////////////////////////////////////////////////////////////////////////
303 and TermDef : public Loc =
307 inherits : Inherits = #[],
309 print_formats : PrintFormats = #[],
310 opt : TyOpt = OPTnone,
311 qual : TyQual = QUALnone,
312 view_predicate : Exp = NOexp
315 ///////////////////////////////////////////////////////////////////////////////
317 // Definition of a datatype view
319 ///////////////////////////////////////////////////////////////////////////////
320 and ViewDef : public Loc = VIEWdef(Pat, Exp, List<LabExp>)
322 ///////////////////////////////////////////////////////////////////////////////
324 // Definition of a pattern law.
326 ///////////////////////////////////////////////////////////////////////////////
327 and LawDef : public Loc =
336 ///////////////////////////////////////////////////////////////////////////////
340 ///////////////////////////////////////////////////////////////////////////////
341 and TyDef : public Loc = TYdef (Id, TyVars, Ty, Bool)
343 ///////////////////////////////////////////////////////////////////////////////
345 // Function definitions.
347 ///////////////////////////////////////////////////////////////////////////////
348 and FunDef : public Loc = FUNdef (QualId, Ty, Ty, MatchRules)
350 ///////////////////////////////////////////////////////////////////////////////
354 ///////////////////////////////////////////////////////////////////////////////
355 and QualId : public MEM = NESTEDid (Ty, QualId)
358 ///////////////////////////////////////////////////////////////////////////////
362 ///////////////////////////////////////////////////////////////////////////////
363 and InferenceRule : public Loc =
364 INFERENCErule(MatchRules, Exp, Conclusions)
366 and Conclusion : public Loc = ASSERTaction (Exp)
367 | RETRACTaction (Exp)
370 ///////////////////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////////////////
375 where type MatchExps = List<MatchExp>
376 and MatchRules = List<MatchRule>
377 and Decls = List<Decl>
378 and DatatypeDefs = List<DatatypeDef>
379 and ViewDefs = List<ViewDef>
380 and LawDefs = List<LawDef>
381 and TyDefs = List<TyDef>
382 and TermDefs = List<TermDef>
383 and FunDefs = List<FunDef>
384 and Protocols = List<Protocol>
385 and LabExp = { label : Id, exp : Exp }
386 and LabMatchRule = { id : QualId, rule : MatchRule, return_ty : Ty }
387 and LabMatchRules = { id : QualId, rules : MatchRules, return_ty : Ty }
389 and LabExps = List<LabExp>
390 and InferenceRules = List<InferenceRule>
391 and Conclusions = List<Conclusion>
392 and GraphRewritingRules = List<GraphRewritingRule>
393 and FieldDefs = List<FieldDef>
394 and FieldLaws = List<FieldLaw>
397 and MatchOptions = int
398 and RewriteIndexings = List<RewriteIndexing *>
401 ///////////////////////////////////////////////////////////////////////////////
403 // AST manipulation methods.
405 ///////////////////////////////////////////////////////////////////////////////
406 extern Exp component_exp(Exps, int);
407 extern Exp component_exp(Exps, Id);
408 extern Exp component_exp(LabExps, Id);
410 ///////////////////////////////////////////////////////////////////////////////
412 // Pretty printing methods.
414 ///////////////////////////////////////////////////////////////////////////////
415 extern ostream& operator << (ostream&, Exp);
416 extern ostream& operator << (ostream&, List<Exp>);
417 extern ostream& operator << (ostream&, LabExp);
418 extern ostream& operator << (ostream&, List<LabExp>);
419 extern ostream& operator << (ostream&, Cost);
420 extern ostream& operator << (ostream&, MatchRule);
421 extern ostream& operator << (ostream&, QualId);