mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Adds `BindAlias` with a hybrid of `BindName` and `NameRef` semantics. I think it's slightly closer to `BindName` because it introduces a name, so I'm going more in that direction. This also matches the need for `bind_name_id` with imports on enclosing scopes. Note, only things that look like a name reference are being allowed on the RHS of `alias`. This includes builtins that look like name references, such as `bool`, but not ones that turn into values underneath, such as `false`.
62 lines
2.0 KiB
C++
62 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/lex/token_kind.h"
|
|
#include "toolchain/lex/tokenized_buffer.h"
|
|
#include "toolchain/parse/context.h"
|
|
#include "toolchain/parse/node_kind.h"
|
|
#include "toolchain/parse/state.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
auto HandleAlias(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
context.PushState(state, State::AliasAfterName);
|
|
context.PushState(State::DeclNameAndParamsAsNone, state.token);
|
|
}
|
|
|
|
auto HandleAliasAfterName(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (state.has_error) {
|
|
context.RecoverFromDeclError(state, NodeKind::Alias,
|
|
/*skip_past_likely_end=*/true);
|
|
return;
|
|
}
|
|
|
|
if (auto equal = context.ConsumeIf(Lex::TokenKind::Equal)) {
|
|
context.AddLeafNode(NodeKind::AliasInitializer, *equal);
|
|
context.PushState(state, State::AliasFinish);
|
|
context.PushState(State::Expr);
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ExpectedAliasInitializer, Error,
|
|
"`alias` requires a `=` for the source.");
|
|
context.emitter().Emit(*context.position(), ExpectedAliasInitializer);
|
|
context.RecoverFromDeclError(state, NodeKind::Alias,
|
|
/*skip_past_likely_end=*/true);
|
|
}
|
|
}
|
|
|
|
auto HandleAliasFinish(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (state.has_error) {
|
|
context.RecoverFromDeclError(state, NodeKind::Alias,
|
|
/*skip_past_likely_end=*/true);
|
|
return;
|
|
}
|
|
|
|
if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
|
|
context.AddNode(NodeKind::Alias, *semi, state.subtree_start,
|
|
state.has_error);
|
|
} else {
|
|
context.EmitExpectedDeclSemi(Lex::TokenKind::Alias);
|
|
context.RecoverFromDeclError(state, NodeKind::Alias,
|
|
/*skip_past_likely_end=*/true);
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|