[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> |
---|
| 20 | |
---|
| 21 | #include "InstantiateGeneric.h" |
---|
| 22 | |
---|
| 23 | #include "DeclMutator.h" |
---|
| 24 | #include "GenPoly.h" |
---|
| 25 | #include "ScopedMap.h" |
---|
[3bb195cb] | 26 | #include "ScopedSet.h" |
---|
[ea5daeb] | 27 | |
---|
| 28 | #include "ResolvExpr/typeops.h" |
---|
| 29 | |
---|
| 30 | #include "SynTree/Declaration.h" |
---|
| 31 | #include "SynTree/Expression.h" |
---|
| 32 | #include "SynTree/Type.h" |
---|
| 33 | |
---|
| 34 | #include "Common/UniqueName.h" |
---|
| 35 | #include "Common/utility.h" |
---|
| 36 | |
---|
| 37 | namespace GenPoly { |
---|
| 38 | |
---|
| 39 | /// Abstracts type equality for a list of parameter types |
---|
| 40 | struct TypeList { |
---|
| 41 | TypeList() : params() {} |
---|
| 42 | TypeList( const std::list< Type* > &_params ) : params() { cloneAll(_params, params); } |
---|
| 43 | TypeList( std::list< Type* > &&_params ) : params( _params ) {} |
---|
| 44 | |
---|
| 45 | TypeList( const TypeList &that ) : params() { cloneAll(that.params, params); } |
---|
| 46 | TypeList( TypeList &&that ) : params( std::move( that.params ) ) {} |
---|
| 47 | |
---|
| 48 | /// Extracts types from a list of TypeExpr* |
---|
| 49 | TypeList( const std::list< TypeExpr* >& _params ) : params() { |
---|
| 50 | for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) { |
---|
| 51 | params.push_back( (*param)->get_type()->clone() ); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | TypeList& operator= ( const TypeList &that ) { |
---|
| 56 | deleteAll( params ); |
---|
| 57 | |
---|
| 58 | params.clear(); |
---|
| 59 | cloneAll( that.params, params ); |
---|
| 60 | |
---|
| 61 | return *this; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | TypeList& operator= ( TypeList &&that ) { |
---|
| 65 | deleteAll( params ); |
---|
| 66 | |
---|
| 67 | params = std::move( that.params ); |
---|
| 68 | |
---|
| 69 | return *this; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | ~TypeList() { deleteAll( params ); } |
---|
| 73 | |
---|
| 74 | bool operator== ( const TypeList& that ) const { |
---|
| 75 | if ( params.size() != that.params.size() ) return false; |
---|
| 76 | |
---|
| 77 | SymTab::Indexer dummy; |
---|
| 78 | for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) { |
---|
| 79 | if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false; |
---|
| 80 | } |
---|
| 81 | return true; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | std::list< Type* > params; ///< Instantiation parameters |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | /// Maps a key and a TypeList to the some value, accounting for scope |
---|
| 88 | template< typename Key, typename Value > |
---|
| 89 | class InstantiationMap { |
---|
| 90 | /// Wraps value for a specific (Key, TypeList) combination |
---|
| 91 | typedef std::pair< TypeList, Value* > Instantiation; |
---|
| 92 | /// List of TypeLists paired with their appropriate values |
---|
| 93 | typedef std::vector< Instantiation > ValueList; |
---|
| 94 | /// Underlying map type; maps keys to a linear list of corresponding TypeLists and values |
---|
| 95 | typedef ScopedMap< Key*, ValueList > InnerMap; |
---|
| 96 | |
---|
| 97 | InnerMap instantiations; ///< instantiations |
---|
| 98 | |
---|
| 99 | public: |
---|
| 100 | /// Starts a new scope |
---|
| 101 | void beginScope() { instantiations.beginScope(); } |
---|
| 102 | |
---|
| 103 | /// Ends a scope |
---|
| 104 | void endScope() { instantiations.endScope(); } |
---|
| 105 | |
---|
| 106 | /// Gets the value for the (key, typeList) pair, returns NULL on none such. |
---|
| 107 | Value *lookup( Key *key, const std::list< TypeExpr* >& params ) const { |
---|
| 108 | TypeList typeList( params ); |
---|
| 109 | |
---|
| 110 | // scan scopes for matches to the key |
---|
| 111 | for ( typename InnerMap::const_iterator insts = instantiations.find( key ); insts != instantiations.end(); insts = instantiations.findNext( insts, key ) ) { |
---|
| 112 | for ( typename ValueList::const_reverse_iterator inst = insts->second.rbegin(); inst != insts->second.rend(); ++inst ) { |
---|
| 113 | if ( inst->first == typeList ) return inst->second; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | // no matching instantiations found |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /// Adds a value for a (key, typeList) pair to the current scope |
---|
| 121 | void insert( Key *key, const std::list< TypeExpr* > ¶ms, Value *value ) { |
---|
| 122 | instantiations[ key ].push_back( Instantiation( TypeList( params ), value ) ); |
---|
| 123 | } |
---|
| 124 | }; |
---|
[3bb195cb] | 125 | |
---|
| 126 | /// Possible options for a given specialization of a generic type |
---|
| 127 | enum class genericType { |
---|
| 128 | dtypeStatic, ///< Concrete instantiation based solely on {d,f}type-to-void conversions |
---|
| 129 | concrete, ///< Concrete instantiation requiring at least one parameter type |
---|
| 130 | dynamic ///< No concrete instantiation |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | genericType& operator |= ( genericType& gt, const genericType& ht ) { |
---|
| 134 | switch ( gt ) { |
---|
| 135 | case genericType::dtypeStatic: |
---|
| 136 | gt = ht; |
---|
| 137 | break; |
---|
| 138 | case genericType::concrete: |
---|
| 139 | if ( ht == genericType::dynamic ) { gt = genericType::dynamic; } |
---|
| 140 | break; |
---|
| 141 | case genericType::dynamic: |
---|
| 142 | // nothing possible |
---|
| 143 | break; |
---|
| 144 | } |
---|
| 145 | return gt; |
---|
| 146 | } |
---|
[ea5daeb] | 147 | |
---|
| 148 | /// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately |
---|
| 149 | class GenericInstantiator : public DeclMutator { |
---|
| 150 | /// Map of (generic type, parameter list) pairs to concrete type instantiations |
---|
| 151 | InstantiationMap< AggregateDecl, AggregateDecl > instantiations; |
---|
[3bb195cb] | 152 | /// Set of types which are dtype-only generic (and therefore have static layout) |
---|
| 153 | ScopedSet< AggregateDecl* > dtypeStatics; |
---|
[ea5daeb] | 154 | /// Namer for concrete types |
---|
| 155 | UniqueName typeNamer; |
---|
| 156 | |
---|
| 157 | public: |
---|
[3bb195cb] | 158 | GenericInstantiator() : DeclMutator(), instantiations(), dtypeStatics(), typeNamer("_conc_") {} |
---|
[ea5daeb] | 159 | |
---|
| 160 | virtual Type* mutate( StructInstType *inst ); |
---|
| 161 | virtual Type* mutate( UnionInstType *inst ); |
---|
| 162 | |
---|
| 163 | virtual void doBeginScope(); |
---|
| 164 | virtual void doEndScope(); |
---|
| 165 | private: |
---|
| 166 | /// Wrap instantiation lookup for structs |
---|
| 167 | StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); } |
---|
| 168 | /// Wrap instantiation lookup for unions |
---|
| 169 | UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); } |
---|
| 170 | /// Wrap instantiation insertion for structs |
---|
| 171 | void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); } |
---|
| 172 | /// Wrap instantiation insertion for unions |
---|
| 173 | void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); } |
---|
[3bb195cb] | 174 | |
---|
| 175 | /// Strips a dtype-static aggregate decl of its type parameters, marks it as stripped |
---|
| 176 | void stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ); |
---|
[ea5daeb] | 177 | }; |
---|
| 178 | |
---|
| 179 | void instantiateGeneric( std::list< Declaration* > &translationUnit ) { |
---|
| 180 | GenericInstantiator instantiator; |
---|
| 181 | instantiator.mutateDeclarationList( translationUnit ); |
---|
| 182 | } |
---|
| 183 | |
---|
[3bb195cb] | 184 | /// Makes substitutions of params into baseParams; returns dtypeStatic if there is a concrete instantiation based only on {d,f}type-to-void conversions, |
---|
| 185 | /// concrete if there is a concrete instantiation requiring at least one parameter type, and dynamic if there is no concrete instantiation |
---|
[ea5daeb] | 186 | genericType makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) { |
---|
| 187 | genericType gt = genericType::dtypeStatic; |
---|
| 188 | |
---|
| 189 | // substitute concrete types for given parameters, and incomplete types for placeholders |
---|
| 190 | std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin(); |
---|
| 191 | std::list< Expression* >::const_iterator param = params.begin(); |
---|
| 192 | for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) { |
---|
| 193 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
| 194 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
| 195 | |
---|
| 196 | switch ( (*baseParam)->get_kind() ) { |
---|
| 197 | case TypeDecl::Any: { |
---|
| 198 | // substitute parameter for otype; makes the type concrete or dynamic depending on the parameter |
---|
| 199 | out.push_back( paramType->clone() ); |
---|
| 200 | gt |= isPolyType( paramType->get_type() ) ? genericType::dynamic : genericType::concrete; |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | case TypeDecl::Dtype: |
---|
| 204 | // can pretend that any dtype is `void` |
---|
| 205 | out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) ); |
---|
| 206 | break; |
---|
| 207 | case TypeDecl::Ftype: |
---|
| 208 | // can pretend that any ftype is `void (*)(void)` |
---|
| 209 | out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) ); |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | assert( baseParam == baseParams.end() && param == params.end() && "Type parameters should match type variables" ); |
---|
| 215 | return gt; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out |
---|
| 219 | void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs, |
---|
| 220 | std::list< Declaration* >& out ) { |
---|
| 221 | // substitute types into new members |
---|
| 222 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
| 223 | for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) { |
---|
| 224 | Declaration *newMember = (*member)->clone(); |
---|
| 225 | subs.apply(newMember); |
---|
| 226 | out.push_back( newMember ); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
[3bb195cb] | 230 | /// Substitutes types of members according to baseParams => typeSubs, working in-place |
---|
| 231 | void substituteMembers( std::list< Declaration* >& members, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) { |
---|
| 232 | // substitute types into new members |
---|
| 233 | TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() ); |
---|
| 234 | for ( std::list< Declaration* >::iterator member = members.begin(); member != members.end(); ++member ) { |
---|
| 235 | subs.apply(*member); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[f18a711] | 239 | /// Strips the instances's type parameters |
---|
| 240 | void stripInstParams( ReferenceToType *inst ) { |
---|
[3bb195cb] | 241 | deleteAll( inst->get_parameters() ); |
---|
| 242 | inst->get_parameters().clear(); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void GenericInstantiator::stripDtypeParams( AggregateDecl *base, std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs ) { |
---|
| 246 | substituteMembers( base->get_members(), baseParams, typeSubs ); |
---|
| 247 | |
---|
| 248 | deleteAll( baseParams ); |
---|
| 249 | baseParams.clear(); |
---|
| 250 | |
---|
| 251 | dtypeStatics.insert( base ); |
---|
| 252 | } |
---|
| 253 | |
---|
[ea5daeb] | 254 | Type* GenericInstantiator::mutate( StructInstType *inst ) { |
---|
| 255 | // mutate subtypes |
---|
| 256 | Type *mutated = Mutator::mutate( inst ); |
---|
| 257 | inst = dynamic_cast< StructInstType* >( mutated ); |
---|
| 258 | if ( ! inst ) return mutated; |
---|
| 259 | |
---|
| 260 | // exit early if no need for further mutation |
---|
| 261 | if ( inst->get_parameters().empty() ) return inst; |
---|
| 262 | |
---|
[3bb195cb] | 263 | // check for an already-instantiatiated dtype-static type |
---|
[f18a711] | 264 | if ( dtypeStatics.find( inst->get_baseStruct() ) != dtypeStatics.end() ) { |
---|
| 265 | stripInstParams( inst ); |
---|
| 266 | return inst; |
---|
| 267 | } |
---|
[3bb195cb] | 268 | |
---|
[ea5daeb] | 269 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
[3bb195cb] | 270 | assert( inst->get_baseParameters() && "Base struct has parameters" ); |
---|
[ea5daeb] | 271 | std::list< TypeExpr* > typeSubs; |
---|
| 272 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ); |
---|
| 273 | switch ( gt ) { |
---|
[3bb195cb] | 274 | case genericType::dtypeStatic: |
---|
| 275 | stripDtypeParams( inst->get_baseStruct(), *inst->get_baseParameters(), typeSubs ); |
---|
[f18a711] | 276 | stripInstParams( inst ); |
---|
[3bb195cb] | 277 | break; |
---|
| 278 | |
---|
| 279 | case genericType::concrete: { |
---|
[ea5daeb] | 280 | // make concrete instantiation of generic type |
---|
| 281 | StructDecl *concDecl = lookup( inst, typeSubs ); |
---|
| 282 | if ( ! concDecl ) { |
---|
| 283 | // set concDecl to new type, insert type declaration into statements to add |
---|
| 284 | concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) ); |
---|
| 285 | substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
| 286 | DeclMutator::addDeclaration( concDecl ); |
---|
| 287 | insert( inst, typeSubs, concDecl ); |
---|
| 288 | } |
---|
| 289 | StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
| 290 | newInst->set_baseStruct( concDecl ); |
---|
| 291 | |
---|
| 292 | delete inst; |
---|
| 293 | inst = newInst; |
---|
| 294 | break; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | case genericType::dynamic: |
---|
| 298 | // do nothing |
---|
| 299 | break; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | deleteAll( typeSubs ); |
---|
| 303 | return inst; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | Type* GenericInstantiator::mutate( UnionInstType *inst ) { |
---|
| 307 | // mutate subtypes |
---|
| 308 | Type *mutated = Mutator::mutate( inst ); |
---|
| 309 | inst = dynamic_cast< UnionInstType* >( mutated ); |
---|
| 310 | if ( ! inst ) return mutated; |
---|
| 311 | |
---|
| 312 | // exit early if no need for further mutation |
---|
| 313 | if ( inst->get_parameters().empty() ) return inst; |
---|
[3bb195cb] | 314 | |
---|
| 315 | // check for an already-instantiatiated dtype-static type |
---|
[f18a711] | 316 | if ( dtypeStatics.find( inst->get_baseUnion() ) != dtypeStatics.end() ) { |
---|
| 317 | stripInstParams( inst ); |
---|
| 318 | return inst; |
---|
| 319 | } |
---|
[ea5daeb] | 320 | |
---|
| 321 | // check if type can be concretely instantiated; put substitutions into typeSubs |
---|
[3bb195cb] | 322 | assert( inst->get_baseParameters() && "Base union has parameters" ); |
---|
[ea5daeb] | 323 | std::list< TypeExpr* > typeSubs; |
---|
| 324 | genericType gt = makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ); |
---|
| 325 | switch ( gt ) { |
---|
[3bb195cb] | 326 | case genericType::dtypeStatic: |
---|
| 327 | stripDtypeParams( inst->get_baseUnion(), *inst->get_baseParameters(), typeSubs ); |
---|
[f18a711] | 328 | stripInstParams( inst ); |
---|
[3bb195cb] | 329 | break; |
---|
| 330 | |
---|
[ea5daeb] | 331 | case genericType::concrete: |
---|
| 332 | { |
---|
| 333 | // make concrete instantiation of generic type |
---|
| 334 | UnionDecl *concDecl = lookup( inst, typeSubs ); |
---|
| 335 | if ( ! concDecl ) { |
---|
| 336 | // set concDecl to new type, insert type declaration into statements to add |
---|
| 337 | concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) ); |
---|
| 338 | substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() ); |
---|
| 339 | DeclMutator::addDeclaration( concDecl ); |
---|
| 340 | insert( inst, typeSubs, concDecl ); |
---|
| 341 | } |
---|
| 342 | UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() ); |
---|
| 343 | newInst->set_baseUnion( concDecl ); |
---|
| 344 | |
---|
| 345 | delete inst; |
---|
| 346 | inst = newInst; |
---|
| 347 | break; |
---|
| 348 | } |
---|
| 349 | case genericType::dynamic: |
---|
| 350 | // do nothing |
---|
| 351 | break; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | deleteAll( typeSubs ); |
---|
| 355 | return inst; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | void GenericInstantiator::doBeginScope() { |
---|
| 359 | DeclMutator::doBeginScope(); |
---|
| 360 | instantiations.beginScope(); |
---|
[3bb195cb] | 361 | dtypeStatics.beginScope(); |
---|
[ea5daeb] | 362 | } |
---|
| 363 | |
---|
| 364 | void GenericInstantiator::doEndScope() { |
---|
| 365 | DeclMutator::doEndScope(); |
---|
| 366 | instantiations.endScope(); |
---|
[3bb195cb] | 367 | dtypeStatics.endScope(); |
---|
[ea5daeb] | 368 | } |
---|
| 369 | |
---|
| 370 | } // namespace GenPoly |
---|
| 371 | |
---|
| 372 | // Local Variables: // |
---|
| 373 | // tab-width: 4 // |
---|
| 374 | // mode: c++ // |
---|
| 375 | // compile-command: "make install" // |
---|
| 376 | // End: // |
---|