Skip to content

Change builtins to return a string literal #8636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,7 @@ test "@typeInfo" {
try expect(mem.eql(u8, @typeInfo(Small).Enum.fields[1].name, "two"));
}

// @tagName gives a []const u8 representation of an enum value:
// @tagName gives a [:0]const u8 representation of an enum value:
test "@tagName" {
try expect(mem.eql(u8, @tagName(Small.three), "three"));
}
Expand Down Expand Up @@ -3233,7 +3233,7 @@ test "union method" {
{#code_end#}
<p>
{#link|@tagName#} can be used to return a {#link|comptime#}
{#syntax#}[]const u8{#endsyntax#} value representing the field name:
{#syntax#}[:0]const u8{#endsyntax#} value representing the field name:
</p>
{#code_begin|test#}
const std = @import("std");
Expand Down Expand Up @@ -7447,7 +7447,7 @@ test "main" {
{#see_also|@divFloor|@divExact#}
{#header_close#}
{#header_open|@embedFile#}
<pre>{#syntax#}@embedFile(comptime path: []const u8) *const [X:0]u8{#endsyntax#}</pre>
<pre>{#syntax#}@embedFile(comptime path: []const u8) *const [N:0]u8{#endsyntax#}</pre>
<p>
This function returns a compile time constant pointer to null-terminated,
fixed-size array with length equal to the byte count of the file given by
Expand Down Expand Up @@ -7475,7 +7475,7 @@ test "main" {
{#header_close#}

{#header_open|@errorName#}
<pre>{#syntax#}@errorName(err: anyerror) []const u8{#endsyntax#}</pre>
<pre>{#syntax#}@errorName(err: anyerror) [:0]const u8{#endsyntax#}</pre>
<p>
This function returns the string representation of an error. The string representation
of {#syntax#}error.OutOfMem{#endsyntax#} is {#syntax#}"OutOfMem"{#endsyntax#}.
Expand Down Expand Up @@ -8494,9 +8494,9 @@ fn doTheTest() !void {
{#header_close#}

{#header_open|@tagName#}
<pre>{#syntax#}@tagName(value: anytype) []const u8{#endsyntax#}</pre>
<pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
<p>
Converts an enum value or union value to a slice of bytes representing the name.</p><p>If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}.
Converts an enum value or union value to a string literal representing the name.</p><p>If the enum is non-exhaustive and the tag value does not map to a name, it invokes safety-checked {#link|Undefined Behavior#}.
</p>
{#header_close#}

Expand Down Expand Up @@ -8623,7 +8623,7 @@ test "integer truncation" {
{#header_close#}

{#header_open|@typeName#}
<pre>{#syntax#}@typeName(T: type) [N]u8{#endsyntax#}</pre>
<pre>{#syntax#}@typeName(T: type) *const [N:0]u8{#endsyntax#}</pre>
<p>
This function returns the string representation of a type, as
an array. It is equivalent to a string literal of the type name.
Expand Down
15 changes: 10 additions & 5 deletions src/stage1/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6291,6 +6291,11 @@ ZigValue *create_const_str_lit(CodeGen *g, Buf *str) {
return const_val;
}

ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel) {
ZigValue *array_val = create_const_str_lit(g, str)->data.x_ptr.data.ref.pointee;
return create_const_slice(g, array_val, 0, buf_len(str), true, sentinel);
}

void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint) {
const_val->special = ConstValSpecialStatic;
const_val->type = type;
Expand Down Expand Up @@ -6444,12 +6449,12 @@ ZigValue *create_const_type(CodeGen *g, ZigType *type_value) {
}

void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
size_t start, size_t len, bool is_const)
size_t start, size_t len, bool is_const, ZigValue *sentinel)
{
assert(array_val->type->id == ZigTypeIdArray);

ZigType *ptr_type = get_pointer_to_type_extra(g, array_val->type->data.array.child_type,
is_const, false, PtrLenUnknown, 0, 0, 0, false);
ZigType *ptr_type = get_pointer_to_type_extra2(g, array_val->type->data.array.child_type,
is_const, false, PtrLenUnknown, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel);

const_val->special = ConstValSpecialStatic;
const_val->type = get_slice_type(g, ptr_type);
Expand All @@ -6460,9 +6465,9 @@ void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
init_const_usize(g, const_val->data.x_struct.fields[slice_len_index], len);
}

ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const) {
ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel) {
ZigValue *const_val = g->pass1_arena->create<ZigValue>();
init_const_slice(g, const_val, array_val, start, len, is_const);
init_const_slice(g, const_val, array_val, start, len, is_const, sentinel);
return const_val;
}

Expand Down
5 changes: 3 additions & 2 deletions src/stage1/analyze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent);

void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str);
ZigValue *create_const_str_lit(CodeGen *g, Buf *str);
ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel);

void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint);
ZigValue *create_const_bigint(CodeGen *g, ZigType *type, const BigInt *bigint);
Expand Down Expand Up @@ -186,8 +187,8 @@ ZigValue *create_const_ptr_array(CodeGen *g, ZigValue *array_val, size_t elem_in
bool is_const, PtrLen ptr_len);

void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val,
size_t start, size_t len, bool is_const);
ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const);
size_t start, size_t len, bool is_const, ZigValue *sentinel);
ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel);

void init_const_null(ZigValue *const_val, ZigType *type);
ZigValue *create_const_null(CodeGen *g, ZigType *type);
Expand Down
6 changes: 3 additions & 3 deletions src/stage1/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {

Buf *buf_msg = panic_msg_buf(msg_id);
ZigValue *array_val = create_const_str_lit(g, buf_msg)->data.x_ptr.data.ref.pointee;
init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true);
init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true, nullptr);

render_const_val(g, val, "");
render_const_val_global(g, val, "");
Expand Down Expand Up @@ -9424,7 +9424,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {

ZigValue *name_field = this_val->data.x_struct.fields[0];
ZigValue *name_array_val = create_const_str_lit(g, &test_fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true);
init_const_slice(g, name_field, name_array_val, 0, buf_len(&test_fn_entry->symbol_name), true, nullptr);

ZigValue *fn_field = this_val->data.x_struct.fields[1];
fn_field->type = fn_type;
Expand All @@ -9448,7 +9448,7 @@ static void update_test_functions_builtin_decl(CodeGen *g) {
}
report_errors_and_maybe_exit(g);

ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
ZigValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true, nullptr);

update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice);
assert(g->test_runner_package != nullptr);
Expand Down
Loading