Changes in src/AST/Convert.cpp [954c954:4ef08f7]
- File:
-
- 1 edited
-
src/AST/Convert.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r954c954 r4ef08f7 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 decl182 // must be handled **differently** on convert back to old.183 auto ftype = new FunctionType(184 cv(node->type),185 (bool)node->type->isVarArgs186 );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 194 179 auto decl = new FunctionDecl( 195 180 node->name, 196 181 Type::StorageClasses( node->storage.val ), 197 182 LinkageSpec::Spec( node->linkage.val ), 198 ftype, 199 //get<FunctionType>().accept1( node->type ), 183 get<FunctionType>().accept1( node->type ), 200 184 {}, 201 185 get<Attribute>().acceptL( node->attributes ), … … 1168 1152 1169 1153 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 1173 1154 auto ty = new FunctionType { 1174 1155 cv( node ), 1175 1156 (bool)node->isVarArgs 1176 1157 }; 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 ); 1158 ty->returnVals = get<DeclarationWithType>().acceptL( node->returns ); 1159 ty->parameters = get<DeclarationWithType>().acceptL( node->params ); 1194 1160 ty->forall = get<TypeDecl>().acceptL( node->forall ); 1195 1161 return visitType( node, ty ); 1196 1162 } 1197 1163 1198 const ast::Type * postvisit( const ast:: BaseInstType * old, ReferenceToType * ty ) {1164 const ast::Type * postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) { 1199 1165 ty->forall = get<TypeDecl>().acceptL( old->forall ); 1200 1166 ty->parameters = get<Expression>().acceptL( old->params ); … … 1408 1374 ast::Node * node = nullptr; 1409 1375 /// cache of nodes that might be referenced by readonly<> for de-duplication 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 = {}; 1376 std::unordered_map< const BaseSyntaxNode *, ast::Node * > cache = {}; 1415 1377 1416 1378 // Local Utilities: … … 1485 1447 auto it = cache.find( old ); 1486 1448 if ( it == cache.end() ) return false; 1487 node = const_cast<ast::Node *>(it->second.get());1449 node = it->second; 1488 1450 return true; 1489 1451 } … … 1524 1486 virtual void visit( const FunctionDecl * old ) override final { 1525 1487 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 them1531 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 1544 1488 auto decl = new ast::FunctionDecl{ 1545 1489 old->location, 1546 1490 old->name, 1547 // GET_ACCEPT_1(type, FunctionType), 1548 std::move(paramVars), 1549 std::move(returnVars), 1491 GET_ACCEPT_1(type, FunctionType), 1550 1492 {}, 1551 1493 { old->storageClasses.val }, … … 1554 1496 { old->get_funcSpec().val } 1555 1497 }; 1556 1557 decl->type = ftype;1558 1498 cache.emplace( old, decl ); 1559 1560 1499 decl->withExprs = GET_ACCEPT_V(withExprs, Expr); 1561 1500 decl->stmts = GET_ACCEPT_1(statements, CompoundStmt); … … 2576 2515 cv( old ) 2577 2516 }; 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 } 2517 ty->returns = GET_ACCEPT_V( returnVals, DeclWithType ); 2518 ty->params = GET_ACCEPT_V( parameters, DeclWithType ); 2588 2519 ty->forall = GET_ACCEPT_V( forall, TypeDecl ); 2589 2520 visitType( old, ty ); 2590 2521 } 2591 2522 2592 void postvisit( const ReferenceToType * old, ast:: BaseInstType * ty ) {2523 void postvisit( const ReferenceToType * old, ast::ReferenceToType * ty ) { 2593 2524 ty->forall = GET_ACCEPT_V( forall, TypeDecl ); 2594 2525 ty->params = GET_ACCEPT_V( parameters, Expr );
Note:
See TracChangeset
for help on using the changeset viewer.