source: src/GenPoly/InstantiateGeneric.cc@ 9dc3eb21

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9dc3eb21 was 9dc3eb21, checked in by Fangren Yu <f37yu@…>, 5 years ago

fix #196

  • Property mode set to 100644
File size: 23.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 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// InstantiateGeneric.cc --
8//
9// Author : Aaron B. Moss
10// Created On : Thu Aug 04 18:33:00 2016
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jul 16 10:17:00 2020
13// Update Count : 2
14//
15#include "InstantiateGeneric.h"
16
17#include <cassert> // for assertf, assert
18#include <iterator> // for back_inserter, inserter
19#include <list> // for list, _List_const_iterator
20#include <utility> // for move, pair
21#include <vector> // for vector
22
23#include "CodeGen/OperatorTable.h"
24#include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd
25#include "Common/ScopedMap.h" // for ScopedMap
26#include "Common/UniqueName.h" // for UniqueName
27#include "Common/utility.h" // for deleteAll, cloneAll
28#include "GenPoly.h" // for isPolyType, typesPolyCompatible
29#include "InitTweak/InitTweak.h"
30#include "ResolvExpr/typeops.h"
31#include "ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator
32#include "ScrubTyVars.h" // for ScrubTyVars
33#include "SynTree/Declaration.h" // for StructDecl, UnionDecl, TypeDecl
34#include "SynTree/Expression.h" // for TypeExpr, Expression
35#include "SynTree/Mutator.h" // for mutateAll
36#include "SynTree/Type.h" // for StructInstType, UnionInstType
37#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
38#include "SynTree/Visitor.h" // for acceptAll
39
40
41namespace GenPoly {
42
43 /// Abstracts type equality for a list of parameter types
44 struct TypeList {
45 TypeList() : params() {}
46 TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); }
47 TypeList( std::list< Type* > &&_params ) : params( _params ) {}
48
49 TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); }
50 TypeList( TypeList &&that ) : params( std::move( that.params ) ) {}
51
52 /// Extracts types from a list of TypeExpr*
53 TypeList( const std::list< TypeExpr* >& _params ) : params() {
54 for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
55 params.push_back( (*param)->get_type()->clone() );
56 }
57 }
58
59 TypeList& operator= ( const TypeList &that ) {
60 deleteAll( params );
61
62 params.clear();
63 cloneAll( that.params, params );
64
65 return *this;
66 }
67
68 TypeList& operator= ( TypeList &&that ) {
69 deleteAll( params );
70
71 params = std::move( that.params );
72
73 return *this;
74 }
75
76 ~TypeList() { deleteAll( params ); }
77
78 bool operator== ( const TypeList& that ) const {
79 if ( params.size() != that.params.size() ) return false;
80
81 for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
82 if ( ! typesPolyCompatible( *it, *jt ) ) return false;
83 }
84 return true;
85 }
86
87 std::list< Type* > params; ///< Instantiation parameters
88 };
89
90 /// Maps a key and a TypeList to the some value, accounting for scope
91 template< typename Key, typename Value >
92 class InstantiationMap {
93 /// Wraps value for a specific (Key, TypeList) combination
94 typedef std::pair< TypeList, Value* > Instantiation;
95 /// List of TypeLists paired with their appropriate values
96 typedef std::vector< Instantiation > ValueList;
97 /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values
98 typedef ScopedMap< Key*, ValueList > InnerMap;
99
100 InnerMap instantiations; ///< instantiations
101
102 public:
103 /// Starts a new scope
104 void beginScope() { instantiations.beginScope(); }
105
106 /// Ends a scope
107 void endScope() { instantiations.endScope(); }
108
109 /// Gets the value for the (key, typeList) pair, returns NULL on none such.
110 Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const {
111 TypeList typeList( params );
112
113 // scan scopes for matches to the key
114 for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) {
115 for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) {
116 if ( inst->first == typeList ) return inst->second;
117 }
118 }
119 // no matching instantiations found
120 return 0;
121 }
122
123 /// Adds a value for a (key, typeList) pair to the current scope
124 void insert( Key *key, const std::list< TypeExpr* > &params, Value *value ) {
125 auto it = instantiations.findAt( instantiations.currentScope(), key );
126 if ( it == instantiations.end() ) {
127 instantiations.insert( key, ValueList{ Instantiation{ TypeList( params ), value } } );
128 } else {
129 it->second.push_back( Instantiation{ TypeList( params ), value } );
130 }
131 }
132 };
133
134 /// Possible options for a given specialization of a generic type
135 enum class genericType {
136 dtypeStatic, ///< Concrete instantiation based solely on {d,f}type-to-void conversions
137 concrete, ///< Concrete instantiation requiring at least one parameter type
138 dynamic ///< No concrete instantiation
139 };
140
141 genericType& operator |= ( genericType& gt, const genericType& ht ) {
142 switch ( gt ) {
143 case genericType::dtypeStatic:
144 gt = ht;
145 break;
146 case genericType::concrete:
147 if ( ht == genericType::dynamic ) { gt = genericType::dynamic; }
148 break;
149 case genericType::dynamic:
150 // nothing possible
151 break;
152 }
153 return gt;
154 }
155
156 /// Add cast to dtype-static member expressions so that type information is not lost in GenericInstantiator
157 struct FixDtypeStatic final : public WithGuards, public WithVisitorRef<FixDtypeStatic>, public WithShortCircuiting, public WithStmtsToAdd {
158 Expression * postmutate( MemberExpr * memberExpr );
159
160 void premutate( ApplicationExpr * appExpr );
161 void premutate( AddressExpr * addrExpr );
162
163 template<typename AggrInst>
164 Expression * fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr );
165
166 bool isLvalueArg = false;
167 };
168
169 /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
170 struct GenericInstantiator final : public WithConstTypeSubstitution, public WithDeclsToAdd, public WithVisitorRef<GenericInstantiator>, public WithGuards {
171 /// Map of (generic type, parameter list) pairs to concrete type instantiations
172 InstantiationMap< AggregateDecl, AggregateDecl > instantiations;
173 /// Set of types which are dtype-only generic (and therefore have static layout)
174 // ScopedSet< AggregateDecl* > dtypeStatics;
175 std::set<AggregateDecl *> dtypeStatics;
176 /// Namer for concrete types
177 UniqueName typeNamer;
178 /// Should not make use of type environment to replace types of function parameter and return values.
179 bool inFunctionType = false;
180 /// Index of current member, used to recreate MemberExprs with the member from an instantiation
181 int memberIndex = -1;
182 GenericInstantiator() : instantiations(), dtypeStatics(), typeNamer("_conc_") {}
183
184 Type* postmutate( StructInstType *inst );
185 Type* postmutate( UnionInstType *inst );
186
187 // fix MemberExprs to use the member from the instantiation
188 void premutate( MemberExpr * memberExpr );
189 Expression * postmutate( MemberExpr * memberExpr );
190
191 void premutate( FunctionType * ) {
192 GuardValue( inFunctionType );
193 inFunctionType = true;
194 }
195
196 void beginScope();
197 void endScope();
198 private:
199 /// Wrap instantiation lookup for structs
200 StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); }
201 /// Wrap instantiation lookup for unions
202 UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); }
203 /// Wrap instantiation insertion for structs
204 void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); }
205 /// Wrap instantiation insertion for unions
206 void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); }
207
208 void replaceParametersWithConcrete( std::list< Expression* >& params );
209 Type *replaceWithConcrete( Type *type, bool doClone );
210
211 /// Strips a dtype-static aggregate decl of its type parameters, marks it as stripped
212 void stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs );
213 };
214
215 void instantiateGeneric( std::list< Declaration* > &translationUnit ) {
216 PassVisitor<FixDtypeStatic> fixer;
217 PassVisitor<GenericInstantiator> instantiator;
218
219 mutateAll( translationUnit, fixer );
220 mutateAll( translationUnit, instantiator );
221 }
222
223 bool isDtypeStatic( const std::list< TypeDecl* >& baseParams ) {
224 return std::all_of( baseParams.begin(), baseParams.end(), []( TypeDecl * td ) { return ! td->isComplete(); } );
225 }
226
227 /// Makes substitutions of params into baseParams; returns dtypeStatic if there is a concrete instantiation based only on {d,f}type-to-void conversions,
228 /// concrete if there is a concrete instantiation requiring at least one parameter type, and dynamic if there is no concrete instantiation
229 genericType makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) {
230 genericType gt = genericType::dtypeStatic;
231
232 // substitute concrete types for given parameters, and incomplete types for placeholders
233 std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
234 std::list< Expression* >::const_iterator param = params.begin();
235 for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) {
236 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
237 assert(paramType && "Aggregate parameters should be type expressions");
238
239 if ( (*baseParam)->isComplete() ) {
240 // substitute parameter for complete (otype or sized dtype) type
241 if ( isPolyType( paramType->get_type() ) ) {
242 // substitute polymorphic parameter type in to generic type
243 out.push_back( paramType->clone() );
244 gt = genericType::dynamic;
245 } else {
246 // normalize possibly dtype-static parameter type
247 out.push_back( new TypeExpr{
248 ScrubTyVars::scrubAll( paramType->get_type()->clone() ) } );
249 gt |= genericType::concrete;
250 }
251 } else switch ( (*baseParam)->get_kind() ) {
252 case TypeDecl::Dtype:
253 // can pretend that any incomplete dtype is `void`
254 out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
255 break;
256 case TypeDecl::Ftype:
257 // can pretend that any ftype is `void (*)(void)`
258 out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
259 break;
260 case TypeDecl::Ttype:
261 assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
262 break;
263 default:
264 assertf( false, "Unhandled type parameter kind" );
265 break;
266 }
267 }
268
269 assertf( baseParam == baseParams.end() && param == params.end(), "Type parameters should match type variables" );
270 return gt;
271 }
272
273 /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
274 void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs,
275 std::list< Declaration* >& out ) {
276 // substitute types into new members
277 TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
278 for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
279 Declaration *newMember = (*member)->clone();
280 subs.apply(newMember);
281 out.push_back( newMember );
282 }
283 }
284
285 /// Substitutes types of members according to baseParams => typeSubs, working in-place
286 void substituteMembers( std::list< Declaration* >& members, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
287 // substitute types into new members
288 TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
289 for ( std::list< Declaration* >::iterator member = members.begin(); member != members.end(); ++member ) {
290 subs.apply(*member);
291 }
292 }
293
294 /// Strips the instances's type parameters
295 void stripInstParams( ReferenceToType *inst ) {
296 deleteAll( inst->get_parameters() );
297 inst->get_parameters().clear();
298 }
299
300 template< typename AggrInst >
301 static AggrInst * asForward( AggrInst * decl ) {
302 if ( !decl->body ) {
303 return nullptr;
304 }
305 decl = decl->clone();
306 decl->body = false;
307 deleteAll( decl->members );
308 decl->members.clear();
309 return decl;
310 }
311
312 void GenericInstantiator::stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
313 substituteMembers( base->get_members(), baseParams, typeSubs );
314
315 // xxx - can't delete type parameters because they may have assertions that are used
316 // deleteAll( baseParams );
317 baseParams.clear();
318
319 dtypeStatics.insert( base );
320 }
321
322 /// xxx - more or less copied from box -- these should be merged with those somehow...
323 void GenericInstantiator::replaceParametersWithConcrete( std::list< Expression* >& params ) {
324 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
325 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
326 assertf(paramType, "Aggregate parameters should be type expressions");
327 paramType->set_type( replaceWithConcrete( paramType->get_type(), false ) );
328 }
329 }
330
331 Type *GenericInstantiator::replaceWithConcrete( Type *type, bool doClone ) {
332 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
333 if ( env && ! inFunctionType ) {
334 Type *concrete = env->lookup( typeInst->get_name() );
335 if ( concrete ) {
336 return concrete->clone();
337 }
338 else return typeInst->clone();
339 }
340 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
341 if ( doClone ) {
342 structType = structType->clone();
343 }
344 replaceParametersWithConcrete( structType->get_parameters() );
345 return structType;
346 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
347 if ( doClone ) {
348 unionType = unionType->clone();
349 }
350 replaceParametersWithConcrete( unionType->get_parameters() );
351 return unionType;
352 }
353 return type;
354 }
355
356
357 Type* GenericInstantiator::postmutate( StructInstType *inst ) {
358 // exit early if no need for further mutation
359 if ( inst->get_parameters().empty() ) return inst;
360
361 // need to replace type variables to ensure that generic types are instantiated for the return values of polymorphic functions (in particular, for thunks, because they are not [currently] copy constructed).
362 replaceWithConcrete( inst, false );
363
364 // check for an already-instantiatiated dtype-static type
365 if ( dtypeStatics.find( inst->get_baseStruct() ) != dtypeStatics.end() ) {
366 stripInstParams( inst );
367 return inst;
368 }
369
370 // check if type can be concretely instantiated; put substitutions into typeSubs
371 assertf( inst->get_baseParameters(), "Base struct has parameters" );
372 std::list< TypeExpr* > typeSubs;
373 genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
374 switch ( gt ) {
375 case genericType::dtypeStatic:
376 stripDtypeParams( inst->get_baseStruct(), *inst->get_baseParameters(), typeSubs );
377 stripInstParams( inst );
378 break;
379
380 case genericType::concrete: {
381 // make concrete instantiation of generic type
382 StructDecl *concDecl = lookup( inst, typeSubs );
383 if ( ! concDecl ) {
384 // set concDecl to new type, insert type declaration into statements to add
385 concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
386 concDecl->set_body( inst->get_baseStruct()->has_body() );
387 substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
388 // Forward declare before recursion. (TODO: Only when needed, #199.)
389 insert( inst, typeSubs, concDecl );
390 if ( StructDecl *forwardDecl = asForward( concDecl ) ) {
391 declsToAddBefore.push_back( forwardDecl );
392 }
393 concDecl->acceptMutator( *visitor ); // recursively instantiate members
394 declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first
395 }
396 StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
397 newInst->set_baseStruct( concDecl );
398
399 delete inst;
400 inst = newInst;
401 break;
402 }
403
404 case genericType::dynamic:
405 // do nothing
406 break;
407 }
408
409 deleteAll( typeSubs );
410 return inst;
411 }
412
413 Type* GenericInstantiator::postmutate( UnionInstType *inst ) {
414 // exit early if no need for further mutation
415 if ( inst->get_parameters().empty() ) return inst;
416
417 // check for an already-instantiatiated dtype-static type
418 if ( dtypeStatics.find( inst->get_baseUnion() ) != dtypeStatics.end() ) {
419 stripInstParams( inst );
420 return inst;
421 }
422
423 // check if type can be concretely instantiated; put substitutions into typeSubs
424 assert( inst->get_baseParameters() && "Base union has parameters" );
425 std::list< TypeExpr* > typeSubs;
426 genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
427 switch ( gt ) {
428 case genericType::dtypeStatic:
429 stripDtypeParams( inst->get_baseUnion(), *inst->get_baseParameters(), typeSubs );
430 stripInstParams( inst );
431 break;
432
433 case genericType::concrete:
434 {
435 // make concrete instantiation of generic type
436 UnionDecl *concDecl = lookup( inst, typeSubs );
437 if ( ! concDecl ) {
438 // set concDecl to new type, insert type declaration into statements to add
439 concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
440 concDecl->set_body( inst->get_baseUnion()->has_body() );
441 substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
442 // Forward declare before recursion. (TODO: Only when needed, #199.)
443 insert( inst, typeSubs, concDecl );
444 if ( UnionDecl *forwardDecl = asForward( concDecl ) ) {
445 declsToAddBefore.push_back( forwardDecl );
446 }
447 concDecl->acceptMutator( *visitor ); // recursively instantiate members
448 declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first
449 }
450 UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
451 newInst->set_baseUnion( concDecl );
452
453 delete inst;
454 inst = newInst;
455 break;
456 }
457 case genericType::dynamic:
458 // do nothing
459 break;
460 }
461
462 deleteAll( typeSubs );
463 return inst;
464 }
465
466 namespace {
467 bool isGenericType( Type * t ) {
468 if ( StructInstType * inst = dynamic_cast< StructInstType * >( t ) ) {
469 return ! inst->parameters.empty();
470 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) {
471 return ! inst->parameters.empty();
472 }
473 return false;
474 }
475 }
476
477 void GenericInstantiator::premutate( MemberExpr * memberExpr ) {
478 GuardValue( memberIndex );
479 memberIndex = -1;
480 if ( isGenericType( memberExpr->aggregate->result ) ) {
481 // find the location of the member
482 AggregateDecl * aggr = memberExpr->aggregate->result->getAggr();
483 std::list< Declaration * > & members = aggr->members;
484 memberIndex = std::distance( members.begin(), std::find( members.begin(), members.end(), memberExpr->member ) );
485 assertf( memberIndex < (int)members.size(), "Could not find member %s in generic type %s", toString( memberExpr->member ).c_str(), toString( memberExpr->aggregate ).c_str() );
486 }
487 }
488
489 Expression * GenericInstantiator::postmutate( MemberExpr * memberExpr ) {
490 if ( memberIndex != -1 ) {
491 // using the location from the generic type, find the member in the instantiation and rebuild the member expression
492 AggregateDecl * aggr = memberExpr->aggregate->result->getAggr();
493 assertf( memberIndex < (int)aggr->members.size(), "Instantiation somehow has fewer members than the generic type." );
494 Declaration * member = *std::next( aggr->members.begin(), memberIndex );
495 assertf( member->name == memberExpr->member->name, "Instantiation has different member order than the generic type. %s / %s", toString( member ).c_str(), toString( memberExpr->member ).c_str() );
496 DeclarationWithType * field = strict_dynamic_cast< DeclarationWithType * >( member );
497 MemberExpr * ret = new MemberExpr( field, memberExpr->aggregate->clone() );
498 ResolvExpr::adjustExprType( ret->result ); // pointer decay
499 std::swap( ret->env, memberExpr->env );
500 delete memberExpr;
501 return ret;
502 }
503 return memberExpr;
504 }
505
506 void GenericInstantiator::beginScope() {
507 instantiations.beginScope();
508 //dtypeStatics.beginScope();
509 }
510
511 void GenericInstantiator::endScope() {
512 instantiations.endScope();
513 //dtypeStatics.endScope();
514 }
515
516 template< typename AggrInst >
517 Expression * FixDtypeStatic::fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr ) {
518 // need to cast dtype-static member expressions to their actual type before that type is erased.
519 // NOTE: the casts here have the third argument (isGenerated) set to false so that these casts persist until Box, where they are needed.
520 auto & baseParams = *inst->get_baseParameters();
521 if ( isDtypeStatic( baseParams ) ) {
522 if ( ! ResolvExpr::typesCompatible( memberExpr->result, memberExpr->member->get_type(), SymTab::Indexer() ) ) {
523 // type of member and type of expression differ
524 Type * concType = memberExpr->result->clone();
525 if ( isLvalueArg ) {
526 // result must be C lvalue, so make a new reference variable with the correct actual type to replace the member expression
527 // forall(dtype T)
528 // struct Ptr {
529 // T * x
530 // };
531 // Ptr(int) p;
532 // int i;
533 // p.x = &i;
534 // becomes
535 // int *& _dtype_static_member_0 = (int **)&p.x;
536 // _dtype_static_member_0 = &i;
537 // Note: this currently creates more temporaries than is strictly necessary, since it does not check for duplicate uses of the same member expression.
538 static UniqueName tmpNamer( "_dtype_static_member_" );
539 Expression * init = new CastExpr( new AddressExpr( memberExpr ), new PointerType( Type::Qualifiers(), concType->clone() ), false );
540 ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), new ReferenceType( Type::Qualifiers(), concType ), new SingleInit( init ) );
541 stmtsToAddBefore.push_back( new DeclStmt( tmp ) );
542 return new VariableExpr( tmp );
543 } else {
544 // can simply add a cast to actual type
545 return new CastExpr( memberExpr, concType, false );
546 }
547 }
548 }
549 return memberExpr;
550 }
551
552 Expression * FixDtypeStatic::postmutate( MemberExpr * memberExpr ) {
553 Type * aggrType = memberExpr->aggregate->result;
554 if ( isGenericType( aggrType ) ) {
555 if ( StructInstType * inst = dynamic_cast< StructInstType * >( aggrType ) ) {
556 return fixMemberExpr( inst, memberExpr );
557 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( aggrType ) ) {
558 return fixMemberExpr( inst, memberExpr );
559 }
560 }
561 return memberExpr;
562 }
563
564 void FixDtypeStatic::premutate( ApplicationExpr * appExpr ) {
565 GuardValue( isLvalueArg );
566 isLvalueArg = false;
567 DeclarationWithType * function = InitTweak::getFunction( appExpr );
568 if ( function->linkage == LinkageSpec::Intrinsic && CodeGen::isAssignment( function->name ) ) {
569 // explicitly visit children because only the first argument must be a C lvalue.
570 visit_children = false;
571 appExpr->env = maybeMutate( appExpr->env, *visitor );
572 appExpr->result = maybeMutate( appExpr->result, *visitor );
573 appExpr->function = maybeMutate( appExpr->function, *visitor );
574 isLvalueArg = true;
575 for ( Expression * arg : appExpr->args ) {
576 arg = maybeMutate( arg, *visitor );
577 isLvalueArg = false;
578 }
579 }
580 }
581
582 void FixDtypeStatic::premutate( AddressExpr * ) {
583 // argument of & must be C lvalue
584 GuardValue( isLvalueArg );
585 isLvalueArg = true;
586 }
587} // namespace GenPoly
588
589// Local Variables: //
590// tab-width: 4 //
591// mode: c++ //
592// compile-command: "make install" //
593// End: //
Note: See TracBrowser for help on using the repository browser.