Changeset 278e162 for src


Ignore:
Timestamp:
Oct 23, 2023, 11:13:00 AM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
cb94e41
Parents:
4d2d7e27 (diff), abb04a4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/DeclStats.cc

    r4d2d7e27 r278e162  
    2525#include "Common/PassVisitor.h"
    2626#include "Common/VectorMap.h"      // for VectorMap
    27 #include "GenPoly/GenPoly.h"       // for hasPolyBase
    2827#include "SynTree/LinkageSpec.h"   // for ::NoOfSpecs, Spec
    2928#include "SynTree/Declaration.h"   // for FunctionDecl, TypeDecl, Declaration
  • src/GenPoly/BoxNew.cpp

    r4d2d7e27 r278e162  
    3939
    4040namespace {
    41 
    42 /// Common field of several sub-passes of box.
    43 struct BoxPass {
    44         TypeVarMap scopeTypeVars;
    45         BoxPass() : scopeTypeVars( ast::TypeData() ) {}
    46 };
    47 
    48 // TODO: Could this be a common helper somewhere?
    49 ast::FunctionType * makeFunctionType( ast::FunctionDecl const * decl ) {
    50         ast::FunctionType * type = new ast::FunctionType(
    51                 decl->type->isVarArgs, decl->type->qualifiers
    52         );
    53         for ( auto type_param : decl->type_params ) {
    54                 type->forall.emplace_back( new ast::TypeInstType( type_param ) );
    55         }
    56         for ( auto assertion : decl->assertions ) {
    57                 type->assertions.emplace_back( new ast::VariableExpr(
    58                         assertion->location, assertion ) );
    59         }
    60         for ( auto param : decl->params ) {
    61                 type->params.emplace_back( param->get_type() );
    62         }
    63         for ( auto retval : decl->returns ) {
    64                 type->returns.emplace_back( retval->get_type() );
    65         }
    66         return type;
    67 }
    6841
    6942// --------------------------------------------------------------------------
     
    359332/// * Adds appropriate type variables to the function calls.
    360333struct CallAdapter final :
    361                 public BoxPass,
    362334                public ast::WithConstTypeSubstitution,
    363335                public ast::WithGuards,
     
    438410                CodeLocation const & location, ast::Type const * type );
    439411
    440         /// Set of adapter functions in the current scope.
     412        TypeVarMap scopeTypeVars;
    441413        ScopedMap< std::string, ast::DeclWithType const * > adapters;
    442414        std::map< ast::ApplicationExpr const *, ast::Expr const * > retVals;
     
    659631        ptrdiff_t initArgCount = mutExpr->args.size();
    660632
    661         TypeVarMap exprTypeVars = { ast::TypeData() };
     633        TypeVarMap exprTypeVars;
    662634        // TODO: Should this take into account the variables already bound in
    663635        // scopeTypeVars ([ex] remove them from exprTypeVars)?
     
    14101382
    14111383ast::FunctionDecl const * DeclAdapter::previsit( ast::FunctionDecl const * decl ) {
    1412         TypeVarMap localTypeVars = { ast::TypeData() };
     1384        TypeVarMap localTypeVars;
    14131385        makeTypeVarMap( decl, localTypeVars );
    14141386
     
    14411413                        layoutParams.emplace_back( alignParam );
    14421414                }
    1443                 // TODO: These should possibly all be gone.
    1444                 // More all assertions into parameter list.
    1445                 for ( ast::ptr<ast::DeclWithType> & assert : mutParam->assertions ) {
    1446                         // Assertion parameters may not be used in body,
    1447                         // pass along with unused attribute.
    1448                         assert.get_and_mutate()->attributes.push_back(
    1449                                 new ast::Attribute( "unused" ) );
    1450                         inferredParams.push_back( assert );
    1451                 }
    1452                 mutParam->assertions.clear();
     1415                // Assertions should be stored in the main list.
     1416                assert( mutParam->assertions.empty() );
    14531417                typeParam = mutParam;
    14541418        }
    1455         // TODO: New version of inner loop.
    14561419        for ( ast::ptr<ast::DeclWithType> & assert : mutDecl->assertions ) {
    14571420                // Assertion parameters may not be used in body,
     
    14681431        spliceBegin( mutDecl->params, layoutParams );
    14691432        addAdapters( mutDecl, localTypeVars );
     1433
     1434        // Now have to update the type to match the declaration.
     1435        ast::FunctionType * type = new ast::FunctionType(
     1436                mutDecl->type->isVarArgs, mutDecl->type->qualifiers );
     1437        for ( auto type_param : mutDecl->type_params ) {
     1438                type->forall.emplace_back( new ast::TypeInstType( type_param ) );
     1439        }
     1440        for ( auto param : mutDecl->params ) {
     1441                type->params.emplace_back( param->get_type() );
     1442        }
     1443        for ( auto retval : mutDecl->returns ) {
     1444                type->returns.emplace_back( retval->get_type() );
     1445        }
     1446        mutDecl->type = type;
    14701447
    14711448        return mutDecl;
     
    15011478                }
    15021479        }
    1503         // TODO: Can this be updated as we go along?
    1504         mutDecl->type = makeFunctionType( mutDecl );
    15051480        return mutDecl;
    15061481}
     
    15581533        assertf( it != adapters.end(), "Could not correct floating node." );
    15591534        return ast::mutate_field( expr, &ast::VariableExpr::var, it->second );
    1560 
    15611535}
    15621536
     
    15701544/// * Inserts dynamic calculation of polymorphic type layouts where needed.
    15711545struct PolyGenericCalculator final :
    1572                 public BoxPass,
    15731546                public ast::WithConstTypeSubstitution,
    15741547                public ast::WithDeclsToAdd<>,
     
    16151588        /// C sizeof().
    16161589        ast::Expr const * genSizeof( CodeLocation const &, ast::Type const * );
    1617 
    16181590        /// Enters a new scope for type-variables,
    16191591        /// adding the type variables from the provided type.
    16201592        void beginTypeScope( ast::Type const * );
    16211593
     1594        /// The type variables and polymorphic parameters currently in scope.
     1595        TypeVarMap scopeTypeVars;
    16221596        /// Set of generic type layouts known in the current scope,
    16231597        /// indexed by sizeofName.
     
    16681642                ast::TypeDecl const * decl ) {
    16691643        ast::Type const * base = decl->base;
    1670         if ( nullptr == base) return decl;
     1644        if ( nullptr == base ) return decl;
    16711645
    16721646        // Add size/align variables for opaque type declarations.
     
    16931667        alignDecl->accept( *visitor );
    16941668
    1695         // Can't use [makeVar], because it inserts into stmtsToAdd and TypeDecls
    1696         // can occur at global scope.
     1669        // A little trick to replace this with two declarations.
     1670        // Adding after makes sure that there is no conflict with adding stmts.
    16971671        declsToAddAfter.push_back( alignDecl );
    1698         // replace with sizeDecl.
    16991672        return sizeDecl;
    17001673}
     
    17211694
    17221695        // Change initialization of a polymorphic value object to allocate via a
    1723         // variable-length-array (alloca was previouly used, but it cannot be
    1724         // safely used in loops).
     1696        // variable-length-array (alloca cannot be safely used in loops).
    17251697        ast::ObjectDecl * newBuf = new ast::ObjectDecl( decl->location,
    17261698                bufNamer.newName(),
     
    22102182/// * Strips fields from generic structure declarations.
    22112183struct Eraser final :
    2212                 public BoxPass,
    22132184                public ast::WithGuards {
    22142185        void guardTypeVarMap( ast::Type const * type ) {
     
    22252196        void previsit( ast::PointerType const * type );
    22262197        void previsit( ast::FunctionType const * type );
     2198public:
     2199        TypeVarMap scopeTypeVars;
    22272200};
    22282201
  • src/GenPoly/GenPoly.cc

    r4d2d7e27 r278e162  
    273273        if ( func->returns.empty() ) return nullptr;
    274274
    275         TypeVarMap forallTypes = { ast::TypeData() };
     275        TypeVarMap forallTypes;
    276276        makeTypeVarMap( func, forallTypes );
    277277        return isDynType( func->returns.front(), forallTypes );
     
    801801        const ast::FunctionType * function = getFunctionType( expr->func->result );
    802802        assertf( function, "ApplicationExpr has non-function type: %s", toString( expr->func->result ).c_str() );
    803         TypeVarMap exprTyVars = { ast::TypeData() };
     803        TypeVarMap exprTyVars;
    804804        makeTypeVarMap( function, exprTyVars );
    805805        return needsBoxing( param, arg, exprTyVars, subst );
  • src/GenPoly/GenPoly.h

    r4d2d7e27 r278e162  
    3333
    3434        typedef ErasableScopedMap< std::string, TypeDecl::Data > TyVarMap;
    35         using TypeVarMap = ErasableScopedMap< ast::TypeEnvKey, ast::TypeData >;
     35        struct TypeVarMap : public ErasableScopedMap<ast::TypeEnvKey, ast::TypeData> {
     36                TypeVarMap() : ErasableScopedMap( ast::TypeData() ) {}
     37        };
    3638
    3739        /// Replaces a TypeInstType by its referrent in the environment, if applicable
  • src/GenPoly/SpecializeNew.cpp

    r4d2d7e27 r278e162  
    8181}
    8282
    83 // The number of elements in a type if it is a flattened tuple.
    84 size_t flatTupleSize( const ast::Type * type ) {
    85         if ( auto tuple = dynamic_cast<const ast::TupleType *>( type ) ) {
    86                 size_t sum = 0;
    87                 for ( auto t : *tuple ) {
    88                         sum += flatTupleSize( t );
    89                 }
    90                 return sum;
    91         } else {
    92                 return 1;
    93         }
     83// The number of elements in a list, if all tuples had been flattened.
     84size_t flatTypeListSize( const std::vector<ast::ptr<ast::Type>> & types ) {
     85        size_t sum = 0;
     86        for ( const ast::ptr<ast::Type> & type : types ) {
     87                if ( const ast::TupleType * tuple = type.as<ast::TupleType>() ) {
     88                        sum += flatTypeListSize( tuple->types );
     89                } else {
     90                        sum += 1;
     91                }
     92        }
     93        return sum;
    9494}
    9595
    9696// Find the total number of components in a parameter list.
    9797size_t functionParameterSize( const ast::FunctionType * type ) {
    98         size_t sum = 0;
    99         for ( auto param : type->params ) {
    100                 sum += flatTupleSize( param );
    101         }
    102         return sum;
     98        return flatTypeListSize( type->params );
    10399}
    104100
Note: See TracChangeset for help on using the changeset viewer.