literal.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/parse/node_ids.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. namespace Carbon::Check {
  17. // Adds a TypeLiteral instruction to represent a syntactic type literal.
  18. static auto MakeTypeLiteral(Context& context, SemIR::LocId loc_id,
  19. SemIR::InstId value_id) -> SemIR::TypeInstId {
  20. auto type_inst_id = ExprAsType(context, loc_id, value_id).inst_id;
  21. return AddTypeInst<SemIR::TypeLiteral>(
  22. context, loc_id,
  23. {.type_id = SemIR::TypeType::TypeId, .value_id = type_inst_id});
  24. }
  25. auto MakeTypeTypeLiteral(Context& context, Parse::NodeId node_id)
  26. -> SemIR::TypeInstId {
  27. return MakeTypeLiteral(context, node_id, SemIR::TypeType::TypeInstId);
  28. }
  29. auto MakeBoolTypeLiteral(Context& context, Parse::NodeId node_id)
  30. -> SemIR::TypeInstId {
  31. auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
  32. auto inst_id =
  33. LookupNameInCore(context, desugared_loc_id, CoreIdentifier::Bool);
  34. inst_id = PerformCall(context, desugared_loc_id, inst_id, {});
  35. return MakeTypeLiteral(context, node_id, inst_id);
  36. }
  37. auto MakeBoolLiteral(Context& context, SemIR::LocId loc_id,
  38. SemIR::BoolValue value) -> SemIR::InstId {
  39. return AddInst<SemIR::BoolLiteral>(
  40. context, loc_id,
  41. {.type_id = GetSingletonType(context, SemIR::BoolType::TypeInstId),
  42. .value = value});
  43. }
  44. auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
  45. -> SemIR::InstId {
  46. return AddInst<SemIR::IntValue>(
  47. context, node_id,
  48. {.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
  49. .int_id = int_id});
  50. }
  51. // Returns an instruction with the given constant integer value.
  52. static auto GetOrAddIntValue(Context& context, SemIR::LocId loc_id,
  53. IntId int_id) -> SemIR::InstId {
  54. return GetOrAddInst<SemIR::IntValue>(
  55. context, loc_id,
  56. {.type_id = GetSingletonType(context, SemIR::IntLiteralType::TypeInstId),
  57. .int_id = int_id});
  58. }
  59. auto MakeCharTypeLiteral(Context& context, Parse::NodeId node_id)
  60. -> SemIR::TypeInstId {
  61. auto inst_id = LookupNameInCore(context, node_id, CoreIdentifier::Char);
  62. return MakeTypeLiteral(context, node_id, inst_id);
  63. }
  64. // Returns an instruction representing the type `iN` or `uN`.
  65. static auto GetOrAddIntTypeInst(Context& context, SemIR::LocId loc_id,
  66. SemIR::IntKind int_kind, IntId size_id)
  67. -> SemIR::InstId {
  68. auto width_id = GetOrAddIntValue(context, loc_id, size_id);
  69. auto fn_inst_id = LookupNameInCore(context, loc_id,
  70. int_kind == SemIR::IntKind::Signed
  71. ? CoreIdentifier::Int
  72. : CoreIdentifier::UInt);
  73. return PerformCall(context, loc_id, fn_inst_id, {width_id});
  74. }
  75. auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
  76. SemIR::IntKind int_kind, IntId size_id)
  77. -> SemIR::TypeInstId {
  78. auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
  79. auto type_inst_id =
  80. GetOrAddIntTypeInst(context, desugared_loc_id, int_kind, size_id);
  81. return MakeTypeLiteral(context, node_id, type_inst_id);
  82. }
  83. auto MakeIntType(Context& context, Parse::NodeId node_id,
  84. SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId {
  85. auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
  86. auto type_inst_id =
  87. GetOrAddIntTypeInst(context, desugared_loc_id, int_kind, size_id);
  88. return ExprAsType(context, node_id, type_inst_id).type_id;
  89. }
  90. auto MakeFloatTypeLiteral(Context& context, Parse::NodeId node_id,
  91. IntId size_id) -> SemIR::TypeInstId {
  92. auto desugared_loc_id = SemIR::LocId(node_id).AsDesugared();
  93. auto width_id = GetOrAddIntValue(context, desugared_loc_id, size_id);
  94. auto fn_inst_id =
  95. LookupNameInCore(context, desugared_loc_id, CoreIdentifier::Float);
  96. auto call_id = PerformCall(context, desugared_loc_id, fn_inst_id, {width_id});
  97. return MakeTypeLiteral(context, node_id, call_id);
  98. }
  99. namespace {
  100. // The extracted representation of the type `Core.String`.
  101. struct StringRepr {
  102. SemIR::TypeId ptr_field_type_id;
  103. SemIR::TypeId size_field_type_id;
  104. SemIR::TypeStore::IntTypeInfo size_field_type_info;
  105. };
  106. } // namespace
  107. // Extracts information about the representation of the `Core.String` type
  108. // necessary for building a string literal.
  109. static auto GetStringLiteralRepr(Context& context, SemIR::LocId loc_id,
  110. SemIR::TypeId type_id)
  111. -> std::optional<StringRepr> {
  112. // The object representation should be a struct type.
  113. auto object_repr_id = context.types().GetObjectRepr(type_id);
  114. auto struct_repr =
  115. context.types().TryGetAs<SemIR::StructType>(object_repr_id);
  116. if (!struct_repr) {
  117. return std::nullopt;
  118. }
  119. // The struct should have two fields.
  120. auto fields = context.struct_type_fields().Get(struct_repr->fields_id);
  121. if (fields.size() != 2) {
  122. return std::nullopt;
  123. }
  124. // The first field should be a pointer to 8-bit integers.
  125. auto ptr_type =
  126. context.insts().TryGetAs<SemIR::PointerType>(fields[0].type_inst_id);
  127. if (!ptr_type) {
  128. return std::nullopt;
  129. }
  130. auto pointee_type_id =
  131. context.types().GetTypeIdForTypeInstId(ptr_type->pointee_id);
  132. if (!TryToCompleteType(context, pointee_type_id, loc_id)) {
  133. return std::nullopt;
  134. }
  135. auto elem_type_info = context.types().TryGetIntTypeInfo(pointee_type_id);
  136. if (!elem_type_info || context.ints().Get(elem_type_info->bit_width) != 8) {
  137. return std::nullopt;
  138. }
  139. // The second field should be an integer type.
  140. auto size_field_type_id =
  141. context.types().GetTypeIdForTypeInstId(fields[1].type_inst_id);
  142. auto size_type_info = context.types().TryGetIntTypeInfo(size_field_type_id);
  143. if (!size_type_info) {
  144. return std::nullopt;
  145. }
  146. return StringRepr{.ptr_field_type_id = context.types().GetTypeIdForTypeInstId(
  147. fields[0].type_inst_id),
  148. .size_field_type_id = size_field_type_id,
  149. .size_field_type_info = *size_type_info};
  150. }
  151. auto MakeStringLiteral(Context& context, Parse::StringLiteralId node_id,
  152. StringLiteralValueId value_id) -> SemIR::InstId {
  153. auto str_type = MakeStringType(context, SemIR::LocId(node_id).AsDesugared());
  154. if (!RequireCompleteType(
  155. context, str_type.type_id, node_id, [&](auto& builder) {
  156. CARBON_DIAGNOSTIC(StringLiteralTypeIncomplete, Context,
  157. "type {0} is incomplete", InstIdAsType);
  158. builder.Context(node_id, StringLiteralTypeIncomplete,
  159. str_type.inst_id);
  160. })) {
  161. return SemIR::ErrorInst::InstId;
  162. }
  163. auto repr = GetStringLiteralRepr(context, node_id, str_type.type_id);
  164. if (!repr) {
  165. if (str_type.type_id != SemIR::ErrorInst::TypeId) {
  166. CARBON_DIAGNOSTIC(StringLiteralTypeUnexpected, Error,
  167. "unexpected representation for type {0}", InstIdAsType);
  168. context.emitter().Emit(node_id, StringLiteralTypeUnexpected,
  169. str_type.inst_id);
  170. }
  171. return SemIR::ErrorInst::InstId;
  172. }
  173. // The pointer field is a `StringLiteral` object.
  174. // TODO: Perhaps `StringLiteral` should instead produce a durable reference,
  175. // and we should take its address here?
  176. auto ptr_value_id = AddInst<SemIR::StringLiteral>(
  177. context, node_id,
  178. {.type_id = repr->ptr_field_type_id, .string_literal_id = value_id});
  179. // The size field is an integer literal.
  180. auto size = context.string_literal_values().Get(value_id).size();
  181. if (repr->size_field_type_info.bit_width.has_value()) {
  182. // Check that the size value fits in the size field.
  183. auto width = context.ints()
  184. .Get(repr->size_field_type_info.bit_width)
  185. .getLimitedValue();
  186. if (repr->size_field_type_info.is_signed ? !llvm::isIntN(width, size)
  187. : !llvm::isUIntN(width, size)) {
  188. CARBON_DIAGNOSTIC(StringLiteralTooLong, Error,
  189. "string literal is too long");
  190. context.emitter().Emit(node_id, StringLiteralTooLong);
  191. return SemIR::ErrorInst::InstId;
  192. }
  193. }
  194. auto size_value_id =
  195. AddInst<SemIR::IntValue>(context, node_id,
  196. {.type_id = repr->size_field_type_id,
  197. .int_id = context.ints().Add(size)});
  198. // Build the representation struct.
  199. auto elements_id = context.inst_blocks().Add({ptr_value_id, size_value_id});
  200. return AddInst<SemIR::StructValue>(
  201. context, node_id,
  202. {.type_id = str_type.type_id, .elements_id = elements_id});
  203. }
  204. // Returns an instruction with the value `str`.
  205. static auto GetOrAddStringTypeInst(Context& context, SemIR::LocId loc_id)
  206. -> SemIR::InstId {
  207. return LookupNameInCore(context, loc_id, CoreIdentifier::String);
  208. }
  209. auto MakeStringTypeLiteral(Context& context, Parse::StringTypeLiteralId node_id)
  210. -> SemIR::TypeInstId {
  211. auto inst_id = GetOrAddStringTypeInst(context, node_id);
  212. return MakeTypeLiteral(context, node_id, inst_id);
  213. }
  214. auto MakeStringType(Context& context, SemIR::LocId loc_id) -> TypeExpr {
  215. auto type_inst_id = GetOrAddStringTypeInst(context, loc_id);
  216. return ExprAsType(context, loc_id, type_inst_id);
  217. }
  218. } // namespace Carbon::Check