source: translator/SymTab/Validate.cc@ d0e8cfe4

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 d0e8cfe4 was 1ead581, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

remove configure files

  • Property mode set to 100644
File size: 27.7 KB
Line 
1/*
2 The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
3 as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is
4 complete, including:
5
6 - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
7 union.
8
9 - All enumeration constants have type EnumInstType.
10
11 - The type "void" never occurs in lists of function parameter or return types; neither do tuple types. A function
12 taking no arguments has no argument types, and tuples are flattened.
13
14 - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
15 by the particular set of type arguments.
16
17 - Every declaration is assigned a unique id.
18
19 - No typedef declarations or instances exist; the actual type is substituted for each instance.
20
21 - Each type, struct, and union definition is followed by an appropriate assignment operator.
22
23 - Each use of a struct or union is connected to a complete definition of that struct or union, even if that definition
24 occurs later in the input.
25*/
26
27#include <list>
28#include <iterator>
29#include "Validate.h"
30#include "SynTree/Visitor.h"
31#include "SynTree/Mutator.h"
32#include "SynTree/Type.h"
33#include "SynTree/Statement.h"
34#include "Indexer.h"
35#include "SynTree/TypeSubstitution.h"
36#include "FixFunction.h"
37#include "ImplementationType.h"
38#include "utility.h"
39#include "UniqueName.h"
40#include "AddVisit.h"
41
42
43#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
44
45namespace SymTab {
46 class HoistStruct : public Visitor {
47 public:
48 static void hoistStruct( std::list< Declaration * > &translationUnit );
49
50 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
51
52 virtual void visit( StructDecl *aggregateDecl );
53 virtual void visit( UnionDecl *aggregateDecl );
54
55 virtual void visit( CompoundStmt *compoundStmt );
56 virtual void visit( IfStmt *ifStmt );
57 virtual void visit( WhileStmt *whileStmt );
58 virtual void visit( ForStmt *forStmt );
59 virtual void visit( SwitchStmt *switchStmt );
60 virtual void visit( ChooseStmt *chooseStmt );
61 virtual void visit( CaseStmt *caseStmt );
62 virtual void visit( CatchStmt *catchStmt );
63 private:
64 HoistStruct();
65
66 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
67
68 std::list< Declaration * > declsToAdd;
69 bool inStruct;
70 };
71
72 class Pass1 : public Visitor {
73 typedef Visitor Parent;
74 virtual void visit( EnumDecl *aggregateDecl );
75 virtual void visit( FunctionType *func );
76 };
77
78 class Pass2 : public Indexer {
79 typedef Indexer Parent;
80 public:
81 Pass2( bool doDebug, const Indexer *indexer );
82 private:
83 virtual void visit( StructInstType *structInst );
84 virtual void visit( UnionInstType *unionInst );
85 virtual void visit( ContextInstType *contextInst );
86 virtual void visit( StructDecl *structDecl );
87 virtual void visit( UnionDecl *unionDecl );
88 virtual void visit( TypeInstType *typeInst );
89
90 const Indexer *indexer;
91
92 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
93 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
94 ForwardStructsType forwardStructs;
95 ForwardUnionsType forwardUnions;
96 };
97
98 class Pass3 : public Indexer {
99 typedef Indexer Parent;
100 public:
101 Pass3( const Indexer *indexer );
102 private:
103 virtual void visit( ObjectDecl *object );
104 virtual void visit( FunctionDecl *func );
105
106 const Indexer *indexer;
107 };
108
109 class AddStructAssignment : public Visitor {
110 public:
111 static void addStructAssignment( std::list< Declaration * > &translationUnit );
112
113 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
114
115 virtual void visit( StructDecl *structDecl );
116 virtual void visit( UnionDecl *structDecl );
117 virtual void visit( TypeDecl *typeDecl );
118 virtual void visit( ContextDecl *ctxDecl );
119
120 virtual void visit( FunctionType *ftype );
121 virtual void visit( PointerType *ftype );
122
123 virtual void visit( CompoundStmt *compoundStmt );
124 virtual void visit( IfStmt *ifStmt );
125 virtual void visit( WhileStmt *whileStmt );
126 virtual void visit( ForStmt *forStmt );
127 virtual void visit( SwitchStmt *switchStmt );
128 virtual void visit( ChooseStmt *chooseStmt );
129 virtual void visit( CaseStmt *caseStmt );
130 virtual void visit( CatchStmt *catchStmt );
131 private:
132 template< typename StmtClass > void visitStatement( StmtClass *stmt );
133
134 std::list< Declaration * > declsToAdd;
135 std::set< std::string > structsDone;
136 };
137
138 class EliminateTypedef : public Mutator {
139 public:
140 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
141 private:
142 virtual Declaration *mutate( TypedefDecl *typeDecl );
143 virtual TypeDecl *mutate( TypeDecl *typeDecl );
144 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
145 virtual ObjectDecl *mutate( ObjectDecl *objDecl );
146 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
147 virtual Type *mutate( TypeInstType *aggregateUseType );
148 virtual Expression *mutate( CastExpr *castExpr );
149
150 std::map< std::string, TypedefDecl * > typedefNames;
151 };
152
153 void validate( std::list< Declaration * > &translationUnit, bool doDebug, const Indexer *indexer ) {
154 Pass1 pass1;
155 Pass2 pass2( doDebug, indexer );
156 Pass3 pass3( indexer );
157 EliminateTypedef::eliminateTypedef( translationUnit );
158 HoistStruct::hoistStruct( translationUnit );
159 acceptAll( translationUnit, pass1 );
160 acceptAll( translationUnit, pass2 );
161 AddStructAssignment::addStructAssignment( translationUnit );
162 acceptAll( translationUnit, pass3 );
163 }
164
165 void validateType( Type *type, const Indexer *indexer ) {
166 Pass1 pass1;
167 Pass2 pass2( false, indexer );
168 Pass3 pass3( indexer );
169 type->accept( pass1 );
170 type->accept( pass2 );
171 type->accept( pass3 );
172 }
173
174 template< typename Visitor >
175 void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) {
176 std::list< Declaration * >::iterator i = translationUnit.begin();
177 while ( i != translationUnit.end() ) {
178 (*i)->accept( visitor );
179 std::list< Declaration * >::iterator next = i;
180 next++;
181 if ( ! visitor.get_declsToAdd().empty() ) {
182 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );
183 }
184 i = next;
185 }
186 }
187
188 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
189 HoistStruct hoister;
190 acceptAndAdd( translationUnit, hoister, true );
191 }
192
193 HoistStruct::HoistStruct() : inStruct( false ) {
194 }
195
196 void filter( std::list< Declaration * > &declList, bool (*pred )( Declaration * ), bool doDelete ) {
197 std::list< Declaration * >::iterator i = declList.begin();
198 while ( i != declList.end() ) {
199 std::list< Declaration * >::iterator next = i;
200 ++next;
201 if ( pred( *i ) ) {
202 if ( doDelete ) {
203 delete *i;
204 }
205 declList.erase( i );
206 }
207 i = next;
208 }
209 }
210
211 bool
212 isStructOrUnion( Declaration *decl ) {
213 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
214 }
215
216 template< typename AggDecl >
217 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
218 if ( inStruct ) {
219 declsToAdd.push_back( aggregateDecl );
220 Visitor::visit( aggregateDecl );
221 } else {
222 inStruct = true;
223 Visitor::visit( aggregateDecl );
224 inStruct = false;
225 filter( aggregateDecl->get_members(), isStructOrUnion, false );
226 }
227 }
228
229 void HoistStruct::visit( StructDecl *aggregateDecl ) {
230 handleAggregate( aggregateDecl );
231 }
232
233 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
234 handleAggregate( aggregateDecl );
235 }
236
237 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
238 addVisit( compoundStmt, *this );
239 }
240
241 void HoistStruct::visit( IfStmt *ifStmt ) {
242 addVisit( ifStmt, *this );
243 }
244
245 void HoistStruct::visit( WhileStmt *whileStmt ) {
246 addVisit( whileStmt, *this );
247 }
248
249 void HoistStruct::visit( ForStmt *forStmt ) {
250 addVisit( forStmt, *this );
251 }
252
253 void HoistStruct::visit( SwitchStmt *switchStmt ) {
254 addVisit( switchStmt, *this );
255 }
256
257 void HoistStruct::visit( ChooseStmt *switchStmt ) {
258 addVisit( switchStmt, *this );
259 }
260
261 void HoistStruct::visit( CaseStmt *caseStmt ) {
262 addVisit( caseStmt, *this );
263 }
264
265 void HoistStruct::visit( CatchStmt *cathStmt ) {
266 addVisit( cathStmt, *this );
267 }
268
269 void Pass1::visit( EnumDecl *enumDecl ) {
270 // Set the type of each member of the enumeration to be EnumConstant
271
272 for( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
273 ObjectDecl *obj = dynamic_cast< ObjectDecl * >( *i );
274 assert( obj );
275 obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false ), enumDecl->get_name() ) );
276 }
277 Parent::visit( enumDecl );
278 }
279
280 namespace {
281 template< typename DWTIterator >
282 void
283 fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
284 // the only case in which "void" is valid is where it is the only one in the list; then
285 // it should be removed entirely
286 // other fix ups are handled by the FixFunction class
287 if ( begin == end ) return;
288 FixFunction fixer;
289 DWTIterator i = begin;
290 *i = (*i )->acceptMutator( fixer );
291 if ( fixer.get_isVoid() ) {
292 DWTIterator j = i;
293 ++i;
294 func->get_parameters().erase( j );
295 if ( i != end ) {
296 throw SemanticError( "invalid type void in function type ", func );
297 }
298 } else {
299 ++i;
300 for( ; i != end; ++i ) {
301 FixFunction fixer;
302 *i = (*i )->acceptMutator( fixer );
303 if ( fixer.get_isVoid() ) {
304 throw SemanticError( "invalid type void in function type ", func );
305 }
306 }
307 }
308 }
309 }
310
311 void Pass1::visit( FunctionType *func ) {
312 // Fix up parameters and return types
313 fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
314 fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
315 Visitor::visit( func );
316 }
317
318 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
319 if ( other_indexer ) {
320 indexer = other_indexer;
321 } else {
322 indexer = this;
323 }
324 }
325
326 void Pass2::visit( StructInstType *structInst ) {
327 Parent::visit( structInst );
328 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
329 // it's not a semantic error if the struct is not found, just an implicit forward declaration
330 if ( st ) {
331 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
332 structInst->set_baseStruct( st );
333 }
334 if ( ! st || st->get_members().empty() ) {
335 // use of forward declaration
336 forwardStructs[ structInst->get_name() ].push_back( structInst );
337 }
338 }
339
340 void Pass2::visit( UnionInstType *unionInst ) {
341 Parent::visit( unionInst );
342 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
343 // it's not a semantic error if the union is not found, just an implicit forward declaration
344 if ( un ) {
345 unionInst->set_baseUnion( un );
346 }
347 if ( ! un || un->get_members().empty() ) {
348 // use of forward declaration
349 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
350 }
351 }
352
353 void Pass2::visit( ContextInstType *contextInst ) {
354 Parent::visit( contextInst );
355 ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
356 if ( ! ctx ) {
357 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
358 }
359 for( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
360 for( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
361 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
362 cloneAll( otherCtx->get_members(), contextInst->get_members() );
363 } else {
364 contextInst->get_members().push_back( (*assert )->clone() );
365 }
366 }
367 }
368 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() ) );
369 }
370
371 void Pass2::visit( StructDecl *structDecl ) {
372 if ( ! structDecl->get_members().empty() ) {
373 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
374 if ( fwds != forwardStructs.end() ) {
375 for( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
376 (*inst )->set_baseStruct( structDecl );
377 }
378 forwardStructs.erase( fwds );
379 }
380 }
381 Indexer::visit( structDecl );
382 }
383
384 void Pass2::visit( UnionDecl *unionDecl ) {
385 if ( ! unionDecl->get_members().empty() ) {
386 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
387 if ( fwds != forwardUnions.end() ) {
388 for( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
389 (*inst )->set_baseUnion( unionDecl );
390 }
391 forwardUnions.erase( fwds );
392 }
393 }
394 Indexer::visit( unionDecl );
395 }
396
397 void Pass2::visit( TypeInstType *typeInst ) {
398 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
399 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
400 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
401 }
402 }
403 }
404
405 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
406 if ( other_indexer ) {
407 indexer = other_indexer;
408 } else {
409 indexer = this;
410 }
411 }
412
413 void forallFixer( Type *func ) {
414 // Fix up assertions
415 for( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
416 std::list< DeclarationWithType * > toBeDone, nextRound;
417 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
418 while ( ! toBeDone.empty() ) {
419 for( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
420 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
421 for( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
422 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
423 assert( dwt );
424 nextRound.push_back( dwt->clone() );
425 }
426 delete ctx;
427 } else {
428 FixFunction fixer;
429 *assertion = (*assertion )->acceptMutator( fixer );
430 if ( fixer.get_isVoid() ) {
431 throw SemanticError( "invalid type void in assertion of function ", func );
432 }
433 (*type )->get_assertions().push_back( *assertion );
434 }
435 }
436 toBeDone.clear();
437 toBeDone.splice( toBeDone.end(), nextRound );
438 }
439 }
440 }
441
442 void Pass3::visit( ObjectDecl *object ) {
443 forallFixer( object->get_type() );
444 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
445 forallFixer( pointer->get_base() );
446 }
447 Parent::visit( object );
448 object->fixUniqueId();
449 }
450
451 void Pass3::visit( FunctionDecl *func ) {
452 forallFixer( func->get_type() );
453 Parent::visit( func );
454 func->fixUniqueId();
455 }
456
457 static const std::list< std::string > noLabels;
458
459 void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) {
460 AddStructAssignment visitor;
461 acceptAndAdd( translationUnit, visitor, false );
462 }
463
464 template< typename OutputIterator >
465 void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {
466 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member ); // PAB: unnamed bit fields are not copied
467 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
468
469 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
470
471 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
472 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
473
474 // do something special for unnamed members
475 Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
476 assignExpr->get_args().push_back( dstselect );
477
478 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
479 assignExpr->get_args().push_back( srcselect );
480
481 *out++ = new ExprStmt( noLabels, assignExpr );
482 }
483
484 template< typename OutputIterator >
485 void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {
486 static UniqueName indexName( "_index" );
487
488 // for a flexible array member nothing is done -- user must define own assignment
489 if ( ! array->get_dimension() ) return;
490
491 ObjectDecl *index = new ObjectDecl( indexName.newName(), Declaration::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );
492 *out++ = new DeclStmt( noLabels, index );
493
494 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
495 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
496 init->get_args().push_back( new NameExpr( "0" ) );
497 Statement *initStmt = new ExprStmt( noLabels, init );
498
499 UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );
500 cond->get_args().push_back( new VariableExpr( index ) );
501 cond->get_args().push_back( array->get_dimension()->clone() );
502
503 UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );
504 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
505
506 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
507
508 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
509 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
510
511 Expression *dstselect = new MemberExpr( member, derefExpr );
512 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
513 dstIndex->get_args().push_back( dstselect );
514 dstIndex->get_args().push_back( new VariableExpr( index ) );
515 assignExpr->get_args().push_back( dstIndex );
516
517 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
518 UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
519 srcIndex->get_args().push_back( srcselect );
520 srcIndex->get_args().push_back( new VariableExpr( index ) );
521 assignExpr->get_args().push_back( srcIndex );
522
523 *out++ = new ForStmt( noLabels, initStmt, cond, inc, new ExprStmt( noLabels, assignExpr ) );
524 }
525
526 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType ) {
527 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
528
529 ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
530 assignType->get_returnVals().push_back( returnVal );
531
532 ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
533 assignType->get_parameters().push_back( dstParam );
534
535 ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
536 assignType->get_parameters().push_back( srcParam );
537
538 FunctionDecl *assignDecl = new FunctionDecl( "?=?", Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true );
539 assignDecl->fixUniqueId();
540
541 for( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
542 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
543 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
544 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
545 } else {
546 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
547 }
548 }
549 }
550 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
551
552 return assignDecl;
553 }
554
555 Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType ) {
556 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
557
558 ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
559 assignType->get_returnVals().push_back( returnVal );
560
561 ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
562 assignType->get_parameters().push_back( dstParam );
563
564 ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
565 assignType->get_parameters().push_back( srcParam );
566
567 FunctionDecl *assignDecl = new FunctionDecl( "?=?", Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true );
568 assignDecl->fixUniqueId();
569
570 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
571 copy->get_args().push_back( new VariableExpr( dstParam ) );
572 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
573 copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
574
575 assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
576 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
577
578 return assignDecl;
579 }
580
581 void AddStructAssignment::visit( StructDecl *structDecl ) {
582 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
583 StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
584 structInst->set_baseStruct( structDecl );
585 declsToAdd.push_back( makeStructAssignment( structDecl, structInst ) );
586 structsDone.insert( structDecl->get_name() );
587 }
588 }
589
590 void AddStructAssignment::visit( UnionDecl *unionDecl ) {
591 if ( ! unionDecl->get_members().empty() ) {
592 UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
593 unionInst->set_baseUnion( unionDecl );
594 declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst ) );
595 }
596 }
597
598 void AddStructAssignment::visit( TypeDecl *typeDecl ) {
599 CompoundStmt *stmts = 0;
600 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
601 typeInst->set_baseType( typeDecl );
602 ObjectDecl *src = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
603 ObjectDecl *dst = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
604 if ( typeDecl->get_base() ) {
605 stmts = new CompoundStmt( std::list< Label >() );
606 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
607 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
608 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
609 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
610 }
611 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
612 type->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
613 type->get_parameters().push_back( dst );
614 type->get_parameters().push_back( src );
615 FunctionDecl *func = new FunctionDecl( "?=?", Declaration::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false );
616 declsToAdd.push_back( func );
617 }
618
619 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
620 if ( ! declsToAdd.empty() ) {
621 for( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
622 statements.insert( i, new DeclStmt( noLabels, *decl ) );
623 }
624 declsToAdd.clear();
625 }
626 }
627
628 void AddStructAssignment::visit( FunctionType *) {
629 // ensure that we don't add assignment ops for types defined
630 // as part of the function
631 }
632
633 void AddStructAssignment::visit( PointerType *) {
634 // ensure that we don't add assignment ops for types defined
635 // as part of the pointer
636 }
637
638 void AddStructAssignment::visit( ContextDecl *) {
639 // ensure that we don't add assignment ops for types defined
640 // as part of the context
641 }
642
643 template< typename StmtClass >
644 inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
645 std::set< std::string > oldStructs = structsDone;
646 addVisit( stmt, *this );
647 structsDone = oldStructs;
648 }
649
650 void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
651 visitStatement( compoundStmt );
652 }
653
654 void AddStructAssignment::visit( IfStmt *ifStmt ) {
655 visitStatement( ifStmt );
656 }
657
658 void AddStructAssignment::visit( WhileStmt *whileStmt ) {
659 visitStatement( whileStmt );
660 }
661
662 void AddStructAssignment::visit( ForStmt *forStmt ) {
663 visitStatement( forStmt );
664 }
665
666 void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
667 visitStatement( switchStmt );
668 }
669
670 void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
671 visitStatement( switchStmt );
672 }
673
674 void AddStructAssignment::visit( CaseStmt *caseStmt ) {
675 visitStatement( caseStmt );
676 }
677
678 void AddStructAssignment::visit( CatchStmt *cathStmt ) {
679 visitStatement( cathStmt );
680 }
681
682 bool isTypedef( Declaration *decl ) {
683 return dynamic_cast< TypedefDecl * >( decl );
684 }
685
686 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
687 EliminateTypedef eliminator;
688 mutateAll( translationUnit, eliminator );
689 filter( translationUnit, isTypedef, true );
690 }
691
692 Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
693 std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() );
694 if ( def != typedefNames.end() ) {
695 Type *ret = def->second->get_base()->clone();
696 ret->get_qualifiers() += typeInst->get_qualifiers();
697 delete typeInst;
698 return ret;
699 }
700 return typeInst;
701 }
702
703 Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
704 Declaration *ret = Mutator::mutate( tyDecl );
705 typedefNames[ tyDecl->get_name() ] = tyDecl;
706 if ( AggregateDecl *aggDecl = dynamic_cast< AggregateDecl * >( tyDecl->get_base() ) ) {
707 tyDecl->set_base( 0 );
708 delete tyDecl;
709 return aggDecl;
710 } else {
711 return ret;
712 }
713 }
714
715 TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
716 std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );
717 if ( i != typedefNames.end() ) {
718 typedefNames.erase( i ) ;
719 }
720 return typeDecl;
721 }
722
723 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
724 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
725 DeclarationWithType *ret = Mutator::mutate( funcDecl );
726 typedefNames = oldNames;
727 return ret;
728 }
729
730 ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
731 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
732 ObjectDecl *ret = Mutator::mutate( objDecl );
733 typedefNames = oldNames;
734 return ret;
735 }
736
737 Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
738 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
739 Expression *ret = Mutator::mutate( castExpr );
740 typedefNames = oldNames;
741 return ret;
742 }
743
744 CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
745 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
746 CompoundStmt *ret = Mutator::mutate( compoundStmt );
747 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
748 while ( i != compoundStmt->get_kids().end() ) {
749 std::list< Statement * >::iterator next = i;
750 ++next;
751 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
752 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
753 delete *i;
754 compoundStmt->get_kids().erase( i );
755 }
756 }
757 i = next;
758 }
759 typedefNames = oldNames;
760 return ret;
761 }
762} // namespace SymTab
Note: See TracBrowser for help on using the repository browser.