Changeset 6380f78 for src


Ignore:
Timestamp:
May 22, 2019, 3:40:36 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
76ed81f
Parents:
d8938622 (diff), f4c2f1a (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:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    rd8938622 r6380f78  
    13261326
    13271327        const ast::Attribute * visit( const ast::Attribute * node ) override final {
    1328                 (void)node;
     1328                auto attr = new Attribute(
     1329                        node->name,
     1330                        get<Expression>().acceptL(node->parameters)
     1331                );
     1332                this->node = attr;
    13291333                return nullptr;
    13301334        }
    13311335
    13321336        const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
    1333                 (void)node;
     1337                // Handled by convertTypeSubstitution helper instead.
     1338                // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
     1339                assert( 0 );
    13341340                return nullptr;
    13351341        }
     
    26702676
    26712677        virtual void visit( Constant * ) override final {
    2672 
    2673         }
    2674 
    2675         virtual void visit( Attribute * ) override final {
    2676 
     2678                // Handled in visit( ConstantEpxr * ).
     2679                // In the new tree, Constant fields are inlined into containing ConstantExpression.
     2680                assert( 0 );
     2681        }
     2682
     2683        virtual void visit( Attribute * old ) override final {
     2684                this->node = new ast::Attribute(
     2685                        old->name,
     2686                        GET_ACCEPT_V( parameters, Expr )
     2687                );
    26772688        }
    26782689
    26792690        virtual void visit( AttrExpr * ) override final {
    2680 
    2681                 assert( 0 );
     2691                assertf( false, "AttrExpr deprecated in new AST." );
    26822692        }
    26832693};
  • src/AST/Decl.hpp

    rd8938622 r6380f78  
    232232        AggregateDecl* set_body( bool b ) { body = b; return this; }
    233233
    234 private:
    235         AggregateDecl * clone() const override = 0;
    236         MUTATE_FRIEND
    237 
    238 protected:
    239234        /// Produces a name for the kind of aggregate
    240235        virtual std::string typeString() const = 0;
     236
     237private:
     238        AggregateDecl * clone() const override = 0;
     239        MUTATE_FRIEND
    241240};
    242241
     
    256255
    257256        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     257
     258        std::string typeString() const override { return "struct"; }
     259
    258260private:
    259261        StructDecl * clone() const override { return new StructDecl{ *this }; }
    260262        MUTATE_FRIEND
    261 
    262         std::string typeString() const override { return "struct"; }
    263263};
    264264
     
    271271
    272272        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
     273
     274        std::string typeString() const override { return "union"; }
     275
    273276private:
    274277        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
    275278        MUTATE_FRIEND
    276 
    277         std::string typeString() const override { return "union"; }
    278279};
    279280
     
    289290
    290291        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     292
     293        std::string typeString() const override { return "enum"; }
     294
    291295private:
    292296        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
    293297        MUTATE_FRIEND
    294 
    295         std::string typeString() const override { return "enum"; }
    296298
    297299        /// Map from names to enumerator values; kept private for lazy initialization
     
    307309
    308310        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
     311
     312        std::string typeString() const override { return "trait"; }
     313
    309314private:
    310315        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
    311316        MUTATE_FRIEND
    312 
    313         std::string typeString() const override { return "trait"; }
    314317};
    315318
  • src/AST/DeclReplacer.cpp

    rd8938622 r6380f78  
    1414//
    1515
    16 #warning unimplemented
     16#include "DeclReplacer.hpp"
     17#include "Expr.hpp"
     18#include "Type.hpp"
     19
     20#include "Pass.hpp"
     21
     22namespace ast {
     23
     24namespace DeclReplacer {
     25        namespace {
     26                struct DeclReplacer {
     27                private:
     28                        const DeclMap & declMap;
     29                        const TypeMap & typeMap;
     30                        bool debug;
     31
     32                public:
     33                        DeclReplacer(const DeclMap & declMap, const TypeMap & typeMap, bool debug)
     34                                : declMap( declMap ), typeMap( typeMap ), debug( debug )
     35                        {}
     36
     37                        const ast::VariableExpr * previsit( const ast::VariableExpr * );
     38                        const ast::TypeInstType * previsit( const ast::TypeInstType * );
     39                };
     40        }
     41
     42        const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug ) {
     43                if(!node) return nullptr;
     44                Pass<DeclReplacer> replacer = { declMap, typeMap, debug };
     45                return node->accept( replacer );
     46        }
     47
     48        const ast::Node * replace( const ast::Node * node, const DeclMap & declMap, bool debug ) {
     49                TypeMap typeMap;
     50                return replace( node, declMap, typeMap, debug );
     51        }
     52
     53        const ast::Node * replace( const ast::Node * node, const TypeMap & typeMap, bool debug ) {
     54                DeclMap declMap;
     55                return replace( node, declMap, typeMap, debug );
     56        }
     57
     58        namespace {
     59                // replace variable with new node from decl map
     60                const ast::VariableExpr * DeclReplacer::previsit( const VariableExpr * varExpr ) {
     61                        // xxx - assertions and parameters aren't accounted for in this... (i.e. they aren't inserted into the map when it's made, only DeclStmts are)
     62                        if ( !declMap.count( varExpr->var ) ) return varExpr;
     63
     64                        auto replacement = declMap.at( varExpr->var );
     65                        if ( debug ) {
     66                                std::cerr << "replacing variable reference: "
     67                                        << (void*)varExpr->var.get() << " " << varExpr->var
     68                                        << " with " << (void*)replacement << " " << replacement
     69                                        << std::endl;
     70                        }
     71                        auto nexpr = mutate(varExpr);
     72                        nexpr->var = replacement;
     73                        return nexpr;
     74                }
     75
     76                const TypeInstType * DeclReplacer::previsit( const TypeInstType * inst ) {
     77                        if ( !typeMap.count( inst->base ) ) return inst;
     78
     79                        auto replacement = typeMap.at( inst->base );
     80                        if ( debug ) {
     81                                std::cerr << "replacing type reference: "
     82                                        << (void*)inst->base.get() << " " << inst->base
     83                                        << " with " << (void*)replacement << " " << replacement
     84                                        << std::endl;
     85                        }
     86                        auto ninst = mutate(inst);
     87                        ninst->base = replacement;
     88                        return ninst;
     89                }
     90        }
     91}
     92
     93}
    1794
    1895// Local Variables: //
  • src/AST/DeclReplacer.hpp

    rd8938622 r6380f78  
    2525
    2626        namespace DeclReplacer {
    27                 using DeclMap = std::unordered_map< DeclWithType*, DeclWithType* >;
    28                 using TypeMap = std::unordered_map< TypeDecl*, TypeDecl* >;
     27                using DeclMap = std::unordered_map< const DeclWithType *, const DeclWithType * >;
     28                using TypeMap = std::unordered_map< const TypeDecl *, const TypeDecl * >;
    2929
    30                 void replace( Node* node, const DeclMap& declMap );
    31                 void replace( Node* node, const TypeMap& typeMap );
    32                 void replace( Node* node, const DeclMap& declMap, const TypeMap& typeMap );
     30                const Node * replace( const Node * node, const DeclMap & declMap, bool debug = false );
     31                const Node * replace( const Node * node, const TypeMap & typeMap, bool debug = false );
     32                const Node * replace( const Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false );
    3333        }
    3434}
  • src/AST/Pass.impl.hpp

    rd8938622 r6380f78  
    841841                for( const auto & clause : node->clauses ) {
    842842
    843                         Expr * func = clause.target.func ? clause.target.func->accept(*this) : nullptr;
     843                        const Expr * func = clause.target.func ? clause.target.func->accept(*this) : nullptr;
    844844                        if(func != clause.target.func) mutated = true;
    845845
     
    852852                        }
    853853
    854                         Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr;
     854                        const Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr;
    855855                        if(stmt != clause.stmt) mutated = true;
    856856
    857                         Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr;
     857                        const Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr;
    858858                        if(cond != clause.cond) mutated = true;
    859859
     
    15671567                bool mutated = false;
    15681568                for( const auto & assoc : node->associations ) {
    1569                         Type * type = nullptr;
     1569                        const Type * type = nullptr;
    15701570                        if( assoc.type ) {
    15711571                                guard_indexer guard { *this };
     
    15731573                                if( type != assoc.type ) mutated = true;
    15741574                        }
    1575                         Expr * expr = nullptr;
     1575                        const Expr * expr = nullptr;
    15761576                        if( assoc.expr ) {
    15771577                                expr = assoc.expr->accept( *this );
     
    16851685        VISIT_START( node );
    16861686
    1687         __pass::indexer::addStruct( node->name, 0, pass );
     1687        __pass::indexer::addStruct( pass, 0, node->name );
    16881688
    16891689        VISIT({
     
    17021702        VISIT_START( node );
    17031703
    1704         __pass::indexer::addStruct( node->name, 0, pass );
     1704        __pass::indexer::addStruct( pass, 0, node->name );
    17051705
    17061706        {
     
    18851885}
    18861886
    1887 // //--------------------------------------------------------------------------
    1888 // // TypeSubstitution
    1889 // template< typename pass_t >
    1890 // const ast::TypeSubstitution * ast::Pass< pass_t >::visit( const ast::TypeSubstitution * node ) {
    1891 //      VISIT_START( node );
    1892 
    1893 //      VISIT(
    1894 //              {
    1895 //                      bool mutated = false;
    1896 //                      std::unordered_map< std::string, ast::ptr< ast::Type > > new_map;
    1897 //                      for ( const auto & p : node->typeEnv ) {
    1898 //                              guard_indexer guard { *this };
    1899 //                              auto new_node = p.second->accept( *this );
    1900 //                              if (new_node != p.second) mutated = false;
    1901 //                              new_map.insert({ p.first, new_node });
    1902 //                      }
    1903 //                      if (mutated) {
    1904 //                              auto new_node = mutate( node );
    1905 //                              new_node->typeEnv.swap( new_map );
    1906 //                              node = new_node;
    1907 //                      }
    1908 //              }
    1909 
    1910 //              {
    1911 //                      bool mutated = false;
    1912 //                      std::unordered_map< std::string, ast::ptr< ast::Expr > > new_map;
    1913 //                      for ( const auto & p : node->varEnv ) {
    1914 //                              guard_indexer guard { *this };
    1915 //                              auto new_node = p.second->accept( *this );
    1916 //                              if (new_node != p.second) mutated = false;
    1917 //                              new_map.insert({ p.first, new_node });
    1918 //                      }
    1919 //                      if (mutated) {
    1920 //                              auto new_node = mutate( node );
    1921 //                              new_node->varEnv.swap( new_map );
    1922 //                              node = new_node;
    1923 //                      }
    1924 //              }
    1925 //      )
    1926 
    1927 //      VISIT_END( TypeSubstitution, node );
    1928 // }
     1887//--------------------------------------------------------------------------
     1888// TypeSubstitution
     1889template< typename pass_t >
     1890const ast::TypeSubstitution * ast::Pass< pass_t >::visit( const ast::TypeSubstitution * node ) {
     1891        VISIT_START( node );
     1892
     1893        VISIT(
     1894                {
     1895                        bool mutated = false;
     1896                        std::unordered_map< std::string, ast::ptr< ast::Type > > new_map;
     1897                        for ( const auto & p : node->typeEnv ) {
     1898                                guard_indexer guard { *this };
     1899                                auto new_node = p.second->accept( *this );
     1900                                if (new_node != p.second) mutated = false;
     1901                                new_map.insert({ p.first, new_node });
     1902                        }
     1903                        if (mutated) {
     1904                                auto new_node = mutate( node );
     1905                                new_node->typeEnv.swap( new_map );
     1906                                node = new_node;
     1907                        }
     1908                }
     1909
     1910                {
     1911                        bool mutated = false;
     1912                        std::unordered_map< std::string, ast::ptr< ast::Expr > > new_map;
     1913                        for ( const auto & p : node->varEnv ) {
     1914                                guard_indexer guard { *this };
     1915                                auto new_node = p.second->accept( *this );
     1916                                if (new_node != p.second) mutated = false;
     1917                                new_map.insert({ p.first, new_node });
     1918                        }
     1919                        if (mutated) {
     1920                                auto new_node = mutate( node );
     1921                                new_node->varEnv.swap( new_map );
     1922                                node = new_node;
     1923                        }
     1924                }
     1925        )
     1926
     1927        VISIT_END( TypeSubstitution, node );
     1928}
    19291929
    19301930#undef VISIT_START
  • src/AST/Pass.proto.hpp

    rd8938622 r6380f78  
    126126        template<typename pass_t, typename node_t>
    127127        static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
     128                static_assert(
     129                        std::is_base_of<const node_t, typename std::remove_pointer<decltype( pass.previsit( node ) )>::type >::value,
     130                        "Previsit may not change the type of the node. Use postvisit instead."
     131                );
    128132                node = pass.previsit( node );
    129133                assert(node);
  • src/AST/Stmt.cpp

    rd8938622 r6380f78  
    1616#include "Stmt.hpp"
    1717
     18
    1819#include "DeclReplacer.hpp"
     20#include "Type.hpp"
    1921
    2022namespace ast {
    2123
    2224// --- CompoundStmt
    23 CompoundStmt::CompoundStmt( const CompoundStmt& o ) : Stmt(o), kids(o.kids) {
    24 #   warning unimplemented
    25         assert(!"implemented");
     25CompoundStmt::CompoundStmt( const CompoundStmt& other ) : Stmt(other), kids(other.kids) {
     26        // when cloning a compound statement, we may end up cloning declarations which
     27        // are referred to by VariableExprs throughout the block. Cloning a VariableExpr
     28        // does a shallow copy, so the VariableExpr will end up pointing to the original
     29        // declaration. If the original declaration is deleted, e.g. because the original
     30        // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case,
     31        // find all DeclarationWithType nodes (since a VariableExpr must point to a
     32        // DeclarationWithType) in the original CompoundStmt and map them to the cloned
     33        // node in the new CompoundStmt ('this'), then replace the Declarations referred to
     34        // by each VariableExpr according to the constructed map. Note that only the declarations
     35        // in the current level are collected into the map, because child CompoundStmts will
     36        // recursively execute this routine. There may be more efficient ways of doing
     37        // this.
     38        DeclReplacer::DeclMap declMap;
     39        auto origit = other.kids.begin();
     40        for ( const Stmt * s : kids ) {
     41                assert( origit != other.kids.end() );
     42                const Stmt * origStmt = *origit++;
     43                if ( const DeclStmt * declStmt = dynamic_cast< const DeclStmt * >( s ) ) {
     44                        const DeclStmt * origDeclStmt = strict_dynamic_cast< const DeclStmt * >( origStmt );
     45                        if ( const DeclWithType * dwt = dynamic_cast< const DeclWithType * > ( declStmt->decl.get() ) ) {
     46                                const DeclWithType * origdwt = strict_dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() );
     47                                assert( dwt->name == origdwt->name );
     48                                declMap[ origdwt ] = dwt;
     49                        } else assert( ! dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() ) );
     50                } else assert( ! dynamic_cast< const DeclStmt * > ( s ) );
     51        }
     52        if ( ! declMap.empty() ) {
     53                DeclReplacer::replace( this, declMap );
     54        }
    2655}
    2756
  • src/AST/module.mk

    rd8938622 r6380f78  
    2525        AST/LinkageSpec.cpp \
    2626        AST/Node.cpp \
     27        AST/Pass.cpp \
    2728        AST/Print.cpp \
    2829        AST/Stmt.cpp \
  • src/Makefile.in

    rd8938622 r6380f78  
    169169        AST/Expr.$(OBJEXT) AST/GenericSubstitution.$(OBJEXT) \
    170170        AST/Init.$(OBJEXT) AST/LinkageSpec.$(OBJEXT) \
    171         AST/Node.$(OBJEXT) AST/Print.$(OBJEXT) AST/Stmt.$(OBJEXT) \
    172         AST/Type.$(OBJEXT) AST/TypeSubstitution.$(OBJEXT)
     171        AST/Node.$(OBJEXT) AST/Pass.$(OBJEXT) AST/Print.$(OBJEXT) \
     172        AST/Stmt.$(OBJEXT) AST/Type.$(OBJEXT) \
     173        AST/TypeSubstitution.$(OBJEXT)
    173174am__objects_2 = CodeGen/CodeGenerator.$(OBJEXT) \
    174175        CodeGen/FixMain.$(OBJEXT) CodeGen/GenType.$(OBJEXT) \
     
    578579        AST/LinkageSpec.cpp \
    579580        AST/Node.cpp \
     581        AST/Pass.cpp \
    580582        AST/Print.cpp \
    581583        AST/Stmt.cpp \
     
    745747        AST/$(DEPDIR)/$(am__dirstamp)
    746748AST/Node.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp)
     749AST/Pass.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp)
    747750AST/Print.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp)
    748751AST/Stmt.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp)
     
    11781181@AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/LinkageSpec.Po@am__quote@
    11791182@AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Node.Po@am__quote@
     1183@AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Pass.Po@am__quote@
    11801184@AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Print.Po@am__quote@
    11811185@AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Stmt.Po@am__quote@
  • src/SynTree/Declaration.h

    rd8938622 r6380f78  
    286286        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
    287287
    288         virtual void print( std::ostream &os, Indenter indent = {} ) const override;
     288        virtual void print( std::ostream &os, Indenter indent = {} ) const override final;
    289289        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
    290290  protected:
Note: See TracChangeset for help on using the changeset viewer.