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