| [0dd3a2f] | 1 | //
 | 
|---|
 | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
 | 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
 | 6 | //
 | 
|---|
| [9cb8e88d] | 7 | // Validate.cc --
 | 
|---|
| [0dd3a2f] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Sun May 17 21:50:04 2015
 | 
|---|
| [4e06c1e] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
 | 12 | // Last Modified On : Tue Jul 12 17:49:21 2016
 | 
|---|
 | 13 | // Update Count     : 298
 | 
|---|
| [0dd3a2f] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
 | 
|---|
 | 17 | // as regular in structure as possible.  Some assumptions can be made regarding the state of the tree after this pass is
 | 
|---|
 | 18 | // complete, including:
 | 
|---|
 | 19 | //
 | 
|---|
 | 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
 | 
|---|
 | 21 | //   union.
 | 
|---|
 | 22 | //
 | 
|---|
 | 23 | // - All enumeration constants have type EnumInstType.
 | 
|---|
 | 24 | //
 | 
|---|
 | 25 | // - The type "void" never occurs in lists of function parameter or return types; neither do tuple types.  A function
 | 
|---|
 | 26 | //   taking no arguments has no argument types, and tuples are flattened.
 | 
|---|
 | 27 | //
 | 
|---|
 | 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
 | 
|---|
 | 29 | //   by the particular set of type arguments.
 | 
|---|
 | 30 | //
 | 
|---|
 | 31 | // - Every declaration is assigned a unique id.
 | 
|---|
 | 32 | //
 | 
|---|
 | 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance.
 | 
|---|
 | 34 | //
 | 
|---|
 | 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator.
 | 
|---|
 | 36 | //
 | 
|---|
 | 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that
 | 
|---|
 | 38 | //   definition occurs later in the input.
 | 
|---|
| [51b73452] | 39 | 
 | 
|---|
 | 40 | #include <list>
 | 
|---|
 | 41 | #include <iterator>
 | 
|---|
| [630a82a] | 42 | #include "Common/utility.h"
 | 
|---|
 | 43 | #include "Common/UniqueName.h"
 | 
|---|
| [51b73452] | 44 | #include "Validate.h"
 | 
|---|
 | 45 | #include "SynTree/Visitor.h"
 | 
|---|
 | 46 | #include "SynTree/Mutator.h"
 | 
|---|
 | 47 | #include "SynTree/Type.h"
 | 
|---|
| [630a82a] | 48 | #include "SynTree/Expression.h"
 | 
|---|
| [51b73452] | 49 | #include "SynTree/Statement.h"
 | 
|---|
 | 50 | #include "SynTree/TypeSubstitution.h"
 | 
|---|
| [68cd1ce] | 51 | #include "Indexer.h"
 | 
|---|
| [51b73452] | 52 | #include "FixFunction.h"
 | 
|---|
| [cc79d97] | 53 | // #include "ImplementationType.h"
 | 
|---|
| [630a82a] | 54 | #include "GenPoly/DeclMutator.h"
 | 
|---|
| [51b73452] | 55 | #include "AddVisit.h"
 | 
|---|
| [f6d7e0f] | 56 | #include "MakeLibCfa.h"
 | 
|---|
| [cc79d97] | 57 | #include "TypeEquality.h"
 | 
|---|
| [620cb95] | 58 | #include "Autogen.h"
 | 
|---|
| [1cbca6e] | 59 | #include "ResolvExpr/typeops.h"
 | 
|---|
| [51b73452] | 60 | 
 | 
|---|
| [c8ffe20b] | 61 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
 | 
|---|
| [51b73452] | 62 | 
 | 
