source: src/SymTab/Autogen.cc@ fbfde843

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 with_gc
Last change on this file since fbfde843 was d8ba086, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

add ?{}, ?{} implicit otype assertions to autogenerated generic type functions

  • Property mode set to 100644
File size: 26.1 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 : Fri Apr 29 13:11:04 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 bool isUnnamedBitfield( ObjectDecl * obj ) {
62 return obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL;
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 ( isUnnamedBitfield( obj ) ) 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 makeEnumFunctions( 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 ?=?, ?{} (default and copy), and ^?{} assertions (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 TypeInstType *opParamType = new TypeInstType( Type::Qualifiers(), name, dst );
169 FunctionType *opFunctionType = new FunctionType( Type::Qualifiers(), false );
170 opFunctionType->get_parameters().push_back(
171 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), opParamType->clone() ), 0 ) );
172 FunctionDecl *ctorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
173 FunctionDecl *dtorAssert = new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
174
175 opFunctionType->get_parameters().push_back(
176 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType, 0 ) );
177 FunctionDecl *copyCtorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
178
179 opFunctionType->get_returnVals().push_back(
180 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType->clone(), 0 ) );
181 FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType, 0, false, false );
182
183 dst->get_assertions().push_back( assignAssert );
184 dst->get_assertions().push_back( ctorAssert );
185 dst->get_assertions().push_back( dtorAssert );
186 dst->get_assertions().push_back( copyCtorAssert );
187 }
188
189 return dst;
190 }
191
192 void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
193 if ( isGeneric ) {
194 // rewrite member type in terms of the type variables on this operator
195 field = field->clone();
196 genericSubs.apply( field );
197
198 if ( src ) {
199 genericSubs.apply( src );
200 }
201 }
202
203 ObjectDecl * returnVal = NULL;
204 if ( ! func->get_functionType()->get_returnVals().empty() ) {
205 returnVal = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_returnVals().front() );
206 }
207
208 // assign to destination (and return value if generic)
209 if ( ArrayType *array = dynamic_cast< ArrayType * >( field->get_type() ) ) {
210 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
211 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
212 Expression *dstselect = new MemberExpr( field, derefExpr );
213
214 makeArrayFunction( src, dstselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
215 if ( isGeneric && returnVal ) {
216 UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) );
217 derefRet->get_args().push_back( new VariableExpr( returnVal ) );
218 Expression *retselect = new MemberExpr( field, derefRet );
219
220 makeArrayFunction( src, retselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
221 }
222 } else {
223 makeScalarFunction( src, dstParam, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
224 if ( isGeneric && returnVal ) makeScalarFunction( src, returnVal, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
225 } // if
226 }
227
228 template<typename Iterator>
229 void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
230 for ( ; member != end; ++member ) {
231 if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
232 // query the type qualifiers of this field and skip assigning it if it is marked const.
233 // If it is an array type, we need to strip off the array layers to find its qualifiers.
234 Type * type = field->get_type();
235 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
236 type = at->get_base();
237 }
238
239 if ( type->get_qualifiers().isConst ) {
240 // don't assign const members
241 continue;
242 }
243
244 assert( ! func->get_functionType()->get_parameters().empty() );
245 ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
246 ObjectDecl * srcParam = NULL;
247 if ( func->get_functionType()->get_parameters().size() == 2 ) {
248 srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
249 }
250 // srcParam may be NULL, in which case we have default ctor/dtor
251 assert( dstParam );
252
253 Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL;
254 makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, forward );
255 } // if
256 } // for
257 } // makeStructFunctionBody
258
259 /// generate the body of a constructor which takes parameters that match fields, e.g.
260 /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
261 template<typename Iterator>
262 void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric ) {
263 FunctionType * ftype = func->get_functionType();
264 std::list<DeclarationWithType*> & params = ftype->get_parameters();
265 assert( params.size() >= 2 ); // should not call this function for default ctor, etc.
266
267 // skip 'this' parameter
268 ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() );
269 assert( dstParam );
270 std::list<DeclarationWithType*>::iterator parameter = params.begin()+1;
271 for ( ; member != end; ++member ) {
272 if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) {
273 if ( parameter != params.end() ) {
274 // matching parameter, initialize field with copy ctor
275 Expression *srcselect = new VariableExpr(*parameter);
276 makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric );
277 ++parameter;
278 } else {
279 // no matching parameter, initialize field with default ctor
280 makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isGeneric );
281 }
282 }
283 }
284 }
285
286 void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
287 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
288
289 // Make function polymorphic in same parameters as generic struct, if applicable
290 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)
291 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
292 std::list< Expression* > structParams; // List of matching parameters to put on types
293 TypeSubstitution genericSubs; // Substitutions to make to member types of struct
294 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
295 isGeneric = true;
296 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
297 assignType->get_forall().push_back( typeParam );
298 TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam );
299 genericSubs.add( (*param)->get_name(), newParamType );
300 structParams.push_back( new TypeExpr( newParamType ) );
301 }
302
303 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
304 assignType->get_parameters().push_back( dstParam );
305
306 // void ?{}(T *); void ^?{}(T *);
307 FunctionType *ctorType = assignType->clone();
308 FunctionType *dtorType = assignType->clone();
309
310 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
311 assignType->get_parameters().push_back( srcParam );
312
313 // void ?{}(T *, T);
314 FunctionType *copyCtorType = assignType->clone();
315
316 // T ?=?(T *, T);
317 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
318 assignType->get_returnVals().push_back( returnVal );
319
320 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
321 // because each unit generates copies of the default routines for each aggregate.
322 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
323 FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
324 FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, new CompoundStmt( noLabels ), true, false );
325 FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
326 assignDecl->fixUniqueId();
327 ctorDecl->fixUniqueId();
328 copyCtorDecl->fixUniqueId();
329 dtorDecl->fixUniqueId();
330
331 // create constructors which take each member type as a parameter.
332 // for example, for struct A { int x, y; }; generate
333 // void ?{}(A *, int) and void ?{}(A *, int, int)
334 std::list<Declaration *> memCtors;
335 FunctionType * memCtorType = ctorType->clone();
336 for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
337 DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
338 assert( member );
339 if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
340 // don't make a function whose parameter is an unnamed bitfield
341 continue;
342 }
343 memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
344 FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType->clone(), new CompoundStmt( noLabels ), true, false );
345 ctor->fixUniqueId();
346 makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isGeneric );
347 memCtors.push_back( ctor );
348 }
349 delete memCtorType;
350
351 // generate appropriate calls to member ctor, assignment
352 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isGeneric );
353 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isGeneric );
354 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isGeneric );
355 // needs to do everything in reverse, so pass "forward" as false
356 makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isGeneric, false );
357
358 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
359
360 declsToAdd.push_back( assignDecl );
361 declsToAdd.push_back( ctorDecl );
362 declsToAdd.push_back( copyCtorDecl );
363 declsToAdd.push_back( dtorDecl );
364 declsToAdd.splice( declsToAdd.end(), memCtors );
365 }
366
367 void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
368 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
369
370 // Make function polymorphic in same parameters as generic union, if applicable
371 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
372 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
373 std::list< Expression* > unionParams; // List of matching parameters to put on types
374 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
375 isGeneric = true;
376 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
377 assignType->get_forall().push_back( typeParam );
378 unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
379 }
380
381 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
382 assignType->get_parameters().push_back( dstParam );
383
384 // default ctor/dtor need only first parameter
385 FunctionType * ctorType = assignType->clone();
386 FunctionType * dtorType = assignType->clone();
387
388 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
389 assignType->get_parameters().push_back( srcParam );
390
391 // copy ctor needs both parameters
392 FunctionType * copyCtorType = assignType->clone();
393
394 // assignment needs both and return value
395 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
396 assignType->get_returnVals().push_back( returnVal );
397
398 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
399 // because each unit generates copies of the default routines for each aggregate.
400 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
401 FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
402 FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, NULL, true, false );
403 FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
404
405 assignDecl->fixUniqueId();
406 ctorDecl->fixUniqueId();
407 copyCtorDecl->fixUniqueId();
408 dtorDecl->fixUniqueId();
409
410 makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
411 if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
412
413 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
414
415 // body of assignment and copy ctor is the same
416 copyCtorDecl->set_statements( assignDecl->get_statements()->clone() );
417
418 declsToAdd.push_back( assignDecl );
419 declsToAdd.push_back( ctorDecl );
420 declsToAdd.push_back( copyCtorDecl );
421 declsToAdd.push_back( dtorDecl );
422 }
423
424 void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
425 if ( ! enumDecl->get_members().empty() ) {
426 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
427 // enumInst->set_baseEnum( enumDecl );
428 // declsToAdd.push_back(
429 makeEnumFunctions( enumDecl, enumInst, functionNesting, declsToAdd );
430 }
431 }
432
433 void AutogenerateRoutines::visit( StructDecl *structDecl ) {
434 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
435 StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
436 structInst.set_baseStruct( structDecl );
437 makeStructFunctions( structDecl, &structInst, functionNesting, declsToAdd );
438 structsDone.insert( structDecl->get_name() );
439 } // if
440 }
441
442 void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
443 if ( ! unionDecl->get_members().empty() ) {
444 UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
445 unionInst.set_baseUnion( unionDecl );
446 makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAdd );
447 } // if
448 }
449
450 void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
451 CompoundStmt *stmts = 0;
452 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
453 typeInst->set_baseType( typeDecl );
454 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
455 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
456 if ( typeDecl->get_base() ) {
457 stmts = new CompoundStmt( std::list< Label >() );
458 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
459 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
460 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
461 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
462 } // if
463 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
464 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
465 type->get_parameters().push_back( dst );
466 type->get_parameters().push_back( src );
467 FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
468 declsToAdd.push_back( func );
469 }
470
471 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
472 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
473 statements.insert( i, new DeclStmt( noLabels, *decl ) );
474 } // for
475 declsToAdd.clear();
476 }
477
478 void AutogenerateRoutines::visit( FunctionType *) {
479 // ensure that we don't add assignment ops for types defined as part of the function
480 }
481
482 void AutogenerateRoutines::visit( PointerType *) {
483 // ensure that we don't add assignment ops for types defined as part of the pointer
484 }
485
486 void AutogenerateRoutines::visit( TraitDecl *) {
487 // ensure that we don't add assignment ops for types defined as part of the trait
488 }
489
490 template< typename StmtClass >
491 inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
492 std::set< std::string > oldStructs = structsDone;
493 addVisit( stmt, *this );
494 structsDone = oldStructs;
495 }
496
497 void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
498 maybeAccept( functionDecl->get_functionType(), *this );
499 acceptAll( functionDecl->get_oldDecls(), *this );
500 functionNesting += 1;
501 maybeAccept( functionDecl->get_statements(), *this );
502 functionNesting -= 1;
503 }
504
505 void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
506 visitStatement( compoundStmt );
507 }
508
509 void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
510 visitStatement( switchStmt );
511 }
512
513 void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {
514 visitStatement( switchStmt );
515 }
516
517 // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
518 // visitStatement( caseStmt );
519 // }
520} // SymTab
Note: See TracBrowser for help on using the repository browser.