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  

blocknode.cc

Go to the documentation of this file.
00001 // $Id: blocknode.cc,v 1.6 2003/08/07 23:12:59 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 
00040 // --------------------------------------------------------------------
00041 // Constructors
00042 // --------------------------------------------------------------------
00043 
00044 blockNode::blockNode(decl_list * decls, stmt_list * stmts,
00045                      const Coord left_coord, const Coord right_brace)
00046   : stmtNode(Block, left_coord),
00047     _decls(),
00048     _stmts(),
00049     _right_brace(right_brace)
00050 {
00051   if (decls) {
00052     _decls.swap(* decls);
00053     //delete decls;
00054   }
00055 
00056   if (stmts) {
00057     _stmts.swap(* stmts);
00058     //delete stmts;
00059   }
00060 }
00061 
00062 // ------------------------------------------------------------
00063 // toBlock is used in the parser to ensure that statement blocks
00064 // with only one statement (and thus without braces) are properly
00065 // converted to blocks.
00066 // ------------------------------------------------------------
00067 
00068 blockNode * blockNode::toBlock(stmtNode *stmt, Coord coord)
00069 {
00070   if (stmt && (stmt->typ() != Block)) {
00071     stmt_list * stmts = new stmt_list();
00072     stmts->push_front(stmt);
00073     return new blockNode((decl_list *)0, stmts, coord);
00074   }
00075   else
00076     return (blockNode *)stmt;
00077 }
00078 
00079 // ------------------------------------------------------------
00080 // Data type base
00081 // ------------------------------------------------------------
00082 
00083 typeNode * blockNode::base_type(bool TdefIndir) const
00084 {
00085   return (typeNode *) 0;
00086 }
00087 
00088 // ------------------------------------------------------------
00089 //  Walker
00090 // ------------------------------------------------------------
00091 
00092 void blockNode::visit(Visitor * the_visitor) 
00093 {
00094   the_visitor->at_block(this);
00095 }
00096 
00097 void blockNode::walk(Walker & the_walker)
00098 {
00099   Walker::Order ord = the_walker.order(); 
00100 
00101   if (ord == Walker::Preorder || ord == Walker::Both)
00102     the_walker.at_block(this, Walker::Preorder);
00103 
00104   if (the_walker.depth() == Walker::Subtree) {
00105     // -- Visit the children 
00106 
00107     list_walker(decls(), the_walker);
00108     list_walker(stmts(), the_walker);
00109 
00110   }
00111 
00112   if (ord == Walker::Postorder || ord == Walker::Both)
00113     the_walker.at_block(this, Walker::Postorder);
00114 }
00115 
00116 // ------------------------------------------------------------
00117 //  Dataflow
00118 // ------------------------------------------------------------
00119 
00120 void blockNode::dataflow(FlowVal * v, FlowProblem & fp)
00121 {
00122   if (fp.forward()) {
00123     fp.flow_block(v, this, FlowProblem::Entry);
00124 
00125     dataflow_forward_list(decls(), v, fp);
00126     dataflow_forward_list(stmts(), v, fp);
00127 
00128     fp.flow_block(v, this, FlowProblem::Exit);
00129   }
00130   else {
00131     fp.flow_block(v, this, FlowProblem::Exit);
00132 
00133     dataflow_reverse_list(stmts(), v, fp);
00134     dataflow_reverse_list(decls(), v, fp);
00135 
00136     fp.flow_block(v, this, FlowProblem::Entry);
00137   }
00138 }
00139 
00140 // ------------------------------------------------------------
00141 // Output
00142 // ------------------------------------------------------------
00143 
00144 void blockNode::output_stmt(output_context & ct, Node * parent)
00145 {
00146   int ds = decls().size();
00147   int ss = stmts().size();
00148   int l = ds + ss;
00149   NodeType parent_type;
00150 
00151   if (parent)
00152     parent_type = parent->typ();
00153   else
00154     parent_type = Proc;
00155 
00156   if ((parent_type == Block) ||
00157       (parent_type == Proc))
00158     ct.new_line();
00159 
00160   if ((l != 1) || (parent_type == Proc) || (parent_type == If)) {
00161     ct << '{';
00162     CBZ::current_unit->enter_scope();
00163   }
00164 
00165   ct.indent_in();
00166 
00167   output_comment(ct);
00168 
00169   output_list(decls(), ct, this);
00170 
00171   if ((ds > 0) && (ss > 0))
00172     ct.new_line();
00173 
00174   output_list(stmts(), ct, this);
00175 
00176   ct.indent_out();
00177 
00178   if ((l != 1) || (parent_type == Proc) || (parent_type == If)) {
00179 
00180     // an suespec that was output at this scope level should
00181     // be marked as having not been output, because it has gone
00182     // out of scope. -- djimenez
00183 
00184 #ifdef FOOO
00185     // oops.  this causes us to get an error in spec vortex.
00186     for (suespec_list_p p=CBZ::current_unit->suespecs().begin();
00187             p!=CBZ::current_unit->suespecs().end(); p++) {
00188                 if ((*p)->scope_output() >= CBZ::current_unit->symbol_level()) {
00189                         (*p)->already_output(false);
00190                 }
00191     }
00192 #endif
00193     CBZ::current_unit->exit_scope();
00194     ct.new_line();
00195     ct << '}';
00196   }
00197 }
00198 
00199 // ------------------------------------------------------------
00200 //  Changer
00201 // ------------------------------------------------------------
00202 
00203 Node * blockNode::change(Changer & the_changer, bool redispatch)
00204 {
00205   Changer::Order ord = the_changer.order(); 
00206   blockNode * the_block = this;
00207 
00208   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00209     the_block = (blockNode *) the_changer.at_block(the_block, Changer::Preorder);
00210 
00211   if (the_block) {
00212 
00213     if (the_block != this)
00214       return the_block->change(the_changer, true);
00215 
00216     change_list(the_block->decls(), the_changer);
00217 
00218     change_list(the_block->stmts(), the_changer);
00219   }
00220 
00221   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00222     the_block = (blockNode *) the_changer.at_block(the_block, Changer::Postorder);
00223 
00224   return the_block;
00225 }
00226 
00227 
00228 // ------------------------------------------------------------
00229 // Destructor
00230 // ------------------------------------------------------------
00231 
00232 blockNode::~blockNode()
00233 {
00234   //delete_list(_decls);
00235   //delete_list(_stmts);
00236 }

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