|---|
 | 63 | namespace SymTab {
 | 
|---|
| [a08ba92] | 64 |         class HoistStruct : public Visitor {
 | 
|---|
 | 65 |           public:
 | 
|---|
| [82dd287] | 66 |                 /// Flattens nested struct types
 | 
|---|
| [0dd3a2f] | 67 |                 static void hoistStruct( std::list< Declaration * > &translationUnit );
 | 
|---|
| [9cb8e88d] | 68 | 
 | 
|---|
| [0dd3a2f] | 69 |                 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
 | 
|---|
| [9cb8e88d] | 70 | 
 | 
|---|
| [0dd3a2f] | 71 |                 virtual void visit( StructDecl *aggregateDecl );
 | 
|---|
 | 72 |                 virtual void visit( UnionDecl *aggregateDecl );
 | 
|---|
| [c8ffe20b] | 73 | 
 | 
|---|
| [0dd3a2f] | 74 |                 virtual void visit( CompoundStmt *compoundStmt );
 | 
|---|
 | 75 |                 virtual void visit( SwitchStmt *switchStmt );
 | 
|---|
| [a08ba92] | 76 |           private:
 | 
|---|
| [0dd3a2f] | 77 |                 HoistStruct();
 | 
|---|
| [c8ffe20b] | 78 | 
 | 
|---|
| [0dd3a2f] | 79 |                 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
 | 
|---|
| [c8ffe20b] | 80 | 
 | 
|---|
| [0dd3a2f] | 81 |                 std::list< Declaration * > declsToAdd;
 | 
|---|
 | 82 |                 bool inStruct;
 | 
|---|
| [a08ba92] | 83 |         };
 | 
|---|
| [c8ffe20b] | 84 | 
 | 
|---|
| [de91427b] | 85 |         /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
 | 
|---|
| [a08ba92] | 86 |         class Pass1 : public Visitor {
 | 
|---|
| [0dd3a2f] | 87 |                 typedef Visitor Parent;
 | 
|---|
 | 88 |                 virtual void visit( EnumDecl *aggregateDecl );
 | 
|---|
 | 89 |                 virtual void visit( FunctionType *func );
 | 
|---|
| [a08ba92] | 90 |         };
 | 
|---|
| [82dd287] | 91 | 
 | 
|---|
 | 92 |         /// Associates forward declarations of aggregates with their definitions
 | 
|---|
| [a08ba92] | 93 |         class Pass2 : public Indexer {
 | 
|---|
| [0dd3a2f] | 94 |                 typedef Indexer Parent;
 | 
|---|
| [a08ba92] | 95 |           public:
 | 
|---|
| [0dd3a2f] | 96 |                 Pass2( bool doDebug, const Indexer *indexer );
 | 
|---|
| [a08ba92] | 97 |           private:
 | 
|---|
| [0dd3a2f] | 98 |                 virtual void visit( StructInstType *structInst );
 | 
|---|
 | 99 |                 virtual void visit( UnionInstType *unionInst );
 | 
|---|
| [4040425] | 100 |                 virtual void visit( TraitInstType *contextInst );
 | 
|---|
| [0dd3a2f] | 101 |                 virtual void visit( StructDecl *structDecl );
 | 
|---|
 | 102 |                 virtual void visit( UnionDecl *unionDecl );
 | 
|---|
 | 103 |                 virtual void visit( TypeInstType *typeInst );
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |                 const Indexer *indexer;
 | 
|---|
| [9cb8e88d] | 106 | 
 | 
|---|
| [0dd3a2f] | 107 |                 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
 | 
|---|
 | 108 |                 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
 | 
|---|
 | 109 |                 ForwardStructsType forwardStructs;
 | 
|---|
 | 110 |                 ForwardUnionsType forwardUnions;
 | 
|---|
| [a08ba92] | 111 |         };
 | 
|---|
| [c8ffe20b] | 112 | 
 | 
|---|
| [82dd287] | 113 |         /// Replaces array and function types in forall lists by appropriate pointer type
 | 
|---|
| [a08ba92] | 114 |         class Pass3 : public Indexer {
 | 
|---|
| [0dd3a2f] | 115 |                 typedef Indexer Parent;
 | 
|---|
| [a08ba92] | 116 |           public:
 | 
|---|
| [0dd3a2f] | 117 |                 Pass3( const Indexer *indexer );
 | 
|---|
| [a08ba92] | 118 |           private:
 | 
|---|
| [0dd3a2f] | 119 |                 virtual void visit( ObjectDecl *object );
 | 
|---|
 | 120 |                 virtual void visit( FunctionDecl *func );
 | 
|---|
| [c8ffe20b] | 121 | 
 | 
|---|
| [0dd3a2f] | 122 |                 const Indexer *indexer;
 | 
|---|
| [a08ba92] | 123 |         };
 | 
|---|
| [c8ffe20b] | 124 | 
 | 
|---|
| [de91427b] | 125 |         class ReturnChecker : public Visitor {
 | 
|---|
 | 126 |           public:
 | 
|---|
 | 127 |                 /// Checks that return statements return nothing if their return type is void
 | 
|---|
 | 128 |                 /// and return something if the return type is non-void.
 | 
|---|
 | 129 |                 static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
 | 
|---|
 | 130 |           private:
 | 
|---|
 | 131 |                 virtual void visit( FunctionDecl * functionDecl );
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |                 virtual void visit( ReturnStmt * returnStmt );
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |                 std::list< DeclarationWithType * > returnVals;
 | 
|---|
 | 136 |         };
 | 
|---|
 | 137 | 
 | 
|---|
| [a08ba92] | 138 |         class EliminateTypedef : public Mutator {
 | 
|---|
 | 139 |           public:
 | 
|---|
| [de91427b] | 140 |                 EliminateTypedef() : scopeLevel( 0 ) {}
 | 
|---|
 | 141 |                 /// Replaces typedefs by forward declarations
 | 
|---|
| [0dd3a2f] | 142 |                 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
 | 
|---|
| [a08ba92] | 143 |           private:
 | 
|---|
| [0dd3a2f] | 144 |                 virtual Declaration *mutate( TypedefDecl *typeDecl );
 | 
|---|
 | 145 |                 virtual TypeDecl *mutate( TypeDecl *typeDecl );
 | 
|---|
 | 146 |                 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
 | 
|---|
| [1db21619] | 147 |                 virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
 | 
|---|
| [0dd3a2f] | 148 |                 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
 | 
|---|
 | 149 |                 virtual Type *mutate( TypeInstType *aggregateUseType );
 | 
|---|
 | 150 |                 virtual Expression *mutate( CastExpr *castExpr );
 | 
|---|
| [cc79d97] | 151 | 
 | 
|---|
| [85c4ef0] | 152 |                 virtual Declaration *mutate( StructDecl * structDecl );
 | 
|---|
 | 153 |                 virtual Declaration *mutate( UnionDecl * unionDecl );
 | 
|---|
 | 154 |                 virtual Declaration *mutate( EnumDecl * enumDecl );
 | 
|---|
| [4040425] | 155 |                 virtual Declaration *mutate( TraitDecl * contextDecl );
 | 
|---|
| [85c4ef0] | 156 | 
 | 
|---|
 | 157 |                 template<typename AggDecl>
 | 
|---|
 | 158 |                 AggDecl *handleAggregate( AggDecl * aggDecl );
 | 
|---|
 | 159 | 
 | 
|---|
| [45161b4d] | 160 |                 template<typename AggDecl>
 | 
|---|
 | 161 |                 void addImplicitTypedef( AggDecl * aggDecl );
 | 
|---|
| [70a06f6] | 162 | 
 | 
|---|
| [cc79d97] | 163 |                 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
 | 
|---|
 | 164 |                 TypedefMap typedefNames;
 | 
|---|
 | 165 |                 int scopeLevel;
 | 
|---|
| [a08ba92] | 166 |         };
 | 
|---|
| [c8ffe20b] | 167 | 
 | 
|---|
| [9cb8e88d] | 168 |         class VerifyCtorDtor : public Visitor {
 | 
|---|
 | 169 |         public:
 | 
|---|
 | 170 |                 /// ensure that constructors and destructors have at least one
 | 
|---|
 | 171 |                 /// parameter, the first of which must be a pointer, and no
 | 
|---|
 | 172 |                 /// return values.
 | 
|---|
 | 173 |                 static void verify( std::list< Declaration * > &translationUnit );
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 |                 virtual void visit( FunctionDecl *funcDecl );
 | 
|---|
| [5f98ce5] | 176 |         };
 | 
|---|
| [70a06f6] | 177 | 
 | 
|---|
| [630a82a] | 178 |         class CompoundLiteral : public GenPoly::DeclMutator {
 | 
|---|
 | 179 |                 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |                 virtual DeclarationWithType * mutate( ObjectDecl *objectDecl );
 | 
|---|
 | 182 |                 virtual Expression *mutate( CompoundLiteralExpr *compLitExpr );
 | 
|---|
| [9cb8e88d] | 183 |         };
 | 
|---|
 | 184 | 
 | 
|---|
| [a08ba92] | 185 |         void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
 | 
|---|
| [0dd3a2f] | 186 |                 Pass1 pass1;
 | 
|---|
 | 187 |                 Pass2 pass2( doDebug, 0 );
 | 
|---|
 | 188 |                 Pass3 pass3( 0 );
 | 
|---|
| [630a82a] | 189 |                 CompoundLiteral compoundliteral;
 | 
|---|
 | 190 | 
 | 
|---|
| [0dd3a2f] | 191 |                 EliminateTypedef::eliminateTypedef( translationUnit );
 | 
|---|
 | 192 |                 HoistStruct::hoistStruct( translationUnit );
 | 
|---|
| [40e636a] | 193 |                 autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs Pass1
 | 
|---|
| [0dd3a2f] | 194 |                 acceptAll( translationUnit, pass1 );
 | 
|---|
 | 195 |                 acceptAll( translationUnit, pass2 );
 | 
|---|
| [de91427b] | 196 |                 ReturnChecker::checkFunctionReturns( translationUnit );
 | 
|---|
| [40e636a] | 197 |                 compoundliteral.mutateDeclarationList( translationUnit );
 | 
|---|
| [0dd3a2f] | 198 |                 acceptAll( translationUnit, pass3 );
 | 
|---|
| [9cb8e88d] | 199 |                 VerifyCtorDtor::verify( translationUnit );
 | 
|---|
| [a08ba92] | 200 |         }
 | 
