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