@@ -12,7 +12,7 @@ enum Emit.Alloc:
1212
1313// the declarations emit reads, by name
1414enum Emit.Cx:
15- case Emit.Cx(s: Coll.IMap[K.Term], fns: Coll.SMap[El.Fn], cases: Coll.SMap[Emit.CaseAt], enums: Coll.SMap[Ast.Enum], protos: Coll.SMap[Ast.Proto], hprotos: Coll.SMap[Sym.Sym], alloc: Emit.Alloc)
15+ case Emit.Cx(s: Coll.IMap[K.Term], fns: Coll.SMap[El.Fn], cases: Coll.SMap[Emit.CaseAt], enums: Coll.SMap[Ast.Enum], protos: Coll.SMap[Ast.Proto], hprotos: Coll.SMap[Sym.Sym], alloc: Emit.Alloc, nominal: Coll.SMap[K.Term] )
1616
1717// a C local: a param p_x, or a binding v_x_n (n = the number of locals bound before it)
1818enum Emit.Name:
@@ -65,7 +65,7 @@ enum Emit.Part:
6565
6666// ── the declarations by name ──
6767def Emit.cxOf {r: Region} (ds: Coll.List[Ast.Decl], s: Coll.IMap[K.Term], fs: Coll.List[El.Fn]): Emit.Cx =
68- Emit.cxDecls(ds, Emit.Cx(s, Emit.fnMap(fs, Coll.sEmpty()), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Emit.VTable))
68+ Emit.cxDecls(ds, Emit.Cx(s, Emit.fnMap(fs, Coll.sEmpty()), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Emit.VTable, Coll.sEmpty() ))
6969
7070def Emit.fnMap {r: Region} (fs: Coll.List[El.Fn], m: Coll.SMap[El.Fn]): Coll.SMap[El.Fn] = fs match
7171 case Coll.Nil => m
@@ -76,17 +76,23 @@ def Emit.fnMap {r: Region} (fs: Coll.List[El.Fn], m: Coll.SMap[El.Fn]): Coll.SMa
7676def Emit.cxDecls {r: Region} (ds: Coll.List[Ast.Decl], cx: Emit.Cx): Emit.Cx = ds match
7777 case Coll.Nil => cx
7878 case Coll.Cons(d, rest) =>
79- val Emit.Cx(s, fns, cs, es, ps, hp, al) = cx
79+ val Emit.Cx(s, fns, cs, es, ps, hp, al, nm ) = cx
8080 d match
8181 case Ast.DEnum(e) =>
8282 val Ast.Enum(_, n, _, cases) = e
83- Emit.cxDecls(rest, Emit.Cx(s, fns, Emit.caseMap(cases, e, 0, cs), Coll.sInsert(es, n, e), ps, hp, al))
83+ Emit.cxDecls(rest, Emit.Cx(s, fns, Emit.caseMap(cases, e, 0, cs), Coll.sInsert(es, n, e), ps, hp, al, nm ))
8484 case Ast.DProto(p) =>
8585 val Ast.Proto(_, n, _, _, _, _, _, _) = p
86- Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, Coll.sInsert(ps, n, p), hp, al))
86+ Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, Coll.sInsert(ps, n, p), hp, al, nm ))
8787 case Ast.DHandler(h) =>
8888 val Ast.Handler(_, n, p, _) = h
89- Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, Coll.sInsert(hp, n, p), if rt_str_eq(Sym.str(n), "Arena") then Emit.Inline else al))
89+ Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, Coll.sInsert(hp, n, p), if rt_str_eq(Sym.str(n), "Arena") then Emit.Inline else al, nm))
90+ // a type with <> facts stays nominal through typing (s2/tlevel.srs Tl.nominal): in C it is its body's type
91+ case Ast.DType(td) =>
92+ val Ast.TypeDecl(_, n, _, _, body) = td
93+ body match
94+ case Ast.Alias(t) => Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, hp, al, Coll.sInsert(nm, n, Ty.term(t, Coll.Nil))))
95+ case _ => Emit.cxDecls(rest, cx)
9096 case _ => Emit.cxDecls(rest, cx)
9197
9298def Emit.caseMap {r: Region} (cs: Coll.List[Ast.Case], e: Ast.Enum, i: I64, m: Coll.SMap[Emit.CaseAt]): Coll.SMap[Emit.CaseAt] = cs match
@@ -96,17 +102,17 @@ def Emit.caseMap {r: Region} (cs: Coll.List[Ast.Case], e: Ast.Enum, i: I64, m: C
96102 Emit.caseMap(rest, e, i + 1, Coll.sInsert(m, n, Emit.CaseAt(i, e, c)))
97103
98104def Emit.isProto {r: Region} (cx: Emit.Cx, n: Sym.Sym): Bool =
99- val Emit.Cx(_, _, _, _, ps, _, _) = cx
105+ val Emit.Cx(_, _, _, _, ps, _, _, _ ) = cx
100106 Coll.sLookup(ps, n) match
101107 case Coll.Some(_) => true
102108 case Coll.None => false
103109
104110def Emit.caseAt {r: Region} (cx: Emit.Cx, c: Sym.Sym): Coll.Option[Emit.CaseAt] =
105- val Emit.Cx(_, _, cs, _, _, _, _) = cx
111+ val Emit.Cx(_, _, cs, _, _, _, _, _ ) = cx
106112 Coll.sLookup(cs, c)
107113
108114def Emit.subst(cx: Emit.Cx): Coll.IMap[K.Term] =
109- val Emit.Cx(s, _, _, _, _, _, _) = cx
115+ val Emit.Cx(s, _, _, _, _, _, _, _ ) = cx
110116 s
111117
112118def Emit.grAll {r: Region} (cx: Emit.Cx, fi: Emit.Fi, ts: Coll.List[K.Term]): Coll.List[K.Term] =
@@ -201,12 +207,18 @@ def Emit.ctype {r: Region} (cx: Emit.Cx, t: K.Term): Unit = t match
201207 else if rt_str_eq(x, "Str") then rt_print("srs_str")
202208 else if rt_str_eq(x, "Ptr") then rt_print("void*")
203209 else if Emit.isProto(cx, n) then rt_print("srs_cap*")
204- else
205- rt_print("struct E_")
206- Emit.enumName(n, args)
207- rt_print("*")
210+ else Emit.nominalOf(cx, n) match
211+ case Coll.Some(b) => Emit.ctype(cx, b)
212+ case Coll.None =>
213+ rt_print("struct E_")
214+ Emit.enumName(n, args)
215+ rt_print("*")
208216 case _ => rt_print("int64_t")
209217
218+ def Emit.nominalOf {r: Region} (cx: Emit.Cx, n: Sym.Sym): Coll.Option[K.Term] =
219+ val Emit.Cx(_, _, _, _, _, _, _, nm) = cx
220+ Coll.sLookup(nm, n)
221+
210222// ── expressions (spec sec 4) ──
211223def Emit.ex {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, pos: Emit.Pos, e: El.Expr): Unit = e match
212224 case El.Int(_, v) =>
@@ -327,7 +339,7 @@ def Emit.isSelf(fi: Emit.Fi, f: Sym.Sym, ts: Coll.List[K.Term]): Bool =
327339 Sym.eq(f, self) && Mono.termsEq(ts, sts)
328340
329341def Emit.fnName {r: Region} (cx: Emit.Cx, f: Sym.Sym, ts: Coll.List[K.Term]): Unit =
330- val Emit.Cx(_, fns, _, _, _, _, _) = cx
342+ val Emit.Cx(_, fns, _, _, _, _, _, _ ) = cx
331343 Coll.sLookup(fns, f) match
332344 case Coll.Some(g) =>
333345 val El.Fn(_, _, _, _, _, _, body) = g
@@ -412,7 +424,7 @@ def Emit.ctor {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, en: Sym.Sym,
412424 case Coll.Some(ca) =>
413425 val Emit.CaseAt(tag, _, cs) = ca
414426 val Ast.Case(_, _, fields) = cs
415- val Emit.Cx(_, _, _, _, _, _, al) = cx
427+ val Emit.Cx(_, _, _, _, _, _, al, _ ) = cx
416428 val n = Emit.envN(env)
417429 rt_print("({ struct E_")
418430 Emit.enumName(en, ts)
@@ -469,7 +481,7 @@ def Emit.fieldSets {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, n: I64,
469481
470482// the index of op in protocol p's op list
471483def Emit.opIndex {r: Region} (cx: Emit.Cx, p: Sym.Sym, op: Sym.Sym): I64 =
472- val Emit.Cx(_, _, _, _, ps, _, _) = cx
484+ val Emit.Cx(_, _, _, _, ps, _, _, _ ) = cx
473485 Coll.sLookup(ps, p) match
474486 case Coll.None => 0
475487 case Coll.Some(pr) =>
@@ -486,7 +498,7 @@ def Emit.opIn(os: Coll.List[Ast.Op], op: Sym.Sym, i: I64): I64 = os match
486498// trait method of {} evidence: the given's static fn (rung T2)
487499def Emit.op {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, recv: Sym.Sym, ty: K.Term, op: Sym.Sym, args: Coll.List[El.Expr]): Unit = ty match
488500 case K.Con(pn, targs) =>
489- val Emit.Cx(_, _, _, _, ps, _, _) = cx
501+ val Emit.Cx(_, _, _, _, ps, _, _, _ ) = cx
490502 Coll.sLookup(ps, pn) match
491503 case Coll.Some(p) =>
492504 val Ast.Proto(_, _, _, _, ends, _, ops, _) = p
@@ -650,7 +662,7 @@ def Emit.drops {r: Region} (cx: Emit.Cx, ds: Coll.List[Emit.Drop]): Unit = ds ma
650662 case Coll.Nil => ()
651663 case Coll.Cons(d, rest) =>
652664 val Emit.Drop(v, p) = d
653- val Emit.Cx(_, _, _, _, ps, _, _) = cx
665+ val Emit.Cx(_, _, _, _, ps, _, _, _ ) = cx
654666 Coll.sLookup(ps, p) match
655667 case Coll.None => ()
656668 case Coll.Some(pr) =>
@@ -671,7 +683,7 @@ def Emit.drops {r: Region} (cx: Emit.Cx, ds: Coll.List[Emit.Drop]): Unit = ds ma
671683
672684// the protocol of handler h
673685def Emit.protoOfH {r: Region} (cx: Emit.Cx, h: Sym.Sym): Sym.Sym =
674- val Emit.Cx(_, _, _, _, _, hp, _) = cx
686+ val Emit.Cx(_, _, _, _, _, hp, _, _ ) = cx
675687 Coll.sLookup(hp, h) match
676688 case Coll.Some(p) => p
677689 case Coll.None => h
@@ -836,7 +848,7 @@ def Emit.closeEnums {r: Region} (cx: Emit.Cx, todo: Coll.List[K.Term], seen: Col
836848 case Coll.Nil => Coll.rev(out)
837849 case Coll.Cons(t, rest) => t match
838850 case K.Con(n, args) =>
839- val Emit.Cx(_, _, _, es, _, _, _) = cx
851+ val Emit.Cx(_, _, _, es, _, _, _, _ ) = cx
840852 val h = Mono.thash(t, 0)
841853 Coll.sLookup(es, n) match
842854 case Coll.None => Emit.closeEnums(cx, rest, seen, out)
@@ -894,7 +906,7 @@ def Emit.statics {r: Region} (cx: Emit.Cx, ts: Coll.List[K.Term]): Unit = ts mat
894906// the struct (Layout) or the nullary statics (Code) of enum term t
895907def Emit.withEnum {r: Region} (cx: Emit.Cx, t: K.Term, what: Emit.Part): Unit = t match
896908 case K.Con(n, args) =>
897- val Emit.Cx(_, _, _, es, _, _, _) = cx
909+ val Emit.Cx(_, _, _, es, _, _, _, _ ) = cx
898910 Coll.sLookup(es, n) match
899911 case Coll.None => ()
900912 case Coll.Some(e) =>
@@ -971,7 +983,7 @@ def Emit.ptrs {r: Region} (tps: Coll.List[Sym.Sym], acc: Coll.List[Ty.Bind]): Co
971983
972984// a generic extern whose result is a [] binder returns void*: the call casts it to the instance's type (spec sec 4 XInst)
973985def Emit.externCast {r: Region} (cx: Emit.Cx, f: Sym.Sym, gts: Coll.List[K.Term]): Coll.Option[K.Term] =
974- val Emit.Cx(_, fns, _, _, _, _, _) = cx
986+ val Emit.Cx(_, fns, _, _, _, _, _, _ ) = cx
975987 Coll.sLookup(fns, f) match
976988 case Coll.Some(g) =>
977989 val El.Fn(_, _, tps, _, _, ret, body) = g
@@ -1028,7 +1040,7 @@ def Emit.vtables {r: Region} (cx: Emit.Cx, hs: Coll.List[El.Handler]): Unit = hs
10281040 case Coll.Nil => ()
10291041 case Coll.Cons(h, rest) =>
10301042 val El.Handler(hn, p, _) = h
1031- val Emit.Cx(_, _, _, _, ps, _, _) = cx
1043+ val Emit.Cx(_, _, _, _, ps, _, _, _ ) = cx
10321044 rt_print("static void* const vt_")
10331045 Emit.id(hn)
10341046 rt_print("[] = {")
@@ -1067,7 +1079,7 @@ def Emit.allocFast {r: Region} (cx: Emit.Cx): Unit =
10671079 rt_print("}\n")
10681080
10691081def Emit.hasMain {r: Region} (cx: Emit.Cx): Bool =
1070- val Emit.Cx(_, fns, _, _, _, _, _) = cx
1082+ val Emit.Cx(_, fns, _, _, _, _, _, _ ) = cx
10711083 Coll.sLookup(fns, Sym.of("main")) match
10721084 case Coll.Some(_) => true
10731085 case Coll.None => false
@@ -1090,7 +1102,7 @@ def Emit.program {r: Region} (ds: Coll.List[Ast.Decl], res: Ty.Result): Unit =
10901102 Emit.protos(cx, is)
10911103 Emit.handlers(cx, hs, Emit.Layout)
10921104 Emit.vtables(cx, hs)
1093- val Emit.Cx(_, _, _, _, _, _, al) = cx
1105+ val Emit.Cx(_, _, _, _, _, _, al, _ ) = cx
10941106 al match
10951107 case Emit.Inline => Emit.allocFast(cx)
10961108 case Emit.VTable => ()
0 commit comments