C-Breeze
C Compiler Infrastructure

[ Project home page]

tree_checker.h

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

Generated on February 1, 2006
Back to the C-Breeze home page