source: src/SymTab/Autogen.cc @ dac0aa9

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

generate bodies for autogenerated field ctors which appropriately calls default/copy ctor on each field

  • Property mode set to 100644
File size: 25.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Autogen.cc --
8//
9// Author           : Rob Schluntz
10// Created On       : Thu Mar 03 15:45:56 2016
11// Last Modified By : Rob Schluntz
12// Last Modified On : Thu Mar 03 15:45:56 2016
13// Update Count     : 1
14//
15
16#include <list>
17#include <iterator>
18#include "SynTree/Visitor.h"
19#include "SynTree/Type.h"
20#include "SynTree/Statement.h"
21#include "SynTree/TypeSubstitution.h"
22#include "Common/utility.h"
23#include "AddVisit.h"
24#include "MakeLibCfa.h"
25#include "Autogen.h"
26
27namespace SymTab {
28        class AutogenerateRoutines : public Visitor {
29                public:
30                std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
31
32                virtual void visit( EnumDecl *enumDecl );
33                virtual void visit( StructDecl *structDecl );
34                virtual void visit( UnionDecl *structDecl );
35                virtual void visit( TypeDecl *typeDecl );
36                virtual void visit( ContextDecl *ctxDecl );
37                virtual void visit( FunctionDecl *functionDecl );
38
39                virtual void visit( FunctionType *ftype );
40                virtual void visit( PointerType *ftype );
41
42                virtual void visit( CompoundStmt *compoundStmt );
43                virtual void visit( IfStmt *ifStmt );
44                virtual void visit( WhileStmt *whileStmt );
45                virtual void visit( ForStmt *forStmt );
46                virtual void visit( SwitchStmt *switchStmt );
47                virtual void visit( ChooseStmt *chooseStmt );
48                virtual void visit( CaseStmt *caseStmt );
49                virtual void visit( CatchStmt *catchStmt );
50
51                AutogenerateRoutines() : functionNesting( 0 ) {}
52                private:
53                template< typename StmtClass > void visitStatement( StmtClass *stmt );
54
55                std::list< Declaration * > declsToAdd;
56                std::set< std::string > structsDone;
57                unsigned int functionNesting;     // current level of nested functions
58        };
59
60        void autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
61                AutogenerateRoutines visitor;
62                acceptAndAdd( translationUnit, visitor, false );
63        }
64
65        template< typename OutputIterator >
66        void makeScalarFunction( Expression *src, ObjectDecl *dstParam, DeclarationWithType *member, std::string fname, OutputIterator out ) {
67                ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member );
68                // unnamed bit fields are not copied as they cannot be accessed
69                if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
70
71                // want to be able to generate assignment, ctor, and dtor generically,
72                // so fname is either ?=?, ?{}, or ^?{}
73                UntypedExpr *fExpr = new UntypedExpr( new NameExpr( fname ) );
74
75                UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
76                derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
77
78                // do something special for unnamed members
79                Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
80                fExpr->get_args().push_back( dstselect );
81
82                if ( src ) {
83                        fExpr->get_args().push_back( src );
84                }
85
86                *out++ = new ExprStmt( noLabels, fExpr );
87        }
88
89        template< typename OutputIterator >
90        void makeUnionFieldsAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, UnionInstType *unionType, OutputIterator out ) {
91                UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
92                copy->get_args().push_back( new VariableExpr( dstParam ) );
93                copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
94                copy->get_args().push_back( new SizeofExpr( unionType ) );
95
96                *out++ = new ExprStmt( noLabels, copy );
97        }
98
99        //E ?=?(E volatile*, int),
100        //  ?=?(E _Atomic volatile*, int);
101        void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
102                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
103
104                ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
105                assignType->get_returnVals().push_back( returnVal );
106
107                // need two assignment operators with different types
108                FunctionType * assignType2 = assignType->clone();
109
110                // E ?=?(E volatile *, E)
111                Type *etype = refType->clone();
112                // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false);
113
114                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 );
115                assignType->get_parameters().push_back( dstParam );
116
117                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 );
118                assignType->get_parameters().push_back( srcParam );
119
120                // E ?=?(E volatile *, int)
121                assignType2->get_parameters().push_back( dstParam->clone() );
122                BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt);
123                ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 );
124                assignType2->get_parameters().push_back( srcParam2 );
125
126                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
127                // because each unit generates copies of the default routines for each aggregate.
128
129                // since there is no definition, these should not be inline
130                // make these intrinsic so that the code generator does not make use of them
131                FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false );
132                assignDecl->fixUniqueId();
133                FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false );
134                assignDecl2->fixUniqueId();
135
136                // these should be built in the same way that the prelude
137                // functions are, so build a list containing the prototypes
138                // and allow MakeLibCfa to autogenerate the bodies.
139                std::list< Declaration * > assigns;
140                assigns.push_back( assignDecl );
141                assigns.push_back( assignDecl2 );
142
143                LibCfa::makeLibCfa( assigns );
144
145                // need to remove the prototypes, since this may be nested in a routine
146                for (int start = 0, end = assigns.size()/2; start < end; start++) {
147                        delete assigns.front();
148                        assigns.pop_front();
149                } // for
150
151                declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
152        }
153
154        /// Clones a reference type, replacing any parameters it may have with a clone of the provided list
155        template< typename GenericInstType >
156        GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {
157                GenericInstType *clone = refType->clone();
158                clone->get_parameters().clear();
159                cloneAll( params, clone->get_parameters() );
160                return clone;
161        }
162
163        /// Creates a new type decl that's the same as src, but renamed and with only the ?=? assertion (for complete types only)
164        TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) {
165                TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() );
166
167                if ( src->get_kind() == TypeDecl::Any ) {
168                        // just include assignment operator assertion
169                        TypeInstType *assignParamType = new TypeInstType( Type::Qualifiers(), name, dst );
170                        FunctionType *assignFunctionType = new FunctionType( Type::Qualifiers(), false );
171                        assignFunctionType->get_returnVals().push_back(
172                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType->clone(), 0 ) );
173                        assignFunctionType->get_parameters().push_back(
174                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), assignParamType->clone() ), 0 ) );
175                        assignFunctionType->get_parameters().push_back(
176                                new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType, 0 ) );
177                        FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignFunctionType, 0, false, false );
178                        dst->get_assertions().push_back( assignAssert );
179                }
180
181                return dst;
182        }
183
184        void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
185                if ( isGeneric ) {
186                        // rewrite member type in terms of the type variables on this operator
187                        field = field->clone();
188                        genericSubs.apply( field );
189                }
190
191                ObjectDecl * returnVal = NULL;
192                if ( ! func->get_functionType()->get_returnVals().empty() ) {
193                        returnVal = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_returnVals().front() );
194                }
195
196                // assign to destination (and return value if generic)
197                if ( ArrayType *array = dynamic_cast< ArrayType * >( field->get_type() ) ) {
198                        UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
199                        derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
200                        Expression *dstselect = new MemberExpr( field, derefExpr );
201
202                        makeArrayFunction( src, dstselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
203                        if ( isGeneric && returnVal ) {
204                                UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) );
205                                derefRet->get_args().push_back( new VariableExpr( returnVal ) );
206                                Expression *retselect = new MemberExpr( field, derefRet );
207
208                                makeArrayFunction( src, retselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
209                        }
210                } else {
211                        makeScalarFunction( src, dstParam, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
212                        if ( isGeneric && returnVal ) makeScalarFunction( src, returnVal, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
213                } // if
214        }
215
216        template<typename Iterator>
217        void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
218                for ( ; member != end; ++member ) {
219                        if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
220                                // query the type qualifiers of this field and skip assigning it if it is marked const.
221                                // If it is an array type, we need to strip off the array layers to find its qualifiers.
222                                Type * type = field->get_type();
223                                while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
224                                        type = at->get_base();
225                                }
226
227                                if ( type->get_qualifiers().isConst ) {
228                                        // don't assign const members
229                                        continue;
230                                }
231
232                                assert( ! func->get_functionType()->get_parameters().empty() );
233                                ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
234                                ObjectDecl * srcParam = NULL;
235                                if ( func->get_functionType()->get_parameters().size() == 2 ) {
236                                        srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
237                                }
238                                // srcParam may be NULL, in which case we have default ctor/dtor
239                                assert( dstParam );
240
241                                Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL;
242                                makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, forward );
243                        } // if
244                } // for
245        } // makeStructFunctionBody
246
247        /// generate the body of a constructor which takes parameters that match fields, e.g.
248        /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
249        template<typename Iterator>
250        void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric ) {
251                FunctionType * ftype = func->get_functionType();
252                std::list<DeclarationWithType*> & params = ftype->get_parameters();
253                assert( params.size() >= 2 );  // should not call this function for default ctor, etc.
254
255                // skip 'this' parameter
256                ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() );
257                assert( dstParam );
258                std::list<DeclarationWithType*>::iterator parameter = params.begin()+1;
259                for ( ; member != end; ++member ) {
260                        if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) {
261                                if ( parameter != params.end() ) {
262                                        // matching parameter, initialize field with copy ctor
263                                        std::cerr << "matching parameter - copy ctor" << std::endl;
264                                        Expression *srcselect = new VariableExpr(*parameter);
265                                        makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric );
266                                        ++parameter;
267                                } else {
268                                        std::cerr << "no matching parameter - default ctor" << std::endl;
269                                        // no matching parameter, initialize field with default ctor
270                                        makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isGeneric );
271                                }
272                        }
273                }
274        }
275
276        void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
277                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
278
279                // Make function polymorphic in same parameters as generic struct, if applicable
280                bool isGeneric = false;  // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)
281                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
282                std::list< Expression* > structParams;  // List of matching parameters to put on types
283                TypeSubstitution genericSubs; // Substitutions to make to member types of struct
284                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
285                        isGeneric = true;
286                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
287                        assignType->get_forall().push_back( typeParam );
288                        TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam );
289                        genericSubs.add( (*param)->get_name(), newParamType );
290                        structParams.push_back( new TypeExpr( newParamType ) );
291                }
292
293                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
294                assignType->get_parameters().push_back( dstParam );
295
296                // void ?{}(T *); void ^?{}(T *);
297                FunctionType *ctorType = assignType->clone();
298                FunctionType *dtorType = assignType->clone();
299
300                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
301                assignType->get_parameters().push_back( srcParam );
302
303                // void ?{}(T *, T);
304                FunctionType *copyCtorType = assignType->clone();
305
306                // T ?=?(T *, T);
307                ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
308                assignType->get_returnVals().push_back( returnVal );
309
310                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
311                // because each unit generates copies of the default routines for each aggregate.
312                FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
313                FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
314                FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, new CompoundStmt( noLabels ), true, false );
315                FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
316                assignDecl->fixUniqueId();
317                ctorDecl->fixUniqueId();
318                copyCtorDecl->fixUniqueId();
319                dtorDecl->fixUniqueId();
320
321                // create constructors which take each member type as a parameter.
322                // we might be able to eventually collapse this into a single function
323                // which uses default parameters
324                std::list<Declaration *> memCtors;
325                FunctionType * memCtorType = ctorType->clone();
326                for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
327                        DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
328                        assert( member );
329                        memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
330                        FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType->clone(), new CompoundStmt( noLabels ), true, false );
331                        ctor->fixUniqueId();
332                        makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isGeneric );
333                        memCtors.push_back( ctor );
334                }
335                delete memCtorType;
336
337                // generate appropriate calls to member ctor, assignment
338                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isGeneric );
339                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isGeneric );
340                makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isGeneric );
341                // needs to do everything in reverse, so pass "forward" as false
342                makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isGeneric, false );
343
344                if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
345
346                declsToAdd.push_back( assignDecl );
347                declsToAdd.push_back( ctorDecl );
348                declsToAdd.push_back( copyCtorDecl );
349                declsToAdd.push_back( dtorDecl );
350                declsToAdd.splice( declsToAdd.end(), memCtors );
351        }
352
353        void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
354                FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
355
356                // Make function polymorphic in same parameters as generic union, if applicable
357                bool isGeneric = false;  // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
358                std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
359                std::list< Expression* > unionParams;  // List of matching parameters to put on types
360                for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
361                        isGeneric = true;
362                        TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
363                        assignType->get_forall().push_back( typeParam );
364                        unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
365                }
366
367                ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
368                assignType->get_parameters().push_back( dstParam );
369
370                // default ctor/dtor need only first parameter
371                FunctionType * ctorType = assignType->clone();
372                FunctionType * dtorType = assignType->clone();
373
374                ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
375                assignType->get_parameters().push_back( srcParam );
376
377                // copy ctor needs both parameters
378                FunctionType * copyCtorType = assignType->clone();
379
380                // assignment needs both and return value
381                ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
382                assignType->get_returnVals().push_back( returnVal );
383
384                // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
385                // because each unit generates copies of the default routines for each aggregate.
386                FunctionDecl *assignDecl = new FunctionDecl( "?=?",  functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
387                FunctionDecl *ctorDecl = new FunctionDecl( "?{}",  functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
388                FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}",  functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, NULL, true, false );
389                FunctionDecl *dtorDecl = new FunctionDecl( "^?{}",  functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
390
391                assignDecl->fixUniqueId();
392                ctorDecl->fixUniqueId();
393                copyCtorDecl->fixUniqueId();
394                dtorDecl->fixUniqueId();
395
396                makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
397                if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
398
399                if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
400
401                // body of assignment and copy ctor is the same
402                copyCtorDecl->set_statements( assignDecl->get_statements()->clone() );
403
404                declsToAdd.push_back( assignDecl );
405                declsToAdd.push_back( ctorDecl );
406                declsToAdd.push_back( copyCtorDecl );
407                declsToAdd.push_back( dtorDecl );
408        }
409
410        void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
411                if ( ! enumDecl->get_members().empty() ) {
412                        EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
413                        // enumInst->set_baseEnum( enumDecl );
414                        // declsToAdd.push_back(
415                        makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );
416                }
417        }
418
419        void AutogenerateRoutines::visit( StructDecl *structDecl ) {
420                if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
421                        StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
422                        structInst.set_baseStruct( structDecl );
423                        makeStructFunctions( structDecl, &structInst, functionNesting, declsToAdd );
424                        structsDone.insert( structDecl->get_name() );
425                } // if
426        }
427
428        void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
429                if ( ! unionDecl->get_members().empty() ) {
430                        UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
431                        unionInst.set_baseUnion( unionDecl );
432                        makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAdd );
433                } // if
434        }
435
436        void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
437                CompoundStmt *stmts = 0;
438                TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
439                typeInst->set_baseType( typeDecl );
440                ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
441                ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
442                if ( typeDecl->get_base() ) {
443                        stmts = new CompoundStmt( std::list< Label >() );
444                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
445                        assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
446                        assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
447                        stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
448                } // if
449                FunctionType *type = new FunctionType( Type::Qualifiers(), false );
450                type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
451                type->get_parameters().push_back( dst );
452                type->get_parameters().push_back( src );
453                FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
454                declsToAdd.push_back( func );
455        }
456
457        void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
458                for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
459                        statements.insert( i, new DeclStmt( noLabels, *decl ) );
460                } // for
461                declsToAdd.clear();
462        }
463
464        void AutogenerateRoutines::visit( FunctionType *) {
465                // ensure that we don't add assignment ops for types defined as part of the function
466        }
467
468        void AutogenerateRoutines::visit( PointerType *) {
469                // ensure that we don't add assignment ops for types defined as part of the pointer
470        }
471
472        void AutogenerateRoutines::visit( ContextDecl *) {
473                // ensure that we don't add assignment ops for types defined as part of the context
474        }
475
476        template< typename StmtClass >
477        inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
478                std::set< std::string > oldStructs = structsDone;
479                addVisit( stmt, *this );
480                structsDone = oldStructs;
481        }
482
483        void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
484                maybeAccept( functionDecl->get_functionType(), *this );
485                acceptAll( functionDecl->get_oldDecls(), *this );
486                functionNesting += 1;
487                maybeAccept( functionDecl->get_statements(), *this );
488                functionNesting -= 1;
489        }
490
491        void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
492                visitStatement( compoundStmt );
493        }
494
495        void AutogenerateRoutines::visit( IfStmt *ifStmt ) {
496                visitStatement( ifStmt );
497        }
498
499        void AutogenerateRoutines::visit( WhileStmt *whileStmt ) {
500                visitStatement( whileStmt );
501        }
502
503        void AutogenerateRoutines::visit( ForStmt *forStmt ) {
504                visitStatement( forStmt );
505        }
506
507        void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
508                visitStatement( switchStmt );
509        }
510
511        void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {
512                visitStatement( switchStmt );
513        }
514
515        void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
516                visitStatement( caseStmt );
517        }
518
519        void AutogenerateRoutines::visit( CatchStmt *cathStmt ) {
520                visitStatement( cathStmt );
521        }
522
523} // SymTab
Note: See TracBrowser for help on using the repository browser.