|---|
| [9cb8e88d] | 201 | 
 | 
|---|
| [a08ba92] | 202 |         void validateType( Type *type, const Indexer *indexer ) {
 | 
|---|
| [0dd3a2f] | 203 |                 Pass1 pass1;
 | 
|---|
 | 204 |                 Pass2 pass2( false, indexer );
 | 
|---|
 | 205 |                 Pass3 pass3( indexer );
 | 
|---|
 | 206 |                 type->accept( pass1 );
 | 
|---|
 | 207 |                 type->accept( pass2 );
 | 
|---|
 | 208 |                 type->accept( pass3 );
 | 
|---|
| [a08ba92] | 209 |         }
 | 
|---|
| [c8ffe20b] | 210 | 
 | 
|---|
| [a08ba92] | 211 |         void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
 | 
|---|
| [0dd3a2f] | 212 |                 HoistStruct hoister;
 | 
|---|
 | 213 |                 acceptAndAdd( translationUnit, hoister, true );
 | 
|---|
| [a08ba92] | 214 |         }
 | 
|---|
| [c8ffe20b] | 215 | 
 | 
|---|
| [a08ba92] | 216 |         HoistStruct::HoistStruct() : inStruct( false ) {
 | 
|---|
 | 217 |         }
 | 
|---|
| [c8ffe20b] | 218 | 
 | 
|---|
| [a08ba92] | 219 |         void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
 | 
|---|
| [0dd3a2f] | 220 |                 std::list< Declaration * >::iterator i = declList.begin();
 | 
|---|
 | 221 |                 while ( i != declList.end() ) {
 | 
|---|
 | 222 |                         std::list< Declaration * >::iterator next = i;
 | 
|---|
 | 223 |                         ++next;
 | 
|---|
 | 224 |                         if ( pred( *i ) ) {
 | 
|---|
 | 225 |                                 if ( doDelete ) {
 | 
|---|
 | 226 |                                         delete *i;
 | 
|---|
 | 227 |                                 } // if
 | 
|---|
 | 228 |                                 declList.erase( i );
 | 
|---|
 | 229 |                         } // if
 | 
|---|
 | 230 |                         i = next;
 | 
|---|
 | 231 |                 } // while
 | 
|---|
| [a08ba92] | 232 |         }
 | 
|---|
| [c8ffe20b] | 233 | 
 | 
|---|
| [a08ba92] | 234 |         bool isStructOrUnion( Declaration *decl ) {
 | 
|---|
| [0dd3a2f] | 235 |                 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
 | 
|---|
| [a08ba92] | 236 |         }
 | 
|---|
| [51b73452] | 237 | 
 | 
|---|
| [a08ba92] | 238 |         template< typename AggDecl >
 | 
|---|
 | 239 |         void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 240 |                 if ( inStruct ) {
 | 
|---|
 | 241 |                         // Add elements in stack order corresponding to nesting structure.
 | 
|---|
 | 242 |                         declsToAdd.push_front( aggregateDecl );
 | 
|---|
 | 243 |                         Visitor::visit( aggregateDecl );
 | 
|---|
 | 244 |                 } else {
 | 
|---|
 | 245 |                         inStruct = true;
 | 
|---|
 | 246 |                         Visitor::visit( aggregateDecl );
 | 
|---|
 | 247 |                         inStruct = false;
 | 
|---|
 | 248 |                 } // if
 | 
|---|
 | 249 |                 // Always remove the hoisted aggregate from the inner structure.
 | 
|---|
 | 250 |                 filter( aggregateDecl->get_members(), isStructOrUnion, false );
 | 
|---|
| [a08ba92] | 251 |         }
 | 
|---|
| [c8ffe20b] | 252 | 
 | 
|---|
| [a08ba92] | 253 |         void HoistStruct::visit( StructDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 254 |                 handleAggregate( aggregateDecl );
 | 
|---|
| [a08ba92] | 255 |         }
 | 
|---|
| [c8ffe20b] | 256 | 
 | 
|---|
| [a08ba92] | 257 |         void HoistStruct::visit( UnionDecl *aggregateDecl ) {
 | 
|---|
| [0dd3a2f] | 258 |                 handleAggregate( aggregateDecl );
 | 
|---|
| [a08ba92] | 259 |         }
 | 
|---|
| [c8ffe20b] | 260 | 
 | 
|---|
| [a08ba92] | 261 |         void HoistStruct::visit( CompoundStmt *compoundStmt ) {
 | 
|---|
| [0dd3a2f] | 262 |                 addVisit( compoundStmt, *this );
 | 
|---|
| [a08ba92] | 263 |         }
 | 
|---|
| [c8ffe20b] | 264 | 
 | 
|---|
| [a08ba92] | 265 |         void HoistStruct::visit( SwitchStmt *switchStmt ) {
 | 
|---|
| [0dd3a2f] | 266 |                 addVisit( switchStmt, *this );
 | 
|---|
| [a08ba92] | 267 |         }
 | 
|---|
| [c8ffe20b] | 268 | 
 | 
