source: src/SymTab/Validate.cc@ d88f256a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors 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 with_gc
Last change on this file since d88f256a was d1969a6, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

check that assignment routines have a reasonable signature, add noreturn attribute to assert_fail_f to silence warnings

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