source: src/SymTab/Validate.cc @ 8686f31

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 8686f31 was 8686f31, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix generated enum assignment for nested declarations

  • Property mode set to 100644
File size: 33.7 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 07 10:41:23 2015
13// Update Count     : 136
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                // need to remove the prototypes, since these can appear nested in a routine
593                for (int start = 0, end = assigns.size()/2; start < end; start++) {
594                        delete assigns.front();
595                        assigns.pop_front();
596                }
597
598                declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
599
600                // return assignDecl;
601        }
602
603
604        Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
605                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
606 
607                ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
608                assignType->get_returnVals().push_back( returnVal );
609 
610                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
611                assignType->get_parameters().push_back( dstParam );
612 
613                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
614                assignType->get_parameters().push_back( srcParam );
615
616                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
617                // because each unit generates copies of the default routines for each aggregate.
618                FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
619                assignDecl->fixUniqueId();
620 
621                for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
622                        if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
623                                // query the type qualifiers of this field and skip assigning it if it is marked const.
624                                // If it is an array type, we need to strip off the array layers to find its qualifiers.
625                                Type * type = dwt->get_type();
626                                while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
627                                        type = at->get_base();
628                                }
629
630                                if ( type->get_qualifiers().isConst ) {
631                                        // don't assign const members
632                                        continue;
633                                }
634
635                                if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
636                                        makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
637                                } else {
638                                        makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
639                                } // if
640                        } // if
641                } // for
642                assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
643 
644                return assignDecl;
645        }
646
647        Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
648                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
649 
650                ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
651                assignType->get_returnVals().push_back( returnVal );
652 
653                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
654                assignType->get_parameters().push_back( dstParam );
655 
656                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
657                assignType->get_parameters().push_back( srcParam );
658 
659                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
660                // because each unit generates copies of the default routines for each aggregate.
661                FunctionDecl *assignDecl = new FunctionDecl( "?=?",  functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
662                assignDecl->fixUniqueId();
663 
664                UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
665                copy->get_args().push_back( new VariableExpr( dstParam ) );
666                copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
667                copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
668
669                assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
670                assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
671 
672                return assignDecl;
673        }
674
675        void AddStructAssignment::visit( EnumDecl *enumDecl ) {
676                if ( ! enumDecl->get_members().empty() ) {
677                        EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
678                        // enumInst->set_baseEnum( enumDecl );
679                        // declsToAdd.push_back(
680                        makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );
681                }
682        }
683
684        void AddStructAssignment::visit( StructDecl *structDecl ) {
685                if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
686                        StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
687                        structInst->set_baseStruct( structDecl );
688                        declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
689                        structsDone.insert( structDecl->get_name() );
690                } // if
691        }
692
693        void AddStructAssignment::visit( UnionDecl *unionDecl ) {
694                if ( ! unionDecl->get_members().empty() ) {
695                        UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
696                        unionInst->set_baseUnion( unionDecl );
697                        declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
698                } // if
699        }
700
701        void AddStructAssignment::visit( TypeDecl *typeDecl ) {
702                CompoundStmt *stmts = 0;
703                TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
704                typeInst->set_baseType( typeDecl );
705                ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
706                ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
707                if ( typeDecl->get_base() ) {
708                        stmts = new CompoundStmt( std::list< Label >() );
709                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
710                        assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
711                        assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
712                        stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
713                } // if
714                FunctionType *type = new FunctionType( Type::Qualifiers(), false );
715                type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
716                type->get_parameters().push_back( dst );
717                type->get_parameters().push_back( src );
718                FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
719                declsToAdd.push_back( func );
720        }
721
722        void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
723                for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
724                        statements.insert( i, new DeclStmt( noLabels, *decl ) );
725                } // for
726                declsToAdd.clear();
727        }
728
729        void AddStructAssignment::visit( FunctionType *) {
730                // ensure that we don't add assignment ops for types defined as part of the function
731        }
732
733        void AddStructAssignment::visit( PointerType *) {
734                // ensure that we don't add assignment ops for types defined as part of the pointer
735        }
736
737        void AddStructAssignment::visit( ContextDecl *) {
738                // ensure that we don't add assignment ops for types defined as part of the context
739        }
740
741        template< typename StmtClass >
742        inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
743                std::set< std::string > oldStructs = structsDone;
744                addVisit( stmt, *this );
745                structsDone = oldStructs;
746        }
747
748        void AddStructAssignment::visit( FunctionDecl *functionDecl ) {
749                maybeAccept( functionDecl->get_functionType(), *this );
750                acceptAll( functionDecl->get_oldDecls(), *this );
751                functionNesting += 1;
752                maybeAccept( functionDecl->get_statements(), *this );
753                functionNesting -= 1;
754        }
755
756        void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
757                visitStatement( compoundStmt );
758        }
759
760        void AddStructAssignment::visit( IfStmt *ifStmt ) {
761                visitStatement( ifStmt );
762        }
763
764        void AddStructAssignment::visit( WhileStmt *whileStmt ) {
765                visitStatement( whileStmt );
766        }
767
768        void AddStructAssignment::visit( ForStmt *forStmt ) {
769                visitStatement( forStmt );
770        }
771
772        void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
773                visitStatement( switchStmt );
774        }
775
776        void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
777                visitStatement( switchStmt );
778        }
779
780        void AddStructAssignment::visit( CaseStmt *caseStmt ) {
781                visitStatement( caseStmt );
782        }
783
784        void AddStructAssignment::visit( CatchStmt *cathStmt ) {
785                visitStatement( cathStmt );
786        }
787
788        bool isTypedef( Declaration *decl ) {
789                return dynamic_cast< TypedefDecl * >( decl );
790        }
791
792        void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
793                EliminateTypedef eliminator;
794                mutateAll( translationUnit, eliminator );
795                filter( translationUnit, isTypedef, true );
796        }
797
798        Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
799                std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() );
800                if ( def != typedefNames.end() ) {
801                        Type *ret = def->second->get_base()->clone();
802                        ret->get_qualifiers() += typeInst->get_qualifiers();
803                        delete typeInst;
804                        return ret;
805                } // if
806                return typeInst;
807        }
808
809        Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
810                Declaration *ret = Mutator::mutate( tyDecl );
811                typedefNames[ tyDecl->get_name() ] = tyDecl;
812                // When a typedef is a forward declaration:
813                //    typedef struct screen SCREEN;
814                // the declaration portion must be retained:
815                //    struct screen;
816                // because the expansion of the typedef is:
817                //    void rtn( SCREEN *p ) => void rtn( struct screen *p )
818                // hence the type-name "screen" must be defined.
819                // Note, qualifiers on the typedef are superfluous for the forward declaration.
820                if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
821                        return new StructDecl( aggDecl->get_name() );
822                } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
823                        return new UnionDecl( aggDecl->get_name() );
824                } else {
825                        return ret;
826                } // if
827        }
828
829        TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
830                std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );
831                if ( i != typedefNames.end() ) {
832                        typedefNames.erase( i ) ;
833                } // if
834                return typeDecl;
835        }
836
837        DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
838                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
839                DeclarationWithType *ret = Mutator::mutate( funcDecl );
840                typedefNames = oldNames;
841                return ret;
842        }
843
844        ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
845                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
846                ObjectDecl *ret = Mutator::mutate( objDecl );
847                typedefNames = oldNames;
848                return ret;
849        }
850
851        Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
852                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
853                Expression *ret = Mutator::mutate( castExpr );
854                typedefNames = oldNames;
855                return ret;
856        }
857
858        CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
859                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
860                CompoundStmt *ret = Mutator::mutate( compoundStmt );
861                std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
862                while ( i != compoundStmt->get_kids().end() ) {
863                        std::list< Statement * >::iterator next = i;
864                        ++next;
865                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
866                                if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
867                                        delete *i;
868                                        compoundStmt->get_kids().erase( i );
869                                } // if
870                        } // if
871                        i = next;
872                } // while
873                typedefNames = oldNames;
874                return ret;
875        }
876} // namespace SymTab
877
878// Local Variables: //
879// tab-width: 4 //
880// mode: c++ //
881// compile-command: "make install" //
882// End: //
Note: See TracBrowser for help on using the repository browser.