|---|
| [a08ba92] | 269 |         void Pass1::visit( EnumDecl *enumDecl ) {
 | 
|---|
| [0dd3a2f] | 270 |                 // Set the type of each member of the enumeration to be EnumConstant
 | 
|---|
 | 271 |                 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
 | 
|---|
| [f6d7e0f] | 272 |                         ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
 | 
|---|
| [0dd3a2f] | 273 |                         assert( obj );
 | 
|---|
| [a436947] | 274 |                         obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
 | 
|---|
| [0dd3a2f] | 275 |                 } // for
 | 
|---|
 | 276 |                 Parent::visit( enumDecl );
 | 
|---|
| [a08ba92] | 277 |         }
 | 
|---|
| [51b73452] | 278 | 
 | 
|---|
| [a08ba92] | 279 |         namespace {
 | 
|---|
| [83de11e] | 280 |                 template< typename DWTList >
 | 
|---|
 | 281 |                 void fixFunctionList( DWTList & dwts, FunctionType * func ) {
 | 
|---|
| [0dd3a2f] | 282 |                         // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
 | 
|---|
 | 283 |                         // entirely other fix ups are handled by the FixFunction class
 | 
|---|
| [83de11e] | 284 |                         typedef typename DWTList::iterator DWTIterator;
 | 
|---|
 | 285 |                         DWTIterator begin( dwts.begin() ), end( dwts.end() );
 | 
|---|
| [0dd3a2f] | 286 |                         if ( begin == end ) return;
 | 
|---|
 | 287 |                         FixFunction fixer;
 | 
|---|
 | 288 |                         DWTIterator i = begin;
 | 
|---|
| [83de11e] | 289 |                         *i = (*i)->acceptMutator( fixer );
 | 
|---|
| [0dd3a2f] | 290 |                         if ( fixer.get_isVoid() ) {
 | 
|---|
 | 291 |                                 DWTIterator j = i;
 | 
|---|
 | 292 |                                 ++i;
 | 
|---|
| [83de11e] | 293 |                                 dwts.erase( j );
 | 
|---|
| [9cb8e88d] | 294 |                                 if ( i != end ) {
 | 
|---|
| [0dd3a2f] | 295 |                                         throw SemanticError( "invalid type void in function type ", func );
 | 
|---|
 | 296 |                                 } // if
 | 
|---|
 | 297 |                         } else {
 | 
|---|
 | 298 |                                 ++i;
 | 
|---|
 | 299 |                                 for ( ; i != end; ++i ) {
 | 
|---|
 | 300 |                                         FixFunction fixer;
 | 
|---|
 | 301 |                                         *i = (*i )->acceptMutator( fixer );
 | 
|---|
 | 302 |                                         if ( fixer.get_isVoid() ) {
 | 
|---|
 | 303 |                                                 throw SemanticError( "invalid type void in function type ", func );
 | 
|---|
 | 304 |                                         } // if
 | 
|---|
 | 305 |                                 } // for
 | 
|---|
 | 306 |                         } // if
 | 
|---|
 | 307 |                 }
 | 
|---|
| [a08ba92] | 308 |         }
 | 
|---|
| [c8ffe20b] | 309 | 
 | 
|---|
| [a08ba92] | 310 |         void Pass1::visit( FunctionType *func ) {
 | 
|---|
| [0dd3a2f] | 311 |                 // Fix up parameters and return types
 | 
|---|
| [83de11e] | 312 |                 fixFunctionList( func->get_parameters(), func );
 | 
|---|
 | 313 |                 fixFunctionList( func->get_returnVals(), func );
 | 
|---|
| [0dd3a2f] | 314 |                 Visitor::visit( func );
 | 
|---|
| [a08ba92] | 315 |         }
 | 
|---|
| [c8ffe20b] | 316 | 
 | 
|---|
| [a08ba92] | 317 |         Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
 | 
|---|
| [0dd3a2f] | 318 |                 if ( other_indexer ) {
 | 
|---|
 | 319 |                         indexer = other_indexer;
 | 
|---|
 | 320 |                 } else {
 | 
|---|
 | 321 |                         indexer = this;
 | 
|---|
 | 322 |                 } // if
 | 
|---|
| [a08ba92] | 323 |         }
 | 
|---|
| [c8ffe20b] | 324 | 
 | 
|---|
| [a08ba92] | 325 |         void Pass2::visit( StructInstType *structInst ) {
 | 
|---|
| [0dd3a2f] | 326 |                 Parent::visit( structInst );
 | 
|---|
 | 327 |                 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
 | 
|---|
 | 328 |                 // it's not a semantic error if the struct is not found, just an implicit forward declaration
 | 
|---|
 | 329 |                 if ( st ) {
 | 
|---|
| [98735ef] | 330 |                         //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
 | 
|---|
| [0dd3a2f] | 331 |                         structInst->set_baseStruct( st );
 | 
|---|
 | 332 |                 } // if
 | 
|---|
 | 333 |                 if ( ! st || st->get_members().empty() ) {
 | 
|---|
 | 334 |                         // use of forward declaration
 | 
|---|
 | 335 |                         forwardStructs[ structInst->get_name() ].push_back( structInst );
 | 
|---|
 | 336 |                 } // if
 | 
|---|
| [a08ba92] | 337 |         }
 | 
|---|
| [c8ffe20b] | 338 | 
 | 
|---|
| [a08ba92] | 339 |         void Pass2::visit( UnionInstType *unionInst ) {
 | 
|---|
| [0dd3a2f] | 340 |                 Parent::visit( unionInst );
 | 
|---|
 | 341 |                 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
 | 
|---|
 | 342 |                 // it's not a semantic error if the union is not found, just an implicit forward declaration
 | 
|---|
 | 343 |                 if ( un ) {
 | 
|---|
 | 344 |                         unionInst->set_baseUnion( un );
 | 
|---|
 | 345 |                 } // if
 | 
|---|
 | 346 |                 if ( ! un || un->get_members().empty() ) {
 | 
|---|
 | 347 |                         // use of forward declaration
 | 
|---|
 | 348 |                         forwardUnions[ unionInst->get_name() ].push_back( unionInst );
 | 
|---|
 | 349 |                 } // if
 | 
|---|
| [a08ba92] | 350 |         }
 | 
|---|
| [c8ffe20b] | 351 | 
 | 
|---|
| [4040425] | 352 |         void Pass2::visit( TraitInstType *contextInst ) {
 | 
|---|
| [0dd3a2f] | 353 |                 Parent::visit( contextInst );
 | 
|---|
| [4040425] | 354 |                 TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() );
 | 
