type.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/sem_ir/type.h"
  5. #include "toolchain/sem_ir/file.h"
  6. namespace Carbon::SemIR {
  7. auto TypeStore::GetInstId(TypeId type_id) const -> InstId {
  8. return file_->constant_values().GetInstId(GetConstantId(type_id));
  9. }
  10. auto TypeStore::GetAsInst(TypeId type_id) const -> Inst {
  11. return file_->insts().Get(GetInstId(type_id));
  12. }
  13. auto TypeStore::GetObjectRepr(TypeId type_id) const -> TypeId {
  14. type_id = GetUnqualifiedType(type_id);
  15. auto class_type = TryGetAs<ClassType>(type_id);
  16. if (!class_type) {
  17. return type_id;
  18. }
  19. const auto& class_info = file_->classes().Get(class_type->class_id);
  20. if (!class_info.is_defined()) {
  21. return TypeId::Invalid;
  22. }
  23. return class_info.GetObjectRepr(*file_, class_type->specific_id);
  24. }
  25. auto TypeStore::GetUnqualifiedType(TypeId type_id) const -> TypeId {
  26. if (auto const_type = TryGetAs<ConstType>(type_id)) {
  27. return const_type->inner_id;
  28. }
  29. return type_id;
  30. }
  31. auto TypeStore::IsSignedInt(TypeId int_type_id) const -> bool {
  32. auto object_repr_id = GetObjectRepr(int_type_id);
  33. if (!object_repr_id.is_valid()) {
  34. return false;
  35. }
  36. auto inst_id = GetInstId(int_type_id);
  37. if (inst_id == InstId::BuiltinIntLiteralType) {
  38. return true;
  39. }
  40. auto int_type = file_->insts().TryGetAs<IntType>(inst_id);
  41. return int_type && int_type->int_kind.is_signed();
  42. }
  43. auto TypeStore::GetIntTypeInfo(TypeId int_type_id) const -> IntTypeInfo {
  44. auto object_repr_id = GetObjectRepr(int_type_id);
  45. auto inst_id = GetInstId(object_repr_id);
  46. if (inst_id == InstId::BuiltinIntLiteralType) {
  47. return {.is_signed = true, .bit_width = IntId::Invalid};
  48. }
  49. auto int_type = file_->insts().GetAs<IntType>(inst_id);
  50. auto bit_width_inst =
  51. file_->insts().TryGetAs<IntValue>(int_type.bit_width_id);
  52. return {
  53. .is_signed = int_type.int_kind.is_signed(),
  54. .bit_width = bit_width_inst ? bit_width_inst->int_id : IntId::Invalid};
  55. }
  56. } // namespace Carbon::SemIR