source: src/SymTab/Validate.cc@ 3da470c

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 string with_gc
Last change on this file since 3da470c was 630a82a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

C99 compound literals now work, comment rational code, clean up hoisting AddVisit

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