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  

set_container_walker.cc

Go to the documentation of this file.
00001 // $Id: set_container_walker.cc,v 1.6 2003/08/07 23:13:53 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 "set_container_walker.h"
00040 
00041 // ------------------------------------------------------------
00042 // Entry point
00043 // ------------------------------------------------------------
00044 
00045 void set_container_walker::fixup(Node * n)
00046 {
00047   set_container_walker s;
00048   n->walk(s);
00049 }
00050 
00051 // ------------------------------------------------------------
00052 // Search the stack of containers...
00053 // ------------------------------------------------------------
00054 
00055 stmtNode * set_container_walker::nearest()
00056 {
00057   return _stack.front();
00058 }
00059 
00060 switchNode * set_container_walker::nearest_switch()
00061 {
00062   stmt_list_p p;
00063 
00064   p = _stack.begin();
00065   while (p != _stack.end()) {
00066     if ((*p)->typ() == Switch)
00067       return (switchNode *)(*p);
00068     ++p;
00069   }
00070 
00071   return (switchNode *) 0;
00072 }
00073 
00074 loopNode * set_container_walker::nearest_loop()
00075 {
00076   stmt_list_p p;
00077 
00078   p = _stack.begin();
00079   while (p != _stack.end()) {
00080     NodeType t = (*p)->typ();
00081     if ((t == For) || (t == While) || (t == Do))
00082       return (loopNode *)(*p);
00083     ++p;
00084   }
00085 
00086   return (loopNode *) 0;
00087 }
00088 
00089 // ------------------------------------------------------------
00090 // Use the container stack to set the appropriate
00091 // container data member
00092 // ------------------------------------------------------------
00093 
00094 void set_container_walker::at_case(caseNode * the_case, Order ord)
00095 {
00096   if (ord == Postorder) return;
00097 
00098   // -- Find the nearest enclosing switch statement
00099   switchNode * sw = nearest_switch();
00100 
00101   if (sw) {
00102     // -- If found, set the container and add to the case list
00103 
00104     the_case->container(sw);
00105 
00106     // -- If necessary, indicate that this switch has a default case
00107 
00108     if ( ! the_case->expr())
00109       sw->has_default(true);
00110 
00111   } else
00112     // -- Othewise, issue an error
00113     CBZ::SyntaxError(the_case->coord(), "no enclosing switch statement");
00114 }
00115 
00116 void set_container_walker::at_continue(continueNode * the_continue, Order ord)
00117 {
00118   if (ord == Postorder) return;
00119 
00120   // -- Find the nearest enclosing loop
00121 
00122   loopNode * lp = nearest_loop();
00123 
00124   if (lp)
00125     the_continue->container(lp);
00126   else
00127     CBZ::SyntaxError(the_continue->coord(), "no enclosing loop");
00128 }
00129 
00130 void set_container_walker::at_break(breakNode * the_break, Order ord)
00131 {
00132   if (ord == Postorder) return;
00133 
00134   // -- Find the nearest enclosing loop or switch
00135 
00136   stmtNode * st = nearest();
00137 
00138   if (st)
00139     the_break->container(st);
00140   else
00141     CBZ::SyntaxError(the_break->coord(),
00142                      "no enclosing loop or switch statement found");
00143 }

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