mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Add support for inline Cpp declarations. (#6994)
For #6830, add support for inline C++ fragments as a declaration rather than as a packaging directive. For now, this uses `inline Cpp <string-literal>;` as syntax. The prior `import Cpp inline <string-literal>;` is left alone for the time being. We can decide separately whether to remove that. `inline Cpp` requires that there was at least one `import Cpp`. It's not clear to me if that's the right design long-term, but it seems reasonable for now. Assisted-by: Gemini via Google Antigravity
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// 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/parse/context.h"
|
||||
#include "toolchain/parse/handle.h"
|
||||
#include "toolchain/parse/node_kind.h"
|
||||
#include "toolchain/parse/state.h"
|
||||
|
||||
namespace Carbon::Parse {
|
||||
|
||||
auto HandleInlineDeclAfterIntroducer(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
if (auto cpp_token = context.ConsumeIf(Lex::TokenKind::Cpp)) {
|
||||
context.AddLeafNode(NodeKind::CppNameExpr, *cpp_token);
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(ExpectedCppAfterInline, Error,
|
||||
"expected `Cpp` after `inline`");
|
||||
context.emitter().Emit(*context.position(), ExpectedCppAfterInline);
|
||||
context.AddNode(NodeKind::InlineCppDecl,
|
||||
context.SkipPastLikelyEnd(state.token),
|
||||
/*has_error=*/true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto str = context.ConsumeIf(Lex::TokenKind::StringLiteral)) {
|
||||
context.AddLeafNode(NodeKind::InlineImportBody, *str);
|
||||
context.AddNodeExpectingDeclSemi(state, NodeKind::InlineCppDecl,
|
||||
Lex::TokenKind::Inline,
|
||||
/*is_def_allowed=*/false);
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(ExpectedStringAfterInlineCpp, Error,
|
||||
"expected string literal after `inline Cpp`");
|
||||
context.emitter().Emit(*context.position(), ExpectedStringAfterInlineCpp);
|
||||
context.AddNode(NodeKind::InlineCppDecl,
|
||||
context.SkipPastLikelyEnd(state.token),
|
||||
/*has_error=*/true);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon::Parse
|
||||
Reference in New Issue
Block a user