source: src/GenPoly/InstantiateGeneric.cc@ 8fd1b7c

ADT ast-experimental
Last change on this file since 8fd1b7c was 5bf3976, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Header Clean-Up: Created new headers for new AST typeops and moved declarations.

  • 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/AdjustExprType.hpp" // for adjustExprType
31#include "ResolvExpr/Unify.h" // for typesCompatible
32#include "ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator
33#include "ScrubTyVars.h" // for ScrubTyVars
34#include "SynTree/Declaration.h" // for StructDecl, UnionDecl, TypeDecl
35#include "SynTree/Expression.h" // for TypeExpr, Expression
36#include "SynTree/Mutator.h" // for mutateAll
37#include "SynTree/Type.h" // for StructInstType, UnionInstType
38#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
39#include "SynTree/Visitor.h" // for acceptAll
40
41
42namespace GenPoly {
43
44 /// Abstracts type equality for a list of parameter types
45 struct TypeList {
46 TypeList() : params() {}
47 TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); }
48 TypeList( std::list< Type* > &&_params ) : params( _params ) {}
49
50 TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); }
51 TypeList( TypeList &&that ) : params( std::move( that.params ) ) {}
52
53 /// Extracts types from a list of TypeExpr*
54 TypeList( const std::list< TypeExpr* >& _params ) : params() {
55 for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
56 params.push_back( (*param)->get_type()->clone() );
57 }
58 }
59
60 TypeList& operator= ( const TypeList &that ) {
61 deleteAll( params );
62
63 params.clear();
64 cloneAll( that.params, params );
65
66 return *this;
67 }
68
69 TypeList& operator= ( TypeList &&that ) {
70 deleteAll( params );
71
72 params = std::move( that.params );
73
74 return *this;
75 }
76
77 ~TypeList() { deleteAll( params ); }
78
79 bool operator== ( const TypeList& that ) const {
80 if ( params.size() != that.params.size() ) return false;
81
82 for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
83 if ( ! typesPolyCompatible( *it, *jt ) ) return false;
84 }
85 return true;
86 }
87
88 std::list< Type* > params; ///< Instantiation parameters
89 };
90
91 /// Maps a key and a TypeList to the some value, accounting for scope
92 template< typename Key, typename Value >
93 class InstantiationMap {
94 /// Wraps value for a specific (Key, TypeList) combination
95 typedef std::pair< TypeList, Value* > Instantiation;
96 /// List of TypeLists paired with their appropriate values
97 typedef std::vector< Instantiation > ValueList;
98 /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values
99 typedef ScopedMap< Key*, ValueList > InnerMap;
100
101 InnerMap instantiations; ///< instantiations
102
103 public:
104 /// Starts a new scope
105 void beginScope() { instantiations.beginScope(); }
106
107 /// Ends a scope
108 void endScope() { instantiations.endScope(); }
109
110 /// Gets the value for the (key, typeList) pair, returns NULL on none such.
111 Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const {
112 TypeList typeList( params );
113
114 // scan scopes for matches to the key
115 for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) {
116 for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) {
117 if ( inst->first == typeList ) return inst->second;
118 }
119 }
120 // no matching instantiations found
121 return 0;
122 }
123
124 /// Adds a value for a (key, typeList) pair to the current scope
125 void insert( Key *key, const std::list< TypeExpr* > &params, Value *value ) {
126 auto it = instantiations.findAt( instantiations.currentScope(), key );
127 if ( it == instantiations.end() ) {
128 instantiations.insert( key, ValueList{ Instantiation{ TypeList( params ), value } } );
129 } else {
130 it->second.push_back( Instantiation{ TypeList( params ), value } );
131 }
132 }
133 };
134
135 /// Possible options for a given specialization of a generic type
136 enum class genericType {
137 dtypeStatic, ///< Concrete instantiation based solely on {d,f}type-to-void conversions
138 concrete, ///< Concrete instantiation requiring at least one parameter type
139 dynamic ///< No concrete instantiation
140 };
141
142 genericType& operator |= ( genericType& gt, const genericType& ht ) {
143 switch ( gt ) {
144 case genericType::dtypeStatic:
145 gt = ht;
146 break;
147 case genericType::concrete:
148 if ( ht == genericType::dynamic ) { gt = genericType::dynamic; }
149 break;
150 case genericType::dynamic:
151 // nothing possible
152 break;
153 }
154 return gt;
155 }
156
157 /// Add cast to dtype-static member expressions so that type information is not lost in GenericInstantiator
158 struct FixDtypeStatic final : public WithGuards, public WithVisitorRef<FixDtypeStatic>, public WithShortCircuiting, public WithStmtsToAdd {
159 Expression * postmutate( MemberExpr * memberExpr );
160
161 void premutate( ApplicationExpr * appExpr );
162 void premutate( AddressExpr * addrExpr );
163
164 template<typename AggrInst>
165 Expression * fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr );
166
167 bool isLvalueArg = false;
168 };
169
170 /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
171 struct GenericInstantiator final : public WithConstTypeSubstitution, public WithDeclsToAdd, public WithVisitorRef<GenericInstantiator>, public WithGuards {
172 /// Map of (generic type, parameter list) pairs to concrete type instantiations
173 InstantiationMap< AggregateDecl, AggregateDecl > instantiations;
174 /// Set of types which are dtype-only generic (and therefore have static layout)
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 }
509
510 void GenericInstantiator::endScope() {
511 instantiations.endScope();
512 }
513
514 template< typename AggrInst >
515 Expression * FixDtypeStatic::fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr ) {
516 // need to cast dtype-static member expressions to their actual type before that type is erased.
517 // NOTE: the casts here have the third argument (isGenerated) set to false so that these casts persist until Box, where they are needed.
518 auto & baseParams = *inst->get_baseParameters();
519 if ( isDtypeStatic( baseParams ) ) {
520 if ( ! ResolvExpr::typesCompatible( memberExpr->result, memberExpr->member->get_type(), SymTab::Indexer() ) ) {
521 // type of member and type of expression differ
522 Type * concType = memberExpr->result->clone();
523 if ( isLvalueArg ) {
524 // result must be C lvalue, so make a new reference variable with the correct actual type to replace the member expression
525 // forall(dtype T)
526 // struct Ptr {
527 // T * x
528 // };
529 // Ptr(int) p;
530 // int i;
531 // p.x = &i;
532 // becomes
533 // int *& _dtype_static_member_0 = (int **)&p.x;
534 // _dtype_static_member_0 = &i;
535 // Note: this currently creates more temporaries than is strictly necessary, since it does not check for duplicate uses of the same member expression.
536 static UniqueName tmpNamer( "_dtype_static_member_" );
537 Expression * init = new CastExpr( new AddressExpr( memberExpr ), new PointerType( Type::Qualifiers(), concType->clone() ), false );
538 ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), new ReferenceType( Type::Qualifiers(), concType ), new SingleInit( init ) );
539 stmtsToAddBefore.push_back( new DeclStmt( tmp ) );
540 return new VariableExpr( tmp );
541 } else {
542 // can simply add a cast to actual type
543 return new CastExpr( memberExpr, concType, false );
544 }
545 }
546 }
547 return memberExpr;
548 }
549
550 Expression * FixDtypeStatic::postmutate( MemberExpr * memberExpr ) {
551 Type * aggrType = memberExpr->aggregate->result;
552 if ( isGenericType( aggrType ) ) {
553 if ( StructInstType * inst = dynamic_cast< StructInstType * >( aggrType ) ) {
554 return fixMemberExpr( inst, memberExpr );
555 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( aggrType ) ) {
556 return fixMemberExpr( inst, memberExpr );
557 }
558 }
559 return memberExpr;
560 }
561
562 void FixDtypeStatic::premutate( ApplicationExpr * appExpr ) {
563 GuardValue( isLvalueArg );
564 isLvalueArg = false;
565 DeclarationWithType * function = InitTweak::getFunction( appExpr );
566 if ( function->linkage == LinkageSpec::Intrinsic && CodeGen::isAssignment( function->name ) ) {
567 // explicitly visit children because only the first argument must be a C lvalue.
568 visit_children = false;
569 appExpr->env = maybeMutate( appExpr->env, *visitor );
570 appExpr->result = maybeMutate( appExpr->result, *visitor );
571 appExpr->function = maybeMutate( appExpr->function, *visitor );
572 isLvalueArg = true;
573 for ( Expression * arg : appExpr->args ) {
574 arg = maybeMutate( arg, *visitor );
575 isLvalueArg = false;
576 }
577 }
578 }
579
580 void FixDtypeStatic::premutate( AddressExpr * ) {
581 // argument of & must be C lvalue
582 GuardValue( isLvalueArg );
583 isLvalueArg = true;
584 }
585} // namespace GenPoly
586
587// Local Variables: //
588// tab-width: 4 //
589// mode: c++ //
590// compile-command: "make install" //
591// End: //
Note: See TracBrowser for help on using the repository browser.