mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This handles namespacing of functions. Parsing and semantics are changed significantly, while lowering works without changes. Variables can't be namespaced yet because they're dealing with patterns, and I didn't dig through that code. Most of the logic is done through the new name declaration stack, which is necessary because semantics isn't quite sure where the declaration name ends. It'd be complex for parsing to send a signal about this, probably involving node variants and rewrites of the tree, and this solution seems to work well. Unfortunately this means a new stack, but that may be inevitable due to the extra information needing to be tracked. Note this doesn't deal with scoped lookups of non-namespace things, which we'll need for generics. That'll probably involve pushing resolved scopes onto a stack (or maybe just setting a singleton value?) to affect contextual name lookup. But, I think the basics are there to make it work when we can test the behavior. This renames "designator expression" to "qualified expression" and adds "qualified declaration" in order to use terminology more consistent with C++. Namespaces will probably need to be considered for name mangling down the line, but this still uses the basic name.
69 lines
2.1 KiB
C++
69 lines
2.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/parser/parser_context.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Handles an unrecognized declaration, adding an error node.
|
|
static auto ParserHandleUnrecognizedDeclaration(ParserContext& context)
|
|
-> void {
|
|
CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
|
|
"Unrecognized declaration introducer.");
|
|
context.emitter().Emit(*context.position(), UnrecognizedDeclaration);
|
|
auto cursor = *context.position();
|
|
auto semi = context.SkipPastLikelyEnd(cursor);
|
|
// Locate the EmptyDeclaration at the semi when found, but use the
|
|
// original cursor location for an error when not.
|
|
context.AddLeafNode(ParseNodeKind::EmptyDeclaration, semi ? *semi : cursor,
|
|
/*has_error=*/true);
|
|
}
|
|
|
|
auto ParserHandleDeclarationScopeLoop(ParserContext& context) -> void {
|
|
// This maintains the current state unless we're at the end of the scope.
|
|
|
|
switch (context.PositionKind()) {
|
|
case TokenKind::CloseCurlyBrace:
|
|
case TokenKind::EndOfFile: {
|
|
// This is the end of the scope, so the loop state ends.
|
|
context.PopAndDiscardState();
|
|
break;
|
|
}
|
|
case TokenKind::Class: {
|
|
context.PushState(ParserState::TypeIntroducerAsClass);
|
|
break;
|
|
}
|
|
case TokenKind::Constraint: {
|
|
context.PushState(ParserState::TypeIntroducerAsNamedConstraint);
|
|
break;
|
|
}
|
|
case TokenKind::Fn: {
|
|
context.PushState(ParserState::FunctionIntroducer);
|
|
break;
|
|
}
|
|
case TokenKind::Interface: {
|
|
context.PushState(ParserState::TypeIntroducerAsInterface);
|
|
break;
|
|
}
|
|
case TokenKind::Namespace: {
|
|
context.PushState(ParserState::Namespace);
|
|
break;
|
|
}
|
|
case TokenKind::Semi: {
|
|
context.AddLeafNode(ParseNodeKind::EmptyDeclaration, context.Consume());
|
|
break;
|
|
}
|
|
case TokenKind::Var: {
|
|
context.PushState(ParserState::VarAsSemicolon);
|
|
break;
|
|
}
|
|
default: {
|
|
ParserHandleUnrecognizedDeclaration(context);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|