Support for declaring functions within a namespace (#2569)

First step towards permitting declarations within namespaces. Supports only functions within namespaces for now, with no way to call those functions except from within other such functions. Unqualified lookups within a function in a namespace look in that namespace first.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
Richard Smith
2023-02-02 14:11:03 -08:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 0b6411e6cc
commit c172487395
20 changed files with 463 additions and 113 deletions
+10 -3
View File
@@ -225,13 +225,20 @@ void Declaration::PrintID(llvm::raw_ostream& out) const {
}
}
void DeclaredName::Print(llvm::raw_ostream& out) const {
for (const auto& [loc, name] : qualifiers()) {
out << name << ".";
}
out << inner_name();
}
auto GetName(const Declaration& declaration)
-> std::optional<std::string_view> {
switch (declaration.kind()) {
case DeclarationKind::NamespaceDeclaration:
return cast<NamespaceDeclaration>(declaration).name();
case DeclarationKind::FunctionDeclaration:
return cast<FunctionDeclaration>(declaration).name();
return cast<FunctionDeclaration>(declaration).name().inner_name();
case DeclarationKind::DestructorDeclaration:
return "destructor";
case DeclarationKind::ClassDeclaration:
@@ -360,7 +367,7 @@ auto DestructorDeclaration::CreateDestructor(
}
auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
SourceLocation source_loc, std::string name,
SourceLocation source_loc, DeclaredName name,
std::vector<Nonnull<AstNode*>> deduced_params,
Nonnull<TuplePattern*> param_pattern,
ReturnTerm return_term,
@@ -371,7 +378,7 @@ auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
CARBON_ASSIGN_OR_RETURN(split_params,
SplitDeducedParameters(source_loc, deduced_params));
return arena->New<FunctionDeclaration>(
source_loc, name, std::move(split_params.resolved_params),
source_loc, std::move(name), std::move(split_params.resolved_params),
split_params.self_pattern, param_pattern, return_term, body,
virt_override);
}