|---|
| [0dd3a2f] | 355 |                 if ( ! ctx ) {
 | 
|---|
 | 356 |                         throw SemanticError( "use of undeclared context " + contextInst->get_name() );
 | 
|---|
| [17cd4eb] | 357 |                 } // if
 | 
|---|
| [0dd3a2f] | 358 |                 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
 | 
|---|
 | 359 |                         for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
 | 
|---|
| [4040425] | 360 |                                 if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) {
 | 
|---|
| [0dd3a2f] | 361 |                                         cloneAll( otherCtx->get_members(), contextInst->get_members() );
 | 
|---|
 | 362 |                                 } else {
 | 
|---|
 | 363 |                                         contextInst->get_members().push_back( (*assert )->clone() );
 | 
|---|
 | 364 |                                 } // if
 | 
|---|
 | 365 |                         } // for
 | 
|---|
 | 366 |                 } // for
 | 
|---|
| [51b986f] | 367 | 
 | 
|---|
 | 368 |                 if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
 | 
|---|
 | 369 |                         throw SemanticError( "incorrect number of context parameters: ", contextInst );
 | 
|---|
 | 370 |                 } // if
 | 
|---|
 | 371 | 
 | 
|---|
| [0dd3a2f] | 372 |                 applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) );
 | 
|---|
| [a08ba92] | 373 |         }
 | 
|---|
| [c8ffe20b] | 374 | 
 | 
|---|
| [a08ba92] | 375 |         void Pass2::visit( StructDecl *structDecl ) {
 | 
|---|
| [677c1be] | 376 |                 // visit struct members first so that the types of self-referencing members are updated properly
 | 
|---|
 | 377 |                 Parent::visit( structDecl );
 | 
|---|
| [0dd3a2f] | 378 |                 if ( ! structDecl->get_members().empty() ) {
 | 
|---|
 | 379 |                         ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
 | 
|---|
 | 380 |                         if ( fwds != forwardStructs.end() ) {
 | 
|---|
 | 381 |                                 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
 | 
|---|
 | 382 |                                         (*inst )->set_baseStruct( structDecl );
 | 
|---|
 | 383 |                                 } // for
 | 
|---|
 | 384 |                                 forwardStructs.erase( fwds );
 | 
|---|
 | 385 |                         } // if
 | 
|---|
 | 386 |                 } // if
 | 
|---|
| [a08ba92] | 387 |         }
 | 
|---|
| [c8ffe20b] | 388 | 
 | 
|---|
| [a08ba92] | 389 |         void Pass2::visit( UnionDecl *unionDecl ) {
 | 
|---|
| [677c1be] | 390 |                 Parent::visit( unionDecl );
 | 
|---|
| [0dd3a2f] | 391 |                 if ( ! unionDecl->get_members().empty() ) {
 | 
|---|
 | 392 |                         ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
 | 
|---|
 | 393 |                         if ( fwds != forwardUnions.end() ) {
 | 
|---|
 | 394 |                                 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
 | 
|---|
 | 395 |                                         (*inst )->set_baseUnion( unionDecl );
 | 
|---|
 | 396 |                                 } // for
 | 
|---|
 | 397 |                                 forwardUnions.erase( fwds );
 | 
|---|
 | 398 |                         } // if
 | 
|---|
 | 399 |                 } // if
 | 
|---|
| [a08ba92] | 400 |         }
 | 
|---|
| [c8ffe20b] | 401 | 
 | 
|---|
| [a08ba92] | 402 |         void Pass2::visit( TypeInstType *typeInst ) {
 | 
|---|
| [0dd3a2f] | 403 |                 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
 | 
|---|
 | 404 |                         if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
 | 
|---|
 | 405 |                                 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
 | 
|---|
 | 406 |                         } // if
 | 
|---|
 | 407 |                 } // if
 | 
|---|
| [a08ba92] | 408 |         }
 | 
|---|
| [c8ffe20b] | 409 | 
 | 
|---|
| [a08ba92] | 410 |         Pass3::Pass3( const Indexer *other_indexer ) :  Indexer( false ) {
 | 
|---|
| [0dd3a2f] | 411 |                 if ( other_indexer ) {
 | 
|---|
 | 412 |                         indexer = other_indexer;
 | 
|---|
 | 413 |                 } else {
 | 
|---|
 | 414 |                         indexer = this;
 | 
|---|
 | 415 |                 } // if
 | 
|---|
| [a08ba92] | 416 |         }
 | 
|---|
| [c8ffe20b] | 417 | 
 | 
|---|
| [82dd287] | 418 |         /// Fix up assertions
 | 
|---|
| [a08ba92] | 419 |         void forallFixer( Type *func ) {
 | 
|---|
| [0dd3a2f] | 420 |                 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
 | 
|---|
 | 421 |                         std::list< DeclarationWithType * > toBeDone, nextRound;
 | 
|---|
 | 422 |                         toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
 | 
|---|
 | 423 |                         while ( ! toBeDone.empty() ) {
 | 
|---|
 | 424 |                                 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
 | 
|---|
| [4040425] | 425 |                                         if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) {
 | 
|---|
| [0dd3a2f] | 426 |                                                 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
 | 
|---|
 | 427 |                                                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
 | 
|---|
 | 428 |                                                         assert( dwt );
 | 
|---|
 | 429 |                                                         nextRound.push_back( dwt->clone() );
 | 
|---|
 | 430 |                                                 }
 | 
|---|
 | 431 |                                                 delete ctx;
 | 
|---|
 | 432 |                                         } else {
 | 
|---|
 | 433 |                                                 FixFunction fixer;
 | 
|---|
 | 434 |                                                 *assertion = (*assertion )->acceptMutator( fixer );
 | 
|---|
 | 435 |                                                 if ( fixer.get_isVoid() ) {
 | 
|---|
 | 436 |                                                         throw SemanticError( "invalid type void in assertion of function ", func );
 | 
|---|
 | 437 |                                                 }
 | 
|---|
 | 438 |                                                 (*type )->get_assertions().push_back( *assertion );
 | 
|---|
 | 439 |                                         } // if
 | 
|---|
 | 440 |                                 } // for
 | 
|---|
 | 441 |                                 toBeDone.clear();
 | 
|---|
 | 442 |                                 toBeDone.splice( toBeDone.end(), nextRound );
 | 
|---|
 | 443 |                         } // while
 | 
|---|
 | 444 |                 } // for
 | 
|---|
| [a08ba92] | 445 |         }
 | 
