Skip to content

Commit 22e68c7

Browse files
committed
Merge Scope and ScopedSymbol into ScopeSymbol
1 parent 2d78c28 commit 22e68c7

39 files changed

+648
-735
lines changed

src/frontend/cxx/frontend.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <cxx/memory_layout.h>
3333
#include <cxx/preprocessor.h>
3434
#include <cxx/private/path.h>
35-
#include <cxx/scope.h>
3635
#include <cxx/symbols.h>
3736
#include <cxx/translation_unit.h>
3837
#include <cxx/types.h>
@@ -439,7 +438,7 @@ void Frontend::Private::dumpTokens(std::ostream& out) {
439438
void Frontend::Private::dumpSymbols(std::ostream& out) {
440439
if (!cli.opt_dump_symbols) return;
441440
auto globalScope = unit_->globalScope();
442-
auto globalNamespace = globalScope->owner();
441+
auto globalNamespace = globalScope;
443442
cxx::dump(out, globalNamespace);
444443
}
445444

src/lsp/cxx/lsp/cxx_document.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#include <cxx/macos_toolchain.h>
2929
#include <cxx/preprocessor.h>
3030
#include <cxx/private/path.h>
31-
#include <cxx/scope.h>
3231
#include <cxx/symbols.h>
3332
#include <cxx/translation_unit.h>
3433
#include <cxx/types.h>
34+
#include <cxx/views/symbols.h>
3535
#include <cxx/wasm32_wasi_toolchain.h>
3636
#include <cxx/windows_toolchain.h>
3737

src/mlir/cxx/mlir/codegen.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,10 @@ auto Codegen::findOrCreateFunction(FunctionSymbol* functionSymbol)
109109
std::vector<mlir::Type> inputTypes;
110110
std::vector<mlir::Type> resultTypes;
111111

112-
if (!functionSymbol->isStatic() &&
113-
functionSymbol->enclosingSymbol()->isClass()) {
112+
if (!functionSymbol->isStatic() && functionSymbol->parent()->isClass()) {
114113
// if it is a non static member function, we need to add the `this` pointer
115114

116-
auto classSymbol =
117-
symbol_cast<ClassSymbol>(functionSymbol->enclosingSymbol());
115+
auto classSymbol = symbol_cast<ClassSymbol>(functionSymbol->parent());
118116

119117
inputTypes.push_back(builder_.getType<mlir::cxx::PointerType>(
120118
convertType(classSymbol->type())));

src/mlir/cxx/mlir/codegen_declarations.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#include <cxx/ast.h>
2525
#include <cxx/control.h>
2626
#include <cxx/external_name_encoder.h>
27-
#include <cxx/scope.h>
2827
#include <cxx/symbols.h>
2928
#include <cxx/translation_unit.h>
3029
#include <cxx/types.h>
30+
#include <cxx/views/symbols.h>
3131

3232
// mlir
3333
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
@@ -40,7 +40,7 @@ namespace cxx {
4040
struct Codegen::DeclarationVisitor {
4141
Codegen& gen;
4242

43-
void allocateLocals(ScopedSymbol* block);
43+
void allocateLocals(ScopeSymbol* block);
4444

4545
auto operator()(SimpleDeclarationAST* ast) -> DeclarationResult;
4646
auto operator()(AsmDeclarationAST* ast) -> DeclarationResult;
@@ -142,8 +142,8 @@ auto Codegen::lambdaSpecifier(LambdaSpecifierAST* ast)
142142
return {};
143143
}
144144

145-
void Codegen::DeclarationVisitor::allocateLocals(ScopedSymbol* block) {
146-
for (auto symbol : block->scope()->symbols()) {
145+
void Codegen::DeclarationVisitor::allocateLocals(ScopeSymbol* block) {
146+
for (auto symbol : views::members(block)) {
147147
if (auto nestedBlock = symbol_cast<BlockSymbol>(symbol)) {
148148
allocateLocals(nestedBlock);
149149
continue;
@@ -380,10 +380,8 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
380380
mlir::Value thisValue;
381381

382382
// if this is a non static member function, we need to allocate the `this`
383-
if (!functionSymbol->isStatic() &&
384-
functionSymbol->enclosingSymbol()->isClass()) {
385-
auto classSymbol =
386-
symbol_cast<ClassSymbol>(functionSymbol->enclosingSymbol());
383+
if (!functionSymbol->isStatic() && functionSymbol->parent()->isClass()) {
384+
auto classSymbol = symbol_cast<ClassSymbol>(functionSymbol->parent());
387385

388386
auto thisType = gen.convertType(classSymbol->type());
389387
auto ptrType = gen.builder_.getType<mlir::cxx::PointerType>(thisType);
@@ -396,13 +394,13 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
396394
}
397395

398396
FunctionParametersSymbol* params = nullptr;
399-
for (auto member : ast->symbol->scope()->symbols()) {
397+
for (auto member : views::members(ast->symbol)) {
400398
params = symbol_cast<FunctionParametersSymbol>(member);
401399
if (!params) continue;
402400

403401
int argc = 0;
404402
auto args = gen.entryBlock_->getArguments();
405-
for (auto param : params->scope()->symbols()) {
403+
for (auto param : views::members(params)) {
406404
auto arg = symbol_cast<ParameterSymbol>(param);
407405
if (!arg) continue;
408406

src/mlir/cxx/mlir/codegen_expressions.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#include <cxx/control.h>
2727
#include <cxx/literals.h>
2828
#include <cxx/memory_layout.h>
29-
#include <cxx/scope.h>
3029
#include <cxx/symbols.h>
3130
#include <cxx/translation_unit.h>
3231
#include <cxx/types.h>
32+
#include <cxx/views/symbols.h>
3333

3434
// mlir
3535
#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
@@ -645,8 +645,8 @@ auto Codegen::ExpressionVisitor::operator()(MemberExpressionAST* ast)
645645
// todo: introduce ClassLayout to avoid linear searches and support c++
646646
// class layout
647647
int fieldIndex = 0;
648-
auto classSymbol = symbol_cast<ClassSymbol>(field->enclosingSymbol());
649-
for (auto member : classSymbol->scope()->symbols()) {
648+
auto classSymbol = symbol_cast<ClassSymbol>(field->parent());
649+
for (auto member : cxx::views::members(classSymbol)) {
650650
auto f = symbol_cast<FieldSymbol>(member);
651651
if (!f) continue;
652652
if (f->isStatic()) continue;

src/mlir/cxx/mlir/codegen_units.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#include <cxx/ast_visitor.h>
2626
#include <cxx/control.h>
2727
#include <cxx/memory_layout.h>
28-
#include <cxx/scope.h>
2928
#include <cxx/symbols.h>
3029
#include <cxx/translation_unit.h>
30+
#include <cxx/views/symbols.h>
3131

3232
// mlir
3333
#include <llvm/IR/DataLayout.h>
@@ -69,7 +69,7 @@ struct Codegen::UnitVisitor {
6969
UnitVisitor& p;
7070

7171
void operator()(NamespaceSymbol* symbol) {
72-
for (auto member : symbol->scope()->symbols()) {
72+
for (auto member : views::members(symbol)) {
7373
visit(*this, member);
7474
}
7575
}
@@ -86,7 +86,7 @@ struct Codegen::UnitVisitor {
8686
}
8787

8888
if (!symbol->templateParameters()) {
89-
for (auto member : symbol->scope()->symbols()) {
89+
for (auto member : views::members(symbol)) {
9090
visit(*this, member);
9191
}
9292
}
@@ -149,7 +149,7 @@ auto Codegen::UnitVisitor::operator()(TranslationUnitAST* ast) -> UnitResult {
149149

150150
std::swap(gen.module_, module);
151151

152-
visit(visitor, gen.unit_->globalScope()->owner());
152+
visit(visitor, gen.unit_->globalScope());
153153

154154
#if false
155155
ForEachExternalDefinition forEachExternalDefinition;

src/mlir/cxx/mlir/convert_type.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#include <cxx/literals.h>
2727
#include <cxx/memory_layout.h>
2828
#include <cxx/names.h>
29-
#include <cxx/scope.h>
3029
#include <cxx/symbols.h>
3130
#include <cxx/translation_unit.h>
3231
#include <cxx/types.h>
32+
#include <cxx/views/symbols.h>
3333

3434
#include <format>
3535

@@ -294,7 +294,7 @@ auto Codegen::ConvertType::operator()(const ClassType* type) -> mlir::Type {
294294

295295
std::vector<mlir::Type> memberTypes;
296296

297-
for (auto member : classSymbol->scope()->symbols()) {
297+
for (auto member : views::members(classSymbol)) {
298298
auto field = symbol_cast<FieldSymbol>(member);
299299
if (!field) continue;
300300
if (field->isStatic()) continue;

src/parser/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ add_library(cxx-parser
5858
cxx/parser.cc
5959
cxx/path.cc
6060
cxx/preprocessor.cc
61-
cxx/scope.cc
6261
cxx/source_location.cc
6362
cxx/symbol_chain_view.cc
6463
cxx/symbol_printer.cc

src/parser/cxx/ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class MemInitializerAST : public AST {
228228
class NestedNameSpecifierAST : public AST {
229229
public:
230230
using AST::AST;
231-
ScopedSymbol* symbol = nullptr;
231+
ScopeSymbol* symbol = nullptr;
232232
};
233233

234234
class NewInitializerAST : public AST {

src/parser/cxx/ast_rewriter.cc

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,7 @@
3434

3535
namespace cxx {
3636

37-
auto ASTRewriter::make_substitution(
38-
TranslationUnit* unit, TemplateDeclarationAST* templateDecl,
39-
List<TemplateArgumentAST*>* templateArgumentList)
40-
-> std::vector<TemplateArgument> {
41-
auto control = unit->control();
42-
auto interp = ASTInterpreter{unit};
43-
44-
std::vector<TemplateArgument> templateArguments;
45-
46-
for (auto arg : ListView{templateArgumentList}) {
47-
if (auto exprArg = ast_cast<ExpressionTemplateArgumentAST>(arg)) {
48-
auto expr = exprArg->expression;
49-
auto value = interp.evaluate(expr);
50-
if (!value.has_value()) {
51-
#if false
52-
unit->error(arg->firstSourceLocation(),
53-
"template argument is not a constant expression");
54-
#endif
55-
continue;
56-
}
57-
58-
// ### need to set scope and location
59-
auto templArg = control->newVariableSymbol(nullptr, {});
60-
templArg->setInitializer(expr);
61-
templArg->setType(control->add_const(expr->type));
62-
templArg->setConstValue(value);
63-
templateArguments.push_back(templArg);
64-
} else if (auto typeArg = ast_cast<TypeTemplateArgumentAST>(arg)) {
65-
auto type = typeArg->typeId->type;
66-
// ### need to set scope and location
67-
auto templArg = control->newTypeAliasSymbol(nullptr, {});
68-
templArg->setType(type);
69-
templateArguments.push_back(templArg);
70-
}
71-
}
72-
73-
return templateArguments;
74-
}
75-
76-
ASTRewriter::ASTRewriter(TranslationUnit* unit, Scope* scope,
37+
ASTRewriter::ASTRewriter(TranslationUnit* unit, ScopeSymbol* scope,
7738
const std::vector<TemplateArgument>& templateArguments)
7839
: unit_(unit), templateArguments_(templateArguments), binder_(unit_) {
7940
binder_.setScope(scope);
@@ -167,7 +128,7 @@ auto ASTRewriter::instantiateClassTemplate(
167128
return subst;
168129
}
169130

170-
auto parentScope = classSymbol->enclosingSymbol()->scope();
131+
auto parentScope = classSymbol->enclosingNonTemplateParametersScope();
171132

172133
auto rewriter = ASTRewriter{unit, parentScope, templateArguments};
173134

@@ -222,7 +183,10 @@ auto ASTRewriter::instantiateTypeAliasTemplate(
222183
}
223184
#endif
224185

225-
auto parentScope = typeAliasSymbol->enclosingSymbol()->scope();
186+
auto parentScope = typeAliasSymbol->parent();
187+
while (parentScope->isTemplateParameters()) {
188+
parentScope = parentScope->parent();
189+
}
226190

227191
auto rewriter = ASTRewriter{unit, parentScope, templateArguments};
228192

@@ -236,4 +200,43 @@ auto ASTRewriter::instantiateTypeAliasTemplate(
236200
return instance->symbol;
237201
}
238202

203+
auto ASTRewriter::make_substitution(
204+
TranslationUnit* unit, TemplateDeclarationAST* templateDecl,
205+
List<TemplateArgumentAST*>* templateArgumentList)
206+
-> std::vector<TemplateArgument> {
207+
auto control = unit->control();
208+
auto interp = ASTInterpreter{unit};
209+
210+
std::vector<TemplateArgument> templateArguments;
211+
212+
for (auto arg : ListView{templateArgumentList}) {
213+
if (auto exprArg = ast_cast<ExpressionTemplateArgumentAST>(arg)) {
214+
auto expr = exprArg->expression;
215+
auto value = interp.evaluate(expr);
216+
if (!value.has_value()) {
217+
#if false
218+
unit->error(arg->firstSourceLocation(),
219+
"template argument is not a constant expression");
220+
#endif
221+
continue;
222+
}
223+
224+
// ### need to set scope and location
225+
auto templArg = control->newVariableSymbol(nullptr, {});
226+
templArg->setInitializer(expr);
227+
templArg->setType(control->add_const(expr->type));
228+
templArg->setConstValue(value);
229+
templateArguments.push_back(templArg);
230+
} else if (auto typeArg = ast_cast<TypeTemplateArgumentAST>(arg)) {
231+
auto type = typeArg->typeId->type;
232+
// ### need to set scope and location
233+
auto templArg = control->newTypeAliasSymbol(nullptr, {});
234+
templArg->setType(type);
235+
templateArguments.push_back(templArg);
236+
}
237+
}
238+
239+
return templateArguments;
240+
}
241+
239242
} // namespace cxx

0 commit comments

Comments
 (0)