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  

idnode.cc

Go to the documentation of this file.
00001 // $Id: idnode.cc,v 1.9 2003/08/07 23:13:06 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 "ref_clone_changer.h"
00040 
00041 // --------------------------------------------------------------------
00042 // Constructors
00043 // --------------------------------------------------------------------
00044 
00045 idNode::idNode(const char * text, const Coord coord)
00046   : indexNode(Id, 0, coord),
00047     _name(string(text)),
00048     _decl(0)
00049 {}
00050 
00051 idNode::idNode(declNode * the_decl, const Coord coord)
00052   : indexNode(Id, 0, coord),
00053     _name(the_decl->name()),
00054     _decl(0)
00055 {
00056   if (the_decl) {
00057     _name = the_decl->name();
00058     this->decl(the_decl);
00059   }
00060 }
00061 
00062 // ------------------------------------------------------------
00063 // Data type base
00064 // ------------------------------------------------------------
00065 
00066 typeNode * idNode::base_type(bool TdefIndir) const
00067 {
00068   declNode * d = decl();
00069 
00070   if (d)
00071     return d->type()->base_type(TdefIndir);
00072   else
00073     return (typeNode *)0;
00074 }
00075 
00076 void idNode::decl(declNode * the_decl) {
00077   declNode * old_decl = _decl;
00078   _decl = the_decl;
00079   if ( old_decl )
00080     old_decl->ref_list().remove(this);
00081   if ( the_decl ) {
00082     the_decl->ref_list().push_back(this);
00083     the_decl->ref_list().sort();
00084     the_decl->ref_list().unique();
00085     // TODO: is this the right way to handle it?
00086     type((typeNode *) ref_clone_changer::clone(the_decl->type(), false));
00087   }
00088 }
00089 
00090 // ------------------------------------------------------------
00091 // Symbol lookup
00092 // ------------------------------------------------------------
00093 
00094 // -- LookupPostfixExpression in type.c
00095 // This function performs the initial lookup of an identifier
00096 // and establishes a pointer to it's declaration.
00097 
00098 /*
00099 void idNode::lookup()
00100 {
00101   declNode * var;
00102   const string my_name = name();
00103 
00104   var = CBZ::current_unit->ids()->lookup(my_name);
00105 
00106   if (! var) {
00107 
00108     // -- Declaration not found...
00109 
00110     var = 0;
00111     CBZ::SyntaxError(coord(), string("undeclared variable `") +
00112                      my_name + string("'"));
00113   } else {
00114 
00115     // -- Declaration found...
00116 
00117     var->inc_references();
00118 
00119     if (Symbols::TrackIds)
00120       cerr << "=== `" << my_name << "' ===" << endl;
00121   }
00122 
00123   // -- Point to the declaration
00124 
00125   decl(var);
00126 }
00127 
00128 // -- For the parser
00129 
00130 idNode * idNode::lookup_and()
00131 {
00132   lookup();
00133   return this;
00134 }
00135 */
00136 
00137 // ------------------------------------------------------------
00138 // Expression evaluator
00139 // ------------------------------------------------------------
00140 
00141 void idNode::eval()
00142 {
00143   // -- Check for enumerated type...
00144 
00145   declNode * ed = decl();
00146   if (ed && (ed->decl_location() == declNode::ENUM)) {
00147 
00148     // -- It is a constant from an enum...find it's value
00149 
00150     exprNode * e = ed->init();
00151     if (e) {
00152       e->eval();
00153       value(constant(e->value()));
00154       return;
00155     }
00156   }
00157 
00158   // -- Default: no value
00159 
00160   value(constant());
00161 }
00162 
00163 // ------------------------------------------------------------
00164 //  Walker
00165 // ------------------------------------------------------------
00166 
00167 void idNode::visit(Visitor * the_visitor) 
00168 {
00169   the_visitor->at_id(this);
00170 }
00171 
00172 void idNode::walk(Walker & the_walker)
00173 {
00174   Walker::Order ord = the_walker.order(); 
00175 
00176   if (ord == Walker::Preorder || ord == Walker::Both)
00177     the_walker.at_id(this, Walker::Preorder);
00178 
00179   if (the_walker.depth() == Walker::Subtree) {
00180     // -- Visit the children 
00181 
00182     if (type())
00183       type()->walk(the_walker);
00184   }
00185 
00186   if (ord == Walker::Postorder || ord == Walker::Both)
00187     the_walker.at_id(this, Walker::Postorder);
00188 }
00189 
00190 // ------------------------------------------------------------
00191 //  Dataflow
00192 // ------------------------------------------------------------
00193 
00194 void idNode::dataflow(FlowVal * v, FlowProblem & fp)
00195 {
00196   if (fp.forward()) {
00197     fp.flow_id(v, this, FlowProblem::Entry);
00198     fp.flow_id(v, this, FlowProblem::Exit);
00199   }
00200   else {
00201     fp.flow_id(v, this, FlowProblem::Exit);
00202     fp.flow_id(v, this, FlowProblem::Entry);
00203   }
00204 }
00205 
00206 // ------------------------------------------------------------
00207 // Output
00208 // ------------------------------------------------------------
00209 
00210 void idNode::output_expr(output_context & ct, Node * parent, int prec, Assoc assoc)
00211 {
00212   bool par = parens(prec, assoc);
00213 
00214   if (par)
00215     ct << '(';
00216 
00217   ct << name();
00218 
00219   if (par)
00220     ct << ')';
00221 }
00222 
00223 // ------------------------------------------------------------
00224 //  Changer
00225 // ------------------------------------------------------------
00226 
00227 Node * idNode::change(Changer & the_changer, bool redispatch)
00228 {
00229   Changer::Order ord = the_changer.order(); 
00230   idNode * the_id = this;
00231 
00232   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00233     the_id = (idNode *) the_changer.at_id(the_id, Changer::Preorder);
00234 
00235   if (the_id) {
00236 
00237     if (the_id != this)
00238       return the_id->change(the_changer, true);
00239 
00240     typeNode * old_type = the_id->type();
00241     if (old_type) {
00242       typeNode * new_type = (typeNode *) old_type->change(the_changer);
00243       if (old_type != new_type) {
00244         //if (the_changer.delete_old())
00245           //delete old_type;
00246         the_id->type(new_type);
00247       }
00248     }
00249 
00250   }
00251 
00252   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00253     the_id = (idNode *) the_changer.at_id(the_id, Changer::Postorder);
00254 
00255   return the_id;
00256 }
00257 
00258 
00259 // ------------------------------------------------------------
00260 // Destructor
00261 // ------------------------------------------------------------
00262 
00263 idNode::~idNode()
00264 {
00265 }

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