|---|
| [c8ffe20b] | 446 | 
 | 
|---|
| [a08ba92] | 447 |         void Pass3::visit( ObjectDecl *object ) {
 | 
|---|
| [0dd3a2f] | 448 |                 forallFixer( object->get_type() );
 | 
|---|
 | 449 |                 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
 | 
|---|
 | 450 |                         forallFixer( pointer->get_base() );
 | 
|---|
 | 451 |                 } // if
 | 
|---|
 | 452 |                 Parent::visit( object );
 | 
|---|
 | 453 |                 object->fixUniqueId();
 | 
|---|
| [a08ba92] | 454 |         }
 | 
|---|
| [c8ffe20b] | 455 | 
 | 
|---|
| [a08ba92] | 456 |         void Pass3::visit( FunctionDecl *func ) {
 | 
|---|
| [0dd3a2f] | 457 |                 forallFixer( func->get_type() );
 | 
|---|
 | 458 |                 Parent::visit( func );
 | 
|---|
 | 459 |                 func->fixUniqueId();
 | 
|---|
| [a08ba92] | 460 |         }
 | 
|---|
| [c8ffe20b] | 461 | 
 | 
|---|
| [de91427b] | 462 |         void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 463 |                 ReturnChecker checker;
 | 
|---|
 | 464 |                 acceptAll( translationUnit, checker );
 | 
|---|
 | 465 |         }
 | 
|---|
 | 466 | 
 | 
|---|
 | 467 |         void ReturnChecker::visit( FunctionDecl * functionDecl ) {
 | 
|---|
 | 468 |                 std::list< DeclarationWithType * > oldReturnVals = returnVals;
 | 
|---|
 | 469 |                 returnVals = functionDecl->get_functionType()->get_returnVals();
 | 
|---|
 | 470 |                 Visitor::visit( functionDecl );
 | 
|---|
 | 471 |                 returnVals = oldReturnVals;
 | 
|---|
 | 472 |         }
 | 
|---|
 | 473 | 
 | 
|---|
 | 474 |         void ReturnChecker::visit( ReturnStmt * returnStmt ) {
 | 
|---|
| [74d1804] | 475 |                 // Previously this also checked for the existence of an expr paired with no return values on
 | 
|---|
 | 476 |                 // the  function return type. This is incorrect, since you can have an expression attached to
 | 
|---|
 | 477 |                 // a return statement in a void-returning function in C. The expression is treated as if it
 | 
|---|
 | 478 |                 // were cast to void.
 | 
|---|
| [de91427b] | 479 |                 if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
 | 
|---|
 | 480 |                         throw SemanticError( "Non-void function returns no values: " , returnStmt );
 | 
|---|
 | 481 |                 }
 | 
|---|
 | 482 |         }
 | 
|---|
 | 483 | 
 | 
|---|
 | 484 | 
 | 
|---|
| [a08ba92] | 485 |         bool isTypedef( Declaration *decl ) {
 | 
|---|
| [0dd3a2f] | 486 |                 return dynamic_cast< TypedefDecl * >( decl );
 | 
|---|
| [a08ba92] | 487 |         }
 | 
|---|
| [c8ffe20b] | 488 | 
 | 
|---|
| [a08ba92] | 489 |         void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
 | 
|---|
| [0dd3a2f] | 490 |                 EliminateTypedef eliminator;
 | 
|---|
 | 491 |                 mutateAll( translationUnit, eliminator );
 | 
|---|
| [5f98ce5] | 492 |                 if ( eliminator.typedefNames.count( "size_t" ) ) {
 | 
|---|
 | 493 |                         // grab and remember declaration of size_t
 | 
|---|
 | 494 |                         SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
 | 
|---|
 | 495 |                 } else {
 | 
|---|
| [40e636a] | 496 |                         // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
 | 
|---|
 | 497 |                         // eventually should have a warning for this case.
 | 
|---|
 | 498 |                         SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| [5f98ce5] | 499 |                 }
 | 
|---|
| [0dd3a2f] | 500 |                 filter( translationUnit, isTypedef, true );
 | 
|---|
| [5f98ce5] | 501 | 
 | 
|---|
| [a08ba92] | 502 |         }
 | 
|---|
| [c8ffe20b] | 503 | 
 | 
|---|
| [85c4ef0] | 504 |         Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
 | 
|---|
| [9cb8e88d] | 505 |                 // instances of typedef types will come here. If it is an instance
 | 
|---|
| [cc79d97] | 506 |                 // of a typdef type, link the instance to its actual type.
 | 
|---|
 | 507 |                 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
 | 
|---|
| [0dd3a2f] | 508 |                 if ( def != typedefNames.end() ) {
 | 
|---|
| [cc79d97] | 509 |                         Type *ret = def->second.first->get_base()->clone();
 | 
|---|
| [0dd3a2f] | 510 |                         ret->get_qualifiers() += typeInst->get_qualifiers();
 | 
|---|
| [0215a76f] | 511 |                         // place instance parameters on the typedef'd type
 | 
|---|
 | 512 |                         if ( ! typeInst->get_parameters().empty() ) {
 | 
|---|
 | 513 |                                 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
 | 
|---|
 | 514 |                                 if ( ! rtt ) {
 | 
|---|
 | 515 |                                         throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
 | 
|---|
 | 516 |                                 }
 | 
|---|
 | 517 |                                 rtt->get_parameters().clear();
 | 
|---|
| [b644d6f] | 518 |                                 cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
 | 
|---|
 | 519 |                                 mutateAll( rtt->get_parameters(), *this );  // recursively fix typedefs on parameters
 | 
|---|
| [1db21619] | 520 |                         } // if
 | 
|---|
| [0dd3a2f] | 521 |                         delete typeInst;
 | 
|---|
 | 522 |                         return ret;
 | 
|---|
 | 523 |                 } // if
 | 
|---|
 | 524 |                 return typeInst;
 | 
|---|
| [a08ba92] | 525 |         }
 | 
|---|
| [c8ffe20b] | 526 | 
 | 
|---|
| [85c4ef0] | 527 |         Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
 | 
|---|
| [0dd3a2f] | 528 |                 Declaration *ret = Mutator::mutate( tyDecl );
 | 
|---|
| [5f98ce5] | 529 | 
 | 
|---|
| [cc79d97] | 530 |                 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
 | 
|---|
| [9cb8e88d] | 531 |                         // typedef to the same name from the same scope
 | 
