Files
sx/examples/80-dot-shorthand-protocol-field.sx
agra a938c4f900 metal: GPU protocol + MetalGPU renders MSL triangle on iOS
Phase 8 step 3a of the Metal renderer port:

- New library/modules/gpu/ with types.sx (handles + ClearColor +
  TextureFormat enum), api.sx (GPU :: protocol { ... } covering the
  lifecycle / per-frame / resource / per-draw surface), and metal.sx
  (MetalGPU backend implementing the protocol against CAMetalLayer).
  Resource handles are 1-based indices into backend List(*void) tables.
  MTL aggregates >16 bytes (MTLRegion, MTLScissorRect) pass via *T to
  match arm64 Apple's indirect-by-reference ABI; MTLClearColor + CGSize
  go through the HFA path as direct fn-pointer casts on objc_msgSend.

- UIKitPlatform got a gpu_mode: GpuMode toggle + sibling SxMetalView
  class registration. In metal mode init skips EAGL context, the
  did_finish_launching IMP skips the EAGL drawable-properties dict,
  layoutSubviews reads the layer's bounds * dpi_scale into pixel_w/h
  instead of allocating a GL renderbuffer, and end_frame is a no-op
  (the MetalGPU owns its own present).

- examples/63-metal-clear.sx verifies the pipeline end-to-end on iOS
  sim — compiles a pass-through MSL shader (packed_float2/packed_float4
  to avoid alignment padding), uploads 3 vertices, draws a colored
  triangle on a dark-blue clear.

Compiler fixes (filed-and-fixed in this branch):

- inline if X { return E; } followed by a fall-through final expression
  no longer emits two terminators into the same basic block. Verified
  by examples/83-inline-if-return-fallthrough.sx.

- Top-level type alias Name :: u32; now resolves correctly as the type
  annotation on a global variable (was treated as ptr {}, breaking
  comparisons + initializers). Verified by examples/84-global-type-alias.sx.

Issue->feature promotion:

- 16 historical examples/issue-NNNN.sx repros now confirmed-fixed and
  renamed to focused feature names (67-82). Each gains a
  tests/expected/*.txt + .exit pair so the regression suite covers them.

- 5 stale issue repros deleted (subsumed by broader tests).

Regression suite: 68 passing, 0 failed. macOS chess builds + runs; wasm
chess builds; iOS sim GLES chess still renders the full board; iOS sim
Metal demo renders the triangle.
2026-05-17 19:36:37 +03:00

80 lines
2.2 KiB
Plaintext

// Dot-shorthand `.{ child = d }` for a struct whose first field is a protocol
// value, used as the argument to `List(Container).append` from two distinct
// container types. Exercises the cross-callsite path of dot-shorthand inference.
#import "modules/std.sx";
Drawable :: protocol {
draw :: () -> s32;
name :: () -> string;
layout :: (x: s32) -> s32;
handle :: (event: s32) -> bool;
}
Circle :: struct { radius: s32; }
impl Drawable for Circle {
draw :: (self: *Circle) -> s32 { self.radius; }
name :: (self: *Circle) -> string { "circle"; }
layout :: (self: *Circle, x: s32) -> s32 { x + self.radius; }
handle :: (self: *Circle, event: s32) -> bool { event > 0; }
}
Square :: struct { side: s32; }
impl Drawable for Square {
draw :: (self: *Square) -> s32 { self.side * self.side; }
name :: (self: *Square) -> string { "square"; }
layout :: (self: *Square, x: s32) -> s32 { x + self.side; }
handle :: (self: *Square, event: s32) -> bool { event > 1; }
}
Rect :: struct {
x: f32;
y: f32;
w: f32;
h: f32;
zero :: () -> Rect { Rect.{ x = 0.0, y = 0.0, w = 0.0, h = 0.0 }; }
}
Container :: struct {
child: Drawable;
computed_frame: Rect = .zero();
}
// Two different structs, each with List(Container), both calling .append(.{...})
// This mirrors VStack/HStack in the game.
StackA :: struct {
children: List(Container);
add :: (self: *StackA, d: Drawable) {
// BUG: `.{ child = d }` causes LLVM error when 2+ structs do this
self.children.append(.{ child = d });
}
}
StackB :: struct {
children: List(Container);
add :: (self: *StackB, d: Drawable) {
// BUG: second struct doing `.{ child = d }` triggers the error
self.children.append(.{ child = d });
// FIX: explicit `Container.{ child = d }` works
// self.children.append(Container.{ child = d });
}
}
main :: () -> void {
c := Circle.{ radius = 42 };
s := Square.{ side = 5 };
a : StackA = .{};
a.add(c);
print("StackA: draw={}\n", a.children.items[0].child.draw());
b : StackB = .{};
b.add(s);
print("StackB: draw={}\n", b.children.items[0].child.draw());
print("OK\n");
}