source: src/SymTab/Validate.cc@ 2794fff

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 stuck-waitfor-destruct with_gc
Last change on this file since 2794fff was 145f1fc, checked in by Rob Schluntz <rschlunt@…>, 11 years ago

modified ForStmt to have a list of statements for the initialization portion, and reverted to hoisting the initialization

  • Property mode set to 100644
File size: 35.9 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 : Tue Jul 14 12:27:54 2015
13// Update Count : 186
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#include "TypeEquality.h"
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 EliminateTypedef() : scopeLevel( 0 ) {}
160 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
161 private:
162 virtual Declaration *mutate( TypedefDecl *typeDecl );
163 virtual TypeDecl *mutate( TypeDecl *typeDecl );
164 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
165 virtual ObjectDecl *mutate( ObjectDecl *objDecl );
166 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
167 virtual Type *mutate( TypeInstType *aggregateUseType );
168 virtual Expression *mutate( CastExpr *castExpr );
169
170 virtual Declaration *mutate( StructDecl * structDecl );
171 virtual Declaration *mutate( UnionDecl * unionDecl );
172 virtual Declaration *mutate( EnumDecl * enumDecl );
173 virtual Declaration *mutate( ContextDecl * contextDecl );
174
175 template<typename AggDecl>
176 AggDecl *handleAggregate( AggDecl * aggDecl );
177
178 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
179 TypedefMap typedefNames;
180 int scopeLevel;
181 };
182
183 void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
184 Pass1 pass1;
185 Pass2 pass2( doDebug, 0 );
186 Pass3 pass3( 0 );
187 EliminateTypedef::eliminateTypedef( translationUnit );
188 HoistStruct::hoistStruct( translationUnit );
189 acceptAll( translationUnit, pass1 );
190 acceptAll( translationUnit, pass2 );
191 // need to collect all of the assignment operators prior to
192 // this point and only generate assignment operators if one doesn't exist
193 AddStructAssignment::addStructAssignment( translationUnit );
194 acceptAll( translationUnit, pass3 );
195 }
196
197 void validateType( Type *type, const Indexer *indexer ) {
198 Pass1 pass1;
199 Pass2 pass2( false, indexer );
200 Pass3 pass3( indexer );
201 type->accept( pass1 );
202 type->accept( pass2 );
203 type->accept( pass3 );
204 }
205
206 template< typename Visitor >
207 void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) {
208 std::list< Declaration * >::iterator i = translationUnit.begin();
209 while ( i != translationUnit.end() ) {
210 (*i)->accept( visitor );
211 std::list< Declaration * >::iterator next = i;
212 next++;
213 if ( ! visitor.get_declsToAdd().empty() ) {
214 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );
215 } // if
216 i = next;
217 } // while
218 }
219
220 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
221 HoistStruct hoister;
222 acceptAndAdd( translationUnit, hoister, true );
223 }
224
225 HoistStruct::HoistStruct() : inStruct( false ) {
226 }
227
228 void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
229 std::list< Declaration * >::iterator i = declList.begin();
230 while ( i != declList.end() ) {
231 std::list< Declaration * >::iterator next = i;
232 ++next;
233 if ( pred( *i ) ) {
234 if ( doDelete ) {
235 delete *i;
236 } // if
237 declList.erase( i );
238 } // if
239 i = next;
240 } // while
241 }
242
243 bool isStructOrUnion( Declaration *decl ) {
244 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
245 }
246
247 template< typename AggDecl >
248 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
249 if ( inStruct ) {
250 // Add elements in stack order corresponding to nesting structure.
251 declsToAdd.push_front( aggregateDecl );
252 Visitor::visit( aggregateDecl );
253 } else {
254 inStruct = true;
255 Visitor::visit( aggregateDecl );
256 inStruct = false;
257 } // if
258 // Always remove the hoisted aggregate from the inner structure.
259 filter( aggregateDecl->get_members(), isStructOrUnion, false );
260 }
261
262 void HoistStruct::visit( StructDecl *aggregateDecl ) {
263 handleAggregate( aggregateDecl );
264 }
265
266 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
267 handleAggregate( aggregateDecl );
268 }
269
270 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
271 addVisit( compoundStmt, *this );
272 }
273
274 void HoistStruct::visit( IfStmt *ifStmt ) {
275 addVisit( ifStmt, *this );
276 }
277
278 void HoistStruct::visit( WhileStmt *whileStmt ) {
279 addVisit( whileStmt, *this );
280 }
281
282 void HoistStruct::visit( ForStmt *forStmt ) {
283 addVisit( forStmt, *this );
284 }
285
286 void HoistStruct::visit( SwitchStmt *switchStmt ) {
287 addVisit( switchStmt, *this );
288 }
289
290 void HoistStruct::visit( ChooseStmt *switchStmt ) {
291 addVisit( switchStmt, *this );
292 }
293
294 void HoistStruct::visit( CaseStmt *caseStmt ) {
295 addVisit( caseStmt, *this );
296 }
297
298 void HoistStruct::visit( CatchStmt *cathStmt ) {
299 addVisit( cathStmt, *this );
300 }
301
302 void Pass1::visit( EnumDecl *enumDecl ) {
303 // Set the type of each member of the enumeration to be EnumConstant
304
305 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
306 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
307 assert( obj );
308 // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
309 BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
310 obj->set_type( enumType ) ;
311 } // for
312 Parent::visit( enumDecl );
313 }
314
315 namespace {
316 template< typename DWTIterator >
317 void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
318 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
319 // entirely other fix ups are handled by the FixFunction class
320 if ( begin == end ) return;
321 FixFunction fixer;
322 DWTIterator i = begin;
323 *i = (*i )->acceptMutator( fixer );
324 if ( fixer.get_isVoid() ) {
325 DWTIterator j = i;
326 ++i;
327 func->get_parameters().erase( j );
328 if ( i != end ) {
329 throw SemanticError( "invalid type void in function type ", func );
330 } // if
331 } else {
332 ++i;
333 for ( ; i != end; ++i ) {
334 FixFunction fixer;
335 *i = (*i )->acceptMutator( fixer );
336 if ( fixer.get_isVoid() ) {
337 throw SemanticError( "invalid type void in function type ", func );
338 } // if
339 } // for
340 } // if
341 }
342 }
343
344 void Pass1::visit( FunctionType *func ) {
345 // Fix up parameters and return types
346 fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
347 fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
348 Visitor::visit( func );
349 }
350
351 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
352 if ( other_indexer ) {
353 indexer = other_indexer;
354 } else {
355 indexer = this;
356 } // if
357 }
358
359 void Pass2::visit( StructInstType *structInst ) {
360 Parent::visit( structInst );
361 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
362 // it's not a semantic error if the struct is not found, just an implicit forward declaration
363 if ( st ) {
364 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
365 structInst->set_baseStruct( st );
366 } // if
367 if ( ! st || st->get_members().empty() ) {
368 // use of forward declaration
369 forwardStructs[ structInst->get_name() ].push_back( structInst );
370 } // if
371 }
372
373 void Pass2::visit( UnionInstType *unionInst ) {
374 Parent::visit( unionInst );
375 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
376 // it's not a semantic error if the union is not found, just an implicit forward declaration
377 if ( un ) {
378 unionInst->set_baseUnion( un );
379 } // if
380 if ( ! un || un->get_members().empty() ) {
381 // use of forward declaration
382 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
383 } // if
384 }
385
386 void Pass2::visit( ContextInstType *contextInst ) {
387 Parent::visit( contextInst );
388 ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
389 if ( ! ctx ) {
390 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
391 } // if
392 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
393 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
394 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
395 cloneAll( otherCtx->get_members(), contextInst->get_members() );
396 } else {
397 contextInst->get_members().push_back( (*assert )->clone() );
398 } // if
399 } // for
400 } // for
401 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() ) );
402 }
403
404 void Pass2::visit( StructDecl *structDecl ) {
405 if ( ! structDecl->get_members().empty() ) {
406 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
407 if ( fwds != forwardStructs.end() ) {
408 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
409 (*inst )->set_baseStruct( structDecl );
410 } // for
411 forwardStructs.erase( fwds );
412 } // if
413 } // if
414 Indexer::visit( structDecl );
415 }
416
417 void Pass2::visit( UnionDecl *unionDecl ) {
418 if ( ! unionDecl->get_members().empty() ) {
419 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
420 if ( fwds != forwardUnions.end() ) {
421 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
422 (*inst )->set_baseUnion( unionDecl );
423 } // for
424 forwardUnions.erase( fwds );
425 } // if
426 } // if
427 Indexer::visit( unionDecl );
428 }
429
430 void Pass2::visit( TypeInstType *typeInst ) {
431 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
432 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
433 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
434 } // if
435 } // if
436 }
437
438 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
439 if ( other_indexer ) {
440 indexer = other_indexer;
441 } else {
442 indexer = this;
443 } // if
444 }
445
446 void forallFixer( Type *func ) {
447 // Fix up assertions
448 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
449 std::list< DeclarationWithType * > toBeDone, nextRound;
450 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
451 while ( ! toBeDone.empty() ) {
452 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
453 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
454 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
455 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
456 assert( dwt );
457 nextRound.push_back( dwt->clone() );
458 }
459 delete ctx;
460 } else {
461 FixFunction fixer;
462 *assertion = (*assertion )->acceptMutator( fixer );
463 if ( fixer.get_isVoid() ) {
464 throw SemanticError( "invalid type void in assertion of function ", func );
465 }
466 (*type )->get_assertions().push_back( *assertion );
467 } // if
468 } // for
469 toBeDone.clear();
470 toBeDone.splice( toBeDone.end(), nextRound );
471 } // while
472 } // for
473 }
474
475 void Pass3::visit( ObjectDecl *object ) {
476 forallFixer( object->get_type() );
477 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
478 forallFixer( pointer->get_base() );
479 } // if
480 Parent::visit( object );
481 object->fixUniqueId();
482 }
483
484 void Pass3::visit( FunctionDecl *func ) {
485 forallFixer( func->get_type() );
486 Parent::visit( func );
487 func->fixUniqueId();
488 }
489
490 static const std::list< std::string > noLabels;
491
492 void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) {
493 AddStructAssignment visitor;
494 acceptAndAdd( translationUnit, visitor, false );
495 }
496
497 template< typename OutputIterator >
498 void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {
499 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member );
500 // unnamed bit fields are not copied as they cannot be accessed
501 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
502
503 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
504
505 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
506 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
507
508 // do something special for unnamed members
509 Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
510 assignExpr->get_args().push_back( dstselect );
511
512 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
513 assignExpr->get_args().push_back( srcselect );
514
515 *out++ = new ExprStmt( noLabels, assignExpr );
516 }
517
518 template< typename OutputIterator >
519 void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {
520 static UniqueName indexName( "_index" );
521
522 // for a flexible array member nothing is done -- user must define own assignment
523 if ( ! array->get_dimension() ) return;
524
525 ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );
526 *out++ = new DeclStmt( noLabels, index );
527
528 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
529 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
530 init->get_args().push_back( new NameExpr( "0" ) );
531 Statement *initStmt = new ExprStmt( noLabels, init );
532 std::list<Statement *> initList;
533 initList.push_back( initStmt );
534
535 UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );
536 cond->get_args().push_back( new VariableExpr( index ) );
537 cond->get_args().push_back( array->get_dimension()->clone() );
538
539 UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );
540 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
541
542 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
543
544 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
545 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
546
547 Expression *dstselect = new MemberExpr( member, derefExpr );
548 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
549 dstIndex->get_args().push_back( dstselect );
550 dstIndex->get_args().push_back( new VariableExpr( index ) );
551 assignExpr->get_args().push_back( dstIndex );
552
553 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
554 UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
555 srcIndex->get_args().push_back( srcselect );
556 srcIndex->get_args().push_back( new VariableExpr( index ) );
557 assignExpr->get_args().push_back( srcIndex );
558
559 *out++ = new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, assignExpr ) );
560 }
561
562 //E ?=?(E volatile*, int),
563 // ?=?(E _Atomic volatile*, int);
564 void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
565 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
566
567 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
568 assignType->get_returnVals().push_back( returnVal );
569
570 // need two assignment operators with different types
571 FunctionType * assignType2 = assignType->clone();
572
573 // E ?=?(E volatile *, E)
574 Type *etype = refType->clone();
575 // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false);
576
577 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 );
578 assignType->get_parameters().push_back( dstParam );
579
580 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 );
581 assignType->get_parameters().push_back( srcParam );
582
583 // E ?=?(E volatile *, int)
584 assignType2->get_parameters().push_back( dstParam->clone() );
585 BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt);
586 ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 );
587 assignType2->get_parameters().push_back( srcParam2 );
588
589 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
590 // because each unit generates copies of the default routines for each aggregate.
591
592 // since there is no definition, these should not be inline
593 // make these intrinsic so that the code generator does not make use of them
594 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false );
595 assignDecl->fixUniqueId();
596 FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false );
597 assignDecl2->fixUniqueId();
598
599 // these should be built in the same way that the prelude
600 // functions are, so build a list containing the prototypes
601 // and allow MakeLibCfa to autogenerate the bodies.
602 std::list< Declaration * > assigns;
603 assigns.push_back( assignDecl );
604 assigns.push_back( assignDecl2 );
605
606 LibCfa::makeLibCfa( assigns );
607
608 // need to remove the prototypes, since this may be nested in a routine
609 for (int start = 0, end = assigns.size()/2; start < end; start++) {
610 delete assigns.front();
611 assigns.pop_front();
612 }
613
614 declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
615 }
616
617
618 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
619 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
620
621 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
622 assignType->get_returnVals().push_back( returnVal );
623
624 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
625 assignType->get_parameters().push_back( dstParam );
626
627 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
628 assignType->get_parameters().push_back( srcParam );
629
630 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
631 // because each unit generates copies of the default routines for each aggregate.
632 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
633 assignDecl->fixUniqueId();
634
635 for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
636 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
637 // query the type qualifiers of this field and skip assigning it if it is marked const.
638 // If it is an array type, we need to strip off the array layers to find its qualifiers.
639 Type * type = dwt->get_type();
640 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
641 type = at->get_base();
642 }
643
644 if ( type->get_qualifiers().isConst ) {
645 // don't assign const members
646 continue;
647 }
648
649 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
650 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
651 } else {
652 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
653 } // if
654 } // if
655 } // for
656 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
657
658 return assignDecl;
659 }
660
661 Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
662 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
663
664 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
665 assignType->get_returnVals().push_back( returnVal );
666
667 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
668 assignType->get_parameters().push_back( dstParam );
669
670 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
671 assignType->get_parameters().push_back( srcParam );
672
673 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
674 // because each unit generates copies of the default routines for each aggregate.
675 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
676 assignDecl->fixUniqueId();
677
678 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
679 copy->get_args().push_back( new VariableExpr( dstParam ) );
680 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
681 copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
682
683 assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
684 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
685
686 return assignDecl;
687 }
688
689 void AddStructAssignment::visit( EnumDecl *enumDecl ) {
690 if ( ! enumDecl->get_members().empty() ) {
691 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
692 // enumInst->set_baseEnum( enumDecl );
693 // declsToAdd.push_back(
694 makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );
695 }
696 }
697
698 void AddStructAssignment::visit( StructDecl *structDecl ) {
699 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
700 StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
701 structInst->set_baseStruct( structDecl );
702 declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
703 structsDone.insert( structDecl->get_name() );
704 } // if
705 }
706
707 void AddStructAssignment::visit( UnionDecl *unionDecl ) {
708 if ( ! unionDecl->get_members().empty() ) {
709 UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
710 unionInst->set_baseUnion( unionDecl );
711 declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
712 } // if
713 }
714
715 void AddStructAssignment::visit( TypeDecl *typeDecl ) {
716 CompoundStmt *stmts = 0;
717 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
718 typeInst->set_baseType( typeDecl );
719 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
720 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
721 if ( typeDecl->get_base() ) {
722 stmts = new CompoundStmt( std::list< Label >() );
723 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
724 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
725 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
726 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
727 } // if
728 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
729 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
730 type->get_parameters().push_back( dst );
731 type->get_parameters().push_back( src );
732 FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
733 declsToAdd.push_back( func );
734 }
735
736 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
737 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
738 statements.insert( i, new DeclStmt( noLabels, *decl ) );
739 } // for
740 declsToAdd.clear();
741 }
742
743 void AddStructAssignment::visit( FunctionType *) {
744 // ensure that we don't add assignment ops for types defined as part of the function
745 }
746
747 void AddStructAssignment::visit( PointerType *) {
748 // ensure that we don't add assignment ops for types defined as part of the pointer
749 }
750
751 void AddStructAssignment::visit( ContextDecl *) {
752 // ensure that we don't add assignment ops for types defined as part of the context
753 }
754
755 template< typename StmtClass >
756 inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
757 std::set< std::string > oldStructs = structsDone;
758 addVisit( stmt, *this );
759 structsDone = oldStructs;
760 }
761
762 void AddStructAssignment::visit( FunctionDecl *functionDecl ) {
763 maybeAccept( functionDecl->get_functionType(), *this );
764 acceptAll( functionDecl->get_oldDecls(), *this );
765 functionNesting += 1;
766 maybeAccept( functionDecl->get_statements(), *this );
767 functionNesting -= 1;
768 }
769
770 void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
771 visitStatement( compoundStmt );
772 }
773
774 void AddStructAssignment::visit( IfStmt *ifStmt ) {
775 visitStatement( ifStmt );
776 }
777
778 void AddStructAssignment::visit( WhileStmt *whileStmt ) {
779 visitStatement( whileStmt );
780 }
781
782 void AddStructAssignment::visit( ForStmt *forStmt ) {
783 visitStatement( forStmt );
784 }
785
786 void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
787 visitStatement( switchStmt );
788 }
789
790 void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
791 visitStatement( switchStmt );
792 }
793
794 void AddStructAssignment::visit( CaseStmt *caseStmt ) {
795 visitStatement( caseStmt );
796 }
797
798 void AddStructAssignment::visit( CatchStmt *cathStmt ) {
799 visitStatement( cathStmt );
800 }
801
802 bool isTypedef( Declaration *decl ) {
803 return dynamic_cast< TypedefDecl * >( decl );
804 }
805
806 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
807 EliminateTypedef eliminator;
808 mutateAll( translationUnit, eliminator );
809 filter( translationUnit, isTypedef, true );
810 }
811
812 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
813 // instances of typedef types will come here. If it is an instance
814 // of a typdef type, link the instance to its actual type.
815 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
816 if ( def != typedefNames.end() ) {
817 Type *ret = def->second.first->get_base()->clone();
818 ret->get_qualifiers() += typeInst->get_qualifiers();
819 delete typeInst;
820 return ret;
821 } // if
822 return typeInst;
823 }
824
825 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
826 Declaration *ret = Mutator::mutate( tyDecl );
827 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
828 // typedef to the same name from the same scope
829 // must be from the same type
830
831 Type * t1 = tyDecl->get_base();
832 Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
833 if ( ! typeEquals( t1, t2, true ) ) {
834 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
835 }
836 } else {
837 typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
838 } // if
839
840 // When a typedef is a forward declaration:
841 // typedef struct screen SCREEN;
842 // the declaration portion must be retained:
843 // struct screen;
844 // because the expansion of the typedef is:
845 // void rtn( SCREEN *p ) => void rtn( struct screen *p )
846 // hence the type-name "screen" must be defined.
847 // Note, qualifiers on the typedef are superfluous for the forward declaration.
848 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
849 return new StructDecl( aggDecl->get_name() );
850 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
851 return new UnionDecl( aggDecl->get_name() );
852 } else {
853 return ret;
854 } // if
855 }
856
857 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
858 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
859 if ( i != typedefNames.end() ) {
860 typedefNames.erase( i ) ;
861 } // if
862 return typeDecl;
863 }
864
865 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
866 TypedefMap oldNames = typedefNames;
867 DeclarationWithType *ret = Mutator::mutate( funcDecl );
868 typedefNames = oldNames;
869 return ret;
870 }
871
872 ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
873 TypedefMap oldNames = typedefNames;
874 ObjectDecl *ret = Mutator::mutate( objDecl );
875 typedefNames = oldNames;
876 return ret;
877 }
878
879 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
880 TypedefMap oldNames = typedefNames;
881 Expression *ret = Mutator::mutate( castExpr );
882 typedefNames = oldNames;
883 return ret;
884 }
885
886 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
887 TypedefMap oldNames = typedefNames;
888 scopeLevel += 1;
889 CompoundStmt *ret = Mutator::mutate( compoundStmt );
890 scopeLevel -= 1;
891 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
892 while ( i != compoundStmt->get_kids().end() ) {
893 std::list< Statement * >::iterator next = i+1;
894 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
895 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
896 delete *i;
897 compoundStmt->get_kids().erase( i );
898 } // if
899 } // if
900 i = next;
901 } // while
902 typedefNames = oldNames;
903 return ret;
904 }
905
906 // there may be typedefs nested within aggregates
907 // in order for everything to work properly, these
908 // should be removed as well
909 template<typename AggDecl>
910 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
911 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
912 for ( ; it != aggDecl->get_members().end(); ) {
913 std::list< Declaration * >::iterator next = it+1;
914 if ( dynamic_cast< TypedefDecl * >( *it ) ) {
915 delete *it;
916 aggDecl->get_members().erase( it );
917 } // if
918 it = next;
919 }
920 return aggDecl;
921 }
922
923 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
924 Mutator::mutate( structDecl );
925 return handleAggregate( structDecl );
926 }
927
928 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
929 Mutator::mutate( unionDecl );
930 return handleAggregate( unionDecl );
931 }
932
933 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
934 Mutator::mutate( enumDecl );
935 return handleAggregate( enumDecl );
936 }
937
938 Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
939 Mutator::mutate( contextDecl );
940 return handleAggregate( contextDecl );
941 }
942
943} // namespace SymTab
944
945// Local Variables: //
946// tab-width: 4 //
947// mode: c++ //
948// compile-command: "make install" //
949// End: //
Note: See TracBrowser for help on using the repository browser.