|---|
| [cc79d97] | 532 |                         // must be from the same type
 | 
|---|
 | 533 | 
 | 
|---|
 | 534 |                         Type * t1 = tyDecl->get_base();
 | 
|---|
 | 535 |                         Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
 | 
|---|
| [1cbca6e] | 536 |                         if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
 | 
|---|
| [cc79d97] | 537 |                                 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
 | 
|---|
| [85c4ef0] | 538 |                         }
 | 
|---|
| [cc79d97] | 539 |                 } else {
 | 
|---|
 | 540 |                         typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
 | 
|---|
 | 541 |                 } // if
 | 
|---|
 | 542 | 
 | 
|---|
| [0dd3a2f] | 543 |                 // When a typedef is a forward declaration:
 | 
|---|
 | 544 |                 //    typedef struct screen SCREEN;
 | 
|---|
 | 545 |                 // the declaration portion must be retained:
 | 
|---|
 | 546 |                 //    struct screen;
 | 
|---|
 | 547 |                 // because the expansion of the typedef is:
 | 
|---|
 | 548 |                 //    void rtn( SCREEN *p ) => void rtn( struct screen *p )
 | 
|---|
 | 549 |                 // hence the type-name "screen" must be defined.
 | 
|---|
 | 550 |                 // Note, qualifiers on the typedef are superfluous for the forward declaration.
 | 
|---|
 | 551 |                 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
 | 
|---|
 | 552 |                         return new StructDecl( aggDecl->get_name() );
 | 
|---|
 | 553 |                 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
 | 
|---|
 | 554 |                         return new UnionDecl( aggDecl->get_name() );
 | 
|---|
| [956a9c77] | 555 |                 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) {
 | 
|---|
 | 556 |                         return new EnumDecl( enumDecl->get_name() );
 | 
|---|
| [0dd3a2f] | 557 |                 } else {
 | 
|---|
 | 558 |                         return ret;
 | 
|---|
 | 559 |                 } // if
 | 
|---|
| [a08ba92] | 560 |         }
 | 
|---|
| [c8ffe20b] | 561 | 
 | 
|---|
| [85c4ef0] | 562 |         TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
 | 
|---|
| [cc79d97] | 563 |                 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
 | 
|---|
| [0dd3a2f] | 564 |                 if ( i != typedefNames.end() ) {
 | 
|---|
 | 565 |                         typedefNames.erase( i ) ;
 | 
|---|
 | 566 |                 } // if
 | 
|---|
 | 567 |                 return typeDecl;
 | 
|---|
| [a08ba92] | 568 |         }
 | 
|---|
| [c8ffe20b] | 569 | 
 | 
|---|
| [85c4ef0] | 570 |         DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
 | 
|---|
| [cc79d97] | 571 |                 TypedefMap oldNames = typedefNames;
 | 
|---|
| [0dd3a2f] | 572 |                 DeclarationWithType *ret = Mutator::mutate( funcDecl );
 | 
|---|
 | 573 |                 typedefNames = oldNames;
 | 
|---|
 | 574 |                 return ret;
 | 
|---|
| [a08ba92] | 575 |         }
 | 
|---|
| [c8ffe20b] | 576 | 
 | 
|---|
| [1db21619] | 577 |         DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
 | 
|---|
| [cc79d97] | 578 |                 TypedefMap oldNames = typedefNames;
 | 
|---|
| [1db21619] | 579 |                 DeclarationWithType *ret = Mutator::mutate( objDecl );
 | 
|---|
| [ae4c85a] | 580 |                 typedefNames = oldNames;
 | 
|---|
| [02e5ab6] | 581 |                 // is the type a function?
 | 
|---|
| [1db21619] | 582 |                 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
 | 
|---|
| [02e5ab6] | 583 |                         // replace the current object declaration with a function declaration
 | 
|---|
| [1db21619] | 584 |                         return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
 | 
|---|
 | 585 |                 } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
 | 
|---|
 | 586 |                         throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
 | 
|---|
 | 587 |                 } // if
 | 
|---|
| [0dd3a2f] | 588 |                 return ret;
 | 
|---|
| [a08ba92] | 589 |         }
 | 
|---|
| [c8ffe20b] | 590 | 
 | 
|---|
| [85c4ef0] | 591 |         Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
 | 
|---|
| [cc79d97] | 592 |                 TypedefMap oldNames = typedefNames;
 | 
|---|
| [0dd3a2f] | 593 |                 Expression *ret = Mutator::mutate( castExpr );
 | 
|---|
 | 594 |                 typedefNames = oldNames;
 | 
|---|
 | 595 |                 return ret;
 | 
|---|
| [a08ba92] | 596 |         }
 | 
|---|
| [c8ffe20b] | 597 | 
 | 
|---|
| [85c4ef0] | 598 |         CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
 | 
|---|
| [cc79d97] | 599 |                 TypedefMap oldNames = typedefNames;
 | 
|---|
 | 600 |                 scopeLevel += 1;
 | 
|---|
| [0dd3a2f] | 601 |                 CompoundStmt *ret = Mutator::mutate( compoundStmt );
 | 
|---|
| [cc79d97] | 602 |                 scopeLevel -= 1;
 | 
|---|
| [0dd3a2f] | 603 |                 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
 | 
|---|
 | 604 |                 while ( i != compoundStmt->get_kids().end() ) {
 | 
|---|
| [85c4ef0] | 605 |                         std::list< Statement * >::iterator next = i+1;
 | 
|---|
| [0dd3a2f] | 606 |                         if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
 | 
|---|
 | 607 |                                 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
 | 
|---|
 | 608 |                                         delete *i;
 | 
|---|
 | 609 |                                         compoundStmt->get_kids().erase( i );
 | 
|---|
 | 610 |                                 } // if
 | 
|---|
 | 611 |                         } // if
 | 
|---|
 | 612 |                         i = next;
 | 
|---|
 | 613 |                 } // while
 | 
|---|
 | 614 |                 typedefNames = oldNames;
 | 
|---|
 | 615 |                 return ret;
 | 
|---|
| [a08ba92] | 616 |         }
 | 
|---|
| [85c4ef0] | 617 | 
 | 
|---|
| [45161b4d] | 618 |         // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed
 | 
|---|
 | 619 |         // as well
 | 
|---|
| [85c4ef0] | 620 |         template<typename AggDecl>
 | 
|---|
 | 621 |         AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
 | 
|---|
 | 622 |                 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
 | 
