C-Breeze
C Compiler Infrastructure

[ Project home page]

walker.h

Go to the documentation of this file.
00001 // $Id: walker.h,v 1.9 2003/08/07 23:13:57 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #ifndef CBZ_WALKER_H
00039 #define CBZ_WALKER_H
00040 
00142 class Walker {
00143 
00144 public:
00145 
00147   typedef enum {
00148     Preorder,     
00149     Postorder,    
00150     Both          
00152   } Order;
00153 
00155   typedef enum {
00156     Subtree,     
00157     NodeOnly     
00158   } Depth;
00159 
00160 private:
00161 
00163   Order _order;
00164 
00166   Depth _depth;
00167 
00168 
00169 public:
00170 
00187   Walker(Order the_order, Depth depth)
00188     : _order(the_order),
00189       _depth(depth)
00190   {}
00191 
00196 
00203   inline Order order() const { return _order; }
00204 
00211   inline Depth depth() const { return _depth; }
00212 
00214 
00222 
00223   virtual void at_node(Node * the_node, Order ord)
00224      { // Default: do nothing
00225      }
00226 
00227   virtual void at_unit(unitNode * the_unit, Order ord)
00228      { at_node(the_unit, ord); }
00229 
00230   virtual void at_def(defNode * the_def, Order ord)
00231      { at_node(the_def, ord); }
00232 
00233   virtual void at_decl(declNode * the_decl, Order ord)
00234      { at_def(the_decl, ord); }
00235 
00236   virtual void at_subdecl(subdeclNode * the_subdecl, Order ord)
00237      { at_decl(the_subdecl, ord); }
00238 
00239   virtual void at_proc(procNode * the_proc, Order ord)
00240      { at_def(the_proc, ord); }
00241 
00242   virtual void at_type(typeNode * the_type, Order ord)
00243      { at_node(the_type, ord); }
00244 
00245   virtual void at_prim(primNode * the_prim, Order ord)
00246      { at_type(the_prim, ord); }
00247 
00248   virtual void at_tdef(tdefNode * the_tdef, Order ord)
00249      { at_type(the_tdef, ord); }
00250 
00251   virtual void at_ptr(ptrNode * the_ptr, Order ord)
00252      { at_type(the_ptr, ord); }
00253 
00254   virtual void at_array(arrayNode * the_array, Order ord)
00255      { at_type(the_array, ord); }
00256 
00257   virtual void at_func(funcNode * the_func, Order ord)
00258      { at_type(the_func, ord); }
00259 
00260   virtual void at_sue(sueNode * the_sue, Order ord)
00261      { at_type(the_sue, ord); }
00262 
00263   virtual void at_struct(structNode * the_struct, Order ord)
00264      { at_sue(the_struct, ord); }
00265 
00266   virtual void at_union(unionNode * the_union, Order ord)
00267      { at_sue(the_union, ord); }
00268 
00269   virtual void at_enum(enumNode * the_enum, Order ord)
00270      { at_sue(the_enum, ord); }
00271 
00272   virtual void at_suespec(suespecNode * the_suespec, Order ord)
00273      { at_type(the_suespec, ord); }
00274 
00275   virtual void at_expr(exprNode * the_expr, Order ord)
00276      { at_node(the_expr, ord); }
00277 
00278   virtual void at_index(indexNode * the_index, Order ord)
00279     { at_expr(the_index, ord); }
00280 
00281   virtual void at_const(constNode * the_const, Order ord)
00282      { at_index(the_const, ord); }
00283 
00284   virtual void at_id(idNode * the_id, Order ord)
00285      { at_index(the_id, ord); }
00286 
00287   virtual void at_binary(binaryNode * the_binary, Order ord)
00288      { at_expr(the_binary, ord); }
00289 
00290   virtual void at_unary(unaryNode * the_unary, Order ord)
00291      { at_expr(the_unary, ord); }
00292 
00293   virtual void at_cast(castNode * the_cast, Order ord)
00294      { at_expr(the_cast, ord); }
00295 
00296   virtual void at_comma(commaNode * the_comma, Order ord)
00297      { at_expr(the_comma, ord); }
00298 
00299   virtual void at_ternary(ternaryNode * the_ternary, Order ord)
00300      { at_expr(the_ternary, ord); }
00301 
00302   virtual void at_call(callNode * the_call, Order ord)
00303      { at_expr(the_call, ord); }
00304 
00305   virtual void at_initializer(initializerNode * the_initializer, Order ord)
00306      { at_expr(the_initializer, ord); }
00307 
00308   virtual void at_stmt(stmtNode * the_stmt, Order ord)
00309      { at_node(the_stmt, ord); }
00310 
00311   virtual void at_block(blockNode * the_block, Order ord)
00312      { at_stmt(the_block, ord); }
00313 
00314   virtual void at_basicblock(basicblockNode * the_basicblock, Order ord)
00315      { at_block(the_basicblock, ord); }
00316 
00317   virtual void at_exprstmt(exprstmtNode * the_exprstmt, Order ord)
00318      { at_stmt(the_exprstmt, ord); }
00319 
00320   virtual void at_target(targetNode * the_target, Order ord)
00321      { at_stmt(the_target, ord); }
00322 
00323   virtual void at_label(labelNode * the_label, Order ord)
00324      { at_target(the_label, ord); }
00325 
00326   virtual void at_case(caseNode * the_case, Order ord)
00327      { at_target(the_case, ord); }
00328 
00329   virtual void at_selection(selectionNode * the_selection, Order ord)
00330      { at_stmt(the_selection, ord); }
00331 
00332   virtual void at_if(ifNode * the_if, Order ord)
00333      { at_selection(the_if, ord); }
00334 
00335   virtual void at_switch(switchNode * the_switch, Order ord)
00336      { at_selection(the_switch, ord); }
00337 
00338   virtual void at_loop(loopNode * the_loop, Order ord)
00339      { at_stmt(the_loop, ord); }
00340 
00341   virtual void at_while(whileNode * the_while, Order ord)
00342      { at_loop(the_while, ord); }
00343 
00344   virtual void at_do(doNode * the_do, Order ord)
00345      { at_loop(the_do, ord); }
00346 
00347   virtual void at_for(forNode * the_for, Order ord)
00348      { at_loop(the_for, ord); }
00349 
00350   virtual void at_jump(jumpNode * the_jump, Order ord)
00351      { at_stmt(the_jump, ord); }
00352 
00353   virtual void at_goto(gotoNode * the_goto, Order ord)
00354      { at_jump(the_goto, ord); }
00355 
00356   virtual void at_continue(continueNode * the_continue, Order ord)
00357      { at_jump(the_continue, ord); }
00358 
00359   virtual void at_break(breakNode * the_break, Order ord)
00360      { at_jump(the_break, ord); }
00361 
00362   virtual void at_return(returnNode * the_return, Order ord)
00363      { at_jump(the_return, ord); }
00364 
00365   virtual void at_attrib(attribNode * the_attrib, Order ord)
00366      { at_stmt(the_attrib, ord); }
00367 
00368   virtual void at_operand(operandNode * the_oper, Order ord)
00369     { at_expr(the_oper, ord); }
00370 
00371   virtual void at_conditiongoto(conditiongotoNode * the_condgoto, Order ord)
00372     { at_goto(the_condgoto, ord); }
00373 
00374   virtual void at_threeAddr(threeAddrNode * the_3addr, Order ord)
00375     { at_stmt(the_3addr, ord); }
00376 
00377   virtual void at_text(textNode * the_text, Order ord)
00378      { at_node(the_text, ord); }
00379 
00381 
00382 private:
00383 
00384   /* -- Deprecated...
00385 
00386   virtual void at_implicitcast(implicitcastNode * the_implicitcast, Order ord)
00387      { at_expr(the_implicitcast, ord); }
00388 
00389   virtual void at_default(defaultNode * the_default, Order ord)
00390      { at_target(the_default, ord); }
00391 
00392   virtual void at_ifelse(ifelseNode * the_ifelse, Order ord)
00393      { at_selection(the_ifelse, ord); }
00394 
00395   */
00396 };
00397 
00398 // -- Invoke the walker on a list...
00399 
00408 template< class T >
00409 void list_walker(list< T > & l, Walker & the_walker)
00410 {
00411   for (typename list< T >::iterator p = l.begin();
00412        p != l.end();
00413        ++p)
00414     (*p)->walk(the_walker);
00415 }
00416 
00417 
00418 #endif // CBZ_WALKER_H

Generated on February 1, 2006
Back to the C-Breeze home page