const std = @import("std"); const pi = @import("program_index.zig"); const ProgramIndex = pi.ProgramIndex; const ast = @import("../ast.zig"); const types = @import("types.zig"); const inst = @import("inst.zig"); test "ProgramIndex.init starts empty with unset borrowed views" { var idx = ProgramIndex.init(std.testing.allocator); defer idx.deinit(); try std.testing.expectEqual(@as(u32, 0), idx.import_flags.count()); try std.testing.expect(idx.module_scopes == null); try std.testing.expect(idx.import_graph == null); } test "ProgramIndex.import_flags round-trips imported vs local" { var idx = ProgramIndex.init(std.testing.allocator); defer idx.deinit(); try idx.import_flags.put("printf", true); try idx.import_flags.put("main", false); try std.testing.expectEqual(@as(?bool, true), idx.import_flags.get("printf")); try std.testing.expectEqual(@as(?bool, false), idx.import_flags.get("main")); try std.testing.expect(idx.import_flags.get("absent") == null); } test "ProgramIndex borrows module_scopes / import_graph without owning them" { const ScopeSet = std.StringHashMap(std.StringHashMap(void)); var scopes = ScopeSet.init(std.testing.allocator); defer scopes.deinit(); var graph = ScopeSet.init(std.testing.allocator); defer graph.deinit(); var idx = ProgramIndex.init(std.testing.allocator); defer idx.deinit(); idx.module_scopes = &scopes; idx.import_graph = &graph; // Reads go through the borrowed pointer; the backing stays caller-owned, // so idx.deinit() must not free it (testing.allocator would flag a // double-free / leak otherwise). try std.testing.expect(idx.module_scopes.? == &scopes); try std.testing.expect(idx.import_graph.? == &graph); try std.testing.expectEqual(@as(u32, 0), idx.module_scopes.?.count()); } test "ProgramIndex declaration maps round-trip (A1.1b)" { var idx = ProgramIndex.init(std.testing.allocator); defer idx.deinit(); // Minimal AST node reused wherever a *Node is required. var blk = ast.Node{ .span = .{ .start = 0, .end = 0 }, .data = .{ .block = .{ .stmts = &.{} } } }; // fn_ast_map: function name → AST decl. const fd = ast.FnDecl{ .name = "main", .params = &.{}, .return_type = null, .body = &blk }; try idx.fn_ast_map.put("main", &fd); try std.testing.expect(idx.fn_ast_map.get("main").? == &fd); // type_alias_map: alias name → target TypeId. try idx.type_alias_map.put("ShaderHandle", .s64); try std.testing.expectEqual(@as(?types.TypeId, .s64), idx.type_alias_map.get("ShaderHandle")); // global_names: #run global name → GlobalInfo. try idx.global_names.put("g", .{ .id = inst.GlobalId.fromIndex(0), .ty = .s64 }); try std.testing.expect(idx.global_names.get("g").?.id == inst.GlobalId.fromIndex(0)); // module_const_map: const name → ModuleConstInfo. try idx.module_const_map.put("AF_INET", .{ .value = &blk, .ty = .s32 }); try std.testing.expect(idx.module_const_map.get("AF_INET").?.value == &blk); // foreign_class_map: sx alias → ForeignClassDecl. const fcd = ast.ForeignClassDecl{ .name = "NSString", .foreign_path = "NSString", .runtime = .objc_class, .members = &.{}, .is_foreign = true, .is_main = false, }; try idx.foreign_class_map.put("NSString", &fcd); try std.testing.expect(idx.foreign_class_map.get("NSString").? == &fcd); // protocol_decl_map: protocol name → ProtocolDeclInfo. try idx.protocol_decl_map.put("Show", .{ .name = "Show", .is_inline = false, .methods = &.{} }); try std.testing.expectEqualStrings("Show", idx.protocol_decl_map.get("Show").?.name); // protocol_ast_map: protocol name → AST decl. const pd = ast.ProtocolDecl{ .name = "Show", .methods = &.{} }; try idx.protocol_ast_map.put("Show", &pd); try std.testing.expect(idx.protocol_ast_map.get("Show").? == &pd); // struct_template_map: generic struct name → template. try idx.struct_template_map.put("List", .{ .name = "List", .type_params = &.{}, .field_names = &.{}, .field_type_nodes = &.{} }); try std.testing.expectEqualStrings("List", idx.struct_template_map.get("List").?.name); // ufcs_alias_map: alias name → target function name. try idx.ufcs_alias_map.put("len", "list_len"); try std.testing.expectEqualStrings("list_len", idx.ufcs_alias_map.get("len").?); } /// Stand-in for the leaf-name lookup both array-dimension resolvers pass to the /// shared `evalConstIntExpr`: `M`/`N` resolve to integers, everything else is /// genuinely non-comptime. const DimCtx = struct { pub fn lookupDimName(_: DimCtx, name: []const u8) ?i64 { if (std.mem.eql(u8, name, "M")) return 4; if (std.mem.eql(u8, name, "N")) return 6; return null; } // `xs` stands in for a pack of arity 3; every other name has no pack length. pub fn lookupPackLen(_: DimCtx, name: []const u8) ?i64 { if (std.mem.eql(u8, name, "xs")) return 3; return null; } }; fn nLit(v: i64) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .int_literal = .{ .value = v } } }; } fn nFloat(v: f64) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .float_literal = .{ .value = v } } }; } fn nIdent(name: []const u8) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .identifier = .{ .name = name } } }; } fn nBin(op: ast.BinaryOp.Op, l: *ast.Node, r: *ast.Node) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .binary_op = .{ .op = op, .lhs = l, .rhs = r } } }; } fn nNeg(operand: *ast.Node) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .unary_op = .{ .op = .negate, .operand = operand } } }; } fn nField(obj: *ast.Node, field: []const u8) ast.Node { return .{ .span = .{ .start = 0, .end = 0 }, .data = .{ .field_access = .{ .object = obj, .field = field } } }; } test "evalConstIntExpr folds constant-expression array dimensions, halts on non-const" { const eval = pi.evalConstIntExpr; const ctx = DimCtx{}; var l5 = nLit(5); var one = nLit(1); var two = nLit(2); var zero = nLit(0); var m = nIdent("M"); var n = nIdent("N"); var z = nIdent("Z"); // unbound — genuinely non-comptime // Leaves: literal, named const, unbound name. try std.testing.expectEqual(@as(?i64, 5), eval(&l5, ctx)); try std.testing.expectEqual(@as(?i64, 4), eval(&m, ctx)); try std.testing.expect(eval(&z, ctx) == null); // `M + 1`, `M * N`, `N - M`. var add = nBin(.add, &m, &one); var mul = nBin(.mul, &m, &n); var sub = nBin(.sub, &n, &m); try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx)); try std.testing.expectEqual(@as(?i64, 24), eval(&mul, ctx)); try std.testing.expectEqual(@as(?i64, 2), eval(&sub, ctx)); // Nested `(M + N) - 1` and parenthesised `(M + 1) * 2` (parens carry no node). var addmn = nBin(.add, &m, &n); var nested = nBin(.sub, &addmn, &one); var paren = nBin(.mul, &add, &two); try std.testing.expectEqual(@as(?i64, 9), eval(&nested, ctx)); try std.testing.expectEqual(@as(?i64, 10), eval(&paren, ctx)); // Unary negate. var neg = nNeg(&m); try std.testing.expectEqual(@as(?i64, -4), eval(&neg, ctx)); // `.len` leaf resolves via `ctx.lookupPackLen` and folds in an // expression (`xs.len` → 3, `xs.len - 1` → 2). A `.len` on a non-pack name // and a non-`len` field are not compile-time integer leaves → null. var xs = nIdent("xs"); var xslen = nField(&xs, "len"); var xslen_m1 = nBin(.sub, &xslen, &one); try std.testing.expectEqual(@as(?i64, 3), eval(&xslen, ctx)); try std.testing.expectEqual(@as(?i64, 2), eval(&xslen_m1, ctx)); var zlen = nField(&z, "len"); var xscap = nField(&xs, "cap"); try std.testing.expect(eval(&zlen, ctx) == null); try std.testing.expect(eval(&xscap, ctx) == null); // Genuinely non-const operand, division by zero, a non-arithmetic operator, // and overflow all yield null → the caller's clean compile-halt (no panic, // no fabricated length). var addz = nBin(.add, &m, &z); var divz = nBin(.div, &m, &zero); var cmp = nBin(.lt, &m, &n); var big = nLit(std.math.maxInt(i64)); var ovf = nBin(.mul, &big, &two); try std.testing.expect(eval(&addz, ctx) == null); try std.testing.expect(eval(&divz, ctx) == null); try std.testing.expect(eval(&cmp, ctx) == null); try std.testing.expect(eval(&ovf, ctx) == null); } test "floatToIntExact accepts integral floats, rejects the rest" { const f = pi.floatToIntExact; // Integral floats (positive, zero, negative) fold to their exact integer. try std.testing.expectEqual(@as(?i64, 4), f(4.0)); try std.testing.expectEqual(@as(?i64, 0), f(0.0)); try std.testing.expectEqual(@as(?i64, -2), f(-2.0)); // Non-integral / non-finite → null (the caller's clean halt). try std.testing.expect(f(4.5) == null); try std.testing.expect(f(0.1) == null); try std.testing.expect(f(std.math.inf(f64)) == null); try std.testing.expect(f(-std.math.inf(f64)) == null); try std.testing.expect(f(std.math.nan(f64)) == null); // Out-of-i64-range integral floats → null (no @intFromFloat range panic). // `-2^63` is exactly the i64 minimum and IS representable. try std.testing.expectEqual(@as(?i64, std.math.minInt(i64)), f(-9223372036854775808.0)); try std.testing.expect(f(9223372036854775808.0) == null); // 2^63, just past maxInt(i64) try std.testing.expect(f(1.0e30) == null); } test "moduleConstInt folds expression-RHS consts and rejects cycles" { var table = types.TypeTable.init(std.testing.allocator); defer table.deinit(); var map = std.StringHashMap(pi.ModuleConstInfo).init(std.testing.allocator); defer map.deinit(); // M :: 2 (literal), N :: M + 1 (expression), P :: N * 2 (expression over an // expression const), F :: 4.0 (integral float), G :: 4.5 (fractional). var m_val = nLit(2); var m_id = nIdent("M"); var one = nLit(1); var n_val = nBin(.add, &m_id, &one); var n_id = nIdent("N"); var two = nLit(2); var p_val = nBin(.mul, &n_id, &two); var f_val = nFloat(4.0); var g_val = nFloat(4.5); try map.put("M", .{ .value = &m_val, .ty = .s64 }); try map.put("N", .{ .value = &n_val, .ty = .s64 }); try map.put("P", .{ .value = &p_val, .ty = .s64 }); try map.put("F", .{ .value = &f_val, .ty = .f64 }); try map.put("G", .{ .value = &g_val, .ty = .f64 }); try std.testing.expectEqual(@as(?i64, 2), pi.moduleConstInt(&map, &table, "M")); try std.testing.expectEqual(@as(?i64, 3), pi.moduleConstInt(&map, &table, "N")); try std.testing.expectEqual(@as(?i64, 6), pi.moduleConstInt(&map, &table, "P")); try std.testing.expectEqual(@as(?i64, 4), pi.moduleConstInt(&map, &table, "F")); try std.testing.expect(pi.moduleConstInt(&map, &table, "G") == null); try std.testing.expect(pi.moduleConstInt(&map, &table, "absent") == null); // A cyclic const has no compile-time integer value, and folding it must not // recurse forever: mutual `A :: B + 0; B :: A + 0` and self `C :: C + 0` all // fold to null via the frame-based cycle guard. var a_id = nIdent("A"); var b_id = nIdent("B"); var c_id = nIdent("C"); var zero = nLit(0); var a_val = nBin(.add, &b_id, &zero); var b_val = nBin(.add, &a_id, &zero); var c_val = nBin(.add, &c_id, &zero); try map.put("A", .{ .value = &a_val, .ty = .s64 }); try map.put("B", .{ .value = &b_val, .ty = .s64 }); try map.put("C", .{ .value = &c_val, .ty = .s64 }); try std.testing.expect(pi.moduleConstInt(&map, &table, "A") == null); try std.testing.expect(pi.moduleConstInt(&map, &table, "B") == null); try std.testing.expect(pi.moduleConstInt(&map, &table, "C") == null); } test "moduleConstInt gates the fold on the declared type, not the initializer node" { var table = types.TypeTable.init(std.testing.allocator); defer table.deinit(); var map = std.StringHashMap(pi.ModuleConstInfo).init(std.testing.allocator); defer map.deinit(); // An `int_literal` value node folds to an integer ONLY when the declared // type is numeric. A `string`/`bool`-typed const carrying an integer-looking // initializer must never be folded into a count (issue 0088): the count path // consults `ModuleConstInfo.ty`, not just the node shape. var int_val = nLit(4); try map.put("OK", .{ .value = &int_val, .ty = .s64 }); try map.put("STR", .{ .value = &int_val, .ty = .string }); try map.put("BOOLEAN", .{ .value = &int_val, .ty = .bool }); try std.testing.expectEqual(@as(?i64, 4), pi.moduleConstInt(&map, &table, "OK")); try std.testing.expect(pi.moduleConstInt(&map, &table, "STR") == null); try std.testing.expect(pi.moduleConstInt(&map, &table, "BOOLEAN") == null); // The same gate holds for a const-EXPRESSION value node (`M + 2`), not just // a bare literal: a `string`-typed const whose initializer is a foldable // integer expression must still never fold as a count (issue 0088 attempt 2 — // the const-expression leak). `KEXPR : s64 : M + 2` (numeric type) folds; the // same expression declared `string` does not. var m_lit = nLit(2); var add2 = nLit(2); var expr_val = nBin(.add, &m_lit, &add2); try map.put("KEXPR", .{ .value = &expr_val, .ty = .s64 }); try map.put("STREXPR", .{ .value = &expr_val, .ty = .string }); try std.testing.expectEqual(@as(?i64, 4), pi.moduleConstInt(&map, &table, "KEXPR")); try std.testing.expect(pi.moduleConstInt(&map, &table, "STREXPR") == null); } test "evalConstIntExpr folds an integral float literal, halts on a fractional one" { const eval = pi.evalConstIntExpr; const ctx = DimCtx{}; var f4 = nFloat(4.0); var f45 = nFloat(4.5); var one = nLit(1); // A direct integral float dimension (`[4.0]T`) folds; `4.5` does not. try std.testing.expectEqual(@as(?i64, 4), eval(&f4, ctx)); try std.testing.expect(eval(&f45, ctx) == null); // It composes inside an expression dimension (`4.0 + 1` → 5); a fractional // operand poisons the whole fold to null. var add = nBin(.add, &f4, &one); var addbad = nBin(.add, &f45, &one); try std.testing.expectEqual(@as(?i64, 5), eval(&add, ctx)); try std.testing.expect(eval(&addbad, ctx) == null); }