literal.cpp 7.0 KB

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