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