literal.cpp 7.3 KB

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