source: src/SymTab/Validate.cc@ a2a8d2a6

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

implement warnings for missing struct member constructor calls, remove bad clones

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