Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

oldtypechecker.hh

Go to the documentation of this file.
00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; -*-
00002 
00003 /* libutap - Uppaal Timed Automata Parser.
00004    Copyright (C) 2002 Uppsala University and Aalborg University.
00005    
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public License
00008    as published by the Free Software Foundation; either version 2.1 of
00009    the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful, but
00012    WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with this library; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00019    USA
00020 */
00021 
00022 #ifndef UTAP_TYPECHECKER_HH
00023 #define UTAP_TYPECHECKER_HH
00024 
00025 #include <vector>
00026 #include <inttypes.h>
00027 
00028 #include "utap/builder.hh"
00029 #include "utap/utap.hh"
00030 
00031 // The OldTypeChecker is a ParserBuilder, which can
00032 //
00033 //  a) construct an IntermediateSystem
00034 //  b) ensure that the system is type correct
00035 //  c) ensure that properties are type correct
00036 // 
00037 // It might be operated in one of two modes depending on whether
00038 // strict ranging checking is turned on or not. If it is on,
00039 // one might only assign use expression which are guaranteed to
00040 // be within the range of the variable it is assigned to.
00041 //
00042 // Properties are checked, but the OldTypeChecker class must be
00043 // subtyped in order actually get access to the properties as they are
00044 // not stored by the type check builder.
00045 //
00046 // Internally, two stacks are used to keep track of fragments of 
00047 // the system:
00048 //
00049 //   - Expressions are kept on the stack with 
00050 //     expression fragments.
00051 //   - Types are kept on the stack with type fragments.
00052 //
00053 // These stacks are used while building more complex expressions
00054 // and types. Each stack is indexed from the top, i.e. the top
00055 // element is indexed as 0.
00056 //
00057 
00058 namespace UTAP
00059 {
00060     
00061     class OldTypeChecker : public ParserBuilder, 
00062                         protected TimedAutomataStructures
00063     {
00064     protected:
00065         // Error message for unsupported features
00066         static const char *const unsupported;
00067         static const char *const invalid_type;
00068 
00069         /* Flag used to determine how strict we check for inclusion 
00070            between ranges of integers. TODO: Replace this with a
00071            system, which reports a warning if there is a potential
00072            overflow. */
00073         bool strict_range;
00074 
00075         /* Type used for booleans */
00076         type_t BOOL;
00077 
00078         /* Pointer to the intermediate system under construction */
00079         TimedAutomataSystem *system;
00080 
00081         /* A pointer to the error handler */
00082         ErrorHandler *errorHandler;
00083 
00084         /* The currene position */
00085         position_t position;
00086         
00087     private:
00088         //
00089         // The stack of expression fragments.
00090         //
00091         class ExpressionFragments 
00092         {
00093         private:
00094             std::vector<ExpressionProgram> data;
00095         public:
00096             ExpressionProgram &operator[] (int idx)
00097                 { return data[data.size() - idx - 1]; }
00098             void push(const ExpressionProgram &e)
00099                 { data.push_back(e); }
00100             void pop()
00101                 { data.pop_back(); }
00102             void pop(int n);
00103             void merge(uint32_t number);
00104             uint32_t size() { return data.size(); }
00105         } fragments;
00106 
00107         //
00108         // The stack of type fragments.
00109         //
00110         class TypeFragments 
00111         {
00112         private:
00113             std::vector<type_t> data;
00114         public:
00115             type_t &operator[] (int idx)
00116                 { return data[data.size() - idx - 1]; }
00117             void push(type_t value)
00118                 { data.push_back(value); }
00119             void pop()
00120                 { assert(!data.empty()); data.pop_back(); }
00121         } typeFragments;
00122 
00123         // 
00124         // Support for handling template and function parameters
00125         //
00126 
00127         /* The params vector is used temporarily during parameter parsing */
00128         frame_t params;
00129 
00130         /* Current frame */
00131         frame_t frame;
00132 
00133         /* current parsing function structure */
00134         function_t *currentFun;
00135 
00136         /* stack of function body statement blocks */
00137         std::vector<BlockStatement*> blocks; 
00138 
00139         //
00140         // Fields for handling transition labels
00141         //
00142         int32_t guard;
00143         int32_t sync;
00144         int32_t update;
00145 
00146         //
00147         // Fields for function call and template instantiation handling
00148         //
00149         
00150         /* list of expected arguments */
00151         std::list<std::vector<type_t> > expectedArguments;
00152 
00153         /* stack of function or template uids */
00154         std::vector<symbol_t> identifierStack;
00155 
00156         //
00157         // Private functions
00158         // 
00159 
00160         //
00161         // Method for handling types
00162         //
00163 
00164         type_t applyPrefix(uint32_t, type_t);
00165         void checkClass(type_t type, type_t expected, const char *err);
00166         void checkLValue(type_t type, const char *err);
00167         void checkAssignmentCompatible(type_t lvalue, type_t rvalue);
00168         void checkLocation(symbol_t uid, const char *err);
00169         // isParameterCompatible returns static error message or NULL if ok.
00170         char* isParameterCompatible(type_t argType, type_t paramValue);
00171         type_t buildArrayType(type_t type, uint32_t dim);
00172         type_t makeConstantIf(bool cond, type_t type);
00173         type_t makeSideEffectFreeIf(bool cond, type_t type);
00174         type_t makeReferenceIf(bool cond, type_t type);
00175         bool isInteger(type_t type);
00176         bool isClock(type_t type);
00177         bool isDiff(type_t type);
00178         bool isInvariant(type_t type);
00179         bool isGuard(type_t type);
00180         bool isConstraint(type_t type);
00181         bool isTemporary(ExpressionProgram &expr);
00182         type_t typeOfBinaryIntExpression(
00183             type_t left, uint32_t op, type_t right);    
00184         type_t typeOfUnaryMinus(type_t type);
00185         type_t getElementTypeOfArray(type_t type);
00186         int32_t findFieldInRecord(const std::vector<std::pair<char *, int> > &, const char *);
00187         void checkGuardSynchronisationConflict();
00188 
00189         //
00190         // Methods for handling expressions
00191         //
00192 
00193         void exprBinaryInt(type_t left, uint32_t binaryop, type_t right);
00194         void exprBinaryNonInt(type_t left, uint32_t binaryop, type_t right);
00195         void checkInitialiser(type_t type, SubExpression init, bool required);
00196         type_t combineInlineTypes(type_t t, type_t e);
00197         void insertDefault(ExpressionProgram &expr,
00198                            ExpressionProgram::iterator pos,
00199                            type_t type);
00200         ExpressionProgram::expression_t makeConstant(int value);
00201 
00202     protected:
00203 
00205         // allowProcessReferences()
00207         // If this method returns true, it is allowed to access the 
00208         // private identifiers of a process by prefixing the identifier
00209         // with the process name. 
00210         //
00211         // This is only interesting when parsing properties. In this case
00212         // the method should be overridden by a sub class.
00213         virtual bool allowProcessReferences() { return false; }
00214 
00216         // property()
00218         // This method is called once for each property, which has
00219         // been parsed. The default implementation does nothing, so
00220         // you need to override this in a subclass.
00221         virtual void property(int kind, int line, ExpressionProgram &) {};
00222 
00223     public:
00224 
00225         OldTypeChecker(TimedAutomataSystem *);
00226 
00227         virtual void setErrorHandler(ErrorHandler *);
00228         virtual void setPosition(const position_t &);
00229 
00230         /************************************************************
00231          * Query functions
00232          */
00233         virtual bool isType(const char*);
00234         virtual bool isLocation(const char*);
00235 
00236         /************************************************************
00237          * Types
00238          */
00239         virtual void declType(uint32_t prefix, const char* name, bool range);
00240         // 2 expr if range==true
00241         virtual void declStruct(uint32_t prefix, uint32_t fields);
00242         virtual void declField(const char* name, uint32_t dim); 
00243         // 1 type and dim array sizes
00244         virtual void declFieldEnd();
00245         virtual void declTypeDef(const char* name, uint32_t dim); 
00246         // 1 type and dim array sizes
00247         virtual void declTypeDefEnd();
00248 
00249         /************************************************************
00250          * Variable declarations
00251          */
00252         virtual void declVar(const char* name, uint32_t dim, bool init); 
00253         // 1 type, dims, initializer if init==true
00254         virtual void declVarEnd();
00255         virtual void declInitialiserList(uint32_t num); // n initialisers
00256         virtual void declFieldInit(const char* name); // 1 initialiser
00257     
00258         /************************************************************
00259          * Function declarations
00260          */
00261         virtual void declParameter(const char* name, bool reference, uint32_t dim);
00262         // 1 type, dim array sizes
00263         virtual void declParameterEnd(); // pop parameter type
00264     
00265         virtual void declFuncBegin(const char* name, uint32_t n); // n paramaters
00266         virtual void declFuncEnd(); // 1 block
00267 
00268         /************************************************************
00269          * Process declarations
00270          */
00271         virtual void procBegin(const char* name, uint32_t n); // n parameters
00272         virtual void procEnd(); // 1 ProcBody
00273         virtual void procState(const char* name); // 1 expr
00274         virtual void procStateCommit(const char* name); // mark previously decl. state
00275         virtual void procStateUrgent(const char* name); // mark previously decl. state
00276         virtual void procStateInit(const char* name); // mark previously decl. state
00277         virtual void procTransition(const char* from, const char* to); 
00278         // 1 epxr,1sync,1expr
00279         virtual void procGuard();
00280         virtual void procSync(uint32_t type); // 1 expr
00281         virtual void procUpdate();
00282     
00283         /************************************************************
00284          * Statements
00285          */
00286         virtual void blockBegin();
00287         virtual void blockEnd();
00288         virtual void emptyStatement();
00289         virtual void forBegin();
00290         virtual void forEnd(); // 3 expr, 1 stat
00291         virtual void whileBegin();
00292         virtual void whileEnd(); // 1 expr, 1 stat
00293         virtual void doWhileBegin();
00294         virtual void doWhileEnd(); // 1 stat, 1 expr
00295         virtual void ifBegin();
00296         virtual void ifElse();
00297         virtual void ifEnd(bool); // 1 expr, 1 or 2 statements
00298         virtual void breakStatement();
00299         virtual void continueStatement();
00300         virtual void switchBegin();
00301         virtual void switchEnd(); // 1 expr, 1+ case/default
00302         virtual void caseBegin();
00303         virtual void caseEnd();  // 1 expr, 0+ stat
00304         virtual void defaultBegin();
00305         virtual void defaultEnd(); // 0+ statements
00306         virtual void exprStatement(); // 1 expr
00307         virtual void returnStatement(bool); // 1 expr if argument is true
00308     
00309         /************************************************************
00310          * Expressions
00311          */
00312         virtual void exprTrue();
00313         virtual void exprFalse();
00314         virtual void exprId(const char * varName);
00315         virtual void exprNat(int32_t); // natural number
00316         virtual void exprCallBegin(const char * functionName);
00317         virtual void exprCallEnd(uint32_t n); // n exprs as arguments
00318         virtual void exprArg(uint32_t n); // 1 exprs as n-th argument for fn-call
00319         virtual void exprArray(); // 2 expr 
00320         virtual void exprPostIncrement(); // 1 expr
00321         virtual void exprPreIncrement(); // 1 expr
00322         virtual void exprPostDecrement(); // 1 expr
00323         virtual void exprPreDecrement(); // 1 expr
00324         virtual void exprAssignment(uint32_t op); // 2 expr
00325         virtual void exprUnary(uint32_t unaryop); // 1 expr
00326         virtual void exprBinary(uint32_t binaryop); // 2 expr
00327         virtual void exprInlineIf(); // 3 expr
00328         virtual void exprComma(); // 2 expr
00329         virtual void exprDot(const char *); // 1 expr
00330         virtual void exprDeadlock();
00331     
00332         /************************************************************
00333          * System declaration
00334          */
00335         virtual void instantiationBegin(const char*, const char*);
00336         virtual void instantiationEnd(const char *, const char *, uint32_t n); // n arguments
00337         virtual void process(const char*);
00338 
00339         virtual void done();    
00340 
00341         virtual void property(uint32_t kind, int line);
00342 
00343         /********************************************************************
00344          * Guiding
00345          */
00346 
00347         virtual void beforeUpdate();
00348         virtual void afterUpdate();
00349     };
00350 }
00351 #endif

Generated on Wed Jul 2 12:08:18 2003 for libutap by doxygen 1.3.2