Skip to content

Commit 119bc2c

Browse files
committed
Rewrite block symbols during template instantiation
1 parent 2f7b27d commit 119bc2c

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

packages/cxx-gen-ast/src/new_ast_rewriter_cc.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,25 @@ export function new_ast_rewriter_cc({
8080
});
8181

8282
const emitRewriterBody = (members: Member[], visitor: string = "rewrite") => {
83+
const blockSymbol = members.find(
84+
(m) => m.kind === "attribute" && m.type === "BlockSymbol"
85+
);
86+
87+
if (blockSymbol) {
88+
emit(`auto _ = Binder::ScopeGuard(&rewrite.binder_);
89+
90+
if (ast->${blockSymbol.name}) {
91+
copy->${blockSymbol.name} = control()->newBlockSymbol(rewrite.binder_.scope(),
92+
ast->${blockSymbol.name}->location());
93+
94+
rewrite.binder_.setScope(copy->${blockSymbol.name});
95+
}
96+
`);
97+
}
98+
8399
members.forEach((m) => {
100+
if (m === blockSymbol) return;
101+
84102
switch (m.kind) {
85103
case "node": {
86104
if (isBase(m.type)) {
@@ -242,6 +260,9 @@ export function new_ast_rewriter_cc({
242260
#include <cxx/type_checker.h>
243261
#include <cxx/decl_specs.h>
244262
#include <cxx/decl.h>
263+
#include <cxx/symbols.h>
264+
#include <cxx/types.h>
265+
#include <cxx/binder.h>
245266
246267
namespace cxx {
247268

src/parser/cxx/ast.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ class IfStatementAST final : public StatementAST {
928928
StatementAST* statement = nullptr;
929929
SourceLocation elseLoc;
930930
StatementAST* elseStatement = nullptr;
931+
BlockSymbol* symbol = nullptr;
931932

932933
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
933934

@@ -967,6 +968,7 @@ class SwitchStatementAST final : public StatementAST {
967968
ExpressionAST* condition = nullptr;
968969
SourceLocation rparenLoc;
969970
StatementAST* statement = nullptr;
971+
BlockSymbol* symbol = nullptr;
970972

971973
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
972974

@@ -985,6 +987,7 @@ class WhileStatementAST final : public StatementAST {
985987
ExpressionAST* condition = nullptr;
986988
SourceLocation rparenLoc;
987989
StatementAST* statement = nullptr;
990+
BlockSymbol* symbol = nullptr;
988991

989992
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
990993

@@ -1026,6 +1029,7 @@ class ForRangeStatementAST final : public StatementAST {
10261029
ExpressionAST* rangeInitializer = nullptr;
10271030
SourceLocation rparenLoc;
10281031
StatementAST* statement = nullptr;
1032+
BlockSymbol* symbol = nullptr;
10291033

10301034
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
10311035

@@ -1047,6 +1051,7 @@ class ForStatementAST final : public StatementAST {
10471051
ExpressionAST* expression = nullptr;
10481052
SourceLocation rparenLoc;
10491053
StatementAST* statement = nullptr;
1054+
BlockSymbol* symbol = nullptr;
10501055

10511056
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
10521057

src/parser/cxx/ast_rewriter.cc

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222

2323
// cxx
2424
#include <cxx/ast.h>
25+
#include <cxx/binder.h>
2526
#include <cxx/control.h>
2627
#include <cxx/decl.h>
2728
#include <cxx/decl_specs.h>
29+
#include <cxx/symbols.h>
2830
#include <cxx/translation_unit.h>
2931
#include <cxx/type_checker.h>
32+
#include <cxx/types.h>
3033

3134
namespace cxx {
3235

@@ -2016,6 +2019,15 @@ auto ASTRewriter::StatementVisitor::operator()(CompoundStatementAST* ast)
20162019
-> StatementAST* {
20172020
auto copy = make_node<CompoundStatementAST>(arena());
20182021

2022+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2023+
2024+
if (ast->symbol) {
2025+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2026+
ast->symbol->location());
2027+
2028+
rewrite.binder_.setScope(copy->symbol);
2029+
}
2030+
20192031
copy->lbraceLoc = ast->lbraceLoc;
20202032

20212033
if (auto it = ast->statementList) {
@@ -2028,7 +2040,6 @@ auto ASTRewriter::StatementVisitor::operator()(CompoundStatementAST* ast)
20282040
}
20292041

20302042
copy->rbraceLoc = ast->rbraceLoc;
2031-
copy->symbol = ast->symbol;
20322043

20332044
return copy;
20342045
}
@@ -2037,6 +2048,15 @@ auto ASTRewriter::StatementVisitor::operator()(IfStatementAST* ast)
20372048
-> StatementAST* {
20382049
auto copy = make_node<IfStatementAST>(arena());
20392050

2051+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2052+
2053+
if (ast->symbol) {
2054+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2055+
ast->symbol->location());
2056+
2057+
rewrite.binder_.setScope(copy->symbol);
2058+
}
2059+
20402060
copy->ifLoc = ast->ifLoc;
20412061
copy->constexprLoc = ast->constexprLoc;
20422062
copy->lparenLoc = ast->lparenLoc;
@@ -2069,6 +2089,15 @@ auto ASTRewriter::StatementVisitor::operator()(SwitchStatementAST* ast)
20692089
-> StatementAST* {
20702090
auto copy = make_node<SwitchStatementAST>(arena());
20712091

2092+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2093+
2094+
if (ast->symbol) {
2095+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2096+
ast->symbol->location());
2097+
2098+
rewrite.binder_.setScope(copy->symbol);
2099+
}
2100+
20722101
copy->switchLoc = ast->switchLoc;
20732102
copy->lparenLoc = ast->lparenLoc;
20742103
copy->initializer = rewrite(ast->initializer);
@@ -2083,6 +2112,15 @@ auto ASTRewriter::StatementVisitor::operator()(WhileStatementAST* ast)
20832112
-> StatementAST* {
20842113
auto copy = make_node<WhileStatementAST>(arena());
20852114

2115+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2116+
2117+
if (ast->symbol) {
2118+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2119+
ast->symbol->location());
2120+
2121+
rewrite.binder_.setScope(copy->symbol);
2122+
}
2123+
20862124
copy->whileLoc = ast->whileLoc;
20872125
copy->lparenLoc = ast->lparenLoc;
20882126
copy->condition = rewrite(ast->condition);
@@ -2111,6 +2149,15 @@ auto ASTRewriter::StatementVisitor::operator()(ForRangeStatementAST* ast)
21112149
-> StatementAST* {
21122150
auto copy = make_node<ForRangeStatementAST>(arena());
21132151

2152+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2153+
2154+
if (ast->symbol) {
2155+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2156+
ast->symbol->location());
2157+
2158+
rewrite.binder_.setScope(copy->symbol);
2159+
}
2160+
21142161
copy->forLoc = ast->forLoc;
21152162
copy->lparenLoc = ast->lparenLoc;
21162163
copy->initializer = rewrite(ast->initializer);
@@ -2127,6 +2174,15 @@ auto ASTRewriter::StatementVisitor::operator()(ForStatementAST* ast)
21272174
-> StatementAST* {
21282175
auto copy = make_node<ForStatementAST>(arena());
21292176

2177+
auto _ = Binder::ScopeGuard(&rewrite.binder_);
2178+
2179+
if (ast->symbol) {
2180+
copy->symbol = control()->newBlockSymbol(rewrite.binder_.scope(),
2181+
ast->symbol->location());
2182+
2183+
rewrite.binder_.setScope(copy->symbol);
2184+
}
2185+
21302186
copy->forLoc = ast->forLoc;
21312187
copy->lparenLoc = ast->lparenLoc;
21322188
copy->initializer = rewrite(ast->initializer);

src/parser/cxx/binder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21+
#pragma once
22+
2123
#include <cxx/ast_fwd.h>
2224
#include <cxx/const_value.h>
2325
#include <cxx/source_location.h>

src/parser/cxx/parser.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,8 @@ auto Parser::parse_if_statement(StatementAST*& yyast) -> bool {
33953395

33963396
parse_statement(ast->elseStatement);
33973397

3398+
ast->symbol = blockSymbol;
3399+
33983400
return true;
33993401
}
34003402

@@ -3422,6 +3424,8 @@ auto Parser::parse_switch_statement(StatementAST*& yyast) -> bool {
34223424

34233425
parse_statement(ast->statement);
34243426

3427+
ast->symbol = blockSymbol;
3428+
34253429
return true;
34263430
}
34273431

@@ -3447,6 +3451,8 @@ auto Parser::parse_while_statement(StatementAST*& yyast) -> bool {
34473451

34483452
parse_statement(ast->statement);
34493453

3454+
ast->symbol = blockSymbol;
3455+
34503456
return true;
34513457
}
34523458

@@ -3524,6 +3530,8 @@ auto Parser::parse_for_statement(StatementAST*& yyast) -> bool {
35243530

35253531
parse_statement(ast->statement);
35263532

3533+
ast->symbol = blockSymbol;
3534+
35273535
return true;
35283536
}
35293537

@@ -3546,6 +3554,8 @@ auto Parser::parse_for_statement(StatementAST*& yyast) -> bool {
35463554

35473555
parse_statement(ast->statement);
35483556

3557+
ast->symbol = blockSymbol;
3558+
35493559
return true;
35503560
}
35513561

src/parser/cxx/type_checker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21+
#pragma once
22+
2123
#include <cxx/ast_fwd.h>
2224
#include <cxx/symbols_fwd.h>
2325
#include <cxx/types_fwd.h>

0 commit comments

Comments
 (0)