source: src/SymTab/Autogen.cc@ 70a06f6

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 70a06f6 was 70a06f6, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/AddVisit.h
src/SymTab/Validate.cc
src/SynTree/Expression.cc
src/SynTree/Expression.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/iostream.c

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