source: src/SymTab/Validate.cc @ f6d7e0f

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since f6d7e0f was f6d7e0f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

label fix, enumeration assignment first attempt

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