source: translator/SymTab/Validate.cc@ d3d8c3f

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 stuck-waitfor-destruct with_gc
Last change on this file since d3d8c3f was d3d8c3f, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

add ChangeLog NEWS

  • 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 isStructOrUnion( Declaration *decl ) {
212 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
213 }
214
215 template< typename AggDecl >
216 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
217 if ( inStruct ) {
218 declsToAdd.push_back( aggregateDecl );
219 Visitor::visit( aggregateDecl );
220 } else {
221 inStruct = true;
222 Visitor::visit( aggregateDecl );
223 inStruct = false;
224 filter( aggregateDecl->get_members(), isStructOrUnion, false );
225 }
226 }
227
228 void HoistStruct::visit( StructDecl *aggregateDecl ) {
229 handleAggregate( aggregateDecl );
230 }
231
232 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
233 handleAggregate( aggregateDecl );
234 }
235
236 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
237 addVisit( compoundStmt, *this );
238 }
239
240 void HoistStruct::visit( IfStmt *ifStmt ) {
241 addVisit( ifStmt, *this );
242 }
243
244 void HoistStruct::visit( WhileStmt *whileStmt ) {
245 addVisit( whileStmt, *this );
246 }
247
248 void HoistStruct::visit( ForStmt *forStmt ) {
249 addVisit( forStmt, *this );
250 }
251
252 void HoistStruct::visit( SwitchStmt *switchStmt ) {
253 addVisit( switchStmt, *this );
254 }
255
256 void HoistStruct::visit( ChooseStmt *switchStmt ) {
257 addVisit( switchStmt, *this );
258 }
259
260 void HoistStruct::visit( CaseStmt *caseStmt ) {
261 addVisit( caseStmt, *this );
262 }
263
264 void HoistStruct::visit( CatchStmt *cathStmt ) {
265 addVisit( cathStmt, *this );
266 }
267
268 void Pass1::visit( EnumDecl *enumDecl ) {
269 // Set the type of each member of the enumeration to be EnumConstant
270
271 for( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
272 ObjectDecl *obj = dynamic_cast< ObjectDecl * >( *i );
273 assert( obj );
274 obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false ), enumDecl->get_name() ) );
275 }
276 Parent::visit( enumDecl );
277 }
278
279 namespace {
280 template< typename DWTIterator >
281 void
282 fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
283 // the only case in which "void" is valid is where it is the only one in the list; then
284 // it should be removed entirely
285 // other fix ups are handled by the FixFunction class
286 if ( begin == end ) return;
287 FixFunction fixer;
288 DWTIterator i = begin;
289 *i = (*i )->acceptMutator( fixer );
290 if ( fixer.get_isVoid() ) {
291 DWTIterator j = i;
292 ++i;
293 func->get_parameters().erase( j );
294 if ( i != end ) {
295 throw SemanticError( "invalid type void in function type ", func );
296 }
297 } else {
298 ++i;
299 for( ; i != end; ++i ) {
300 FixFunction fixer;
301 *i = (*i )->acceptMutator( fixer );
302 if ( fixer.get_isVoid() ) {
303 throw SemanticError( "invalid type void in function type ", func );
304 }
305 }
306 }
307 }
308 }
309
310 void Pass1::visit( FunctionType *func ) {
311 // Fix up parameters and return types
312 fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
313 fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
314 Visitor::visit( func );
315 }
316
317 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
318 if ( other_indexer ) {
319 indexer = other_indexer;
320 } else {
321 indexer = this;
322 }
323 }
324
325 void Pass2::visit( StructInstType *structInst ) {
326 Parent::visit( structInst );
327 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
328 // it's not a semantic error if the struct is not found, just an implicit forward declaration
329 if ( st ) {
330 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
331 structInst->set_baseStruct( st );
332 }
333 if ( ! st || st->get_members().empty() ) {
334 // use of forward declaration
335 forwardStructs[ structInst->get_name() ].push_back( structInst );
336 }
337 }
338
339 void Pass2::visit( UnionInstType *unionInst ) {
340 Parent::visit( unionInst );
341 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
342 // it's not a semantic error if the union is not found, just an implicit forward declaration
343 if ( un ) {
344 unionInst->set_baseUnion( un );
345 }
346 if ( ! un || un->get_members().empty() ) {
347 // use of forward declaration
348 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
349 }
350 }
351
352 void Pass2::visit( ContextInstType *contextInst ) {
353 Parent::visit( contextInst );
354 ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
355 if ( ! ctx ) {
356 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
357 }
358 for( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
359 for( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
360 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
361 cloneAll( otherCtx->get_members(), contextInst->get_members() );
362 } else {
363 contextInst->get_members().push_back( (*assert )->clone() );
364 }
365 }
366 }
367 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() ) );
368 }
369
370 void Pass2::visit( StructDecl *structDecl ) {
371 if ( ! structDecl->get_members().empty() ) {
372 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
373 if ( fwds != forwardStructs.end() ) {
374 for( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
375 (*inst )->set_baseStruct( structDecl );
376 }
377 forwardStructs.erase( fwds );
378 }
379 }
380 Indexer::visit( structDecl );
381 }
382
383 void Pass2::visit( UnionDecl *unionDecl ) {
384 if ( ! unionDecl->get_members().empty() ) {
385 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
386 if ( fwds != forwardUnions.end() ) {
387 for( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
388 (*inst )->set_baseUnion( unionDecl );
389 }
390 forwardUnions.erase( fwds );
391 }
392 }
393 Indexer::visit( unionDecl );
394 }
395
396 void Pass2::visit( TypeInstType *typeInst ) {
397 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
398 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
399 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
400 }
401 }
402 }
403
404 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
405 if ( other_indexer ) {
406 indexer = other_indexer;
407 } else {
408 indexer = this;
409 }
410 }
411
412 void forallFixer( Type *func ) {
413 // Fix up assertions
414 for( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
415 std::list< DeclarationWithType * > toBeDone, nextRound;
416 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
417 while ( ! toBeDone.empty() ) {
418 for( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
419 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
420 for( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
421 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
422 assert( dwt );
423 nextRound.push_back( dwt->clone() );
424 }
425 delete ctx;
426 } else {
427 FixFunction fixer;
428 *assertion = (*assertion )->acceptMutator( fixer );
429 if ( fixer.get_isVoid() ) {
430 throw SemanticError( "invalid type void in assertion of function ", func );
431 }
432 (*type )->get_assertions().push_back( *assertion );
433 }
434 }
435 toBeDone.clear();
436 toBeDone.splice( toBeDone.end(), nextRound );
437 }
438 }
439 }
440
441 void Pass3::visit( ObjectDecl *object ) {
442 forallFixer( object->get_type() );
443 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
444 forallFixer( pointer->get_base() );
445 }
446 Parent::visit( object );
447 object->fixUniqueId();
448 }
449
450 void Pass3::visit( FunctionDecl *func ) {
451 forallFixer( func->get_type() );
452 Parent::visit( func );
453 func->fixUniqueId();
454 }
455
456 static const std::list< std::string > noLabels;
457
458 void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) {
459 AddStructAssignment visitor;
460 acceptAndAdd( translationUnit, visitor, false );
461 }
462
463 template< typename OutputIterator >
464 void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {
465 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member ); // PAB: unnamed bit fields are not copied
466 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
467
468 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
469
470 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
471 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
472
473 // do something special for unnamed members
474 Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
475 assignExpr->get_args().push_back( dstselect );
476
477 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
478 assignExpr->get_args().push_back( srcselect );
479
480 *out++ = new ExprStmt( noLabels, assignExpr );
481 }
482
483 template< typename OutputIterator >
484 void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {
485 static UniqueName indexName( "_index" );
486
487 // for a flexible array member nothing is done -- user must define own assignment
488 if ( ! array->get_dimension() ) return;
489
490 ObjectDecl *index = new ObjectDecl( indexName.newName(), Declaration::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );
491 *out++ = new DeclStmt( noLabels, index );
492
493 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
494 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
495 init->get_args().push_back( new NameExpr( "0" ) );
496 Statement *initStmt = new ExprStmt( noLabels, init );
497
498 UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );
499 cond->get_args().push_back( new VariableExpr( index ) );
500 cond->get_args().push_back( array->get_dimension()->clone() );
501
502 UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );
503 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
504
505 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
506
507 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
508 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
509
510 Expression *dstselect = new MemberExpr( member, derefExpr );
511 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
512 dstIndex->get_args().push_back( dstselect );
513 dstIndex->get_args().push_back( new VariableExpr( index ) );
514 assignExpr->get_args().push_back( dstIndex );
515
516 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
517 UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
518 srcIndex->get_args().push_back( srcselect );
519 srcIndex->get_args().push_back( new VariableExpr( index ) );
520 assignExpr->get_args().push_back( srcIndex );
521
522 *out++ = new ForStmt( noLabels, initStmt, cond, inc, new ExprStmt( noLabels, assignExpr ) );
523 }
524
525 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType ) {
526 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
527
528 ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
529 assignType->get_returnVals().push_back( returnVal );
530
531 ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
532 assignType->get_parameters().push_back( dstParam );
533
534 ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
535 assignType->get_parameters().push_back( srcParam );
536
537 FunctionDecl *assignDecl = new FunctionDecl( "?=?", Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true );
538 assignDecl->fixUniqueId();
539
540 for( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
541 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
542 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
543 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
544 } else {
545 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
546 }
547 }
548 }
549 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
550
551 return assignDecl;
552 }
553
554 Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType ) {
555 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
556
557 ObjectDecl *returnVal = new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
558 assignType->get_returnVals().push_back( returnVal );
559
560 ObjectDecl *dstParam = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
561 assignType->get_parameters().push_back( dstParam );
562
563 ObjectDecl *srcParam = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
564 assignType->get_parameters().push_back( srcParam );
565
566 FunctionDecl *assignDecl = new FunctionDecl( "?=?", Declaration::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true );
567 assignDecl->fixUniqueId();
568
569 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
570 copy->get_args().push_back( new VariableExpr( dstParam ) );
571 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
572 copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
573
574 assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
575 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
576
577 return assignDecl;
578 }
579
580 void AddStructAssignment::visit( StructDecl *structDecl ) {
581 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
582 StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
583 structInst->set_baseStruct( structDecl );
584 declsToAdd.push_back( makeStructAssignment( structDecl, structInst ) );
585 structsDone.insert( structDecl->get_name() );
586 }
587 }
588
589 void AddStructAssignment::visit( UnionDecl *unionDecl ) {
590 if ( ! unionDecl->get_members().empty() ) {
591 UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
592 unionInst->set_baseUnion( unionDecl );
593 declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst ) );
594 }
595 }
596
597 void AddStructAssignment::visit( TypeDecl *typeDecl ) {
598 CompoundStmt *stmts = 0;
599 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
600 typeInst->set_baseType( typeDecl );
601 ObjectDecl *src = new ObjectDecl( "_src", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
602 ObjectDecl *dst = new ObjectDecl( "_dst", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
603 if ( typeDecl->get_base() ) {
604 stmts = new CompoundStmt( std::list< Label >() );
605 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
606 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
607 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
608 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
609 }
610 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
611 type->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
612 type->get_parameters().push_back( dst );
613 type->get_parameters().push_back( src );
614 FunctionDecl *func = new FunctionDecl( "?=?", Declaration::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false );
615 declsToAdd.push_back( func );
616 }
617
618 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
619 if ( ! declsToAdd.empty() ) {
620 for( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
621 statements.insert( i, new DeclStmt( noLabels, *decl ) );
622 }
623 declsToAdd.clear();
624 }
625 }
626
627 void AddStructAssignment::visit( FunctionType *) {
628 // ensure that we don't add assignment ops for types defined
629 // as part of the function
630 }
631
632 void AddStructAssignment::visit( PointerType *) {
633 // ensure that we don't add assignment ops for types defined
634 // as part of the pointer
635 }
636
637 void AddStructAssignment::visit( ContextDecl *) {
638 // ensure that we don't add assignment ops for types defined
639 // as part of the context
640 }
641
642 template< typename StmtClass >
643 inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
644 std::set< std::string > oldStructs = structsDone;
645 addVisit( stmt, *this );
646 structsDone = oldStructs;
647 }
648
649 void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
650 visitStatement( compoundStmt );
651 }
652
653 void AddStructAssignment::visit( IfStmt *ifStmt ) {
654 visitStatement( ifStmt );
655 }
656
657 void AddStructAssignment::visit( WhileStmt *whileStmt ) {
658 visitStatement( whileStmt );
659 }
660
661 void AddStructAssignment::visit( ForStmt *forStmt ) {
662 visitStatement( forStmt );
663 }
664
665 void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
666 visitStatement( switchStmt );
667 }
668
669 void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
670 visitStatement( switchStmt );
671 }
672
673 void AddStructAssignment::visit( CaseStmt *caseStmt ) {
674 visitStatement( caseStmt );
675 }
676
677 void AddStructAssignment::visit( CatchStmt *cathStmt ) {
678 visitStatement( cathStmt );
679 }
680
681 bool isTypedef( Declaration *decl ) {
682 return dynamic_cast< TypedefDecl * >( decl );
683 }
684
685 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
686 EliminateTypedef eliminator;
687 mutateAll( translationUnit, eliminator );
688 filter( translationUnit, isTypedef, true );
689 }
690
691 Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
692 std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() );
693 if ( def != typedefNames.end() ) {
694 Type *ret = def->second->get_base()->clone();
695 ret->get_qualifiers() += typeInst->get_qualifiers();
696 delete typeInst;
697 return ret;
698 }
699 return typeInst;
700 }
701
702 Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
703 Declaration *ret = Mutator::mutate( tyDecl );
704 typedefNames[ tyDecl->get_name() ] = tyDecl;
705 if ( AggregateDecl *aggDecl = dynamic_cast< AggregateDecl * >( tyDecl->get_base() ) ) {
706 tyDecl->set_base( 0 );
707 delete tyDecl;
708 return aggDecl;
709 } else {
710 return ret;
711 }
712 }
713
714 TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
715 std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );
716 if ( i != typedefNames.end() ) {
717 typedefNames.erase( i ) ;
718 }
719 return typeDecl;
720 }
721
722 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
723 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
724 DeclarationWithType *ret = Mutator::mutate( funcDecl );
725 typedefNames = oldNames;
726 return ret;
727 }
728
729 ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
730 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
731 ObjectDecl *ret = Mutator::mutate( objDecl );
732 typedefNames = oldNames;
733 return ret;
734 }
735
736 Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
737 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
738 Expression *ret = Mutator::mutate( castExpr );
739 typedefNames = oldNames;
740 return ret;
741 }
742
743 CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
744 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
745 CompoundStmt *ret = Mutator::mutate( compoundStmt );
746 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
747 while ( i != compoundStmt->get_kids().end() ) {
748 std::list< Statement * >::iterator next = i;
749 ++next;
750 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
751 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
752 delete *i;
753 compoundStmt->get_kids().erase( i );
754 }
755 }
756 i = next;
757 }
758 typedefNames = oldNames;
759 return ret;
760 }
761} // namespace SymTab
Note: See TracBrowser for help on using the repository browser.