|---|
 | 623 |                 for ( ; it != aggDecl->get_members().end(); ) {
 | 
|---|
 | 624 |                         std::list< Declaration * >::iterator next = it+1;
 | 
|---|
 | 625 |                         if ( dynamic_cast< TypedefDecl * >( *it ) ) {
 | 
|---|
 | 626 |                                 delete *it;
 | 
|---|
 | 627 |                                 aggDecl->get_members().erase( it );
 | 
|---|
 | 628 |                         } // if
 | 
|---|
 | 629 |                         it = next;
 | 
|---|
 | 630 |                 }
 | 
|---|
 | 631 |                 return aggDecl;
 | 
|---|
 | 632 |         }
 | 
|---|
 | 633 | 
 | 
|---|
| [45161b4d] | 634 |         template<typename AggDecl>
 | 
|---|
 | 635 |         void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
 | 
|---|
 | 636 |                 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
 | 
|---|
 | 637 |                         Type *type;
 | 
|---|
 | 638 |                         if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
 | 
|---|
 | 639 |                                 type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
 | 
|---|
 | 640 |                         } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
 | 
|---|
 | 641 |                                 type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
 | 
|---|
 | 642 |                         } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl )  ) {
 | 
|---|
 | 643 |                                 type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
 | 
|---|
 | 644 |                         } // if
 | 
|---|
 | 645 |                         TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type );
 | 
|---|
 | 646 |                         typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
 | 
|---|
 | 647 |                 } // if
 | 
|---|
 | 648 |         }
 | 
|---|
| [4e06c1e] | 649 | 
 | 
|---|
| [85c4ef0] | 650 |         Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
 | 
|---|
| [45161b4d] | 651 |                 addImplicitTypedef( structDecl );
 | 
|---|
| [85c4ef0] | 652 |                 Mutator::mutate( structDecl );
 | 
|---|
 | 653 |                 return handleAggregate( structDecl );
 | 
|---|
 | 654 |         }
 | 
|---|
 | 655 | 
 | 
|---|
 | 656 |         Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
 | 
|---|
| [45161b4d] | 657 |                 addImplicitTypedef( unionDecl );
 | 
|---|
| [85c4ef0] | 658 |                 Mutator::mutate( unionDecl );
 | 
|---|
 | 659 |                 return handleAggregate( unionDecl );
 | 
|---|
 | 660 |         }
 | 
|---|
 | 661 | 
 | 
|---|
 | 662 |         Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
 | 
|---|
| [45161b4d] | 663 |                 addImplicitTypedef( enumDecl );
 | 
|---|
| [85c4ef0] | 664 |                 Mutator::mutate( enumDecl );
 | 
|---|
 | 665 |                 return handleAggregate( enumDecl );
 | 
|---|
 | 666 |         }
 | 
|---|
 | 667 | 
 | 
|---|
| [45161b4d] | 668 |         Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
 | 
|---|
| [85c4ef0] | 669 |                 Mutator::mutate( contextDecl );
 | 
|---|
 | 670 |                 return handleAggregate( contextDecl );
 | 
|---|
 | 671 |         }
 | 
|---|
 | 672 | 
 | 
|---|
| [9cb8e88d] | 673 |         void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 674 |                 VerifyCtorDtor verifier;
 | 
|---|
 | 675 |                 acceptAll( translationUnit, verifier );
 | 
|---|
 | 676 |         }
 | 
|---|
 | 677 | 
 | 
|---|
 | 678 |         void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) {
 | 
|---|
 | 679 |                 FunctionType * funcType = funcDecl->get_functionType();
 | 
|---|
 | 680 |                 std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
 | 
|---|
 | 681 |                 std::list< DeclarationWithType * > ¶ms = funcType->get_parameters();
 | 
|---|
 | 682 | 
 | 
|---|
 | 683 |                 if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) {
 | 
|---|
 | 684 |                         if ( params.size() == 0 ) {
 | 
|---|
 | 685 |                                 throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl );
 | 
|---|
 | 686 |                         }
 | 
|---|
 | 687 |                         if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) {
 | 
|---|
 | 688 |                                 throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl );
 | 
|---|
 | 689 |                         }
 | 
|---|
 | 690 |                         if ( returnVals.size() != 0 ) {
 | 
|---|
 | 691 |                                 throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
 | 
|---|
 | 692 |                         }
 | 
|---|
 | 693 |                 }
 | 
|---|
 | 694 | 
 | 
|---|
 | 695 |                 Visitor::visit( funcDecl );
 | 
|---|
 | 696 |         }
 | 
|---|
| [70a06f6] | 697 | 
 | 
|---|
| [630a82a] | 698 |         DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
 | 
|---|
 | 699 |                 storageclass = objectDecl->get_storageClass();
 | 
|---|
 | 700 |                 DeclarationWithType * temp = Mutator::mutate( objectDecl );
 | 
|---|
 | 701 |                 storageclass = DeclarationNode::NoStorageClass;
 | 
|---|
 | 702 |                 return temp;
 | 
|---|
 | 703 |         }
 | 
|---|
 | 704 | 
 | 
|---|
 | 705 |         Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
 | 
|---|
 | 706 |                 // transform [storage_class] ... (struct S){ 3, ... };
 | 
|---|
 | 707 |                 // into [storage_class] struct S temp =  { 3, ... };
 | 
|---|
 | 708 |                 static UniqueName indexName( "_compLit" );
 | 
|---|
 | 709 | 
 | 
|---|
 | 710 |                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() );
 | 
|---|
 | 711 |                 compLitExpr->set_type( 0 );
 | 
|---|
 | 712 |                 compLitExpr->set_initializer( 0 );
 | 
|---|
 | 713 |                 delete compLitExpr;
 | 
|---|
 | 714 |                 DeclarationWithType * newtempvar = mutate( tempvar );
 | 
|---|
 | 715 |                 addDeclaration( newtempvar );                                   // add modified temporary to current block
 | 
|---|
 | 716 |                 return new VariableExpr( newtempvar );
 | 
|---|
 | 717 |         }
 | 
|---|
| [51b73452] | 718 | } // namespace SymTab
 | 
|---|
| [0dd3a2f] | 719 | 
 | 
|---|
 | 720 | // Local Variables: //
 | 
|---|
 | 721 | // tab-width: 4 //
 | 
|---|
 | 722 | // mode: c++ //
 | 
|---|
 | 723 | // compile-command: "make install" //
 | 
|---|
 | 724 | // End: //
 | 
|---|