commit 1513ff78b94a8e826ccb9203a7b30b60a4a3b60a
parent 4f1c5eb58ba3c7376a83262f152db851e73801d4
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Sun, 26 Apr 2026 22:49:22 -0700
cc/parse: T[] in param position decays to T* (§L.2)
parse-fn-params already had this rewrite in place from the initial
implementation: arr ctype in fn-param position becomes ptr-to-elem
(and fn-typed becomes ptr-to-fn). The fixture locks the contract
so any future regression on param-type rewriting surfaces here.
cc-parse/43-array-param-decay.c: int sum(int a[], int n) { ... };
sum(xs, 4) over {1,2,3,4} -> exit 10. The callee's `a[i]` works
because (a) the param slot holds a real pointer (8 bytes) thanks
to the decay, (b) cg-load on the param produces a ptr-rval (no
arr decay needed since the type is already ptr).
Diffstat:
3 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/docs/CC-PUNCHLIST.md b/docs/CC-PUNCHLIST.md
@@ -393,12 +393,13 @@ both branches store into it, vstack ends with one frame opnd.
extent); `cg-push-field` for the flex member returns an `arr`-
typed lval that decays to `ptr` on use.
-- [ ] **`T[]` in parameter position decays to `T *`**
- - parse: `cc-parse/NN-array-param-decay.c` — `int sum(int a[], int n)
- { int s=0; for(int i=0;i<n;i++) s+=a[i]; return s; }` → known sum.
- - Needs: parser detects `arr` ctype in fn-param position and
- rewrites to `ptr` before slot allocation. cg sees a pointer and
- needs no special handling.
+- [x] **`T[]` in parameter position decays to `T *`**
+ - parse: `cc-parse/43-array-param-decay.c` — `int sum(int a[], int n)
+ { ... s+=a[i]; ... } sum(xs,4)` over `{1,2,3,4}` → exit 10.
+ - Done: `parse-fn-params` rewrites arr→ptr (and fn→ptr) before
+ slot allocation, so cg sees an 8-byte ptr slot and the callee's
+ `a[i]` decays the param's loaded ptr-rval and scales the index
+ correctly.
- [ ] **Array of function pointers initialized with named functions**
- parse: `cc-parse/NN-fnptr-tab.c` — `int f1(){return 1;}
diff --git a/tests/cc-parse/43-array-param-decay.c b/tests/cc-parse/43-array-param-decay.c
@@ -0,0 +1,26 @@
+// tests/cc-parse/43-array-param-decay.c — T[] in fn-param position
+// decays to T* (§L.2 of docs/CC-PUNCHLIST.md). The callee declares
+// `int a[]`; the parser must rewrite it to `int *` before slot
+// allocation so `a[i]` indexes through a real pointer (not via an
+// array slot, which would be wrong for an actual int* argument).
+//
+// sum(a, 4) over {1,2,3,4} = 10.
+
+int sum(int a[], int n) {
+ int s = 0;
+ int i = 0;
+ while (i < n) {
+ s = s + a[i];
+ i = i + 1;
+ }
+ return s;
+}
+
+int main() {
+ int xs[4];
+ xs[0] = 1;
+ xs[1] = 2;
+ xs[2] = 3;
+ xs[3] = 4;
+ return sum(xs, 4);
+}
diff --git a/tests/cc-parse/43-array-param-decay.expected-exit b/tests/cc-parse/43-array-param-decay.expected-exit
@@ -0,0 +1 @@
+10