Changeset 4da152a


Ignore:
Timestamp:
Nov 8, 2022, 4:12:00 PM (18 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
6411b7d
Parents:
44547b0
Message:

Cleaning old box pass for easier translation. Adding a bunch of const qualifiers. Hopefully this will remove some ambiguity about where changes happen, but const is dropped quickly in the old ast.

Location:
src/GenPoly
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r44547b0 r4da152a  
    5858namespace GenPoly {
    5959        namespace {
    60                 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
     60                FunctionType *makeAdapterType( FunctionType const *adaptee, const TyVarMap &tyVars );
    6161
    6262                class BoxPass {
     
    124124                        /// `mangleName` as the base name for the adapter. `tyVars` is the map of
    125125                        /// type variables for the function type of the adapted expression.
    126                         FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
     126                        FunctionDecl *makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
    127127                        /// Replaces intrinsic operator functions with their arithmetic desugaring
    128128                        Expression *handleIntrinsics( ApplicationExpr *appExpr );
     
    190190                        ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 );
    191191                        /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns
    192                         bool findGeneric( Type *ty );
     192                        bool findGeneric( Type const *ty );
    193193                        /// adds type parameters to the layout call; will generate the appropriate parameters if needed
    194194                        void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams );
     
    426426
    427427        namespace {
    428                 std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
     428                std::string makePolyMonoSuffix( FunctionType const * function, const TyVarMap &tyVars ) {
    429429                        std::stringstream name;
    430430
     
    435435                        // to take those polymorphic types as pointers. Therefore, there can be two different functions
    436436                        // with the same mangled name, so we need to further mangle the names.
    437                         for ( DeclarationWithType * const ret : function->get_returnVals() ) {
     437                        for ( DeclarationWithType const * const ret : function->returnVals ) {
    438438                                if ( isPolyType( ret->get_type(), tyVars ) ) {
    439439                                        name << "P";
     
    443443                        }
    444444                        name << "_";
    445                         for ( DeclarationWithType * const arg : function->get_parameters() ) {
     445                        for ( DeclarationWithType const * const arg : function->parameters ) {
    446446                                if ( isPolyType( arg->get_type(), tyVars ) ) {
    447447                                        name << "P";
     
    453453                }
    454454
    455                 std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) {
     455                std::string mangleAdapterName( FunctionType const * function, const TyVarMap &tyVars ) {
    456456                        return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars );
    457457                }
     
    489489
    490490                                std::list< DeclarationWithType *> &paramList = functionType->parameters;
    491                                 std::list< FunctionType *> functions;
     491                                std::list< FunctionType const *> functions;
    492492                                for ( TypeDecl * const tyVar : functionType->forall ) {
    493493                                        for ( DeclarationWithType * const assert : tyVar->assertions ) {
     
    499499                                } // for
    500500
    501                                 for ( FunctionType * const funType : functions ) {
     501                                for ( FunctionType const * const funType : functions ) {
    502502                                        std::string mangleName = mangleAdapterName( funType, scopeTyVars );
    503503                                        if ( adapters.find( mangleName ) == adapters.end() ) {
     
    794794                }
    795795
    796                 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
     796                FunctionType *makeAdapterType( FunctionType const *adaptee, const TyVarMap &tyVars ) {
    797797                        // actually make the adapter type
    798798                        FunctionType *adapter = adaptee->clone();
     
    804804                }
    805805
    806                 Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
     806                Expression *makeAdapterArg(
     807                                DeclarationWithType *param,
     808                                DeclarationWithType const *arg,
     809                                DeclarationWithType const *realParam,
     810                                const TyVarMap &tyVars ) {
    807811                        assert( param );
    808812                        assert( arg );
     
    817821                }
    818822
    819                 void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) {
     823                void addAdapterParams(
     824                                ApplicationExpr *adapteeApp,
     825                                std::list< DeclarationWithType *>::const_iterator arg,
     826                                std::list< DeclarationWithType *>::const_iterator param,
     827                                std::list< DeclarationWithType *>::const_iterator paramEnd,
     828                                std::list< DeclarationWithType *>::const_iterator realParam,
     829                                const TyVarMap &tyVars ) {
    820830                        UniqueName paramNamer( "_p" );
    821831                        for ( ; param != paramEnd; ++param, ++arg, ++realParam ) {
     
    828838                }
    829839
    830                 FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
     840                FunctionDecl *Pass1::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
    831841                        FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
    832842                        adapterType = ScrubTyVars::scrub( adapterType, tyVars );
     
    845855                        Statement *bodyStmt;
    846856
    847                         Type::ForallList::iterator tyArg = realType->get_forall().begin();
    848                         Type::ForallList::iterator tyParam = adapterType->get_forall().begin();
    849                         Type::ForallList::iterator realTyParam = adaptee->get_forall().begin();
     857                        Type::ForallList::const_iterator tyArg = realType->forall.begin();
     858                        Type::ForallList::const_iterator tyParam = adapterType->forall.begin();
     859                        Type::ForallList::const_iterator realTyParam = adaptee->forall.begin();
    850860                        for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) {
    851861                                assert( tyArg != realType->get_forall().end() );
    852                                 std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin();
    853                                 std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin();
    854                                 std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin();
     862                                std::list< DeclarationWithType *>::const_iterator assertArg = (*tyArg)->get_assertions().begin();
     863                                std::list< DeclarationWithType *>::const_iterator assertParam = (*tyParam)->get_assertions().begin();
     864                                std::list< DeclarationWithType *>::const_iterator realAssertParam = (*realTyParam)->get_assertions().begin();
    855865                                for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) {
    856866                                        assert( assertArg != (*tyArg)->get_assertions().end() );
     
    859869                        } // for
    860870
    861                         std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin();
    862                         std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin();
    863                         std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin();
     871                        std::list< DeclarationWithType *>::const_iterator arg = realType->parameters.begin();
     872                        std::list< DeclarationWithType *>::const_iterator param = adapterType->parameters.begin();
     873                        std::list< DeclarationWithType *>::const_iterator realParam = adaptee->parameters.begin();
    864874                        param++;                // skip adaptee parameter in the adapter type
    865875                        if ( realType->get_returnVals().empty() ) {
     
    867877                                addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
    868878                                bodyStmt = new ExprStmt( adapteeApp );
    869                         } else if ( isDynType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
     879                        } else if ( isDynType( adaptee->returnVals.front()->get_type(), tyVars ) ) {
    870880                                // return type T
    871881                                if ( (*param)->get_name() == "" ) {
     
    892902                void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
    893903                        // collect a list of function types passed as parameters or implicit parameters (assertions)
    894                         std::list< FunctionType*> functions;
     904                        std::list<FunctionType const *> functions;
    895905                        for ( TypeDecl * const tyVar : functionType->get_forall() ) {
    896906                                for ( DeclarationWithType * const assert : tyVar->get_assertions() ) {
     
    906916                        std::set< std::string > adaptersDone;
    907917
    908                         for ( FunctionType * const funType : functions ) {
     918                        for ( FunctionType const * const funType : functions ) {
    909919                                FunctionType *originalFunction = funType->clone();
    910920                                FunctionType *realFunction = funType->clone();
     
    940950
    941951                Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
    942                         NameExpr *opExpr;
    943                         if ( isIncr ) {
    944                                 opExpr = new NameExpr( "?+=?" );
    945                         } else {
    946                                 opExpr = new NameExpr( "?-=?" );
    947                         } // if
     952                        NameExpr *opExpr = new NameExpr( ( isIncr ) ? "?+=?" : "?-=?" );
    948953                        UntypedExpr *addAssign = new UntypedExpr( opExpr );
    949954                        if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
     
    11461151                }
    11471152
    1148                 bool isPolyDeref( UntypedExpr * expr, TyVarMap const & scopeTyVars, TypeSubstitution const * env ) {
     1153                bool isPolyDeref( UntypedExpr const * expr, TyVarMap const & scopeTyVars, TypeSubstitution const * env ) {
    11491154                        if ( expr->result && isPolyType( expr->result, scopeTyVars, env ) ) {
    1150                                 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->function ) ) {
     1155                                if ( auto name = dynamic_cast<NameExpr const *>( expr->function ) ) {
    11511156                                        if ( name->name == "*?" ) {
    11521157                                                return true;
     
    12291234                void Pass2::addAdapters( FunctionType *functionType ) {
    12301235                        std::list< DeclarationWithType *> &paramList = functionType->parameters;
    1231                         std::list< FunctionType *> functions;
     1236                        std::list< FunctionType const *> functions;
    12321237                        for ( DeclarationWithType * const arg : functionType->parameters ) {
    12331238                                Type *orig = arg->get_type();
     
    12361241                        }
    12371242                        std::set< std::string > adaptersDone;
    1238                         for ( FunctionType * const funType : functions ) {
     1243                        for ( FunctionType const * const funType : functions ) {
    12391244                                std::string mangleName = mangleAdapterName( funType, scopeTyVars );
    12401245                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
     
    14711476
    14721477                /// converts polymorphic type T into a suitable monomorphic representation, currently: __attribute__((aligned(8)) char[size_T]
    1473                 Type * polyToMonoType( Type * declType ) {
     1478                Type * polyToMonoType( Type const * declType ) {
    14741479                        Type * charType = new BasicType( Type::Qualifiers(), BasicType::Kind::Char);
    14751480                        Expression * size = new NameExpr( sizeofName( mangleType(declType) ) );
     
    15681573
    15691574                /// Returns an index expression into the offset array for a type
    1570                 Expression *makeOffsetIndex( Type *objectType, long i ) {
     1575                Expression *makeOffsetIndex( Type const *objectType, long i ) {
    15711576                        ConstantExpr *fieldIndex = new ConstantExpr( Constant::from_ulong( i ) );
    15721577                        UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
     
    16751680
    16761681                /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list
    1677                 bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) {
     1682                bool findGenericParams( std::list< TypeDecl* > const &baseParams, std::list< Expression* > const &typeParams, std::list< Type* > &out ) {
    16781683                        bool hasDynamicLayout = false;
    16791684
     
    16951700                }
    16961701
    1697                 bool PolyGenericCalculator::findGeneric( Type *ty ) {
     1702                bool PolyGenericCalculator::findGeneric( Type const *ty ) {
    16981703                        ty = replaceTypeInst( ty, env );
    16991704
    1700                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) {
     1705                        if ( auto typeInst = dynamic_cast< TypeInstType const * >( ty ) ) {
    17011706                                if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) {
    17021707                                        // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set
     
    17041709                                }
    17051710                                return false;
    1706                         } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) {
     1711                        } else if ( auto structTy = dynamic_cast< StructInstType const * >( ty ) ) {
    17071712                                // check if this type already has a layout generated for it
    17081713                                std::string typeName = mangleType( ty );
     
    17111716                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
    17121717                                std::list< Type* > otypeParams;
    1713                                 if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false;
     1718                                if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->parameters, otypeParams ) ) return false;
    17141719
    17151720                                // insert local variables for layout and generate call to layout function
     
    17411746
    17421747                                return true;
    1743                         } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) {
     1748                        } else if ( auto unionTy = dynamic_cast< UnionInstType const * >( ty ) ) {
    17441749                                // check if this type already has a layout generated for it
    17451750                                std::string typeName = mangleType( ty );
     
    17481753                                // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
    17491754                                std::list< Type* > otypeParams;
    1750                                 if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false;
     1755                                if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->parameters, otypeParams ) ) return false;
    17511756
    17521757                                // insert local variables for layout and generate call to layout function
  • src/GenPoly/FindFunction.cc

    r44547b0 r4da152a  
    2929        class FindFunction : public WithGuards, public WithVisitorRef<FindFunction>, public WithShortCircuiting {
    3030          public:
    31                 FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
     31                FindFunction( std::list< FunctionType const* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
    3232
    3333                void premutate( FunctionType * functionType );
     
    3737                void handleForall( const Type::ForallList &forall );
    3838
    39                 std::list< FunctionType* > &functions;
     39                std::list< FunctionType const * > & functions;
    4040                TyVarMap tyVars;
    4141                bool replaceMode;
     
    4343        };
    4444
    45         void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
     45        void findFunction( Type *type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
    4646                PassVisitor<FindFunction> finder( functions, tyVars, false, predicate );
    4747                type->acceptMutator( finder );
    4848        }
    4949
    50         void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
     50        void findAndReplaceFunction( Type *&type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
    5151                PassVisitor<FindFunction> finder( functions, tyVars, true, predicate );
    5252                type = type->acceptMutator( finder );
    5353        }
    5454
    55         FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
     55        FindFunction::FindFunction( std::list< FunctionType const * > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
    5656                : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
    5757        }
  • src/GenPoly/FindFunction.h

    r44547b0 r4da152a  
    2727
    2828        /// recursively walk `type`, placing all functions that match `predicate` under `tyVars` into `functions`
    29         void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );
     29        void findFunction( Type *type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );
    3030        /// like `findFunction`, but also replaces the function type with void ()(void)
    31         void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );
     31        void findAndReplaceFunction( Type *&type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate );
    3232} // namespace GenPoly
    3333
  • src/GenPoly/GenPoly.cc

    r44547b0 r4da152a  
    112112                if ( ! env ) return type;
    113113                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
     114                        Type *newType = env->lookup( typeInst->get_name() );
     115                        if ( newType ) return newType;
     116                }
     117                return type;
     118        }
     119
     120        const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env ) {
     121                if ( ! env ) return type;
     122                if ( auto typeInst = dynamic_cast< const TypeInstType* >( type ) ) {
    114123                        Type *newType = env->lookup( typeInst->get_name() );
    115124                        if ( newType ) return newType;
  • src/GenPoly/GenPoly.h

    r44547b0 r4da152a  
    3434        /// Replaces a TypeInstType by its referrent in the environment, if applicable
    3535        Type* replaceTypeInst( Type* type, const TypeSubstitution* env );
     36        const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env );
    3637        const ast::Type * replaceTypeInst( const ast::Type *, const ast::TypeSubstitution * );
    3738
     
    116117
    117118        /// Gets the mangled name of this type; alias for SymTab::Mangler::mangleType().
    118         inline std::string mangleType( Type *ty ) { return SymTab::Mangler::mangleType( ty ); }
     119        inline std::string mangleType( const Type *ty ) { return SymTab::Mangler::mangleType( ty ); }
    119120
    120121        /// Gets the name of the sizeof parameter for the type, given its mangled name
Note: See TracChangeset for help on using the changeset viewer.