Skip to content

Commit 9ec35a3

Browse files
committed
Resolve using declarations
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 4adf4c8 commit 9ec35a3

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

src/parser/cxx/name_lookup.cc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ auto Lookup::lookupHelper(Scope* scope, const Name* name,
8484
cache.insert(scope);
8585

8686
for (auto symbol : scope->find(name)) {
87+
if (auto u = symbol_cast<UsingDeclarationSymbol>(symbol);
88+
u && u->target()) {
89+
return u->target();
90+
}
91+
8792
return symbol;
8893
}
8994

@@ -185,6 +190,30 @@ auto Lookup::lookupType(NestedNameSpecifierAST* nestedNameSpecifier,
185190
return nullptr;
186191
}
187192

193+
case SymbolKind::kUsingDeclaration: {
194+
auto usingDeclaration =
195+
symbol_cast<UsingDeclarationSymbol>(nestedNameSpecifier->symbol);
196+
197+
if (!usingDeclaration->target()) return nullptr;
198+
199+
if (auto classSymbol =
200+
symbol_cast<ClassSymbol>(usingDeclaration->target())) {
201+
return lookupTypeHelper(classSymbol->scope(), id, set);
202+
}
203+
204+
if (auto enumSymbol =
205+
symbol_cast<EnumSymbol>(usingDeclaration->target())) {
206+
return lookupTypeHelper(enumSymbol->scope(), id, set);
207+
}
208+
209+
if (auto scopedEnumSymbol =
210+
symbol_cast<ScopedEnumSymbol>(usingDeclaration->target())) {
211+
return lookupTypeHelper(scopedEnumSymbol->scope(), id, set);
212+
}
213+
214+
return nullptr;
215+
}
216+
188217
default:
189218
return nullptr;
190219
} // swotch
@@ -198,9 +227,9 @@ auto Lookup::lookupTypeHelper(Scope* scope, const Identifier* id,
198227
}
199228

200229
for (auto candidate : scope->find(id)) {
201-
if (candidate->isClassOrNamespace() || candidate->isEnumOrScopedEnum() ||
202-
candidate->isTypeAlias() || candidate->isTypeParameter())
230+
if (is_type(candidate) || candidate->isNamespace()) {
203231
return candidate;
232+
}
204233
}
205234

206235
if (auto classSymbol = symbol_cast<ClassSymbol>(scope->owner())) {

src/parser/cxx/parser.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6588,20 +6588,6 @@ auto Parser::is_glvalue(ExpressionAST* expr) const -> bool {
65886588
expr->valueCategory == ValueCategory::kXValue;
65896589
}
65906590

6591-
auto Parser::is_type(Symbol* symbol) const -> bool {
6592-
if (!symbol) return false;
6593-
switch (symbol->kind()) {
6594-
case SymbolKind::kTypeParameter:
6595-
case SymbolKind::kTypeAlias:
6596-
case SymbolKind::kClass:
6597-
case SymbolKind::kEnum:
6598-
case SymbolKind::kScopedEnum:
6599-
return true;
6600-
default:
6601-
return false;
6602-
} // switch
6603-
}
6604-
66056591
auto Parser::is_template(Symbol* symbol) const -> bool {
66066592
auto templateParameters = cxx::getTemplateParameters(symbol);
66076593
return templateParameters != nullptr;
@@ -8325,6 +8311,10 @@ auto Parser::parse_using_declarator(UsingDeclaratorAST*& yyast) -> bool {
83258311

83268312
auto target = Lookup{scope_}.lookup(nestedNameSpecifier, name);
83278313

8314+
if (auto u = symbol_cast<UsingDeclarationSymbol>(target)) {
8315+
target = u->target();
8316+
}
8317+
83288318
auto symbol = control_->newUsingDeclarationSymbol(
83298319
scope_, unqualifiedId->firstSourceLocation());
83308320

src/parser/cxx/parser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ class Parser final {
851851
[[nodiscard]] auto is_xvalue(ExpressionAST* expr) const -> bool;
852852
[[nodiscard]] auto is_glvalue(ExpressionAST* expr) const -> bool;
853853

854-
[[nodiscard]] auto is_type(Symbol* symbol) const -> bool;
855854
[[nodiscard]] auto is_template(Symbol* symbol) const -> bool;
856855
[[nodiscard]] auto is_constructor(Symbol* symbol) const -> bool;
857856

src/parser/cxx/symbols.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,23 @@ void UsingDeclarationSymbol::setDeclarator(UsingDeclaratorAST* declarator) {
631631
declarator_ = declarator;
632632
}
633633

634+
bool is_type(Symbol* symbol) {
635+
if (!symbol) return false;
636+
switch (symbol->kind()) {
637+
case SymbolKind::kTypeParameter:
638+
case SymbolKind::kConstraintTypeParameter:
639+
case SymbolKind::kTypeAlias:
640+
case SymbolKind::kClass:
641+
case SymbolKind::kEnum:
642+
case SymbolKind::kScopedEnum:
643+
return true;
644+
case SymbolKind::kUsingDeclaration: {
645+
auto usingDeclaration = symbol_cast<UsingDeclarationSymbol>(symbol);
646+
return is_type(usingDeclaration->target());
647+
}
648+
default:
649+
return false;
650+
} // switch
651+
}
652+
634653
} // namespace cxx

src/parser/cxx/symbols.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Symbol {
135135
[[nodiscard]] auto next() const -> Symbol*;
136136

137137
#define PROCESS_SYMBOL(S) \
138-
[[nodiscard]] auto is##S() const->bool { return kind_ == SymbolKind::k##S; }
138+
[[nodiscard]] auto is##S() const -> bool { return kind_ == SymbolKind::k##S; }
139139
CXX_FOR_EACH_SYMBOL(PROCESS_SYMBOL)
140140
#undef PROCESS_SYMBOL
141141

@@ -715,6 +715,8 @@ class UsingDeclarationSymbol final : public Symbol {
715715
UsingDeclaratorAST* declarator_ = nullptr;
716716
};
717717

718+
bool is_type(Symbol* symbol);
719+
718720
template <typename Visitor>
719721
auto visit(Visitor&& visitor, Symbol* symbol) {
720722
#define PROCESS_SYMBOL(S) \
@@ -731,7 +733,7 @@ auto visit(Visitor&& visitor, Symbol* symbol) {
731733
}
732734

733735
#define PROCESS_SYMBOL(S) \
734-
inline auto is##S##Symbol(Symbol* symbol)->bool { \
736+
inline auto is##S##Symbol(Symbol* symbol) -> bool { \
735737
return symbol && symbol->kind() == SymbolKind::k##S; \
736738
}
737739

0 commit comments

Comments
 (0)