Changeset 045cda3


Ignore:
Timestamp:
Oct 4, 2023, 5:30:12 PM (8 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
04db9f6
Parents:
15b5abac
Message:

First clean-up pass on box pass. Some of it is stuff that could have been done in the initial commit but there was just so much of it. The rest is mostly simple restructuring that makes the translation less direct.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/BoxNew.cpp

    r15b5abac r045cda3  
    7777};
    7878
    79 // Formally takeOtypeOnly
    8079/// Get all sized type declarations; those that affect a layout function.
    8180ast::vector<ast::TypeDecl> takeSizedParams(
     
    510509                ast::FunctionType const * function,
    511510                TypeVarMap const & typeVars ) {
    512         // TODO
    513         // NOTE: This function previously used isPolyObj, which failed to produce
    514         // the correct thing in some situations. It's not clear to [Rob Schluntz]
    515         // why this wasn't working.
    516 
    517511        // If the return type or a parameter type involved polymorphic types,
    518512        // then the adapter will need to take those polymorphic types as pointers.
     
    587581        ast::FunctionType const * type = decl->type;
    588582        if ( isDynRet( type ) && decl->linkage != ast::Linkage::C ) {
    589                 //retval = type->returns.front();
    590583                retval = decl->returns.front();
    591584
     
    748741ast::Expr const * CallAdapter::postvisit( ast::UntypedExpr const * expr ) {
    749742        if ( isPolyDeref( expr, scopeTypeVars, typeSubs ) ) {
    750                 // TODO Pretty sure this is just a memory management change.
    751                 // Also, I don't understand what this is doing.
    752                 //ast::Expr const * ret = expr->args.front();
    753                 //expr->args.clear();
    754                 //return ret;
    755743                return expr->args.front();
    756744        }
     
    10631051                ast::ptr<ast::Type> newType = ast::deepCopy( param );
    10641052                if ( typeSubs ) typeSubs->apply( newType );
    1065                 // TODO: Is this right? (Why wouldn't it be?)
    1066                 // I think this is to make sure we can write to the temporary.
    1067                 //newType.get_and_mutate()->qt = ast::CV::Qualifiers();
    1068                 //reset_qualifiers( newType );
    10691053                ast::ObjectDecl * newObj = makeTemporary( location, newType );
    10701054                auto assign = ast::UntypedExpr::createCall( location, "?=?", {
     
    12451229                        }
    12461230                        return new ast::ObjectDecl( location, pNamer.newName(), param );
    1247                 } ), // params
     1231                } ),
    12481232                map_range<ast::vector<ast::DeclWithType>>( adapterType->returns,
    12491233                                [&rNamer, &location]( ast::ptr<ast::Type> const & retval ) {
    12501234                        return new ast::ObjectDecl( location, rNamer.newName(), retval );
    1251                 } ), // returns
     1235                } ),
    12521236                nullptr, // stmts
    1253                 ast::Storage::Classes(), // storage
    1254                 ast::Linkage::C // linkage
    1255                 // attrs
    1256                 // fs
    1257                 // isVarArgs
     1237                {}, // storage
     1238                ast::Linkage::C
    12581239        );
    12591240
     
    13141295        // Returns a polymorphic type.
    13151296        } else if ( isDynType( adaptee->returns.front(), typeVars ) ) {
    1316                 if ( "" == (*paramDecl)->name ) {
    1317                         // TODO: Is it easier to make sure it has a name in the first
    1318                         // place? - I believe this is done, however, I could remove the
    1319                         // condition and just rename for clarity.
    1320                         assertf( false, "Wasn't expecting to get here." );
    1321                         auto mutParam = paramDecl->get_and_mutate();
    1322                         mutParam->name = "_ret";
    1323                         mutParam->linkage = ast::Linkage::C;
    1324                 }
    13251297                ast::UntypedExpr * assign = new ast::UntypedExpr( location,
    13261298                        new ast::NameExpr( location, "?=?" ) );
     
    15461518
    15471519// --------------------------------------------------------------------------
    1548 /// Creates the adapter functions. TODO
     1520/// Modifies declarations to accept implicit parameters.
    15491521/// * Move polymorphic returns in function types to pointer-type parameters.
    15501522/// * Adds type size and assertion parameters to parameter lists.
     
    15591531        void previsit( ast::TypeDecl const * decl );
    15601532        void previsit( ast::PointerType const * type );
    1561         void previsit( ast::FunctionType const * type );
    15621533        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl );
    1563         ast::DeclWithType const * postvisit( ast::FunctionDecl const * decl );
     1534        ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl );
    15641535        void previsit( ast::CompoundStmt const * stmt );
    15651536private:
     
    16161587}
    16171588
    1618 // TODO: I think this code is redundent.
    1619 void DeclAdapter::previsit( ast::FunctionType const * type ) {
    1620         GuardScope( scopeTypeVars );
    1621         makeTypeVarMap( type, scopeTypeVars );
     1589// size/align/offset parameters may not be used, so add the unused attribute.
     1590ast::ObjectDecl * makeObj(
     1591                CodeLocation const & location, std::string const & name ) {
     1592        return new ast::ObjectDecl( location, name,
     1593                makeSizeAlignType(),
     1594                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
     1595                { new ast::Attribute( "unused" ) } );
     1596}
     1597
     1598ast::ObjectDecl * makePtr(
     1599                CodeLocation const & location, std::string const & name ) {
     1600        return new ast::ObjectDecl( location, name,
     1601                new ast::PointerType( makeSizeAlignType() ),
     1602                nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr );
    16221603}
    16231604
     
    16391620
    16401621        // Add size/align and assertions for type parameters to parameter list.
    1641         std::vector<ast::ptr<ast::DeclWithType>>::iterator last = mutDecl->params.begin();
    1642         std::vector<ast::ptr<ast::DeclWithType>> inferredParams;
    1643         // size/align/offset parameters may not be used in body, pass along with unused attribute.
    1644         // TODO: These should be created with proper location and name.
    1645         // TODO: makeSizeAlign[Out]Type are the same as these types, but they may
    1646         // be logically different.
    1647         ast::ObjectDecl newObj( mutDecl->location, "",
    1648                 new ast::BasicType( ast::BasicType::LongUnsignedInt ),
    1649                 nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr,
    1650                 { new ast::Attribute( "unused" ) } );
    1651         ast::ObjectDecl newPtr( mutDecl->location, "",
    1652                 new ast::PointerType( new ast::BasicType( ast::BasicType::LongUnsignedInt ) ),
    1653                 nullptr, ast::Storage::Classes(), ast::Linkage::C, nullptr );
     1622        ast::vector<ast::DeclWithType>::iterator last = mutDecl->params.begin();
     1623        ast::vector<ast::DeclWithType> inferredParams;
    16541624        for ( ast::ptr<ast::TypeDecl> & typeParam : mutDecl->type_params ) {
    16551625                auto mutParam = mutate( typeParam.get() );
    16561626                // Add all size and alignment parameters to parameter list.
    16571627                if ( mutParam->isComplete() ) {
    1658                         //ast::TypeInstType paramType( typeParam->name, typeParam );
    16591628                        ast::TypeInstType paramType( mutParam );
    16601629                        std::string paramName = Mangle::mangleType( &paramType );
    16611630
    1662                         ast::ObjectDecl * sizeParam = ast::deepCopy( &newObj );
    1663                         sizeParam->location = typeParam->location;
    1664                         sizeParam->name = sizeofName( paramName );
     1631                        auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) );
    16651632                        last = mutDecl->params.insert( last, sizeParam );
    16661633                        ++last;
    16671634
    1668                         ast::ObjectDecl * alignParam = ast::deepCopy( &newObj );
    1669                         alignParam->location = typeParam->location;
    1670                         alignParam->name = alignofName( paramName );
     1635                        auto alignParam = makeObj( typeParam->location, alignofName( paramName ) );
    16711636                        last = mutDecl->params.insert( last, alignParam );
    16721637                        ++last;
     
    16961661        // Add size/align for generic parameter types to parameter list.
    16971662        std::set<std::string> seenTypes;
    1698         std::vector<ast::ptr<ast::DeclWithType>> otypeParams;
     1663        ast::vector<ast::DeclWithType> otypeParams;
    16991664        for ( ast::ptr<ast::DeclWithType> & funcParam : mutDecl->params ) {
    17001665                ast::Type const * polyType = isPolyType( funcParam->get_type(), scopeTypeVars );
     
    17051670                if ( seenTypes.count( typeName ) ) continue;
    17061671
    1707                 ast::ObjectDecl * sizeParam = ast::deepCopy( &newObj );
    1708                 sizeParam->location = funcParam->location;
    1709                 sizeParam->name = sizeofName( typeName );
     1672                auto sizeParam = makeObj( funcParam->location, sizeofName( typeName ) );
    17101673                otypeParams.emplace_back( sizeParam );
    17111674
    1712                 ast::ObjectDecl * alignParam = ast::deepCopy( &newObj );
    1713                 alignParam->location = funcParam->location;
    1714                 alignParam->name = alignofName( typeName );
     1675                auto alignParam = makeObj( funcParam->location, alignofName( typeName ) );
    17151676                otypeParams.emplace_back( alignParam );
    17161677
     
    17201681                        // offset array.
    17211682                        if ( !polyStruct->base->members.empty() ) {
    1722                                 ast::ObjectDecl * offsetParam = ast::deepCopy( &newPtr );
    1723                                 offsetParam->location = funcParam->location;
    1724                                 offsetParam->name = offsetofName( typeName );
     1683                                auto offsetParam = makePtr( funcParam->location, offsetofName( typeName ) );
    17251684                                otypeParams.emplace_back( offsetParam );
    17261685                        }
     
    17391698}
    17401699
    1741 ast::DeclWithType const * DeclAdapter::postvisit(
     1700ast::FunctionDecl const * DeclAdapter::postvisit(
    17421701                ast::FunctionDecl const * decl ) {
    17431702        ast::FunctionDecl * mutDecl = mutate( decl );
     
    17881747// It actually does mutate in-place, but does the return for consistency.
    17891748ast::FunctionDecl * DeclAdapter::addAdapters( ast::FunctionDecl * mutDecl ) {
    1790         std::vector<ast::ptr<ast::FunctionType>> functions;
     1749        ast::vector<ast::FunctionType> functions;
    17911750        for ( ast::ptr<ast::DeclWithType> & arg : mutDecl->params ) {
    17921751                ast::Type const * type = arg->get_type();
     
    19261885        /// Namer for VLA (variable length array) buffers.
    19271886        UniqueName bufNamer;
    1928         /// AddressExpr argument is MemberExpr? (TODO: What?)
    1929         ast::Expr const * addrMember = nullptr;
     1887        /// If the argument of an AddressExpr is MemberExpr, it is stored here.
     1888        ast::MemberExpr const * addrMember = nullptr;
    19301889        /// Used to avoid recursing too deep in type declarations.
    19311890        bool expect_func_type = false;
     
    19591918        beginTypeScope( decl->type );
    19601919
    1961         // - Tried inserting this code.
    1962         // Make sure that any type information passed into the function is
    1963         // accounted for.
    1964         // TODO: For some reason going through decl->params/->get_type() does
    1965         // not work. Possibly something is not getting updated.
     1920        // TODO: Going though dec->params does not work for some reason.
    19661921        for ( ast::ptr<ast::Type> const & funcParam : decl->type->params ) {
    19671922                // Condition here duplicates that in `DeclAdapter::previsit( FunctionDecl const * )`
     
    20191974ast::StructDecl const * PolyGenericCalculator::previsit(
    20201975                ast::StructDecl const * decl ) {
    2021         //return strict_dynamic_cast<ast::StructDecl *>( mutateMembers( decl ) );
    20221976        auto mutDecl = mutate( decl );
    20231977        mutateMembers( mutDecl );
     
    20271981ast::UnionDecl const * PolyGenericCalculator::previsit(
    20281982                ast::UnionDecl const * decl ) {
    2029         //return strict_dynamic_cast<ast::UnionDecl *>( mutateMembers( decl ) );
    20301983        auto mutDecl = mutate( decl );
    20311984        mutateMembers( mutDecl );
     
    20882041        auto mutDecl = mutate( decl );
    20892042
    2090         //mutDecl->attributes.remove_if( matchAndMove );
    2091         // TODO: This common helper might work, but does not officially support
    2092         // side effects.
     2043        // Forally, side effects are not safe in this function. But it works.
    20932044        erase_if( mutDecl->attributes, matchAndMove );
    20942045
     
    21982149        // the substitution manually. For some reason this is not currently the
    21992150        // case. This requires more investigation.
    2200         ast::Type const * memberType = deepCopy( expr->member->get_type() );
     2151        ast::ptr<ast::Type> memberType = deepCopy( expr->member->get_type() );
    22012152        ast::TypeSubstitution sub = genericSubstitution( objectType );
    2202         auto result = sub.apply( memberType );
    2203         memberType = result.node.get(); // .release();
     2153        sub.apply( memberType );
     2154
    22042155        // Not all members of a polymorphic type are themselves of a polymorphic
    22052156        // type; in this cas the member expression should be wrapped and
     
    22172168
    22182169void PolyGenericCalculator::previsit( ast::AddressExpr const * expr ) {
    2219         // Is the argument a MemberExpr before mutating?
    22202170        GuardValue( addrMember ) = expr->arg.as<ast::MemberExpr>();
    22212171}
     
    22332183        // MemberExpr was converted to pointer + offset; and it is not valid C to
    22342184        // take the address of an addition, so stript the address-of.
    2235         // TODO: should expr->arg->result be changed to expr->result?
    2236         ast::Expr * ret = mutate( expr->arg.get() );
    2237         ret->env = expr->env;
    2238         return ret;
     2185        // It also preserves the env value.
     2186        return ast::mutate_field( expr->arg.get(), &ast::Expr::env, expr->env );
    22392187}
    22402188
     
    25722520                public BoxPass,
    25732521                public ast::WithGuards {
    2574         template<typename decl_t>
    2575         decl_t const * handleDecl( decl_t const * decl, ast::Type const * type );
     2522        void guardTypeVarMap( ast::Type const * type ) {
     2523                GuardScope( scopeTypeVars );
     2524                makeTypeVarMap( type, scopeTypeVars );
     2525        }
    25762526
    25772527        ast::ObjectDecl const * previsit( ast::ObjectDecl const * decl );
     
    25852535};
    25862536
    2587 template<typename decl_t>
    2588 decl_t const * Eraser::handleDecl(
    2589                 decl_t const * decl, ast::Type const * type ) {
    2590         GuardScope( scopeTypeVars );
    2591         makeTypeVarMap( type, scopeTypeVars );
     2537ast::ObjectDecl const * Eraser::previsit( ast::ObjectDecl const * decl ) {
     2538        guardTypeVarMap( decl->type );
    25922539        return scrubAllTypeVars( decl );
    25932540}
    25942541
    2595 ast::ObjectDecl const * Eraser::previsit( ast::ObjectDecl const * decl ) {
    2596         return handleDecl( decl, decl->type );
    2597 }
    2598 
    25992542ast::FunctionDecl const * Eraser::previsit( ast::FunctionDecl const * decl ) {
    2600         return handleDecl( decl, decl->type );
     2543        guardTypeVarMap( decl->type );
     2544        return scrubAllTypeVars( decl );
    26012545}
    26022546
    26032547ast::TypedefDecl const * Eraser::previsit( ast::TypedefDecl const * decl ) {
    2604         return handleDecl( decl, decl->base );
     2548        guardTypeVarMap( decl->base );
     2549        return scrubAllTypeVars( decl );
    26052550}
    26062551
     
    26082553template<typename node_t>
    26092554node_t const * stripGenericMembers( node_t const * decl ) {
    2610         if ( !decl->params.empty() ) {
    2611                 auto mutDecl = ast::mutate( decl );
    2612                 mutDecl->members.clear();
    2613                 return mutDecl;
    2614         }
    2615         return decl;
     2555        if ( decl->params.empty() ) return decl;
     2556        auto mutDecl = ast::mutate( decl );
     2557        mutDecl->members.clear();
     2558        return mutDecl;
    26162559}
    26172560
     
    26292572
    26302573void Eraser::previsit( ast::PointerType const * type ) {
    2631         GuardScope( scopeTypeVars );
    2632         makeTypeVarMap( type, scopeTypeVars );
     2574        guardTypeVarMap( type );
    26332575}
    26342576
    26352577void Eraser::previsit( ast::FunctionType const * type ) {
    2636         GuardScope( scopeTypeVars );
    2637         makeTypeVarMap( type, scopeTypeVars );
     2578        guardTypeVarMap( type );
    26382579}
    26392580
Note: See TracChangeset for help on using the changeset viewer.