source: translator/SymTab/Validate.cc@ 17cd4eb

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

fixed restrict, fixed parameter copy, introduced name table for types, changed variable after to string

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