[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 | //
|
---|
[be9288a] | 15 | #include "InstantiateGeneric.h"
|
---|
[ea5daeb] | 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
|
---|
| 29 | #include "ScopedSet.h" // for ScopedSet, ScopedSet<>::iterator
|
---|
| 30 | #include "ScrubTyVars.h" // for ScrubTyVars
|
---|
| 31 | #include "SynTree/Declaration.h" // for StructDecl, UnionDecl, TypeDecl
|
---|
| 32 | #include "SynTree/Expression.h" // for TypeExpr, Expression
|
---|
| 33 | #include "SynTree/Mutator.h" // for mutateAll
|
---|
| 34 | #include "SynTree/Type.h" // for StructInstType, UnionInstType
|
---|
| 35 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
| 36 | #include "SynTree/Visitor.h" // for acceptAll
|
---|
[2a7b3ca] | 37 |
|
---|
[ea5daeb] | 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 | for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
|
---|
[5a3ac84] | 80 | if ( ! typesPolyCompatible( *it, *jt ) ) return false;
|
---|
[ea5daeb] | 81 | }
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | std::list< Type* > params; ///< Instantiation parameters
|
---|
| 86 | };
|
---|
[e491159] | 87 |
|
---|
[ea5daeb] | 88 | /// Maps a key and a TypeList to the some value, accounting for scope
|
---|
| 89 | template< typename Key, typename Value >
|
---|
| 90 | class InstantiationMap {
|
---|
| 91 | /// Wraps value for a specific (Key, TypeList) combination
|
---|
| 92 | typedef std::pair< TypeList, Value* > Instantiation;
|
---|
| 93 | /// List of TypeLists paired with their appropriate values
|
---|
| 94 | typedef std::vector< Instantiation > ValueList;
|
---|
| 95 | /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values
|
---|
| 96 | typedef ScopedMap< Key*, ValueList > InnerMap;
|
---|
| 97 |
|
---|
| 98 | InnerMap instantiations; ///< instantiations
|
---|
| 99 |
|
---|
| 100 | public:
|
---|
| 101 | /// Starts a new scope
|
---|
| 102 | void beginScope() { instantiations.beginScope(); }
|
---|
| 103 |
|
---|
| 104 | /// Ends a scope
|
---|
| 105 | void endScope() { instantiations.endScope(); }
|
---|
| 106 |
|
---|
| 107 | /// Gets the value for the (key, typeList) pair, returns NULL on none such.
|
---|
| 108 | Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const {
|
---|
| 109 | TypeList typeList( params );
|
---|
| 110 |
|
---|
| 111 | // scan scopes for matches to the key
|
---|
| 112 | for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) {
|
---|
| 113 | for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) {
|
---|
| 114 | if ( inst->first == typeList ) return inst->second;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | // no matching instantiations found
|
---|
| 118 | return 0;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /// Adds a value for a (key, typeList) pair to the current scope
|
---|
| 122 | void insert( Key *key, const std::list< TypeExpr* > ¶ms, Value *value ) {
|
---|
[e58dfb9] | 123 | auto it = instantiations.findAt( instantiations.currentScope(), key );
|
---|
| 124 | if ( it == instantiations.end() ) {
|
---|
| 125 | instantiations.insert( key, ValueList{ Instantiation{ TypeList( params ), value } } );
|
---|
| 126 | } else {
|
---|
| 127 | it->second.push_back( Instantiation{ TypeList( params ), value } );
|
---|
| 128 | }
|
---|
[ea5daeb] | 129 | }
|
---|
| 130 | };
|
---|
[3bb195cb] | 131 |
|
---|
| 132 | /// Possible options for a given specialization of a generic type
|
---|
| 133 | enum class genericType {
|
---|
| 134 | dtypeStatic, ///< Concrete instantiation based solely on {d,f}type-to-void conversions
|
---|
| 135 | concrete, ///< Concrete instantiation requiring at least one parameter type
|
---|
| 136 | dynamic ///< No concrete instantiation
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | genericType& operator |= ( genericType& gt, const genericType& ht ) {
|
---|
| 140 | switch ( gt ) {
|
---|
| 141 | case genericType::dtypeStatic:
|
---|
| 142 | gt = ht;
|
---|
| 143 | break;
|
---|
| 144 | case genericType::concrete:
|
---|
| 145 | if ( ht == genericType::dynamic ) { gt = genericType::dynamic; }
|
---|
| 146 | break;
|
---|
| 147 | case genericType::dynamic:
|
---|
| 148 | // nothing possible
|
---|
| 149 | break;
|
---|
| 150 | }
|
---|
| 151 | return gt;
|
---|
| 152 | }
|
---|
[e491159] | 153 |
|
---|
[ea5daeb] | 154 | /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
|
---|
[2a7b3ca] | 155 | struct GenericInstantiator final : public WithTypeSubstitution, public WithDeclsToAdd, public WithVisitorRef<GenericInstantiator>, public WithGuards {
|
---|
[ea5daeb] | 156 | /// Map of (generic type, parameter list) pairs to concrete type instantiations
|
---|
| 157 | InstantiationMap< AggregateDecl, AggregateDecl > instantiations;
|
---|
[3bb195cb] | 158 | /// Set of types which are dtype-only generic (and therefore have static layout)
|
---|
| 159 | ScopedSet< AggregateDecl* > dtypeStatics;
|
---|
[ea5daeb] | 160 | /// Namer for concrete types
|
---|
| 161 | UniqueName typeNamer;
|
---|
[2a7b3ca] | 162 | /// Should not make use of type environment to replace types of function parameter and return values.
|
---|
| 163 | bool inFunctionType = false;
|
---|
| 164 | GenericInstantiator() : instantiations(), dtypeStatics(), typeNamer("_conc_") {}
|
---|
| 165 |
|
---|
| 166 | Type* postmutate( StructInstType *inst );
|
---|
| 167 | Type* postmutate( UnionInstType *inst );
|
---|
[ea5daeb] | 168 |
|
---|
[9ff56e7] | 169 | void premutate( __attribute__((unused)) FunctionType * ftype ) {
|
---|
[2a7b3ca] | 170 | GuardValue( inFunctionType );
|
---|
| 171 | inFunctionType = true;
|
---|
| 172 | }
|
---|
[ea5daeb] | 173 |
|
---|
[2a7b3ca] | 174 | void beginScope();
|
---|
| 175 | void endScope();
|
---|
[ea5daeb] | 176 | private:
|
---|
| 177 | /// Wrap instantiation lookup for structs
|
---|
| 178 | StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); }
|
---|
| 179 | /// Wrap instantiation lookup for unions
|
---|
| 180 | UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); }
|
---|
| 181 | /// Wrap instantiation insertion for structs
|
---|
| 182 | void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); }
|
---|
| 183 | /// Wrap instantiation insertion for unions
|
---|
| 184 | void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); }
|
---|
[3bb195cb] | 185 |
|
---|
[b940dc71] | 186 | void replaceParametersWithConcrete( std::list< Expression* >& params );
|
---|
| 187 | Type *replaceWithConcrete( Type *type, bool doClone );
|
---|
| 188 |
|
---|
[3bb195cb] | 189 | /// Strips a dtype-static aggregate decl of its type parameters, marks it as stripped
|
---|
| 190 | void stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs );
|
---|
[ea5daeb] | 191 | };
|
---|
| 192 |
|
---|
| 193 | void instantiateGeneric( std::list< Declaration* > &translationUnit ) {
|
---|
[2a7b3ca] | 194 | PassVisitor<GenericInstantiator> instantiator;
|
---|
| 195 | mutateAll( translationUnit, instantiator );
|
---|
[ea5daeb] | 196 | }
|
---|
| 197 |
|
---|
[3bb195cb] | 198 | /// Makes substitutions of params into baseParams; returns dtypeStatic if there is a concrete instantiation based only on {d,f}type-to-void conversions,
|
---|
| 199 | /// concrete if there is a concrete instantiation requiring at least one parameter type, and dynamic if there is no concrete instantiation
|
---|
[ea5daeb] | 200 | genericType makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) {
|
---|
| 201 | genericType gt = genericType::dtypeStatic;
|
---|
| 202 |
|
---|
| 203 | // substitute concrete types for given parameters, and incomplete types for placeholders
|
---|
| 204 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
|
---|
| 205 | std::list< Expression* >::const_iterator param = params.begin();
|
---|
| 206 | for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) {
|
---|
| 207 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 208 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
[e491159] | 209 |
|
---|
[0bfaf80] | 210 | if ( (*baseParam)->isComplete() ) {
|
---|
[87c3bef] | 211 | // substitute parameter for complete (otype or sized dtype) type
|
---|
[5a3ac84] | 212 | if ( isPolyType( paramType->get_type() ) ) {
|
---|
| 213 | // substitute polymorphic parameter type in to generic type
|
---|
[87c3bef] | 214 | out.push_back( paramType->clone() );
|
---|
[5a3ac84] | 215 | gt = genericType::dynamic;
|
---|
| 216 | } else {
|
---|
| 217 | // normalize possibly dtype-static parameter type
|
---|
[6db9dab] | 218 | out.push_back( new TypeExpr{
|
---|
[5a3ac84] | 219 | ScrubTyVars::scrubAll( paramType->get_type()->clone() ) } );
|
---|
| 220 | gt |= genericType::concrete;
|
---|
[87c3bef] | 221 | }
|
---|
[0bfaf80] | 222 | } else switch ( (*baseParam)->get_kind() ) {
|
---|
| 223 | case TypeDecl::Dtype:
|
---|
| 224 | // can pretend that any incomplete dtype is `void`
|
---|
| 225 | out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
|
---|
| 226 | break;
|
---|
| 227 | case TypeDecl::Ftype:
|
---|
| 228 | // can pretend that any ftype is `void (*)(void)`
|
---|
| 229 | out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
|
---|
| 230 | break;
|
---|
| 231 | case TypeDecl::Ttype:
|
---|
| 232 | assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
|
---|
| 233 | break;
|
---|
| 234 | case TypeDecl::Any:
|
---|
| 235 | assertf( false, "otype parameters handled by baseParam->isComplete()." );
|
---|
| 236 | break;
|
---|
[ea5daeb] | 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[b2daebd4] | 240 | assertf( baseParam == baseParams.end() && param == params.end(), "Type parameters should match type variables" );
|
---|
[ea5daeb] | 241 | return gt;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
|
---|
| 245 | void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs,
|
---|
| 246 | std::list< Declaration* >& out ) {
|
---|
| 247 | // substitute types into new members
|
---|
| 248 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
|
---|
| 249 | for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
|
---|
| 250 | Declaration *newMember = (*member)->clone();
|
---|
| 251 | subs.apply(newMember);
|
---|
| 252 | out.push_back( newMember );
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[3bb195cb] | 256 | /// Substitutes types of members according to baseParams => typeSubs, working in-place
|
---|
| 257 | void substituteMembers( std::list< Declaration* >& members, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
|
---|
| 258 | // substitute types into new members
|
---|
| 259 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
|
---|
| 260 | for ( std::list< Declaration* >::iterator member = members.begin(); member != members.end(); ++member ) {
|
---|
| 261 | subs.apply(*member);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[f18a711] | 265 | /// Strips the instances's type parameters
|
---|
| 266 | void stripInstParams( ReferenceToType *inst ) {
|
---|
[3bb195cb] | 267 | deleteAll( inst->get_parameters() );
|
---|
| 268 | inst->get_parameters().clear();
|
---|
| 269 | }
|
---|
[e491159] | 270 |
|
---|
[3bb195cb] | 271 | void GenericInstantiator::stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) {
|
---|
| 272 | substituteMembers( base->get_members(), baseParams, typeSubs );
|
---|
| 273 |
|
---|
| 274 | deleteAll( baseParams );
|
---|
| 275 | baseParams.clear();
|
---|
[e491159] | 276 |
|
---|
[3bb195cb] | 277 | dtypeStatics.insert( base );
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[b940dc71] | 280 | /// xxx - more or less copied from box -- these should be merged with those somehow...
|
---|
| 281 | void GenericInstantiator::replaceParametersWithConcrete( std::list< Expression* >& params ) {
|
---|
| 282 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 283 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 284 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
| 285 | paramType->set_type( replaceWithConcrete( paramType->get_type(), false ) );
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | Type *GenericInstantiator::replaceWithConcrete( Type *type, bool doClone ) {
|
---|
| 290 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[2a7b3ca] | 291 | if ( env && ! inFunctionType ) {
|
---|
[b940dc71] | 292 | Type *concrete = env->lookup( typeInst->get_name() );
|
---|
| 293 | if ( concrete ) {
|
---|
| 294 | return concrete->clone();
|
---|
| 295 | }
|
---|
| 296 | else return typeInst->clone();
|
---|
| 297 | }
|
---|
| 298 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 299 | if ( doClone ) {
|
---|
| 300 | structType = structType->clone();
|
---|
| 301 | }
|
---|
| 302 | replaceParametersWithConcrete( structType->get_parameters() );
|
---|
| 303 | return structType;
|
---|
| 304 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 305 | if ( doClone ) {
|
---|
| 306 | unionType = unionType->clone();
|
---|
| 307 | }
|
---|
| 308 | replaceParametersWithConcrete( unionType->get_parameters() );
|
---|
| 309 | return unionType;
|
---|
| 310 | }
|
---|
| 311 | return type;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 |
|
---|
[2a7b3ca] | 315 | Type* GenericInstantiator::postmutate( StructInstType *inst ) {
|
---|
[ea5daeb] | 316 | // exit early if no need for further mutation
|
---|
| 317 | if ( inst->get_parameters().empty() ) return inst;
|
---|
| 318 |
|
---|
[b940dc71] | 319 | // 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).
|
---|
| 320 | replaceWithConcrete( inst, false );
|
---|
| 321 |
|
---|
[3bb195cb] | 322 | // check for an already-instantiatiated dtype-static type
|
---|
[f18a711] | 323 | if ( dtypeStatics.find( inst->get_baseStruct() ) != dtypeStatics.end() ) {
|
---|
| 324 | stripInstParams( inst );
|
---|
| 325 | return inst;
|
---|
| 326 | }
|
---|
[e491159] | 327 |
|
---|
[ea5daeb] | 328 | // check if type can be concretely instantiated; put substitutions into typeSubs
|
---|
[b940dc71] | 329 | assertf( inst->get_baseParameters(), "Base struct has parameters" );
|
---|
[ea5daeb] | 330 | std::list< TypeExpr* > typeSubs;
|
---|
| 331 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
|
---|
| 332 | switch ( gt ) {
|
---|
[3bb195cb] | 333 | case genericType::dtypeStatic:
|
---|
| 334 | stripDtypeParams( inst->get_baseStruct(), *inst->get_baseParameters(), typeSubs );
|
---|
[f18a711] | 335 | stripInstParams( inst );
|
---|
[3bb195cb] | 336 | break;
|
---|
[e491159] | 337 |
|
---|
[3bb195cb] | 338 | case genericType::concrete: {
|
---|
[ea5daeb] | 339 | // make concrete instantiation of generic type
|
---|
| 340 | StructDecl *concDecl = lookup( inst, typeSubs );
|
---|
| 341 | if ( ! concDecl ) {
|
---|
| 342 | // set concDecl to new type, insert type declaration into statements to add
|
---|
| 343 | concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
|
---|
[2c57025] | 344 | concDecl->set_body( inst->get_baseStruct()->has_body() );
|
---|
[5a3ac84] | 345 | substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
|
---|
[c7a3081] | 346 | insert( inst, typeSubs, concDecl ); // must insert before recursion
|
---|
[2a7b3ca] | 347 | concDecl->acceptMutator( *visitor ); // recursively instantiate members
|
---|
| 348 | declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first
|
---|
[ea5daeb] | 349 | }
|
---|
| 350 | StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
|
---|
| 351 | newInst->set_baseStruct( concDecl );
|
---|
| 352 |
|
---|
| 353 | delete inst;
|
---|
| 354 | inst = newInst;
|
---|
| 355 | break;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | case genericType::dynamic:
|
---|
| 359 | // do nothing
|
---|
| 360 | break;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | deleteAll( typeSubs );
|
---|
| 364 | return inst;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[2a7b3ca] | 367 | Type* GenericInstantiator::postmutate( UnionInstType *inst ) {
|
---|
[ea5daeb] | 368 | // exit early if no need for further mutation
|
---|
| 369 | if ( inst->get_parameters().empty() ) return inst;
|
---|
[3bb195cb] | 370 |
|
---|
| 371 | // check for an already-instantiatiated dtype-static type
|
---|
[f18a711] | 372 | if ( dtypeStatics.find( inst->get_baseUnion() ) != dtypeStatics.end() ) {
|
---|
| 373 | stripInstParams( inst );
|
---|
| 374 | return inst;
|
---|
| 375 | }
|
---|
[ea5daeb] | 376 |
|
---|
| 377 | // check if type can be concretely instantiated; put substitutions into typeSubs
|
---|
[3bb195cb] | 378 | assert( inst->get_baseParameters() && "Base union has parameters" );
|
---|
[ea5daeb] | 379 | std::list< TypeExpr* > typeSubs;
|
---|
| 380 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs );
|
---|
| 381 | switch ( gt ) {
|
---|
[3bb195cb] | 382 | case genericType::dtypeStatic:
|
---|
| 383 | stripDtypeParams( inst->get_baseUnion(), *inst->get_baseParameters(), typeSubs );
|
---|
[f18a711] | 384 | stripInstParams( inst );
|
---|
[3bb195cb] | 385 | break;
|
---|
[e491159] | 386 |
|
---|
[ea5daeb] | 387 | case genericType::concrete:
|
---|
| 388 | {
|
---|
| 389 | // make concrete instantiation of generic type
|
---|
| 390 | UnionDecl *concDecl = lookup( inst, typeSubs );
|
---|
| 391 | if ( ! concDecl ) {
|
---|
| 392 | // set concDecl to new type, insert type declaration into statements to add
|
---|
| 393 | concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
|
---|
[2c57025] | 394 | concDecl->set_body( inst->get_baseUnion()->has_body() );
|
---|
[ea5daeb] | 395 | substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
|
---|
[c7a3081] | 396 | insert( inst, typeSubs, concDecl ); // must insert before recursion
|
---|
[2a7b3ca] | 397 | concDecl->acceptMutator( *visitor ); // recursively instantiate members
|
---|
| 398 | declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first
|
---|
[ea5daeb] | 399 | }
|
---|
| 400 | UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
|
---|
| 401 | newInst->set_baseUnion( concDecl );
|
---|
| 402 |
|
---|
| 403 | delete inst;
|
---|
| 404 | inst = newInst;
|
---|
| 405 | break;
|
---|
| 406 | }
|
---|
| 407 | case genericType::dynamic:
|
---|
| 408 | // do nothing
|
---|
| 409 | break;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | deleteAll( typeSubs );
|
---|
| 413 | return inst;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[2a7b3ca] | 416 | void GenericInstantiator::beginScope() {
|
---|
[ea5daeb] | 417 | instantiations.beginScope();
|
---|
[3bb195cb] | 418 | dtypeStatics.beginScope();
|
---|
[ea5daeb] | 419 | }
|
---|
| 420 |
|
---|
[2a7b3ca] | 421 | void GenericInstantiator::endScope() {
|
---|
[ea5daeb] | 422 | instantiations.endScope();
|
---|
[3bb195cb] | 423 | dtypeStatics.endScope();
|
---|
[ea5daeb] | 424 | }
|
---|
| 425 |
|
---|
| 426 | } // namespace GenPoly
|
---|
| 427 |
|
---|
| 428 | // Local Variables: //
|
---|
| 429 | // tab-width: 4 //
|
---|
| 430 | // mode: c++ //
|
---|
| 431 | // compile-command: "make install" //
|
---|
| 432 | // End: //
|
---|