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