Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    reada3cf re3e16bc  
    2727
    2828#include "CodeGen/OperatorTable.h"
    29 #include "Common/PassVisitor.h"          // for PassVisitor
    3029#include "Common/ScopedMap.h"            // for ScopedMap, ScopedMap<>::iter...
    3130#include "Common/SemanticError.h"        // for SemanticError
     
    158157                /// * Calculates polymorphic offsetof expressions from offset array
    159158                /// * Inserts dynamic calculation of polymorphic type layouts where needed
    160                 class PolyGenericCalculator final : public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution {
     159                class PolyGenericCalculator final : public PolyMutator {
    161160                public:
     161                        typedef PolyMutator Parent;
     162                        using Parent::mutate;
     163
    162164                        PolyGenericCalculator();
    163165
    164                         void premutate( ObjectDecl *objectDecl );
    165                         void premutate( FunctionDecl *functionDecl );
    166                         void premutate( TypedefDecl *objectDecl );
    167                         void premutate( TypeDecl *objectDecl );
    168                         Declaration * postmutate( TypeDecl *TraitDecl );
    169                         void premutate( PointerType *pointerType );
    170                         void premutate( FunctionType *funcType );
    171                         void premutate( DeclStmt *declStmt );
    172                         Expression *postmutate( MemberExpr *memberExpr );
    173                         Expression *postmutate( SizeofExpr *sizeofExpr );
    174                         Expression *postmutate( AlignofExpr *alignofExpr );
    175                         Expression *postmutate( OffsetofExpr *offsetofExpr );
    176                         Expression *postmutate( OffsetPackExpr *offsetPackExpr );
    177 
    178                         void beginScope();
    179                         void endScope();
     166                        template< typename DeclClass >
     167                        DeclClass *handleDecl( DeclClass *decl, Type *type );
     168                        virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ) override;
     169                        virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) override;
     170                        virtual TypedefDecl *mutate( TypedefDecl *objectDecl ) override;
     171                        virtual TypeDecl *mutate( TypeDecl *objectDecl ) override;
     172                        virtual Statement *mutate( DeclStmt *declStmt ) override;
     173                        virtual Type *mutate( PointerType *pointerType ) override;
     174                        virtual Type *mutate( FunctionType *funcType ) override;
     175                        virtual Expression *mutate( MemberExpr *memberExpr ) override;
     176                        virtual Expression *mutate( SizeofExpr *sizeofExpr ) override;
     177                        virtual Expression *mutate( AlignofExpr *alignofExpr ) override;
     178                        virtual Expression *mutate( OffsetofExpr *offsetofExpr ) override;
     179                        virtual Expression *mutate( OffsetPackExpr *offsetPackExpr ) override;
     180
     181                        virtual void doBeginScope() override;
     182                        virtual void doEndScope() override;
    180183
    181184                private:
     
    191194                        /// Exits the type-variable scope
    192195                        void endTypeScope();
    193                         /// Enters a new scope for knowLayouts and knownOffsets and queues exit calls
    194                         void beginGenericScope();
    195196
    196197                        ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
    197198                        ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName
    198199                        UniqueName bufNamer;                           ///< Namer for VLA buffers
    199                         TyVarMap scopeTyVars;
    200200                };
    201201
     
    250250                Pass1 pass1;
    251251                Pass2 pass2;
    252                 PassVisitor<PolyGenericCalculator> polyCalculator;
     252                PolyGenericCalculator polyCalculator;
    253253                Pass3 pass3;
    254254
     
    256256                mutateTranslationUnit/*All*/( translationUnit, pass1 );
    257257                mutateTranslationUnit/*All*/( translationUnit, pass2 );
    258                 mutateAll( translationUnit, polyCalculator );
     258                mutateTranslationUnit/*All*/( translationUnit, polyCalculator );
    259259                mutateTranslationUnit/*All*/( translationUnit, pass3 );
    260260        }
     
    555555                TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
    556556                        addToTyVarMap( typeDecl, scopeTyVars );
    557                         return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) );
     557                        return Mutator::mutate( typeDecl );
    558558                }
    559559
     
    628628                                        } else {
    629629                                                // xxx - should this be an assertion?
    630                                                 throw SemanticError( toString( *env, "\nunbound type variable: ", tyParm->first, " in application " ), appExpr );
     630                                                std::string x = env ? toString( *env ) : "missing env";
     631                                                throw SemanticError( x + "\n" + "unbound type variable: " + tyParm->first + " in application ", appExpr );
    631632                                        } // if
    632633                                } // if
     
    705706                                if ( concrete == 0 ) {
    706707                                        return typeInst;
     708                                        // xxx - should this be an assertion?
     709//                                      std::string x = env ? toString( *env ) : "missing env";
     710//                                      throw SemanticError( x + "\n" + "Unbound type variable " + typeInst->get_name() + " in ", appExpr );
    707711                                } // if
    708712                                return concrete;
     
    758762                                } else if ( arg->get_result()->get_lvalue() ) {
    759763                                        // argument expression may be CFA lvalue, but not C lvalue -- apply generalizedLvalue transformations.
    760                                         // if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( arg ) ) {
    761                                         //      if ( dynamic_cast<ArrayType *>( varExpr->var->get_type() ) ){
    762                                         //              // temporary hack - don't box arrays, because &arr is not the same as &arr[0]
    763                                         //              return;
    764                                         //      }
    765                                         // }
    766764                                        arg =  generalizedLvalue( new AddressExpr( arg ) );
    767765                                        if ( ! ResolvExpr::typesCompatible( param, arg->get_result(), SymTab::Indexer() ) ) {
     
    13551353                                return handleDecl( typeDecl );
    13561354                        } else {
    1357                                 return dynamic_cast<TypeDecl*>( Parent::mutate( typeDecl ) );
     1355                                return Parent::mutate( typeDecl );
    13581356                        }
    13591357                }
     
    14681466
    14691467                PolyGenericCalculator::PolyGenericCalculator()
    1470                         : knownLayouts(), knownOffsets(), bufNamer( "_buf" ), scopeTyVars( TypeDecl::Data{} ) {}
     1468                        : Parent(), knownLayouts(), knownOffsets(), bufNamer( "_buf" ) {}
    14711469
    14721470                void PolyGenericCalculator::beginTypeScope( Type *ty ) {
    1473                         GuardScope( scopeTyVars );
     1471                        scopeTyVars.beginScope();
    14741472                        makeTyVarMap( ty, scopeTyVars );
    14751473                }
    14761474
    1477                 void PolyGenericCalculator::beginGenericScope() {
    1478                         GuardScope( *this );
    1479                 }
    1480 
    1481                 void PolyGenericCalculator::premutate( ObjectDecl *objectDecl ) {
    1482                         beginTypeScope( objectDecl->get_type() );
    1483                 }
    1484 
    1485                 void PolyGenericCalculator::premutate( FunctionDecl *functionDecl ) {
    1486                         beginGenericScope();
    1487 
    1488                         beginTypeScope( functionDecl->get_functionType() );
    1489                 }
    1490 
    1491                 void PolyGenericCalculator::premutate( TypedefDecl *typedefDecl ) {
    1492                         beginTypeScope( typedefDecl->get_base() );
    1493                 }
    1494 
    1495                 void PolyGenericCalculator::premutate( TypeDecl * typeDecl ) {
     1475                void PolyGenericCalculator::endTypeScope() {
     1476                        scopeTyVars.endScope();
     1477                }
     1478
     1479                template< typename DeclClass >
     1480                DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
     1481                        beginTypeScope( type );
     1482                        // knownLayouts.beginScope();
     1483                        // knownOffsets.beginScope();
     1484
     1485                        DeclClass *ret = static_cast< DeclClass *>( Parent::mutate( decl ) );
     1486
     1487                        // knownOffsets.endScope();
     1488                        // knownLayouts.endScope();
     1489                        endTypeScope();
     1490                        return ret;
     1491                }
     1492
     1493                ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) {
     1494                        return handleDecl( objectDecl, objectDecl->get_type() );
     1495                }
     1496
     1497                DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) {
     1498                        knownLayouts.beginScope();
     1499                        knownOffsets.beginScope();
     1500
     1501                        DeclarationWithType * decl = handleDecl( functionDecl, functionDecl->get_functionType() );
     1502                        knownOffsets.endScope();
     1503                        knownLayouts.endScope();
     1504                        return decl;
     1505                }
     1506
     1507                TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) {
     1508                        return handleDecl( typedefDecl, typedefDecl->get_base() );
     1509                }
     1510
     1511                TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) {
    14961512                        addToTyVarMap( typeDecl, scopeTyVars );
    1497                 }
    1498 
    1499                 Declaration * PolyGenericCalculator::postmutate( TypeDecl *typeDecl ) {
    1500                         if ( Type * base = typeDecl->base ) {
    1501                                 // add size/align variables for opaque type declarations
    1502                                 TypeInstType inst( Type::Qualifiers(), typeDecl->name, typeDecl );
    1503                                 std::string typeName = mangleType( &inst );
    1504                                 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    1505 
    1506                                 ObjectDecl * sizeDecl = ObjectDecl::newObject( sizeofName( typeName ), layoutType, new SingleInit( new SizeofExpr( base->clone() ) ) );
    1507                                 ObjectDecl * alignDecl = ObjectDecl::newObject( alignofName( typeName ), layoutType->clone(), new SingleInit( new AlignofExpr( base->clone() ) ) );
    1508 
    1509                                 // ensure that the initializing sizeof/alignof exprs are properly mutated
    1510                                 sizeDecl->acceptMutator( *visitor );
    1511                                 alignDecl->acceptMutator( *visitor );
    1512 
    1513                                 // can't use makeVar, because it inserts into stmtsToAdd and TypeDecls can occur at global scope
    1514                                 declsToAddAfter.push_back( alignDecl );
    1515                                 // replace with sizeDecl
    1516                                 return sizeDecl;
    1517                         }
    1518                         return typeDecl;
    1519                 }
    1520 
    1521                 void PolyGenericCalculator::premutate( PointerType *pointerType ) {
     1513                        return Parent::mutate( typeDecl );
     1514                }
     1515
     1516                Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
    15221517                        beginTypeScope( pointerType );
    1523                 }
    1524 
    1525                 void PolyGenericCalculator::premutate( FunctionType *funcType ) {
     1518
     1519                        Type *ret = Parent::mutate( pointerType );
     1520
     1521                        endTypeScope();
     1522                        return ret;
     1523                }
     1524
     1525                Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
    15261526                        beginTypeScope( funcType );
    15271527
     
    15341534                                }
    15351535                        }
    1536                 }
    1537 
    1538                 void PolyGenericCalculator::premutate( DeclStmt *declStmt ) {
     1536
     1537                        Type *ret = Parent::mutate( funcType );
     1538
     1539                        endTypeScope();
     1540                        return ret;
     1541                }
     1542
     1543                Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) {
    15391544                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    15401545                                if ( findGeneric( objectDecl->get_type() ) ) {
     
    15451550                                                new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Kind::Char), new NameExpr( sizeofName( mangleType(declType) ) ),
    15461551                                                true, false, std::list<Attribute*>{ new Attribute( "aligned", std::list<Expression*>{ new ConstantExpr( Constant::from_int(8) ) } ) } ), 0 );
    1547                                         stmtsToAddBefore.push_back( new DeclStmt( noLabels, newBuf ) );
     1552                                        stmtsToAdd.push_back( new DeclStmt( noLabels, newBuf ) );
    15481553
    15491554                                        delete objectDecl->get_init();
     
    15511556                                }
    15521557                        }
     1558                        return Parent::mutate( declStmt );
    15531559                }
    15541560
     
    15771583                }
    15781584
    1579                 Expression *PolyGenericCalculator::postmutate( MemberExpr *memberExpr ) {
     1585                Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) {
     1586                        // mutate, exiting early if no longer MemberExpr
     1587                        Expression *expr = Parent::mutate( memberExpr );
     1588                        memberExpr = dynamic_cast< MemberExpr* >( expr );
     1589                        if ( ! memberExpr ) return expr;
     1590
    15801591                        // only mutate member expressions for polymorphic types
    15811592                        int tyDepth;
     
    16241635                ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) {
    16251636                        ObjectDecl *newObj = new ObjectDecl( name, Type::StorageClasses(), LinkageSpec::C, 0, type, init );
    1626                         stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
     1637                        stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
    16271638                        return newObj;
    16281639                }
     
    17031714                                        addOtypeParamsToLayoutCall( layoutCall, otypeParams );
    17041715
    1705                                         stmtsToAddBefore.push_back( new ExprStmt( noLabels, layoutCall ) );
     1716                                        stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
    17061717                                }
    17071718
     
    17291740                                addOtypeParamsToLayoutCall( layoutCall, otypeParams );
    17301741
    1731                                 stmtsToAddBefore.push_back( new ExprStmt( noLabels, layoutCall ) );
     1742                                stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
    17321743
    17331744                                return true;
     
    17371748                }
    17381749
    1739                 Expression *PolyGenericCalculator::postmutate( SizeofExpr *sizeofExpr ) {
     1750                Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) {
    17401751                        Type *ty = sizeofExpr->get_isType() ? sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
    17411752                        if ( findGeneric( ty ) ) {
     
    17471758                }
    17481759
    1749                 Expression *PolyGenericCalculator::postmutate( AlignofExpr *alignofExpr ) {
     1760                Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) {
    17501761                        Type *ty = alignofExpr->get_isType() ? alignofExpr->get_type() : alignofExpr->get_expr()->get_result();
    17511762                        if ( findGeneric( ty ) ) {
     
    17571768                }
    17581769
    1759                 Expression *PolyGenericCalculator::postmutate( OffsetofExpr *offsetofExpr ) {
     1770                Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) {
     1771                        // mutate, exiting early if no longer OffsetofExpr
     1772                        Expression *expr = Parent::mutate( offsetofExpr );
     1773                        offsetofExpr = dynamic_cast< OffsetofExpr* >( expr );
     1774                        if ( ! offsetofExpr ) return expr;
     1775
    17601776                        // only mutate expressions for polymorphic structs/unions
    17611777                        Type *ty = offsetofExpr->get_type();
     
    17771793                }
    17781794
    1779                 Expression *PolyGenericCalculator::postmutate( OffsetPackExpr *offsetPackExpr ) {
     1795                Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) {
    17801796                        StructInstType *ty = offsetPackExpr->get_type();
    17811797
     
    18161832                }
    18171833
    1818                 void PolyGenericCalculator::beginScope() {
     1834                void PolyGenericCalculator::doBeginScope() {
    18191835                        knownLayouts.beginScope();
    18201836                        knownOffsets.beginScope();
    18211837                }
    18221838
    1823                 void PolyGenericCalculator::endScope() {
     1839                void PolyGenericCalculator::doEndScope() {
    18241840                        knownLayouts.endScope();
    18251841                        knownOffsets.endScope();
     
    18781894
    18791895                        addToTyVarMap( typeDecl, scopeTyVars );
    1880                         return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) );
     1896                        return Mutator::mutate( typeDecl );
    18811897                }
    18821898
Note: See TracChangeset for help on using the changeset viewer.