- Timestamp:
- Oct 23, 2020, 9:06:16 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 37b7d95
- Parents:
- 41b8ea4
- Location:
- src/AST
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/AST/Convert.cpp ¶
r41b8ea4 r490fb92e 47 47 48 48 //================================================================================================ 49 namespace {49 namespace ast { 50 50 51 51 // This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not) 52 52 // allow us to use the same stratagy in the new ast. 53 // xxx - since convert back pass works, this concern seems to be unnecessary. 54 55 // these need to be accessed in new FixInit now 53 56 ast::Type * sizeType = nullptr; 54 57 ast::FunctionDecl * dereferenceOperator = nullptr; … … 63 66 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; 64 67 Cache cache; 68 69 // Statements can no longer be shared. 70 // however, since StmtExprResult is now implemented, need to still maintain 71 // readonly references. 72 Cache readonlyCache; 65 73 66 74 template<typename T> … … 154 162 } 155 163 156 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 157 auto&& bfwd = get<Expression>().accept1( node->bitfieldWidth ); 158 auto&& type = get<Type>().accept1( node->type ); 159 auto&& init = get<Initializer>().accept1( node->init ); 160 auto&& attr = get<Attribute>().acceptL( node->attributes ); 164 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 161 165 if ( inCache( node ) ) { 162 166 return nullptr; 163 167 } 168 auto bfwd = get<Expression>().accept1( node->bitfieldWidth ); 169 auto type = get<Type>().accept1( node->type ); 170 auto attr = get<Attribute>().acceptL( node->attributes ); 171 164 172 auto decl = new ObjectDecl( 165 173 node->name, … … 168 176 bfwd, 169 177 type->clone(), 170 init,178 nullptr, // prevent infinite loop 171 179 attr, 172 180 Type::FuncSpecifiers( node->funcSpec.val ) 173 181 ); 174 return declWithTypePostamble( decl, node ); 182 183 // handles the case where node->init references itself 184 // xxx - does it really happen? 185 declWithTypePostamble(decl, node); 186 auto init = get<Initializer>().accept1( node->init ); 187 decl->init = init; 188 189 this->node = decl; 190 return nullptr; 175 191 } 176 192 … … 205 221 decl->statements = get<CompoundStmt>().accept1( node->stmts ); 206 222 decl->withExprs = get<Expression>().acceptL( node->withExprs ); 207 if ( dereferenceOperator == node ) {223 if ( ast::dereferenceOperator == node ) { 208 224 Validate::dereferenceOperator = decl; 209 225 } 210 if ( dtorStructDestroy == node ) {226 if ( ast::dtorStructDestroy == node ) { 211 227 Validate::dtorStructDestroy = decl; 212 228 } … … 267 283 ); 268 284 269 if ( dtorStruct == node ) {285 if ( ast::dtorStruct == node ) { 270 286 Validate::dtorStruct = decl; 271 287 } … … 320 336 321 337 const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) { 322 cache.emplace( node, stmt ); 338 // force statements in old tree to be unique. 339 // cache.emplace( node, stmt ); 340 readonlyCache.emplace( node, stmt ); 323 341 stmt->location = node->location; 324 342 stmt->labels = makeLabelL( stmt, node->labels ); … … 337 355 if ( inCache( node ) ) return nullptr; 338 356 auto stmt = new ExprStmt( nullptr ); 339 cache.emplace( node, stmt );340 357 stmt->expr = get<Expression>().accept1( node->expr ); 341 358 return stmtPostamble( stmt, node ); … … 1011 1028 auto stmts = node->stmts; 1012 1029 // disable sharing between multiple StmtExprs explicitly. 1013 if (inCache(stmts)) { 1014 stmts = ast::deepCopy(stmts.get()); 1015 } 1030 // this should no longer be true. 1031 1016 1032 auto rslt = new StmtExpr( 1017 1033 get<CompoundStmt>().accept1(stmts) … … 1020 1036 rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls); 1021 1037 rslt->dtors = get<Expression>().acceptL(node->dtors); 1038 if (node->resultExpr) { 1039 // this MUST be found by children visit 1040 rslt->resultExpr = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr)); 1041 } 1022 1042 1023 1043 auto expr = visitBaseExpr( node, rslt ); … … 1036 1056 1037 1057 auto expr = visitBaseExpr( node, rslt ); 1038 this->node = expr ;1058 this->node = expr->clone(); 1039 1059 return nullptr; 1040 1060 } … … 1126 1146 auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind }; 1127 1147 // I believe this should always be a BasicType. 1128 if ( sizeType == node ) {1148 if ( ast::sizeType == node ) { 1129 1149 Validate::SizeType = type; 1130 1150 } … … 1529 1549 1530 1550 // function type is now derived from parameter decls instead of storing them 1551 1552 /* 1531 1553 auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type)); 1532 1554 ftype->params.reserve(paramVars.size()); … … 1540 1562 } 1541 1563 ftype->forall = std::move(forall); 1542 visitType(old->type, ftype); 1564 */ 1565 1566 // can function type have attributes? seems not to be the case. 1567 // visitType(old->type, ftype); 1543 1568 1544 1569 auto decl = new ast::FunctionDecl{ … … 1546 1571 old->name, 1547 1572 // GET_ACCEPT_1(type, FunctionType), 1573 std::move(forall), 1548 1574 std::move(paramVars), 1549 1575 std::move(returnVars), … … 1552 1578 { old->linkage.val }, 1553 1579 GET_ACCEPT_V(attributes, Attribute), 1554 { old->get_funcSpec().val } 1580 { old->get_funcSpec().val }, 1581 old->type->isVarArgs 1555 1582 }; 1556 1583 1557 decl->type = ftype;1584 // decl->type = ftype; 1558 1585 cache.emplace( old, decl ); 1559 1586 … … 1570 1597 1571 1598 if ( Validate::dereferenceOperator == old ) { 1572 dereferenceOperator = decl;1599 ast::dereferenceOperator = decl; 1573 1600 } 1574 1601 1575 1602 if ( Validate::dtorStructDestroy == old ) { 1576 dtorStructDestroy = decl;1603 ast::dtorStructDestroy = decl; 1577 1604 } 1578 1605 } … … 1599 1626 1600 1627 if ( Validate::dtorStruct == old ) { 1601 dtorStruct = decl;1628 ast::dtorStruct = decl; 1602 1629 } 1603 1630 } … … 2531 2558 // I believe this should always be a BasicType. 2532 2559 if ( Validate::SizeType == old ) { 2533 sizeType = type;2560 ast::sizeType = type; 2534 2561 } 2535 2562 visitType( old, type ); -
TabularUnified src/AST/Decl.cpp ¶
r41b8ea4 r490fb92e 48 48 49 49 // --- FunctionDecl 50 51 FunctionDecl::FunctionDecl( const CodeLocation & loc, const std::string & name, 52 std::vector<ptr<TypeDecl>>&& forall, 53 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, 54 CompoundStmt * stmts, Storage::Classes storage, Linkage::Spec linkage, 55 std::vector<ptr<Attribute>>&& attrs, Function::Specs fs, bool isVarArgs) 56 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), 57 stmts( stmts ) { 58 FunctionType * ftype = new FunctionType(static_cast<ArgumentFlag>(isVarArgs)); 59 for (auto & param : this->params) { 60 ftype->params.emplace_back(param->get_type()); 61 } 62 for (auto & ret : this->returns) { 63 ftype->returns.emplace_back(ret->get_type()); 64 } 65 ftype->forall = std::move(forall); 66 this->type = ftype; 67 } 68 50 69 51 70 const Type * FunctionDecl::get_type() const { return type.get(); } -
TabularUnified src/AST/Decl.hpp ¶
r41b8ea4 r490fb92e 131 131 std::vector< ptr<Expr> > withExprs; 132 132 133 FunctionDecl( const CodeLocation & loc, const std::string & name, 133 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall, 134 134 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, 135 135 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 136 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {} )137 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),138 stmts( stmts ) {}136 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false); 137 // : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), 138 // stmts( stmts ) {} 139 139 140 140 const Type * get_type() const override; -
TabularUnified src/AST/DeclReplacer.cpp ¶
r41b8ea4 r490fb92e 38 38 const ast::TypeInstType * previsit( const ast::TypeInstType * ); 39 39 }; 40 41 struct VarExprReplacer { 42 private: 43 const ExprMap & exprMap; 44 45 public: 46 VarExprReplacer(const ExprMap & exprMap): exprMap (exprMap) {} 47 48 const Expr * postvisit (const VariableExpr *); 49 }; 40 50 } 41 51 … … 54 64 DeclMap declMap; 55 65 return replace( node, declMap, typeMap, debug ); 66 } 67 68 const ast::Node * replace( const ast::Node * node, const ExprMap & exprMap) { 69 Pass<VarExprReplacer> replacer = {exprMap}; 70 return node->accept( replacer ); 56 71 } 57 72 … … 88 103 return ninst; 89 104 } 105 106 const Expr * VarExprReplacer::postvisit( const VariableExpr * expr ) { 107 if (!exprMap.count(expr->var)) return expr; 108 109 return exprMap.at(expr->var); 110 } 111 90 112 } 91 113 } -
TabularUnified src/AST/DeclReplacer.hpp ¶
r41b8ea4 r490fb92e 23 23 class DeclWithType; 24 24 class TypeDecl; 25 class Expr; 25 26 26 27 namespace DeclReplacer { 27 28 using DeclMap = std::unordered_map< const DeclWithType *, const DeclWithType * >; 28 29 using TypeMap = std::unordered_map< const TypeDecl *, const TypeDecl * >; 30 using ExprMap = std::unordered_map< const DeclWithType *, const Expr * >; 29 31 30 32 const Node * replace( const Node * node, const DeclMap & declMap, bool debug = false ); 31 33 const Node * replace( const Node * node, const TypeMap & typeMap, bool debug = false ); 32 34 const Node * replace( const Node * node, const DeclMap & declMap, const TypeMap & typeMap, bool debug = false ); 35 const Node * replace( const Node * node, const ExprMap & exprMap); 33 36 } 34 37 } -
TabularUnified src/AST/Expr.cpp ¶
r41b8ea4 r490fb92e 67 67 // --- UntypedExpr 68 68 69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, const Expr * arg ) { 70 70 assert( arg ); 71 71 … … 92 92 } 93 93 94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs,Expr * rhs ) {94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ) { 95 95 assert( lhs && rhs ); 96 96 -
TabularUnified src/AST/Expr.hpp ¶
r41b8ea4 r490fb92e 226 226 227 227 /// Creates a new dereference expression 228 static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg );228 static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg ); 229 229 /// Creates a new assignment expression 230 static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs,Expr * rhs );230 static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ); 231 231 232 232 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } … … 422 422 const CodeLocation & loc, const Type * ty, const std::string & r, 423 423 std::optional<unsigned long long> i ) 424 : Expr( loc, ty ), rep( r ), ival( i ) {}424 : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {} 425 425 426 426 /// Gets the integer value of this constant, if one is appropriate to its type. … … 617 617 618 618 ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call ) 619 : Expr( loc, call->result ) { assert( call); }619 : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); } 620 620 621 621 const Expr * accept( Visitor & v ) const override { return v.visit( this ); } … … 742 742 std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s) 743 743 744 readonly<ExprStmt> resultExpr; 745 744 746 StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ); 745 747 -
TabularUnified src/AST/Fwd.hpp ¶
r41b8ea4 r490fb92e 137 137 typedef unsigned int UniqueId; 138 138 139 extern Type * sizeType; 140 extern FunctionDecl * dereferenceOperator; 141 extern StructDecl * dtorStruct; 142 extern FunctionDecl * dtorStructDestroy; 143 139 144 } -
TabularUnified src/AST/Node.hpp ¶
r41b8ea4 r490fb92e 49 49 50 50 bool unique() const { return strong_count == 1; } 51 bool isManaged() const {return strong_count > 0; } 51 52 52 53 private: -
TabularUnified src/AST/Pass.hpp ¶
r41b8ea4 r490fb92e 236 236 const ast::Expr * call_accept( const ast::Expr * ); 237 237 238 // requests WithStmtsToAdd directly add to this statement, as if it is a compound. 239 240 const ast::Stmt * call_accept_as_compound(const ast::Stmt *); 241 238 242 template< typename node_t > 239 243 auto call_accept( const node_t * node ) -> typename std::enable_if< … … 257 261 template<typename node_t, typename parent_t, typename child_t> 258 262 void maybe_accept(const node_t * &, child_t parent_t::* child); 263 264 template<typename node_t, typename parent_t, typename child_t> 265 void maybe_accept_as_compound(const node_t * &, child_t parent_t::* child); 259 266 260 267 private: -
TabularUnified src/AST/Pass.impl.hpp ¶
r41b8ea4 r490fb92e 167 167 __pedantic_pass_assert( stmt ); 168 168 169 return stmt->accept( *this ); 170 } 171 172 template< typename core_t > 173 const ast::Stmt * ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) { 174 __pedantic_pass_assert( __visit_children() ); 175 __pedantic_pass_assert( stmt ); 176 169 177 // add a few useful symbols to the scope 170 178 using __pass::empty; … … 324 332 325 333 auto new_val = call_accept( old_val ); 334 335 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR"); 336 337 if( __pass::differs(old_val, new_val) ) { 338 auto new_parent = __pass::mutate<core_t>(parent); 339 new_parent->*child = new_val; 340 parent = new_parent; 341 } 342 } 343 344 template< typename core_t > 345 template<typename node_t, typename parent_t, typename child_t> 346 void ast::Pass< core_t >::maybe_accept_as_compound( 347 const node_t * & parent, 348 child_t parent_t::*child 349 ) { 350 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" ); 351 352 if(__pass::skip(parent->*child)) return; 353 const auto & old_val = __pass::get(parent->*child, 0); 354 355 static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR"); 356 357 auto new_val = call_accept_as_compound( old_val ); 326 358 327 359 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR"); … … 703 735 maybe_accept( node, &IfStmt::inits ); 704 736 maybe_accept( node, &IfStmt::cond ); 705 maybe_accept ( node, &IfStmt::thenPart );706 maybe_accept ( node, &IfStmt::elsePart );737 maybe_accept_as_compound( node, &IfStmt::thenPart ); 738 maybe_accept_as_compound( node, &IfStmt::elsePart ); 707 739 }) 708 740 … … 721 753 maybe_accept( node, &WhileStmt::inits ); 722 754 maybe_accept( node, &WhileStmt::cond ); 723 maybe_accept ( node, &WhileStmt::body );755 maybe_accept_as_compound( node, &WhileStmt::body ); 724 756 }) 725 757 … … 736 768 // for statements introduce a level of scope (for the initialization) 737 769 guard_symtab guard { *this }; 770 // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later. 738 771 maybe_accept( node, &ForStmt::inits ); 739 772 maybe_accept( node, &ForStmt::cond ); 740 773 maybe_accept( node, &ForStmt::inc ); 741 maybe_accept ( node, &ForStmt::body );774 maybe_accept_as_compound( node, &ForStmt::body ); 742 775 }) 743 776 … … 834 867 maybe_accept( node, &CatchStmt::decl ); 835 868 maybe_accept( node, &CatchStmt::cond ); 836 maybe_accept ( node, &CatchStmt::body );869 maybe_accept_as_compound( node, &CatchStmt::body ); 837 870 }) 838 871 -
TabularUnified src/AST/SymbolTable.cpp ¶
r41b8ea4 r490fb92e 335 335 } 336 336 337 /* 338 void SymbolTable::addFunction Type( const FunctionType * ftype) {339 addTypes( f type->forall );340 addIds( f type->returns );341 addIds( f type->params );342 } 343 */ 337 338 void SymbolTable::addFunction( const FunctionDecl * func ) { 339 addTypes( func->type->forall ); 340 addIds( func->returns ); 341 addIds( func->params ); 342 } 343 344 344 345 345 void SymbolTable::lazyInitScope() { -
TabularUnified src/AST/SymbolTable.hpp ¶
r41b8ea4 r490fb92e 145 145 146 146 /// convenience function for adding all of the declarations in a function type to the indexer 147 // void addFunctionType( const FunctionType * ftype);147 void addFunction( const FunctionDecl * ); 148 148 149 149 private:
Note: See TracChangeset
for help on using the changeset viewer.