tiny.chant.ast.types.layout
Defined in ast.types.
API (2)
Actions
Public operations.
Source
Source: lib/chant/src/ast/type/layout.zig
zig
const std = @import("std");const types = @import("types.zig");const Type = types.Type;pub fn byteSize(c_type: *const Type) ?u64 { return switch (c_type.kind) { .auto_type, .void_type, .struct_type => null, .nullptr_type => 8, .enum_type => byteSize(c_type.child orelse &types.int_type), .char_type => 1, .short_type => 2, .int_type => 4, .long_type => 8, .bitint_type => bitintByteSize(c_type.bit_width), .float_type => 4, .double_type => 8, .decimal32_type => 4, .decimal64_type => 8, .decimal128_type => 16, .pointer => 8, .array => if (c_type.array_len) |len| blk: { const child_size = byteSize(c_type.child orelse return null) orelse return null; break :blk len * child_size; } else null, .function => null, };}pub fn byteAlign(c_type: *const Type) ?u64 { return switch (c_type.kind) { .auto_type, .void_type, .struct_type, .function => null, .nullptr_type => 8, .enum_type => byteAlign(c_type.child orelse &types.int_type), .char_type => 1, .short_type => 2, .int_type => 4, .long_type => 8, .bitint_type => bitintByteAlign(c_type.bit_width), .float_type => 4, .double_type => 8, .decimal32_type => 4, .decimal64_type => 8, .decimal128_type => 16, .pointer => 8, .array => byteAlign(c_type.child orelse return null), };}fn bitintByteSize(width: u16) u64 { return (@as(u64, width) + 7) / 8;}fn bitintByteAlign(width: u16) u64 { const size = bitintByteSize(width); if (size <= 1) return 1; if (size <= 2) return 2; if (size <= 4) return 4; return 8;}test "byte sizes follow the lp64 model" { try std.testing.expectEqual(@as(u64, 4), byteSize(&types.int_type).?); try std.testing.expectEqual(@as(u64, 8), byteSize(&types.long_type).?); try std.testing.expectEqual(@as(u64, 8), byteSize(&types.double_type).?); try std.testing.expectEqual(@as(u64, 16), byteSize(&types.decimal128_type).?); const pointer = Type{ .kind = .pointer, .child = &types.double_type }; try std.testing.expectEqual(@as(u64, 8), byteSize(&pointer).?); const bitint = Type{ .kind = .bitint_type, .bit_width = 17 }; try std.testing.expectEqual(@as(u64, 3), byteSize(&bitint).?); const fixed_enum = Type{ .kind = .enum_type, .child = &types.uchar_type }; try std.testing.expectEqual(@as(u64, 1), byteSize(&fixed_enum).?); const array = Type{ .kind = .array, .child = &types.double_type, .array_len = 10 }; try std.testing.expectEqual(@as(u64, 80), byteSize(&array).?);}test "byte alignments follow the lp64 model" { try std.testing.expectEqual(@as(u64, 1), byteAlign(&types.char_type).?); try std.testing.expectEqual(@as(u64, 4), byteAlign(&types.int_type).?); try std.testing.expectEqual(@as(u64, 8), byteAlign(&types.double_type).?); try std.testing.expectEqual(@as(u64, 16), byteAlign(&types.decimal128_type).?); const pointer = Type{ .kind = .pointer, .child = &types.double_type }; try std.testing.expectEqual(@as(u64, 8), byteAlign(&pointer).?); const bitint = Type{ .kind = .bitint_type, .bit_width = 17 }; try std.testing.expectEqual(@as(u64, 4), byteAlign(&bitint).?); const fixed_enum = Type{ .kind = .enum_type, .child = &types.uchar_type }; try std.testing.expectEqual(@as(u64, 1), byteAlign(&fixed_enum).?); const array = Type{ .kind = .array, .child = &types.double_type, .array_len = 10 }; try std.testing.expectEqual(@as(u64, 8), byteAlign(&array).?);}Source: lib/chant/src/ast/type/root.zig:6
zig
pub const layout = layout_mod;Audit
| Definitions | 3 |
|---|---|
| Public names | 5 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |