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  

tree_checker.h

Go to the documentation of this file.
00001 // $Id: tree_checker.h,v 1.3 2003/08/11 17:28:45 abrown 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 #ifndef CBZ_TREE_CHECKER_H
00039 #define CBZ_TREE_CHECKER_H
00040 
00041 #include "c_breeze.h"
00042 
00043 typedef list<procNode *> proc_list;
00044 typedef proc_list::iterator proc_list_p;
00045 typedef list<switchNode *> switch_list;
00046 typedef switch_list::iterator switch_list_p;
00047 typedef list<labelNode *> label_list;
00048 typedef label_list::iterator label_list_p;
00049 
00050 class OperatorCheck {
00051  public:
00052   virtual bool operator()(Operator *) = 0;
00053   virtual string desc() = 0;
00054 };
00055 
00056 class TreeChecker : public Walker {
00057 private:
00058   map<Node *, int> _visitedNodes;
00059   goto_list  _gotos;
00060   label_list _labels;
00061   proc_list _procs;
00062   proc_list _dupProcs;  // re-defined proc nodes 
00063   switch_list _switches;
00064   decl_list _decls;
00065   // indicates the number of gotoNodes that "point to" a labelNode
00066   map<labelNode *, int> _gotoCount;
00067   // argument count in procedures' formal parameter list
00068   map<string, int > _formals;
00069   int _typeDepth; // number of typeNodes we've traversed upto this Node
00070   int _arrayDepth; // number of arrayNodes we've traverse upto this Node
00071 
00072   bool _verbose;
00073   bool _warning;
00074   bool _preDismantled;
00075   bool _postDismantled;
00076   int _errorCount;
00077 
00078   void setPreDismantled(const char *, Node *);
00079   void setPostDismantled(const char *, Node *);
00080 
00081   void checkTree (Node * n, const char * type);
00082 
00083   void checkField (Node * node, Node * field, const char * type, 
00084                    const char * fieldName, bool isError = true);
00085   void checkString (Node * node, string & field, const char * type, 
00086                    const char * fieldName, bool isError = true);
00087   void checkConstant (Node * node, constant & field, const char * type, 
00088                    const char * fieldName, bool isError = true);
00089   template <class T>
00090     void checkOperator (Node * node, Operator * field, const char * type, 
00091                         const char * fieldName, T check, string desc,
00092                         bool isError = true);
00093   template <class T>
00094     void checkFieldInList (Node * node, T field, const char * type, 
00095                            const char * fieldName, list<T> & collection, 
00096                            bool isError = true);
00097   template <class T>
00098     void checkList (Node * node, list<T> & field, const char * type, 
00099                     const char * fieldName, bool isError = true);
00100   template <class T>
00101     void checkFieldInList (Node * node, list<T> & field, const char * type, 
00102                            const char * fieldName, int count, 
00103                            list<T> & collection, const char * itemName,
00104                            bool isError = true);
00105 
00106 public:
00107   TreeChecker ();
00108   TreeChecker (bool verbose, bool warning);
00109   virtual ~TreeChecker ();
00110 
00111   static void check();
00112   static void check(unitNode *);
00113 
00114   virtual void at_node (Node *, Order);
00115   virtual void at_unit (unitNode *, Order);
00116   virtual void at_proc (procNode *, Order);
00117   virtual void at_decl (declNode *, Order);
00118 
00119   // target node
00120   virtual void at_label (labelNode *, Order);
00121   virtual void at_case (caseNode *, Order);
00122 
00123   // jump node
00124   virtual void at_goto (gotoNode *, Order);
00125   virtual void at_conditiongoto (conditiongotoNode *, Order);
00126   virtual void at_break (breakNode *, Order);
00127   virtual void at_continue (continueNode *, Order);
00128   virtual void at_return (returnNode *, Order);
00129 
00130   // selection node
00131   virtual void at_if (ifNode *, Order);
00132   virtual void at_switch (switchNode *, Order);
00133 
00134   // loop node
00135   virtual void at_for (forNode *, Order);
00136   virtual void at_loop (loopNode *, Order);
00137 
00138   // expr node
00139   virtual void at_call (callNode *, Order);
00140   virtual void at_id (idNode *, Order);
00141   virtual void at_const (constNode *, Order);
00142   virtual void at_unary (unaryNode *, Order);
00143   virtual void at_binary (binaryNode *, Order);
00144   virtual void at_ternary (ternaryNode *, Order);
00145   virtual void at_threeAddr (threeAddrNode *, Order);
00146   virtual void at_operand (operandNode *, Order);
00147   virtual void at_cast (castNode *, Order);
00148   virtual void at_comma (commaNode *, Order);
00149   virtual void at_initializer (initializerNode *, Order);
00150 
00151   // type node
00152   virtual void at_type (typeNode *, Order);
00153   virtual void at_func (funcNode *, Order);
00154   virtual void at_tdef (tdefNode *, Order);
00155   virtual void at_array (arrayNode *, Order);
00156 };
00157 
00158 class ProcWalker : public Walker {
00159 public:
00160   ProcWalker () : Walker(Preorder, Subtree) {}
00161   virtual ~ProcWalker () {
00162     _labels.clear();
00163     _gotos.clear();
00164     _switches.clear();
00165     _gotoCount.clear();
00166     _decls.clear();
00167   }
00168 
00169   virtual void at_decl (declNode * the_decl, Order ord)
00170     { _decls.push_back(the_decl); }
00171   virtual void at_label (labelNode * the_label, Order ord)
00172     { _labels.push_back(the_label); }
00173   virtual void at_switch (switchNode * the_switch, Order ord)
00174     { _switches.push_back(the_switch); }
00175   virtual void at_goto (gotoNode * the_goto, Order ord) {
00176     _gotos.push_back(the_goto);
00177     // add a reference count to the label node
00178     if (the_goto->label()) _gotoCount[the_goto->label()]++;
00179   }
00180 
00181   const label_list & labels() const { return _labels; }
00182   const goto_list & gotos() const { return _gotos; }
00183   const switch_list & switches() const { return _switches; }
00184   const decl_list & decls() const { return _decls; }
00185   const map<labelNode *, int> & gotoCount() const { return _gotoCount; }
00186 
00187  private:
00188   label_list _labels;
00189   goto_list _gotos;
00190   switch_list _switches;
00191   decl_list _decls;
00192   // indicates the number of gotoNodes that "point to" a labelNode
00193   map<labelNode *, int> _gotoCount;
00194 };
00195 
00196 class UnitWalker : public Walker {
00197  public:
00198   UnitWalker();
00199   ~UnitWalker();
00200 
00201   virtual void at_proc(procNode *, Order);
00202   
00203   const proc_list & procs() const { return _procs; }
00204   const list<string> & procNames() const { return _procNames; }
00205   const proc_list & dupProcs() const { return _dupProcs; }
00206   const map<string, int> & formals() const { return _formals; }
00207  private:
00208   proc_list _procs;
00209   list<string> _procNames;
00210   proc_list _dupProcs;  // re-defined proc nodes
00211   // argument count in procedures' formal parameter list
00212   map<string, int> _formals;
00213 };
00214 
00215 #endif /* CBZ_TREE_CHECKER_H */

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