Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/InstantiateGeneric.cpp

    r594671a red96731  
    1010// Created On       : Tue Aug 16 10:51:00 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Mar 12 15:18:00 2025
    13 // Update Count     : 2
     12// Last Modified On : Mon Oct 31 16:48:00 2022
     13// Update Count     : 1
    1414//
    1515
     
    159159}
    160160
    161 /// Get the scrubbed type of a declaration (see scrubTypeVars functions).
    162 ast::TypeExpr const * scrubTypeDecl(
    163                 CodeLocation const & location, ast::TypeDecl const * typeDecl ) {
    164         switch ( typeDecl->kind ) {
    165         // Erase any incomplete dtype to `void` (`T *` -> `void *`).
    166         case ast::TypeDecl::Dtype:
    167                 return new ast::TypeExpr( location, new ast::VoidType() );
    168         // Erase any ftype to `void (*)(void)`.
    169         case ast::TypeDecl::Ftype:
    170                 return new ast::TypeExpr( location, new ast::FunctionType() );
    171         // Remaining cases are not supported.
    172         case ast::TypeDecl::Ttype:
    173                 assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
    174                 break;
    175         default:
    176                 assertf( false, "Unhandled type parameter kind" );
    177                 break;
    178         }
    179 }
    180 
    181161/// Makes substitutions of params into baseParams; returns dtypeStatic if
    182162/// there is a concrete instantiation based only on {d,f}type-to-void
     
    210190                                gt |= GenericType::concrete;
    211191                        }
    212                 } else {
    213                         out.emplace_back( scrubTypeDecl( paramExpr->location, *baseParam ) );
     192                } else switch ( (*baseParam)->kind ) {
     193                case ast::TypeDecl::Dtype:
     194                        // Here, pretend that any incomplete dtype is `void`.
     195                        out.emplace_back( new ast::TypeExpr( paramExpr->location,
     196                                new ast::VoidType() ) );
     197                        break;
     198                case ast::TypeDecl::Ftype:
     199                        // Here, pretend that any ftype is `void (*)(void)`.
     200                        out.emplace_back( new ast::TypeExpr( paramExpr->location,
     201                                new ast::FunctionType() ) );
     202                        break;
     203                case ast::TypeDecl::Ttype:
     204                        assertf( false, "Ttype parameters are not currently allowed as parameters to generic types." );
     205                        break;
     206                default:
     207                        assertf( false, "Unhandled type parameter kind" );
     208                        break;
    214209                }
    215210        }
     
    448443                instantiations(), dtypeStatics(), typeNamer("_conc_") {}
    449444
    450         ast::StructDecl const * previsit( ast::StructDecl const * );
    451         ast::UnionDecl const * previsit( ast::UnionDecl const * );
    452 
    453445        ast::Type const * postvisit( ast::StructInstType const * inst );
    454446        ast::Type const * postvisit( ast::UnionInstType const * inst );
     
    489481
    490482        template<typename AggrDecl>
    491         AggrDecl const * fixAggrDecl( AggrDecl const * decl );
    492 
    493         template<typename AggrDecl>
    494483        ast::Type const * fixInstType( ast::SueInstType<AggrDecl> const * inst );
    495484
     
    500489                ast::vector<ast::TypeExpr> const & typeSubs );
    501490};
    502 
    503 ast::StructDecl const * GenericInstantiator::previsit( ast::StructDecl const * decl ) {
    504         return fixAggrDecl( decl );
    505 }
    506 
    507 ast::UnionDecl const * GenericInstantiator::previsit( ast::UnionDecl const * decl ) {
    508         return fixAggrDecl( decl );
    509 }
    510 
    511 template<typename AggrDecl>
    512 AggrDecl const * GenericInstantiator::fixAggrDecl( AggrDecl const * decl ) {
    513         // This function and stripDtypeParams handle declarations before their
    514         // first use (required to be in the previsit for types with a self use).
    515         if ( decl->params.empty() || !isDtypeStatic( decl->params ) ) {
    516                 return decl;
    517         }
    518 
    519         ast::vector<ast::TypeExpr> typeSubs;
    520         for ( auto const & param : decl->params ) {
    521                 assert( !param->isComplete() );
    522                 typeSubs.emplace_back( scrubTypeDecl( param->location, param ) );
    523         }
    524 
    525         assert( decl->unique() );
    526         auto mutDecl = ast::mutate( decl );
    527         stripDtypeParams( mutDecl, mutDecl->params, typeSubs );
    528 
    529         return mutDecl;
    530 }
    531491
    532492ast::Type const * GenericInstantiator::postvisit(
     
    571531        case GenericType::dtypeStatic:
    572532        {
    573                 // This call to stripDtypeParams is used when a forward declaration
    574                 // has allowed an instance to appear before the full declaration.
    575533                auto mutInst = ast::mutate( inst );
    576534                assert( mutInst->base->unique() );
Note: See TracChangeset for help on using the changeset viewer.