source: src/SymTab/Autogen.cc@ d9fa60a

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

moved substituion into MemberExpr constructor, change generated tuple structs to generic structs, tuples containing type variables almost works

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