mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 21:24:56 +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.
54 lines
2.0 KiB
C++
54 lines
2.0 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 PeriodAs variants.
|
|
// TODO: This currently only supports identifiers on the rhs, but will in the
|
|
// future need to handle things like `object.(Interface.member)` for qualifiers.
|
|
auto ParserHandlePeriod(ParserContext& context, ParseNodeKind node_kind)
|
|
-> void {
|
|
auto state = context.PopState();
|
|
|
|
// `.` identifier
|
|
auto dot = context.ConsumeChecked(TokenKind::Period);
|
|
|
|
if (!context.ConsumeAndAddLeafNodeIf(TokenKind::Identifier,
|
|
ParseNodeKind::Name)) {
|
|
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
|
|
"Expected identifier after `.`.");
|
|
context.emitter().Emit(*context.position(), ExpectedIdentifierAfterDot);
|
|
// If we see a keyword, assume it was intended to be a name.
|
|
// TODO: Should keywords be valid here?
|
|
if (context.PositionKind().is_keyword()) {
|
|
context.AddLeafNode(ParseNodeKind::Name, context.Consume(),
|
|
/*has_error=*/true);
|
|
} else {
|
|
context.AddLeafNode(ParseNodeKind::Name, *context.position(),
|
|
/*has_error=*/true);
|
|
// Indicate the error to the parent state so that it can avoid producing
|
|
// more errors.
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
context.AddNode(node_kind, dot, state.subtree_start, state.has_error);
|
|
}
|
|
|
|
auto ParserHandlePeriodAsDeclaration(ParserContext& context) -> void {
|
|
ParserHandlePeriod(context, ParseNodeKind::QualifiedDeclaration);
|
|
}
|
|
|
|
auto ParserHandlePeriodAsExpression(ParserContext& context) -> void {
|
|
ParserHandlePeriod(context, ParseNodeKind::MemberAccessExpression);
|
|
}
|
|
|
|
auto ParserHandlePeriodAsStruct(ParserContext& context) -> void {
|
|
ParserHandlePeriod(context, ParseNodeKind::StructFieldDesignator);
|
|
}
|
|
|
|
} // namespace Carbon
|