mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 15:41:04 +01:00
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:
co-authored by
Jon Ross-Perkins
parent
0b6411e6cc
commit
c172487395
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user