source: src/SymTab/Validate.cc@ 28a8cf9

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 28a8cf9 was 28a8cf9, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

missing declaration in Validate

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