make it compile with modern C++
[prop.git] / prop-src / rwgen4.cc
blobc5299c92aa326c033587da613a8ad97da737b5fd
1 ///////////////////////////////////////////////////////////////////////////////
2 // This file is generated automatically using Prop (version 2.3.6),
3 // last updated on Nov 2, 1999.
4 // The original source file is "rwgen4.pcc".
5 ///////////////////////////////////////////////////////////////////////////////
7 #define PROP_STRCMP_USED
8 #include <propdefs.h>
9 #line 1 "rwgen4.pcc"
10 ///////////////////////////////////////////////////////////////////////////////
12 // This file implements the tree rewriting/tree parsing compiler.
13 // This is used to implement the rewrite class/rewrite constructs of Prop.
15 ///////////////////////////////////////////////////////////////////////////////
16 #include <iostream>
17 #include <strstream>
18 #include "funmap.h"
19 #include "ir.h"
20 #include "ast.h"
21 #include "matchcom.h"
22 #include "type.h"
23 #include "hashtab.h"
24 #include "list.h"
25 #include "rwgen.h"
26 #include "trs.h"
28 ///////////////////////////////////////////////////////////////////////////////
30 // Method to generate a set of pattern matching rules for a labeler
32 ///////////////////////////////////////////////////////////////////////////////
33 void RewritingCompiler::gen_auxiliary_rules
34 (FunctorMap& F, Ty ty, MatchRuleInfo::RewritingMode mode)
35 { if (F.rule_maps[mode]->contains(ty))
36 { MatchRules rules = rev(MatchRules((*F.rule_maps[mode])[ty]));
37 debug_msg("%Lgenerating auxiliary rules for rewrite class %s type %T:\n",
38 F.class_name, ty);
39 for_each(MatchRule,r,rules) debug_msg("%!\t%r\n",r->loc(),r);
40 gen_match_stmt(
41 #line 31 "rwgen4.pcc"
42 #line 31 "rwgen4.pcc"
43 list_1_(MATCHexp(IDexp(redex_name(ty)),0))
44 #line 31 "rwgen4.pcc"
45 #line 31 "rwgen4.pcc"
46 ,rules,MATCHnocheck);
50 ///////////////////////////////////////////////////////////////////////////////
52 // Method to generate the before rules of a labeler
54 ///////////////////////////////////////////////////////////////////////////////
55 void RewritingCompiler::gen_before_rules(FunctorMap& F, Ty ty)
56 { gen_auxiliary_rules(F,ty,MatchRuleInfo::BEFORE);
59 ///////////////////////////////////////////////////////////////////////////////
61 // Method to generate the preorder rules of a labeler
63 ///////////////////////////////////////////////////////////////////////////////
64 void RewritingCompiler::gen_preorder_rules(FunctorMap& F, Ty ty)
65 { gen_auxiliary_rules(F,ty,MatchRuleInfo::PREORDER);
68 ///////////////////////////////////////////////////////////////////////////////
70 // Method to generate the postorder rules of a labeler
72 ///////////////////////////////////////////////////////////////////////////////
73 void RewritingCompiler::gen_postorder_rules(FunctorMap& F, Ty ty)
74 { gen_auxiliary_rules(F,ty,MatchRuleInfo::POSTORDER);
77 ///////////////////////////////////////////////////////////////////////////////
79 // Method to generate the topdown rules of a labeler
81 ///////////////////////////////////////////////////////////////////////////////
82 void RewritingCompiler::gen_topdown_rules(FunctorMap& F, Ty ty)
83 { gen_auxiliary_rules(F,ty,MatchRuleInfo::TOPDOWN);
86 ///////////////////////////////////////////////////////////////////////////////
88 // Method to generate a tracing macro
90 ///////////////////////////////////////////////////////////////////////////////
91 void RewritingCompiler::gen_trace_macro(Id name, FunctorMap& F)
93 pr("%n%/"
94 "%n//"
95 "%n// This macro can be redefined by the user for debugging"
96 "%n//"
97 "%n%/"
98 "%n#ifndef DEBUG_%S"
99 "%n#define DEBUG_%S(repl,redex,file,line,rule) repl"
100 "%n#else"
101 "%nstatic const char * %S_file_name = \"%s\";"
102 "%n#endif\n\n",
103 name, name, name, file
107 ///////////////////////////////////////////////////////////////////////////////
109 // Method to find out whether the replacement term is simple.
110 // A replacement term is simple if it is a projection/selection from
111 // the redex, or the redex itself.
113 ///////////////////////////////////////////////////////////////////////////////
114 Bool is_simple_replacement(Exp exp)
116 #line 100 "rwgen4.pcc"
117 #line 105 "rwgen4.pcc"
119 for (;;) {
120 if (exp) {
121 switch (exp->tag__) {
122 case a_Exp::tag_IDexp: {
123 if (_equal_string(((Exp_IDexp *)exp)->IDexp,"redex")) {
124 #line 101 "rwgen4.pcc"
125 return true;
126 #line 101 "rwgen4.pcc"
128 else {
129 L2:;
130 #line 105 "rwgen4.pcc"
131 return false;
132 #line 105 "rwgen4.pcc"
134 } break;
135 case a_Exp::tag_DOTexp: {
136 if (((Exp_DOTexp *)exp)->_1) {
137 switch (((Exp_DOTexp *)exp)->_1->tag__) {
138 case a_Exp::tag_SELECTORexp: {
139 #line 102 "rwgen4.pcc"
140 exp = ((Exp_SELECTORexp *)((Exp_DOTexp *)exp)->_1)->_1;
141 #line 102 "rwgen4.pcc"
142 } break;
143 default: { goto L2; } break;
145 } else { goto L2; }
146 } break;
147 case a_Exp::tag_SELECTORexp: {
148 #line 103 "rwgen4.pcc"
149 exp = ((Exp_SELECTORexp *)exp)->_1;
150 #line 103 "rwgen4.pcc"
151 } break;
152 case a_Exp::tag_MARKEDexp: {
153 #line 104 "rwgen4.pcc"
154 exp = ((Exp_MARKEDexp *)exp)->_2;
155 #line 104 "rwgen4.pcc"
156 } break;
157 default: { goto L2; } break;
159 } else { goto L2; }
162 #line 106 "rwgen4.pcc"
163 #line 106 "rwgen4.pcc"
167 ///////////////////////////////////////////////////////////////////////////////
169 // Method to generate a replacement statement. We'll optimize all
170 // bottomup and after rewriting rules.
172 ///////////////////////////////////////////////////////////////////////////////
173 void RewritingCompiler::gen_replacement
174 (Exp exp, MatchRuleInfo::RewritingMode mode)
175 { Bool optimized =
176 trs && trs->gen_replacement(*this,current_rule->rule_number,exp);
177 pr("%^{ redex = DEBUG_%S(%e,redex,%S_file_name,%i,%s);",
178 Fmap->class_name, (optimized ? IDexp("repl__") : exp),
179 Fmap->class_name, current_rule_line(), current_rule_text()
181 pr ("%^ r__ = 1; goto replacement__; }");
184 ///////////////////////////////////////////////////////////////////////////////
186 // Method to generate a failrewrite statement.
188 ///////////////////////////////////////////////////////////////////////////////
189 void RewritingCompiler::gen_failrewrite (MatchRuleInfo::RewritingMode)
191 pr("%^{ ++o__; goto accept__; }");
194 ///////////////////////////////////////////////////////////////////////////////
196 // Method to generate a cut/replacement statement
198 ///////////////////////////////////////////////////////////////////////////////
199 void RewritingCompiler::gen_cutreplacement
200 (Exp exp, MatchRuleInfo::RewritingMode mode)
201 { if (Fmap == 0)
202 { error("%Lcutrewrite is not used within a rewrite class: cutrewrite %e\n",
203 exp);
204 return;
206 if (Fmap->is_applicative)
207 { if (exp == NOexp)
208 pr("%^{ s__ = 0; return redex; }");
209 else
210 pr("%^{ s__ = 0; return DEBUG_%S(%e,redex,%S_file_name,%i,%s); }",
211 Fmap->class_name, exp, Fmap->class_name,
212 current_rule_line(), current_rule_text());
213 } else
214 { if (exp == NOexp)
215 pr("%^{ s__ = 0; return; }");
216 else
217 pr("%^{ s__ = 0; redex = DEBUG_%S(%e,redex);"
218 " return; }",
219 Fmap->class_name, exp, Fmap->class_name,
220 current_rule_line(), current_rule_text());
223 #line 165 "rwgen4.pcc"
225 ------------------------------- Statistics -------------------------------
226 Merge matching rules = yes
227 Number of DFA nodes merged = 71
228 Number of ifs generated = 3
229 Number of switches generated = 2
230 Number of labels = 1
231 Number of gotos = 4
232 Adaptive matching = enabled
233 Fast string matching = disabled
234 Inline downcasts = enabled
235 --------------------------------------------------------------------------