[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 | //
|
---|
[de91427b] | 7 | // Validate.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 21:50:04 2015
|
---|
[f066321] | 11 | // Last Modified By : Rob Schluntz
|
---|
[de91427b] | 12 | // Last Modified On : Fri Dec 18 15:34:05 2015
|
---|
| 13 | // Update Count : 218
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
|
---|
| 17 | // as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is
|
---|
| 18 | // complete, including:
|
---|
| 19 | //
|
---|
| 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
|
---|
| 21 | // union.
|
---|
| 22 | //
|
---|
| 23 | // - All enumeration constants have type EnumInstType.
|
---|
| 24 | //
|
---|
| 25 | // - The type "void" never occurs in lists of function parameter or return types; neither do tuple types. A function
|
---|
| 26 | // taking no arguments has no argument types, and tuples are flattened.
|
---|
| 27 | //
|
---|
| 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
|
---|
| 29 | // by the particular set of type arguments.
|
---|
| 30 | //
|
---|
| 31 | // - Every declaration is assigned a unique id.
|
---|
| 32 | //
|
---|
| 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance.
|
---|
| 34 | //
|
---|
| 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator.
|
---|
| 36 | //
|
---|
| 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that
|
---|
| 38 | // definition occurs later in the input.
|
---|
[51b73452] | 39 |
|
---|
| 40 | #include <list>
|
---|
| 41 | #include <iterator>
|
---|
| 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"
|
---|
[51b73452] | 49 | #include "FixFunction.h"
|
---|
[cc79d97] | 50 | // #include "ImplementationType.h"
|
---|
[51b73452] | 51 | #include "utility.h"
|
---|
| 52 | #include "UniqueName.h"
|
---|
| 53 | #include "AddVisit.h"
|
---|
[f6d7e0f] | 54 | #include "MakeLibCfa.h"
|
---|
[cc79d97] | 55 | #include "TypeEquality.h"
|
---|
[1cbca6e] | 56 | #include "ResolvExpr/typeops.h"
|
---|
[51b73452] | 57 |
|
---|
[c8ffe20b] | 58 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
|
---|
[51b73452] | 59 |
|
---|
| 60 | namespace SymTab {
|
---|
[a08ba92] | 61 | class HoistStruct : public Visitor {
|
---|
| 62 | public:
|
---|
[82dd287] | 63 | /// Flattens nested struct types
|
---|
[0dd3a2f] | 64 | static void hoistStruct( std::list< Declaration * > &translationUnit );
|
---|
[de91427b] | 65 |
|
---|
[0dd3a2f] | 66 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
|
---|
[de91427b] | 67 |
|
---|
[0dd3a2f] | 68 | virtual void visit( StructDecl *aggregateDecl );
|
---|
| 69 | virtual void visit( UnionDecl *aggregateDecl );
|
---|
[c8ffe20b] | 70 |
|
---|
[0dd3a2f] | 71 | virtual void visit( CompoundStmt *compoundStmt );
|
---|
| 72 | virtual void visit( IfStmt *ifStmt );
|
---|
| 73 | virtual void visit( WhileStmt *whileStmt );
|
---|
| 74 | virtual void visit( ForStmt *forStmt );
|
---|
| 75 | virtual void visit( SwitchStmt *switchStmt );
|
---|
| 76 | virtual void visit( ChooseStmt *chooseStmt );
|
---|
| 77 | virtual void visit( CaseStmt *caseStmt );
|
---|
| 78 | virtual void visit( CatchStmt *catchStmt );
|
---|
[a08ba92] | 79 | private:
|
---|
[0dd3a2f] | 80 | HoistStruct();
|
---|
[c8ffe20b] | 81 |
|
---|
[0dd3a2f] | 82 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
|
---|
[c8ffe20b] | 83 |
|
---|
[0dd3a2f] | 84 | std::list< Declaration * > declsToAdd;
|
---|
| 85 | bool inStruct;
|
---|
[a08ba92] | 86 | };
|
---|
[c8ffe20b] | 87 |
|
---|
[de91427b] | 88 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
|
---|
[a08ba92] | 89 | class Pass1 : public Visitor {
|
---|
[0dd3a2f] | 90 | typedef Visitor Parent;
|
---|
| 91 | virtual void visit( EnumDecl *aggregateDecl );
|
---|
| 92 | virtual void visit( FunctionType *func );
|
---|
[a08ba92] | 93 | };
|
---|
[82dd287] | 94 |
|
---|
| 95 | /// Associates forward declarations of aggregates with their definitions
|
---|
[a08ba92] | 96 | class Pass2 : public Indexer {
|
---|
[0dd3a2f] | 97 | typedef Indexer Parent;
|
---|
[a08ba92] | 98 | public:
|
---|
[0dd3a2f] | 99 | Pass2( bool doDebug, const Indexer *indexer );
|
---|
[a08ba92] | 100 | private:
|
---|
[0dd3a2f] | 101 | virtual void visit( StructInstType *structInst );
|
---|
| 102 | virtual void visit( UnionInstType *unionInst );
|
---|
| 103 | virtual void visit( ContextInstType *contextInst );
|
---|
| 104 | virtual void visit( StructDecl *structDecl );
|
---|
| 105 | virtual void visit( UnionDecl *unionDecl );
|
---|
| 106 | virtual void visit( TypeInstType *typeInst );
|
---|
| 107 |
|
---|
| 108 | const Indexer *indexer;
|
---|
[de91427b] | 109 |
|
---|
[0dd3a2f] | 110 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
|
---|
| 111 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
|
---|
| 112 | ForwardStructsType forwardStructs;
|
---|
| 113 | ForwardUnionsType forwardUnions;
|
---|
[a08ba92] | 114 | };
|
---|
[c8ffe20b] | 115 |
|
---|
[82dd287] | 116 | /// Replaces array and function types in forall lists by appropriate pointer type
|
---|
[a08ba92] | 117 | class Pass3 : public Indexer {
|
---|
[0dd3a2f] | 118 | typedef Indexer Parent;
|
---|
[a08ba92] | 119 | public:
|
---|
[0dd3a2f] | 120 | Pass3( const Indexer *indexer );
|
---|
[a08ba92] | 121 | private:
|
---|
[0dd3a2f] | 122 | virtual void visit( ObjectDecl *object );
|
---|
| 123 | virtual void visit( FunctionDecl *func );
|
---|
[c8ffe20b] | 124 |
|
---|
[0dd3a2f] | 125 | const Indexer *indexer;
|
---|
[a08ba92] | 126 | };
|
---|
[c8ffe20b] | 127 |
|
---|
[f066321] | 128 | class AutogenerateRoutines : public Visitor {
|
---|
[a08ba92] | 129 | public:
|
---|
[82dd287] | 130 | /// Generates assignment operators for aggregate types as required
|
---|
[f066321] | 131 | static void autogenerateRoutines( std::list< Declaration * > &translationUnit );
|
---|
[c8ffe20b] | 132 |
|
---|
[0dd3a2f] | 133 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
|
---|
[de91427b] | 134 |
|
---|
[28a8cf9] | 135 | virtual void visit( EnumDecl *enumDecl );
|
---|
[0dd3a2f] | 136 | virtual void visit( StructDecl *structDecl );
|
---|
| 137 | virtual void visit( UnionDecl *structDecl );
|
---|
| 138 | virtual void visit( TypeDecl *typeDecl );
|
---|
| 139 | virtual void visit( ContextDecl *ctxDecl );
|
---|
| 140 | virtual void visit( FunctionDecl *functionDecl );
|
---|
[c8ffe20b] | 141 |
|
---|
[0dd3a2f] | 142 | virtual void visit( FunctionType *ftype );
|
---|
| 143 | virtual void visit( PointerType *ftype );
|
---|
[de91427b] | 144 |
|
---|
[0dd3a2f] | 145 | virtual void visit( CompoundStmt *compoundStmt );
|
---|
| 146 | virtual void visit( IfStmt *ifStmt );
|
---|
| 147 | virtual void visit( WhileStmt *whileStmt );
|
---|
| 148 | virtual void visit( ForStmt *forStmt );
|
---|
| 149 | virtual void visit( SwitchStmt *switchStmt );
|
---|
| 150 | virtual void visit( ChooseStmt *chooseStmt );
|
---|
| 151 | virtual void visit( CaseStmt *caseStmt );
|
---|
| 152 | virtual void visit( CatchStmt *catchStmt );
|
---|
[3c70d38] | 153 |
|
---|
[f066321] | 154 | AutogenerateRoutines() : functionNesting( 0 ) {}
|
---|
[a08ba92] | 155 | private:
|
---|
[0dd3a2f] | 156 | template< typename StmtClass > void visitStatement( StmtClass *stmt );
|
---|
[de91427b] | 157 |
|
---|
[0dd3a2f] | 158 | std::list< Declaration * > declsToAdd;
|
---|
| 159 | std::set< std::string > structsDone;
|
---|
| 160 | unsigned int functionNesting; // current level of nested functions
|
---|
[a08ba92] | 161 | };
|
---|
[c8ffe20b] | 162 |
|
---|
[de91427b] | 163 | class ReturnChecker : public Visitor {
|
---|
| 164 | public:
|
---|
| 165 | /// Checks that return statements return nothing if their return type is void
|
---|
| 166 | /// and return something if the return type is non-void.
|
---|
| 167 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
|
---|
| 168 |
|
---|
| 169 | private:
|
---|
| 170 | virtual void visit( FunctionDecl * functionDecl );
|
---|
| 171 |
|
---|
| 172 | virtual void visit( ReturnStmt * returnStmt );
|
---|
| 173 |
|
---|
| 174 | std::list< DeclarationWithType * > returnVals;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
[a08ba92] | 177 | class EliminateTypedef : public Mutator {
|
---|
| 178 | public:
|
---|
[de91427b] | 179 | EliminateTypedef() : scopeLevel( 0 ) {}
|
---|
| 180 | /// Replaces typedefs by forward declarations
|
---|
[0dd3a2f] | 181 | static void eliminateTypedef( std::list< Declaration * > &translationUnit );
|
---|
[a08ba92] | 182 | private:
|
---|
[0dd3a2f] | 183 | virtual Declaration *mutate( TypedefDecl *typeDecl );
|
---|
| 184 | virtual TypeDecl *mutate( TypeDecl *typeDecl );
|
---|
| 185 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
|
---|
[1db21619] | 186 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
|
---|
[0dd3a2f] | 187 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
|
---|
| 188 | virtual Type *mutate( TypeInstType *aggregateUseType );
|
---|
| 189 | virtual Expression *mutate( CastExpr *castExpr );
|
---|
[cc79d97] | 190 |
|
---|
[85c4ef0] | 191 | virtual Declaration *mutate( StructDecl * structDecl );
|
---|
| 192 | virtual Declaration *mutate( UnionDecl * unionDecl );
|
---|
| 193 | virtual Declaration *mutate( EnumDecl * enumDecl );
|
---|
| 194 | virtual Declaration *mutate( ContextDecl * contextDecl );
|
---|
| 195 |
|
---|
| 196 | template<typename AggDecl>
|
---|
| 197 | AggDecl *handleAggregate( AggDecl * aggDecl );
|
---|
| 198 |
|
---|
[cc79d97] | 199 | typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
|
---|
| 200 | TypedefMap typedefNames;
|
---|
| 201 | int scopeLevel;
|
---|
[a08ba92] | 202 | };
|
---|
[c8ffe20b] | 203 |
|
---|
[a08ba92] | 204 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
|
---|
[0dd3a2f] | 205 | Pass1 pass1;
|
---|
| 206 | Pass2 pass2( doDebug, 0 );
|
---|
| 207 | Pass3 pass3( 0 );
|
---|
| 208 | EliminateTypedef::eliminateTypedef( translationUnit );
|
---|
| 209 | HoistStruct::hoistStruct( translationUnit );
|
---|
| 210 | acceptAll( translationUnit, pass1 );
|
---|
| 211 | acceptAll( translationUnit, pass2 );
|
---|
[de91427b] | 212 | ReturnChecker::checkFunctionReturns( translationUnit );
|
---|
[f066321] | 213 | AutogenerateRoutines::autogenerateRoutines( translationUnit );
|
---|
[0dd3a2f] | 214 | acceptAll( translationUnit, pass3 );
|
---|
[a08ba92] | 215 | }
|
---|
[de91427b] | 216 |
|
---|
[a08ba92] | 217 | void validateType( Type *type, const Indexer *indexer ) {
|
---|
[0dd3a2f] | 218 | Pass1 pass1;
|
---|
| 219 | Pass2 pass2( false, indexer );
|
---|
| 220 | Pass3 pass3( indexer );
|
---|
| 221 | type->accept( pass1 );
|
---|
| 222 | type->accept( pass2 );
|
---|
| 223 | type->accept( pass3 );
|
---|
[a08ba92] | 224 | }
|
---|
[c8ffe20b] | 225 |
|
---|
[a08ba92] | 226 | template< typename Visitor >
|
---|
| 227 | void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) {
|
---|
[0dd3a2f] | 228 | std::list< Declaration * >::iterator i = translationUnit.begin();
|
---|
| 229 | while ( i != translationUnit.end() ) {
|
---|
| 230 | (*i)->accept( visitor );
|
---|
| 231 | std::list< Declaration * >::iterator next = i;
|
---|
| 232 | next++;
|
---|
| 233 | if ( ! visitor.get_declsToAdd().empty() ) {
|
---|
| 234 | translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );
|
---|
| 235 | } // if
|
---|
| 236 | i = next;
|
---|
| 237 | } // while
|
---|
[a08ba92] | 238 | }
|
---|
[c8ffe20b] | 239 |
|
---|
[a08ba92] | 240 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
|
---|
[0dd3a2f] | 241 | HoistStruct hoister;
|
---|
| 242 | acceptAndAdd( translationUnit, hoister, true );
|
---|
[a08ba92] | 243 | }
|
---|
[c8ffe20b] | 244 |
|
---|
[a08ba92] | 245 | HoistStruct::HoistStruct() : inStruct( false ) {
|
---|
| 246 | }
|
---|
[c8ffe20b] | 247 |
|
---|
[a08ba92] | 248 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
|
---|
[0dd3a2f] | 249 | std::list< Declaration * >::iterator i = declList.begin();
|
---|
| 250 | while ( i != declList.end() ) {
|
---|
| 251 | std::list< Declaration * >::iterator next = i;
|
---|
| 252 | ++next;
|
---|
| 253 | if ( pred( *i ) ) {
|
---|
| 254 | if ( doDelete ) {
|
---|
| 255 | delete *i;
|
---|
| 256 | } // if
|
---|
| 257 | declList.erase( i );
|
---|
| 258 | } // if
|
---|
| 259 | i = next;
|
---|
| 260 | } // while
|
---|
[a08ba92] | 261 | }
|
---|
[c8ffe20b] | 262 |
|
---|
[a08ba92] | 263 | bool isStructOrUnion( Declaration *decl ) {
|
---|
[0dd3a2f] | 264 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
|
---|
[a08ba92] | 265 | }
|
---|
[51b73452] | 266 |
|
---|
[a08ba92] | 267 | template< typename AggDecl >
|
---|
| 268 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 269 | if ( inStruct ) {
|
---|
| 270 | // Add elements in stack order corresponding to nesting structure.
|
---|
| 271 | declsToAdd.push_front( aggregateDecl );
|
---|
| 272 | Visitor::visit( aggregateDecl );
|
---|
| 273 | } else {
|
---|
| 274 | inStruct = true;
|
---|
| 275 | Visitor::visit( aggregateDecl );
|
---|
| 276 | inStruct = false;
|
---|
| 277 | } // if
|
---|
| 278 | // Always remove the hoisted aggregate from the inner structure.
|
---|
| 279 | filter( aggregateDecl->get_members(), isStructOrUnion, false );
|
---|
[a08ba92] | 280 | }
|
---|
[c8ffe20b] | 281 |
|
---|
[a08ba92] | 282 | void HoistStruct::visit( StructDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 283 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 284 | }
|
---|
[c8ffe20b] | 285 |
|
---|
[a08ba92] | 286 | void HoistStruct::visit( UnionDecl *aggregateDecl ) {
|
---|
[0dd3a2f] | 287 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 288 | }
|
---|
[c8ffe20b] | 289 |
|
---|
[a08ba92] | 290 | void HoistStruct::visit( CompoundStmt *compoundStmt ) {
|
---|
[0dd3a2f] | 291 | addVisit( compoundStmt, *this );
|
---|
[a08ba92] | 292 | }
|
---|
[c8ffe20b] | 293 |
|
---|
[a08ba92] | 294 | void HoistStruct::visit( IfStmt *ifStmt ) {
|
---|
[0dd3a2f] | 295 | addVisit( ifStmt, *this );
|
---|
[a08ba92] | 296 | }
|
---|
[c8ffe20b] | 297 |
|
---|
[a08ba92] | 298 | void HoistStruct::visit( WhileStmt *whileStmt ) {
|
---|
[0dd3a2f] | 299 | addVisit( whileStmt, *this );
|
---|
[a08ba92] | 300 | }
|
---|
[c8ffe20b] | 301 |
|
---|
[a08ba92] | 302 | void HoistStruct::visit( ForStmt *forStmt ) {
|
---|
[0dd3a2f] | 303 | addVisit( forStmt, *this );
|
---|
[a08ba92] | 304 | }
|
---|
[c8ffe20b] | 305 |
|
---|
[a08ba92] | 306 | void HoistStruct::visit( SwitchStmt *switchStmt ) {
|
---|
[0dd3a2f] | 307 | addVisit( switchStmt, *this );
|
---|
[a08ba92] | 308 | }
|
---|
[c8ffe20b] | 309 |
|
---|
[a08ba92] | 310 | void HoistStruct::visit( ChooseStmt *switchStmt ) {
|
---|
[0dd3a2f] | 311 | addVisit( switchStmt, *this );
|
---|
[a08ba92] | 312 | }
|
---|
[c8ffe20b] | 313 |
|
---|
[a08ba92] | 314 | void HoistStruct::visit( CaseStmt *caseStmt ) {
|
---|
[0dd3a2f] | 315 | addVisit( caseStmt, *this );
|
---|
[a08ba92] | 316 | }
|
---|
[c8ffe20b] | 317 |
|
---|
[a08ba92] | 318 | void HoistStruct::visit( CatchStmt *cathStmt ) {
|
---|
[0dd3a2f] | 319 | addVisit( cathStmt, *this );
|
---|
[a08ba92] | 320 | }
|
---|
[c8ffe20b] | 321 |
|
---|
[a08ba92] | 322 | void Pass1::visit( EnumDecl *enumDecl ) {
|
---|
[0dd3a2f] | 323 | // Set the type of each member of the enumeration to be EnumConstant
|
---|
[de91427b] | 324 |
|
---|
[0dd3a2f] | 325 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
|
---|
[f6d7e0f] | 326 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
|
---|
[0dd3a2f] | 327 | assert( obj );
|
---|
[f6d7e0f] | 328 | // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
|
---|
| 329 | BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
|
---|
| 330 | obj->set_type( enumType ) ;
|
---|
[0dd3a2f] | 331 | } // for
|
---|
| 332 | Parent::visit( enumDecl );
|
---|
[a08ba92] | 333 | }
|
---|
[51b73452] | 334 |
|
---|
[a08ba92] | 335 | namespace {
|
---|
[0dd3a2f] | 336 | template< typename DWTIterator >
|
---|
| 337 | void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
|
---|
| 338 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
|
---|
| 339 | // entirely other fix ups are handled by the FixFunction class
|
---|
| 340 | if ( begin == end ) return;
|
---|
| 341 | FixFunction fixer;
|
---|
| 342 | DWTIterator i = begin;
|
---|
| 343 | *i = (*i )->acceptMutator( fixer );
|
---|
| 344 | if ( fixer.get_isVoid() ) {
|
---|
| 345 | DWTIterator j = i;
|
---|
| 346 | ++i;
|
---|
| 347 | func->get_parameters().erase( j );
|
---|
[de91427b] | 348 | if ( i != end ) {
|
---|
[0dd3a2f] | 349 | throw SemanticError( "invalid type void in function type ", func );
|
---|
| 350 | } // if
|
---|
| 351 | } else {
|
---|
| 352 | ++i;
|
---|
| 353 | for ( ; i != end; ++i ) {
|
---|
| 354 | FixFunction fixer;
|
---|
| 355 | *i = (*i )->acceptMutator( fixer );
|
---|
| 356 | if ( fixer.get_isVoid() ) {
|
---|
| 357 | throw SemanticError( "invalid type void in function type ", func );
|
---|
| 358 | } // if
|
---|
| 359 | } // for
|
---|
| 360 | } // if
|
---|
| 361 | }
|
---|
[a08ba92] | 362 | }
|
---|
[c8ffe20b] | 363 |
|
---|
[a08ba92] | 364 | void Pass1::visit( FunctionType *func ) {
|
---|
[0dd3a2f] | 365 | // Fix up parameters and return types
|
---|
| 366 | fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
|
---|
| 367 | fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
|
---|
| 368 | Visitor::visit( func );
|
---|
[a08ba92] | 369 | }
|
---|
[c8ffe20b] | 370 |
|
---|
[a08ba92] | 371 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
|
---|
[0dd3a2f] | 372 | if ( other_indexer ) {
|
---|
| 373 | indexer = other_indexer;
|
---|
| 374 | } else {
|
---|
| 375 | indexer = this;
|
---|
| 376 | } // if
|
---|
[a08ba92] | 377 | }
|
---|
[c8ffe20b] | 378 |
|
---|
[a08ba92] | 379 | void Pass2::visit( StructInstType *structInst ) {
|
---|
[0dd3a2f] | 380 | Parent::visit( structInst );
|
---|
| 381 | StructDecl *st = indexer->lookupStruct( structInst->get_name() );
|
---|
| 382 | // it's not a semantic error if the struct is not found, just an implicit forward declaration
|
---|
| 383 | if ( st ) {
|
---|
| 384 | assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
|
---|
| 385 | structInst->set_baseStruct( st );
|
---|
| 386 | } // if
|
---|
| 387 | if ( ! st || st->get_members().empty() ) {
|
---|
| 388 | // use of forward declaration
|
---|
| 389 | forwardStructs[ structInst->get_name() ].push_back( structInst );
|
---|
| 390 | } // if
|
---|
[a08ba92] | 391 | }
|
---|
[c8ffe20b] | 392 |
|
---|
[a08ba92] | 393 | void Pass2::visit( UnionInstType *unionInst ) {
|
---|
[0dd3a2f] | 394 | Parent::visit( unionInst );
|
---|
| 395 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
|
---|
| 396 | // it's not a semantic error if the union is not found, just an implicit forward declaration
|
---|
| 397 | if ( un ) {
|
---|
| 398 | unionInst->set_baseUnion( un );
|
---|
| 399 | } // if
|
---|
| 400 | if ( ! un || un->get_members().empty() ) {
|
---|
| 401 | // use of forward declaration
|
---|
| 402 | forwardUnions[ unionInst->get_name() ].push_back( unionInst );
|
---|
| 403 | } // if
|
---|
[a08ba92] | 404 | }
|
---|
[c8ffe20b] | 405 |
|
---|
[a08ba92] | 406 | void Pass2::visit( ContextInstType *contextInst ) {
|
---|
[0dd3a2f] | 407 | Parent::visit( contextInst );
|
---|
| 408 | ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
|
---|
| 409 | if ( ! ctx ) {
|
---|
| 410 | throw SemanticError( "use of undeclared context " + contextInst->get_name() );
|
---|
[17cd4eb] | 411 | } // if
|
---|
[0dd3a2f] | 412 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
|
---|
| 413 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
|
---|
| 414 | if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
|
---|
| 415 | cloneAll( otherCtx->get_members(), contextInst->get_members() );
|
---|
| 416 | } else {
|
---|
| 417 | contextInst->get_members().push_back( (*assert )->clone() );
|
---|
| 418 | } // if
|
---|
| 419 | } // for
|
---|
| 420 | } // for
|
---|
[51b986f] | 421 |
|
---|
| 422 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
|
---|
| 423 | throw SemanticError( "incorrect number of context parameters: ", contextInst );
|
---|
| 424 | } // if
|
---|
| 425 |
|
---|
[0dd3a2f] | 426 | 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] | 427 | }
|
---|
[c8ffe20b] | 428 |
|
---|
[a08ba92] | 429 | void Pass2::visit( StructDecl *structDecl ) {
|
---|
[0dd3a2f] | 430 | if ( ! structDecl->get_members().empty() ) {
|
---|
| 431 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
|
---|
| 432 | if ( fwds != forwardStructs.end() ) {
|
---|
| 433 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 434 | (*inst )->set_baseStruct( structDecl );
|
---|
| 435 | } // for
|
---|
| 436 | forwardStructs.erase( fwds );
|
---|
| 437 | } // if
|
---|
| 438 | } // if
|
---|
| 439 | Indexer::visit( structDecl );
|
---|
[a08ba92] | 440 | }
|
---|
[c8ffe20b] | 441 |
|
---|
[a08ba92] | 442 | void Pass2::visit( UnionDecl *unionDecl ) {
|
---|
[0dd3a2f] | 443 | if ( ! unionDecl->get_members().empty() ) {
|
---|
| 444 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
|
---|
| 445 | if ( fwds != forwardUnions.end() ) {
|
---|
| 446 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 447 | (*inst )->set_baseUnion( unionDecl );
|
---|
| 448 | } // for
|
---|
| 449 | forwardUnions.erase( fwds );
|
---|
| 450 | } // if
|
---|
| 451 | } // if
|
---|
| 452 | Indexer::visit( unionDecl );
|
---|
[a08ba92] | 453 | }
|
---|
[c8ffe20b] | 454 |
|
---|
[a08ba92] | 455 | void Pass2::visit( TypeInstType *typeInst ) {
|
---|
[0dd3a2f] | 456 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
|
---|
| 457 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
|
---|
| 458 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
|
---|
| 459 | } // if
|
---|
| 460 | } // if
|
---|
[a08ba92] | 461 | }
|
---|
[c8ffe20b] | 462 |
|
---|
[a08ba92] | 463 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
|
---|
[0dd3a2f] | 464 | if ( other_indexer ) {
|
---|
| 465 | indexer = other_indexer;
|
---|
| 466 | } else {
|
---|
| 467 | indexer = this;
|
---|
| 468 | } // if
|
---|
[a08ba92] | 469 | }
|
---|
[c8ffe20b] | 470 |
|
---|
[82dd287] | 471 | /// Fix up assertions
|
---|
[a08ba92] | 472 | void forallFixer( Type *func ) {
|
---|
[0dd3a2f] | 473 | for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
|
---|
| 474 | std::list< DeclarationWithType * > toBeDone, nextRound;
|
---|
| 475 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
|
---|
| 476 | while ( ! toBeDone.empty() ) {
|
---|
| 477 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
|
---|
| 478 | if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
|
---|
| 479 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
|
---|
| 480 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
|
---|
| 481 | assert( dwt );
|
---|
| 482 | nextRound.push_back( dwt->clone() );
|
---|
| 483 | }
|
---|
| 484 | delete ctx;
|
---|
| 485 | } else {
|
---|
| 486 | FixFunction fixer;
|
---|
| 487 | *assertion = (*assertion )->acceptMutator( fixer );
|
---|
| 488 | if ( fixer.get_isVoid() ) {
|
---|
| 489 | throw SemanticError( "invalid type void in assertion of function ", func );
|
---|
| 490 | }
|
---|
| 491 | (*type )->get_assertions().push_back( *assertion );
|
---|
| 492 | } // if
|
---|
| 493 | } // for
|
---|
| 494 | toBeDone.clear();
|
---|
| 495 | toBeDone.splice( toBeDone.end(), nextRound );
|
---|
| 496 | } // while
|
---|
| 497 | } // for
|
---|
[a08ba92] | 498 | }
|
---|
[c8ffe20b] | 499 |
|
---|
[a08ba92] | 500 | void Pass3::visit( ObjectDecl *object ) {
|
---|
[0dd3a2f] | 501 | forallFixer( object->get_type() );
|
---|
| 502 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
|
---|
| 503 | forallFixer( pointer->get_base() );
|
---|
| 504 | } // if
|
---|
| 505 | Parent::visit( object );
|
---|
| 506 | object->fixUniqueId();
|
---|
[a08ba92] | 507 | }
|
---|
[c8ffe20b] | 508 |
|
---|
[a08ba92] | 509 | void Pass3::visit( FunctionDecl *func ) {
|
---|
[0dd3a2f] | 510 | forallFixer( func->get_type() );
|
---|
| 511 | Parent::visit( func );
|
---|
| 512 | func->fixUniqueId();
|
---|
[a08ba92] | 513 | }
|
---|
[c8ffe20b] | 514 |
|
---|
[a08ba92] | 515 | static const std::list< std::string > noLabels;
|
---|
[c8ffe20b] | 516 |
|
---|
[f066321] | 517 | void AutogenerateRoutines::autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
|
---|
| 518 | AutogenerateRoutines visitor;
|
---|
[0dd3a2f] | 519 | acceptAndAdd( translationUnit, visitor, false );
|
---|
[a08ba92] | 520 | }
|
---|
[c8ffe20b] | 521 |
|
---|
[a08ba92] | 522 | template< typename OutputIterator >
|
---|
| 523 | void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {
|
---|
[0dd3a2f] | 524 | ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member );
|
---|
| 525 | // unnamed bit fields are not copied as they cannot be accessed
|
---|
| 526 | if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
|
---|
[c8ffe20b] | 527 |
|
---|
[0dd3a2f] | 528 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
[de91427b] | 529 |
|
---|
[0dd3a2f] | 530 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 531 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
|
---|
[de91427b] | 532 |
|
---|
[0dd3a2f] | 533 | // do something special for unnamed members
|
---|
| 534 | Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
|
---|
| 535 | assignExpr->get_args().push_back( dstselect );
|
---|
[de91427b] | 536 |
|
---|
[0dd3a2f] | 537 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
|
---|
| 538 | assignExpr->get_args().push_back( srcselect );
|
---|
[de91427b] | 539 |
|
---|
[0dd3a2f] | 540 | *out++ = new ExprStmt( noLabels, assignExpr );
|
---|
[a08ba92] | 541 | }
|
---|
[c8ffe20b] | 542 |
|
---|
[a08ba92] | 543 | template< typename OutputIterator >
|
---|
| 544 | void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {
|
---|
[0dd3a2f] | 545 | static UniqueName indexName( "_index" );
|
---|
[de91427b] | 546 |
|
---|
[0dd3a2f] | 547 | // for a flexible array member nothing is done -- user must define own assignment
|
---|
| 548 | if ( ! array->get_dimension() ) return;
|
---|
[de91427b] | 549 |
|
---|
[68cd1ce] | 550 | ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );
|
---|
[0dd3a2f] | 551 | *out++ = new DeclStmt( noLabels, index );
|
---|
[de91427b] | 552 |
|
---|
[0dd3a2f] | 553 | UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
| 554 | init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
|
---|
| 555 | init->get_args().push_back( new NameExpr( "0" ) );
|
---|
| 556 | Statement *initStmt = new ExprStmt( noLabels, init );
|
---|
[145f1fc] | 557 | std::list<Statement *> initList;
|
---|
| 558 | initList.push_back( initStmt );
|
---|
[de91427b] | 559 |
|
---|
[0dd3a2f] | 560 | UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );
|
---|
| 561 | cond->get_args().push_back( new VariableExpr( index ) );
|
---|
| 562 | cond->get_args().push_back( array->get_dimension()->clone() );
|
---|
[de91427b] | 563 |
|
---|
[0dd3a2f] | 564 | UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );
|
---|
| 565 | inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
|
---|
[de91427b] | 566 |
|
---|
[0dd3a2f] | 567 | UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
[de91427b] | 568 |
|
---|
[0dd3a2f] | 569 | UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 570 | derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
|
---|
[de91427b] | 571 |
|
---|
[0dd3a2f] | 572 | Expression *dstselect = new MemberExpr( member, derefExpr );
|
---|
| 573 | UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
|
---|
| 574 | dstIndex->get_args().push_back( dstselect );
|
---|
| 575 | dstIndex->get_args().push_back( new VariableExpr( index ) );
|
---|
| 576 | assignExpr->get_args().push_back( dstIndex );
|
---|
[de91427b] | 577 |
|
---|
[0dd3a2f] | 578 | Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
|
---|
| 579 | UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
|
---|
| 580 | srcIndex->get_args().push_back( srcselect );
|
---|
| 581 | srcIndex->get_args().push_back( new VariableExpr( index ) );
|
---|
| 582 | assignExpr->get_args().push_back( srcIndex );
|
---|
[de91427b] | 583 |
|
---|
[145f1fc] | 584 | *out++ = new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, assignExpr ) );
|
---|
[a08ba92] | 585 | }
|
---|
[c8ffe20b] | 586 |
|
---|
[de91427b] | 587 | //E ?=?(E volatile*, int),
|
---|
[f6d7e0f] | 588 | // ?=?(E _Atomic volatile*, int);
|
---|
| 589 | void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
|
---|
| 590 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
|
---|
[de91427b] | 591 |
|
---|
[f6d7e0f] | 592 | ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
|
---|
| 593 | assignType->get_returnVals().push_back( returnVal );
|
---|
| 594 |
|
---|
| 595 | // need two assignment operators with different types
|
---|
| 596 | FunctionType * assignType2 = assignType->clone();
|
---|
| 597 |
|
---|
| 598 | // E ?=?(E volatile *, E)
|
---|
| 599 | Type *etype = refType->clone();
|
---|
[8686f31] | 600 | // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false);
|
---|
[f6d7e0f] | 601 |
|
---|
| 602 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 );
|
---|
| 603 | assignType->get_parameters().push_back( dstParam );
|
---|
| 604 |
|
---|
| 605 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 );
|
---|
| 606 | assignType->get_parameters().push_back( srcParam );
|
---|
| 607 |
|
---|
| 608 | // E ?=?(E volatile *, int)
|
---|
| 609 | assignType2->get_parameters().push_back( dstParam->clone() );
|
---|
[de91427b] | 610 | BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt);
|
---|
[f6d7e0f] | 611 | ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 );
|
---|
| 612 | assignType2->get_parameters().push_back( srcParam2 );
|
---|
| 613 |
|
---|
| 614 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
|
---|
| 615 | // because each unit generates copies of the default routines for each aggregate.
|
---|
| 616 |
|
---|
| 617 | // since there is no definition, these should not be inline
|
---|
| 618 | // make these intrinsic so that the code generator does not make use of them
|
---|
| 619 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false );
|
---|
| 620 | assignDecl->fixUniqueId();
|
---|
| 621 | FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false );
|
---|
| 622 | assignDecl2->fixUniqueId();
|
---|
| 623 |
|
---|
[cc79d97] | 624 | // these should be built in the same way that the prelude
|
---|
| 625 | // functions are, so build a list containing the prototypes
|
---|
| 626 | // and allow MakeLibCfa to autogenerate the bodies.
|
---|
[f6d7e0f] | 627 | std::list< Declaration * > assigns;
|
---|
| 628 | assigns.push_back( assignDecl );
|
---|
| 629 | assigns.push_back( assignDecl2 );
|
---|
| 630 |
|
---|
| 631 | LibCfa::makeLibCfa( assigns );
|
---|
| 632 |
|
---|
[cc79d97] | 633 | // need to remove the prototypes, since this may be nested in a routine
|
---|
[8686f31] | 634 | for (int start = 0, end = assigns.size()/2; start < end; start++) {
|
---|
| 635 | delete assigns.front();
|
---|
| 636 | assigns.pop_front();
|
---|
[1db21619] | 637 | } // for
|
---|
[8686f31] | 638 |
|
---|
[f6d7e0f] | 639 | declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
|
---|
| 640 | }
|
---|
| 641 |
|
---|
[32d281d] | 642 | /// Clones a reference type, replacing any parameters it may have with a clone of the provided list
|
---|
| 643 | template< typename GenericInstType >
|
---|
| 644 | GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {
|
---|
| 645 | GenericInstType *clone = refType->clone();
|
---|
| 646 | clone->get_parameters().clear();
|
---|
| 647 | cloneAll( params, clone->get_parameters() );
|
---|
| 648 | return clone;
|
---|
| 649 | }
|
---|
[f6d7e0f] | 650 |
|
---|
[a08ba92] | 651 | Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
|
---|
[0dd3a2f] | 652 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
|
---|
[37a3b8f9] | 653 |
|
---|
| 654 | // Make function polymorphic in same parameters as generic struct, if applicable
|
---|
| 655 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
|
---|
[32d281d] | 656 | std::list< Expression* > structParams; // List of matching parameters to put on types
|
---|
[37a3b8f9] | 657 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
|
---|
[32d281d] | 658 | TypeDecl *typeParam = (*param)->clone();
|
---|
| 659 | assignType->get_forall().push_back( typeParam );
|
---|
| 660 | structParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
|
---|
[37a3b8f9] | 661 | }
|
---|
[de91427b] | 662 |
|
---|
[32d281d] | 663 | ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
|
---|
[0dd3a2f] | 664 | assignType->get_returnVals().push_back( returnVal );
|
---|
[de91427b] | 665 |
|
---|
[32d281d] | 666 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
|
---|
[0dd3a2f] | 667 | assignType->get_parameters().push_back( dstParam );
|
---|
[de91427b] | 668 |
|
---|
[32d281d] | 669 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
|
---|
[0dd3a2f] | 670 | assignType->get_parameters().push_back( srcParam );
|
---|
| 671 |
|
---|
| 672 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
|
---|
| 673 | // because each unit generates copies of the default routines for each aggregate.
|
---|
[de62360d] | 674 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
|
---|
[0dd3a2f] | 675 | assignDecl->fixUniqueId();
|
---|
[de91427b] | 676 |
|
---|
[0dd3a2f] | 677 | for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
|
---|
| 678 | if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
|
---|
[de91427b] | 679 | // query the type qualifiers of this field and skip assigning it if it is marked const.
|
---|
[367e082] | 680 | // If it is an array type, we need to strip off the array layers to find its qualifiers.
|
---|
| 681 | Type * type = dwt->get_type();
|
---|
| 682 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 683 | type = at->get_base();
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | if ( type->get_qualifiers().isConst ) {
|
---|
[53a2e97] | 687 | // don't assign const members
|
---|
| 688 | continue;
|
---|
| 689 | }
|
---|
| 690 |
|
---|
[0dd3a2f] | 691 | if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
|
---|
| 692 | makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
|
---|
| 693 | } else {
|
---|
| 694 | makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
|
---|
| 695 | } // if
|
---|
| 696 | } // if
|
---|
| 697 | } // for
|
---|
| 698 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
|
---|
[de91427b] | 699 |
|
---|
[0dd3a2f] | 700 | return assignDecl;
|
---|
[a08ba92] | 701 | }
|
---|
[c8ffe20b] | 702 |
|
---|
[a08ba92] | 703 | Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
|
---|
[0dd3a2f] | 704 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
|
---|
[32d281d] | 705 |
|
---|
| 706 | // Make function polymorphic in same parameters as generic union, if applicable
|
---|
| 707 | std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
|
---|
| 708 | std::list< Expression* > unionParams; // List of matching parameters to put on types
|
---|
| 709 | for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
|
---|
| 710 | TypeDecl *typeParam = (*param)->clone();
|
---|
| 711 | assignType->get_forall().push_back( typeParam );
|
---|
| 712 | unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
|
---|
| 713 | }
|
---|
[de91427b] | 714 |
|
---|
[32d281d] | 715 | ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
|
---|
[0dd3a2f] | 716 | assignType->get_returnVals().push_back( returnVal );
|
---|
[de91427b] | 717 |
|
---|
[32d281d] | 718 | ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
|
---|
[0dd3a2f] | 719 | assignType->get_parameters().push_back( dstParam );
|
---|
[de91427b] | 720 |
|
---|
[32d281d] | 721 | ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
|
---|
[0dd3a2f] | 722 | assignType->get_parameters().push_back( srcParam );
|
---|
[de91427b] | 723 |
|
---|
[0dd3a2f] | 724 | // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
|
---|
| 725 | // because each unit generates copies of the default routines for each aggregate.
|
---|
[de62360d] | 726 | FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
|
---|
[0dd3a2f] | 727 | assignDecl->fixUniqueId();
|
---|
[de91427b] | 728 |
|
---|
[0dd3a2f] | 729 | UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
|
---|
| 730 | copy->get_args().push_back( new VariableExpr( dstParam ) );
|
---|
| 731 | copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
|
---|
[32d281d] | 732 | copy->get_args().push_back( new SizeofExpr( cloneWithParams( refType, unionParams ) ) );
|
---|
[c8ffe20b] | 733 |
|
---|
[0dd3a2f] | 734 | assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
|
---|
| 735 | assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
|
---|
[de91427b] | 736 |
|
---|
[0dd3a2f] | 737 | return assignDecl;
|
---|
[a08ba92] | 738 | }
|
---|
[c8ffe20b] | 739 |
|
---|
[f066321] | 740 | void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
|
---|
[f6d7e0f] | 741 | if ( ! enumDecl->get_members().empty() ) {
|
---|
| 742 | EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
|
---|
| 743 | // enumInst->set_baseEnum( enumDecl );
|
---|
[de91427b] | 744 | // declsToAdd.push_back(
|
---|
[f6d7e0f] | 745 | makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );
|
---|
| 746 | }
|
---|
| 747 | }
|
---|
| 748 |
|
---|
[f066321] | 749 | void AutogenerateRoutines::visit( StructDecl *structDecl ) {
|
---|
[0dd3a2f] | 750 | if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
|
---|
[32d281d] | 751 | StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
|
---|
| 752 | structInst.set_baseStruct( structDecl );
|
---|
| 753 | declsToAdd.push_back( makeStructAssignment( structDecl, &structInst, functionNesting ) );
|
---|
[0dd3a2f] | 754 | structsDone.insert( structDecl->get_name() );
|
---|
| 755 | } // if
|
---|
[a08ba92] | 756 | }
|
---|
[c8ffe20b] | 757 |
|
---|
[f066321] | 758 | void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
|
---|
[0dd3a2f] | 759 | if ( ! unionDecl->get_members().empty() ) {
|
---|
[32d281d] | 760 | UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
|
---|
| 761 | unionInst.set_baseUnion( unionDecl );
|
---|
| 762 | declsToAdd.push_back( makeUnionAssignment( unionDecl, &unionInst, functionNesting ) );
|
---|
[0dd3a2f] | 763 | } // if
|
---|
[a08ba92] | 764 | }
|
---|
[c8ffe20b] | 765 |
|
---|
[f066321] | 766 | void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
|
---|
[0dd3a2f] | 767 | CompoundStmt *stmts = 0;
|
---|
| 768 | TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
|
---|
| 769 | typeInst->set_baseType( typeDecl );
|
---|
[68cd1ce] | 770 | ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
|
---|
| 771 | ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
|
---|
[0dd3a2f] | 772 | if ( typeDecl->get_base() ) {
|
---|
| 773 | stmts = new CompoundStmt( std::list< Label >() );
|
---|
| 774 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
| 775 | assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
|
---|
| 776 | assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
|
---|
| 777 | stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
|
---|
| 778 | } // if
|
---|
| 779 | FunctionType *type = new FunctionType( Type::Qualifiers(), false );
|
---|
[68cd1ce] | 780 | type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
|
---|
[0dd3a2f] | 781 | type->get_parameters().push_back( dst );
|
---|
| 782 | type->get_parameters().push_back( src );
|
---|
[de62360d] | 783 | FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
|
---|
[0dd3a2f] | 784 | declsToAdd.push_back( func );
|
---|
[a08ba92] | 785 | }
|
---|
[c8ffe20b] | 786 |
|
---|
[a08ba92] | 787 | void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
|
---|
[5f2f2d7] | 788 | for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
|
---|
| 789 | statements.insert( i, new DeclStmt( noLabels, *decl ) );
|
---|
| 790 | } // for
|
---|
| 791 | declsToAdd.clear();
|
---|
[a08ba92] | 792 | }
|
---|
[c8ffe20b] | 793 |
|
---|
[f066321] | 794 | void AutogenerateRoutines::visit( FunctionType *) {
|
---|
[0dd3a2f] | 795 | // ensure that we don't add assignment ops for types defined as part of the function
|
---|
[a08ba92] | 796 | }
|
---|
[c8ffe20b] | 797 |
|
---|
[f066321] | 798 | void AutogenerateRoutines::visit( PointerType *) {
|
---|
[0dd3a2f] | 799 | // ensure that we don't add assignment ops for types defined as part of the pointer
|
---|
[a08ba92] | 800 | }
|
---|
[c8ffe20b] | 801 |
|
---|
[f066321] | 802 | void AutogenerateRoutines::visit( ContextDecl *) {
|
---|
[0dd3a2f] | 803 | // ensure that we don't add assignment ops for types defined as part of the context
|
---|
[a08ba92] | 804 | }
|
---|
[c8ffe20b] | 805 |
|
---|
[a08ba92] | 806 | template< typename StmtClass >
|
---|
[f066321] | 807 | inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
|
---|
[0dd3a2f] | 808 | std::set< std::string > oldStructs = structsDone;
|
---|
| 809 | addVisit( stmt, *this );
|
---|
| 810 | structsDone = oldStructs;
|
---|
[a08ba92] | 811 | }
|
---|
[c8ffe20b] | 812 |
|
---|
[f066321] | 813 | void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
|
---|
[0dd3a2f] | 814 | maybeAccept( functionDecl->get_functionType(), *this );
|
---|
| 815 | acceptAll( functionDecl->get_oldDecls(), *this );
|
---|
| 816 | functionNesting += 1;
|
---|
| 817 | maybeAccept( functionDecl->get_statements(), *this );
|
---|
| 818 | functionNesting -= 1;
|
---|
[a08ba92] | 819 | }
|
---|
[3c70d38] | 820 |
|
---|
[f066321] | 821 | void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
|
---|
[0dd3a2f] | 822 | visitStatement( compoundStmt );
|
---|
[a08ba92] | 823 | }
|
---|
[c8ffe20b] | 824 |
|
---|
[f066321] | 825 | void AutogenerateRoutines::visit( IfStmt *ifStmt ) {
|
---|
[0dd3a2f] | 826 | visitStatement( ifStmt );
|
---|
[a08ba92] | 827 | }
|
---|
[c8ffe20b] | 828 |
|
---|
[f066321] | 829 | void AutogenerateRoutines::visit( WhileStmt *whileStmt ) {
|
---|
[0dd3a2f] | 830 | visitStatement( whileStmt );
|
---|
[a08ba92] | 831 | }
|
---|
[c8ffe20b] | 832 |
|
---|
[f066321] | 833 | void AutogenerateRoutines::visit( ForStmt *forStmt ) {
|
---|
[0dd3a2f] | 834 | visitStatement( forStmt );
|
---|
[a08ba92] | 835 | }
|
---|
[c8ffe20b] | 836 |
|
---|
[f066321] | 837 | void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
|
---|
[0dd3a2f] | 838 | visitStatement( switchStmt );
|
---|
[a08ba92] | 839 | }
|
---|
[c8ffe20b] | 840 |
|
---|
[f066321] | 841 | void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {
|
---|
[0dd3a2f] | 842 | visitStatement( switchStmt );
|
---|
[a08ba92] | 843 | }
|
---|
[c8ffe20b] | 844 |
|
---|
[f066321] | 845 | void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
|
---|
[0dd3a2f] | 846 | visitStatement( caseStmt );
|
---|
[a08ba92] | 847 | }
|
---|
[c8ffe20b] | 848 |
|
---|
[f066321] | 849 | void AutogenerateRoutines::visit( CatchStmt *cathStmt ) {
|
---|
[0dd3a2f] | 850 | visitStatement( cathStmt );
|
---|
[a08ba92] | 851 | }
|
---|
[c8ffe20b] | 852 |
|
---|
[de91427b] | 853 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
|
---|
| 854 | ReturnChecker checker;
|
---|
| 855 | acceptAll( translationUnit, checker );
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 | void ReturnChecker::visit( FunctionDecl * functionDecl ) {
|
---|
| 859 | std::list< DeclarationWithType * > oldReturnVals = returnVals;
|
---|
| 860 | returnVals = functionDecl->get_functionType()->get_returnVals();
|
---|
| 861 | Visitor::visit( functionDecl );
|
---|
| 862 | returnVals = oldReturnVals;
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | void ReturnChecker::visit( ReturnStmt * returnStmt ) {
|
---|
| 866 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
|
---|
| 867 | throw SemanticError( "Non-void function returns no values: " , returnStmt );
|
---|
| 868 | } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) {
|
---|
| 869 | throw SemanticError( "void function returns values: " , returnStmt );
|
---|
| 870 | }
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 |
|
---|
[a08ba92] | 874 | bool isTypedef( Declaration *decl ) {
|
---|
[0dd3a2f] | 875 | return dynamic_cast< TypedefDecl * >( decl );
|
---|
[a08ba92] | 876 | }
|
---|
[c8ffe20b] | 877 |
|
---|
[a08ba92] | 878 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
|
---|
[0dd3a2f] | 879 | EliminateTypedef eliminator;
|
---|
| 880 | mutateAll( translationUnit, eliminator );
|
---|
| 881 | filter( translationUnit, isTypedef, true );
|
---|
[a08ba92] | 882 | }
|
---|
[c8ffe20b] | 883 |
|
---|
[85c4ef0] | 884 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
|
---|
[de91427b] | 885 | // instances of typedef types will come here. If it is an instance
|
---|
[cc79d97] | 886 | // of a typdef type, link the instance to its actual type.
|
---|
| 887 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
|
---|
[0dd3a2f] | 888 | if ( def != typedefNames.end() ) {
|
---|
[cc79d97] | 889 | Type *ret = def->second.first->get_base()->clone();
|
---|
[0dd3a2f] | 890 | ret->get_qualifiers() += typeInst->get_qualifiers();
|
---|
[0215a76f] | 891 | // place instance parameters on the typedef'd type
|
---|
| 892 | if ( ! typeInst->get_parameters().empty() ) {
|
---|
| 893 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
|
---|
| 894 | if ( ! rtt ) {
|
---|
| 895 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
|
---|
| 896 | }
|
---|
| 897 | rtt->get_parameters().clear();
|
---|
| 898 | cloneAll(typeInst->get_parameters(), rtt->get_parameters());
|
---|
[1db21619] | 899 | } // if
|
---|
[0dd3a2f] | 900 | delete typeInst;
|
---|
| 901 | return ret;
|
---|
| 902 | } // if
|
---|
| 903 | return typeInst;
|
---|
[a08ba92] | 904 | }
|
---|
[c8ffe20b] | 905 |
|
---|
[85c4ef0] | 906 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
|
---|
[0dd3a2f] | 907 | Declaration *ret = Mutator::mutate( tyDecl );
|
---|
[cc79d97] | 908 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
|
---|
[de91427b] | 909 | // typedef to the same name from the same scope
|
---|
[cc79d97] | 910 | // must be from the same type
|
---|
| 911 |
|
---|
| 912 | Type * t1 = tyDecl->get_base();
|
---|
| 913 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
|
---|
[1cbca6e] | 914 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
|
---|
[cc79d97] | 915 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
|
---|
[85c4ef0] | 916 | }
|
---|
[cc79d97] | 917 | } else {
|
---|
| 918 | typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
|
---|
| 919 | } // if
|
---|
| 920 |
|
---|
[0dd3a2f] | 921 | // When a typedef is a forward declaration:
|
---|
| 922 | // typedef struct screen SCREEN;
|
---|
| 923 | // the declaration portion must be retained:
|
---|
| 924 | // struct screen;
|
---|
| 925 | // because the expansion of the typedef is:
|
---|
| 926 | // void rtn( SCREEN *p ) => void rtn( struct screen *p )
|
---|
| 927 | // hence the type-name "screen" must be defined.
|
---|
| 928 | // Note, qualifiers on the typedef are superfluous for the forward declaration.
|
---|
| 929 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
|
---|
| 930 | return new StructDecl( aggDecl->get_name() );
|
---|
| 931 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
|
---|
| 932 | return new UnionDecl( aggDecl->get_name() );
|
---|
| 933 | } else {
|
---|
| 934 | return ret;
|
---|
| 935 | } // if
|
---|
[a08ba92] | 936 | }
|
---|
[c8ffe20b] | 937 |
|
---|
[85c4ef0] | 938 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
|
---|
[cc79d97] | 939 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
|
---|
[0dd3a2f] | 940 | if ( i != typedefNames.end() ) {
|
---|
| 941 | typedefNames.erase( i ) ;
|
---|
| 942 | } // if
|
---|
| 943 | return typeDecl;
|
---|
[a08ba92] | 944 | }
|
---|
[c8ffe20b] | 945 |
|
---|
[85c4ef0] | 946 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
|
---|
[cc79d97] | 947 | TypedefMap oldNames = typedefNames;
|
---|
[0dd3a2f] | 948 | DeclarationWithType *ret = Mutator::mutate( funcDecl );
|
---|
| 949 | typedefNames = oldNames;
|
---|
| 950 | return ret;
|
---|
[a08ba92] | 951 | }
|
---|
[c8ffe20b] | 952 |
|
---|
[1db21619] | 953 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
|
---|
[cc79d97] | 954 | TypedefMap oldNames = typedefNames;
|
---|
[1db21619] | 955 | DeclarationWithType *ret = Mutator::mutate( objDecl );
|
---|
[ae4c85a] | 956 | typedefNames = oldNames;
|
---|
[02e5ab6] | 957 | // is the type a function?
|
---|
[1db21619] | 958 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
|
---|
[02e5ab6] | 959 | // replace the current object declaration with a function declaration
|
---|
[1db21619] | 960 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
|
---|
| 961 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
|
---|
| 962 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
|
---|
| 963 | } // if
|
---|
[0dd3a2f] | 964 | return ret;
|
---|
[a08ba92] | 965 | }
|
---|
[c8ffe20b] | 966 |
|
---|
[85c4ef0] | 967 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
|
---|
[cc79d97] | 968 | TypedefMap oldNames = typedefNames;
|
---|
[0dd3a2f] | 969 | Expression *ret = Mutator::mutate( castExpr );
|
---|
| 970 | typedefNames = oldNames;
|
---|
| 971 | return ret;
|
---|
[a08ba92] | 972 | }
|
---|
[c8ffe20b] | 973 |
|
---|
[85c4ef0] | 974 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
|
---|
[cc79d97] | 975 | TypedefMap oldNames = typedefNames;
|
---|
| 976 | scopeLevel += 1;
|
---|
[0dd3a2f] | 977 | CompoundStmt *ret = Mutator::mutate( compoundStmt );
|
---|
[cc79d97] | 978 | scopeLevel -= 1;
|
---|
[0dd3a2f] | 979 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
|
---|
| 980 | while ( i != compoundStmt->get_kids().end() ) {
|
---|
[85c4ef0] | 981 | std::list< Statement * >::iterator next = i+1;
|
---|
[0dd3a2f] | 982 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
|
---|
| 983 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
|
---|
| 984 | delete *i;
|
---|
| 985 | compoundStmt->get_kids().erase( i );
|
---|
| 986 | } // if
|
---|
| 987 | } // if
|
---|
| 988 | i = next;
|
---|
| 989 | } // while
|
---|
| 990 | typedefNames = oldNames;
|
---|
| 991 | return ret;
|
---|
[a08ba92] | 992 | }
|
---|
[85c4ef0] | 993 |
|
---|
| 994 | // there may be typedefs nested within aggregates
|
---|
| 995 | // in order for everything to work properly, these
|
---|
| 996 | // should be removed as well
|
---|
| 997 | template<typename AggDecl>
|
---|
| 998 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
|
---|
| 999 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
|
---|
| 1000 | for ( ; it != aggDecl->get_members().end(); ) {
|
---|
| 1001 | std::list< Declaration * >::iterator next = it+1;
|
---|
| 1002 | if ( dynamic_cast< TypedefDecl * >( *it ) ) {
|
---|
| 1003 | delete *it;
|
---|
| 1004 | aggDecl->get_members().erase( it );
|
---|
| 1005 | } // if
|
---|
| 1006 | it = next;
|
---|
| 1007 | }
|
---|
| 1008 | return aggDecl;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
|
---|
| 1012 | Mutator::mutate( structDecl );
|
---|
| 1013 | return handleAggregate( structDecl );
|
---|
| 1014 | }
|
---|
| 1015 |
|
---|
| 1016 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
|
---|
| 1017 | Mutator::mutate( unionDecl );
|
---|
| 1018 | return handleAggregate( unionDecl );
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
| 1021 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
|
---|
| 1022 | Mutator::mutate( enumDecl );
|
---|
| 1023 | return handleAggregate( enumDecl );
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
|
---|
| 1027 | Mutator::mutate( contextDecl );
|
---|
| 1028 | return handleAggregate( contextDecl );
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
[51b73452] | 1031 | } // namespace SymTab
|
---|
[0dd3a2f] | 1032 |
|
---|
| 1033 | // Local Variables: //
|
---|
| 1034 | // tab-width: 4 //
|
---|
| 1035 | // mode: c++ //
|
---|
| 1036 | // compile-command: "make install" //
|
---|
| 1037 | // End: //
|
---|