Changeset c935c3a for src/GenPoly/Box.cc


Ignore:
Timestamp:
Sep 13, 2017, 3:03:32 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
ed235b6
Parents:
b2e2e34 (diff), 982832e (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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rb2e2e34 rc935c3a  
    1515
    1616#include <algorithm>                     // for mismatch
    17 #include <cassert>                       // for assert, safe_dynamic_cast
     17#include <cassert>                       // for assert, strict_dynamic_cast
    1818#include <iostream>                      // for operator<<, stringstream
    1919#include <list>                          // for list, list<>::iterator, _Lis...
     
    2727
    2828#include "CodeGen/OperatorTable.h"
     29#include "Common/PassVisitor.h"          // for PassVisitor
    2930#include "Common/ScopedMap.h"            // for ScopedMap, ScopedMap<>::iter...
    3031#include "Common/SemanticError.h"        // for SemanticError
     
    157158                /// * Calculates polymorphic offsetof expressions from offset array
    158159                /// * Inserts dynamic calculation of polymorphic type layouts where needed
    159                 class PolyGenericCalculator final : public PolyMutator {
     160                class PolyGenericCalculator final : public WithGuards, public WithVisitorRef<PolyGenericCalculator>, public WithStmtsToAdd, public WithDeclsToAdd, public WithTypeSubstitution {
    160161                public:
    161                         typedef PolyMutator Parent;
    162                         using Parent::mutate;
    163 
    164162                        PolyGenericCalculator();
    165163
    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;
     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();
    183180
    184181                private:
     
    194191                        /// Exits the type-variable scope
    195192                        void endTypeScope();
     193                        /// Enters a new scope for knowLayouts and knownOffsets and queues exit calls
     194                        void beginGenericScope();
    196195
    197196                        ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
    198197                        ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName
    199198                        UniqueName bufNamer;                           ///< Namer for VLA buffers
     199                        TyVarMap scopeTyVars;
    200200                };
    201201
     
    250250                Pass1 pass1;
    251251                Pass2 pass2;
    252                 PolyGenericCalculator polyCalculator;
     252                PassVisitor<PolyGenericCalculator> polyCalculator;
    253253                Pass3 pass3;
    254254
     
    256256                mutateTranslationUnit/*All*/( translationUnit, pass1 );
    257257                mutateTranslationUnit/*All*/( translationUnit, pass2 );
    258                 mutateTranslationUnit/*All*/( translationUnit, polyCalculator );
     258                mutateAll( translationUnit, polyCalculator );
    259259                mutateTranslationUnit/*All*/( translationUnit, pass3 );
    260260        }
     
    555555                TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
    556556                        addToTyVarMap( typeDecl, scopeTyVars );
    557                         return Mutator::mutate( typeDecl );
     557                        return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) );
    558558                }
    559559
     
    762762                                } else if ( arg->get_result()->get_lvalue() ) {
    763763                                        // argument expression may be CFA lvalue, but not C lvalue -- apply generalizedLvalue transformations.
     764                                        // if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( arg ) ) {
     765                                        //      if ( dynamic_cast<ArrayType *>( varExpr->var->get_type() ) ){
     766                                        //              // temporary hack - don't box arrays, because &arr is not the same as &arr[0]
     767                                        //              return;
     768                                        //      }
     769                                        // }
    764770                                        arg =  generalizedLvalue( new AddressExpr( arg ) );
    765771                                        if ( ! ResolvExpr::typesCompatible( param, arg->get_result(), SymTab::Indexer() ) ) {
     
    12991305
    13001306                DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
    1301                         functionDecl = safe_dynamic_cast< FunctionDecl * > ( handleDecl( functionDecl ) );
     1307                        functionDecl = strict_dynamic_cast< FunctionDecl * > ( handleDecl( functionDecl ) );
    13021308                        FunctionType * ftype = functionDecl->get_functionType();
    13031309                        if ( ! ftype->get_returnVals().empty() && functionDecl->get_statements() ) {
     
    13531359                                return handleDecl( typeDecl );
    13541360                        } else {
    1355                                 return Parent::mutate( typeDecl );
     1361                                return dynamic_cast<TypeDecl*>( Parent::mutate( typeDecl ) );
    13561362                        }
    13571363                }
     
    13781384                        // move polymorphic return type to parameter list
    13791385                        if ( isDynRet( funcType ) ) {
    1380                                 ObjectDecl *ret = safe_dynamic_cast< ObjectDecl* >( funcType->get_returnVals().front() );
     1386                                ObjectDecl *ret = strict_dynamic_cast< ObjectDecl* >( funcType->get_returnVals().front() );
    13811387                                ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
    13821388                                funcType->get_parameters().push_front( ret );
     
    14661472
    14671473                PolyGenericCalculator::PolyGenericCalculator()
    1468                         : Parent(), knownLayouts(), knownOffsets(), bufNamer( "_buf" ) {}
     1474                        : knownLayouts(), knownOffsets(), bufNamer( "_buf" ), scopeTyVars( TypeDecl::Data{} ) {}
    14691475
    14701476                void PolyGenericCalculator::beginTypeScope( Type *ty ) {
    1471                         scopeTyVars.beginScope();
     1477                        GuardScope( scopeTyVars );
    14721478                        makeTyVarMap( ty, scopeTyVars );
    14731479                }
    14741480
    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 ) {
     1481                void PolyGenericCalculator::beginGenericScope() {
     1482                        GuardScope( *this );
     1483                }
     1484
     1485                void PolyGenericCalculator::premutate( ObjectDecl *objectDecl ) {
     1486                        beginTypeScope( objectDecl->get_type() );
     1487                }
     1488
     1489                void PolyGenericCalculator::premutate( FunctionDecl *functionDecl ) {
     1490                        beginGenericScope();
     1491
     1492                        beginTypeScope( functionDecl->get_functionType() );
     1493                }
     1494
     1495                void PolyGenericCalculator::premutate( TypedefDecl *typedefDecl ) {
     1496                        beginTypeScope( typedefDecl->get_base() );
     1497                }
     1498
     1499                void PolyGenericCalculator::premutate( TypeDecl * typeDecl ) {
    15121500                        addToTyVarMap( typeDecl, scopeTyVars );
    1513                         return Parent::mutate( typeDecl );
    1514                 }
    1515 
    1516                 Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
     1501                }
     1502
     1503                Declaration * PolyGenericCalculator::postmutate( TypeDecl *typeDecl ) {
     1504                        if ( Type * base = typeDecl->base ) {
     1505                                // add size/align variables for opaque type declarations
     1506                                TypeInstType inst( Type::Qualifiers(), typeDecl->name, typeDecl );
     1507                                std::string typeName = mangleType( &inst );
     1508                                Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     1509
     1510                                ObjectDecl * sizeDecl = ObjectDecl::newObject( sizeofName( typeName ), layoutType, new SingleInit( new SizeofExpr( base->clone() ) ) );
     1511                                ObjectDecl * alignDecl = ObjectDecl::newObject( alignofName( typeName ), layoutType->clone(), new SingleInit( new AlignofExpr( base->clone() ) ) );
     1512
     1513                                // ensure that the initializing sizeof/alignof exprs are properly mutated
     1514                                sizeDecl->acceptMutator( *visitor );
     1515                                alignDecl->acceptMutator( *visitor );
     1516
     1517                                // can't use makeVar, because it inserts into stmtsToAdd and TypeDecls can occur at global scope
     1518                                declsToAddAfter.push_back( alignDecl );
     1519                                // replace with sizeDecl
     1520                                return sizeDecl;
     1521                        }
     1522                        return typeDecl;
     1523                }
     1524
     1525                void PolyGenericCalculator::premutate( PointerType *pointerType ) {
    15171526                        beginTypeScope( pointerType );
    1518 
    1519                         Type *ret = Parent::mutate( pointerType );
    1520 
    1521                         endTypeScope();
    1522                         return ret;
    1523                 }
    1524 
    1525                 Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
     1527                }
     1528
     1529                void PolyGenericCalculator::premutate( FunctionType *funcType ) {
    15261530                        beginTypeScope( funcType );
    15271531
     
    15341538                                }
    15351539                        }
    1536 
    1537                         Type *ret = Parent::mutate( funcType );
    1538 
    1539                         endTypeScope();
    1540                         return ret;
    1541                 }
    1542 
    1543                 Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) {
     1540                }
     1541
     1542                void PolyGenericCalculator::premutate( DeclStmt *declStmt ) {
    15441543                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    15451544                                if ( findGeneric( objectDecl->get_type() ) ) {
     
    15501549                                                new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::Kind::Char), new NameExpr( sizeofName( mangleType(declType) ) ),
    15511550                                                true, false, std::list<Attribute*>{ new Attribute( "aligned", std::list<Expression*>{ new ConstantExpr( Constant::from_int(8) ) } ) } ), 0 );
    1552                                         stmtsToAdd.push_back( new DeclStmt( noLabels, newBuf ) );
     1551                                        stmtsToAddBefore.push_back( new DeclStmt( noLabels, newBuf ) );
    15531552
    15541553                                        delete objectDecl->get_init();
     
    15561555                                }
    15571556                        }
    1558                         return Parent::mutate( declStmt );
    15591557                }
    15601558
     
    15831581                }
    15841582
    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 
     1583                Expression *PolyGenericCalculator::postmutate( MemberExpr *memberExpr ) {
    15911584                        // only mutate member expressions for polymorphic types
    15921585                        int tyDepth;
     
    16351628                ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) {
    16361629                        ObjectDecl *newObj = new ObjectDecl( name, Type::StorageClasses(), LinkageSpec::C, 0, type, init );
    1637                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
     1630                        stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
    16381631                        return newObj;
    16391632                }
     
    17141707                                        addOtypeParamsToLayoutCall( layoutCall, otypeParams );
    17151708
    1716                                         stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
     1709                                        stmtsToAddBefore.push_back( new ExprStmt( noLabels, layoutCall ) );
    17171710                                }
    17181711
     
    17401733                                addOtypeParamsToLayoutCall( layoutCall, otypeParams );
    17411734
    1742                                 stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
     1735                                stmtsToAddBefore.push_back( new ExprStmt( noLabels, layoutCall ) );
    17431736
    17441737                                return true;
     
    17481741                }
    17491742
    1750                 Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) {
     1743                Expression *PolyGenericCalculator::postmutate( SizeofExpr *sizeofExpr ) {
    17511744                        Type *ty = sizeofExpr->get_isType() ? sizeofExpr->get_type() : sizeofExpr->get_expr()->get_result();
    17521745                        if ( findGeneric( ty ) ) {
     
    17581751                }
    17591752
    1760                 Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) {
     1753                Expression *PolyGenericCalculator::postmutate( AlignofExpr *alignofExpr ) {
    17611754                        Type *ty = alignofExpr->get_isType() ? alignofExpr->get_type() : alignofExpr->get_expr()->get_result();
    17621755                        if ( findGeneric( ty ) ) {
     
    17681761                }
    17691762
    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 
     1763                Expression *PolyGenericCalculator::postmutate( OffsetofExpr *offsetofExpr ) {
    17761764                        // only mutate expressions for polymorphic structs/unions
    17771765                        Type *ty = offsetofExpr->get_type();
     
    17931781                }
    17941782
    1795                 Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) {
     1783                Expression *PolyGenericCalculator::postmutate( OffsetPackExpr *offsetPackExpr ) {
    17961784                        StructInstType *ty = offsetPackExpr->get_type();
    17971785
     
    18321820                }
    18331821
    1834                 void PolyGenericCalculator::doBeginScope() {
     1822                void PolyGenericCalculator::beginScope() {
    18351823                        knownLayouts.beginScope();
    18361824                        knownOffsets.beginScope();
    18371825                }
    18381826
    1839                 void PolyGenericCalculator::doEndScope() {
     1827                void PolyGenericCalculator::endScope() {
    18401828                        knownLayouts.endScope();
    18411829                        knownOffsets.endScope();
     
    18941882
    18951883                        addToTyVarMap( typeDecl, scopeTyVars );
    1896                         return Mutator::mutate( typeDecl );
     1884                        return dynamic_cast<TypeDecl*>( Mutator::mutate( typeDecl ) );
    18971885                }
    18981886
Note: See TracChangeset for help on using the changeset viewer.