[1c0657a] | 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 | // InstantiateGenericNew.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Tue Aug 16 10:51:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Tue Sep 13 16:03:00 2022 |
---|
| 13 | // Update Count : 0 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include "InstantiateGeneric.h" |
---|
| 17 | |
---|
| 18 | #include <cassert> // for assertf, assert |
---|
| 19 | #include <set> // for set |
---|
| 20 | #include <utility> // for move, pair |
---|
| 21 | #include <vector> // for vector |
---|
| 22 | |
---|
| 23 | #include "AST/Copy.hpp" // for deepCopy |
---|
[8f1e035] | 24 | #include "AST/Create.hpp" // for asForward |
---|
[e01eb4a] | 25 | #include "AST/Inspect.hpp" // for getFunction |
---|
[1c0657a] | 26 | #include "AST/Pass.hpp" // for Pass, WithGuard, WithShortCi... |
---|
| 27 | #include "AST/TranslationUnit.hpp" // for TranslationUnit |
---|
[bc899d6] | 28 | #include "AST/Vector.hpp" // for vector |
---|
[1c0657a] | 29 | #include "CodeGen/OperatorTable.h" // for isAssignment |
---|
| 30 | #include "Common/ScopedMap.h" // for ScopedMap |
---|
| 31 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 32 | #include "GenPoly/GenPoly.h" // for isPolyType, typesPolyCompatible |
---|
| 33 | #include "GenPoly/ScrubTyVars.h" // for scrubAll |
---|
| 34 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
| 35 | |
---|
| 36 | namespace GenPoly { |
---|
| 37 | |
---|
| 38 | namespace { |
---|
| 39 | |
---|
| 40 | // Utilities: |
---|
| 41 | |
---|
[bc899d6] | 42 | using type_vector = ast::vector< ast::TypeExpr >; |
---|
[1c0657a] | 43 | |
---|
| 44 | /// Abstracts type equality for a list of parameter types. |
---|
| 45 | struct TypeList { |
---|
| 46 | TypeList() : params() {} |
---|
[bc899d6] | 47 | TypeList( ast::vector< ast::Type > const & params ) : |
---|
[1c0657a] | 48 | params( params ) {} |
---|
[bc899d6] | 49 | TypeList( ast::vector< ast::Type > && params ) : |
---|
[1c0657a] | 50 | params( std::move( params ) ) {} |
---|
| 51 | TypeList( TypeList const & that ) : params( that.params ) {} |
---|
| 52 | TypeList( TypeList && that ) : params( std::move( that.params ) ) {} |
---|
| 53 | |
---|
[bc899d6] | 54 | TypeList( ast::vector< ast::TypeExpr > const & exprs ) : |
---|
[1c0657a] | 55 | params() { |
---|
| 56 | for ( auto expr : exprs ) { |
---|
| 57 | params.emplace_back( ast::deepCopy( expr->type ) ); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | TypeList & operator=( TypeList const & that ) { |
---|
| 62 | params = that.params; |
---|
| 63 | return *this; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | TypeList & operator=( TypeList && that ) { |
---|
| 67 | params = std::move( that.params ); |
---|
| 68 | return *this; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | bool operator==( TypeList const & that ) const { |
---|
| 72 | if ( params.size() != that.params.size() ) { |
---|
| 73 | return false; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | for ( auto it = params.begin(), jt = that.params.begin() ; |
---|
| 77 | it != params.end() ; ++it, ++jt ) { |
---|
| 78 | if ( !typesPolyCompatible( it->get(), jt->get() ) ) { |
---|
| 79 | return false; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | return true; |
---|
| 83 | } |
---|
| 84 | |
---|
[bc899d6] | 85 | ast::vector<ast::Type> params; |
---|
[1c0657a] | 86 | }; |
---|
| 87 | |
---|
| 88 | /// Maps a key and a TypeList to a valuue. Also supports scoping. |
---|
[c36814a] | 89 | class InstantiationMap final { |
---|
[1c0657a] | 90 | /// Wraps value for a specific (AggregateDecl, TypeList) combination. |
---|
| 91 | using Instantiation = std::pair<TypeList, ast::ptr<ast::AggregateDecl>>; |
---|
| 92 | /// List of TypeLists paired with the appropriate values. |
---|
| 93 | using ValueList = std::vector<Instantiation>; |
---|
| 94 | /// Underlying map type; maps keys to a linear list of corresponding |
---|
| 95 | /// TypeLists and values. |
---|
| 96 | using InnerMap = ScopedMap<ast::ptr<ast::AggregateDecl>, ValueList>; |
---|
| 97 | |
---|
| 98 | InnerMap data; |
---|
| 99 | public: |
---|
| 100 | void beginScope() { data.beginScope(); } |
---|
| 101 | void endScope() { data.endScope(); } |
---|
| 102 | |
---|
| 103 | /// Gets the value for the (declaration, type list) pair, |
---|
| 104 | /// returns null if no such value exists. |
---|
| 105 | ast::AggregateDecl const * lookup( |
---|
[bc899d6] | 106 | ast::AggregateDecl const * key, |
---|
| 107 | type_vector const & params ) const { |
---|
[1c0657a] | 108 | // This type repackaging is used for the helpers. |
---|
| 109 | ast::ptr<ast::AggregateDecl> ptr = key; |
---|
| 110 | TypeList typeList( params ); |
---|
| 111 | |
---|
| 112 | // Scan scopes for matches to the key. |
---|
| 113 | for ( auto insts = data.find( key ) ; |
---|
| 114 | insts != data.end() ; insts = data.findNext( insts, ptr )) { |
---|
| 115 | for ( auto inst = insts->second.rbegin() ; |
---|
| 116 | inst != insts->second.rend() ; ++inst ) { |
---|
| 117 | if ( inst->first == typeList ) { |
---|
| 118 | return inst->second; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | return nullptr; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /// Adds a value for a (key, type list) pair to the current scope. |
---|
| 126 | void insert( ast::AggregateDecl const * key, type_vector const & params, |
---|
| 127 | ast::AggregateDecl const * value ) { |
---|
| 128 | auto it = data.findAt( data.currentScope(), key ); |
---|
| 129 | if ( it == data.end() ) { |
---|
| 130 | data.insert( key, ValueList( 1, |
---|
| 131 | Instantiation( TypeList( params ), value ) ) ); |
---|
| 132 | } else { |
---|
| 133 | it->second.emplace_back( |
---|
| 134 | Instantiation( TypeList( params ), value ) ); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | }; |
---|
| 138 | |
---|
| 139 | /// Possible options for a given specialization of a generic type. |
---|
| 140 | enum class GenericType { |
---|
| 141 | /// Concrete instatiation based solely on {d,f}type-to-void conversions. |
---|
| 142 | dtypeStatic, |
---|
| 143 | /// Concrete instatiation requiring at least one parameters type. |
---|
| 144 | concrete, |
---|
| 145 | /// No concrete instantiation. |
---|
| 146 | dynamic |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | GenericType & operator|=( GenericType & gt, const GenericType & ht ) { |
---|
| 150 | if ( gt < ht ) gt = ht; |
---|
| 151 | return gt; |
---|
| 152 | } |
---|
| 153 | |
---|
[bc899d6] | 154 | bool isDtypeStatic( ast::vector<ast::TypeDecl> const & baseParams ) { |
---|
[1c0657a] | 155 | return std::all_of( baseParams.begin(), baseParams.end(), |
---|
| 156 | []( ast::TypeDecl const * td ){ return !td->isComplete(); } |
---|
| 157 | ); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /// Makes substitutions of params into baseParams; returns dtypeStatic if |
---|
| 161 | /// there is a concrete instantiation based only on {d,f}type-to-void |
---|
| 162 | /// conversions, concrete if there is a concrete instantiation requiring at |
---|
| 163 | /// least one parameter type, and dynamic if there is no concrete instantiation. |
---|
| 164 | GenericType makeSubstitutions( |
---|
[bc899d6] | 165 | ast::vector<ast::TypeExpr> & out, |
---|
| 166 | ast::vector<ast::TypeDecl> const & baseParams, |
---|
| 167 | ast::vector<ast::Expr> const & params ) { |
---|
[1c0657a] | 168 | GenericType gt = GenericType::dtypeStatic; |
---|
| 169 | |
---|
| 170 | // Substitute concrete types for given parameters, |
---|
| 171 | // using incomplete types for placeholders. |
---|
| 172 | auto baseParam = baseParams.begin(); |
---|
| 173 | auto param = params.begin(); |
---|
| 174 | for ( ; baseParam != baseParams.end() && param != params.end() ; |
---|
| 175 | ++baseParam, ++param ) { |
---|
| 176 | ast::TypeExpr const * paramExpr = param->as<ast::TypeExpr>(); |
---|
| 177 | assertf( paramExpr, "Aggregate parameters should be type expressions." ); |
---|
| 178 | |
---|
| 179 | if ( (*baseParam)->isComplete() ) { |
---|
| 180 | // Substitute parameter for complete (otype or sized dtype) type. |
---|
| 181 | if ( isPolyType( paramExpr->type ) ) { |
---|
| 182 | // Substitute polymorphic parameter type in to generic type. |
---|
| 183 | out.push_back( ast::deepCopy( paramExpr ) ); |
---|
| 184 | gt = GenericType::dynamic; |
---|
| 185 | } else { |
---|
| 186 | // Normalize possibly dtype-static parameter type. |
---|
| 187 | out.emplace_back( new ast::TypeExpr( paramExpr->location, |
---|
| 188 | scrubAllTypeVars( ast::deepCopy( paramExpr->type ) ) ) ); |
---|
| 189 | gt |= GenericType::concrete; |
---|
| 190 | } |
---|
| 191 | } else switch ( (*baseParam)->kind ) { |
---|
| 192 | case ast::TypeDecl::Dtype: |
---|
| 193 | // Here, pretend that any incomplete dtype is `void`. |
---|
| 194 | out.emplace_back( new ast::TypeExpr( paramExpr->location, |
---|
| 195 | new ast::VoidType() ) ); |
---|
| 196 | break; |
---|
| 197 | case ast::TypeDecl::Ftype: |
---|
| 198 | // Here, pretend that any ftype is `void (*)(void)`. |
---|
| 199 | out.emplace_back( new ast::TypeExpr( paramExpr->location, |
---|
| 200 | new ast::FunctionType() ) ); |
---|
| 201 | break; |
---|
| 202 | case ast::TypeDecl::Ttype: |
---|
| 203 | assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." ); |
---|
| 204 | break; |
---|
| 205 | default: |
---|
| 206 | assertf( false, "Unhandled type parameter kind" ); |
---|
| 207 | break; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | assertf( baseParam == baseParams.end(), "Base Parameters not exausted." ); |
---|
| 212 | assertf( param == params.end(), "Type parameters not exausted." ); |
---|
| 213 | return gt; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /// Substitutes types of members according to baseParams => typeSubs, |
---|
| 217 | /// returning the result in a new vector. |
---|
[bc899d6] | 218 | ast::vector<ast::Decl> substituteMembers( |
---|
| 219 | ast::vector<ast::Decl> const & members, |
---|
| 220 | ast::vector<ast::TypeDecl> const & baseParams, |
---|
| 221 | ast::vector<ast::TypeExpr> const & typeSubs ) { |
---|
| 222 | ast::vector<ast::Decl> out; |
---|
[1c0657a] | 223 | ast::TypeSubstitution subs( baseParams, typeSubs ); |
---|
| 224 | for ( ast::ptr<ast::Decl> const & member : members ) { |
---|
| 225 | // Create a manual copy to avoid in-place mutation. |
---|
| 226 | // If being a PureVisitor is decided to be part of apply's interface, |
---|
| 227 | // then we can actually skip this step as it will never mutate in- |
---|
| 228 | // place. (Then we don't need the extra guard to free temp value.) |
---|
| 229 | ast::ptr<ast::Decl> copy = ast::deepCopy( member.get() ); |
---|
| 230 | auto result = subs.apply( copy.get() ); |
---|
| 231 | out.emplace_back( result.node ); |
---|
| 232 | } |
---|
| 233 | return out; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /// Substitutes types of members according to baseParams => typeSubs, |
---|
| 237 | /// modifying them in-place. |
---|
| 238 | void substituteMembersHere( |
---|
[bc899d6] | 239 | ast::vector<ast::Decl> & members, |
---|
| 240 | ast::vector<ast::TypeDecl> const & baseParams, |
---|
| 241 | ast::vector<ast::TypeExpr> const & typeSubs ) { |
---|
[1c0657a] | 242 | ast::TypeSubstitution subs( baseParams, typeSubs ); |
---|
| 243 | for ( ast::ptr<ast::Decl> & member : members ) { |
---|
| 244 | // The member must be mutated in place to avoid breaking |
---|
| 245 | assert( member->unique() ); |
---|
| 246 | |
---|
| 247 | auto field = member.strict_as<ast::ObjectDecl>(); |
---|
| 248 | auto result = subs.apply( field->type.get() ); |
---|
| 249 | auto copy = ast::mutate_field( |
---|
| 250 | field, &ast::ObjectDecl::type, result.node ); |
---|
| 251 | |
---|
| 252 | // I'm not kidding, it is very important. |
---|
| 253 | assert( copy == member.get() ); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /// Strips the instances' type parameters. |
---|
| 258 | void stripInstParams( ast::BaseInstType * inst ) { |
---|
| 259 | inst->params.clear(); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | bool isGenericType( ast::Type const * type ) { |
---|
| 263 | if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) { |
---|
| 264 | return !inst->params.empty(); |
---|
| 265 | } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) { |
---|
| 266 | return !inst->params.empty(); |
---|
| 267 | } else { |
---|
| 268 | return false; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | // The Passes: |
---|
| 273 | |
---|
| 274 | struct FixDtypeStatic final : |
---|
| 275 | public ast::WithGuards, |
---|
| 276 | public ast::WithVisitorRef<FixDtypeStatic>, |
---|
| 277 | public ast::WithShortCircuiting, |
---|
| 278 | public ast::WithStmtsToAdd<> { |
---|
| 279 | ast::ApplicationExpr const * previsit( ast::ApplicationExpr const * expr ); |
---|
| 280 | void previsit( ast::AddressExpr const * expr ); |
---|
| 281 | |
---|
| 282 | ast::Expr const * postvisit( ast::MemberExpr const * expr ); |
---|
| 283 | private: |
---|
| 284 | template<typename Aggr> |
---|
| 285 | ast::Expr const * fixMemberExpr( |
---|
| 286 | Aggr const * inst, ast::MemberExpr const * memberExpr ); |
---|
| 287 | |
---|
| 288 | ast::Expr const * fixMemberExpr( |
---|
[bc899d6] | 289 | ast::vector<ast::TypeDecl> const & baseParams, |
---|
[1c0657a] | 290 | ast::MemberExpr const * memberExpr ); |
---|
| 291 | |
---|
| 292 | bool isLValueArg = false; |
---|
| 293 | }; |
---|
| 294 | |
---|
| 295 | ast::ApplicationExpr const * FixDtypeStatic::previsit( |
---|
| 296 | ast::ApplicationExpr const * expr ) { |
---|
| 297 | GuardValue( isLValueArg ) = false; |
---|
[e01eb4a] | 298 | ast::Decl const * function = ast::getFunction( expr ); |
---|
[1c0657a] | 299 | if ( ast::Linkage::Intrinsic != function->linkage |
---|
| 300 | || !CodeGen::isAssignment( function->name ) ) { |
---|
| 301 | return expr; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // Explicity visit children because only the first element must be a |
---|
| 305 | // C lvalue (normally, it can only send info to both or neither). |
---|
| 306 | visit_children = false; |
---|
| 307 | expr = mutate_field( expr, &ast::ApplicationExpr::env, |
---|
| 308 | maybe_accept( expr->env.get(), *visitor ) ); |
---|
| 309 | expr = mutate_field( expr, &ast::ApplicationExpr::result, |
---|
| 310 | maybe_accept( expr->result.get(), *visitor ) ); |
---|
| 311 | expr = mutate_field( expr, &ast::ApplicationExpr::func, |
---|
| 312 | maybe_accept( expr->func.get(), *visitor ) ); |
---|
| 313 | isLValueArg = true; |
---|
| 314 | for ( unsigned i = 0; i < expr->args.size(); ++i ) { |
---|
| 315 | ast::Expr const * newExpr = expr->args[i]->accept( *visitor ); |
---|
| 316 | // This is declared here for lifetime reasons. |
---|
| 317 | ast::ptr<ast::CastExpr> cast; |
---|
| 318 | if ( newExpr != expr->args[i].get() && |
---|
| 319 | (cast = dynamic_cast<ast::CastExpr const *>( newExpr )) ) { |
---|
| 320 | newExpr = cast->arg.get(); |
---|
| 321 | } |
---|
| 322 | expr = mutate_field_index( expr, &ast::ApplicationExpr::args, |
---|
| 323 | i, newExpr ); |
---|
| 324 | isLValueArg = false; |
---|
| 325 | } |
---|
| 326 | return expr; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | void FixDtypeStatic::previsit( ast::AddressExpr const * ) { |
---|
| 330 | // The argument of an address expression (`&`) must be a C lvalue. |
---|
| 331 | GuardValue( isLValueArg ) = true; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | ast::Expr const * FixDtypeStatic::postvisit( ast::MemberExpr const * expr ) { |
---|
| 335 | ast::ptr<ast::Type> const & type = expr->aggregate->result; |
---|
| 336 | if ( !isGenericType( type ) ) { |
---|
| 337 | return expr; |
---|
| 338 | } else if ( auto inst = type.as<ast::StructInstType>() ) { |
---|
| 339 | return fixMemberExpr( inst, expr ); |
---|
| 340 | } else if ( auto inst = type.as<ast::UnionInstType>() ) { |
---|
| 341 | return fixMemberExpr( inst, expr ); |
---|
| 342 | } |
---|
| 343 | return expr; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | template<typename Aggr> |
---|
| 347 | ast::Expr const * FixDtypeStatic::fixMemberExpr( |
---|
| 348 | Aggr const * inst, ast::MemberExpr const * memberExpr ) { |
---|
| 349 | return fixMemberExpr( inst->base->params, memberExpr ); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | ast::Expr const * FixDtypeStatic::fixMemberExpr( |
---|
[bc899d6] | 353 | ast::vector<ast::TypeDecl> const & baseParams, |
---|
[1c0657a] | 354 | ast::MemberExpr const * memberExpr ) { |
---|
| 355 | // Need to cast dtype-static member expressions to their actual type |
---|
| 356 | // before the actual type type is erased. |
---|
| 357 | // NOTE: The casts here have the third argument (isGenerated) set to |
---|
| 358 | // ExplicitCast so that they casts persist until Box, where they are needed. |
---|
| 359 | |
---|
| 360 | if ( !isDtypeStatic( baseParams ) || |
---|
| 361 | ResolvExpr::typesCompatible( |
---|
| 362 | memberExpr->result, |
---|
| 363 | memberExpr->member->get_type(), ast::SymbolTable() ) ) { |
---|
| 364 | return memberExpr; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | // Type of member and type of expression differ. |
---|
| 368 | ast::Type const * concType = ast::deepCopy( memberExpr->result ); |
---|
| 369 | CodeLocation const & location = memberExpr->location; |
---|
| 370 | if ( isLValueArg ) { |
---|
| 371 | // The result must be a C lvalue expression. So make a new reference |
---|
| 372 | // variable with the correct actual type to replace the |
---|
| 373 | // member expression. |
---|
| 374 | // forall(T &) |
---|
| 375 | // struct Ptr { |
---|
| 376 | // T * x; |
---|
| 377 | // }; |
---|
| 378 | // Ptr(int) p; |
---|
| 379 | // int i; |
---|
| 380 | // p.x = &i; |
---|
| 381 | // becomes |
---|
| 382 | // int *& _dtype_static_member_0 = (int **)&p.x; |
---|
| 383 | // _dtype_static_member_0 = &i; |
---|
| 384 | static UniqueName tmpNamer( "_dtype_static_member_" ); |
---|
| 385 | ast::Expr * init = new ast::CastExpr( location, |
---|
| 386 | new ast::AddressExpr( location, memberExpr ), |
---|
| 387 | new ast::PointerType( ast::deepCopy( concType ) ), |
---|
| 388 | ast::ExplicitCast |
---|
| 389 | ); |
---|
| 390 | ast::ObjectDecl * tmp = new ast::ObjectDecl( location, |
---|
| 391 | tmpNamer.newName(), |
---|
| 392 | new ast::ReferenceType( concType ), |
---|
| 393 | new ast::SingleInit( location, init ), |
---|
| 394 | ast::Storage::Classes(), |
---|
| 395 | ast::Linkage::C |
---|
| 396 | ); |
---|
| 397 | stmtsToAddBefore.push_back( new ast::DeclStmt( location, tmp ) ); |
---|
| 398 | return new ast::VariableExpr( location, tmp ); |
---|
| 399 | } else { |
---|
| 400 | // Here, it can simply add a cast to actual types. |
---|
| 401 | return new ast::CastExpr( location, |
---|
| 402 | memberExpr, |
---|
| 403 | concType, |
---|
| 404 | ast::ExplicitCast |
---|
| 405 | ); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | struct GenericInstantiator final : |
---|
[01865fb] | 410 | public ast::WithCodeLocation, |
---|
[1c0657a] | 411 | public ast::WithConstTypeSubstitution, |
---|
[01865fb] | 412 | public ast::WithDeclsToAdd<>, |
---|
| 413 | public ast::WithGuards, |
---|
| 414 | public ast::WithVisitorRef<GenericInstantiator> |
---|
| 415 | { |
---|
[1c0657a] | 416 | /// Map of (generic type, parameter list) pairs |
---|
| 417 | /// to concrete type instantiations. |
---|
| 418 | InstantiationMap instantiations; |
---|
| 419 | /// Set of types which are dtype-only generic |
---|
| 420 | /// (and therefore have static layout). |
---|
| 421 | std::set<ast::AggregateDecl const *> dtypeStatics; |
---|
| 422 | /// Namer for concrete types. |
---|
| 423 | UniqueName typeNamer; |
---|
| 424 | /// Should not make use of type environment to replace types of function |
---|
| 425 | /// parameter and return values. |
---|
| 426 | bool inFunctionType = false; |
---|
| 427 | /// Index of current member, used to recreate MemberExprs with the |
---|
| 428 | /// member from an instantiation. |
---|
| 429 | int memberIndex = -1; |
---|
| 430 | |
---|
| 431 | GenericInstantiator() : |
---|
| 432 | instantiations(), dtypeStatics(), typeNamer("_conc_") {} |
---|
| 433 | |
---|
| 434 | ast::Type const * postvisit( ast::StructInstType const * inst ); |
---|
| 435 | ast::Type const * postvisit( ast::UnionInstType const * inst ); |
---|
| 436 | |
---|
| 437 | void previsit( ast::MemberExpr const * expr ); |
---|
| 438 | ast::Expr const * postvisit( ast::MemberExpr const * expr ); |
---|
| 439 | ast::Expr const * postvisit( ast::Expr const * expr ); |
---|
| 440 | void previsit( ast::ParseNode const * node ); |
---|
| 441 | |
---|
| 442 | void previsit( ast::FunctionType const * ) { |
---|
| 443 | GuardValue( inFunctionType ) = true; |
---|
| 444 | } |
---|
| 445 | void beginScope() { |
---|
| 446 | instantiations.beginScope(); |
---|
| 447 | } |
---|
| 448 | void endScope() { |
---|
| 449 | instantiations.endScope(); |
---|
| 450 | } |
---|
| 451 | private: |
---|
| 452 | /// Wrap instantiation lookup for structures. |
---|
| 453 | ast::StructDecl const * lookup( |
---|
| 454 | ast::StructInstType const * inst, type_vector const & typeSubs ) const; |
---|
| 455 | /// Wrap instantiation lookup for unions. |
---|
| 456 | ast::UnionDecl const * lookup( |
---|
| 457 | ast::UnionInstType const * inst, type_vector const & typeSubs ) const; |
---|
| 458 | /// Wrap instantiation insertion for structures. |
---|
| 459 | void insert( ast::StructInstType const * inst, |
---|
| 460 | type_vector const & typeSubs, ast::StructDecl const * decl ); |
---|
| 461 | /// Wrap instantiation insertion for unions. |
---|
| 462 | void insert( ast::UnionInstType const * inst, |
---|
| 463 | type_vector const & typeSubs, ast::UnionDecl const * decl ); |
---|
| 464 | |
---|
[bc899d6] | 465 | void replaceParametersWithConcrete( ast::vector<ast::Expr> & params ); |
---|
[1c0657a] | 466 | ast::Type const * replaceWithConcrete( ast::Type const * type, bool doClone ); |
---|
| 467 | |
---|
| 468 | template<typename AggrDecl> |
---|
| 469 | ast::Type const * fixInstType( ast::SueInstType<AggrDecl> const * inst ); |
---|
| 470 | |
---|
| 471 | /// Strips a dtype-static aggregate decl of its type parameters, |
---|
| 472 | /// marks it as stripped. |
---|
| 473 | void stripDtypeParams( ast::AggregateDecl * base, |
---|
[bc899d6] | 474 | ast::vector<ast::TypeDecl> & baseParams, |
---|
| 475 | ast::vector<ast::TypeExpr> const & typeSubs ); |
---|
[1c0657a] | 476 | }; |
---|
| 477 | |
---|
| 478 | // I think this and the UnionInstType can be made into a template function. |
---|
| 479 | ast::Type const * GenericInstantiator::postvisit( |
---|
| 480 | ast::StructInstType const * inst ) { |
---|
| 481 | return fixInstType( inst ); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | ast::Type const * GenericInstantiator::postvisit( |
---|
| 485 | ast::UnionInstType const * inst ) { |
---|
| 486 | return fixInstType( inst ); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | template<typename AggrDecl> |
---|
| 490 | ast::Type const * GenericInstantiator::fixInstType( |
---|
| 491 | ast::SueInstType<AggrDecl> const * inst ) { |
---|
| 492 | assert( location ); |
---|
| 493 | |
---|
| 494 | // There is nothing to mutate if the params are empty. |
---|
| 495 | if ( inst->params.empty() ) return inst; |
---|
| 496 | |
---|
| 497 | // Need to replace type variables to ensure that generic types are |
---|
| 498 | // instantiated for the return values of polymorphic functions (in |
---|
| 499 | // particular, for thunks, because they are not [currently] copy |
---|
| 500 | // constructed). |
---|
| 501 | // (This used to be run only on structures, but I believe both need it.) |
---|
| 502 | inst = strict_dynamic_cast<ast::SueInstType<AggrDecl> const *>( |
---|
| 503 | replaceWithConcrete( inst, false ) ); |
---|
| 504 | |
---|
| 505 | // Check for an already-instantiatiated dtype-static type. |
---|
| 506 | if ( dtypeStatics.find( inst->base ) != dtypeStatics.end() ) { |
---|
| 507 | auto mutInst = ast::mutate( inst ); |
---|
| 508 | stripInstParams( mutInst ); |
---|
| 509 | return mutInst; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | // Check if the type can be concretely instantiated; |
---|
| 513 | // and put substitutions in typeSubs. |
---|
| 514 | assertf( inst->base, "Base data-type has parameters." ); |
---|
[bc899d6] | 515 | ast::vector<ast::TypeExpr> typeSubs; |
---|
[1c0657a] | 516 | GenericType gt = makeSubstitutions( typeSubs, inst->base->params, inst->params ); |
---|
| 517 | switch ( gt ) { |
---|
| 518 | case GenericType::dtypeStatic: |
---|
| 519 | { |
---|
| 520 | auto mutInst = ast::mutate( inst ); |
---|
| 521 | assert( mutInst->base->unique() ); |
---|
| 522 | auto mutBase = mutInst->base.get_and_mutate(); |
---|
| 523 | stripDtypeParams( mutBase, mutBase->params, typeSubs ); |
---|
| 524 | stripInstParams( mutInst ); |
---|
| 525 | return mutInst; |
---|
| 526 | } |
---|
| 527 | case GenericType::concrete: |
---|
| 528 | { |
---|
| 529 | // Make concrete instantiation of generic type. |
---|
| 530 | AggrDecl const * concDecl = lookup( inst, typeSubs ); |
---|
| 531 | if ( !concDecl ) { |
---|
| 532 | // Set concDecl to new type, insert type declaration |
---|
| 533 | // into statements to add. |
---|
| 534 | AggrDecl * newDecl = new AggrDecl( *location, |
---|
| 535 | typeNamer.newName( inst->name ) |
---|
| 536 | ); |
---|
| 537 | newDecl->body = inst->base->body; |
---|
| 538 | newDecl->members = substituteMembers( |
---|
| 539 | inst->base->members, |
---|
| 540 | inst->base->params, |
---|
| 541 | typeSubs |
---|
| 542 | ); |
---|
| 543 | |
---|
| 544 | // Forward declare before recursion. (TODO: Only when needed, #199.) |
---|
| 545 | insert( inst, typeSubs, newDecl ); |
---|
[8f1e035] | 546 | if ( AggrDecl const * forwardDecl = ast::asForward( newDecl ) ) { |
---|
[1c0657a] | 547 | declsToAddBefore.push_back( forwardDecl ); |
---|
| 548 | } |
---|
| 549 | // Recursively instantiate members: |
---|
| 550 | concDecl = strict_dynamic_cast<AggrDecl const *>( |
---|
| 551 | newDecl->accept( *visitor ) ); |
---|
| 552 | // Must occur before declaration is added so |
---|
| 553 | // that member instantiation appear first. |
---|
| 554 | declsToAddBefore.push_back( concDecl ); |
---|
| 555 | } |
---|
| 556 | return new ast::SueInstType<AggrDecl>( concDecl, inst->qualifiers ); |
---|
| 557 | } |
---|
| 558 | case GenericType::dynamic: |
---|
| 559 | // Do nothing. |
---|
| 560 | default: |
---|
| 561 | // Should never happen. |
---|
| 562 | return inst; |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | void GenericInstantiator::previsit( ast::MemberExpr const * expr ) { |
---|
| 567 | GuardValue( location ) = &expr->location; |
---|
| 568 | GuardValue( memberIndex ) = -1; |
---|
| 569 | // Only run on expressions where the field being accessed is generic. |
---|
| 570 | if ( isGenericType( expr->aggregate->result ) ) { |
---|
| 571 | // Find the location of the member: |
---|
| 572 | ast::AggregateDecl const * aggr = |
---|
| 573 | expr->aggregate->result.strict_as<ast::BaseInstType>()->aggr(); |
---|
[bc899d6] | 574 | ast::vector<ast::Decl> const & members = aggr->members; |
---|
[1c0657a] | 575 | auto it = std::find( members.begin(), members.end(), expr->member ); |
---|
| 576 | memberIndex = std::distance( members.begin(), it ); |
---|
| 577 | assertf( memberIndex < (int)members.size(), "Could not find member %s in generic type %s.", toString( expr->member ).c_str(), toString( expr->aggregate ).c_str() ); |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | ast::Expr const * GenericInstantiator::postvisit( |
---|
| 582 | ast::MemberExpr const * expr ) { |
---|
| 583 | if ( memberIndex == -1 ) { |
---|
| 584 | return expr; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | // Using the location from the generic type, find the member |
---|
| 588 | // in the instantiation and rebuild the member expression. |
---|
| 589 | ast::AggregateDecl const * aggr = |
---|
| 590 | expr->aggregate->result.strict_as<ast::BaseInstType>()->aggr(); |
---|
| 591 | assertf( memberIndex < (int)aggr->members.size(), "Instantiation somehow has fewer members than the generic type." ); |
---|
| 592 | ast::Decl const * member = *std::next( aggr->members.begin(), memberIndex ); |
---|
| 593 | assertf( member->name == expr->member->name, "Instantiation has different member order than the generic type. %s / %s", toString( member ).c_str(), toString( expr->member ).c_str() ); |
---|
| 594 | auto field = strict_dynamic_cast< ast::DeclWithType const * >( member ); |
---|
| 595 | ast::MemberExpr * ret = new ast::MemberExpr( expr->location, |
---|
| 596 | field, |
---|
| 597 | ast::deepCopy( expr->aggregate ) |
---|
| 598 | ); |
---|
| 599 | // For pointer decay: |
---|
| 600 | ret->result = ResolvExpr::adjustExprType( |
---|
| 601 | ret->result, |
---|
| 602 | ast::TypeEnvironment(), |
---|
| 603 | ast::SymbolTable() |
---|
| 604 | ); |
---|
| 605 | ret->env = expr->env; |
---|
| 606 | return ret; |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | ast::Expr const * GenericInstantiator::postvisit( ast::Expr const * expr ) { |
---|
| 610 | // We are not modifying env on MemberExpr, but that seems to work. |
---|
| 611 | if ( expr->env ) { |
---|
| 612 | auto newEnv = expr->env->accept( *visitor ); |
---|
| 613 | expr = ast::mutate_field( expr, &ast::Expr::env, newEnv ); |
---|
| 614 | } |
---|
| 615 | return expr; |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | void GenericInstantiator::previsit( ast::ParseNode const * node ) { |
---|
| 619 | GuardValue( location ) = &node->location; |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | ast::StructDecl const * GenericInstantiator::lookup( |
---|
| 623 | ast::StructInstType const * inst, |
---|
| 624 | type_vector const & typeSubs ) const { |
---|
| 625 | auto ret = instantiations.lookup( inst->base, typeSubs ); |
---|
| 626 | return strict_dynamic_cast<ast::StructDecl const *, nullptr>( ret ); |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | ast::UnionDecl const * GenericInstantiator::lookup( |
---|
| 630 | ast::UnionInstType const * inst, |
---|
| 631 | type_vector const & typeSubs ) const { |
---|
| 632 | auto ret = instantiations.lookup( inst->base, typeSubs ); |
---|
| 633 | return strict_dynamic_cast<ast::UnionDecl const *, nullptr>( ret ); |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | void GenericInstantiator::insert( ast::StructInstType const * inst, |
---|
| 637 | type_vector const & typeSubs, ast::StructDecl const * decl ) { |
---|
| 638 | instantiations.insert( inst->base, typeSubs, decl ); |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | void GenericInstantiator::insert( ast::UnionInstType const * inst, |
---|
| 642 | type_vector const & typeSubs, ast::UnionDecl const * decl ) { |
---|
| 643 | instantiations.insert( inst->base, typeSubs, decl ); |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | void GenericInstantiator::replaceParametersWithConcrete( |
---|
[bc899d6] | 647 | ast::vector<ast::Expr> & params ) { |
---|
[1c0657a] | 648 | for ( ast::ptr<ast::Expr> & param : params ) { |
---|
| 649 | auto paramType = param.as<ast::TypeExpr>(); |
---|
| 650 | assertf( paramType, "Aggregate parameters should be type expressions." ); |
---|
| 651 | auto type = replaceWithConcrete( paramType->type, false ); |
---|
| 652 | param = ast::mutate_field( paramType, &ast::TypeExpr::type, type ); |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | ast::Type const * GenericInstantiator::replaceWithConcrete( |
---|
| 657 | ast::Type const * type, bool doClone ) { |
---|
| 658 | if ( auto inst = dynamic_cast<ast::TypeInstType const *>( type ) ) { |
---|
| 659 | if ( typeSubs && !inFunctionType ) { |
---|
| 660 | ast::Type const * concType = typeSubs->lookup( inst ); |
---|
| 661 | return ast::deepCopy( ( concType ) ? concType : inst ); |
---|
| 662 | } |
---|
| 663 | } else if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) { |
---|
| 664 | auto mut = ( doClone ) ? ast::deepCopy( inst ) : ast::mutate( inst ); |
---|
| 665 | replaceParametersWithConcrete( mut->params ); |
---|
| 666 | return mut; |
---|
| 667 | } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) { |
---|
| 668 | auto mut = ( doClone ) ? ast::deepCopy( inst ) : ast::mutate( inst ); |
---|
| 669 | replaceParametersWithConcrete( mut->params ); |
---|
| 670 | return mut; |
---|
| 671 | } |
---|
| 672 | return type; |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | void GenericInstantiator::stripDtypeParams( |
---|
| 676 | ast::AggregateDecl * base, |
---|
[bc899d6] | 677 | ast::vector<ast::TypeDecl> & baseParams, |
---|
| 678 | ast::vector<ast::TypeExpr> const & typeSubs ) { |
---|
[1c0657a] | 679 | substituteMembersHere( base->members, baseParams, typeSubs ); |
---|
| 680 | |
---|
| 681 | baseParams.clear(); |
---|
| 682 | |
---|
| 683 | dtypeStatics.insert( base ); |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | } // namespace |
---|
| 687 | |
---|
| 688 | void instantiateGeneric( ast::TranslationUnit & translationUnit ) { |
---|
| 689 | ast::Pass<FixDtypeStatic>::run( translationUnit ); |
---|
| 690 | ast::Pass<GenericInstantiator>::run( translationUnit ); |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | } // namespace GenPoly |
---|
| 694 | |
---|
| 695 | // Local Variables: // |
---|
| 696 | // tab-width: 4 // |
---|
| 697 | // mode: c++ // |
---|
| 698 | // compile-command: "make install" // |
---|
| 699 | // End: // |
---|