export.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef CARBON_TOOLCHAIN_CHECK_CPP_EXPORT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CPP_EXPORT_H_
  6. #include "clang/AST/Decl.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Exports a Carbon name scope into C++ as a namespace or class, or returns the
  11. // C++ namespace or class declaration that it was imported from.
  12. //
  13. // If the name scope has already been exported, returns the existing context.
  14. // Otherwise, creates a new C++ declaration context and returns it. Returns
  15. // nullptr if the name scope could not be exported and an error was diagnosed.
  16. auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id,
  17. SemIR::NameScopeId name_scope_id)
  18. -> clang::DeclContext*;
  19. // Exports a Carbon class into C++ as a class, or returns the C++ tag type that
  20. // the class was imported from.
  21. //
  22. // If the class has already been exported, returns the existing C++ class.
  23. // Otherwise, creates a new C++ class and returns it. Returns nullptr if the
  24. // class could not be exported and an error was diagnosed.
  25. auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
  26. SemIR::InstId class_inst_id, SemIR::ClassType class_type)
  27. -> clang::TagDecl*;
  28. // Export all `SemIR::FieldDecl`s in the class body as `clang::FieldDecl`s.
  29. auto ExportAllFieldsToCpp(Context& context, SemIR::Class& class_info) -> void;
  30. // Exports a Carbon class field into C++.
  31. //
  32. // If the field has already been exported, returns the existing C++
  33. // field.
  34. //
  35. // If the field has not already been exported, *all* fields of the class
  36. // are exported, and then the requested C++ field is returned.
  37. //
  38. // Returns nullptr if the class could not be exported and an error was
  39. // diagnosed.
  40. auto ExportFieldToCpp(Context& context, SemIR::InstId field_inst_id,
  41. SemIR::FieldDecl field_decl) -> clang::FieldDecl*;
  42. // Get the field offset for each field in a class.
  43. //
  44. // Returns true on success, false if any error occurs.
  45. auto CalculateCppFieldOffsets(
  46. Context& context, SemIR::ClassId class_id,
  47. llvm::DenseMap<const clang::FieldDecl*, uint64_t>& field_offsets) -> bool;
  48. // Get a `clang::FunctionDecl` that can be used to call a Carbon function.
  49. auto ExportFunctionToCpp(Context& context, SemIR::LocId loc_id,
  50. SemIR::FunctionId function_id) -> clang::FunctionDecl*;
  51. } // namespace Carbon::Check
  52. #endif // CARBON_TOOLCHAIN_CHECK_CPP_EXPORT_H_