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