C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

vcgast.cc

Go to the documentation of this file.
00001 // $Id: vcgast.cc,v 1.5 2003/08/11 19:05:01 toktb Exp $
00002 
00003 // ----------------------------------------------------------------------
00004 //
00005 //  J-Breeze
00006 //  Java Compiler Framework
00007 // 
00008 //  Copyright (c) 2001 University of Texas at Austin
00009 // 
00010 //  Teck B. Tok
00011 //  Samuel Z. Guyer
00012 //  Daniel A. Jimenez
00013 //  Calvin Lin
00014 // 
00015 //  Permission is hereby granted, free of charge, to any person
00016 //  obtaining a copy of this software and associated documentation
00017 //  files (the "Software"), to deal in the Software without
00018 //  restriction, including without limitation the rights to use, copy,
00019 //  modify, merge, publish, distribute, and/or sublicense copies
00020 //  of the Software, and to permit persons to whom the Software is
00021 //  furnished to do so, subject to the following conditions:
00022 //  
00023 //  The above copyright notice and this permission notice shall be
00024 //  included in all copies or substantial portions of the Software.
00025 //  
00026 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00030 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00031 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00032 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00033 //  THE SOFTWARE.
00034 //
00035 //  J-Breeze extends from C-Breeze (copyright University of Texas at
00036 //  Austin), part of whose design is inspired by the C-to-C Translator
00037 //  from MIT Laboratory for Computer Science.
00038 //
00039 // ----------------------------------------------------------------------
00040 
00041 #include "vcg.h"
00042 
00043 class vcgASTWalker : public vcgWalker {
00044 
00045   typedef pair<Node*, string>  Edge;  // <target node, name of edge>
00046   typedef list<Edge>           Edges;
00047   typedef Edges::iterator      Edges_p;
00048   typedef map<Node*,Edges>     Edge_map;
00049 
00050   node_list  nodes;  // nodes that are printed.
00051   Edge_map   edges;  // to avoid duplicate edges; printed is delayed, so that
00052                      // only whose nodes are printed that an edge is printed.
00053 
00054 public:
00055   vcgASTWalker(ostream& ostr, const string& comment, string excludefilename)
00056     : vcgWalker(ostr, comment, excludefilename) {
00057     nodes.clear();  edges.clear();  
00058     // Start graph description
00059     print_comment();
00060     print_comment( "=== J-Breeze AST visualization ===" );
00061     print_comment();
00062     if( !comment.empty() ) {
00063       print_comment( comment );
00064       print_comment();
00065     }
00066 
00067     start_graph();
00068     print_graph_attribute( "orientation", "left_to_right" );
00069     print_graph_attribute( "display_edge_labels", "yes");
00070   } // vcgASTWalker
00071 
00072   virtual ~vcgASTWalker(void) { finalize_graph(); } // finalize_graph is called
00073 
00075 private:
00076   virtual void finalize_graph() {
00077     delayed_print_edge();
00078     // Close the graph.
00079     //graph << endl;
00080     //graph << "} // end of graph" << endl;
00081   } // finalize_graph
00082 
00083   void print_node(Node *n) {
00084     // has the node been printed?
00085     if(find(nodes.begin(), nodes.end(), n) != nodes.end()) return;
00086 #ifndef J_BREEZE
00087     if(n->typ()!=Unit && excluded(n->coord())) return;
00088 #endif
00089 
00090     nodes.push_front(n);
00091 
00092     graph << endl;
00093     graph << "   node: {" << endl;
00094     print_node_attribute( "title", node_title(n));
00095     print_node_attribute( "label", node_label(n));
00096     switch(n->typ()) { // more node attributes
00097       // expr
00098       case Const: case Id: case Binary: case Unary: case Cast: case Comma:
00099       case Ternary: /*case Index:*/ case Call: case Initializer:
00100       // case ImplicitCast:
00101       case Expr:
00102 #ifndef J_BREEZE
00103       case ThreeAddr: case Condition: case Operand:
00104 #else
00105       case QualifiedName: case Special: case New: case AccessClass:
00106       case InstanceOf:
00107 #endif
00108         print_node_value( "shape", "ellipse" ); break;
00109       // statements
00110       case Label: case Switch: case Case: /*case Default:*/ case If:
00111       // case IfElse:
00112       case While: case Do: case For: case Goto: case Continue: case Break:
00113       case Return: case Block:
00114 #ifdef J_BREEZE
00115       case DeclStmt: case ForInit: case Break_Id: case Continue_Id: case Throw:
00116       case Synchronized: case Try: case Catch: case Finally:
00117 #endif
00118         print_node_value( "shape", "box" ); break;
00119       // types
00120       case Prim: case Tdef: case Ptr: case Array: case Func: case Struct:
00121       case Union: case Enum: case sueSpec:
00122 #ifdef J_BREEZE
00123       case Class: case Interface: case TypeName:
00124 #endif
00125         print_node_value( "shape", "rhomb" );
00126         print_node_value( "textcolor", "green" );
00127         print_node_value( "bordercolor", "green" ); break;
00128       // defs
00129       case Decl: case Proc: case Unit:
00130 #ifdef J_BREEZE
00131       case FieldVar: case Method: case Constructor: case FieldInit:
00132       case MemberType: case Package: case Import:
00133 #endif
00134         print_node_value( "shape", "triangle" );
00135         print_node_value( "textcolor", "blue" );
00136         print_node_value( "bordercolor", "blue" ); break;
00137       // misc
00138       case Text: case Undeclared: case Meta: case Attrib:
00139         print_node_value( "shape", "triangle" );
00140         print_node_value( "textcolor", "red" );
00141         print_node_value( "bordercolor", "red" ); break;
00142       default: assert(false);
00143     }
00144     graph << "   }" << endl;
00145   } // print_node
00146 
00147   string node_title(Node *n) {
00148     ostringstream title;
00149     char hex[16];
00150     sprintf(hex, "(%x)\0", n);
00151     title << node_name(n) << '@' << n->coord() << '.' << n->coord().offset()
00152           << hex << '\0';
00153     return title.str();
00154   } // node_title
00155 
00156   string node_label(Node *n) {
00157     ostringstream label;
00158     Coord coord = n->coord();
00159     label << node_name(n) << ':' << coord.line() << '.' << coord.offset()
00160           << '\0';
00161     return label.str();
00162   } // node_label
00163 
00164   void print_edge(Node *from, Node *to, string name) {
00165     if(!from || !to) return;
00166 #ifndef J_BREEZE
00167     if((from->typ()!=Unit && excluded(from->coord()))
00168        || excluded(to->coord())) return;
00169 #endif
00170     Edges targets=edges[from];
00171     // has the edge been printed?
00172     for(Edges_p e=targets.begin(); e!=targets.end(); e++)
00173       if((*e).first == to) return;
00174     edges[from].push_front( Edge(to,name) );
00175   }
00176 
00177   void delayed_print_edge(void) {
00178     for(Edge_map::iterator es=edges.begin(); es!=edges.end(); es++) {
00179       Node *from = (*es).first;
00180       for(Edges_p e=(*es).second.begin(); e!=(*es).second.end(); e++) {
00181         Node *to = (*e).first;
00182         // if node is not in nodes, do not print the edge
00183         if(find(nodes.begin(), nodes.end(), to)==nodes.end()) continue;
00184 
00185         graph << endl;
00186         graph << "   edge: {";
00187         graph << "  sourcename: \"" << node_title(from) << "\"";
00188         graph << "  targetname: \"" << node_title(to)   << "\"";
00189         graph << "  label: \"" << (*e).second << "\"";
00190         graph << "  }" << endl;
00191       }
00192     }
00193   } // print_edge
00194 
00195 #define STRING(s1,s2) (string(s1 + string(" `") + s2 + "'"))
00196 
00197   string node_name(Node *n) {
00198     if(!n) return string("");
00199     switch(n->typ()) { // more node attributes
00200       // expr
00201       case Const: {
00202         constant v = ((constNode*)n)->value();
00203         string s = v.to_string();
00204         if(v.is_str()) { s.erase(0,1);  s.erase(s.length()-1,1); }
00205         return STRING("Const", s);
00206       }
00207       case Id:
00208         return STRING("Id", ((idNode*)n)->name());
00209       case Binary:
00210         return STRING("Binary", ((binaryNode*)n)->op()->print());
00211       case Unary:
00212         return STRING("Unary", ((unaryNode*)n)->op()->print());
00213       case Cast:
00214 #ifdef J_BREEZE
00215         return STRING("Cast", typenameNode::type_name(((castNode*)n)->type()));
00216 #else
00217         return string("Cast");
00218 #endif
00219       case Comma: return string("Comma");
00220       case Ternary: return string("Ternary");
00221       /*case Index:
00222 #ifdef J_BREEZE
00223         return STRING("Index", qnameNode::expand_name(((indexNode*)n)->name()));
00224 #else
00225         return string("Index");
00226 #endif
00227 */
00228 #ifdef J_BREEZE
00229       case Call:
00230         return STRING("Call", qnameNode::expand_name(((mcallNode*)n)->name()));
00231 #else
00232       case Call: return string("Call");
00233 #endif
00234       case Initializer: return string("Initializer");
00235       //case ImplicitCast: return string("Implicitcast");
00236 #ifdef J_BREEZE
00237       case QualifiedName: return string("QualifiedName");
00238         //return STRING("QualifiedName",((qnameNode*)n)->expand_name());
00239       case Special: return STRING("Special", (((specialNode*)n)->text()));
00240       case New:
00241         return STRING("New", typenameNode::type_name(((newNode*)n)->type()));
00242       case AccessClass:
00243         return STRING("AccessClass",
00244                       typenameNode::type_name(((classLiteralNode*)n)->type()));
00245       case InstanceOf: return string("InstanceOf");
00246 #endif
00247       case Expr: return string("Expr");
00248       // statements
00249       case Label: return STRING("Label", ((labelNode*)n)->name());
00250       case Switch: return string("Switch");
00251       case Case:
00252 #ifdef J_BREEZE
00253         return STRING("Case", qnameNode::expand_name(((caseNode*)n)->expr()));
00254 #else
00255         return string("Case");
00256 #endif
00257       //case Default: return string("Default");
00258       case If: return string("If");
00259       //case IfElse: return string("Ifelse");
00260       case While: return string("While");
00261       case Do: return string("Do");
00262       case For: return string("For");
00263 #ifndef J_BREEZE
00264       case Goto: return STRING("Goto", ((gotoNode*)n)->name());
00265 #endif
00266       case Continue: return string("Continue");
00267       case Break: return string("Break");
00268       case Return: return string("Return");
00269       case Block: return string("Block");
00270 #ifdef J_BREEZE
00271       case DeclStmt: return string("DeclStmt");
00272       case ForInit: return string("ForInit");
00273       case Break_Id:
00274         return STRING("Break_Id", ((break_idNode*)n)->name());
00275       case Continue_Id:
00276         return STRING("Continue_Id", ((continue_idNode*)n)->name());
00277       case Throw: return string("Throw");
00278       case Synchronized:
00279         return STRING("Synchronized",
00280                       qnameNode::expand_name(((synchNode*)n)->expr()));
00281       case Try: return string("Try");
00282       case Catch: return string("Catch");
00283         /*return STRING("Catch",
00284                       ((catchNode*)n)->to_catch()->name()); */
00285       case Finally: return string("finally");
00286 #endif
00287       // types
00288       case Prim: return STRING("Prim", (((primNode*)n)->basic().to_string()));
00289       case Tdef: return STRING("Tdef", ((tdefNode*)n)->name());
00290       case Ptr: return string("Ptr");
00291       case Array:
00292 #ifdef J_BREEZE
00293         return STRING("Array",typenameNode::type_name((arrayNode*)n));
00294 #else
00295         return string("Array");
00296 #endif
00297       case Func: return string("Func");
00298 #ifndef J_BREEZE
00299       case Struct: return string("Struct");
00300       case Union: return string("Union");
00301       case Enum: return string("Enum");
00302       case sueSpec: return STRING("sueSpec", (((suespecNode*)n)->name()));
00303 #else
00304       case Class: return STRING("Class", ((classNode*)n)->name());
00305       case Interface: return STRING("Interface", ((interfaceNode*)n)->name());
00306       case TypeName:
00307         return STRING("TypeName",
00308                       qnameNode::expand_name(((typenameNode*)n)->name()));
00309 #endif
00310       // defs
00311       case Decl: return STRING("Decl", ((declNode*)n)->name());
00312       case Unit: return STRING("Unit", ((unitNode*)n)->input_file());
00313 #ifndef J_BREEZE
00314       case Proc: return STRING("Proc", ((procNode*)n)->decl()->name());
00315       case ThreeAddr: return string("ThreeAddr");
00316       case Condition: return string("Condition");
00317       case Operand: return string("Operand");
00318 #else
00319       case FieldVar: return string("FieldVar");
00320       case Method: return STRING("Method", ((methodNode*)n)->decl()->name());
00321       case Constructor: return string("Constructor");
00322       case FieldInit: return string("FieldInit");
00323       case MemberType: return string("MemberType");
00324       case Package: return string("Package");
00325       case Import: return string("Import");
00326 #endif
00327       // misc
00328 #ifndef J_BREEZE
00329       case Text: return string("Text"); //STRING("Text",((textNode*)n)->text());
00330       case Undeclared: return string("Undeclared");
00331       case Meta: return string("Meta");
00332       case Attrib: return STRING("Attrib", ((attribNode*)n)->name());
00333 #endif
00334       default: assert(false);
00335     }
00336   } // node_name
00337 
00338   template< class S, class T >
00339   void print_edges(S parent, list< T > &children, string name) {
00340     for (typename list< T >::iterator c=children.begin(); c!=children.end();
00341          ++c)
00342       print_edge(parent, *c, name);
00343   } // print_edges
00344 
00345   void at_unit(unitNode * the_unit, Order ord) {
00346     if (ord == Postorder) return;
00347     print_node(the_unit);
00348 #ifndef J_BREEZE
00349     print_edges(the_unit, the_unit->undef_funcs(), "undef_funcs");
00350     print_edges(the_unit, the_unit->suespecs(), "suspecs");
00351 #endif
00352     print_edges(the_unit, the_unit->defs(), "defs");
00353   } // at_unit
00354 
00355   void at_decl(declNode * the_decl, Order ord) {
00356     if (ord == Postorder || currently_excluded) return;
00357     print_node(the_decl);
00358     typeNode *type = the_decl->type();
00359 #ifdef J_BREEZE
00360     if((type->typ()!=Class && type->typ()!=Interface) ||
00361        !excluded(type))
00362 #endif
00363       print_edge(the_decl, type, "type");
00364     print_edge(the_decl, the_decl->init(), "init");
00365 #ifndef J_BREEZE
00366     print_edge(the_decl, the_decl->bitsize(), "bitsize");
00367 #endif
00368   } // at_decl
00369 
00370 #ifndef J_BREEZE
00371   void at_proc(procNode * the_proc, Order ord) {
00372     if(ord==Postorder) currently_excluded = false;
00373     else               currently_excluded = excluded(the_proc);
00374     if (ord == Postorder || currently_excluded) return;
00375     print_node(the_proc);
00376     print_edge(the_proc, the_proc->decl(), "decl");
00377     print_edge(the_proc, the_proc->body(), "body");
00378   } // at_proc
00379 #endif
00380 
00381   void at_prim(primNode * the_prim, Order ord) {
00382     if (ord == Postorder || currently_excluded) return;
00383     print_node(the_prim);
00384   } // at_prim
00385 
00386   void at_tdef(tdefNode * the_tdef, Order ord) {
00387     if (ord == Postorder || currently_excluded) return;
00388     print_node(the_tdef);
00389   } // at_tdef
00390 
00391 #ifndef J_BREEZE
00392   void at_ptr(ptrNode * the_ptr, Order ord) {
00393     if (ord == Postorder || currently_excluded) return;
00394     print_node(the_ptr);
00395     print_edge(the_ptr, the_ptr->type(), "type");
00396   } // at_ptr
00397 #endif
00398 
00399   void at_array(arrayNode * the_array, Order ord) {
00400     if (ord == Postorder || currently_excluded) return;
00401     print_node(the_array);
00402     print_edge(the_array, the_array->type(), "type");
00403     print_edge(the_array, the_array->dim(), "dim");
00404   } // at_array
00405 
00406   void at_func(funcNode * the_func, Order ord) {
00407     if (ord == Postorder || currently_excluded) return;
00408     print_node(the_func);
00409     print_edges(the_func, the_func->args(), "args");
00410     print_edge(the_func, the_func->returns(), "returns");
00411   } // at_func
00412 
00413 #ifndef J_BREEZE
00414   void at_sue(sueNode * the_sue, Order ord) {
00415     if (ord == Postorder || currently_excluded) return;
00416     print_node(the_sue);
00417   } // at_sue
00418 
00419   void at_suespec(suespecNode * the_suespec, Order ord) {
00420     if (ord == Postorder || currently_excluded) return;
00421     print_node(the_suespec);
00422     print_edges(the_suespec, the_suespec->fields(), "fields");
00423   } // at_suespec
00424 #endif
00425 
00426   void at_const(constNode * the_const, Order ord) {
00427     if (ord == Postorder || currently_excluded) return;
00428     print_node(the_const);
00429     print_edge(the_const, the_const->type(), "type");
00430   } // at_const
00431 
00432   void at_id(idNode * the_id, Order ord) {
00433     if (ord == Postorder || currently_excluded) return;
00434     print_node(the_id);
00435     print_edge(the_id, the_id->type(), "type");
00436   } // at_id
00437 
00438   void at_binary(binaryNode * the_binary, Order ord) {
00439     if (ord == Postorder || currently_excluded) return;
00440     print_node(the_binary);
00441     print_edge(the_binary, the_binary->type(), "type");
00442     print_edge(the_binary, the_binary->left(), "left");
00443     print_edge(the_binary, the_binary->right(), "right");
00444     // hack, due to binarynode.cc
00445     if(the_binary->right() &&
00446        (the_binary->op()->id()=='.' ||
00447 #ifdef J_BREEZE
00448         the_binary->op()->id()==Operator::opARROW
00449 #else
00450         the_binary->op()->id()==Operator::ARROW
00451 #endif
00452        ))
00453       the_binary->right()->walk(*this);
00454   } // at_binary
00455 
00456   void at_unary(unaryNode * the_unary, Order ord) {
00457     if (ord == Postorder || currently_excluded) return;
00458     print_node(the_unary);
00459     print_edge(the_unary, the_unary->expr(), "expr");
00460     print_edge(the_unary, the_unary->type(), "type");
00461 #ifndef J_BREEZE
00462     print_edge(the_unary, the_unary->sizeof_type(), "sizeof_type");
00463 #endif
00464   } // at_unary
00465 
00466   void at_cast(castNode * the_cast, Order ord) {
00467     if (ord == Postorder || currently_excluded) return;
00468     print_node(the_cast);
00469     print_edge(the_cast, the_cast->type(), "type");
00470     print_edge(the_cast, the_cast->expr(), "expr");
00471   } // at_cast
00472 
00473   void at_comma(commaNode * the_comma, Order ord) {
00474     if (ord == Postorder || currently_excluded) return;
00475     print_node(the_comma);
00476     print_edges(the_comma, the_comma->exprs(), "exprs");
00477     print_edge(the_comma, the_comma->type(), "type");
00478   } // at_comma
00479 
00480   void at_ternary(ternaryNode * the_ternary, Order ord) {
00481     if (ord == Postorder || currently_excluded) return;
00482     print_node(the_ternary);
00483     print_edge(the_ternary, the_ternary->cond(), "cond");
00484     print_edge(the_ternary, the_ternary->true_br(), "true_br");
00485     print_edge(the_ternary, the_ternary->false_br(), "false_br");
00486     print_edge(the_ternary, the_ternary->type(), "type");
00487   } // at_ternary
00488 
00489   /*void at_index(indexNode * the_index, Order ord) {
00490     if (ord == Postorder || currently_excluded) return;
00491     print_node(the_index);
00492     print_edge(the_index, the_index->name(), "name");
00493     print_edge(the_index, the_index->dim(), "dim");
00494     print_edge(the_index, the_index->type(), "type");
00495   } // at_index */
00496 
00497 #ifndef J_BREEZE
00498   void at_call(callNode * the_call, Order ord) {
00499     if (ord == Postorder || currently_excluded) return;
00500     print_node(the_call);
00501     print_edge(the_call, the_call->name(), "name");
00502     print_edges(the_call, the_call->args(), "args");
00503     print_edge(the_call, the_call->type(), "type");
00504   } // at_call
00505 #else
00506   void at_mcall(mcallNode * the_call, Order ord) {
00507     if (ord == Postorder || currently_excluded) return;
00508     print_node(the_call);
00509     print_edge(the_call, the_call->name(), "name");
00510     print_edges(the_call, the_call->args(), "args");
00511     print_edge(the_call, the_call->type(), "type");
00512   } // at_mcall
00513 #endif
00514 
00515   void at_initializer(initializerNode * the_initializer, Order ord) {
00516     if (ord == Postorder || currently_excluded) return;
00517     print_node(the_initializer);
00518     print_edges(the_initializer, the_initializer->exprs(), "exprs");
00519     print_edge(the_initializer, the_initializer->type(), "type");
00520   } // at_initializer
00521 
00522   /*void at_implicitcast(implicitcastNode * the_implicitcast, Order ord) {
00523     if (ord == Postorder || currently_excluded) return;
00524     print_node(the_implicitcast);
00525     print_edge(the_implicitcast, the_implicitcast->type(), "type");
00526     print_edge(the_implicitcast, the_implicitcast->expr(), "expr");
00527   } // at_implicitcast */
00528 
00529   void at_block(blockNode * the_block, Order ord) {
00530     if (ord == Postorder || currently_excluded) return;
00531     print_node(the_block);
00532     print_edges(the_block, the_block->decls(), "decls");
00533     print_edges(the_block, the_block->stmts(), "stmts");
00534   } // at_block
00535 
00536   void at_exprstmt(exprstmtNode * the_exprstmt, Order ord) {
00537     if (ord == Postorder || currently_excluded) return;
00538     print_node(the_exprstmt);
00539     print_edge(the_exprstmt, the_exprstmt->expr(), "expr");
00540   } // at_exprstmt
00541 
00542   void at_label(labelNode * the_label, Order ord) {
00543     if (ord == Postorder || currently_excluded) return;
00544     print_node(the_label);
00545     print_edge(the_label, the_label->stmt(), "stmt");
00546   } // at_label
00547 
00548   void at_case(caseNode * the_case, Order ord) {
00549     if (ord == Postorder || currently_excluded) return;
00550     print_node(the_case);
00551     print_edge(the_case, the_case->expr(), "expr");
00552     print_edge(the_case, the_case->stmt(), "stmt");
00553   } // at_case
00554 
00555   /*void at_default(defaultNode * the_default, Order ord) {
00556     if (ord == Postorder || currently_excluded) return;
00557     print_node(the_default);
00558     print_edge(the_default, the_default->stmt(), "stmt");
00559   } // at_default */
00560 
00561   void at_if(ifNode * the_if, Order ord) {
00562     if (ord == Postorder || currently_excluded) return;
00563     print_node(the_if);
00564     print_edge(the_if, the_if->expr(), "expr");
00565     print_edge(the_if, the_if->true_br(), "true_br");
00566     print_edge(the_if, the_if->false_br(), "false_br");
00567   } // at_if
00568 
00569   /*void at_ifelse(ifelseNode * the_ifelse, Order ord) {
00570     if (ord == Postorder || currently_excluded) return;
00571     print_node(the_ifelse);
00572     print_edge(the_ifelse, the_ifelse->expr(), "expr");
00573     print_edge(the_ifelse, the_ifelse->true_br(), "true_br");
00574     print_edge(the_ifelse, the_ifelse->false_br(), "false_br");
00575   } // at_ifelse */
00576 
00577   void at_switch(switchNode * the_switch, Order ord) {
00578     if (ord == Postorder || currently_excluded) return;
00579     print_node(the_switch);
00580     print_edge(the_switch, the_switch->expr(), "expr");
00581     print_edge(the_switch, the_switch->stmt(), "stmt");
00582   } // at_switch
00583 
00584   void at_while(whileNode * the_while, Order ord) {
00585     if (ord == Postorder || currently_excluded) return;
00586     print_node(the_while);
00587     print_edge(the_while, the_while->cond(), "cond");
00588     print_edge(the_while, the_while->body(), "body");
00589   } // at_while
00590 
00591   void at_do(doNode * the_do, Order ord) {
00592     if (ord == Postorder || currently_excluded) return;
00593     print_node(the_do);
00594     print_edge(the_do, the_do->body(), "body");
00595     print_edge(the_do, the_do->cond(), "cond");
00596   } // at_do
00597 
00598   void at_for(forNode * the_for, Order ord) {
00599     if (ord == Postorder || currently_excluded) return;
00600     print_node(the_for);
00601     print_edge(the_for, the_for->init(), "init");
00602     print_edge(the_for, the_for->cond(), "cond");
00603     print_edge(the_for, the_for->next(), "next");
00604     print_edge(the_for, the_for->body(), "body");
00605   } // at_for
00606 
00607 #ifndef J_BREEZE
00608   void at_goto(gotoNode * the_goto, Order ord) {
00609     if (ord == Postorder || currently_excluded) return;
00610     print_node(the_goto);
00611   } // at_goto
00612 #endif
00613 
00614   void at_continue(continueNode * the_continue, Order ord) {
00615     if (ord == Postorder || currently_excluded) return;
00616     print_node(the_continue);
00617   } // at_continue
00618 
00619   void at_break(breakNode * the_break, Order ord) {
00620     if (ord == Postorder || currently_excluded) return;
00621     print_node(the_break);
00622   } // at_break
00623 
00624   void at_return(returnNode * the_return, Order ord) {
00625     if (ord == Postorder || currently_excluded) return;
00626     print_node(the_return);
00627     print_edge(the_return, the_return->expr(), "expr");
00628   } // at_return
00629 
00630 #ifndef J_BREEZE
00631   void at_attrib(attribNode * the_attrib, Order ord) {
00632     if (ord == Postorder || currently_excluded) return;
00633     print_node(the_attrib);
00634     print_edge(the_attrib, the_attrib->arg(), "arg");
00635   } // at_attrib
00636 
00637   void at_text(textNode * the_text, Order ord) {
00638     if (ord == Postorder || currently_excluded) return;
00639     print_node(the_text);
00640   } // at_text
00641 
00642   void at_threeAddr(threeAddrNode *t, Order ord) {
00643     if (ord == Postorder || currently_excluded) return;
00644     print_node(t);
00645     print_edge(t, t->lhs(), "lhs");
00646     print_edge(t, t->rhs1(), "rhs1");
00647     print_edge(t, t->rhs2(), "rhs2");
00648     print_edges(t, t->arg_list(), "args");
00649   }
00650 
00651   void at_conditiongoto(conditiongotoNode *c, Order ord) {
00652     if (ord == Postorder || currently_excluded) return;
00653     print_node(c);
00654     print_edge(c, c->left(), "left");
00655     print_edge(c, c->right(), "right");
00656   }
00657 
00658   void at_operand(operandNode *o, Order ord) {
00659     if (ord == Postorder || currently_excluded) return;
00660     print_node(o);
00661     print_edge(o, o->cast(), "cast");
00662     print_edge(o, o->var(), "var");
00663     print_edge(o, o->index(), "index");
00664     print_edges(o, o->fields(), "fields");
00665   }
00666 #endif
00667 
00668 #ifdef J_BREEZE
00669   void at_class(classNode * the_class, Order ord) {
00670     if(ord==Preorder) current_type.push_front(the_class);
00671     else              current_type.pop_front();
00672     if(current_type.empty())
00673       currently_excluded = false;
00674     else
00675       currently_excluded = excluded(current_type.front());
00676     if (ord == Postorder || currently_excluded) return;
00677     print_node(the_class);
00678     print_edge(the_class, the_class->extend(), "extend");
00679     print_edges(the_class, the_class->interfaces(), "interfaces");
00680     print_edges(the_class, the_class->fields(), "fields");
00681   } // at_class
00682  
00683   void at_interface(interfaceNode * the_interface, Order ord) {
00684     if(ord==Preorder) current_type.push_front(the_interface);
00685     else              current_type.pop_front();
00686     if(current_type.empty())
00687       currently_excluded = false;
00688     else
00689       currently_excluded = excluded(current_type.front());
00690     if (ord == Postorder || currently_excluded) return;
00691     print_node(the_interface);
00692     print_edges(the_interface, the_interface->extends(), "extends");
00693     print_edges(the_interface, the_interface->fields(), "fields");
00694   } // at_interface
00695  
00696   void at_typename(typenameNode * the_tn, Order ord) {
00697     if (ord == Postorder || currently_excluded) return;
00698     print_node(the_tn);
00699     print_edge(the_tn, the_tn->name(), "name");
00700   } // at_typename
00701  
00702   void at_fieldvar(fieldvarNode * the_fieldvar, Order ord) {
00703     if (ord == Postorder || currently_excluded) return;
00704     print_node(the_fieldvar);
00705     print_edges(the_fieldvar, the_fieldvar->decls(), "decls");
00706   } // at_fieldvar
00707  
00708   void at_method(methodNode * the_method, Order ord) {
00709     if (ord == Postorder || currently_excluded) return;
00710     print_node(the_method);
00711     print_edge(the_method, the_method->decl(), "decl");
00712     print_edges(the_method, the_method->throws(), "throws");
00713     print_edge(the_method, the_method->body(), "body");
00714   } // at_method
00715  
00716   void at_constructor(constructorNode * the_constructor, Order ord) {
00717     if (ord == Postorder || currently_excluded) return;
00718     print_node(the_constructor);
00719     print_edge(the_constructor, the_constructor->decl(), "decl");
00720     print_edges(the_constructor, the_constructor->throws(), "throws");
00721     print_edge(the_constructor, the_constructor->body(), "body");
00722   } // at_constructor
00723  
00724   void at_fieldinit(fieldinitNode * the_init, Order ord) {
00725     if (ord == Postorder || currently_excluded) return;
00726     print_node(the_init);
00727     print_edge(the_init, the_init->body(), "body");
00728   } // at_fieldinit
00729 
00730   void at_membertype(membertypeNode * the_membertype, Order ord) {
00731     if (ord == Postorder || currently_excluded) return;
00732     print_node(the_membertype);
00733     print_edge(the_membertype, the_membertype->member(), "membertype");
00734   } // at_membertype
00735  
00736   void at_declstmt(declstmtNode * the_declstmt, Order ord) {
00737     if (ord == Postorder || currently_excluded) return;
00738     print_node(the_declstmt);
00739     print_edges(the_declstmt, the_declstmt->decls(), "decls");
00740   } // at_declstmt
00741  
00742   void at_forinit(forinitNode * the_forinit, Order ord) {
00743     if (ord == Postorder || currently_excluded) return;
00744     print_node(the_forinit);
00745     print_edge(the_forinit, the_forinit->init(), "init");
00746     print_edge(the_forinit, the_forinit->cond(), "cond");
00747     print_edge(the_forinit, the_forinit->next(), "next");
00748     print_edge(the_forinit, the_forinit->body(), "body");
00749   } // at_forinit
00750  
00751   void at_break_id(break_idNode * the_break_id, Order ord) {
00752     if (ord == Postorder || currently_excluded) return;
00753     print_node(the_break_id);
00754   } // at_break_id
00755  
00756   void at_continue_id(continue_idNode * the_continue_id, Order ord) {
00757     if (ord == Postorder || currently_excluded) return;
00758     print_node(the_continue_id);
00759   } // at_continue_id
00760  
00761   void at_throw(throwNode * the_throw, Order ord) {
00762     if (ord == Postorder || currently_excluded) return;
00763     print_node(the_throw);
00764     print_edge(the_throw, the_throw->expr(), "expr");
00765   } // at_throw
00766  
00767   void at_synch(synchNode * the_synch, Order ord) {
00768     if (ord == Postorder || currently_excluded) return;
00769     print_node(the_synch);
00770     print_edge(the_synch, the_synch->expr(), "expr");
00771     print_edge(the_synch, the_synch->body(), "body");
00772   } // at_synch
00773  
00774   void at_try(tryNode * the_try, Order ord) {
00775     if (ord == Postorder || currently_excluded) return;
00776     print_node(the_try);
00777     print_edge(the_try, the_try->stmts(), "stmts");
00778     print_edges(the_try, the_try->catches(), "catches");
00779     print_edge(the_try, the_try->finally(), "finally");
00780   } // at_try
00781 
00782   void at_catch(catchNode * the_catch, Order ord) {
00783     if (ord == Postorder || currently_excluded) return;
00784     print_node(the_catch);
00785     print_edge(the_catch, the_catch->to_catch(), "to_catch");
00786     print_edge(the_catch, the_catch->body(), "body");
00787   } // at_catch
00788  
00789   void at_finally(finallyNode * the_finally, Order ord) {
00790     if (ord == Postorder || currently_excluded) return;
00791     print_node(the_finally);
00792     print_edge(the_finally, the_finally->block(), "block");
00793   } // at_finally
00794  
00795   void at_qualifiedname(qnameNode * the_qname, Order ord) {
00796     if (ord == Postorder || currently_excluded) return;
00797     print_node(the_qname);
00798     print_edge(the_qname, the_qname->left(), "left");
00799     print_edge(the_qname, the_qname->right(), "right");
00800   } // at_qualifiedname
00801  
00802   void at_special(specialNode * the_special, Order ord) {
00803     if (ord == Postorder || currently_excluded) return;
00804     print_node(the_special);
00805   } // at_special
00806  
00807   void at_new(newNode * the_new, Order ord) {
00808     if (ord == Postorder || currently_excluded) return;
00809     print_node(the_new);
00810     print_edge(the_new, the_new->type(), "type");
00811     if(the_new->newtype()==newNode::Array)
00812       print_edge(the_new, the_new->array_init(), "array_init");
00813     else {
00814       print_edges(the_new, the_new->args(), "args");
00815       print_edge(the_new, the_new->anonymous(), "anonymous");
00816     }
00817   } // at_new
00818  
00819   void at_classLiteral(classLiteralNode * the_ac, Order ord) {
00820     if (ord == Postorder || currently_excluded) return;
00821     print_node(the_ac);
00822     print_edge(the_ac, the_ac->class_of(), "class_of");
00823   } // at_classLiteral
00824  
00825   void at_instanceof(instanceofNode * the_instanceof, Order ord) {
00826     if (ord == Postorder || currently_excluded) return;
00827     print_node(the_instanceof);
00828     print_edge(the_instanceof, the_instanceof->left(), "left");
00829     print_edge(the_instanceof, the_instanceof->right(), "right");
00830   } // at_instanceof
00831 
00832   void at_packagestmt(packagestmtNode *p, Order ord) {
00833     if (ord == Postorder) return;
00834     print_node(p);
00835     print_edge(p, p->name(), "name");
00836   } // at_packagestmt
00837 
00838   void at_import(importNode *import, Order ord) {
00839     if (ord == Postorder) return;
00840     print_node(import);
00841     print_edge(import, import->name(), "name");
00842   } // at_import
00843 #endif
00844 }; // class vcgASTWalker
00845 
00846 // phase for generating AST graph
00847 class vcgASTPhase: public Phase {
00848 private:
00849   string excludefilename;
00850 public:
00851   void run (void) {
00852     string graph_file_name;
00853 
00854     if( CBZ::Program.empty() ) return;  // no units
00855     else if( CBZ::Program.size()==1 ) {
00856       // just one unit
00857        unitNode* u = CBZ::Program.front();
00858        graph_file_name = u->input_file() + ".vcgast";
00859     } else {
00860       cout << "Several compilation units present. Creating combined call graph."
00861            << endl;
00862       graph_file_name = "combined.vcgast";
00863     }
00864 
00865     // Open the output file.
00866     ofstream graph_file( graph_file_name.c_str() );
00867     
00868     if( graph_file ) {
00869       // Create the walker and process the ASTs of each unit.
00870       vcgASTWalker astw(graph_file, "File: " + graph_file_name,
00871                         excludefilename);
00872 
00873       for(unit_list_p i=CBZ::Program.begin(); i!=CBZ::Program.end(); ++i) {
00874         unitNode* u = *i;
00875         u->walk( astw );  // build the graph
00876       }
00877 
00878     } else /* if( !graph_file ) */ {
00879       cerr << "vcgASTPhase: Can not open '" << graph_file_name
00880            << "' for writing." << endl;
00881     }
00882   }
00883 
00884   void get_flags(str_list_p & arg) { 
00885     string opt = *arg;
00886     if(strncmp("exclude:", opt.c_str(), 8) == 0) {
00887       excludefilename = opt;
00888       excludefilename.erase(0,8);
00889       arg++;
00890     }
00891   };
00892 }; // vcgASTPhase
00893 
00894 Phases vcgastPhase( "vcgAST", new vcgASTPhase() );

Generated on August 27, 2003
Back to the C-Breeze home page