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