- Timestamp:
- Sep 24, 2020, 3:56:16 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 6cc913e
- Parents:
- eccb14d
- Location:
- src/AST
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
reccb14d r954c954 177 177 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final { 178 178 if ( inCache( node ) ) return nullptr; 179 180 // function decl contains real variables that the type must use. 181 // the structural change means function type in and out of decl 182 // must be handled **differently** on convert back to old. 183 auto ftype = new FunctionType( 184 cv(node->type), 185 (bool)node->type->isVarArgs 186 ); 187 ftype->returnVals = get<DeclarationWithType>().acceptL(node->returns); 188 ftype->parameters = get<DeclarationWithType>().acceptL(node->params); 189 190 ftype->forall = get<TypeDecl>().acceptL( node->type->forall ); 191 192 visitType(node->type, ftype); 193 179 194 auto decl = new FunctionDecl( 180 195 node->name, 181 196 Type::StorageClasses( node->storage.val ), 182 197 LinkageSpec::Spec( node->linkage.val ), 183 get<FunctionType>().accept1( node->type ), 198 ftype, 199 //get<FunctionType>().accept1( node->type ), 184 200 {}, 185 201 get<Attribute>().acceptL( node->attributes ), … … 1152 1168 1153 1169 const ast::Type * visit( const ast::FunctionType * node ) override final { 1170 static std::string dummy_paramvar_prefix = "__param_"; 1171 static std::string dummy_returnvar_prefix = "__retval_"; 1172 1154 1173 auto ty = new FunctionType { 1155 1174 cv( node ), 1156 1175 (bool)node->isVarArgs 1157 1176 }; 1158 ty->returnVals = get<DeclarationWithType>().acceptL( node->returns ); 1159 ty->parameters = get<DeclarationWithType>().acceptL( node->params ); 1177 auto returns = get<Type>().acceptL(node->returns); 1178 auto params = get<Type>().acceptL(node->params); 1179 1180 int ret_index = 0; 1181 for (auto t: returns) { 1182 // xxx - LinkageSpec shouldn't matter but needs to be something 1183 ObjectDecl * dummy = new ObjectDecl(dummy_returnvar_prefix + std::to_string(ret_index++), {}, LinkageSpec::C, nullptr, t, nullptr); 1184 ty->returnVals.push_back(dummy); 1185 } 1186 int param_index = 0; 1187 for (auto t: params) { 1188 ObjectDecl * dummy = new ObjectDecl(dummy_paramvar_prefix + std::to_string(param_index++), {}, LinkageSpec::C, nullptr, t, nullptr); 1189 ty->parameters.push_back(dummy); 1190 } 1191 1192 // ty->returnVals = get<DeclarationWithType>().acceptL( node->returns ); 1193 // ty->parameters = get<DeclarationWithType>().acceptL( node->params ); 1160 1194 ty->forall = get<TypeDecl>().acceptL( node->forall ); 1161 1195 return visitType( node, ty ); … … 1374 1408 ast::Node * node = nullptr; 1375 1409 /// cache of nodes that might be referenced by readonly<> for de-duplication 1376 std::unordered_map< const BaseSyntaxNode *, ast::Node * > cache = {}; 1410 /// in case that some nodes are dropped by conversion (due to possible structural change) 1411 /// use smart pointers in cache value to prevent accidental invalidation. 1412 /// at conversion stage, all created nodes are guaranteed to be unique, therefore 1413 /// const_casting out of smart pointers is permitted. 1414 std::unordered_map< const BaseSyntaxNode *, ast::ptr<ast::Node> > cache = {}; 1377 1415 1378 1416 // Local Utilities: … … 1447 1485 auto it = cache.find( old ); 1448 1486 if ( it == cache.end() ) return false; 1449 node = it->second;1487 node = const_cast<ast::Node *>(it->second.get()); 1450 1488 return true; 1451 1489 } … … 1486 1524 virtual void visit( const FunctionDecl * old ) override final { 1487 1525 if ( inCache( old ) ) return; 1526 auto paramVars = GET_ACCEPT_V(type->parameters, DeclWithType); 1527 auto returnVars = GET_ACCEPT_V(type->returnVals, DeclWithType); 1528 auto forall = GET_ACCEPT_V(type->forall, TypeDecl); 1529 1530 // function type is now derived from parameter decls instead of storing them 1531 auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type)); 1532 ftype->params.reserve(paramVars.size()); 1533 ftype->returns.reserve(returnVars.size()); 1534 1535 for (auto & v: paramVars) { 1536 ftype->params.emplace_back(v->get_type()); 1537 } 1538 for (auto & v: returnVars) { 1539 ftype->returns.emplace_back(v->get_type()); 1540 } 1541 ftype->forall = std::move(forall); 1542 visitType(old->type, ftype); 1543 1488 1544 auto decl = new ast::FunctionDecl{ 1489 1545 old->location, 1490 1546 old->name, 1491 GET_ACCEPT_1(type, FunctionType), 1547 // GET_ACCEPT_1(type, FunctionType), 1548 std::move(paramVars), 1549 std::move(returnVars), 1492 1550 {}, 1493 1551 { old->storageClasses.val }, … … 1496 1554 { old->get_funcSpec().val } 1497 1555 }; 1556 1557 decl->type = ftype; 1498 1558 cache.emplace( old, decl ); 1559 1499 1560 decl->withExprs = GET_ACCEPT_V(withExprs, Expr); 1500 1561 decl->stmts = GET_ACCEPT_1(statements, CompoundStmt); … … 2515 2576 cv( old ) 2516 2577 }; 2517 ty->returns = GET_ACCEPT_V( returnVals, DeclWithType ); 2518 ty->params = GET_ACCEPT_V( parameters, DeclWithType ); 2578 auto returnVars = GET_ACCEPT_V(returnVals, DeclWithType); 2579 auto paramVars = GET_ACCEPT_V(parameters, DeclWithType); 2580 // ty->returns = GET_ACCEPT_V( returnVals, DeclWithType ); 2581 // ty->params = GET_ACCEPT_V( parameters, DeclWithType ); 2582 for (auto & v: returnVars) { 2583 ty->returns.emplace_back(v->get_type()); 2584 } 2585 for (auto & v: paramVars) { 2586 ty->params.emplace_back(v->get_type()); 2587 } 2519 2588 ty->forall = GET_ACCEPT_V( forall, TypeDecl ); 2520 2589 visitType( old, ty ); -
src/AST/Decl.hpp
reccb14d r954c954 124 124 class FunctionDecl : public DeclWithType { 125 125 public: 126 std::vector<ptr<DeclWithType>> params; 127 std::vector<ptr<DeclWithType>> returns; 128 // declared type, derived from parameter declarations 126 129 ptr<FunctionType> type; 127 130 ptr<CompoundStmt> stmts; 128 131 std::vector< ptr<Expr> > withExprs; 129 132 130 FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type, 133 FunctionDecl( const CodeLocation & loc, const std::string & name, 134 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns, 131 135 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 132 136 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) 133 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type),137 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)), 134 138 stmts( stmts ) {} 135 139 -
src/AST/ForallSubstitutor.hpp
reccb14d r954c954 33 33 } 34 34 35 template<typename node_t > 36 std::vector<ptr<node_t>> operator() (const std::vector<ptr<node_t>> & o) { 37 std::vector<ptr<node_t>> n; 38 n.reserve(o.size()); 39 for (const node_t * d : o) { n.emplace_back(d->accept(*visitor)); } 40 return n; 41 } 42 43 /* 44 35 45 /// Substitute parameter/return type 36 46 std::vector< ptr< DeclWithType > > operator() ( const std::vector< ptr< DeclWithType > > & o ) { … … 48 58 return n; 49 59 } 60 61 */ 50 62 }; 51 63 -
src/AST/Pass.impl.hpp
reccb14d r954c954 465 465 __pass::symtab::addId( core, 0, func ); 466 466 VISIT( 467 // parameter declarations are now directly here 468 maybe_accept( node, &FunctionDecl::params ); 469 maybe_accept( node, &FunctionDecl::returns ); 470 // foralls are still in function type 467 471 maybe_accept( node, &FunctionDecl::type ); 468 472 // function body needs to have the same scope as parameters - CompoundStmt will not enter -
src/AST/SymbolTable.cpp
reccb14d r954c954 335 335 } 336 336 337 /* 337 338 void SymbolTable::addFunctionType( const FunctionType * ftype ) { 338 339 addTypes( ftype->forall ); … … 340 341 addIds( ftype->params ); 341 342 } 343 */ 342 344 343 345 void SymbolTable::lazyInitScope() { … … 368 370 assert( ! params.empty() ); 369 371 // use base type of pointer, so that qualifiers on the pointer type aren't considered. 370 const Type * base = InitTweak::getPointerBase( params.front() ->get_type());372 const Type * base = InitTweak::getPointerBase( params.front() ); 371 373 assert( base ); 372 374 return Mangle::mangle( base ); -
src/AST/SymbolTable.hpp
reccb14d r954c954 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 addFunctionType( const FunctionType * ftype ); 148 148 149 149 private: -
src/AST/Type.cpp
reccb14d r954c954 102 102 // --- FunctionType 103 103 104 104 105 FunctionType::FunctionType( const FunctionType & o ) 105 106 : ParameterizedType( o.qualifiers, copy( o.attributes ) ), returns(), params(), … … 112 113 113 114 namespace { 114 bool containsTtype( const std::vector<ptr< DeclWithType>> & l ) {115 bool containsTtype( const std::vector<ptr<Type>> & l ) { 115 116 if ( ! l.empty() ) { 116 return Tuples::isTtype( l.back() ->get_type());117 return Tuples::isTtype( l.back() ); 117 118 } 118 119 return false; -
src/AST/Type.hpp
reccb14d r954c954 302 302 class FunctionType final : public ParameterizedType { 303 303 public: 304 std::vector<ptr<DeclWithType>> returns; 305 std::vector<ptr<DeclWithType>> params; 304 // std::vector<ptr<DeclWithType>> returns; 305 // std::vector<ptr<DeclWithType>> params; 306 307 std::vector<ptr<Type>> returns; 308 std::vector<ptr<Type>> params; 306 309 307 310 /// Does the function accept a variable number of arguments following the arguments specified
Note:
See TracChangeset
for help on using the changeset viewer.