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
00038 #ifndef CBZ_CHANGER_H
00039 #define CBZ_CHANGER_H
00040
00143 class Changer {
00144
00145 public:
00146
00147 typedef enum { Preorder, Postorder, Both } Order;
00148 typedef enum { Subtree, NodeOnly } Depth;
00149
00150 private:
00151
00154 Order _order;
00155
00158 Depth _depth;
00159
00162 bool _delete_old;
00163
00164 public:
00165
00179 Changer(Order the_order, Depth depth, bool delete_old)
00180 : _order(the_order),
00181 _depth(depth),
00182 _delete_old(delete_old)
00183 {}
00188
00189
00196 inline Order order() const { return _order; }
00197
00204 inline Depth depth() const { return _depth; }
00205
00209 inline bool delete_old() const { return _delete_old; }
00210
00212
00220
00221
00222
00223 virtual Node * at_node(Node * the_node, Order ord)
00224 { return the_node; }
00225
00226 virtual Node * at_unit(unitNode * the_unit, Order ord)
00227 { return at_node(the_unit, ord); }
00228
00229 virtual Node * at_def(defNode * the_def, Order ord)
00230 { return at_node(the_def, ord); }
00231
00232 virtual Node * at_decl(declNode * the_decl, Order ord)
00233 { return at_def(the_decl, ord); }
00234
00235 virtual Node * at_subdecl(subdeclNode * the_subdecl, Order ord)
00236 { return at_decl(the_subdecl, ord); }
00237
00238 virtual Node * at_proc(procNode * the_proc, Order ord)
00239 { return at_def(the_proc, ord); }
00240
00241 virtual Node * at_type(typeNode * the_type, Order ord)
00242 { return at_node(the_type, ord); }
00243
00244 virtual Node * at_prim(primNode * the_prim, Order ord)
00245 { return at_type(the_prim, ord); }
00246
00247 virtual Node * at_tdef(tdefNode * the_tdef, Order ord)
00248 { return at_type(the_tdef, ord); }
00249
00250 virtual Node * at_ptr(ptrNode * the_ptr, Order ord)
00251 { return at_type(the_ptr, ord); }
00252
00253 virtual Node * at_array(arrayNode * the_array, Order ord)
00254 { return at_type(the_array, ord); }
00255
00256 virtual Node * at_func(funcNode * the_func, Order ord)
00257 { return at_type(the_func, ord); }
00258
00259 virtual Node * at_sue(sueNode * the_sue, Order ord)
00260 { return at_type(the_sue, ord); }
00261
00262 virtual Node * at_struct(structNode * the_struct, Order ord)
00263 { return at_sue(the_struct, ord); }
00264
00265 virtual Node * at_union(unionNode * the_union, Order ord)
00266 { return at_sue(the_union, ord); }
00267
00268 virtual Node * at_enum(enumNode * the_enum, Order ord)
00269 { return at_sue(the_enum, ord); }
00270
00271 virtual Node * at_suespec(suespecNode * the_suespec, Order ord)
00272 { return at_type(the_suespec, ord); }
00273
00274 virtual Node * at_expr(exprNode * the_expr, Order ord)
00275 { return at_node(the_expr, ord); }
00276
00277 virtual Node * at_index(indexNode * the_index, Order ord)
00278 { return at_expr(the_index, ord); }
00279
00280 virtual Node * at_const(constNode * the_const, Order ord)
00281 { return at_index(the_const, ord); }
00282
00283 virtual Node * at_id(idNode * the_id, Order ord)
00284 { return at_index(the_id, ord); }
00285
00286 virtual Node * at_binary(binaryNode * the_binary, Order ord)
00287 { return at_expr(the_binary, ord); }
00288
00289 virtual Node * at_unary(unaryNode * the_unary, Order ord)
00290 { return at_expr(the_unary, ord); }
00291
00292 virtual Node * at_cast(castNode * the_cast, Order ord)
00293 { return at_expr(the_cast, ord); }
00294
00295 virtual Node * at_comma(commaNode * the_comma, Order ord)
00296 { return at_expr(the_comma, ord); }
00297
00298 virtual Node * at_ternary(ternaryNode * the_ternary, Order ord)
00299 { return at_expr(the_ternary, ord); }
00300
00301 virtual Node * at_call(callNode * the_call, Order ord)
00302 { return at_expr(the_call, ord); }
00303
00304 virtual Node * at_initializer(initializerNode * the_initializer, Order ord)
00305 { return at_expr(the_initializer, ord); }
00306
00307 virtual Node * at_stmt(stmtNode * the_stmt, Order ord)
00308 { return at_node(the_stmt, ord); }
00309
00310 virtual Node * at_block(blockNode * the_block, Order ord)
00311 { return at_stmt(the_block, ord); }
00312
00313 virtual Node * at_basicblock(basicblockNode * the_basicblock, Order ord)
00314 { return at_block(the_basicblock, ord); }
00315
00316 virtual Node * at_exprstmt(exprstmtNode * the_exprstmt, Order ord)
00317 { return at_stmt(the_exprstmt, ord); }
00318
00319 virtual Node * at_target(targetNode * the_target, Order ord)
00320 { return at_stmt(the_target, ord); }
00321
00322 virtual Node * at_label(labelNode * the_label, Order ord)
00323 { return at_target(the_label, ord); }
00324
00325 virtual Node * at_case(caseNode * the_case, Order ord)
00326 { return at_target(the_case, ord); }
00327
00328 virtual Node * at_selection(selectionNode * the_selection, Order ord)
00329 { return at_stmt(the_selection, ord); }
00330
00331 virtual Node * at_if(ifNode * the_if, Order ord)
00332 { return at_selection(the_if, ord); }
00333
00334 virtual Node * at_switch(switchNode * the_switch, Order ord)
00335 { return at_selection(the_switch, ord); }
00336
00337 virtual Node * at_loop(loopNode * the_loop, Order ord)
00338 { return at_stmt(the_loop, ord); }
00339
00340 virtual Node * at_while(whileNode * the_while, Order ord)
00341 { return at_loop(the_while, ord); }
00342
00343 virtual Node * at_do(doNode * the_do, Order ord)
00344 { return at_loop(the_do, ord); }
00345
00346 virtual Node * at_for(forNode * the_for, Order ord)
00347 { return at_loop(the_for, ord); }
00348
00349 virtual Node * at_jump(jumpNode * the_jump, Order ord)
00350 { return at_stmt(the_jump, ord); }
00351
00352 virtual Node * at_goto(gotoNode * the_goto, Order ord)
00353 { return at_jump(the_goto, ord); }
00354
00355 virtual Node * at_continue(continueNode * the_continue, Order ord)
00356 { return at_jump(the_continue, ord); }
00357
00358 virtual Node * at_break(breakNode * the_break, Order ord)
00359 { return at_jump(the_break, ord); }
00360
00361 virtual Node * at_return(returnNode * the_return, Order ord)
00362 { return at_jump(the_return, ord); }
00363
00364 virtual Node * at_attrib(attribNode * the_attrib, Order ord)
00365 { return at_stmt(the_attrib, ord); }
00366
00367 virtual Node * at_operand(operandNode * the_oper, Order ord)
00368 { return at_expr(the_oper, ord); }
00369
00370 virtual Node * at_conditiongoto(conditiongotoNode * the_condgoto, Order ord)
00371 { return at_goto(the_condgoto, ord); }
00372
00373 virtual Node * at_threeAddr(threeAddrNode * the_3addr, Order ord)
00374 { return at_stmt(the_3addr, ord); }
00375
00376 virtual Node * at_text(textNode * the_text, Order ord)
00377 { return at_node(the_text, ord); }
00379
00380 private:
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 };
00396
00403 template< class T >
00404 void change_list(list< T > & l, Changer & the_changer)
00405 {
00406 typename list< T >::iterator p = l.begin();
00407
00408 while (p != l.end()) {
00409
00410 Node * n = (*p)->change(the_changer);
00411
00412 if (!n) {
00413
00414
00415
00416 typename list< T >::iterator q = p;
00417 p++;
00418 l.erase(q);
00419 }
00420 else {
00421
00422 if (n != (*p)) {
00423
00424
00425
00426 if (the_changer.delete_old())
00427 delete (*p);
00428
00429 (*p) = (T) n;
00430 }
00431
00432 p++;
00433 }
00434 }
00435 }
00436
00437
00438 #endif // CBZ_CHANGER_H