[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 | |
---|
[1393df07] | 23 | #include "CodeGen/OperatorTable.h" |
---|
[08fc48f] | 24 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd |
---|
| 25 | #include "Common/ScopedMap.h" // for ScopedMap |
---|
| 26 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 27 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 28 | #include "Common/utility.h" // for deleteAll, cloneAll |
---|
| 29 | #include "GenPoly.h" // for isPolyType, typesPolyCompatible |
---|
[1393df07] | 30 | #include "InitTweak/InitTweak.h" |
---|
[b95fe40] | 31 | #include "ResolvExpr/typeops.h" |
---|
[08fc48f] | 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 |
---|
[2a7b3ca] | 40 | |
---|
[ea5daeb] | 41 | |
---|
| 42 | namespace 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 ) { |
---|
[5a3ac84] | 83 | if ( ! typesPolyCompatible( *it, *jt ) ) return false; |
---|
[ea5daeb] | 84 | } |
---|
| 85 | return true; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | std::list< Type* > params; ///< Instantiation parameters |
---|
| 89 | }; |
---|
[e491159] | 90 | |
---|
[ea5daeb] | 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* > ¶ms, Value *value ) { |
---|
[e58dfb9] | 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 | } |
---|
[ea5daeb] | 132 | } |
---|
| 133 | }; |
---|
[3bb195cb] | 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 | } |
---|
[e491159] | 156 | |
---|
[b95fe40] | 157 | /// Add cast to dtype-static member expressions so that type information is not lost in GenericInstantiator |
---|
[1393df07] | 158 | struct FixDtypeStatic final : public WithGuards, public WithVisitorRef<FixDtypeStatic>, public WithShortCircuiting, public WithStmtsToAdd { |
---|
[b95fe40] | 159 | Expression * postmutate( MemberExpr * memberExpr ); |
---|
| 160 | |
---|
[1393df07] | 161 | void premutate( ApplicationExpr * appExpr ); |
---|
| 162 | void premutate( AddressExpr * addrExpr ); |
---|
| 163 | |
---|
[b95fe40] | 164 | template<typename AggrInst> |
---|
| 165 | Expression * fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr ); |
---|
[1393df07] | 166 | |
---|
| 167 | bool isLvalueArg = false; |
---|
[b95fe40] | 168 | }; |
---|
| 169 | |
---|
[ea5daeb] | 170 | /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately |
---|
[2a7b3ca] | 171 | struct GenericInstantiator final : public WithTypeSubstitution, public WithDeclsToAdd, public WithVisitorRef<GenericInstantiator>, public WithGuards { |
---|
[ea5daeb] | 172 | /// Map of (generic type, parameter list) pairs to concrete type instantiations |
---|
| 173 | InstantiationMap< AggregateDecl, AggregateDecl > instantiations; |
---|
[3bb195cb] | 174 | /// Set of types which are dtype-only generic (and therefore have static layout) |
---|
| 175 | ScopedSet< AggregateDecl* > dtypeStatics; |
---|
[ea5daeb] | 176 | /// Namer for concrete types |
---|
| 177 | UniqueName typeNamer; |
---|
[2a7b3ca] | 178 | /// Should not make use of type environment to replace types of function parameter and return values. |
---|
| 179 | bool inFunctionType = false; |
---|
[f6582243] | 180 | /// Index of current member, used to recreate MemberExprs with the member from an instantiation |
---|
| 181 | int memberIndex = -1; |
---|
[2a7b3ca] | 182 | GenericInstantiator() : instantiations(), dtypeStatics(), typeNamer("_conc_") {} |
---|
| 183 | |
---|
| 184 | Type* postmutate( StructInstType *inst ); |
---|
| 185 | Type* postmutate( UnionInstType *inst ); |
---|
[ea5daeb] | 186 | |
---|
[f6582243] | 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 * ) { |
---|
[2a7b3ca] | 192 | GuardValue( inFunctionType ); |
---|
| 193 | inFunctionType = true; |
---|
| 194 | } |
---|
[ea5daeb] | 195 | |
---|
[2a7b3ca] | 196 | void beginScope(); |
---|
| 197 | void endScope(); |
---|
[ea5daeb] | 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 ); } |
---|
[3bb195cb] | 207 | |
---|
[b940dc71] | 208 | void replaceParametersWithConcrete( std::list< Expression* >& params ); |
---|
| 209 | Type *replaceWithConcrete( Type *type, bool doClone ); |
---|
| 210 | |
---|
[3bb195cb] | 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 ); |
---|
[ea5daeb] | 213 | }; |
---|
| 214 | |
---|
| 215 | void instantiateGeneric( std::list< Declaration* > &translationUnit ) { |
---|
[b95fe40] | 216 | PassVisitor<FixDtypeStatic> fixer; |
---|
[2a7b3ca] | 217 | PassVisitor<GenericInstantiator> instantiator; |
---|
[b95fe40] | 218 | |
---|
[4e7a4e6] | 219 | mutateAll( translationUnit, fixer ); |
---|
[2a7b3ca] | 220 | mutateAll( translationUnit, instantiator ); |
---|
[ea5daeb] | 221 | } |
---|
| 222 | |
---|
[b95fe40] | 223 | bool isDtypeStatic( const std::list< TypeDecl* >& baseParams ) { |
---|
| 224 | return std::all_of( baseParams.begin(), baseParams.end(), []( TypeDecl * td ) { return ! td->isComplete(); } ); |
---|
| 225 | } |
---|
| 226 | |
---|
[3bb195cb] | 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 |
---|
[ea5daeb] | 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"); |
---|
[e491159] | 238 | |
---|
[0bfaf80] | 239 | if ( (*baseParam)->isComplete() ) { |
---|
[87c3bef] | 240 | // substitute parameter for complete (otype or sized dtype) type |
---|
[5a3ac84] | 241 | if ( isPolyType( paramType->get_type() ) ) { |
---|
| 242 | // substitute polymorphic parameter type in to generic type |
---|
[87c3bef] | 243 | out.push_back( paramType->clone() ); |
---|
[5a3ac84] | 244 | gt = genericType::dynamic; |
---|
| 245 | } else { |
---|
| 246 | // normalize possibly dtype-static parameter type |
---|
[6db9dab] | 247 | out.push_back( new TypeExpr{ |
---|
[5a3ac84] | 248 | ScrubTyVars::scrubAll( paramType->get_type()->clone() ) } ); |
---|
| 249 | gt |= genericType::concrete; |
---|
[87c3bef] | 250 | } |
---|
[0bfaf80] | 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; |
---|
[f0ecf9b] | 263 | default: |
---|
| 264 | assertf( false, "Unhandled type parameter kind" ); |
---|
[0bfaf80] | 265 | break; |
---|
[ea5daeb] | 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[b2daebd4] | 269 | assertf( baseParam == baseParams.end() && param == params.end(), "Type parameters should match type variables" ); |
---|
[ea5daeb] | 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 | |
---|
[3bb195cb] | 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 | |
---|
[f18a711] | 294 | /// Strips the instances's type parameters |
---|
| 295 | void stripInstParams( ReferenceToType *inst ) { |
---|
[3bb195cb] | 296 | deleteAll( inst->get_parameters() ); |
---|
| 297 | inst->get_parameters().clear(); |
---|
| 298 | } |
---|
[e491159] | 299 | |
---|
[3bb195cb] | 300 | void GenericInstantiator::stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) { |
---|
| 301 | substituteMembers( base->get_members(), baseParams, typeSubs ); |
---|
| 302 | |
---|
[760ba67] | 303 | // xxx - can't delete type parameters because they may have assertions that are used |
---|
| 304 | // deleteAll( baseParams ); |
---|
[3bb195cb] | 305 | baseParams.clear(); |
---|
[e491159] | 306 | |
---|
[3bb195cb] | 307 | dtypeStatics.insert( base ); |
---|
| 308 | } |
---|
| 309 | |
---|
[b940dc71] | 310 | /// xxx - more or less copied from box -- these should be merged with those somehow... |
---|
| 311 | void GenericInstantiator::replaceParametersWithConcrete( std::list< Expression* >& params ) { |
---|
| 312 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 313 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
| 314 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
| 315 | paramType->set_type( replaceWithConcrete( paramType->get_type(), false ) ); |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | Type *GenericInstantiator::replaceWithConcrete( Type *type, bool doClone ) { |
---|
| 320 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
[2a7b3ca] | 321 | if ( env && ! inFunctionType ) { |
---|
[b940dc71] | 322 | Type *concrete = env->lookup( typeInst->get_name() ); |
---|
| 323 | if ( concrete ) { |
---|
| 324 | return concrete->clone(); |
---|
| 325 | } |
---|
| 326 | else return typeInst->clone(); |
---|
| 327 | } |
---|
| 328 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 329 | if ( doClone ) { |
---|
| 330 | structType = structType->clone(); |
---|
| 331 | } |
---|
| 332 | replaceParametersWithConcrete( structType->get_parameters() ); |
---|
| 333 | return structType; |
---|
| 334 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 335 | if ( doClone ) { |
---|
| 336 | unionType = unionType->clone(); |
---|
| 337 | } |
---|
| 338 | replaceParametersWithConcrete( unionType->get_parameters() ); |
---|
| 339 | return unionType; |
---|
| 340 | } |
---|
| 341 | return type; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | |
---|
[2a7b3ca] | 345 | Type* GenericInstantiator::postmutate( StructInstType *inst ) { |
---|
[ea5daeb] | 346 | // exit early if no need for further mutation |
---|
| 347 | if ( inst->get_parameters().empty() ) return inst; |
---|
| 348 | |
---|
[b940dc71] | 349 | // 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). |
---|
| 350 | replaceWithConcrete( inst, false ); |
---|
| 351 | |
---|
[3bb195cb] | 352 | // check for an already-instantiatiated dtype-static type |
---|
[f18a711] | 353 | if ( dtypeStatics.find( inst->get_baseStruct() ) != dtypeStatics.end() ) { |
---|
| 354 | stripInstParams( inst ); |
---|
| 355 | return inst; |
---|
| 356 | } |
---|
[e491159] | 357 | |
---|
[ea5daeb] | 358 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
[b940dc71] | 359 | assertf( inst->get_baseParameters(), "Base struct has parameters" ); |
---|
[ea5daeb] | 360 | std::list< TypeExpr* > typeSubs; |
---|
| 361 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ); |
---|
| 362 | switch ( gt ) { |
---|
[3bb195cb] | 363 | case genericType::dtypeStatic: |
---|
| 364 | stripDtypeParams( inst->get_baseStruct(), *inst->get_baseParameters(), typeSubs ); |
---|
[f18a711] | 365 | stripInstParams( inst ); |
---|
[3bb195cb] | 366 | break; |
---|
[e491159] | 367 | |
---|
[3bb195cb] | 368 | case genericType::concrete: { |
---|
[ea5daeb] | 369 | // make concrete instantiation of generic type |
---|
| 370 | StructDecl *concDecl = lookup( inst, typeSubs ); |
---|
| 371 | if ( ! concDecl ) { |
---|
| 372 | // set concDecl to new type, insert type declaration into statements to add |
---|
| 373 | concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) ); |
---|
[2c57025] | 374 | concDecl->set_body( inst->get_baseStruct()->has_body() ); |
---|
[5a3ac84] | 375 | substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
[c7a3081] | 376 | insert( inst, typeSubs, concDecl ); // must insert before recursion |
---|
[2a7b3ca] | 377 | concDecl->acceptMutator( *visitor ); // recursively instantiate members |
---|
| 378 | declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first |
---|
[ea5daeb] | 379 | } |
---|
| 380 | StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
| 381 | newInst->set_baseStruct( concDecl ); |
---|
| 382 | |
---|
| 383 | delete inst; |
---|
| 384 | inst = newInst; |
---|
| 385 | break; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | case genericType::dynamic: |
---|
| 389 | // do nothing |
---|
| 390 | break; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | deleteAll( typeSubs ); |
---|
| 394 | return inst; |
---|
| 395 | } |
---|
| 396 | |
---|
[2a7b3ca] | 397 | Type* GenericInstantiator::postmutate( UnionInstType *inst ) { |
---|
[ea5daeb] | 398 | // exit early if no need for further mutation |
---|
| 399 | if ( inst->get_parameters().empty() ) return inst; |
---|
[3bb195cb] | 400 | |
---|
| 401 | // check for an already-instantiatiated dtype-static type |
---|
[f18a711] | 402 | if ( dtypeStatics.find( inst->get_baseUnion() ) != dtypeStatics.end() ) { |
---|
| 403 | stripInstParams( inst ); |
---|
| 404 | return inst; |
---|
| 405 | } |
---|
[ea5daeb] | 406 | |
---|
| 407 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
[3bb195cb] | 408 | assert( inst->get_baseParameters() && "Base union has parameters" ); |
---|
[ea5daeb] | 409 | std::list< TypeExpr* > typeSubs; |
---|
| 410 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ); |
---|
| 411 | switch ( gt ) { |
---|
[3bb195cb] | 412 | case genericType::dtypeStatic: |
---|
| 413 | stripDtypeParams( inst->get_baseUnion(), *inst->get_baseParameters(), typeSubs ); |
---|
[f18a711] | 414 | stripInstParams( inst ); |
---|
[3bb195cb] | 415 | break; |
---|
[e491159] | 416 | |
---|
[ea5daeb] | 417 | case genericType::concrete: |
---|
| 418 | { |
---|
| 419 | // make concrete instantiation of generic type |
---|
| 420 | UnionDecl *concDecl = lookup( inst, typeSubs ); |
---|
| 421 | if ( ! concDecl ) { |
---|
| 422 | // set concDecl to new type, insert type declaration into statements to add |
---|
| 423 | concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) ); |
---|
[2c57025] | 424 | concDecl->set_body( inst->get_baseUnion()->has_body() ); |
---|
[ea5daeb] | 425 | substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
[c7a3081] | 426 | insert( inst, typeSubs, concDecl ); // must insert before recursion |
---|
[2a7b3ca] | 427 | concDecl->acceptMutator( *visitor ); // recursively instantiate members |
---|
| 428 | declsToAddBefore.push_back( concDecl ); // must occur before declaration is added so that member instantiations appear first |
---|
[ea5daeb] | 429 | } |
---|
| 430 | UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
| 431 | newInst->set_baseUnion( concDecl ); |
---|
| 432 | |
---|
| 433 | delete inst; |
---|
| 434 | inst = newInst; |
---|
| 435 | break; |
---|
| 436 | } |
---|
| 437 | case genericType::dynamic: |
---|
| 438 | // do nothing |
---|
| 439 | break; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | deleteAll( typeSubs ); |
---|
| 443 | return inst; |
---|
| 444 | } |
---|
| 445 | |
---|
[f6582243] | 446 | namespace { |
---|
| 447 | bool isGenericType( Type * t ) { |
---|
| 448 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( t ) ) { |
---|
| 449 | return ! inst->parameters.empty(); |
---|
| 450 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) { |
---|
| 451 | return ! inst->parameters.empty(); |
---|
| 452 | } |
---|
| 453 | return false; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | void GenericInstantiator::premutate( MemberExpr * memberExpr ) { |
---|
| 458 | GuardValue( memberIndex ); |
---|
| 459 | memberIndex = -1; |
---|
| 460 | if ( isGenericType( memberExpr->aggregate->result ) ) { |
---|
| 461 | // find the location of the member |
---|
[373d0b5] | 462 | AggregateDecl * aggr = memberExpr->aggregate->result->getAggr(); |
---|
[f6582243] | 463 | std::list< Declaration * > & members = aggr->members; |
---|
| 464 | memberIndex = std::distance( members.begin(), std::find( members.begin(), members.end(), memberExpr->member ) ); |
---|
| 465 | assertf( memberIndex < (int)members.size(), "Could not find member %s in generic type %s", toString( memberExpr->member ).c_str(), toString( memberExpr->aggregate ).c_str() ); |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | Expression * GenericInstantiator::postmutate( MemberExpr * memberExpr ) { |
---|
| 470 | if ( memberIndex != -1 ) { |
---|
| 471 | // using the location from the generic type, find the member in the instantiation and rebuild the member expression |
---|
[373d0b5] | 472 | AggregateDecl * aggr = memberExpr->aggregate->result->getAggr(); |
---|
[f6582243] | 473 | assertf( memberIndex < (int)aggr->members.size(), "Instantiation somehow has fewer members than the generic type." ); |
---|
| 474 | Declaration * member = *std::next( aggr->members.begin(), memberIndex ); |
---|
| 475 | 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] | 476 | DeclarationWithType * field = strict_dynamic_cast< DeclarationWithType * >( member ); |
---|
[f6582243] | 477 | MemberExpr * ret = new MemberExpr( field, memberExpr->aggregate->clone() ); |
---|
| 478 | std::swap( ret->env, memberExpr->env ); |
---|
| 479 | delete memberExpr; |
---|
| 480 | return ret; |
---|
| 481 | } |
---|
| 482 | return memberExpr; |
---|
| 483 | } |
---|
| 484 | |
---|
[2a7b3ca] | 485 | void GenericInstantiator::beginScope() { |
---|
[ea5daeb] | 486 | instantiations.beginScope(); |
---|
[3bb195cb] | 487 | dtypeStatics.beginScope(); |
---|
[ea5daeb] | 488 | } |
---|
| 489 | |
---|
[2a7b3ca] | 490 | void GenericInstantiator::endScope() { |
---|
[ea5daeb] | 491 | instantiations.endScope(); |
---|
[3bb195cb] | 492 | dtypeStatics.endScope(); |
---|
[ea5daeb] | 493 | } |
---|
| 494 | |
---|
[b95fe40] | 495 | template< typename AggrInst > |
---|
| 496 | Expression * FixDtypeStatic::fixMemberExpr( AggrInst * inst, MemberExpr * memberExpr ) { |
---|
| 497 | // need to cast dtype-static member expressions to their actual type before that type is erased. |
---|
| 498 | auto & baseParams = *inst->get_baseParameters(); |
---|
| 499 | if ( isDtypeStatic( baseParams ) ) { |
---|
| 500 | if ( ! ResolvExpr::typesCompatible( memberExpr->result, memberExpr->member->get_type(), SymTab::Indexer() ) ) { |
---|
[1393df07] | 501 | // type of member and type of expression differ |
---|
| 502 | Type * concType = memberExpr->result->clone(); |
---|
| 503 | if ( isLvalueArg ) { |
---|
| 504 | // result must be C lvalue, so make a new reference variable with the correct actual type to replace the member expression |
---|
| 505 | // forall(dtype T) |
---|
| 506 | // struct Ptr { |
---|
| 507 | // T * x |
---|
| 508 | // }; |
---|
| 509 | // Ptr(int) p; |
---|
| 510 | // int i; |
---|
| 511 | // p.x = &i; |
---|
| 512 | // becomes |
---|
| 513 | // int *& _dtype_static_member_0 = (int **)&p.x; |
---|
| 514 | // _dtype_static_member_0 = &i; |
---|
| 515 | // Note: this currently creates more temporaries than is strictly necessary, since it does not check for duplicate uses of the same member expression. |
---|
| 516 | static UniqueName tmpNamer( "_dtype_static_member_" ); |
---|
| 517 | Expression * init = new CastExpr( new AddressExpr( memberExpr ), new PointerType( Type::Qualifiers(), concType->clone() ) ); |
---|
| 518 | ObjectDecl * tmp = ObjectDecl::newObject( tmpNamer.newName(), new ReferenceType( Type::Qualifiers(), concType ), new SingleInit( init ) ); |
---|
[ba3706f] | 519 | stmtsToAddBefore.push_back( new DeclStmt( tmp ) ); |
---|
[1393df07] | 520 | return new VariableExpr( tmp ); |
---|
| 521 | } else { |
---|
| 522 | // can simply add a cast to actual type |
---|
| 523 | return new CastExpr( memberExpr, concType ); |
---|
| 524 | } |
---|
[b95fe40] | 525 | } |
---|
| 526 | } |
---|
| 527 | return memberExpr; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | Expression * FixDtypeStatic::postmutate( MemberExpr * memberExpr ) { |
---|
| 531 | Type * aggrType = memberExpr->aggregate->result; |
---|
| 532 | if ( isGenericType( aggrType ) ) { |
---|
| 533 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( aggrType ) ) { |
---|
| 534 | return fixMemberExpr( inst, memberExpr ); |
---|
| 535 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( aggrType ) ) { |
---|
| 536 | return fixMemberExpr( inst, memberExpr ); |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | return memberExpr; |
---|
| 540 | } |
---|
| 541 | |
---|
[1393df07] | 542 | void FixDtypeStatic::premutate( ApplicationExpr * appExpr ) { |
---|
| 543 | GuardValue( isLvalueArg ); |
---|
| 544 | isLvalueArg = false; |
---|
| 545 | DeclarationWithType * function = InitTweak::getFunction( appExpr ); |
---|
| 546 | if ( function->linkage == LinkageSpec::Intrinsic && CodeGen::isAssignment( function->name ) ) { |
---|
| 547 | // explicitly visit children because only the first argument must be a C lvalue. |
---|
| 548 | visit_children = false; |
---|
| 549 | appExpr->env = maybeMutate( appExpr->env, *visitor ); |
---|
| 550 | appExpr->result = maybeMutate( appExpr->result, *visitor ); |
---|
| 551 | appExpr->function = maybeMutate( appExpr->function, *visitor ); |
---|
| 552 | isLvalueArg = true; |
---|
| 553 | for ( Expression * arg : appExpr->args ) { |
---|
| 554 | arg = maybeMutate( arg, *visitor ); |
---|
| 555 | isLvalueArg = false; |
---|
| 556 | } |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | void FixDtypeStatic::premutate( AddressExpr * ) { |
---|
| 561 | // argument of & must be C lvalue |
---|
| 562 | GuardValue( isLvalueArg ); |
---|
| 563 | isLvalueArg = true; |
---|
| 564 | } |
---|
[ea5daeb] | 565 | } // namespace GenPoly |
---|
| 566 | |
---|
| 567 | // Local Variables: // |
---|
| 568 | // tab-width: 4 // |
---|
| 569 | // mode: c++ // |
---|
| 570 | // compile-command: "make install" // |
---|
| 571 | // End: // |
---|