source: src/GenPoly/InstantiateGeneric.cc @ a6d70cd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a6d70cd was a6d70cd, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Rollback change to lvalue since it breaks some libcfa code

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