Skip to content
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

Resolve type of floating point builtin functions #2199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,40 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
return ty.instanceTypeVal(analyser);
}

const float_map: std.StaticStringMap(void) = .initComptime(.{
.{"@sqrt"},
.{"@sin"},
.{"@cos"},
.{"@tan"},
.{"@exp"},
.{"@exp2"},
.{"@log"},
.{"@log2"},
.{"@log10"},
.{"@abs"},
.{"@floor"},
.{"@ceil"},
.{"@trunc"},
.{"@round"},
});
if (float_map.has(call_name)) {
if (params.len != 1) return null;
const ty = (try analyser.resolveTypeOfNodeInternal(.{
.node = params[0],
.handle = handle,
})) orelse return null;
const payload = switch (ty.data) {
.ip_index => |payload| payload,
else => return null,
};
const valid_type = switch (analyser.ip.indexToKey(payload.type)) {
.vector_type => |info| analyser.ip.isFloat(info.child),
else => analyser.ip.isFloat(payload.type),
};
if (!valid_type) return null;
return Type.fromIP(analyser, payload.type, null);
}

// Almost the same as the above, return a type value though.
// TODO Do peer type resolution, we just keep the first for now.
if (std.mem.eql(u8, call_name, "@TypeOf")) {
Expand Down
65 changes: 65 additions & 0 deletions tests/analysis/builtins.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const some_float: f32 = undefined;
const some_vector: @Vector(4, f32) = undefined;

const float_builtin_00 = @sqrt(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_01 = @sin(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_02 = @cos(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_03 = @tan(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_04 = @exp(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_05 = @exp2(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_06 = @log(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_07 = @log2(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_08 = @log10(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_09 = @abs(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_10 = @floor(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_11 = @ceil(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_12 = @trunc(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()
const float_builtin_13 = @round(some_float);
// ^^^^^^^^^^^^^^^^ (f32)()

const vector_builtin_00 = @sqrt(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_01 = @sin(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_02 = @cos(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_03 = @tan(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_04 = @exp(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_05 = @exp2(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_06 = @log(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_07 = @log2(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_08 = @log10(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_09 = @abs(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_10 = @floor(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_11 = @ceil(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_12 = @trunc(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()
const vector_builtin_13 = @round(some_vector);
// ^^^^^^^^^^^^^^^^^ (@Vector(4,f32))()

comptime {
// Use @compileLog to verify the expected type with the compiler
// @compileLog(vector_builtin_13);
}