Skip to content

Commit 2e70dd0

Browse files
committed
Move binding of template parameters out of the parser
1 parent e29f694 commit 2e70dd0

File tree

6 files changed

+219
-171
lines changed

6 files changed

+219
-171
lines changed

src/parser/cxx/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ class LambdaExpressionAST final : public ExpressionAST {
13721372
RequiresClauseAST* requiresClause = nullptr;
13731373
CompoundStatementAST* statement = nullptr;
13741374
TokenKind captureDefault = TokenKind::T_EOF_SYMBOL;
1375+
LambdaSymbol* symbol = nullptr;
13751376

13761377
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
13771378

src/parser/cxx/ast_rewriter.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ auto ASTRewriter::ExpressionVisitor::operator()(LambdaExpressionAST* ast)
24132413
copy->requiresClause = rewrite(ast->requiresClause);
24142414
copy->statement = ast_cast<CompoundStatementAST>(rewrite(ast->statement));
24152415
copy->captureDefault = ast->captureDefault;
2416+
copy->symbol = ast->symbol;
24162417

24172418
return copy;
24182419
}

src/parser/cxx/binder.cc

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <cxx/decl.h>
2727
#include <cxx/decl_specs.h>
2828
#include <cxx/memory_layout.h>
29+
#include <cxx/name_lookup.h>
2930
#include <cxx/names.h>
3031
#include <cxx/scope.h>
3132
#include <cxx/symbols.h>
@@ -186,6 +187,153 @@ auto Binder::declareTypeAlias(SourceLocation identifierLoc, TypeIdAST* typeId)
186187
return symbol;
187188
}
188189

