Changeset 6380f78
- Timestamp:
- May 22, 2019, 3:40:36 PM (6 years ago)
- 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. - Location:
- src
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
rd8938622 r6380f78 1326 1326 1327 1327 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; 1329 1333 return nullptr; 1330 1334 } 1331 1335 1332 1336 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 ); 1334 1340 return nullptr; 1335 1341 } … … 2670 2676 2671 2677 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 ); 2677 2688 } 2678 2689 2679 2690 virtual void visit( AttrExpr * ) override final { 2680 2681 assert( 0 ); 2691 assertf( false, "AttrExpr deprecated in new AST." ); 2682 2692 } 2683 2693 }; -
src/AST/Decl.hpp
rd8938622 r6380f78 232 232 AggregateDecl* set_body( bool b ) { body = b; return this; } 233 233 234 private:235 AggregateDecl * clone() const override = 0;236 MUTATE_FRIEND237 238 protected:239 234 /// Produces a name for the kind of aggregate 240 235 virtual std::string typeString() const = 0; 236 237 private: 238 AggregateDecl * clone() const override = 0; 239 MUTATE_FRIEND 241 240 }; 242 241 … … 256 255 257 256 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 257 258 std::string typeString() const override { return "struct"; } 259 258 260 private: 259 261 StructDecl * clone() const override { return new StructDecl{ *this }; } 260 262 MUTATE_FRIEND 261 262 std::string typeString() const override { return "struct"; }263 263 }; 264 264 … … 271 271 272 272 const Decl * accept( Visitor& v ) const override { return v.visit( this ); } 273 274 std::string typeString() const override { return "union"; } 275 273 276 private: 274 277 UnionDecl * clone() const override { return new UnionDecl{ *this }; } 275 278 MUTATE_FRIEND 276 277 std::string typeString() const override { return "union"; }278 279 }; 279 280 … … 289 290 290 291 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 292 293 std::string typeString() const override { return "enum"; } 294 291 295 private: 292 296 EnumDecl * clone() const override { return new EnumDecl{ *this }; } 293 297 MUTATE_FRIEND 294 295 std::string typeString() const override { return "enum"; }296 298 297 299 /// Map from names to enumerator values; kept private for lazy initialization … … 307 309 308 310 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 311 312 std::string typeString() const override { return "trait"; } 313 309 314 private: 310 315 TraitDecl * clone() const override { return new TraitDecl{ *this }; } 311 316 MUTATE_FRIEND 312 313 std::string typeString() const override { return "trait"; }314 317 }; 315 318 -
src/AST/DeclReplacer.cpp
rd8938622 r6380f78 14 14 // 15 15 16 #warning unimplemented 16 #include "DeclReplacer.hpp" 17 #include "Expr.hpp" 18 #include "Type.hpp" 19 20 #include "Pass.hpp" 21 22 namespace ast { 23 24 namespace 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 } 17 94 18 95 // Local Variables: // -
src/AST/DeclReplacer.hpp
rd8938622 r6380f78 25 25 26 26 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 * >; 29 29 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 ); 33 33 } 34 34 } -
src/AST/Pass.impl.hpp
rd8938622 r6380f78 841 841 for( const auto & clause : node->clauses ) { 842 842 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; 844 844 if(func != clause.target.func) mutated = true; 845 845 … … 852 852 } 853 853 854 Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr;854 const Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr; 855 855 if(stmt != clause.stmt) mutated = true; 856 856 857 Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr;857 const Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr; 858 858 if(cond != clause.cond) mutated = true; 859 859 … … 1567 1567 bool mutated = false; 1568 1568 for( const auto & assoc : node->associations ) { 1569 Type * type = nullptr;1569 const Type * type = nullptr; 1570 1570 if( assoc.type ) { 1571 1571 guard_indexer guard { *this }; … … 1573 1573 if( type != assoc.type ) mutated = true; 1574 1574 } 1575 Expr * expr = nullptr;1575 const Expr * expr = nullptr; 1576 1576 if( assoc.expr ) { 1577 1577 expr = assoc.expr->accept( *this ); … … 1685 1685 VISIT_START( node ); 1686 1686 1687 __pass::indexer::addStruct( node->name, 0, pass);1687 __pass::indexer::addStruct( pass, 0, node->name ); 1688 1688 1689 1689 VISIT({ … … 1702 1702 VISIT_START( node ); 1703 1703 1704 __pass::indexer::addStruct( node->name, 0, pass);1704 __pass::indexer::addStruct( pass, 0, node->name ); 1705 1705 1706 1706 { … … 1885 1885 } 1886 1886 1887 // //--------------------------------------------------------------------------1888 // //TypeSubstitution1889 //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 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 } 1929 1929 1930 1930 #undef VISIT_START -
src/AST/Pass.proto.hpp
rd8938622 r6380f78 126 126 template<typename pass_t, typename node_t> 127 127 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 ); 128 132 node = pass.previsit( node ); 129 133 assert(node); -
src/AST/Stmt.cpp
rd8938622 r6380f78 16 16 #include "Stmt.hpp" 17 17 18 18 19 #include "DeclReplacer.hpp" 20 #include "Type.hpp" 19 21 20 22 namespace ast { 21 23 22 24 // --- CompoundStmt 23 CompoundStmt::CompoundStmt( const CompoundStmt& o ) : Stmt(o), kids(o.kids) { 24 # warning unimplemented 25 assert(!"implemented"); 25 CompoundStmt::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 } 26 55 } 27 56 -
src/AST/module.mk
rd8938622 r6380f78 25 25 AST/LinkageSpec.cpp \ 26 26 AST/Node.cpp \ 27 AST/Pass.cpp \ 27 28 AST/Print.cpp \ 28 29 AST/Stmt.cpp \ -
src/Makefile.in
rd8938622 r6380f78 169 169 AST/Expr.$(OBJEXT) AST/GenericSubstitution.$(OBJEXT) \ 170 170 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) 173 174 am__objects_2 = CodeGen/CodeGenerator.$(OBJEXT) \ 174 175 CodeGen/FixMain.$(OBJEXT) CodeGen/GenType.$(OBJEXT) \ … … 578 579 AST/LinkageSpec.cpp \ 579 580 AST/Node.cpp \ 581 AST/Pass.cpp \ 580 582 AST/Print.cpp \ 581 583 AST/Stmt.cpp \ … … 745 747 AST/$(DEPDIR)/$(am__dirstamp) 746 748 AST/Node.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp) 749 AST/Pass.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp) 747 750 AST/Print.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp) 748 751 AST/Stmt.$(OBJEXT): AST/$(am__dirstamp) AST/$(DEPDIR)/$(am__dirstamp) … … 1178 1181 @AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/LinkageSpec.Po@am__quote@ 1179 1182 @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@ 1180 1184 @AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Print.Po@am__quote@ 1181 1185 @AMDEP_TRUE@@am__include@ @am__quote@AST/$(DEPDIR)/Stmt.Po@am__quote@ -
src/SynTree/Declaration.h
rd8938622 r6380f78 286 286 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; } 287 287 288 virtual void print( std::ostream &os, Indenter indent = {} ) const override ;288 virtual void print( std::ostream &os, Indenter indent = {} ) const override final; 289 289 virtual void printShort( std::ostream &os, Indenter indent = {} ) const override; 290 290 protected:
Note: See TracChangeset
for help on using the changeset viewer.