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  

constnode.cc

Go to the documentation of this file.
00001 // $Id: constnode.cc,v 1.5 2003/08/07 23:13:02 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 // Constructor
00042 // ------------------------------------------------------------
00043 
00044 constNode::constNode(constant val, const char * text, const Coord coord)
00045   : indexNode(Const, 0, coord),
00046     _text(string(text))
00047 {
00048   value(val);
00049 
00050   if (val.is_str())
00051     // -- Special case for string..
00052     type(new arrayNode(typeNode::NONE,
00053                       new primNode(basic_type::Char),
00054                       new constNode(constant(strlen(val.Str())+1))));
00055   else
00056     if (val.is_ptr())
00057       // -- Special case for ptr..
00058       type(new ptrNode(typeNode::NONE, 
00059                        new primNode(basic_type::Void)));
00060     else
00061       // -- Otherwise just take the type from the constant..
00062       type(new primNode(val.basic()));
00063 }
00064 
00065 // ------------------------------------------------------------
00066 // Expression evaluator
00067 // ------------------------------------------------------------
00068 
00069 void constNode::eval()
00070 {
00071 
00072 }
00073 
00074 // ------------------------------------------------------------
00075 //  Walker
00076 // ------------------------------------------------------------
00077 
00078 void constNode::visit(Visitor * the_visitor) 
00079 {
00080   the_visitor->at_const(this);
00081 }
00082 
00083 void constNode::walk(Walker & the_walker)
00084 {
00085   Walker::Order ord = the_walker.order(); 
00086 
00087   if (ord == Walker::Preorder || ord == Walker::Both)
00088     the_walker.at_const(this, Walker::Preorder);
00089 
00090   if (the_walker.depth() == Walker::Subtree) {
00091     // -- Visit the children 
00092 
00093     if (type())
00094       type()->walk(the_walker);
00095   }
00096 
00097   if (ord == Walker::Postorder || ord == Walker::Both)
00098     the_walker.at_const(this, Walker::Postorder);
00099 }
00100 
00101 // ------------------------------------------------------------
00102 //  Dataflow
00103 // ------------------------------------------------------------
00104 
00105 void constNode::dataflow(FlowVal * v, FlowProblem & fp)
00106 {
00107   if (fp.forward()) {
00108     fp.flow_const(v, this, FlowProblem::Entry);
00109     fp.flow_const(v, this, FlowProblem::Exit);
00110   }
00111   else {
00112     fp.flow_const(v, this, FlowProblem::Exit);
00113     fp.flow_const(v, this, FlowProblem::Entry);
00114   }
00115 }
00116 
00117 // ------------------------------------------------------------
00118 // Output
00119 // ------------------------------------------------------------
00120 
00121 void constNode::output_expr(output_context & ct, Node * parent, int prec, Assoc assoc)
00122 {
00123   bool par = parens(prec, assoc);
00124 
00125   if (par)
00126     ct << '(';
00127 
00128   // SZG: Always regenerate the textual representation
00129   if (! text().empty())
00130     ct << text();
00131   else
00132     ct << value().to_string();
00133 
00134   if (par)
00135     ct << ')';
00136 }
00137 
00138 // ------------------------------------------------------------
00139 //  Changer
00140 // ------------------------------------------------------------
00141 
00142 Node * constNode::change(Changer & the_changer, bool redispatch)
00143 {
00144   Changer::Order ord = the_changer.order(); 
00145   constNode * the_const = this;
00146 
00147   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00148     the_const = (constNode *) the_changer.at_const(the_const, Changer::Preorder);
00149 
00150   if (the_const) {
00151 
00152     if (the_const != this)
00153       return the_const->change(the_changer, true);
00154 
00155     typeNode * old_type = the_const->type();
00156     if (old_type) {
00157       typeNode * new_type = (typeNode *) old_type->change(the_changer);
00158       if (old_type != new_type) {
00159         //if (the_changer.delete_old())
00160          // delete old_type;
00161         the_const->type(new_type);
00162       }
00163     }
00164 
00165   }
00166 
00167   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00168     the_const = (constNode *) the_changer.at_const(the_const, Changer::Postorder);
00169 
00170   return the_const;
00171 }
00172 
00173 
00174 // ------------------------------------------------------------
00175 // Destructor
00176 // ------------------------------------------------------------
00177 
00178 constNode::~constNode()
00179 {
00180 }

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