source: src/SymTab/Autogen.cc@ 2be1023

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

rework ctor/dtor generation to work properly with multidimensional arrays

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