literal.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/check/literal.h"
  5. #include "toolchain/check/call.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/name_lookup.h"
  9. #include "toolchain/check/type.h"
  10. #include "toolchain/check/type_completion.h"
  11. #include "toolchain/diagnostics/diagnostic.h"
  12. #include "toolchain/lex/token_info.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. namespace Carbon::Check {
  15. auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
  16. -> SemIR::InstId {
  17. return AddInst<SemIR::IntValue>(
  18. context, node_id,
  19. {.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
  20. .int_id = int_id});
  21. }
  22. auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
  23. SemIR::IntKind int_kind, IntId size_id)
  24. -> SemIR::InstId {
  25. auto width_id = MakeIntLiteral(context, node_id, size_id);
  26. auto fn_inst_id = LookupNameInCore(
  27. context, node_id, int_kind == SemIR::IntKind::Signed ? "Int" : "UInt");
  28. return PerformCall(context, node_id, fn_inst_id, {width_id});
  29. }
  30. auto MakeIntType(Context& context, Parse::NodeId node_id,
  31. SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId {
  32. auto type_inst_id = MakeIntTypeLiteral(context, node_id, int_kind, size_id);
  33. return ExprAsType(context, node_id, type_inst_id).type_id;
  34. }
  35. auto MakeFloatTypeLiteral(Context& context, Parse::NodeId node_id,
  36. IntId size_id) -> SemIR::InstId {
  37. auto width_id = MakeIntLiteral(context, node_id, size_id);
  38. auto fn_inst_id = LookupNameInCore(context, node_id, "Float");
  39. return PerformCall(context, node_id, fn_inst_id, {width_id});
  40. }
  41. namespace {
  42. // The extracted representation of the type `Core.String`.
  43. struct StringRepr {
  44. SemIR::TypeId ptr_field_type_id;
  45. SemIR::TypeId size_field_type_id;
  46. SemIR::TypeStore::IntTypeInfo size_field_type_info;
  47. };
  48. } // namespace
  49. // Extracts information about the representation of the `Core.String` type
  50. // necessary for building a string literal.
  51. static auto GetStringLiteralRepr(Context& context, SemIR::LocId loc_id,
  52. SemIR::TypeId type_id)
  53. -> std::optional<StringRepr> {
  54. // The object representation should be a struct type.
  55. auto object_repr_id = context.types().GetObjectRepr(type_id);
  56. auto struct_repr =
  57. context.types().TryGetAs<SemIR::StructType>(object_repr_id);
  58. if (!struct_repr) {
  59. return std::nullopt;
  60. }
  61. // The struct should have two fields.
  62. auto fields = context.struct_type_fields().Get(struct_repr->fields_id);
  63. if (fields.size() != 2) {
  64. return std::nullopt;
  65. }
  66. // The first field should be a pointer to 8-bit integers.
  67. auto ptr_type =
  68. context.insts().TryGetAs<SemIR::PointerType>(fields[0].type_inst_id);
  69. if (!ptr_type) {
  70. return std::nullopt;
  71. }
  72. auto pointee_type_id =
  73. context.types().GetTypeIdForTypeInstId(ptr_type->pointee_id);
  74. if (!TryToCompleteType(context, pointee_type_id, loc_id)) {
  75. return std::nullopt;
  76. }
  77. auto elem_type_info = context.types().TryGetIntTypeInfo(pointee_type_id);
  78. if (!elem_type_info || context.ints().Get(elem_type_info->bit_width) != 8) {
  79. return std::nullopt;
  80. }
  81. // The second field should be an integer type.
  82. auto size_field_type_id =
  83. context.types().GetTypeIdForTypeInstId(fields[1].type_inst_id);
  84. auto size_type_info = context.types().TryGetIntTypeInfo(size_field_type_id);
  85. if (!size_type_info) {
  86. return std::nullopt;
  87. }
  88. return StringRepr{.ptr_field_type_id = context.types().GetTypeIdForTypeInstId(
  89. fields[0].type_inst_id),
  90. .size_field_type_id = size_field_type_id,
  91. .size_field_type_info = *size_type_info};
  92. }
  93. auto MakeStringLiteral(Context& context, Parse::StringLiteralId node_id,
  94. StringLiteralValueId value_id) -> SemIR::InstId {
  95. auto str_type_id = MakeStringType(context, node_id);
  96. if (!RequireCompleteType(context, str_type_id, node_id, [&] {
  97. CARBON_DIAGNOSTIC(StringLiteralTypeIncomplete, Error,
  98. "type {0} is incomplete", SemIR::TypeId);
  99. return context.emitter().Build(node_id, StringLiteralTypeIncomplete,
  100. str_type_id);
  101. })) {
  102. return SemIR::ErrorInst::InstId;
  103. }
  104. auto repr = GetStringLiteralRepr(context, node_id, str_type_id);
  105. if (!repr) {
  106. if (str_type_id != SemIR::ErrorInst::TypeId) {
  107. CARBON_DIAGNOSTIC(StringLiteralTypeUnexpected, Error,
  108. "unexpected representation for type {0}",
  109. SemIR::TypeId);
  110. context.emitter().Emit(node_id, StringLiteralTypeUnexpected, str_type_id);
  111. }
  112. return SemIR::ErrorInst::InstId;
  113. }
  114. // The pointer field is a `StringLiteral` object.
  115. // TODO: Perhaps `StringLiteral` should instead produce a durable reference,
  116. // and we should take its address here?
  117. auto ptr_value_id = AddInst<SemIR::StringLiteral>(
  118. context, node_id,
  119. {.type_id = repr->ptr_field_type_id, .string_literal_id = value_id});
  120. // The size field is an integer literal.
  121. auto size = context.string_literal_values().Get(value_id).size();
  122. if (repr->size_field_type_info.bit_width.has_value()) {
  123. // Check that the size value fits in the size field.
  124. auto width = context.ints()
  125. .Get(repr->size_field_type_info.bit_width)
  126. .getLimitedValue();
  127. if (repr->size_field_type_info.is_signed ? !llvm::isIntN(width, size)
  128. : !llvm::isUIntN(width, size)) {
  129. CARBON_DIAGNOSTIC(StringLiteralTooLong, Error,
  130. "string literal is too long");
  131. context.emitter().Emit(node_id, StringLiteralTooLong);
  132. return SemIR::ErrorInst::InstId;
  133. }
  134. }
  135. auto size_value_id =
  136. AddInst<SemIR::IntValue>(context, node_id,
  137. {.type_id = repr->size_field_type_id,
  138. .int_id = context.ints().Add(size)});
  139. // Build the representation struct.
  140. auto elements_id = context.inst_blocks().Add({ptr_value_id, size_value_id});
  141. return AddInst<SemIR::StructValue>(
  142. context, node_id, {.type_id = str_type_id, .elements_id = elements_id});
  143. }
  144. auto MakeStringTypeLiteral(Context& context, Parse::NodeId node_id)
  145. -> SemIR::InstId {
  146. return LookupNameInCore(context, node_id, "String");
  147. }
  148. auto MakeStringType(Context& context, Parse::NodeId node_id) -> SemIR::TypeId {
  149. auto type_inst_id = MakeStringTypeLiteral(context, node_id);
  150. return ExprAsType(context, node_id, type_inst_id).type_id;
  151. }
  152. } // namespace Carbon::Check