typename fix
[prop.git] / prop-src / rwgen5.cc
blobbdd5fb26788cf9a593db562a311bf579fcf8a37b
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 "rwgen5.pcc".
5 ///////////////////////////////////////////////////////////////////////////////
7 #define PROP_QUARK_USED
8 #include <propdefs.h>
9 ///////////////////////////////////////////////////////////////////////////////
10 // Quark literals
11 ///////////////////////////////////////////////////////////////////////////////
12 static const Quark _r_w_g_e_n_5co_c_c_Q1("none");
13 #line 1 "rwgen5.pcc"
14 ///////////////////////////////////////////////////////////////////////////////
16 // This file implements indexing for rewriters
18 ///////////////////////////////////////////////////////////////////////////////
19 #include "basics.h"
20 #include "hashtab.h"
21 #include "rwgen.h"
22 #include "funmap.h"
23 #include "type.h"
24 #include "options.h"
26 ///////////////////////////////////////////////////////////////////////////////
28 // Indexing class
30 ///////////////////////////////////////////////////////////////////////////////
31 RewriteIndexing::RewriteIndexing(Ty t, Id i, Bool e)
32 : ty(t), name(i), external(e) {}
33 RewriteIndexing::~RewriteIndexing() {}
35 ///////////////////////////////////////////////////////////////////////////////
37 // Mapping from id to RewriteClass
39 ///////////////////////////////////////////////////////////////////////////////
40 HashTable RewritingCompiler::rewrite_classes(string_hash, string_equal);
42 ///////////////////////////////////////////////////////////////////////////////
44 // Enter a rewrite class
46 ///////////////////////////////////////////////////////////////////////////////
47 void RewritingCompiler::add_rewrite_class(RewriteClass * C)
48 { if (rewrite_classes.contains(C->class_name)) {
49 error ("%Lrewrite class %s has already been defined\n", C->class_name);
50 } else {
51 rewrite_classes.insert(C->class_name, C);
52 debug_msg ("[Rewriting class %s declared]\n", C->class_name);
56 ///////////////////////////////////////////////////////////////////////////////
58 // Lookup a rewrite class
60 ///////////////////////////////////////////////////////////////////////////////
61 RewriteClass * RewritingCompiler::lookup_rewrite_class(Id id)
62 { HashTable::Entry * e = rewrite_classes.lookup(id);
63 if (e == 0) {
64 error ("%Lrewrite class %s is undefined\n", id);
65 return 0;
66 } else {
67 return (RewriteClass *)rewrite_classes.value(e);
71 Protocols RewritingCompiler::lookup_protocols(Id id)
72 { RewriteClass * C = lookup_rewrite_class(id);
73 return C ? C->protocols :
74 #line 60 "rwgen5.pcc"
75 #line 60 "rwgen5.pcc"
76 nil_1_
77 #line 60 "rwgen5.pcc"
78 #line 60 "rwgen5.pcc"
82 TyQual RewritingCompiler::lookup_qual(Id id)
83 { RewriteClass * C = lookup_rewrite_class(id);
84 return C ? C->qualifiers : QUALnone;
87 ///////////////////////////////////////////////////////////////////////////////
89 // Method to retrieve the indexing information for a type
91 ///////////////////////////////////////////////////////////////////////////////
92 const RewriteIndexing * RewritingCompiler::get_index(Ty ty) const
93 { for_each (RewriteIndexing *, i, indices)
94 { if (ty_equal(i->ty, ty)) return i;
96 return 0;
99 ///////////////////////////////////////////////////////////////////////////////
101 // Method to compute the index
103 ///////////////////////////////////////////////////////////////////////////////
104 Bool RewritingCompiler::compute_index (Ty ty, Id& name, Bool& external) const
105 { Ty datatype_ty = deref_all(ty);
106 Bool is_boxed = boxed_variants(datatype_ty) > 0;
107 Bool rewritable = has_qual(QUALrewritable, datatype_ty);
108 const RewriteIndexing * I = get_index(ty);
109 Bool need_cache = Fmap->has_replacement || Fmap->gen_reducers;
111 Bool ok = is_boxed && (rewritable || I != 0) && need_cache;
113 if (I != 0 && I->name ==
114 #line 94 "rwgen5.pcc"
115 #line 94 "rwgen5.pcc"
116 _r_w_g_e_n_5co_c_c_Q1
117 #line 94 "rwgen5.pcc"
118 #line 94 "rwgen5.pcc"
119 ) ok = false;
121 if (ok)
122 { if (I) { name = I->name; external = I->external; }
123 else { name = "rewrite"; external = false; }
124 return true;
125 } else
126 return false;
129 ///////////////////////////////////////////////////////////////////////////////
131 // Method to check whether a type has an index
133 ///////////////////////////////////////////////////////////////////////////////
134 Bool RewritingCompiler::has_index (Ty ty) const
135 { Id name; Bool external;
136 return compute_index(ty,name,external);
139 ///////////////////////////////////////////////////////////////////////////////
141 // Method to emit code for retrieving the state from an expression
143 ///////////////////////////////////////////////////////////////////////////////
144 Id RewritingCompiler::gen_get_rewrite_state(Ty ty, Exp pat_exp)
145 { Id name; Bool external;
146 if (! compute_index(ty,name,external))
147 { bug("%Lexpression %e : %T has no index", pat_exp, ty); }
148 int units = unboxed_variants(ty);
149 int args = boxed_variants(ty);
150 Functor f = Fmap->type_map[ty];
151 Id state_var = vars.new_label();
153 if (units == 0)
154 { pr("%^int %s = ", state_var);
155 if (external) pr("get_%s_state(%e);", name, pat_exp);
156 else pr("%e->get_%s_state();", pat_exp, name);
157 } else if (args == 0)
158 { pr("%^int %s = %e + %i;", state_var, pat_exp, f);
159 } else
160 { Id pat_var = vars.new_label();
161 pr("%^%t %s = %e;"
162 "%^int %s = boxed(%s) ? ",
163 ty, "", pat_var, pat_exp, state_var, pat_var);
164 if (external) pr("get_%s_state(%s)", name, pat_var);
165 else pr("%s->get_%s_state()", pat_var, name);
166 pr(" : ((int)%s + %i);", pat_var, f);
168 return state_var;
171 ///////////////////////////////////////////////////////////////////////////////
173 // Method to emit code for retrieving the state from index
175 ///////////////////////////////////////////////////////////////////////////////
176 void RewritingCompiler::gen_get_rewrite_state(Ty ty, Id redex)
177 { Id name; Bool external;
178 if (! compute_index(ty,name,external)) return;
179 if (external)
180 { pr("%^int cached__;"
181 "%^if (r__ && boxed(redex) && (cached__ = get_%s_state(%s)) != BURS::undefined_state)"
182 "%^{ s__ = cached__; return%s; }",
183 name, redex, (Fmap->is_applicative ? " redex" : ""));
184 } else
185 { pr("%^int cached__;"
186 "%^if (r__ && boxed(redex) && (cached__ = %s->get_%s_state()) != BURS::undefined_state)"
187 "%^{ s__ = cached__; return%s; }",
188 redex, name, (Fmap->is_applicative ? " redex" : ""));
192 ///////////////////////////////////////////////////////////////////////////////
194 // Method to emit code for setting the index state
196 ///////////////////////////////////////////////////////////////////////////////
197 void RewritingCompiler::gen_set_rewrite_state_and_rule(Ty ty, Id redex)
198 { Id name; Bool external;
199 if (! compute_index(ty,name,external)) return;
200 if (external)
201 { pr("%^if (boxed(redex)) {%+"
202 "%^set_%s_state(%s,s__);",name,redex);
203 if (Fmap->gen_reducers)
204 pr("%^set_%s_rule(%s,rule__);",name,redex);
205 pr("%-%^}");
206 } else
207 { pr("%^if (boxed(redex)) {%+"
208 "%^%s->set_%s_state(s__);",redex,name);
209 if (Fmap->gen_reducers)
210 pr("%^%s->set_%s_rule(rule__);",redex,name);
211 pr("%-%^}");
215 ///////////////////////////////////////////////////////////////////////////////
217 // Method to emit code for retrieving the cached rule
219 ///////////////////////////////////////////////////////////////////////////////
220 void RewritingCompiler::gen_get_rewrite_rule(Ty ty, Id redex)
221 { Id name; Bool external;
222 if (! compute_index(ty,name,external))
223 { error("%Ltype %T is no index defined in treeparser mode rewrite class %s\n",
224 ty, Fmap->class_name);
225 return;
227 if (external)
228 { pr("get_%s_rule(%s)", name, redex);
229 } else
230 { pr("%s->get_%s_rule()", redex, name);
233 #line 208 "rwgen5.pcc"
235 ------------------------------- Statistics -------------------------------
236 Merge matching rules = yes
237 Number of DFA nodes merged = 0
238 Number of ifs generated = 0
239 Number of switches generated = 0
240 Number of labels = 0
241 Number of gotos = 0
242 Adaptive matching = enabled
243 Fast string matching = disabled
244 Inline downcasts = enabled
245 --------------------------------------------------------------------------