handle_inline_decl.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lex/token_kind.h"
  5. #include "toolchain/parse/context.h"
  6. #include "toolchain/parse/handle.h"
  7. #include "toolchain/parse/node_kind.h"
  8. #include "toolchain/parse/state.h"
  9. namespace Carbon::Parse {
  10. auto HandleInlineDeclAfterIntroducer(Context& context) -> void {
  11. auto state = context.PopState();
  12. if (auto cpp_token = context.ConsumeIf(Lex::TokenKind::Cpp)) {
  13. context.AddLeafNode(NodeKind::CppNameExpr, *cpp_token);
  14. } else {
  15. CARBON_DIAGNOSTIC(ExpectedCppAfterInline, Error,
  16. "expected `Cpp` after `inline`");
  17. context.emitter().Emit(*context.position(), ExpectedCppAfterInline);
  18. context.AddNode(NodeKind::InlineCppDecl,
  19. context.SkipPastLikelyEnd(state.token),
  20. /*has_error=*/true);
  21. return;
  22. }
  23. if (auto str = context.ConsumeIf(Lex::TokenKind::StringLiteral)) {
  24. context.AddLeafNode(NodeKind::InlineImportBody, *str);
  25. context.AddNodeExpectingDeclSemi(state, NodeKind::InlineCppDecl,
  26. Lex::TokenKind::Inline,
  27. /*is_def_allowed=*/false);
  28. } else {
  29. CARBON_DIAGNOSTIC(ExpectedStringAfterInlineCpp, Error,
  30. "expected string literal after `inline Cpp`");
  31. context.emitter().Emit(*context.position(), ExpectedStringAfterInlineCpp);
  32. context.AddNode(NodeKind::InlineCppDecl,
  33. context.SkipPastLikelyEnd(state.token),
  34. /*has_error=*/true);
  35. }
  36. }
  37. } // namespace Carbon::Parse