source: src/SymTab/Validate.cc@ 4cc4286

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 4cc4286 was 620cb95, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

separate Autogen from Validate, call default ctor/dtors on array elements

  • Property mode set to 100644
File size: 24.2 KB
RevLine 
[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//
[9cb8e88d]7// Validate.cc --
[0dd3a2f]8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 21:50:04 2015
[f066321]11// Last Modified By : Rob Schluntz
[7528ba1]12// Last Modified On : Mon Feb 22 12:26:37 2016
13// Update Count : 297
[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"
[d3b7937]51#include "Common/utility.h"
52#include "Common/UniqueName.h"
[51b73452]53#include "AddVisit.h"
[f6d7e0f]54#include "MakeLibCfa.h"
[cc79d97]55#include "TypeEquality.h"
[620cb95]56#include "Autogen.h"
[1cbca6e]57#include "ResolvExpr/typeops.h"
[51b73452]58
[c8ffe20b]59#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
[51b73452]60
61namespace SymTab {
[a08ba92]62 class HoistStruct : public Visitor {
63 public:
[82dd287]64 /// Flattens nested struct types
[0dd3a2f]65 static void hoistStruct( std::list< Declaration * > &translationUnit );
[9cb8e88d]66
[0dd3a2f]67 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
[9cb8e88d]68
[0dd3a2f]69 virtual void visit( StructDecl *aggregateDecl );
70 virtual void visit( UnionDecl *aggregateDecl );
[c8ffe20b]71
[0dd3a2f]72 virtual void visit( CompoundStmt *compoundStmt );
73 virtual void visit( IfStmt *ifStmt );
74 virtual void visit( WhileStmt *whileStmt );
75 virtual void visit( ForStmt *forStmt );
76 virtual void visit( SwitchStmt *switchStmt );
77 virtual void visit( ChooseStmt *chooseStmt );
78 virtual void visit( CaseStmt *caseStmt );
79 virtual void visit( CatchStmt *catchStmt );
[a08ba92]80 private:
[0dd3a2f]81 HoistStruct();
[c8ffe20b]82
[0dd3a2f]83 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
[c8ffe20b]84
[0dd3a2f]85 std::list< Declaration * > declsToAdd;
86 bool inStruct;
[a08ba92]87 };
[c8ffe20b]88
[de91427b]89 /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
[a08ba92]90 class Pass1 : public Visitor {
[0dd3a2f]91 typedef Visitor Parent;
92 virtual void visit( EnumDecl *aggregateDecl );
93 virtual void visit( FunctionType *func );
[a08ba92]94 };
[82dd287]95
96 /// Associates forward declarations of aggregates with their definitions
[a08ba92]97 class Pass2 : public Indexer {
[0dd3a2f]98 typedef Indexer Parent;
[a08ba92]99 public:
[0dd3a2f]100 Pass2( bool doDebug, const Indexer *indexer );
[a08ba92]101 private:
[0dd3a2f]102 virtual void visit( StructInstType *structInst );
103 virtual void visit( UnionInstType *unionInst );
104 virtual void visit( ContextInstType *contextInst );
105 virtual void visit( StructDecl *structDecl );
106 virtual void visit( UnionDecl *unionDecl );
107 virtual void visit( TypeInstType *typeInst );
108
109 const Indexer *indexer;
[9cb8e88d]110
[0dd3a2f]111 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
112 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
113 ForwardStructsType forwardStructs;
114 ForwardUnionsType forwardUnions;
[a08ba92]115 };
[c8ffe20b]116
[82dd287]117 /// Replaces array and function types in forall lists by appropriate pointer type
[a08ba92]118 class Pass3 : public Indexer {
[0dd3a2f]119 typedef Indexer Parent;
[a08ba92]120 public:
[0dd3a2f]121 Pass3( const Indexer *indexer );
[a08ba92]122 private:
[0dd3a2f]123 virtual void visit( ObjectDecl *object );
124 virtual void visit( FunctionDecl *func );
[c8ffe20b]125
[0dd3a2f]126 const Indexer *indexer;
[a08ba92]127 };
[c8ffe20b]128
[de91427b]129 class ReturnChecker : public Visitor {
130 public:
131 /// Checks that return statements return nothing if their return type is void
132 /// and return something if the return type is non-void.
133 static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
134
135 private:
136 virtual void visit( FunctionDecl * functionDecl );
137
138 virtual void visit( ReturnStmt * returnStmt );
139
140 std::list< DeclarationWithType * > returnVals;
141 };
142
[a08ba92]143 class EliminateTypedef : public Mutator {
144 public:
[de91427b]145 EliminateTypedef() : scopeLevel( 0 ) {}
146 /// Replaces typedefs by forward declarations
[0dd3a2f]147 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
[a08ba92]148 private:
[0dd3a2f]149 virtual Declaration *mutate( TypedefDecl *typeDecl );
150 virtual TypeDecl *mutate( TypeDecl *typeDecl );
151 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
[1db21619]152 virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
[0dd3a2f]153 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
154 virtual Type *mutate( TypeInstType *aggregateUseType );
155 virtual Expression *mutate( CastExpr *castExpr );
[cc79d97]156
[85c4ef0]157 virtual Declaration *mutate( StructDecl * structDecl );
158 virtual Declaration *mutate( UnionDecl * unionDecl );
159 virtual Declaration *mutate( EnumDecl * enumDecl );
160 virtual Declaration *mutate( ContextDecl * contextDecl );
161
162 template<typename AggDecl>
163 AggDecl *handleAggregate( AggDecl * aggDecl );
164
[cc79d97]165 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
166 TypedefMap typedefNames;
167 int scopeLevel;
[a08ba92]168 };
[c8ffe20b]169
[9cb8e88d]170 class VerifyCtorDtor : public Visitor {
171 public:
172 /// ensure that constructors and destructors have at least one
173 /// parameter, the first of which must be a pointer, and no
174 /// return values.
175 static void verify( std::list< Declaration * > &translationUnit );
176
177 // VerifyCtorDtor() {}
178
179 virtual void visit( FunctionDecl *funcDecl );
180 private:
181 };
182
[a08ba92]183 void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
[0dd3a2f]184 Pass1 pass1;
185 Pass2 pass2( doDebug, 0 );
186 Pass3 pass3( 0 );
187 EliminateTypedef::eliminateTypedef( translationUnit );
188 HoistStruct::hoistStruct( translationUnit );
189 acceptAll( translationUnit, pass1 );
190 acceptAll( translationUnit, pass2 );
[de91427b]191 ReturnChecker::checkFunctionReturns( translationUnit );
[620cb95]192 autogenerateRoutines( translationUnit );
[0dd3a2f]193 acceptAll( translationUnit, pass3 );
[9cb8e88d]194 VerifyCtorDtor::verify( translationUnit );
[a08ba92]195 }
[9cb8e88d]196
[a08ba92]197 void validateType( Type *type, const Indexer *indexer ) {
[0dd3a2f]198 Pass1 pass1;
199 Pass2 pass2( false, indexer );
200 Pass3 pass3( indexer );
201 type->accept( pass1 );
202 type->accept( pass2 );
203 type->accept( pass3 );
[a08ba92]204 }
[c8ffe20b]205
[a08ba92]206 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
[0dd3a2f]207 HoistStruct hoister;
208 acceptAndAdd( translationUnit, hoister, true );
[a08ba92]209 }
[c8ffe20b]210
[a08ba92]211 HoistStruct::HoistStruct() : inStruct( false ) {
212 }
[c8ffe20b]213
[a08ba92]214 void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
[0dd3a2f]215 std::list< Declaration * >::iterator i = declList.begin();
216 while ( i != declList.end() ) {
217 std::list< Declaration * >::iterator next = i;
218 ++next;
219 if ( pred( *i ) ) {
220 if ( doDelete ) {
221 delete *i;
222 } // if
223 declList.erase( i );
224 } // if
225 i = next;
226 } // while
[a08ba92]227 }
[c8ffe20b]228
[a08ba92]229 bool isStructOrUnion( Declaration *decl ) {
[0dd3a2f]230 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
[a08ba92]231 }
[51b73452]232
[a08ba92]233 template< typename AggDecl >
234 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
[0dd3a2f]235 if ( inStruct ) {
236 // Add elements in stack order corresponding to nesting structure.
237 declsToAdd.push_front( aggregateDecl );
238 Visitor::visit( aggregateDecl );
239 } else {
240 inStruct = true;
241 Visitor::visit( aggregateDecl );
242 inStruct = false;
243 } // if
244 // Always remove the hoisted aggregate from the inner structure.
245 filter( aggregateDecl->get_members(), isStructOrUnion, false );
[a08ba92]246 }
[c8ffe20b]247
[a08ba92]248 void HoistStruct::visit( StructDecl *aggregateDecl ) {
[0dd3a2f]249 handleAggregate( aggregateDecl );
[a08ba92]250 }
[c8ffe20b]251
[a08ba92]252 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
[0dd3a2f]253 handleAggregate( aggregateDecl );
[a08ba92]254 }
[c8ffe20b]255
[a08ba92]256 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
[0dd3a2f]257 addVisit( compoundStmt, *this );
[a08ba92]258 }
[c8ffe20b]259
[a08ba92]260 void HoistStruct::visit( IfStmt *ifStmt ) {
[0dd3a2f]261 addVisit( ifStmt, *this );
[a08ba92]262 }
[c8ffe20b]263
[a08ba92]264 void HoistStruct::visit( WhileStmt *whileStmt ) {
[0dd3a2f]265 addVisit( whileStmt, *this );
[a08ba92]266 }
[c8ffe20b]267
[a08ba92]268 void HoistStruct::visit( ForStmt *forStmt ) {
[0dd3a2f]269 addVisit( forStmt, *this );
[a08ba92]270 }
[c8ffe20b]271
[a08ba92]272 void HoistStruct::visit( SwitchStmt *switchStmt ) {
[0dd3a2f]273 addVisit( switchStmt, *this );
[a08ba92]274 }
[c8ffe20b]275
[a08ba92]276 void HoistStruct::visit( ChooseStmt *switchStmt ) {
[0dd3a2f]277 addVisit( switchStmt, *this );
[a08ba92]278 }
[c8ffe20b]279
[a08ba92]280 void HoistStruct::visit( CaseStmt *caseStmt ) {
[0dd3a2f]281 addVisit( caseStmt, *this );
[a08ba92]282 }
[c8ffe20b]283
[a08ba92]284 void HoistStruct::visit( CatchStmt *cathStmt ) {
[0dd3a2f]285 addVisit( cathStmt, *this );
[a08ba92]286 }
[c8ffe20b]287
[a08ba92]288 void Pass1::visit( EnumDecl *enumDecl ) {
[0dd3a2f]289 // Set the type of each member of the enumeration to be EnumConstant
[9cb8e88d]290
[0dd3a2f]291 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
[f6d7e0f]292 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
[0dd3a2f]293 assert( obj );
[f6d7e0f]294 // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
295 BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
296 obj->set_type( enumType ) ;
[0dd3a2f]297 } // for
298 Parent::visit( enumDecl );
[a08ba92]299 }
[51b73452]300
[a08ba92]301 namespace {
[0dd3a2f]302 template< typename DWTIterator >
303 void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
304 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
305 // entirely other fix ups are handled by the FixFunction class
306 if ( begin == end ) return;
307 FixFunction fixer;
308 DWTIterator i = begin;
309 *i = (*i )->acceptMutator( fixer );
310 if ( fixer.get_isVoid() ) {
311 DWTIterator j = i;
312 ++i;
313 func->get_parameters().erase( j );
[9cb8e88d]314 if ( i != end ) {
[0dd3a2f]315 throw SemanticError( "invalid type void in function type ", func );
316 } // if
317 } else {
318 ++i;
319 for ( ; i != end; ++i ) {
320 FixFunction fixer;
321 *i = (*i )->acceptMutator( fixer );
322 if ( fixer.get_isVoid() ) {
323 throw SemanticError( "invalid type void in function type ", func );
324 } // if
325 } // for
326 } // if
327 }
[a08ba92]328 }
[c8ffe20b]329
[a08ba92]330 void Pass1::visit( FunctionType *func ) {
[0dd3a2f]331 // Fix up parameters and return types
332 fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
333 fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
334 Visitor::visit( func );
[a08ba92]335 }
[c8ffe20b]336
[a08ba92]337 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
[0dd3a2f]338 if ( other_indexer ) {
339 indexer = other_indexer;
340 } else {
341 indexer = this;
342 } // if
[a08ba92]343 }
[c8ffe20b]344
[a08ba92]345 void Pass2::visit( StructInstType *structInst ) {
[0dd3a2f]346 Parent::visit( structInst );
347 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
348 // it's not a semantic error if the struct is not found, just an implicit forward declaration
349 if ( st ) {
[98735ef]350 //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
[0dd3a2f]351 structInst->set_baseStruct( st );
352 } // if
353 if ( ! st || st->get_members().empty() ) {
354 // use of forward declaration
355 forwardStructs[ structInst->get_name() ].push_back( structInst );
356 } // if
[a08ba92]357 }
[c8ffe20b]358
[a08ba92]359 void Pass2::visit( UnionInstType *unionInst ) {
[0dd3a2f]360 Parent::visit( unionInst );
361 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
362 // it's not a semantic error if the union is not found, just an implicit forward declaration
363 if ( un ) {
364 unionInst->set_baseUnion( un );
365 } // if
366 if ( ! un || un->get_members().empty() ) {
367 // use of forward declaration
368 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
369 } // if
[a08ba92]370 }
[c8ffe20b]371
[a08ba92]372 void Pass2::visit( ContextInstType *contextInst ) {
[0dd3a2f]373 Parent::visit( contextInst );
374 ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
375 if ( ! ctx ) {
376 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
[17cd4eb]377 } // if
[0dd3a2f]378 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
379 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
380 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
381 cloneAll( otherCtx->get_members(), contextInst->get_members() );
382 } else {
383 contextInst->get_members().push_back( (*assert )->clone() );
384 } // if
385 } // for
386 } // for
[51b986f]387
388 if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
389 throw SemanticError( "incorrect number of context parameters: ", contextInst );
390 } // if
391
[0dd3a2f]392 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]393 }
[c8ffe20b]394
[a08ba92]395 void Pass2::visit( StructDecl *structDecl ) {
[0dd3a2f]396 if ( ! structDecl->get_members().empty() ) {
397 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
398 if ( fwds != forwardStructs.end() ) {
399 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
400 (*inst )->set_baseStruct( structDecl );
401 } // for
402 forwardStructs.erase( fwds );
403 } // if
404 } // if
405 Indexer::visit( structDecl );
[a08ba92]406 }
[c8ffe20b]407
[a08ba92]408 void Pass2::visit( UnionDecl *unionDecl ) {
[0dd3a2f]409 if ( ! unionDecl->get_members().empty() ) {
410 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
411 if ( fwds != forwardUnions.end() ) {
412 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
413 (*inst )->set_baseUnion( unionDecl );
414 } // for
415 forwardUnions.erase( fwds );
416 } // if
417 } // if
418 Indexer::visit( unionDecl );
[a08ba92]419 }
[c8ffe20b]420
[a08ba92]421 void Pass2::visit( TypeInstType *typeInst ) {
[0dd3a2f]422 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
423 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
424 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
425 } // if
426 } // if
[a08ba92]427 }
[c8ffe20b]428
[a08ba92]429 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
[0dd3a2f]430 if ( other_indexer ) {
431 indexer = other_indexer;
432 } else {
433 indexer = this;
434 } // if
[a08ba92]435 }
[c8ffe20b]436
[82dd287]437 /// Fix up assertions
[a08ba92]438 void forallFixer( Type *func ) {
[0dd3a2f]439 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
440 std::list< DeclarationWithType * > toBeDone, nextRound;
441 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
442 while ( ! toBeDone.empty() ) {
443 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
444 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
445 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
446 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
447 assert( dwt );
448 nextRound.push_back( dwt->clone() );
449 }
450 delete ctx;
451 } else {
452 FixFunction fixer;
453 *assertion = (*assertion )->acceptMutator( fixer );
454 if ( fixer.get_isVoid() ) {
455 throw SemanticError( "invalid type void in assertion of function ", func );
456 }
457 (*type )->get_assertions().push_back( *assertion );
458 } // if
459 } // for
460 toBeDone.clear();
461 toBeDone.splice( toBeDone.end(), nextRound );
462 } // while
463 } // for
[a08ba92]464 }
[c8ffe20b]465
[a08ba92]466 void Pass3::visit( ObjectDecl *object ) {
[0dd3a2f]467 forallFixer( object->get_type() );
468 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
469 forallFixer( pointer->get_base() );
470 } // if
471 Parent::visit( object );
472 object->fixUniqueId();
[a08ba92]473 }
[c8ffe20b]474
[a08ba92]475 void Pass3::visit( FunctionDecl *func ) {
[0dd3a2f]476 forallFixer( func->get_type() );
477 Parent::visit( func );
478 func->fixUniqueId();
[a08ba92]479 }
[c8ffe20b]480
[de91427b]481 void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
482 ReturnChecker checker;
483 acceptAll( translationUnit, checker );
484 }
485
486 void ReturnChecker::visit( FunctionDecl * functionDecl ) {
487 std::list< DeclarationWithType * > oldReturnVals = returnVals;
488 returnVals = functionDecl->get_functionType()->get_returnVals();
489 Visitor::visit( functionDecl );
490 returnVals = oldReturnVals;
491 }
492
493 void ReturnChecker::visit( ReturnStmt * returnStmt ) {
494 if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
495 throw SemanticError( "Non-void function returns no values: " , returnStmt );
496 } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) {
497 throw SemanticError( "void function returns values: " , returnStmt );
498 }
499 }
500
501
[a08ba92]502 bool isTypedef( Declaration *decl ) {
[0dd3a2f]503 return dynamic_cast< TypedefDecl * >( decl );
[a08ba92]504 }
[c8ffe20b]505
[a08ba92]506 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
[0dd3a2f]507 EliminateTypedef eliminator;
508 mutateAll( translationUnit, eliminator );
509 filter( translationUnit, isTypedef, true );
[a08ba92]510 }
[c8ffe20b]511
[85c4ef0]512 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
[9cb8e88d]513 // instances of typedef types will come here. If it is an instance
[cc79d97]514 // of a typdef type, link the instance to its actual type.
515 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
[0dd3a2f]516 if ( def != typedefNames.end() ) {
[cc79d97]517 Type *ret = def->second.first->get_base()->clone();
[0dd3a2f]518 ret->get_qualifiers() += typeInst->get_qualifiers();
[0215a76f]519 // place instance parameters on the typedef'd type
520 if ( ! typeInst->get_parameters().empty() ) {
521 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
522 if ( ! rtt ) {
523 throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
524 }
525 rtt->get_parameters().clear();
526 cloneAll(typeInst->get_parameters(), rtt->get_parameters());
[1db21619]527 } // if
[0dd3a2f]528 delete typeInst;
529 return ret;
530 } // if
531 return typeInst;
[a08ba92]532 }
[c8ffe20b]533
[85c4ef0]534 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
[0dd3a2f]535 Declaration *ret = Mutator::mutate( tyDecl );
[cc79d97]536 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
[9cb8e88d]537 // typedef to the same name from the same scope
[cc79d97]538 // must be from the same type
539
540 Type * t1 = tyDecl->get_base();
541 Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
[1cbca6e]542 if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
[cc79d97]543 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
[85c4ef0]544 }
[cc79d97]545 } else {
546 typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
547 } // if
548
[0dd3a2f]549 // When a typedef is a forward declaration:
550 // typedef struct screen SCREEN;
551 // the declaration portion must be retained:
552 // struct screen;
553 // because the expansion of the typedef is:
554 // void rtn( SCREEN *p ) => void rtn( struct screen *p )
555 // hence the type-name "screen" must be defined.
556 // Note, qualifiers on the typedef are superfluous for the forward declaration.
557 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
558 return new StructDecl( aggDecl->get_name() );
559 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
560 return new UnionDecl( aggDecl->get_name() );
561 } else {
562 return ret;
563 } // if
[a08ba92]564 }
[c8ffe20b]565
[85c4ef0]566 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
[cc79d97]567 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
[0dd3a2f]568 if ( i != typedefNames.end() ) {
569 typedefNames.erase( i ) ;
570 } // if
571 return typeDecl;
[a08ba92]572 }
[c8ffe20b]573
[85c4ef0]574 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
[cc79d97]575 TypedefMap oldNames = typedefNames;
[0dd3a2f]576 DeclarationWithType *ret = Mutator::mutate( funcDecl );
577 typedefNames = oldNames;
578 return ret;
[a08ba92]579 }
[c8ffe20b]580
[1db21619]581 DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
[cc79d97]582 TypedefMap oldNames = typedefNames;
[1db21619]583 DeclarationWithType *ret = Mutator::mutate( objDecl );
[ae4c85a]584 typedefNames = oldNames;
[02e5ab6]585 // is the type a function?
[1db21619]586 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
[02e5ab6]587 // replace the current object declaration with a function declaration
[1db21619]588 return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
589 } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
590 throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
591 } // if
[0dd3a2f]592 return ret;
[a08ba92]593 }
[c8ffe20b]594
[85c4ef0]595 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
[cc79d97]596 TypedefMap oldNames = typedefNames;
[0dd3a2f]597 Expression *ret = Mutator::mutate( castExpr );
598 typedefNames = oldNames;
599 return ret;
[a08ba92]600 }
[c8ffe20b]601
[85c4ef0]602 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
[cc79d97]603 TypedefMap oldNames = typedefNames;
604 scopeLevel += 1;
[0dd3a2f]605 CompoundStmt *ret = Mutator::mutate( compoundStmt );
[cc79d97]606 scopeLevel -= 1;
[0dd3a2f]607 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
608 while ( i != compoundStmt->get_kids().end() ) {
[85c4ef0]609 std::list< Statement * >::iterator next = i+1;
[0dd3a2f]610 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
611 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
612 delete *i;
613 compoundStmt->get_kids().erase( i );
614 } // if
615 } // if
616 i = next;
617 } // while
618 typedefNames = oldNames;
619 return ret;
[a08ba92]620 }
[85c4ef0]621
622 // there may be typedefs nested within aggregates
623 // in order for everything to work properly, these
624 // should be removed as well
625 template<typename AggDecl>
626 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
627 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
628 for ( ; it != aggDecl->get_members().end(); ) {
629 std::list< Declaration * >::iterator next = it+1;
630 if ( dynamic_cast< TypedefDecl * >( *it ) ) {
631 delete *it;
632 aggDecl->get_members().erase( it );
633 } // if
634 it = next;
635 }
636 return aggDecl;
637 }
638
639 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
640 Mutator::mutate( structDecl );
641 return handleAggregate( structDecl );
642 }
643
644 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
645 Mutator::mutate( unionDecl );
646 return handleAggregate( unionDecl );
647 }
648
649 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
650 Mutator::mutate( enumDecl );
651 return handleAggregate( enumDecl );
652 }
653
654 Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
655 Mutator::mutate( contextDecl );
656 return handleAggregate( contextDecl );
657 }
658
[9cb8e88d]659 void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) {
660 VerifyCtorDtor verifier;
661 acceptAll( translationUnit, verifier );
662 }
663
664 void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) {
665 FunctionType * funcType = funcDecl->get_functionType();
666 std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
667 std::list< DeclarationWithType * > &params = funcType->get_parameters();
668
669 if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) {
670 if ( params.size() == 0 ) {
671 throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl );
672 }
673 if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) {
674 throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl );
675 }
676 if ( returnVals.size() != 0 ) {
677 throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
678 }
679 }
680
681 Visitor::visit( funcDecl );
682 // original idea: modify signature of ctor/dtors and insert appropriate return statements
683 // to cause desired behaviour
684 // new idea: add comma exprs to every ctor call to produce first parameter.
685 // this requires some memoization of the first parameter, because it can be a
686 // complicated expression with side effects (see: malloc). idea: add temporary variable
687 // that is assigned address of constructed object in ctor argument position and
688 // return the temporary. It should also be done after all implicit ctors are
689 // added, so not in this pass!
690 }
[51b73452]691} // namespace SymTab
[0dd3a2f]692
693// Local Variables: //
694// tab-width: 4 //
695// mode: c++ //
696// compile-command: "make install" //
697// End: //
Note: See TracBrowser for help on using the repository browser.