@@ -164,6 +164,7 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
164
164
msg " Unexpected arguments for builtin symbol ' ${sym.nme}' " -> arg.toLoc :: Nil , S (arg),
165
165
source = Diagnostic .Source .Compilation )
166
166
End (" error" )
167
+ case st.TyApp (f, ts) => term(f)(k) // * Type arguments are erased
167
168
case st.App (f, arg) =>
168
169
val isMlsFun = f.symbol.fold(f.isInstanceOf [st.Lam ]):
169
170
case _ : sem.BuiltinSymbol => true
@@ -200,7 +201,7 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
200
201
conclude(Select (p, nme)(sel.sym))
201
202
case _ => subTerm(f)(conclude)
202
203
203
- case st.Blk ((h @ Handle (lhs, rhs, args , cls, defs)) :: stmts, res) =>
204
+ case st.Blk ((h @ Handle (lhs, rhs, as , cls, defs)) :: stmts, res) =>
204
205
if ! lowerHandlers then
205
206
raise(ErrorReport (
206
207
msg " Effect handlers are not enabled " ->
@@ -218,14 +219,10 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
218
219
}.collect{ case Some (v) => v }
219
220
val resSym = TempSymbol (S (t))
220
221
subTerm(rhs): par =>
221
- def rec (as : Ls [st], asr : Ls [ Path ]) : Block = as match
222
- case Nil => HandleBlock (lhs, resSym, par, asr.reverse , cls, handlers,
222
+ subTerms (as) : asr =>
223
+ HandleBlock (lhs, resSym, par, asr, cls, handlers,
223
224
term_nonTail(st.Blk (stmts, res))(HandleBlockReturn (_)),
224
225
k(Value .Ref (resSym)))
225
- case a :: as =>
226
- subTerm_nonTail(a): ar =>
227
- rec(as, ar :: asr)
228
- rec(args, Nil )
229
226
230
227
case st.Blk (Nil , res) => term(res)(k)
231
228
case st.Blk ((t : sem.Term ) :: stats, res) =>
@@ -255,33 +252,25 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
255
252
term(st.Blk (stats, res))(k)
256
253
case cls : ClassLikeDef =>
257
254
reportAnnotations(cls, cls.extraAnnotations)
258
- val bodBlk = cls.body.blk
259
- val (mtds, rest1) = bodBlk.stats.partitionMap:
260
- case td : TermDefinition if td.k is syntax.Fun => L (td)
261
- case s => R (s)
262
- val (privateFlds, rest2) = rest1.partitionMap:
263
- case decl @ LetDecl (sym : TermSymbol , annotations) =>
264
- reportAnnotations(decl, annotations)
265
- L (sym)
266
- case s => R (s)
267
- val publicFlds = rest2.collect:
268
- case td @ TermDefinition (k = (_ : syntax.Val )) => td
269
- Define (ClsLikeDefn (cls.owner, cls.sym, cls.bsym, cls.kind, cls.paramsOpt, N ,
270
- mtds.flatMap: td =>
271
- td.body.map: bod =>
272
- val (paramLists, bodyBlock) = setupFunctionDef(td.params, bod, S (td.sym.nme))
273
- FunDefn (td.owner, td.sym, paramLists, bodyBlock)
274
- ,
275
- privateFlds,
276
- publicFlds,
277
- End (),
278
- term_nonTail(Blk (rest2, bodBlk.res))(ImplctRet )
279
- // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }`
280
- .mapTail:
281
- case Return (Value .Lit (syntax.Tree .UnitLit (true )), true ) => End ()
282
- case t => t
283
- ),
284
- term_nonTail(st.Blk (stats, res))(k))
255
+ val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(cls.body)
256
+ cls.ext match
257
+ case N =>
258
+ Define (
259
+ ClsLikeDefn (cls.owner, cls.sym, cls.bsym, cls.kind, cls.paramsOpt, N ,
260
+ mtds, privateFlds, publicFlds, End (), ctor),
261
+ term_nonTail(st.Blk (stats, res))(k)
262
+ )
263
+ case S (ext) =>
264
+ assert(k isnt syntax.Mod ) // modules can't extend things and can't have super calls
265
+ subTerm(ext.cls): clsp =>
266
+ val pctor = // TODO dedup with `New` case
267
+ args(ext.args): args =>
268
+ Return (Call (Value .Ref (State .builtinOpsMap(" super" )), args)(true , true ), implct = true )
269
+ Define (
270
+ ClsLikeDefn (cls.owner, cls.sym, cls.bsym, cls.kind, cls.paramsOpt, S (clsp),
271
+ mtds, privateFlds, publicFlds, pctor, ctor),
272
+ term_nonTail(st.Blk (stats, res))(k)
273
+ )
285
274
case _ =>
286
275
// TODO handle
287
276
term(st.Blk (stats, res))(k)
@@ -463,15 +452,22 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
463
452
subTerm_nonTail(fld): f =>
464
453
k(DynSelect (p, f, ai))
465
454
466
-
467
- case New (cls, as) =>
455
+
456
+ case New (cls, as, N ) =>
468
457
subTerm(cls): sr =>
469
- def rec (as : Ls [st], asr : Ls [Path ]): Block = as match
470
- case Nil => k(Instantiate (sr, asr.reverse))
471
- case a :: as =>
472
- subTerm_nonTail(a): ar =>
473
- rec(as, ar :: asr)
474
- rec(as, Nil )
458
+ subTerms(as): asr =>
459
+ k(Instantiate (sr, asr))
460
+
461
+ case New (cls, as, S ((isym, rft))) =>
462
+ subTerm(cls): clsp =>
463
+ val sym = new BlockMemberSymbol (isym.name, Nil )
464
+ val (mtds, publicFlds, privateFlds, ctor) = gatherMembers(rft)
465
+ val pctor =
466
+ args(as): args =>
467
+ Return (Call (Value .Ref (State .builtinOpsMap(" super" )), args)(true , true ), implct = true )
468
+ val clsDef = ClsLikeDefn (N , isym, sym, syntax.Cls , N , S (clsp),
469
+ mtds, privateFlds, publicFlds, pctor, ctor)
470
+ Define (clsDef, term_nonTail(New (sym.ref(), Nil , N ))(k))
475
471
476
472
case Try (sub, finallyDo) =>
477
473
val l = new TempSymbol (S (sub))
@@ -488,12 +484,8 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
488
484
Assign (reg, Instantiate (Select (Value .Ref (State .globalThisSymbol), Tree .Ident (" Region" ))(N ), Nil ),
489
485
term_nonTail(body)(k))
490
486
case RegRef (reg, value) =>
491
- def rec (as : Ls [st], asr : Ls [Path ]): Block = as match
492
- case Nil => k(Instantiate (Select (Value .Ref (State .globalThisSymbol), Tree .Ident (" Ref" ))(N ), asr.reverse))
493
- case a :: as =>
494
- subTerm_nonTail(a): ar =>
495
- rec(as, ar :: asr)
496
- rec(reg :: value :: Nil , Nil )
487
+ subTerms(reg :: value :: Nil ): args =>
488
+ k(Instantiate (Select (Value .Ref (State .globalThisSymbol), Tree .Ident (" Ref" ))(N ), args))
497
489
case Deref (ref) =>
498
490
subTerm(ref): r =>
499
491
k(Select (r, Tree .Ident (" value" ))(N ))
@@ -520,6 +512,36 @@ class Lowering(lowerHandlers: Bool, stackLimit: Option[Int])(using TL, Raise, St
520
512
// case _ =>
521
513
// subTerm(t)(k)
522
514
515
+ def gatherMembers (clsBody : ObjBody )(using Subst ): (Ls [FunDefn ], Ls [TermDefinition ], Ls [TermSymbol ], Block ) =
516
+ val mtds = clsBody.methods
517
+ .flatMap: td =>
518
+ td.body.map: bod =>
519
+ val (paramLists, bodyBlock) = setupFunctionDef(td.params, bod, S (td.sym.nme))
520
+ FunDefn (td.owner, td.sym, paramLists, bodyBlock)
521
+ val publicFlds = clsBody.publicFlds
522
+ val privateFlds = clsBody.nonMethods.collect:
523
+ case decl @ LetDecl (sym : TermSymbol , annotations) =>
524
+ reportAnnotations(decl, annotations)
525
+ sym
526
+ val ctor =
527
+ term_nonTail(Blk (clsBody.nonMethods, clsBody.blk.res))(ImplctRet )
528
+ // * This is just a minor improvement to get `constructor() {}` instead of `constructor() { null }`
529
+ .mapTail:
530
+ case Return (Value .Lit (syntax.Tree .UnitLit (true )), true ) => End ()
531
+ case t => t
532
+ (mtds, publicFlds, privateFlds, ctor)
533
+
534
+ inline def args (ts : Ls [st])(k : Ls [Arg ] => Block )(using Subst ): Block =
535
+ subTerms(ts)(asr => k(asr.map(Arg (false , _))))
536
+ inline def subTerms (ts : Ls [st])(k : Ls [Path ] => Block )(using Subst ): Block =
537
+ // @tailrec // TODO
538
+ def rec (as : Ls [st], asr : Ls [Path ]): Block = as match
539
+ case Nil => k(asr.reverse)
540
+ case a :: as =>
541
+ subTerm_nonTail(a): ar =>
542
+ rec(as, ar :: asr)
543
+ rec(ts, Nil )
544
+
523
545
def subTerm_nonTail (t : st, inStmtPos : Bool = false )(k : Path => Block )(using Subst ): Block =
524
546
subTerm(t : st, inStmtPos : Bool )(k)
525
547
@@ -591,7 +613,7 @@ trait LoweringSelSanityChecks
591
613
Pattern .Lit (syntax.Tree .UnitLit (false )),
592
614
Split .Else (
593
615
Term .Throw (Term .New (SynthSel (State .globalThisSymbol.ref(), Tree .Ident (" Error" ))(N ),
594
- Term .Lit (syntax.Tree .StrLit (s " Access to required field ' ${nme.name}' yielded 'undefined' " )) :: Nil )
616
+ Term .Lit (syntax.Tree .StrLit (s " Access to required field ' ${nme.name}' yielded 'undefined' " )) :: Nil , N )
595
617
))),
596
618
Split .Else (selRes.ref()))
597
619
Assign (
0 commit comments