handle_inline_decl.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "common/check.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/cpp/generate_ast.h"
  7. #include "toolchain/check/diagnostic_helpers.h"
  8. #include "toolchain/check/handle.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/lex/token_kind.h"
  11. #include "toolchain/parse/node_ids.h"
  12. #include "toolchain/parse/typed_nodes.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. auto HandleParseNode(Context& /*context*/,
  16. Parse::InlineIntroducerId /*node_id*/) -> bool {
  17. return true;
  18. }
  19. auto HandleParseNode(Context& context, Parse::InlineCppDeclId node_id) -> bool {
  20. auto body_id = context.node_stack()
  21. .PopForSoloNodeId<Parse::NodeKind::InlineImportBody>();
  22. // If there are no Cpp imports prior to this point, the `Cpp` expression after
  23. // `import` will have already produced an error.
  24. //
  25. // TODO: It'd be nice to produce a clearer error saying to insert an `import
  26. // Cpp` in a file that uses `inline Cpp` and doesn't otherwise import anything
  27. // from package `Cpp`.
  28. if (context.constant_values().Get(
  29. context.node_stack().Pop<Parse::NodeKind::CppNameExpr>()) ==
  30. SemIR::ErrorInst::ConstantId) {
  31. return true;
  32. }
  33. CARBON_CHECK(context.cpp_context(), "Have `Cpp` name but no Cpp context");
  34. auto string_token = context.parse_tree().node_token(body_id);
  35. auto string_value_id = context.tokens().GetStringLiteralValue(string_token);
  36. auto string_value = context.string_literal_values().Get(string_value_id);
  37. if (context.scope_stack().PeekIndex() != ScopeIndex::Package) {
  38. CARBON_DIAGNOSTIC(InlineDeclNotAtFileScope, Error,
  39. "`inline Cpp` declaration not at file scope");
  40. context.emitter().Emit(node_id, InlineDeclNotAtFileScope);
  41. return true;
  42. }
  43. InjectAstFromInlineCode(context, SemIR::LocId(body_id), string_value);
  44. AddInst<SemIR::InlineCppDecl>(context, node_id, {.text_id = string_value_id});
  45. return true;
  46. }
  47. } // namespace Carbon::Check