|
||
print_tree_visitor.ccGo to the documentation of this file.00001 // $Id: print_tree_visitor.cc,v 1.4 2003/08/07 23:14:04 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 #include "c_breeze.h" 00039 #include "tree_visitor.h" 00040 #include "print_tree_visitor.h" 00041 #include "df_number_walker.h" 00042 00043 // ------------------------------------------------------------ 00044 // Entry point 00045 // ------------------------------------------------------------ 00046 00047 void print_tree_visitor::print(Node * n, ostream & out) 00048 { 00049 numbering_map * num = df_number_walker::number(n, Walker::Preorder); 00050 print_tree_visitor p(out, num); 00051 00052 n->visit(&p); 00053 00054 delete num; 00055 } 00056 00057 // ------------------------------------------------------------ 00058 // Internal methods 00059 // ------------------------------------------------------------ 00060 00061 print_tree_visitor::print_tree_visitor(ostream & out, numbering_map * num) 00062 : tree_visitor(), 00063 _out(out), 00064 _indent(0), 00065 _num(num) 00066 {} 00067 00068 void print_tree_visitor::indent(Node * n) const 00069 { 00070 _out.width(4); 00071 _out << (*_num)[n]; 00072 00073 _out.width(_indent+1); 00074 _out.fill('.'); 00075 // _out.setf(ios_base::left, ios_base::adjustfield); 00076 00077 _out << "."; 00078 00079 _out.width(0); 00080 _out.fill(' '); 00081 // _out.setf(ios_base::right, ios_base::adjustfield); 00082 } 00083 00084 void print_tree_visitor::at_unit(unitNode * the_unit) 00085 { 00086 indent(the_unit); 00087 _out << "unit" << endl; 00088 00089 in(); 00090 00091 tree_visitor::at_unit(the_unit); 00092 00093 out(); 00094 } 00095 00096 void print_tree_visitor::at_decl(declNode * the_decl) 00097 { 00098 indent(the_decl); 00099 00100 _out << "decl: "; 00101 00102 if (the_decl->name().empty()) 00103 _out << "(abstract)"; 00104 else 00105 _out << "\"" << the_decl->name() << "\""; 00106 00107 _out << " "; 00108 _out << declNode::decl_location_name(the_decl->decl_location()); 00109 _out << " "; 00110 _out << declNode::storage_class_name(the_decl->storage_class()); 00111 _out << endl; 00112 00113 in(); 00114 00115 tree_visitor::at_decl(the_decl); 00116 00117 out(); 00118 } 00119 00120 void print_tree_visitor::at_proc(procNode * the_proc) 00121 { 00122 indent(the_proc); 00123 _out << "proc: "; 00124 00125 if (! the_proc->decl()) 00126 _out << "(no func decl) "; 00127 00128 if (! the_proc->body()) 00129 _out << "(no body) "; 00130 00131 _out << endl; 00132 00133 in(); 00134 00135 tree_visitor::at_proc(the_proc); 00136 00137 out(); 00138 } 00139 00140 void print_tree_visitor::at_prim(primNode * the_prim) 00141 { 00142 indent(the_prim); 00143 _out << "prim: "; 00144 00145 _out << the_prim->basic().to_string() << " "; 00146 _out << the_prim->type_qualifiers_name() << " "; 00147 _out << endl; 00148 00149 in(); 00150 00151 tree_visitor::at_prim(the_prim); 00152 00153 out(); 00154 } 00155 00156 void print_tree_visitor::at_tdef(tdefNode * the_tdef) 00157 { 00158 indent(the_tdef); 00159 00160 _out << "tdef: "; 00161 00162 if (the_tdef->name().empty()) 00163 _out << "(no name)"; 00164 else 00165 _out << "\"" << the_tdef->name() << "\""; 00166 00167 _out << " "; 00168 _out << "def=#" << (*_num)[the_tdef->def()] << " "; 00169 _out << the_tdef->type_qualifiers_name() << endl; 00170 00171 in(); 00172 00173 tree_visitor::at_tdef(the_tdef); 00174 00175 out(); 00176 } 00177 00178 void print_tree_visitor::at_ptr(ptrNode * the_ptr) 00179 { 00180 indent(the_ptr); 00181 _out << "ptr: " << the_ptr->type_qualifiers_name() << endl; 00182 00183 in(); 00184 00185 tree_visitor::at_ptr(the_ptr); 00186 00187 out(); 00188 } 00189 00190 void print_tree_visitor::at_array(arrayNode * the_array) 00191 { 00192 indent(the_array); 00193 _out << "array: size=" << the_array->size() << " "; 00194 00195 if (! the_array->dim()) 00196 _out << "(no dimension) "; 00197 00198 _out << the_array->type_qualifiers_name() << endl; 00199 00200 in(); 00201 00202 tree_visitor::at_array(the_array); 00203 00204 out(); 00205 } 00206 00207 void print_tree_visitor::at_func(funcNode * the_func) 00208 { 00209 indent(the_func); 00210 _out << "func: " << the_func->type_qualifiers_name() << endl; 00211 00212 in(); 00213 00214 tree_visitor::at_func(the_func); 00215 00216 out(); 00217 } 00218 00219 void print_tree_visitor::at_sue(sueNode * the_sue) 00220 { 00221 indent(the_sue); 00222 00223 switch (the_sue->typ()) { 00224 case Struct: 00225 _out << "struct: "; 00226 break; 00227 case Union: 00228 _out << "union: "; 00229 break; 00230 case Enum: 00231 _out << "enum: "; 00232 break; 00233 default: 00234 _out << "ERROR "; 00235 } 00236 00237 if (the_sue->spec()->name().empty()) 00238 _out << "(no name) "; 00239 else 00240 _out << "\"" << the_sue->spec()->name() << "\""; 00241 00242 _out << endl; 00243 00244 in(); 00245 00246 if (the_sue->elaborated() && the_sue->spec()) 00247 the_sue->spec()->visit(this); 00248 00249 out(); 00250 } 00251 00252 void print_tree_visitor::at_struct(structNode * the_struct) 00253 { at_sue(the_struct); } 00254 00255 void print_tree_visitor::at_union(unionNode * the_union) 00256 { at_sue(the_union); } 00257 00258 void print_tree_visitor::at_enum(enumNode * the_enum) 00259 { at_sue(the_enum); } 00260 00261 00262 void print_tree_visitor::at_suespec(suespecNode * the_suespec) 00263 { 00264 indent(the_suespec); 00265 _out << "suespec: "; 00266 00267 if (the_suespec->complete()) 00268 _out << "complete "; 00269 00270 if (the_suespec->visited()) 00271 _out << "visited "; 00272 00273 _out << "size=" << the_suespec->size() << " align=" << the_suespec->align(); 00274 _out << " " << the_suespec->type_qualifiers_name() << endl; 00275 00276 in(); 00277 00278 tree_visitor::at_suespec(the_suespec); 00279 00280 out(); 00281 } 00282 00283 void print_tree_visitor::at_const(constNode * the_const) 00284 { 00285 indent(the_const); 00286 _out << "const: val=\"" << the_const->text() << "\"" << endl; 00287 00288 in(); 00289 00290 tree_visitor::at_const(the_const); 00291 00292 out(); 00293 } 00294 00295 void print_tree_visitor::at_id(idNode * the_id) 00296 { 00297 indent(the_id); 00298 _out << "id: "; 00299 _out << "\"" << the_id->name() << "\" "; 00300 _out << "decl=#" << (*_num)[the_id->decl()]; 00301 _out << endl; 00302 00303 in(); 00304 00305 tree_visitor::at_id(the_id); 00306 00307 out(); 00308 } 00309 00310 void print_tree_visitor::at_binary(binaryNode * the_binary) 00311 { 00312 indent(the_binary); 00313 _out << "binary: op='" << the_binary->op()->print() << "' "; 00314 00315 if (! the_binary->left()) 00316 _out << "(no LHS) "; 00317 00318 if (! the_binary->right()) 00319 _out << "(no RHS) "; 00320 00321 _out << endl; 00322 00323 in(); 00324 00325 tree_visitor::at_binary(the_binary); 00326 00327 out(); 00328 } 00329 00330 void print_tree_visitor::at_unary(unaryNode * the_unary) 00331 { 00332 indent(the_unary); 00333 _out << "unary: op='" << the_unary->op()->print() << "'"; 00334 00335 if (! the_unary->expr()) 00336 _out << "(no expr) "; 00337 00338 _out << endl; 00339 00340 in(); 00341 00342 tree_visitor::at_unary(the_unary); 00343 00344 out(); 00345 } 00346 00347 void print_tree_visitor::at_cast(castNode * the_cast) 00348 { 00349 indent(the_cast); 00350 if (the_cast->is_implicit()) 00351 _out << "implicit cast: "; 00352 else 00353 _out << "cast: "; 00354 00355 if (! the_cast->expr()) 00356 _out << "(no type) "; 00357 00358 _out << endl; 00359 00360 in(); 00361 00362 tree_visitor::at_cast(the_cast); 00363 00364 out(); 00365 } 00366 00367 void print_tree_visitor::at_comma(commaNode * the_comma) 00368 { 00369 indent(the_comma); 00370 _out << "comma: " << endl; 00371 00372 in(); 00373 00374 tree_visitor::at_comma(the_comma); 00375 00376 out(); 00377 } 00378 00379 void print_tree_visitor::at_ternary(ternaryNode * the_ternary) 00380 { 00381 indent(the_ternary); 00382 _out << "ternary: "; 00383 00384 if (! the_ternary->cond()) 00385 _out << "(no cond) "; 00386 00387 if (! the_ternary->true_br()) 00388 _out << "(no true branch) "; 00389 00390 if (! the_ternary->false_br()) 00391 _out << "(no false branch) "; 00392 00393 _out << endl; 00394 00395 in(); 00396 00397 tree_visitor::at_ternary(the_ternary); 00398 00399 out(); 00400 } 00401 00402 void print_tree_visitor::at_call(callNode * the_call) 00403 { 00404 indent(the_call); 00405 _out << "call: "; 00406 00407 if (! the_call->name()) 00408 _out << "(no object) "; 00409 00410 if (the_call->args().empty()) 00411 _out << "(no args) "; 00412 00413 _out << "proc=#" << (*_num)[the_call->proc()]; 00414 00415 _out << endl; 00416 00417 in(); 00418 00419 tree_visitor::at_call(the_call); 00420 00421 out(); 00422 } 00423 00424 void print_tree_visitor::at_initializer(initializerNode * the_initializer) 00425 { 00426 indent(the_initializer); 00427 _out << "initializer: "; 00428 00429 if (the_initializer->exprs().empty()) 00430 _out << "(no exprs) "; 00431 00432 _out << endl; 00433 00434 in(); 00435 00436 tree_visitor::at_initializer(the_initializer); 00437 00438 out(); 00439 } 00440 00441 void print_tree_visitor::at_block(blockNode * the_block) 00442 { 00443 indent(the_block); 00444 _out << "block: "; 00445 00446 if (the_block->decls().empty()) 00447 _out << "(no decls) "; 00448 00449 if (the_block->stmts().empty()) 00450 _out << "(no stmts) "; 00451 00452 _out << endl; 00453 00454 in(); 00455 00456 tree_visitor::at_block(the_block); 00457 00458 out(); 00459 } 00460 00461 void print_tree_visitor::at_exprstmt(exprstmtNode * the_exprstmt) 00462 { 00463 indent(the_exprstmt); 00464 _out << "exprstmt: "; 00465 00466 if (! the_exprstmt->expr()) 00467 _out << "(no expr) "; 00468 00469 _out << endl; 00470 00471 in(); 00472 00473 tree_visitor::at_exprstmt(the_exprstmt); 00474 00475 out(); 00476 } 00477 00478 void print_tree_visitor::at_label(labelNode * the_label) 00479 { 00480 indent(the_label); 00481 _out << "label: "; 00482 00483 if (the_label->name().empty()) 00484 _out << "(no name)"; 00485 else 00486 _out << "\"" << the_label->name() << "\" "; 00487 00488 if (! the_label->stmt()) 00489 _out << "(no stmt) "; 00490 00491 _out << " gotos=("; 00492 00493 goto_list & g = the_label->references(); 00494 for (goto_list_p p = g.begin(); 00495 p != g.end(); 00496 ++p) { 00497 if (p != g.begin()) 00498 _out << ", "; 00499 _out << "#" << (*_num)[*p]; 00500 } 00501 00502 _out << ")"; 00503 _out << endl; 00504 00505 in(); 00506 00507 tree_visitor::at_label(the_label); 00508 00509 out(); 00510 } 00511 00512 void print_tree_visitor::at_case(caseNode * the_case) 00513 { 00514 indent(the_case); 00515 00516 if (! the_case->expr()) 00517 _out << "default: "; 00518 else 00519 _out << "case: "; 00520 00521 if (! the_case->stmt()) 00522 _out << "(no stmt) "; 00523 00524 _out << "switch=#" << (*_num)[the_case->container()]; 00525 00526 _out << endl; 00527 00528 in(); 00529 00530 tree_visitor::at_case(the_case); 00531 00532 out(); 00533 } 00534 00535 void print_tree_visitor::at_if(ifNode * the_if) 00536 { 00537 indent(the_if); 00538 _out << "ifelse: "; 00539 00540 if (! the_if->expr()) 00541 _out << "(no expr) "; 00542 00543 if (! the_if->true_br()) 00544 _out << "(no true branch) "; 00545 00546 if (! the_if->false_br()) 00547 _out << "(no false branch) "; 00548 00549 _out << endl; 00550 00551 in(); 00552 00553 tree_visitor::at_if(the_if); 00554 00555 out(); 00556 } 00557 00558 void print_tree_visitor::at_switch(switchNode * the_switch) 00559 { 00560 indent(the_switch); 00561 _out << "switch: "; 00562 00563 if (! the_switch->expr()) 00564 _out << "(no expr) "; 00565 00566 if (! the_switch->stmt()) 00567 _out << "(no body) "; 00568 00569 _out << "cases=("; 00570 00571 target_list & g = the_switch->cases(); 00572 for (target_list_p p = g.begin(); 00573 p != g.end(); 00574 ++p) { 00575 if (p != g.begin()) 00576 _out << ", "; 00577 _out << "#" << (*_num)[*p]; 00578 } 00579 00580 _out << ")"; 00581 _out << endl; 00582 00583 in(); 00584 00585 tree_visitor::at_switch(the_switch); 00586 00587 out(); 00588 } 00589 00590 void print_tree_visitor::at_while(whileNode * the_while) 00591 { 00592 indent(the_while); 00593 _out << "while: "; 00594 00595 if (! the_while->cond()) 00596 _out << "(no cond) "; 00597 00598 if (! the_while->body()) 00599 _out << "(no body) "; 00600 00601 _out << endl; 00602 00603 in(); 00604 00605 tree_visitor::at_while(the_while); 00606 00607 out(); 00608 } 00609 00610 void print_tree_visitor::at_do(doNode * the_do) 00611 { 00612 indent(the_do); 00613 _out << "do: "; 00614 00615 if (! the_do->cond()) 00616 _out << "(no cond) "; 00617 00618 if (! the_do->body()) 00619 _out << "(no body) "; 00620 00621 _out << endl; 00622 00623 in(); 00624 00625 tree_visitor::at_do(the_do); 00626 00627 out(); 00628 } 00629 00630 void print_tree_visitor::at_for(forNode * the_for) 00631 { 00632 indent(the_for); 00633 _out << "for: "; 00634 00635 if (! the_for->init()) 00636 _out << "(no init) "; 00637 00638 if (! the_for->cond()) 00639 _out << "(no cond) "; 00640 00641 if (! the_for->next()) 00642 _out << "(no next) "; 00643 00644 if (! the_for->body()) 00645 _out << "(no body) "; 00646 00647 _out << endl; 00648 00649 in(); 00650 00651 tree_visitor::at_for(the_for); 00652 00653 out(); 00654 } 00655 00656 void print_tree_visitor::at_goto(gotoNode * the_goto) 00657 { 00658 indent(the_goto); 00659 00660 _out << "goto: "; 00661 _out << "\"" << the_goto->name() << "\" "; 00662 _out << "label=#" << (*_num)[the_goto->label()]; 00663 _out << endl; 00664 00665 in(); 00666 00667 tree_visitor::at_goto(the_goto); 00668 00669 out(); 00670 } 00671 00672 void print_tree_visitor::at_continue(continueNode * the_continue) 00673 { 00674 indent(the_continue); 00675 00676 _out << "continue: "; 00677 _out << "loop=#" << (*_num)[the_continue->container()]; 00678 _out << endl; 00679 00680 in(); 00681 00682 tree_visitor::at_continue(the_continue); 00683 00684 out(); 00685 } 00686 00687 void print_tree_visitor::at_break(breakNode * the_break) 00688 { 00689 indent(the_break); 00690 00691 _out << "break: "; 00692 _out << "container=#" << (*_num)[the_break->container()]; 00693 _out << endl; 00694 00695 in(); 00696 00697 tree_visitor::at_break(the_break); 00698 00699 out(); 00700 } 00701 00702 void print_tree_visitor::at_return(returnNode * the_return) 00703 { 00704 indent(the_return); 00705 00706 _out << "return: "; 00707 00708 if (! the_return->expr()) 00709 _out << "(no expr) "; 00710 00711 _out << "proc=#" << (*_num)[the_return->proc()]; 00712 _out << endl; 00713 00714 in(); 00715 00716 tree_visitor::at_return(the_return); 00717 00718 out(); 00719 } 00720 00721 void print_tree_visitor::at_attrib(attribNode * the_attrib) 00722 { 00723 indent(the_attrib); 00724 _out << "attrib: " << the_attrib->name() << endl; 00725 00726 in(); 00727 00728 tree_visitor::at_attrib(the_attrib); 00729 00730 out(); 00731 } 00732 00733 void print_tree_visitor::at_text(textNode * the_text) 00734 { 00735 indent(the_text); 00736 _out << "text: " << the_text->text() << endl; 00737 00738 in(); 00739 00740 tree_visitor::at_text(the_text); 00741 00742 out(); 00743 } 00744 00745 |
Generated on August 27, 2003
Back to the C-Breeze home page