cpp_global_var.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_SEM_IR_CPP_GLOBAL_VAR_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_CPP_GLOBAL_VAR_H_
  6. #include "common/hashing.h"
  7. #include "common/ostream.h"
  8. #include "toolchain/sem_ir/clang_decl.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::SemIR {
  11. // A key describing a C++ global variable imported into Carbon, identified by
  12. // its entity name.
  13. struct CppGlobalVarKey : public Printable<CppGlobalVarKey> {
  14. auto Print(llvm::raw_ostream& out) const -> void {
  15. out << "{entity_name_id: " << entity_name_id << "}";
  16. }
  17. // TODO: Use default when `Printable` supports it.
  18. friend auto operator==(const CppGlobalVarKey& lhs, const CppGlobalVarKey& rhs)
  19. -> bool {
  20. return lhs.entity_name_id == rhs.entity_name_id;
  21. }
  22. // The name of the variable.
  23. EntityNameId entity_name_id;
  24. };
  25. // A C++ global variable imported into Carbon. This is used to map the entity
  26. // name to the Clang declaration so we can use Clang mangling.
  27. struct CppGlobalVar : public Printable<CppGlobalVar> {
  28. auto Print(llvm::raw_ostream& out) const -> void {
  29. out << "{key: " << key << ", clang_decl_id: " << clang_decl_id << "}";
  30. }
  31. // The key by which this variable can be looked up.
  32. CppGlobalVarKey key;
  33. // The Clang declaration for this variable, if any.
  34. // This is ignored for equality and hashing, since it's always unique for a
  35. // given key, in order to store it in `CanonicalValueStore` and allow lookup
  36. // by `CppGlobalVarKey`.
  37. ClangDeclId clang_decl_id;
  38. auto GetAsKey() const -> CppGlobalVarKey { return key; }
  39. };
  40. // Use the name of a C++ global variable when doing `Lookup` to find an ID.
  41. using CppGlobalVarStore = CanonicalValueStore<CppGlobalVarId, CppGlobalVarKey,
  42. Tag<CheckIRId>, CppGlobalVar>;
  43. } // namespace Carbon::SemIR
  44. #endif // CARBON_TOOLCHAIN_SEM_IR_CPP_GLOBAL_VAR_H_