[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 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 | #include <list>
|
---|
| 18 | #include <utility>
|
---|
| 19 | #include <vector>
|
---|
[b940dc71] | 20 | #include <unordered_map>
|
---|
[ea5daeb] | 21 |
|
---|
| 22 | #include "InstantiateGeneric.h"
|
---|
| 23 |
|
---|
| 24 | #include "DeclMutator.h"
|
---|
| 25 | #include "GenPoly.h"
|
---|
[3bb195cb] | 26 | #include "ScopedSet.h"
|
---|
[b940dc71] | 27 | #include "PolyMutator.h"
|
---|
[ea5daeb] | 28 |
|
---|
| 29 | #include "ResolvExpr/typeops.h"
|
---|
| 30 |
|
---|
| 31 | #include "SynTree/Declaration.h"
|
---|
| 32 | #include "SynTree/Expression.h"
|
---|
| 33 | #include "SynTree/Type.h"
|
---|
| 34 |
|
---|
[e491159] | 35 | #include "Common/ScopedMap.h"
|
---|
[ea5daeb] | 36 | #include "Common/UniqueName.h"
|
---|
| 37 | #include "Common/utility.h"
|
---|
| 38 |
|
---|
| 39 | namespace GenPoly {
|
---|
| 40 |
|
---|
| 41 | /// Abstracts type equality for a list of parameter types
|
---|
| 42 | struct TypeList {
|
---|
| 43 | TypeList() : params() {}
|
---|
| 44 | TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); }
|
---|
| 45 | TypeList( std::list< Type* > &&_params ) : params( _params ) {}
|
---|
| 46 |
|
---|
| 47 | TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); }
|
---|
| 48 | TypeList( TypeList &&that ) : params( std::move( that.params ) ) {}
|
---|
| 49 |
|
---|
| 50 | /// Extracts types from a list of TypeExpr*
|
---|
| 51 | TypeList( const std::list< TypeExpr* >& _params ) : params() {
|
---|
| 52 | for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
|
---|
| 53 | params.push_back( (*param)->get_type()->clone() );
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | TypeList& operator= ( const TypeList &that ) {
|
---|
| 58 | deleteAll( params );
|
---|
| 59 |
|
---|
| 60 | params.clear();
|
---|
| 61 | cloneAll( that.params, params );
|
---|
| 62 |
|
---|
| 63 | return *this;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | TypeList& operator= ( TypeList &&that ) {
|
---|
| 67 | deleteAll( params );
|
---|
| 68 |
|
---|
| 69 | params = std::move( that.params );
|
---|
| 70 |
|
---|
| 71 | return *this;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | ~TypeList() { deleteAll( params ); }
|
---|
| 75 |
|
---|
| 76 | bool operator== ( const TypeList& that ) const {
|
---|
| 77 | if ( params.size() != that.params.size() ) return false;
|
---|
| 78 |
|
---|
| 79 | SymTab::Indexer dummy;
|
---|
| 80 | for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
|
---|
| 81 | if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false;
|
---|
| 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* > ¶ms, Value *value ) {
|
---|
| 124 | instantiations[ key ].push_back( Instantiation( TypeList( params ), value ) );
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
[3bb195cb] | 127 |
|
---|
| 128 | /// Possible options for a given specialization of a generic type
|
---|
| 129 | enum class genericType {
|
---|
| 130 | dtypeStatic, ///< Concrete instantiation based solely on {d,f}type-to-void conversions
|
---|
| 131 | concrete, ///< Concrete instantiation requiring at least one parameter type
|
---|
| 132 | dynamic ///< No concrete instantiation
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | genericType& operator |= ( genericType& gt, const genericType& ht ) {
|
---|
| 136 | switch ( gt ) {
|
---|
| 137 | case genericType::dtypeStatic:
|
---|
| 138 | gt = ht;
|
---|
| 139 | break;
|
---|
| 140 | case genericType::concrete:
|
---|
| 141 | if ( ht == genericType::dynamic ) { gt = genericType::dynamic; }
|
---|
| 142 | break;
|
---|
| 143 | case genericType::dynamic:
|
---|
| 144 | // nothing possible
|
---|
| 145 | break;
|
---|
| 146 | }
|
---|
| 147 | return gt;
|
---|
| 148 | }
|
---|
[e491159] | 149 |
|
---|
[b940dc71] | 150 | // collect the environments of each TypeInstType so that type variables can be replaced
|
---|
| 151 | // xxx - possibly temporary solution. Access to type environments is required in GenericInstantiator, but it needs to be a DeclMutator which does not provide easy access to the type environments.
|
---|
| 152 | class EnvFinder final : public GenPoly::PolyMutator {
|
---|
| 153 | public:
|
---|
[075734f] | 154 | using GenPoly::PolyMutator::mutate;
|
---|
[b940dc71] | 155 | virtual Type * mutate( TypeInstType * inst ) override {
|
---|
| 156 | if ( env ) envMap[inst] = env;
|
---|
| 157 | return inst;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | // don't want to associate an environment with TypeInstTypes that occur in function types - this may actually only apply to function types belonging to DeclarationWithTypes (or even just FunctionDecl)?
|
---|
| 161 | virtual Type * mutate( FunctionType * ftype ) override {
|
---|
| 162 | return ftype;
|
---|
| 163 | }
|
---|
| 164 | std::unordered_map< ReferenceToType *, TypeSubstitution * > envMap;
|
---|
| 165 | };
|
---|
| 166 |
|
---|
[ea5daeb] | 167 | /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
|
---|
[62e5546] | 168 | class GenericInstantiator final : public DeclMutator {
|
---|
[ea5daeb] | 169 | /// Map of (generic type, parameter list) pairs to concrete type instantiations
|
---|
| 170 | InstantiationMap< AggregateDecl, AggregateDecl > instantiations;
|
---|
[3bb195cb] | 171 | /// Set of types which are dtype-only generic (and therefore have static layout)
|
---|
| 172 | ScopedSet< AggregateDecl* > dtypeStatics;
|
---|
[ea5daeb] | 173 | /// Namer for concrete types
|
---|
| 174 | UniqueName typeNamer;
|
---|
[b940dc71] | 175 | /// Reference to mapping of environments
|
---|
| 176 | const std::unordered_map< ReferenceToType *, TypeSubstitution * > & envMap;
|
---|
[ea5daeb] | 177 | public:
|
---|
[b940dc71] | 178 | GenericInstantiator( const std::unordered_map< ReferenceToType *, TypeSubstitution * > & envMap ) : DeclMutator(), instantiations(), dtypeStatics(), typeNamer("_conc_"), envMap( envMap ) {}
|
---|
[ea5daeb] | 179 |
|
---|
[62e5546] | 180 | using DeclMutator::mutate;
|
---|
| 181 | virtual Type* mutate( StructInstType *inst ) override;
|
---|
| 182 | virtual Type* mutate( UnionInstType *inst ) override;
|
---|
[ea5daeb] | 183 |
|
---|
[62e5546] | 184 | virtual void doBeginScope() override;
|
---|
| 185 | virtual void doEndScope() override;
|
---|
[ea5daeb] | 186 | private:
|
---|
| 187 | /// Wrap instantiation lookup for structs
|
---|
| 188 | StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); }
|
---|
| 189 | /// Wrap instantiation lookup for unions
|
---|
| 190 | UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); }
|
---|
| 191 | /// Wrap instantiation insertion for structs
|
---|
| 192 | void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); }
|
---|
| 193 | /// Wrap instantiation insertion for unions
|
---|
| 194 | void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); }
|
---|
[3bb195cb] | 195 |
|
---|
[b940dc71] | 196 | void replaceParametersWithConcrete( std::list< Expression* >& params );
|
---|
| 197 | Type *replaceWithConcrete( Type *type, bool doClone );
|
---|
| 198 |
|
---|
[3bb195cb] | 199 | /// Strips a dtype-static aggregate decl of its type parameters, marks it as stripped
|
---|
| 200 | void stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs );
|
---|
[ea5daeb] | 201 | };
|
---|
| 202 |
|
---|
| 203 | void instantiateGeneric( std::list< Declaration* > &translationUnit ) {
|
---|
[b940dc71] | 204 | EnvFinder finder;
|
---|
| 205 | mutateAll( translationUnit, finder );
|
---|
| 206 | GenericInstantiator instantiator( finder.envMap );
|
---|
[ea5daeb] | 207 | instantiator.mutateDeclarationList( translationUnit );
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[3bb195cb] | 210 | /// Makes substitutions of params into baseParams; returns dtypeStatic if there is a concrete instantiation based only on {d,f}type-to-void conversions,
|
---|
| 211 | /// concrete if there is a concrete instantiation requiring at least one parameter type, and dynamic if there is no concrete instantiation
|
---|
[ea5daeb] | 212 | genericType makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) {
|
---|
| 213 | genericType gt = genericType::dtypeStatic;
|
---|
| 214 |
|
---|
| 215 | // substitute concrete types for given parameters, and incomplete types for placeholders
|
---|
| 216 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
|
---|
| 217 | std::list< Expression* >::const_iterator param = params.begin();
|
---|
| 218 | for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) {
|
---|
| 219 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 220 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
[e491159] | 221 |
|
---|
[ea5daeb] | 222 | switch ( (*baseParam)->get_kind() ) {
|
---|
| 223 | case TypeDecl::Any: {
|
---|
| 224 | // substitute parameter for otype; makes the type concrete or dynamic depending on the parameter
|
---|
| 225 | out.push_back( paramType->clone() );
|
---|
| 226 | gt |= isPolyType( paramType->get_type() ) ? genericType::dynamic : genericType::concrete;
|
---|
| 227 | break;
|
---|
| 228 | }
|
---|
| 229 | case TypeDecl::Dtype:
|
---|
| 230 | // can pretend that any dtype is `void`
|
---|
| 231 | out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
|
---|
| 232 | break;
|
---|
| 233 | case TypeDecl::Ftype:
|
---|
| 234 | // can pretend that any ftype is `void (*)(void)`
|
---|
| 235 | out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
|
---|
| 236 | break;
|
---|
[b940dc71] | 237 | case TypeDecl::Ttype:
|
---|
| 238 | assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
|
---|
| 239 | break;
|
---|
[ea5daeb] | 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | assert( baseParam == baseParams.end() && param == params.end() && "Type parameters should match type variables" );
|
---|
| 244 | return gt;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
|
---|
| 248 | void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs,
|
---|
| 249 | std::list< Declaration* >& out ) {
|
---|
| 250 | // substitute types into new members
|
---|
| 251 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
|
---|
| 252 | for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
|
---|
| 253 | Declaration *newMember = (*member)->clone();
|
---|
| 254 | subs.apply(newMember);
|
---|
| 255 | out.push_back( newMember );
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[3bb195cb] | 259 | /// Substitutes types of members according to baseParams => typeSubs, working in-place
|
---|
| 260 | void substituteMembers( std::list< Declaration* >& members, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
|
---|
| 261 | // substitute types into new members
|
---|
| 262 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
|
---|
| 263 | for ( std::list< Declaration* >::iterator member = members.begin(); member != members.end(); ++member ) {
|
---|
| 264 | subs.apply(*member);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[f18a711] | 268 | /// Strips the instances's type parameters
|
---|
| 269 | void stripInstParams( ReferenceToType *inst ) {
|
---|
[3bb195cb] | 270 | deleteAll( inst->get_parameters() );
|
---|
| 271 | inst->get_parameters().clear();
|
---|
| 272 | }
|
---|
[e491159] | 273 |
|
---|
[3bb195cb] | 274 | void GenericInstantiator::stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
|
---|
| 275 | substituteMembers( base->get_members(), baseParams, typeSubs );
|
---|
| 276 |
|
---|
| 277 | deleteAll( baseParams );
|
---|
| 278 | baseParams.clear();
|
---|
[e491159] | 279 |
|
---|
[3bb195cb] | 280 | dtypeStatics.insert( base );
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[b940dc71] | 283 | /// xxx - more or less copied from box -- these should be merged with those somehow...
|
---|
| 284 | void GenericInstantiator::replaceParametersWithConcrete( std::list< Expression* >& params ) {
|
---|
| 285 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 286 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 287 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
| 288 | paramType->set_type( replaceWithConcrete( paramType->get_type(), false ) );
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | Type *GenericInstantiator::replaceWithConcrete( Type *type, bool doClone ) {
|
---|
| 293 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 294 | if ( envMap.count( typeInst ) ) {
|
---|
| 295 | TypeSubstitution * env = envMap.at( typeInst );
|
---|
| 296 | Type *concrete = env->lookup( typeInst->get_name() );
|
---|
| 297 | if ( concrete ) {
|
---|
| 298 | return concrete->clone();
|
---|
| 299 | }
|
---|
| 300 | else return typeInst->clone();
|
---|
| 301 | }
|
---|
| 302 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 303 | if ( doClone ) {
|
---|
| 304 | structType = structType->clone();
|
---|
| 305 | }
|
---|
| 306 | replaceParametersWithConcrete( structType->get_parameters() );
|
---|
| 307 | return structType;
|
---|
| 308 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 309 | if ( doClone ) {
|
---|
| 310 | unionType = unionType->clone();
|
---|
| 311 | }
|
---|
| 312 | replaceParametersWithConcrete( unionType->get_parameters() );
|
---|
| 313 | return unionType;
|
---|
| 314 | }
|
---|
| 315 | return type;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
[ea5daeb] | 319 | Type* GenericInstantiator::mutate( StructInstType *inst ) {
|
---|
| 320 | // mutate subtypes
|
---|
| 321 | Type *mutated = Mutator::mutate( inst );
|
---|
| 322 | inst = dynamic_cast< StructInstType* >( mutated );
|
---|
| 323 | if ( ! inst ) return mutated;
|
---|
| 324 |
|
---|
| 325 | // exit early if no need for further mutation
|
---|
| 326 | if ( inst->get_parameters().empty() ) return inst;
|
---|
| 327 |
|
---|
[b940dc71] | 328 | // 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).
|
---|
| 329 | replaceWithConcrete( inst, false );
|
---|
| 330 |
|
---|
[3bb195cb] | 331 | // check for an already-instantiatiated dtype-static type
|
---|
[f18a711] | 332 | if ( dtypeStatics.find( inst->get_baseStruct() ) != dtypeStatics.end() ) {
|
---|
| 333 | stripInstParams( inst );
|
---|
| 334 | return inst;
|
---|
| 335 | }
|
---|
[e491159] | 336 |
|
---|
[ea5daeb] | 337 | // check if type can be concretely instantiated; put substitutions into typeSubs
|
---|
[b940dc71] | 338 | assertf( inst->get_baseParameters(), "Base struct has parameters" );
|
---|
[ea5daeb] | 339 | std::list< TypeExpr* > typeSubs;
|
---|
| 340 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
|
---|
| 341 | switch ( gt ) {
|
---|
[3bb195cb] | 342 | case genericType::dtypeStatic:
|
---|
| 343 | stripDtypeParams( inst->get_baseStruct(), *inst->get_baseParameters(), typeSubs );
|
---|
[f18a711] | 344 | stripInstParams( inst );
|
---|
[3bb195cb] | 345 | break;
|
---|
[e491159] | 346 |
|
---|
[3bb195cb] | 347 | case genericType::concrete: {
|
---|
[ea5daeb] | 348 | // make concrete instantiation of generic type
|
---|
| 349 | StructDecl *concDecl = lookup( inst, typeSubs );
|
---|
| 350 | if ( ! concDecl ) {
|
---|
| 351 | // set concDecl to new type, insert type declaration into statements to add
|
---|
| 352 | concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
|
---|
[2c57025] | 353 | concDecl->set_body( inst->get_baseStruct()->has_body() );
|
---|
[ea5daeb] | 354 | substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
|
---|
| 355 | DeclMutator::addDeclaration( concDecl );
|
---|
| 356 | insert( inst, typeSubs, concDecl );
|
---|
| 357 | }
|
---|
| 358 | StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
|
---|
| 359 | newInst->set_baseStruct( concDecl );
|
---|
| 360 |
|
---|
| 361 | delete inst;
|
---|
| 362 | inst = newInst;
|
---|
| 363 | break;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | case genericType::dynamic:
|
---|
| 367 | // do nothing
|
---|
| 368 | break;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | deleteAll( typeSubs );
|
---|
| 372 | return inst;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | Type* GenericInstantiator::mutate( UnionInstType *inst ) {
|
---|
| 376 | // mutate subtypes
|
---|
| 377 | Type *mutated = Mutator::mutate( inst );
|
---|
| 378 | inst = dynamic_cast< UnionInstType* >( mutated );
|
---|
| 379 | if ( ! inst ) return mutated;
|
---|
| 380 |
|
---|
| 381 | // exit early if no need for further mutation
|
---|
| 382 | if ( inst->get_parameters().empty() ) return inst;
|
---|
[3bb195cb] | 383 |
|
---|
| 384 | // check for an already-instantiatiated dtype-static type
|
---|
[f18a711] | 385 | if ( dtypeStatics.find( inst->get_baseUnion() ) != dtypeStatics.end() ) {
|
---|
| 386 | stripInstParams( inst );
|
---|
| 387 | return inst;
|
---|
| 388 | }
|
---|
[ea5daeb] | 389 |
|
---|
| 390 | // check if type can be concretely instantiated; put substitutions into typeSubs
|
---|
[3bb195cb] | 391 | assert( inst->get_baseParameters() && "Base union has parameters" );
|
---|
[ea5daeb] | 392 | std::list< TypeExpr* > typeSubs;
|
---|
| 393 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
|
---|
| 394 | switch ( gt ) {
|
---|
[3bb195cb] | 395 | case genericType::dtypeStatic:
|
---|
| 396 | stripDtypeParams( inst->get_baseUnion(), *inst->get_baseParameters(), typeSubs );
|
---|
[f18a711] | 397 | stripInstParams( inst );
|
---|
[3bb195cb] | 398 | break;
|
---|
[e491159] | 399 |
|
---|
[ea5daeb] | 400 | case genericType::concrete:
|
---|
| 401 | {
|
---|
| 402 | // make concrete instantiation of generic type
|
---|
| 403 | UnionDecl *concDecl = lookup( inst, typeSubs );
|
---|
| 404 | if ( ! concDecl ) {
|
---|
| 405 | // set concDecl to new type, insert type declaration into statements to add
|
---|
| 406 | concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
|
---|
[2c57025] | 407 | concDecl->set_body( inst->get_baseUnion()->has_body() );
|
---|
[ea5daeb] | 408 | substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
|
---|
| 409 | DeclMutator::addDeclaration( concDecl );
|
---|
| 410 | insert( inst, typeSubs, concDecl );
|
---|
| 411 | }
|
---|
| 412 | UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
|
---|
| 413 | newInst->set_baseUnion( concDecl );
|
---|
| 414 |
|
---|
| 415 | delete inst;
|
---|
| 416 | inst = newInst;
|
---|
| 417 | break;
|
---|
| 418 | }
|
---|
| 419 | case genericType::dynamic:
|
---|
| 420 | // do nothing
|
---|
| 421 | break;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | deleteAll( typeSubs );
|
---|
| 425 | return inst;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | void GenericInstantiator::doBeginScope() {
|
---|
| 429 | DeclMutator::doBeginScope();
|
---|
| 430 | instantiations.beginScope();
|
---|
[3bb195cb] | 431 | dtypeStatics.beginScope();
|
---|
[ea5daeb] | 432 | }
|
---|
| 433 |
|
---|
| 434 | void GenericInstantiator::doEndScope() {
|
---|
| 435 | DeclMutator::doEndScope();
|
---|
| 436 | instantiations.endScope();
|
---|
[3bb195cb] | 437 | dtypeStatics.endScope();
|
---|
[ea5daeb] | 438 | }
|
---|
| 439 |
|
---|
| 440 | } // namespace GenPoly
|
---|
| 441 |
|
---|
| 442 | // Local Variables: //
|
---|
| 443 | // tab-width: 4 //
|
---|
| 444 | // mode: c++ //
|
---|
| 445 | // compile-command: "make install" //
|
---|
| 446 | // End: //
|
---|