source: src/SymTab/Autogen.cc@ 7d5e243

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay 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 7d5e243 was cad355a, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

generated field constructors for structs with const members no longer cause an error, other generated constructors and destructors construct and destruct const members

  • Property mode set to 100644
File size: 29.4 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 May 26 14:14:09 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 Statement * callStmt = new ExprStmt( noLabels, fExpr );
87 if ( (fname == "?{}" || fname == "^?{}") && ( !obj || ( obj && obj->get_bitfieldWidth() == NULL ) ) ) {
88 // implicitly generated ctor/dtor calls should be wrapped
89 // so that later passes are aware they were generated.
90 // xxx - don't mark as an implicit ctor/dtor if obj is a bitfield,
91 // because this causes the address to be taken at codegen, which is illegal in C.
92 callStmt = new ImplicitCtorDtorStmt( callStmt );
93 }
94 *out++ = callStmt;
95 }
96
97 template< typename OutputIterator >
98 void makeUnionFieldsAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, UnionInstType *unionType, OutputIterator out ) {
99 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
100 copy->get_args().push_back( new VariableExpr( dstParam ) );
101 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
102 copy->get_args().push_back( new SizeofExpr( unionType ) );
103
104 *out++ = new ExprStmt( noLabels, copy );
105 }
106
107 //E ?=?(E volatile*, int),
108 // ?=?(E _Atomic volatile*, int);
109 void makeEnumFunctions( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
110 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
111
112 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType ), 0 );
113 assignType->get_parameters().push_back( dstParam );
114
115 // void ?{}(E *); void ^?{}(E *);
116 FunctionType * ctorType = assignType->clone();
117 FunctionType * dtorType = assignType->clone();
118
119 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
120 assignType->get_parameters().push_back( srcParam );
121 // void ?{}(E *, E);
122 FunctionType *copyCtorType = assignType->clone();
123
124 // T ?=?(E *, E);
125 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
126 assignType->get_returnVals().push_back( returnVal );
127
128 // xxx - should we also generate void ?{}(E *, int) and E ?{}(E *, E)?
129 // right now these cases work, but that might change.
130
131 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
132 // because each unit generates copies of the default routines for each aggregate.
133 // xxx - Temporary: make these functions intrinsic so they codegen as C assignment.
134 // Really they're something of a cross between instrinsic and autogen, so should
135 // probably make a new linkage type
136 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, new CompoundStmt( noLabels ), true, false );
137 FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, ctorType, new CompoundStmt( noLabels ), true, false );
138 FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, copyCtorType, new CompoundStmt( noLabels ), true, false );
139 FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, dtorType, new CompoundStmt( noLabels ), true, false );
140 assignDecl->fixUniqueId();
141 ctorDecl->fixUniqueId();
142 copyCtorDecl->fixUniqueId();
143 dtorDecl->fixUniqueId();
144
145 // enum copy construct and assignment is just C-style assignment.
146 // this looks like a bad recursive call, but code gen will turn it into
147 // a C-style assignment.
148 // This happens before function pointer type conversion, so need to do it manually here
149 VariableExpr * assignVarExpr = new VariableExpr( assignDecl );
150 Type *& assignVarExprType = assignVarExpr->get_results().front();
151 assignVarExprType = new PointerType( Type::Qualifiers(), assignVarExprType );
152 ApplicationExpr * assignExpr = new ApplicationExpr( assignVarExpr );
153 assignExpr->get_args().push_back( new VariableExpr( dstParam ) );
154 assignExpr->get_args().push_back( new VariableExpr( srcParam ) );
155
156 // body is either return stmt or expr stmt
157 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, assignExpr ) );
158 copyCtorDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, assignExpr->clone() ) );
159
160 declsToAdd.push_back( assignDecl );
161 declsToAdd.push_back( ctorDecl );
162 declsToAdd.push_back( copyCtorDecl );
163 declsToAdd.push_back( dtorDecl );
164 }
165
166 /// Clones a reference type, replacing any parameters it may have with a clone of the provided list
167 template< typename GenericInstType >
168 GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {
169 GenericInstType *clone = refType->clone();
170 clone->get_parameters().clear();
171 cloneAll( params, clone->get_parameters() );
172 return clone;
173 }
174
175 /// 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)
176 TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) {
177 // TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() );
178
179 // if ( src->get_kind() == TypeDecl::Any ) {
180 // TypeInstType *opParamType = new TypeInstType( Type::Qualifiers(), name, dst );
181 // FunctionType *opFunctionType = new FunctionType( Type::Qualifiers(), false );
182 // opFunctionType->get_parameters().push_back(
183 // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), opParamType->clone() ), 0 ) );
184 // FunctionDecl *ctorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
185 // FunctionDecl *dtorAssert = new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
186
187 // opFunctionType->get_parameters().push_back(
188 // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType, 0 ) );
189 // FunctionDecl *copyCtorAssert = new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType->clone(), 0, false, false );
190
191 // opFunctionType->get_returnVals().push_back(
192 // new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, opParamType->clone(), 0 ) );
193 // FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, opFunctionType, 0, false, false );
194
195
196 // dst->get_assertions().push_back( assignAssert );
197 // dst->get_assertions().push_back( ctorAssert );
198 // dst->get_assertions().push_back( dtorAssert );
199 // dst->get_assertions().push_back( copyCtorAssert );
200 // }
201
202 TypeDecl *dst = new TypeDecl( src->get_name(), src->get_storageClass(), 0, src->get_kind() );
203 cloneAll(src->get_assertions(), dst->get_assertions());
204 return dst;
205 }
206
207 void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
208 if ( isGeneric ) {
209 // rewrite member type in terms of the type variables on this operator
210 field = field->clone();
211 genericSubs.apply( field );
212
213 if ( src ) {
214 genericSubs.apply( src );
215 }
216 }
217
218 ObjectDecl * returnVal = NULL;
219 if ( ! func->get_functionType()->get_returnVals().empty() ) {
220 returnVal = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_returnVals().front() );
221 }
222
223 // assign to destination (and return value if generic)
224 if ( ArrayType *array = dynamic_cast< ArrayType * >( field->get_type() ) ) {
225 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
226 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
227 Expression *dstselect = new MemberExpr( field, derefExpr );
228
229 makeArrayFunction( src, dstselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
230 if ( isGeneric && returnVal ) {
231 UntypedExpr *derefRet = new UntypedExpr( new NameExpr( "*?" ) );
232 derefRet->get_args().push_back( new VariableExpr( returnVal ) );
233 Expression *retselect = new MemberExpr( field, derefRet );
234
235 makeArrayFunction( src, retselect, array, func->get_name(), back_inserter( func->get_statements()->get_kids() ), forward );
236 }
237 } else {
238 makeScalarFunction( src, dstParam, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
239 if ( isGeneric && returnVal ) makeScalarFunction( src, returnVal, field, func->get_name(), back_inserter( func->get_statements()->get_kids() ) );
240 } // if
241 }
242
243 template<typename Iterator>
244 void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric, bool forward = true ) {
245 for ( ; member != end; ++member ) {
246 if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
247 // query the type qualifiers of this field and skip assigning it if it is marked const.
248 // If it is an array type, we need to strip off the array layers to find its qualifiers.
249 Type * type = field->get_type();
250 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
251 type = at->get_base();
252 }
253
254 if ( type->get_qualifiers().isConst && func->get_name() == "?=?" ) {
255 // don't assign const members, but do construct/destruct
256 continue;
257 }
258
259 if ( field->get_name() == "" ) {
260 // don't assign to anonymous members
261 // xxx - this is a temporary fix. Anonymous members tie into
262 // our inheritance model. I think the correct way to handle this is to
263 // cast the structure to the type of the member and let the resolver
264 // figure out whether it's valid and have a pass afterwards that fixes
265 // the assignment to use pointer arithmetic with the offset of the
266 // member, much like how generic type members are handled.
267 continue;
268 }
269
270 assert( ! func->get_functionType()->get_parameters().empty() );
271 ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
272 ObjectDecl * srcParam = NULL;
273 if ( func->get_functionType()->get_parameters().size() == 2 ) {
274 srcParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().back() );
275 }
276 // srcParam may be NULL, in which case we have default ctor/dtor
277 assert( dstParam );
278
279 Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : NULL;
280 makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric, forward );
281 } // if
282 } // for
283 } // makeStructFunctionBody
284
285 /// generate the body of a constructor which takes parameters that match fields, e.g.
286 /// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
287 template<typename Iterator>
288 void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func, TypeSubstitution & genericSubs, bool isGeneric ) {
289 FunctionType * ftype = func->get_functionType();
290 std::list<DeclarationWithType*> & params = ftype->get_parameters();
291 assert( params.size() >= 2 ); // should not call this function for default ctor, etc.
292
293 // skip 'this' parameter
294 ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( params.front() );
295 assert( dstParam );
296 std::list<DeclarationWithType*>::iterator parameter = params.begin()+1;
297 for ( ; member != end; ++member ) {
298 if ( DeclarationWithType * field = dynamic_cast<DeclarationWithType*>( *member ) ) {
299 if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
300 // don't make a function whose parameter is an unnamed bitfield
301 continue;
302 } else if ( field->get_name() == "" ) {
303 // don't assign to anonymous members
304 // xxx - this is a temporary fix. Anonymous members tie into
305 // our inheritance model. I think the correct way to handle this is to
306 // cast the structure to the type of the member and let the resolver
307 // figure out whether it's valid and have a pass afterwards that fixes
308 // the assignment to use pointer arithmetic with the offset of the
309 // member, much like how generic type members are handled.
310 continue;
311 } else if ( parameter != params.end() ) {
312 // matching parameter, initialize field with copy ctor
313 Expression *srcselect = new VariableExpr(*parameter);
314 makeStructMemberOp( dstParam, srcselect, field, func, genericSubs, isGeneric );
315 ++parameter;
316 } else {
317 // no matching parameter, initialize field with default ctor
318 makeStructMemberOp( dstParam, NULL, field, func, genericSubs, isGeneric );
319 }
320 }
321 }
322 }
323
324 void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
325 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
326
327 // Make function polymorphic in same parameters as generic struct, if applicable
328 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)
329 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
330 std::list< Expression* > structParams; // List of matching parameters to put on types
331 TypeSubstitution genericSubs; // Substitutions to make to member types of struct
332 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
333 isGeneric = true;
334 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
335 assignType->get_forall().push_back( typeParam );
336 TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam );
337 genericSubs.add( (*param)->get_name(), newParamType );
338 structParams.push_back( new TypeExpr( newParamType ) );
339 }
340
341 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
342 assignType->get_parameters().push_back( dstParam );
343
344 // void ?{}(T *); void ^?{}(T *);
345 FunctionType *ctorType = assignType->clone();
346 FunctionType *dtorType = assignType->clone();
347
348 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
349 assignType->get_parameters().push_back( srcParam );
350
351 // void ?{}(T *, T);
352 FunctionType *copyCtorType = assignType->clone();
353
354 // T ?=?(T *, T);
355 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
356 assignType->get_returnVals().push_back( returnVal );
357
358 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
359 // because each unit generates copies of the default routines for each aggregate.
360 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
361 FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
362 FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, new CompoundStmt( noLabels ), true, false );
363 FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
364 assignDecl->fixUniqueId();
365 ctorDecl->fixUniqueId();
366 copyCtorDecl->fixUniqueId();
367 dtorDecl->fixUniqueId();
368
369 // create constructors which take each member type as a parameter.
370 // for example, for struct A { int x, y; }; generate
371 // void ?{}(A *, int) and void ?{}(A *, int, int)
372 std::list<Declaration *> memCtors;
373 FunctionType * memCtorType = ctorType->clone();
374 for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
375 DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
376 assert( member );
377 if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
378 // don't make a function whose parameter is an unnamed bitfield
379 continue;
380 } else if ( member->get_name() == "" ) {
381 // don't assign to anonymous members
382 // xxx - this is a temporary fix. Anonymous members tie into
383 // our inheritance model. I think the correct way to handle this is to
384 // cast the structure to the type of the member and let the resolver
385 // figure out whether it's valid and have a pass afterwards that fixes
386 // the assignment to use pointer arithmetic with the offset of the
387 // member, much like how generic type members are handled.
388 continue;
389 }
390 memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
391 FunctionDecl * ctor = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, memCtorType->clone(), new CompoundStmt( noLabels ), true, false );
392 ctor->fixUniqueId();
393 makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, genericSubs, isGeneric );
394 memCtors.push_back( ctor );
395 }
396 delete memCtorType;
397
398 // generate appropriate calls to member ctor, assignment
399 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, genericSubs, isGeneric );
400 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, genericSubs, isGeneric );
401 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, genericSubs, isGeneric );
402 // needs to do everything in reverse, so pass "forward" as false
403 makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, genericSubs, isGeneric, false );
404
405 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
406
407 declsToAdd.push_back( assignDecl );
408 declsToAdd.push_back( ctorDecl );
409 declsToAdd.push_back( copyCtorDecl );
410 declsToAdd.push_back( dtorDecl );
411 declsToAdd.splice( declsToAdd.end(), memCtors );
412 }
413
414 void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
415 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
416
417 // Make function polymorphic in same parameters as generic union, if applicable
418 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
419 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
420 std::list< Expression* > unionParams; // List of matching parameters to put on types
421 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
422 isGeneric = true;
423 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );
424 assignType->get_forall().push_back( typeParam );
425 unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
426 }
427
428 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
429 assignType->get_parameters().push_back( dstParam );
430
431 // default ctor/dtor need only first parameter
432 FunctionType * ctorType = assignType->clone();
433 FunctionType * dtorType = assignType->clone();
434
435 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
436 assignType->get_parameters().push_back( srcParam );
437
438 // copy ctor needs both parameters
439 FunctionType * copyCtorType = assignType->clone();
440
441 // assignment needs both and return value
442 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
443 assignType->get_returnVals().push_back( returnVal );
444
445 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
446 // because each unit generates copies of the default routines for each aggregate.
447 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
448 FunctionDecl *ctorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, ctorType, new CompoundStmt( noLabels ), true, false );
449 FunctionDecl *copyCtorDecl = new FunctionDecl( "?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, copyCtorType, NULL, true, false );
450 FunctionDecl *dtorDecl = new FunctionDecl( "^?{}", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, dtorType, new CompoundStmt( noLabels ), true, false );
451
452 assignDecl->fixUniqueId();
453 ctorDecl->fixUniqueId();
454 copyCtorDecl->fixUniqueId();
455 dtorDecl->fixUniqueId();
456
457 makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
458 if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
459
460 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
461
462 // body of assignment and copy ctor is the same
463 copyCtorDecl->set_statements( assignDecl->get_statements()->clone() );
464
465 declsToAdd.push_back( assignDecl );
466 declsToAdd.push_back( ctorDecl );
467 declsToAdd.push_back( copyCtorDecl );
468 declsToAdd.push_back( dtorDecl );
469 }
470
471 void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
472 if ( ! enumDecl->get_members().empty() ) {
473 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
474 // enumInst->set_baseEnum( enumDecl );
475 // declsToAdd.push_back(
476 makeEnumFunctions( enumDecl, enumInst, functionNesting, declsToAdd );
477 }
478 }
479
480 void AutogenerateRoutines::visit( StructDecl *structDecl ) {
481 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
482 StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
483 structInst.set_baseStruct( structDecl );
484 makeStructFunctions( structDecl, &structInst, functionNesting, declsToAdd );
485 structsDone.insert( structDecl->get_name() );
486 } // if
487 }
488
489 void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
490 if ( ! unionDecl->get_members().empty() ) {
491 UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
492 unionInst.set_baseUnion( unionDecl );
493 makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAdd );
494 } // if
495 }
496
497 void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
498 CompoundStmt *stmts = 0;
499 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
500 typeInst->set_baseType( typeDecl );
501 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
502 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
503 if ( typeDecl->get_base() ) {
504 // xxx - generate ctor/dtors for typedecls, e.g.
505 // otype T = int *;
506 stmts = new CompoundStmt( std::list< Label >() );
507 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
508 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
509 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
510 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
511 } // if
512 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
513 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
514 type->get_parameters().push_back( dst );
515 type->get_parameters().push_back( src );
516 FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, true, false );
517 declsToAdd.push_back( func );
518 }
519
520 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
521 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
522 statements.insert( i, new DeclStmt( noLabels, *decl ) );
523 } // for
524 declsToAdd.clear();
525 }
526
527 void AutogenerateRoutines::visit( FunctionType *) {
528 // ensure that we don't add assignment ops for types defined as part of the function
529 }
530
531 void AutogenerateRoutines::visit( PointerType *) {
532 // ensure that we don't add assignment ops for types defined as part of the pointer
533 }
534
535 void AutogenerateRoutines::visit( TraitDecl *) {
536 // ensure that we don't add assignment ops for types defined as part of the trait
537 }
538
539 template< typename StmtClass >
540 inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
541 std::set< std::string > oldStructs = structsDone;
542 addVisit( stmt, *this );
543 structsDone = oldStructs;
544 }
545
546 void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
547 maybeAccept( functionDecl->get_functionType(), *this );
548 acceptAll( functionDecl->get_oldDecls(), *this );
549 functionNesting += 1;
550 maybeAccept( functionDecl->get_statements(), *this );
551 functionNesting -= 1;
552 }
553
554 void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
555 visitStatement( compoundStmt );
556 }
557
558 void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
559 visitStatement( switchStmt );
560 }
561
562 void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {
563 visitStatement( switchStmt );
564 }
565
566 // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
567 // visitStatement( caseStmt );
568 // }
569} // SymTab
Note: See TracBrowser for help on using the repository browser.