literal.cpp 6.9 KB

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