source: src/SymTab/Autogen.cc@ 7933351

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 7933351 was 186fd86, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

major refactoring of autogen code

  • Property mode set to 100644
File size: 26.2 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 Type * SizeType = 0;
29
30 class AutogenerateRoutines : public Visitor {
31 public:
32 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
33
34 typedef Visitor Parent;
35 using Parent::visit;
36
37 virtual void visit( EnumDecl *enumDecl );
38 virtual void visit( StructDecl *structDecl );
39 virtual void visit( UnionDecl *structDecl );
40 virtual void visit( TypeDecl *typeDecl );
41 virtual void visit( TraitDecl *ctxDecl );
42 virtual void visit( FunctionDecl *functionDecl );
43
44 virtual void visit( FunctionType *ftype );
45 virtual void visit( PointerType *ftype );
46
47 virtual void visit( CompoundStmt *compoundStmt );
48 virtual void visit( SwitchStmt *switchStmt );
49
50 AutogenerateRoutines() : functionNesting( 0 ) {}
51 private:
52 template< typename StmtClass > void visitStatement( StmtClass *stmt );
53
54 std::list< Declaration * > declsToAdd;
55 std::set< std::string > structsDone;
56 unsigned int functionNesting; // current level of nested functions
57 };
58
59 void autogenerateRoutines( std::list< Declaration * > &translationUnit ) {
60 AutogenerateRoutines visitor;
61 acceptAndAdd( translationUnit, visitor, false );
62 }
63
64 bool isUnnamedBitfield( ObjectDecl * obj ) {
65 return obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL;
66 }
67
68 /// inserts a forward declaration for functionDecl into declsToAdd
69 void addForwardDecl( FunctionDecl * functionDecl, std::list< Declaration * > & declsToAdd ) {
70 FunctionDecl * decl = functionDecl->clone();
71 delete decl->get_statements();
72 decl->set_statements( NULL );
73 declsToAdd.push_back( decl );
74 decl->fixUniqueId();
75 }
76
77 /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *)
78 FunctionType * genDefaultType( Type * paramType ) {
79 FunctionType *ftype = new FunctionType( Type::Qualifiers(), false );
80 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), paramType->clone() ), nullptr );
81 ftype->get_parameters().push_back( dstParam );
82
83 return ftype;
84 }
85
86 /// given type T, generate type of copy ctor, i.e. function type void (*) (T *, T)
87 FunctionType * genCopyType( Type * paramType ) {
88 FunctionType *ftype = genDefaultType( paramType );
89 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
90 ftype->get_parameters().push_back( srcParam );
91 return ftype;
92 }
93
94 /// given type T, generate type of assignment, i.e. function type T (*) (T *, T)
95 FunctionType * genAssignType( Type * paramType, const std::list< Expression* > & params = std::list< Expression* >() ) {
96 FunctionType *ftype = genCopyType( paramType );
97 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr );
98 ftype->get_returnVals().push_back( returnVal );
99 return ftype;
100 }
101
102 /// true if the aggregate's layout is dynamic
103 template< typename AggrDecl >
104 bool hasDynamicLayout( AggrDecl * aggregateDecl ) {
105 for ( TypeDecl * param : aggregateDecl->get_parameters() ) {
106 if ( param->get_kind() == TypeDecl::Any ) return true;
107 }
108 return false;
109 }
110
111 /// generate a function decl from a name and type. Nesting depth determines whether
112 /// the declaration is static or not; optional paramter determines if declaration is intrinsic
113 FunctionDecl * genFunc( const std::string & fname, FunctionType * ftype, unsigned int functionNesting, bool isIntrinsic = false ) {
114 // Routines at global scope marked "static" to prevent multiple definitions in separate translation units
115 // because each unit generates copies of the default routines for each aggregate.
116 DeclarationNode::StorageClass sc = functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static;
117 LinkageSpec::Spec spec = isIntrinsic ? LinkageSpec::Intrinsic : LinkageSpec::AutoGen;
118 FunctionDecl * decl = new FunctionDecl( fname, sc, spec, ftype, new CompoundStmt( noLabels ), true, false );
119 decl->fixUniqueId();
120 return decl;
121 }
122
123 /// generates a single enumeration assignment expression
124 ApplicationExpr * genEnumAssign( FunctionType * ftype, FunctionDecl * assignDecl ) {
125 // enum copy construct and assignment is just C-style assignment.
126 // this looks like a bad recursive call, but code gen will turn it into
127 // a C-style assignment.
128 // This happens before function pointer type conversion, so need to do it manually here
129 // NOTE: ftype is not necessarily the functionType belonging to assignDecl - ftype is the
130 // type of the function that this expression is being generated for (so that the correct
131 // parameters) are using in the variable exprs
132 assert( ftype->get_parameters().size() == 2 );
133 ObjectDecl * dstParam = safe_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
134 ObjectDecl * srcParam = safe_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
135
136 VariableExpr * assignVarExpr = new VariableExpr( assignDecl );
137 Type * assignVarExprType = assignVarExpr->get_result();
138 assignVarExprType = new PointerType( Type::Qualifiers(), assignVarExprType );
139 assignVarExpr->set_result( assignVarExprType );
140 ApplicationExpr * assignExpr = new ApplicationExpr( assignVarExpr );
141 assignExpr->get_args().push_back( new VariableExpr( dstParam ) );
142 assignExpr->get_args().push_back( new VariableExpr( srcParam ) );
143 return assignExpr;
144 }
145
146 // E ?=?(E volatile*, int),
147 // ?=?(E _Atomic volatile*, int);
148 void makeEnumFunctions( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
149
150 // T ?=?(E *, E);
151 FunctionType *assignType = genAssignType( refType );
152
153 // void ?{}(E *); void ^?{}(E *);
154 FunctionType * ctorType = genDefaultType( refType->clone() );
155 FunctionType * dtorType = genDefaultType( refType->clone() );
156
157 // void ?{}(E *, E);
158 FunctionType *copyCtorType = genCopyType( refType->clone() );
159
160 // xxx - should we also generate void ?{}(E *, int) and E ?{}(E *, E)?
161 // right now these cases work, but that might change.
162
163 // xxx - Temporary: make these functions intrinsic so they codegen as C assignment.
164 // Really they're something of a cross between instrinsic and autogen, so should
165 // probably make a new linkage type
166 FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting, true );
167 FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting, true );
168 FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting, true );
169 FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting, true );
170
171 // body is either return stmt or expr stmt
172 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, genEnumAssign( assignType, assignDecl ) ) );
173 copyCtorDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, genEnumAssign( copyCtorType, assignDecl ) ) );
174
175 declsToAdd.push_back( assignDecl );
176 declsToAdd.push_back( ctorDecl );
177 declsToAdd.push_back( copyCtorDecl );
178 declsToAdd.push_back( dtorDecl );
179 }
180
181 /// generates a single struct member operation (constructor call, destructor call, assignment call)
182 void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool isDynamicLayout, bool forward = true ) {
183 ObjectDecl * returnVal = NULL;
184 if ( ! func->get_functionType()->get_returnVals().empty() ) {
185 returnVal = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_returnVals().front() );
186 }
187
188 InitTweak::InitExpander srcParam( src );
189
190 // assign to destination (and return value if generic)
191 UntypedExpr *derefExpr = UntypedExpr::createDeref( new VariableExpr( dstParam ) );
192 Expression *dstselect = new MemberExpr( field, derefExpr );
193 genImplicitCall( srcParam, dstselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
194
195 if ( isDynamicLayout && returnVal ) {
196 // xxx - there used to be a dereference on returnVal, but this seems to have been wrong?
197 Expression *retselect = new MemberExpr( field, new VariableExpr( returnVal ) );
198 genImplicitCall( srcParam, retselect, func->get_name(), back_inserter( func->get_statements()->get_kids() ), field, forward );
199 } // if
200 }
201
202 /// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
203 template<typename Iterator>
204 void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool isDynamicLayout, 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, isDynamicLayout, 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, bool isDynamicLayout ) {
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, isDynamicLayout );
275 ++parameter;
276 } else {
277 // no matching parameter, initialize field with default ctor
278 makeStructMemberOp( dstParam, NULL, field, func, isDynamicLayout );
279 }
280 }
281 }
282 }
283
284 /// generates struct constructors, destructor, and assignment functions
285 void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
286
287 // Make function polymorphic in same parameters as generic struct, if applicable
288 const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
289 bool isDynamicLayout = hasDynamicLayout( aggregateDecl ); // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)
290
291 // T ?=?(T *, T);
292 FunctionType *assignType = genAssignType( refType );
293 cloneAll( typeParams, assignType->get_forall() );
294
295 // void ?{}(T *); void ^?{}(T *);
296 FunctionType *ctorType = genDefaultType( refType );
297 cloneAll( typeParams, ctorType->get_forall() );
298 FunctionType *dtorType = genDefaultType( refType );
299 cloneAll( typeParams, dtorType->get_forall() );
300
301 // void ?{}(T *, T);
302 FunctionType *copyCtorType = genCopyType( refType );
303 cloneAll( typeParams, copyCtorType->get_forall() );
304
305 FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting );
306 FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting );
307 FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting );
308 FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting );
309
310 if ( functionNesting == 0 ) {
311 // forward declare if top-level struct, so that
312 // type is complete as soon as its body ends
313 addForwardDecl( assignDecl, declsToAdd );
314 addForwardDecl( ctorDecl, declsToAdd );
315 addForwardDecl( copyCtorDecl, declsToAdd );
316 addForwardDecl( dtorDecl, declsToAdd );
317 }
318
319 // create constructors which take each member type as a parameter.
320 // for example, for struct A { int x, y; }; generate
321 // void ?{}(A *, int) and void ?{}(A *, int, int)
322 std::list<Declaration *> memCtors;
323 FunctionType * memCtorType = ctorType->clone();
324 for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
325 DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
326 assert( member );
327 if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
328 // don't make a function whose parameter is an unnamed bitfield
329 continue;
330 } else if ( member->get_name() == "" ) {
331 // don't assign to anonymous members
332 // xxx - this is a temporary fix. Anonymous members tie into
333 // our inheritance model. I think the correct way to handle this is to
334 // cast the structure to the type of the member and let the resolver
335 // figure out whether it's valid and have a pass afterwards that fixes
336 // the assignment to use pointer arithmetic with the offset of the
337 // member, much like how generic type members are handled.
338 continue;
339 }
340 memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
341 FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
342 makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor, isDynamicLayout );
343 memCtors.push_back( ctor );
344 }
345 delete memCtorType;
346
347 // generate appropriate calls to member ctor, assignment
348 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), assignDecl, isDynamicLayout );
349 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctorDecl, isDynamicLayout );
350 makeStructFunctionBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), copyCtorDecl, isDynamicLayout );
351 // needs to do everything in reverse, so pass "forward" as false
352 makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dtorDecl, isDynamicLayout, false );
353
354 if ( ! isDynamicLayout ) {
355 assert( assignType->get_parameters().size() == 2 );
356 ObjectDecl * srcParam = safe_dynamic_cast< ObjectDecl * >( assignType->get_parameters().back() );
357 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
358 }
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 /// generate a single union assignment expression (using memcpy)
368 template< typename OutputIterator >
369 void makeUnionFieldsAssignment( ObjectDecl * srcParam, ObjectDecl * dstParam, OutputIterator out ) {
370 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
371 copy->get_args().push_back( new VariableExpr( dstParam ) );
372 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
373 copy->get_args().push_back( new SizeofExpr( srcParam->get_type()->clone() ) );
374 *out++ = new ExprStmt( noLabels, copy );
375 }
376
377 /// generates the body of a union assignment/copy constructor/field constructor
378 void makeUnionAssignBody( FunctionDecl * funcDecl, bool isDynamicLayout ) {
379 FunctionType * ftype = funcDecl->get_functionType();
380 assert( ftype->get_parameters().size() == 2 );
381 ObjectDecl * dstParam = safe_dynamic_cast< ObjectDecl * >( ftype->get_parameters().front() );
382 ObjectDecl * srcParam = safe_dynamic_cast< ObjectDecl * >( ftype->get_parameters().back() );
383 ObjectDecl * returnVal = nullptr;
384 if ( ! ftype->get_returnVals().empty() ) {
385 returnVal = safe_dynamic_cast< ObjectDecl * >( ftype->get_returnVals().front() );
386 }
387
388 makeUnionFieldsAssignment( srcParam, dstParam, back_inserter( funcDecl->get_statements()->get_kids() ) );
389 if ( returnVal ) {
390 if ( isDynamicLayout ) makeUnionFieldsAssignment( srcParam, returnVal, back_inserter( funcDecl->get_statements()->get_kids() ) );
391 else funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
392 }
393 }
394
395 /// generates union constructors, destructors, and assignment operator
396 void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
397 // Make function polymorphic in same parameters as generic union, if applicable
398 const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
399 bool isDynamicLayout = hasDynamicLayout( aggregateDecl ); // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)
400
401 // default ctor/dtor need only first parameter
402 // void ?{}(T *); void ^?{}(T *);
403 FunctionType *ctorType = genDefaultType( refType );
404 cloneAll( typeParams, ctorType->get_forall() );
405 FunctionType *dtorType = genDefaultType( refType );
406 cloneAll( typeParams, dtorType->get_forall() );
407
408 // copy ctor needs both parameters
409 // void ?{}(T *, T);
410 FunctionType *copyCtorType = genCopyType( refType );
411 cloneAll( typeParams, copyCtorType->get_forall() );
412
413 // assignment needs both and return value
414 // T ?=?(T *, T);
415 FunctionType *assignType = genAssignType( refType );
416 cloneAll( typeParams, assignType->get_forall() );
417
418 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
419 // because each unit generates copies of the default routines for each aggregate.
420 FunctionDecl *assignDecl = genFunc( "?=?", assignType, functionNesting );
421 FunctionDecl *ctorDecl = genFunc( "?{}", ctorType, functionNesting );
422 FunctionDecl *copyCtorDecl = genFunc( "?{}", copyCtorType, functionNesting );
423 FunctionDecl *dtorDecl = genFunc( "^?{}", dtorType, functionNesting );
424
425 makeUnionAssignBody( assignDecl, isDynamicLayout );
426
427 // body of assignment and copy ctor is the same
428 makeUnionAssignBody( copyCtorDecl, isDynamicLayout );
429
430 // create a constructor which takes the first member type as a parameter.
431 // for example, for Union A { int x; double y; }; generate
432 // void ?{}(A *, int)
433 // This is to mimic C's behaviour which initializes the first member of the union.
434 std::list<Declaration *> memCtors;
435 for ( Declaration * member : aggregateDecl->get_members() ) {
436 if ( DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member ) ) {
437 ObjectDecl * srcParam = new ObjectDecl( "src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 );
438
439 FunctionType * memCtorType = ctorType->clone();
440 memCtorType->get_parameters().push_back( srcParam );
441 FunctionDecl * ctor = genFunc( "?{}", memCtorType, functionNesting );
442
443 makeUnionAssignBody( ctor, isDynamicLayout );
444 memCtors.push_back( ctor );
445 // only generate a ctor for the first field
446 break;
447 }
448 }
449
450 declsToAdd.push_back( assignDecl );
451 declsToAdd.push_back( ctorDecl );
452 declsToAdd.push_back( copyCtorDecl );
453 declsToAdd.push_back( dtorDecl );
454 declsToAdd.splice( declsToAdd.end(), memCtors );
455 }
456
457 void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {
458 if ( ! enumDecl->get_members().empty() ) {
459 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
460 // enumInst->set_baseEnum( enumDecl );
461 // declsToAdd.push_back(
462 makeEnumFunctions( enumDecl, enumInst, functionNesting, declsToAdd );
463 }
464 }
465
466 void AutogenerateRoutines::visit( StructDecl *structDecl ) {
467 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
468 StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
469 for ( TypeDecl * typeDecl : structDecl->get_parameters() ) {
470 structInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
471 }
472 structInst.set_baseStruct( structDecl );
473 makeStructFunctions( structDecl, &structInst, functionNesting, declsToAdd );
474 structsDone.insert( structDecl->get_name() );
475 } // if
476 }
477
478 void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
479 if ( ! unionDecl->get_members().empty() ) {
480 UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
481 unionInst.set_baseUnion( unionDecl );
482 for ( TypeDecl * typeDecl : unionDecl->get_parameters() ) {
483 unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
484 }
485 makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAdd );
486 } // if
487 }
488
489 void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {
490 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
491 typeInst->set_baseType( typeDecl );
492 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, typeInst->clone(), nullptr );
493 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), typeInst->clone() ), nullptr );
494
495 std::list< Statement * > stmts;
496 if ( typeDecl->get_base() ) {
497 // xxx - generate ctor/dtors for typedecls, e.g.
498 // otype T = int *;
499 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
500 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
501 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
502 stmts.push_back( new ReturnStmt( std::list< Label >(), assign ) );
503 } // if
504 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
505 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
506 type->get_parameters().push_back( dst );
507 type->get_parameters().push_back( src );
508 FunctionDecl *func = genFunc( "?=?", type, functionNesting );
509 func->get_statements()->get_kids() = stmts;
510 declsToAdd.push_back( func );
511 }
512
513 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
514 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
515 statements.insert( i, new DeclStmt( noLabels, *decl ) );
516 } // for
517 declsToAdd.clear();
518 }
519
520 void AutogenerateRoutines::visit( FunctionType *) {
521 // ensure that we don't add assignment ops for types defined as part of the function
522 }
523
524 void AutogenerateRoutines::visit( PointerType *) {
525 // ensure that we don't add assignment ops for types defined as part of the pointer
526 }
527
528 void AutogenerateRoutines::visit( TraitDecl *) {
529 // ensure that we don't add assignment ops for types defined as part of the trait
530 }
531
532 template< typename StmtClass >
533 inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {
534 std::set< std::string > oldStructs = structsDone;
535 addVisit( stmt, *this );
536 structsDone = oldStructs;
537 }
538
539 void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {
540 maybeAccept( functionDecl->get_functionType(), *this );
541 acceptAll( functionDecl->get_oldDecls(), *this );
542 functionNesting += 1;
543 maybeAccept( functionDecl->get_statements(), *this );
544 functionNesting -= 1;
545 }
546
547 void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {
548 visitStatement( compoundStmt );
549 }
550
551 void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
552 visitStatement( switchStmt );
553 }
554} // SymTab
Note: See TracBrowser for help on using the repository browser.