00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef CBZ_CHANGER_H
00038 #define CBZ_CHANGER_H
00039
00142 class Changer {
00143
00144 public:
00145
00146 typedef enum { Preorder, Postorder, Both } Order;
00147 typedef enum { Subtree, NodeOnly } Depth;
00148
00149 private:
00150
00153 Order _order;
00154
00157 Depth _depth;
00158
00161 bool _delete_old;
00162
00163 public:
00164
00178 Changer(Order the_order, Depth depth, bool delete_old)
00179 : _order(the_order),
00180 _depth(depth),
00181 _delete_old(delete_old)
00182 {}
00187
00188
00195 inline Order order() const { return _order; }
00196
00203 inline Depth depth() const { return _depth; }
00204
00208 inline bool delete_old() const { return _delete_old; }
00209
00211
00219
00220
00221
00222 virtual Node * at_node(Node * the_node, Order ord)
00223 { return the_node; }
00224
00225 virtual Node * at_unit(unitNode * the_unit, Order ord)
00226 { return at_node(the_unit, ord); }
00227
00228 virtual Node * at_def(defNode * the_def, Order ord)
00229 { return at_node(the_def, ord); }
00230
00231 virtual Node * at_decl(declNode * the_decl, Order ord)
00232 { return at_def(the_decl, ord); }
00233
00234 virtual Node * at_subdecl(subdeclNode * the_subdecl, Order ord)
00235 { return at_decl(the_subdecl, ord); }
00236
00237 virtual Node * at_proc(procNode * the_proc, Order ord)
00238 { return at_def(the_proc, ord); }
00239
00240 virtual Node * at_type(typeNode * the_type, Order ord)
00241 { return at_node(the_type, ord); }
00242
00243 virtual Node * at_prim(primNode * the_prim, Order ord)
00244 { return at_type(the_prim, ord); }
00245
00246 virtual Node * at_tdef(tdefNode * the_tdef, Order ord)
00247 { return at_type(the_tdef, ord); }
00248
00249 virtual Node * at_ptr(ptrNode * the_ptr, Order ord)
00250 { return at_type(the_ptr, ord); }
00251
00252 virtual Node * at_array(arrayNode * the_array, Order ord)
00253 { return at_type(the_array, ord); }
00254
00255 virtual Node * at_func(funcNode * the_func, Order ord)
00256 { return at_type(the_func, ord); }
00257
00258 virtual Node * at_sue(sueNode * the_sue, Order ord)
00259 { return at_type(the_sue, ord); }
00260
00261 virtual Node * at_struct(structNode * the_struct, Order ord)
00262 { return at_sue(the_struct, ord); }
00263
00264 virtual Node * at_union(unionNode * the_union, Order ord)
00265 { return at_sue(the_union, ord); }
00266
00267 virtual Node * at_enum(enumNode * the_enum, Order ord)
00268 { return at_sue(the_enum, ord); }
00269
00270 virtual Node * at_suespec(suespecNode * the_suespec, Order ord)
00271 { return at_type(the_suespec, ord); }
00272
00273 virtual Node * at_expr(exprNode * the_expr, Order ord)
00274 { return at_node(the_expr, ord); }
00275
00276 virtual Node * at_const(constNode * the_const, Order ord)
00277 { return at_expr(the_const, ord); }
00278
00279 virtual Node * at_id(idNode * the_id, Order ord)
00280 { return at_expr(the_id, ord); }
00281
00282 virtual Node * at_binary(binaryNode * the_binary, Order ord)
00283 { return at_expr(the_binary, ord); }
00284
00285 virtual Node * at_unary(unaryNode * the_unary, Order ord)
00286 { return at_expr(the_unary, ord); }
00287
00288 virtual Node * at_cast(castNode * the_cast, Order ord)
00289 { return at_expr(the_cast, ord); }
00290
00291 virtual Node * at_comma(commaNode * the_comma, Order ord)
00292 { return at_expr(the_comma, ord); }
00293
00294 virtual Node * at_ternary(ternaryNode * the_ternary, Order ord)
00295 { return at_expr(the_ternary, ord); }
00296
00297 virtual Node * at_call(callNode * the_call, Order ord)
00298 { return at_expr(the_call, ord); }
00299
00300 virtual Node * at_initializer(initializerNode * the_initializer, Order ord)
00301 { return at_expr(the_initializer, ord); }
00302
00303 virtual Node * at_stmt(stmtNode * the_stmt, Order ord)
00304 { return at_node(the_stmt, ord); }
00305
00306 virtual Node * at_block(blockNode * the_block, Order ord)
00307 { return at_stmt(the_block, ord); }
00308
00309 virtual Node * at_basicblock(basicblockNode * the_basicblock, Order ord)
00310 { return at_block(the_basicblock, ord); }
00311
00312 virtual Node * at_exprstmt(exprstmtNode * the_exprstmt, Order ord)
00313 { return at_stmt(the_exprstmt, ord); }
00314
00315 virtual Node * at_target(targetNode * the_target, Order ord)
00316 { return at_stmt(the_target, ord); }
00317
00318 virtual Node * at_label(labelNode * the_label, Order ord)
00319 { return at_target(the_label, ord); }
00320
00321 virtual Node * at_case(caseNode * the_case, Order ord)
00322 { return at_target(the_case, ord); }
00323
00324 virtual Node * at_selection(selectionNode * the_selection, Order ord)
00325 { return at_stmt(the_selection, ord); }
00326
00327 virtual Node * at_if(ifNode * the_if, Order ord)
00328 { return at_selection(the_if, ord); }
00329
00330 virtual Node * at_switch(switchNode * the_switch, Order ord)
00331 { return at_selection(the_switch, ord); }
00332
00333 virtual Node * at_loop(loopNode * the_loop, Order ord)
00334 { return at_stmt(the_loop, ord); }
00335
00336 virtual Node * at_while(whileNode * the_while, Order ord)
00337 { return at_loop(the_while, ord); }
00338
00339 virtual Node * at_do(doNode * the_do, Order ord)
00340 { return at_loop(the_do, ord); }
00341
00342 virtual Node * at_for(forNode * the_for, Order ord)
00343 { return at_loop(the_for, ord); }
00344
00345 virtual Node * at_jump(jumpNode * the_jump, Order ord)
00346 { return at_stmt(the_jump, ord); }
00347
00348 virtual Node * at_goto(gotoNode * the_goto, Order ord)
00349 { return at_jump(the_goto, ord); }
00350
00351 virtual Node * at_continue(continueNode * the_continue, Order ord)
00352 { return at_jump(the_continue, ord); }
00353
00354 virtual Node * at_break(breakNode * the_break, Order ord)
00355 { return at_jump(the_break, ord); }
00356
00357 virtual Node * at_return(returnNode * the_return, Order ord)
00358 { return at_jump(the_return, ord); }
00359
00360 virtual Node * at_attrib(attribNode * the_attrib, Order ord)
00361 { return at_stmt(the_attrib, ord); }
00362
00363 virtual Node * at_text(textNode * the_text, Order ord)
00364 { return at_node(the_text, ord); }
00366
00367 private:
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 };
00386
00393 template< class T >
00394 void change_list(list< T > & l, Changer & the_changer)
00395 {
00396 for (list< T >::iterator p = l.begin();
00397 p != l.end(); ++p)
00398 {
00399 Node * n = (*p)->change(the_changer);
00400 if (n != (*p)) {
00401
00402 if (the_changer.delete_old())
00403 delete (*p);
00404
00405 if (n)
00406 (*p) = (T) n;
00407 else {
00408 list< T >::iterator q = p;
00409 l.erase(q);
00410 }
00411 }
00412 }
00413 }
00414
00415
00416 #endif // CBZ_CHANGER_H