190+
void Binder::bind(UsingDeclaratorAST* ast, Symbol* target) {
191+
if (auto u = symbol_cast<UsingDeclarationSymbol>(target)) {
192+
target = u->target();
193+
}
194+
195+
const auto name = get_name(control(), ast->unqualifiedId);
196+
197+
auto symbol = control()->newUsingDeclarationSymbol(
198+
scope(), ast->unqualifiedId->firstSourceLocation());
199+
200+
ast->symbol = symbol;
201+
202+
symbol->setName(name);
203+
symbol->setDeclarator(ast);
204+
symbol->setTarget(target);
205+
206+
scope()->addSymbol(symbol);
207+
}
208+
209+
void Binder::bind(BaseSpecifierAST* ast) {
210+
Symbol* symbol = nullptr;
211+
212+
if (auto decltypeId = ast_cast<DecltypeIdAST>(ast->unqualifiedId)) {
213+
if (auto classType = type_cast<ClassType>(
214+
control()->remove_cv(decltypeId->decltypeSpecifier->type))) {
215+
symbol = classType->symbol();
216+
}
217+
}
218+
219+
if (auto nameId = ast_cast<NameIdAST>(ast->unqualifiedId)) {
220+
symbol = Lookup{scope_}(ast->nestedNameSpecifier, nameId->identifier);
221+
}
222+
223+
if (auto typeAlias = symbol_cast<TypeAliasSymbol>(symbol)) {
224+
if (auto classType =
225+
type_cast<ClassType>(control()->remove_cv(typeAlias->type()))) {
226+
symbol = classType->symbol();
227+
}
228+
}
229+
230+
if (symbol) {
231+
auto location = ast->unqualifiedId->firstSourceLocation();
232+
auto baseClassSymbol = control()->newBaseClassSymbol(scope(), location);
233+
ast->symbol = baseClassSymbol;
234+
235+
baseClassSymbol->setVirtual(ast->isVirtual);
236+
baseClassSymbol->setSymbol(symbol);
237+
238+
if (symbol) {
239+
baseClassSymbol->setName(symbol->name());
240+
}
241+
242+
switch (ast->accessSpecifier) {
243+
case TokenKind::T_PRIVATE:
244+
baseClassSymbol->setAccessSpecifier(AccessSpecifier::kPrivate);
245+
break;
246+
case TokenKind::T_PROTECTED:
247+
baseClassSymbol->setAccessSpecifier(AccessSpecifier::kProtected);
248+
break;
249+
case TokenKind::T_PUBLIC:
250+
baseClassSymbol->setAccessSpecifier(AccessSpecifier::kPublic);
251+
break;
252+
default:
253+
break;
254+
} // switch
255+
}
256+
}
257+
258+
void Binder::bind(NonTypeTemplateParameterAST* ast, int index, int depth) {
259+
auto symbol = control()->newNonTypeParameterSymbol(
260+
scope(), ast->declaration->firstSourceLocation());
261+
ast->symbol = symbol;
262+
263+
symbol->setIndex(index);
264+
symbol->setDepth(depth);
265+
symbol->setName(ast->declaration->identifier);
266+
symbol->setParameterPack(ast->declaration->isPack);
267+
symbol->setObjectType(ast->declaration->type);
268+
scope()->addSymbol(symbol);
269+
}
270+
271+
void Binder::bind(TypenameTypeParameterAST* ast, int index, int depth) {
272+
auto location = ast->identifier ? ast->identifierLoc : ast->classKeyLoc;
273+
274+
auto symbol = control()->newTypeParameterSymbol(scope(), location);
275+
ast->symbol = symbol;
276+
277+
symbol->setIndex(index);
278+
symbol->setDepth(depth);
279+
symbol->setParameterPack(ast->isPack);
280+
symbol->setName(ast->identifier);
281+
scope()->addSymbol(symbol);
282+
}
283+
284+
void Binder::bind(ConstraintTypeParameterAST* ast, int index, int depth) {
285+
auto symbol =
286+
control()->newConstraintTypeParameterSymbol(scope(), ast->identifierLoc);
287+
symbol->setIndex(index);
288+
symbol->setDepth(depth);
289+
symbol->setName(ast->identifier);
290+
scope()->addSymbol(symbol);
291+
}
292+
293+
void Binder::bind(TemplateTypeParameterAST* ast, int index, int depth) {
294+
auto symbol =
295+
control()->newTemplateTypeParameterSymbol(scope(), ast->templateLoc);
296+
297+
ast->symbol = symbol;
298+
299+
symbol->setIndex(index);
300+
symbol->setDepth(depth);
301+
symbol->setName(ast->identifier);
302+
symbol->setParameterPack(ast->isPack);
303+
scope()->addSymbol(symbol);
304+
}
305+
306+
void Binder::bind(ConceptDefinitionAST* ast) {
307+
auto templateParameters = currentTemplateParameters();
308+
309+
auto symbol = control()->newConceptSymbol(scope(), ast->identifierLoc);
310+
symbol->setName(ast->identifier);
311+
symbol->setTemplateParameters(templateParameters);
312+
313+
declaringScope()->addSymbol(symbol);
314+
}
315+
316+
void Binder::bind(LambdaExpressionAST* ast) {
317+
auto parentScope = declaringScope();
318+
auto symbol = control()->newLambdaSymbol(parentScope, ast->lbracketLoc);
319+
ast->symbol = symbol;
320+
321+
setScope(symbol);
322+
}
323+
324+
void Binder::complete(LambdaExpressionAST* ast) {
325+
if (auto params = ast->parameterDeclarationClause) {
326+
auto lambdaScope = ast->symbol->scope();
327+
lambdaScope->addSymbol(params->functionParametersSymbol);
328+
setScope(params->functionParametersSymbol);
329+
} else {
330+
setScope(ast->symbol);
331+
}
332+
333+
auto parentScope = ast->symbol->enclosingScope();
334+
parentScope->addSymbol(ast->symbol);
335+
}
336+
189337
auto Binder::declareTypedef(DeclaratorAST* declarator, const Decl& decl)
190338
-> TypeAliasSymbol* {
191339
auto name = decl.getName();

src/parser/cxx/binder.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ class Binder {
9393
void bind(ParameterDeclarationAST* ast, const Decl& decl,
9494
bool inTemplateParameters);
9595

96+
void bind(UsingDeclaratorAST* ast, Symbol* target);
97+
98+
void bind(BaseSpecifierAST* ast);
99+
100+
void bind(NonTypeTemplateParameterAST* ast, int index, int depth);
101+
102+
void bind(TypenameTypeParameterAST* ast, int index, int depth);
103+
104+
void bind(ConstraintTypeParameterAST* ast, int index, int depth);
105+
106+
void bind(TemplateTypeParameterAST* ast, int index, int depth);
107+
108+
void bind(ConceptDefinitionAST* ast);
109+
110+
void bind(LambdaExpressionAST* ast);
111+
112+
void complete(LambdaExpressionAST* ast);
113+
96114
class ScopeGuard {
97115
public:
98116
Binder* p = nullptr;

0 commit comments

Comments
 (0)