[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 | // |
---|
| 7 | // Validate.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 21:50:04 2015 |
---|
[5f2f2d7] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Mon Jun 8 17:19:35 2015 |
---|
| 13 | // Update Count : 22 |
---|
[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. |
---|
[51b7345] | 39 | |
---|
| 40 | #include <list> |
---|
| 41 | #include <iterator> |
---|
| 42 | #include "Validate.h" |
---|
| 43 | #include "SynTree/Visitor.h" |
---|
| 44 | #include "SynTree/Mutator.h" |
---|
| 45 | #include "SynTree/Type.h" |
---|
| 46 | #include "SynTree/Statement.h" |
---|
| 47 | #include "Indexer.h" |
---|
| 48 | #include "SynTree/TypeSubstitution.h" |
---|
| 49 | #include "FixFunction.h" |
---|
| 50 | #include "ImplementationType.h" |
---|
| 51 | #include "utility.h" |
---|
| 52 | #include "UniqueName.h" |
---|
| 53 | #include "AddVisit.h" |
---|
| 54 | |
---|
| 55 | |
---|
[c8ffe20b] | 56 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } |
---|
[51b7345] | 57 | |
---|
| 58 | namespace SymTab { |
---|
[a08ba92] | 59 | class HoistStruct : public Visitor { |
---|
| 60 | public: |
---|
[0dd3a2f] | 61 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
[c8ffe20b] | 62 | |
---|
[0dd3a2f] | 63 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
[c8ffe20b] | 64 | |
---|
[0dd3a2f] | 65 | virtual void visit( StructDecl *aggregateDecl ); |
---|
| 66 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
[c8ffe20b] | 67 | |
---|
[0dd3a2f] | 68 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 69 | virtual void visit( IfStmt *ifStmt ); |
---|
| 70 | virtual void visit( WhileStmt *whileStmt ); |
---|
| 71 | virtual void visit( ForStmt *forStmt ); |
---|
| 72 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 73 | virtual void visit( ChooseStmt *chooseStmt ); |
---|
| 74 | virtual void visit( CaseStmt *caseStmt ); |
---|
| 75 | virtual void visit( CatchStmt *catchStmt ); |
---|
[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 | |
---|
[a08ba92] | 85 | class Pass1 : public Visitor { |
---|
[0dd3a2f] | 86 | typedef Visitor Parent; |
---|
| 87 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
| 88 | virtual void visit( FunctionType *func ); |
---|
[a08ba92] | 89 | }; |
---|
[c8ffe20b] | 90 | |
---|
[a08ba92] | 91 | class Pass2 : public Indexer { |
---|
[0dd3a2f] | 92 | typedef Indexer Parent; |
---|
[a08ba92] | 93 | public: |
---|
[0dd3a2f] | 94 | Pass2( bool doDebug, const Indexer *indexer ); |
---|
[a08ba92] | 95 | private: |
---|
[0dd3a2f] | 96 | virtual void visit( StructInstType *structInst ); |
---|
| 97 | virtual void visit( UnionInstType *unionInst ); |
---|
| 98 | virtual void visit( ContextInstType *contextInst ); |
---|
| 99 | virtual void visit( StructDecl *structDecl ); |
---|
| 100 | virtual void visit( UnionDecl *unionDecl ); |
---|
| 101 | virtual void visit( TypeInstType *typeInst ); |
---|
| 102 | |
---|
| 103 | const Indexer *indexer; |
---|
| 104 | |
---|
| 105 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
| 106 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
| 107 | ForwardStructsType forwardStructs; |
---|
| 108 | ForwardUnionsType forwardUnions; |
---|
[a08ba92] | 109 | }; |
---|
[c8ffe20b] | 110 | |
---|
[a08ba92] | 111 | class Pass3 : public Indexer { |
---|
[0dd3a2f] | 112 | typedef Indexer Parent; |
---|
[a08ba92] | 113 | public: |
---|
[0dd3a2f] | 114 | Pass3( const Indexer *indexer ); |
---|
[a08ba92] | 115 | private: |
---|
[0dd3a2f] | 116 | virtual void visit( ObjectDecl *object ); |
---|
| 117 | virtual void visit( FunctionDecl *func ); |
---|
[c8ffe20b] | 118 | |
---|
[0dd3a2f] | 119 | const Indexer *indexer; |
---|
[a08ba92] | 120 | }; |
---|
[c8ffe20b] | 121 | |
---|
[a08ba92] | 122 | class AddStructAssignment : public Visitor { |
---|
| 123 | public: |
---|
[0dd3a2f] | 124 | static void addStructAssignment( std::list< Declaration * > &translationUnit ); |
---|
[c8ffe20b] | 125 | |
---|
[0dd3a2f] | 126 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
[c8ffe20b] | 127 | |
---|
[0dd3a2f] | 128 | virtual void visit( StructDecl *structDecl ); |
---|
| 129 | virtual void visit( UnionDecl *structDecl ); |
---|
| 130 | virtual void visit( TypeDecl *typeDecl ); |
---|
| 131 | virtual void visit( ContextDecl *ctxDecl ); |
---|
| 132 | virtual void visit( FunctionDecl *functionDecl ); |
---|
[c8ffe20b] | 133 | |
---|
[0dd3a2f] | 134 | virtual void visit( FunctionType *ftype ); |
---|
| 135 | virtual void visit( PointerType *ftype ); |
---|
[c8ffe20b] | 136 | |
---|
[0dd3a2f] | 137 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 138 | virtual void visit( IfStmt *ifStmt ); |
---|
| 139 | virtual void visit( WhileStmt *whileStmt ); |
---|
| 140 | virtual void visit( ForStmt *forStmt ); |
---|
| 141 | virtual void visit( SwitchStmt *switchStmt ); |
---|
| 142 | virtual void visit( ChooseStmt *chooseStmt ); |
---|
| 143 | virtual void visit( CaseStmt *caseStmt ); |
---|
| 144 | virtual void visit( CatchStmt *catchStmt ); |
---|
[3c70d38] | 145 | |
---|
[0dd3a2f] | 146 | AddStructAssignment() : functionNesting( 0 ) {} |
---|
[a08ba92] | 147 | private: |
---|
[0dd3a2f] | 148 | template< typename StmtClass > void visitStatement( StmtClass *stmt ); |
---|
[c8ffe20b] | 149 | |
---|
[0dd3a2f] | 150 | std::list< Declaration * > declsToAdd; |
---|
| 151 | std::set< std::string > structsDone; |
---|
| 152 | unsigned int functionNesting; // current level of nested functions |
---|
[a08ba92] | 153 | }; |
---|
[c8ffe20b] | 154 | |
---|
[a08ba92] | 155 | class EliminateTypedef : public Mutator { |
---|
| 156 | public: |
---|
[0dd3a2f] | 157 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
[a08ba92] | 158 | private: |
---|
[0dd3a2f] | 159 | virtual Declaration *mutate( TypedefDecl *typeDecl ); |
---|
| 160 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
| 161 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); |
---|
| 162 | virtual ObjectDecl *mutate( ObjectDecl *objDecl ); |
---|
| 163 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); |
---|
| 164 | virtual Type *mutate( TypeInstType *aggregateUseType ); |
---|
| 165 | virtual Expression *mutate( CastExpr *castExpr ); |
---|
| 166 | |
---|
| 167 | std::map< std::string, TypedefDecl * > typedefNames; |
---|
[a08ba92] | 168 | }; |
---|
[c8ffe20b] | 169 | |
---|
[a08ba92] | 170 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { |
---|
[0dd3a2f] | 171 | Pass1 pass1; |
---|
| 172 | Pass2 pass2( doDebug, 0 ); |
---|
| 173 | Pass3 pass3( 0 ); |
---|
| 174 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
| 175 | HoistStruct::hoistStruct( translationUnit ); |
---|
| 176 | acceptAll( translationUnit, pass1 ); |
---|
| 177 | acceptAll( translationUnit, pass2 ); |
---|
| 178 | AddStructAssignment::addStructAssignment( translationUnit ); |
---|
| 179 | acceptAll( translationUnit, pass3 ); |
---|
[a08ba92] | 180 | } |
---|
| 181 | |
---|
| 182 | void validateType( Type *type, const Indexer *indexer ) { |
---|
[0dd3a2f] | 183 | Pass1 pass1; |
---|
| 184 | Pass2 pass2( false, indexer ); |
---|
| 185 | Pass3 pass3( indexer ); |
---|
| 186 | type->accept( pass1 ); |
---|
| 187 | type->accept( pass2 ); |
---|
| 188 | type->accept( pass3 ); |
---|
[a08ba92] | 189 | } |
---|
[c8ffe20b] | 190 | |
---|
[a08ba92] | 191 | template< typename Visitor > |
---|
| 192 | void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) { |
---|
[0dd3a2f] | 193 | std::list< Declaration * >::iterator i = translationUnit.begin(); |
---|
| 194 | while ( i != translationUnit.end() ) { |
---|
| 195 | (*i)->accept( visitor ); |
---|
| 196 | std::list< Declaration * >::iterator next = i; |
---|
| 197 | next++; |
---|
| 198 | if ( ! visitor.get_declsToAdd().empty() ) { |
---|
| 199 | translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() ); |
---|
| 200 | } // if |
---|
| 201 | i = next; |
---|
| 202 | } // while |
---|
[a08ba92] | 203 | } |
---|
[c8ffe20b] | 204 | |
---|
[a08ba92] | 205 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 206 | HoistStruct hoister; |
---|
| 207 | acceptAndAdd( translationUnit, hoister, true ); |
---|
[a08ba92] | 208 | } |
---|
[c8ffe20b] | 209 | |
---|
[a08ba92] | 210 | HoistStruct::HoistStruct() : inStruct( false ) { |
---|
| 211 | } |
---|
[c8ffe20b] | 212 | |
---|
[a08ba92] | 213 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { |
---|
[0dd3a2f] | 214 | std::list< Declaration * >::iterator i = declList.begin(); |
---|
| 215 | while ( i != declList.end() ) { |
---|
| 216 | std::list< Declaration * >::iterator next = i; |
---|
| 217 | ++next; |
---|
| 218 | if ( pred( *i ) ) { |
---|
| 219 | if ( doDelete ) { |
---|
| 220 | delete *i; |
---|
| 221 | } // if |
---|
| 222 | declList.erase( i ); |
---|
| 223 | } // if |
---|
| 224 | i = next; |
---|
| 225 | } // while |
---|
[a08ba92] | 226 | } |
---|
[c8ffe20b] | 227 | |
---|
[a08ba92] | 228 | bool isStructOrUnion( Declaration *decl ) { |
---|
[0dd3a2f] | 229 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); |
---|
[a08ba92] | 230 | } |
---|
[51b7345] | 231 | |
---|
[a08ba92] | 232 | template< typename AggDecl > |
---|
| 233 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 234 | if ( inStruct ) { |
---|
| 235 | // Add elements in stack order corresponding to nesting structure. |
---|
| 236 | declsToAdd.push_front( aggregateDecl ); |
---|
| 237 | Visitor::visit( aggregateDecl ); |
---|
| 238 | } else { |
---|
| 239 | inStruct = true; |
---|
| 240 | Visitor::visit( aggregateDecl ); |
---|
| 241 | inStruct = false; |
---|
| 242 | } // if |
---|
| 243 | // Always remove the hoisted aggregate from the inner structure. |
---|
| 244 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); |
---|
[a08ba92] | 245 | } |
---|
[c8ffe20b] | 246 | |
---|
[a08ba92] | 247 | void HoistStruct::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 248 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 249 | } |
---|
[c8ffe20b] | 250 | |
---|
[a08ba92] | 251 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 252 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 253 | } |
---|
[c8ffe20b] | 254 | |
---|
[a08ba92] | 255 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 256 | addVisit( compoundStmt, *this ); |
---|
[a08ba92] | 257 | } |
---|
[c8ffe20b] | 258 | |
---|
[a08ba92] | 259 | void HoistStruct::visit( IfStmt *ifStmt ) { |
---|
[0dd3a2f] | 260 | addVisit( ifStmt, *this ); |
---|
[a08ba92] | 261 | } |
---|
[c8ffe20b] | 262 | |
---|
[a08ba92] | 263 | void HoistStruct::visit( WhileStmt *whileStmt ) { |
---|
[0dd3a2f] | 264 | addVisit( whileStmt, *this ); |
---|
[a08ba92] | 265 | } |
---|
[c8ffe20b] | 266 | |
---|
[a08ba92] | 267 | void HoistStruct::visit( ForStmt *forStmt ) { |
---|
[0dd3a2f] | 268 | addVisit( forStmt, *this ); |
---|
[a08ba92] | 269 | } |
---|
[c8ffe20b] | 270 | |
---|
[a08ba92] | 271 | void HoistStruct::visit( SwitchStmt *switchStmt ) { |
---|
[0dd3a2f] | 272 | addVisit( switchStmt, *this ); |
---|
[a08ba92] | 273 | } |
---|
[c8ffe20b] | 274 | |
---|
[a08ba92] | 275 | void HoistStruct::visit( ChooseStmt *switchStmt ) { |
---|
[0dd3a2f] | 276 | addVisit( switchStmt, *this ); |
---|
[a08ba92] | 277 | } |
---|
[c8ffe20b] | 278 | |
---|
[a08ba92] | 279 | void HoistStruct::visit( CaseStmt *caseStmt ) { |
---|
[0dd3a2f] | 280 | addVisit( caseStmt, *this ); |
---|
[a08ba92] | 281 | } |
---|
[c8ffe20b] | 282 | |
---|
[a08ba92] | 283 | void HoistStruct::visit( CatchStmt *cathStmt ) { |
---|
[0dd3a2f] | 284 | addVisit( cathStmt, *this ); |
---|
[a08ba92] | 285 | } |
---|
[c8ffe20b] | 286 | |
---|
[a08ba92] | 287 | void Pass1::visit( EnumDecl *enumDecl ) { |
---|
[0dd3a2f] | 288 | // Set the type of each member of the enumeration to be EnumConstant |
---|
[c8ffe20b] | 289 | |
---|
[0dd3a2f] | 290 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { |
---|
| 291 | ObjectDecl *obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
| 292 | assert( obj ); |
---|
| 293 | obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); |
---|
| 294 | } // for |
---|
| 295 | Parent::visit( enumDecl ); |
---|
[a08ba92] | 296 | } |
---|
[51b7345] | 297 | |
---|
[a08ba92] | 298 | namespace { |
---|
[0dd3a2f] | 299 | template< typename DWTIterator > |
---|
| 300 | void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) { |
---|
| 301 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed |
---|
| 302 | // entirely other fix ups are handled by the FixFunction class |
---|
| 303 | if ( begin == end ) return; |
---|
| 304 | FixFunction fixer; |
---|
| 305 | DWTIterator i = begin; |
---|
| 306 | *i = (*i )->acceptMutator( fixer ); |
---|
| 307 | if ( fixer.get_isVoid() ) { |
---|
| 308 | DWTIterator j = i; |
---|
| 309 | ++i; |
---|
| 310 | func->get_parameters().erase( j ); |
---|
| 311 | if ( i != end ) { |
---|
| 312 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 313 | } // if |
---|
| 314 | } else { |
---|
| 315 | ++i; |
---|
| 316 | for ( ; i != end; ++i ) { |
---|
| 317 | FixFunction fixer; |
---|
| 318 | *i = (*i )->acceptMutator( fixer ); |
---|
| 319 | if ( fixer.get_isVoid() ) { |
---|
| 320 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 321 | } // if |
---|
| 322 | } // for |
---|
| 323 | } // if |
---|
| 324 | } |
---|
[a08ba92] | 325 | } |
---|
[c8ffe20b] | 326 | |
---|
[a08ba92] | 327 | void Pass1::visit( FunctionType *func ) { |
---|
[0dd3a2f] | 328 | // Fix up parameters and return types |
---|
| 329 | fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func ); |
---|
| 330 | fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func ); |
---|
| 331 | Visitor::visit( func ); |
---|
[a08ba92] | 332 | } |
---|
[c8ffe20b] | 333 | |
---|
[a08ba92] | 334 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { |
---|
[0dd3a2f] | 335 | if ( other_indexer ) { |
---|
| 336 | indexer = other_indexer; |
---|
| 337 | } else { |
---|
| 338 | indexer = this; |
---|
| 339 | } // if |
---|
[a08ba92] | 340 | } |
---|
[c8ffe20b] | 341 | |
---|
[a08ba92] | 342 | void Pass2::visit( StructInstType *structInst ) { |
---|
[0dd3a2f] | 343 | Parent::visit( structInst ); |
---|
| 344 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); |
---|
| 345 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 346 | if ( st ) { |
---|
| 347 | assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); |
---|
| 348 | structInst->set_baseStruct( st ); |
---|
| 349 | } // if |
---|
| 350 | if ( ! st || st->get_members().empty() ) { |
---|
| 351 | // use of forward declaration |
---|
| 352 | forwardStructs[ structInst->get_name() ].push_back( structInst ); |
---|
| 353 | } // if |
---|
[a08ba92] | 354 | } |
---|
[c8ffe20b] | 355 | |
---|
[a08ba92] | 356 | void Pass2::visit( UnionInstType *unionInst ) { |
---|
[0dd3a2f] | 357 | Parent::visit( unionInst ); |
---|
| 358 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); |
---|
| 359 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
| 360 | if ( un ) { |
---|
| 361 | unionInst->set_baseUnion( un ); |
---|
| 362 | } // if |
---|
| 363 | if ( ! un || un->get_members().empty() ) { |
---|
| 364 | // use of forward declaration |
---|
| 365 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); |
---|
| 366 | } // if |
---|
[a08ba92] | 367 | } |
---|
[c8ffe20b] | 368 | |
---|
[a08ba92] | 369 | void Pass2::visit( ContextInstType *contextInst ) { |
---|
[0dd3a2f] | 370 | Parent::visit( contextInst ); |
---|
| 371 | ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() ); |
---|
| 372 | if ( ! ctx ) { |
---|
| 373 | throw SemanticError( "use of undeclared context " + contextInst->get_name() ); |
---|
[17cd4eb] | 374 | } // if |
---|
[0dd3a2f] | 375 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { |
---|
| 376 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { |
---|
| 377 | if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) { |
---|
| 378 | cloneAll( otherCtx->get_members(), contextInst->get_members() ); |
---|
| 379 | } else { |
---|
| 380 | contextInst->get_members().push_back( (*assert )->clone() ); |
---|
| 381 | } // if |
---|
| 382 | } // for |
---|
| 383 | } // for |
---|
| 384 | 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] | 385 | } |
---|
[c8ffe20b] | 386 | |
---|
[a08ba92] | 387 | void Pass2::visit( StructDecl *structDecl ) { |
---|
[0dd3a2f] | 388 | if ( ! structDecl->get_members().empty() ) { |
---|
| 389 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
| 390 | if ( fwds != forwardStructs.end() ) { |
---|
| 391 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 392 | (*inst )->set_baseStruct( structDecl ); |
---|
| 393 | } // for |
---|
| 394 | forwardStructs.erase( fwds ); |
---|
| 395 | } // if |
---|
| 396 | } // if |
---|
| 397 | Indexer::visit( structDecl ); |
---|
[a08ba92] | 398 | } |
---|
[c8ffe20b] | 399 | |
---|
[a08ba92] | 400 | void Pass2::visit( UnionDecl *unionDecl ) { |
---|
[0dd3a2f] | 401 | if ( ! unionDecl->get_members().empty() ) { |
---|
| 402 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
| 403 | if ( fwds != forwardUnions.end() ) { |
---|
| 404 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 405 | (*inst )->set_baseUnion( unionDecl ); |
---|
| 406 | } // for |
---|
| 407 | forwardUnions.erase( fwds ); |
---|
| 408 | } // if |
---|
| 409 | } // if |
---|
| 410 | Indexer::visit( unionDecl ); |
---|
[a08ba92] | 411 | } |
---|
[c8ffe20b] | 412 | |
---|
[a08ba92] | 413 | void Pass2::visit( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 414 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
| 415 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
| 416 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
| 417 | } // if |
---|
| 418 | } // if |
---|
[a08ba92] | 419 | } |
---|
[c8ffe20b] | 420 | |
---|
[a08ba92] | 421 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
[0dd3a2f] | 422 | if ( other_indexer ) { |
---|
| 423 | indexer = other_indexer; |
---|
| 424 | } else { |
---|
| 425 | indexer = this; |
---|
| 426 | } // if |
---|
[a08ba92] | 427 | } |
---|
[c8ffe20b] | 428 | |
---|
[a08ba92] | 429 | void forallFixer( Type *func ) { |
---|
[0dd3a2f] | 430 | // Fix up assertions |
---|
| 431 | for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { |
---|
| 432 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
| 433 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); |
---|
| 434 | while ( ! toBeDone.empty() ) { |
---|
| 435 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { |
---|
| 436 | if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) { |
---|
| 437 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { |
---|
| 438 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); |
---|
| 439 | assert( dwt ); |
---|
| 440 | nextRound.push_back( dwt->clone() ); |
---|
| 441 | } |
---|
| 442 | delete ctx; |
---|
| 443 | } else { |
---|
| 444 | FixFunction fixer; |
---|
| 445 | *assertion = (*assertion )->acceptMutator( fixer ); |
---|
| 446 | if ( fixer.get_isVoid() ) { |
---|
| 447 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
| 448 | } |
---|
| 449 | (*type )->get_assertions().push_back( *assertion ); |
---|
| 450 | } // if |
---|
| 451 | } // for |
---|
| 452 | toBeDone.clear(); |
---|
| 453 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
| 454 | } // while |
---|
| 455 | } // for |
---|
[a08ba92] | 456 | } |
---|
[c8ffe20b] | 457 | |
---|
[a08ba92] | 458 | void Pass3::visit( ObjectDecl *object ) { |
---|
[0dd3a2f] | 459 | forallFixer( object->get_type() ); |
---|
| 460 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
| 461 | forallFixer( pointer->get_base() ); |
---|
| 462 | } // if |
---|
| 463 | Parent::visit( object ); |
---|
| 464 | object->fixUniqueId(); |
---|
[a08ba92] | 465 | } |
---|
[c8ffe20b] | 466 | |
---|
[a08ba92] | 467 | void Pass3::visit( FunctionDecl *func ) { |
---|
[0dd3a2f] | 468 | forallFixer( func->get_type() ); |
---|
| 469 | Parent::visit( func ); |
---|
| 470 | func->fixUniqueId(); |
---|
[a08ba92] | 471 | } |
---|
[c8ffe20b] | 472 | |
---|
[a08ba92] | 473 | static const std::list< std::string > noLabels; |
---|
[c8ffe20b] | 474 | |
---|
[a08ba92] | 475 | void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 476 | AddStructAssignment visitor; |
---|
| 477 | acceptAndAdd( translationUnit, visitor, false ); |
---|
[a08ba92] | 478 | } |
---|
[c8ffe20b] | 479 | |
---|
[a08ba92] | 480 | template< typename OutputIterator > |
---|
| 481 | void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) { |
---|
[0dd3a2f] | 482 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member ); |
---|
| 483 | // unnamed bit fields are not copied as they cannot be accessed |
---|
| 484 | if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return; |
---|
[c8ffe20b] | 485 | |
---|
[0dd3a2f] | 486 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
[c8ffe20b] | 487 | |
---|
[0dd3a2f] | 488 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 489 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
[c8ffe20b] | 490 | |
---|
[0dd3a2f] | 491 | // do something special for unnamed members |
---|
| 492 | Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) ); |
---|
| 493 | assignExpr->get_args().push_back( dstselect ); |
---|
[c8ffe20b] | 494 | |
---|
[0dd3a2f] | 495 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) ); |
---|
| 496 | assignExpr->get_args().push_back( srcselect ); |
---|
[c8ffe20b] | 497 | |
---|
[0dd3a2f] | 498 | *out++ = new ExprStmt( noLabels, assignExpr ); |
---|
[a08ba92] | 499 | } |
---|
[c8ffe20b] | 500 | |
---|
[a08ba92] | 501 | template< typename OutputIterator > |
---|
| 502 | void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) { |
---|
[0dd3a2f] | 503 | static UniqueName indexName( "_index" ); |
---|
[c8ffe20b] | 504 | |
---|
[0dd3a2f] | 505 | // for a flexible array member nothing is done -- user must define own assignment |
---|
| 506 | if ( ! array->get_dimension() ) return; |
---|
[c8ffe20b] | 507 | |
---|
[0dd3a2f] | 508 | ObjectDecl *index = new ObjectDecl( indexName.newName(), Declaration::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ); |
---|
| 509 | *out++ = new DeclStmt( noLabels, index ); |
---|
[c8ffe20b] | 510 | |
---|
[0dd3a2f] | 511 | UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 512 | init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
| 513 | init->get_args().push_back( new NameExpr( "0" ) ); |
---|
| 514 | Statement *initStmt = new ExprStmt( noLabels, init ); |
---|
[c8ffe20b] | 515 | |
---|
[0dd3a2f] | 516 | UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) ); |
---|
| 517 | cond->get_args().push_back( new VariableExpr( index ) ); |
---|
| 518 | cond->get_args().push_back( array->get_dimension()->clone() ); |
---|
[c8ffe20b] | 519 | |
---|
[0dd3a2f] | 520 | UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
| 521 | inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) ); |
---|
[c8ffe20b] | 522 | |
---|
[0dd3a2f] | 523 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
[c8ffe20b] | 524 | |
---|
[0dd3a2f] | 525 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
| 526 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
[c8ffe20b] | 527 | |
---|
[0dd3a2f] | 528 | Expression *dstselect = new MemberExpr( member, derefExpr ); |
---|
| 529 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) ); |
---|
| 530 | dstIndex->get_args().push_back( dstselect ); |
---|
| 531 | dstIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
| 532 | assignExpr->get_args().push_back( dstIndex ); |
---|
[c8ffe20b] | 533 | |
---|
[0dd3a2f] | 534 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) ); |
---|
| 535 | UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) ); |
---|
| 536 | srcIndex->get_args().push_back( srcselect ); |
---|
| 537 | srcIndex->get_args().push_back( new VariableExpr( index ) ); |
---|
| 538 | assignExpr->get_args().push_back( srcIndex ); |
---|
[c8ffe20b] | 539 | |
---|
[0dd3a2f] | 540 | *out++ = new ForStmt( noLabels, initStmt, cond, inc, new ExprStmt( noLabels, assignExpr ) ); |
---|
[a08ba92] | 541 | } |
---|
[c8ffe20b] | 542 | |
---|
[a08ba92] | 543 | Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) { |
---|
[0dd3a2f] | 544 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
| 545 | |
---|
| 546 | ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 ); |
---|
| 547 | assignType->get_returnVals().push_back( returnVal ); |
---|
| 548 | |
---|
| 549 | ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 ); |
---|
| 550 | assignType->get_parameters().push_back( dstParam ); |
---|
| 551 | |
---|
| 552 | ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 ); |
---|
| 553 | assignType->get_parameters().push_back( srcParam ); |
---|
| 554 | |
---|
| 555 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
| 556 | // because each unit generates copies of the default routines for each aggregate. |
---|
| 557 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? Declaration::NoStorageClass : Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true ); |
---|
| 558 | assignDecl->fixUniqueId(); |
---|
| 559 | |
---|
| 560 | for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) { |
---|
| 561 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) { |
---|
[367e082] | 562 | // query the type qualifiers of this field and skip assigning it if it is marked const. |
---|
| 563 | // If it is an array type, we need to strip off the array layers to find its qualifiers. |
---|
| 564 | Type * type = dwt->get_type(); |
---|
| 565 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 566 | type = at->get_base(); |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | if ( type->get_qualifiers().isConst ) { |
---|
[53a2e97] | 570 | // don't assign const members |
---|
| 571 | continue; |
---|
| 572 | } |
---|
| 573 | |
---|
[0dd3a2f] | 574 | if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) { |
---|
| 575 | makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
| 576 | } else { |
---|
| 577 | makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) ); |
---|
| 578 | } // if |
---|
| 579 | } // if |
---|
| 580 | } // for |
---|
| 581 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
[c8ffe20b] | 582 | |
---|
[0dd3a2f] | 583 | return assignDecl; |
---|
[a08ba92] | 584 | } |
---|
[c8ffe20b] | 585 | |
---|
[a08ba92] | 586 | Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) { |
---|
[0dd3a2f] | 587 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
[c8ffe20b] | 588 | |
---|
[0dd3a2f] | 589 | ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 ); |
---|
| 590 | assignType->get_returnVals().push_back( returnVal ); |
---|
[c8ffe20b] | 591 | |
---|
[0dd3a2f] | 592 | ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 ); |
---|
| 593 | assignType->get_parameters().push_back( dstParam ); |
---|
[c8ffe20b] | 594 | |
---|
[0dd3a2f] | 595 | ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 ); |
---|
| 596 | assignType->get_parameters().push_back( srcParam ); |
---|
[c8ffe20b] | 597 | |
---|
[0dd3a2f] | 598 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units |
---|
| 599 | // because each unit generates copies of the default routines for each aggregate. |
---|
| 600 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? Declaration::NoStorageClass : Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true ); |
---|
| 601 | assignDecl->fixUniqueId(); |
---|
[c8ffe20b] | 602 | |
---|
[0dd3a2f] | 603 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) ); |
---|
| 604 | copy->get_args().push_back( new VariableExpr( dstParam ) ); |
---|
| 605 | copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) ); |
---|
| 606 | copy->get_args().push_back( new SizeofExpr( refType->clone() ) ); |
---|
[c8ffe20b] | 607 | |
---|
[0dd3a2f] | 608 | assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) ); |
---|
| 609 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) ); |
---|
[c8ffe20b] | 610 | |
---|
[0dd3a2f] | 611 | return assignDecl; |
---|
[a08ba92] | 612 | } |
---|
[c8ffe20b] | 613 | |
---|
[a08ba92] | 614 | void AddStructAssignment::visit( StructDecl *structDecl ) { |
---|
[0dd3a2f] | 615 | if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) { |
---|
| 616 | StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() ); |
---|
| 617 | structInst->set_baseStruct( structDecl ); |
---|
| 618 | declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) ); |
---|
| 619 | structsDone.insert( structDecl->get_name() ); |
---|
| 620 | } // if |
---|
[a08ba92] | 621 | } |
---|
[c8ffe20b] | 622 | |
---|
[a08ba92] | 623 | void AddStructAssignment::visit( UnionDecl *unionDecl ) { |
---|
[0dd3a2f] | 624 | if ( ! unionDecl->get_members().empty() ) { |
---|
| 625 | UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() ); |
---|
| 626 | unionInst->set_baseUnion( unionDecl ); |
---|
| 627 | declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) ); |
---|
| 628 | } // if |
---|
[a08ba92] | 629 | } |
---|
[c8ffe20b] | 630 | |
---|
[a08ba92] | 631 | void AddStructAssignment::visit( TypeDecl *typeDecl ) { |
---|
[0dd3a2f] | 632 | CompoundStmt *stmts = 0; |
---|
| 633 | TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false ); |
---|
| 634 | typeInst->set_baseType( typeDecl ); |
---|
| 635 | ObjectDecl *src = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 ); |
---|
| 636 | ObjectDecl *dst = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 ); |
---|
| 637 | if ( typeDecl->get_base() ) { |
---|
| 638 | stmts = new CompoundStmt( std::list< Label >() ); |
---|
| 639 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 640 | assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) ); |
---|
| 641 | assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) ); |
---|
| 642 | stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) ); |
---|
| 643 | } // if |
---|
| 644 | FunctionType *type = new FunctionType( Type::Qualifiers(), false ); |
---|
| 645 | type->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) ); |
---|
| 646 | type->get_parameters().push_back( dst ); |
---|
| 647 | type->get_parameters().push_back( src ); |
---|
| 648 | FunctionDecl *func = new FunctionDecl( "?=?", Declaration::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false ); |
---|
| 649 | declsToAdd.push_back( func ); |
---|
[a08ba92] | 650 | } |
---|
[c8ffe20b] | 651 | |
---|
[a08ba92] | 652 | void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) { |
---|
[5f2f2d7] | 653 | for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) { |
---|
| 654 | statements.insert( i, new DeclStmt( noLabels, *decl ) ); |
---|
| 655 | } // for |
---|
| 656 | declsToAdd.clear(); |
---|
[a08ba92] | 657 | } |
---|
[c8ffe20b] | 658 | |
---|
[a08ba92] | 659 | void AddStructAssignment::visit( FunctionType *) { |
---|
[0dd3a2f] | 660 | // ensure that we don't add assignment ops for types defined as part of the function |
---|
[a08ba92] | 661 | } |
---|
[c8ffe20b] | 662 | |
---|
[a08ba92] | 663 | void AddStructAssignment::visit( PointerType *) { |
---|
[0dd3a2f] | 664 | // ensure that we don't add assignment ops for types defined as part of the pointer |
---|
[a08ba92] | 665 | } |
---|
[c8ffe20b] | 666 | |
---|
[a08ba92] | 667 | void AddStructAssignment::visit( ContextDecl *) { |
---|
[0dd3a2f] | 668 | // ensure that we don't add assignment ops for types defined as part of the context |
---|
[a08ba92] | 669 | } |
---|
[c8ffe20b] | 670 | |
---|
[a08ba92] | 671 | template< typename StmtClass > |
---|
| 672 | inline void AddStructAssignment::visitStatement( StmtClass *stmt ) { |
---|
[0dd3a2f] | 673 | std::set< std::string > oldStructs = structsDone; |
---|
| 674 | addVisit( stmt, *this ); |
---|
| 675 | structsDone = oldStructs; |
---|
[a08ba92] | 676 | } |
---|
[c8ffe20b] | 677 | |
---|
[a08ba92] | 678 | void AddStructAssignment::visit( FunctionDecl *functionDecl ) { |
---|
[0dd3a2f] | 679 | maybeAccept( functionDecl->get_functionType(), *this ); |
---|
| 680 | acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
| 681 | functionNesting += 1; |
---|
| 682 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
| 683 | functionNesting -= 1; |
---|
[a08ba92] | 684 | } |
---|
[3c70d38] | 685 | |
---|
[a08ba92] | 686 | void AddStructAssignment::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 687 | visitStatement( compoundStmt ); |
---|
[a08ba92] | 688 | } |
---|
[c8ffe20b] | 689 | |
---|
[a08ba92] | 690 | void AddStructAssignment::visit( IfStmt *ifStmt ) { |
---|
[0dd3a2f] | 691 | visitStatement( ifStmt ); |
---|
[a08ba92] | 692 | } |
---|
[c8ffe20b] | 693 | |
---|
[a08ba92] | 694 | void AddStructAssignment::visit( WhileStmt *whileStmt ) { |
---|
[0dd3a2f] | 695 | visitStatement( whileStmt ); |
---|
[a08ba92] | 696 | } |
---|
[c8ffe20b] | 697 | |
---|
[a08ba92] | 698 | void AddStructAssignment::visit( ForStmt *forStmt ) { |
---|
[0dd3a2f] | 699 | visitStatement( forStmt ); |
---|
[a08ba92] | 700 | } |
---|
[c8ffe20b] | 701 | |
---|
[a08ba92] | 702 | void AddStructAssignment::visit( SwitchStmt *switchStmt ) { |
---|
[0dd3a2f] | 703 | visitStatement( switchStmt ); |
---|
[a08ba92] | 704 | } |
---|
[c8ffe20b] | 705 | |
---|
[a08ba92] | 706 | void AddStructAssignment::visit( ChooseStmt *switchStmt ) { |
---|
[0dd3a2f] | 707 | visitStatement( switchStmt ); |
---|
[a08ba92] | 708 | } |
---|
[c8ffe20b] | 709 | |
---|
[a08ba92] | 710 | void AddStructAssignment::visit( CaseStmt *caseStmt ) { |
---|
[0dd3a2f] | 711 | visitStatement( caseStmt ); |
---|
[a08ba92] | 712 | } |
---|
[c8ffe20b] | 713 | |
---|
[a08ba92] | 714 | void AddStructAssignment::visit( CatchStmt *cathStmt ) { |
---|
[0dd3a2f] | 715 | visitStatement( cathStmt ); |
---|
[a08ba92] | 716 | } |
---|
[c8ffe20b] | 717 | |
---|
[a08ba92] | 718 | bool isTypedef( Declaration *decl ) { |
---|
[0dd3a2f] | 719 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
[a08ba92] | 720 | } |
---|
[c8ffe20b] | 721 | |
---|
[a08ba92] | 722 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 723 | EliminateTypedef eliminator; |
---|
| 724 | mutateAll( translationUnit, eliminator ); |
---|
| 725 | filter( translationUnit, isTypedef, true ); |
---|
[a08ba92] | 726 | } |
---|
[c8ffe20b] | 727 | |
---|
[a08ba92] | 728 | Type *EliminateTypedef::mutate( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 729 | std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
| 730 | if ( def != typedefNames.end() ) { |
---|
| 731 | Type *ret = def->second->get_base()->clone(); |
---|
| 732 | ret->get_qualifiers() += typeInst->get_qualifiers(); |
---|
| 733 | delete typeInst; |
---|
| 734 | return ret; |
---|
| 735 | } // if |
---|
| 736 | return typeInst; |
---|
[a08ba92] | 737 | } |
---|
[c8ffe20b] | 738 | |
---|
[a08ba92] | 739 | Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) { |
---|
[0dd3a2f] | 740 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
| 741 | typedefNames[ tyDecl->get_name() ] = tyDecl; |
---|
| 742 | // When a typedef is a forward declaration: |
---|
| 743 | // typedef struct screen SCREEN; |
---|
| 744 | // the declaration portion must be retained: |
---|
| 745 | // struct screen; |
---|
| 746 | // because the expansion of the typedef is: |
---|
| 747 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
| 748 | // hence the type-name "screen" must be defined. |
---|
| 749 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
| 750 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { |
---|
| 751 | return new StructDecl( aggDecl->get_name() ); |
---|
| 752 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { |
---|
| 753 | return new UnionDecl( aggDecl->get_name() ); |
---|
| 754 | } else { |
---|
| 755 | return ret; |
---|
| 756 | } // if |
---|
[a08ba92] | 757 | } |
---|
[c8ffe20b] | 758 | |
---|
[a08ba92] | 759 | TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) { |
---|
[0dd3a2f] | 760 | std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
| 761 | if ( i != typedefNames.end() ) { |
---|
| 762 | typedefNames.erase( i ) ; |
---|
| 763 | } // if |
---|
| 764 | return typeDecl; |
---|
[a08ba92] | 765 | } |
---|
[c8ffe20b] | 766 | |
---|
[a08ba92] | 767 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) { |
---|
[0dd3a2f] | 768 | std::map< std::string, TypedefDecl * > oldNames = typedefNames; |
---|
| 769 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
| 770 | typedefNames = oldNames; |
---|
| 771 | return ret; |
---|
[a08ba92] | 772 | } |
---|
[c8ffe20b] | 773 | |
---|
[a08ba92] | 774 | ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) { |
---|
[0dd3a2f] | 775 | std::map< std::string, TypedefDecl * > oldNames = typedefNames; |
---|
| 776 | ObjectDecl *ret = Mutator::mutate( objDecl ); |
---|
| 777 | typedefNames = oldNames; |
---|
| 778 | return ret; |
---|
[a08ba92] | 779 | } |
---|
[c8ffe20b] | 780 | |
---|
[a08ba92] | 781 | Expression *EliminateTypedef::mutate( CastExpr *castExpr ) { |
---|
[0dd3a2f] | 782 | std::map< std::string, TypedefDecl * > oldNames = typedefNames; |
---|
| 783 | Expression *ret = Mutator::mutate( castExpr ); |
---|
| 784 | typedefNames = oldNames; |
---|
| 785 | return ret; |
---|
[a08ba92] | 786 | } |
---|
[c8ffe20b] | 787 | |
---|
[a08ba92] | 788 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 789 | std::map< std::string, TypedefDecl * > oldNames = typedefNames; |
---|
| 790 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
| 791 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
| 792 | while ( i != compoundStmt->get_kids().end() ) { |
---|
| 793 | std::list< Statement * >::iterator next = i; |
---|
| 794 | ++next; |
---|
| 795 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
| 796 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
| 797 | delete *i; |
---|
| 798 | compoundStmt->get_kids().erase( i ); |
---|
| 799 | } // if |
---|
| 800 | } // if |
---|
| 801 | i = next; |
---|
| 802 | } // while |
---|
| 803 | typedefNames = oldNames; |
---|
| 804 | return ret; |
---|
[a08ba92] | 805 | } |
---|
[51b7345] | 806 | } // namespace SymTab |
---|
[0dd3a2f] | 807 | |
---|
| 808 | // Local Variables: // |
---|
| 809 | // tab-width: 4 // |
---|
| 810 | // mode: c++ // |
---|
| 811 | // compile-command: "make install" // |
---|
| 812 | // End: // |
---|