@@ -5230,6 +5230,19 @@ export class Compiler extends DiagnosticEmitter {
5230
5230
break ;
5231
5231
}
5232
5232
5233
+ // call to `super()`
5234
+ case ElementKind . CLASS : {
5235
+ if ( expression . expression . kind == NodeKind . SUPER ) {
5236
+ let classInstance = assert ( currentFunction . parent ) ;
5237
+ assert ( classInstance . kind == ElementKind . CLASS ) ;
5238
+ let expr = this . compileSuperInstantiate ( < Class > classInstance , expression . arguments , expression ) ;
5239
+ this . currentType = Type . void ;
5240
+ let thisLocal = assert ( this . currentFunction . flow . getScopedLocal ( "this" ) ) ;
5241
+ return module . createSetLocal ( thisLocal . index , expr ) ;
5242
+ }
5243
+ // otherwise fall-through
5244
+ }
5245
+
5233
5246
// not supported
5234
5247
default : {
5235
5248
this . error (
@@ -6658,6 +6671,37 @@ export class Compiler extends DiagnosticEmitter {
6658
6671
return expr ;
6659
6672
}
6660
6673
6674
+ compileSuperInstantiate ( classInstance : Class , argumentExpressions : Expression [ ] , reportNode : Node ) : ExpressionRef {
6675
+ // traverse to the top-most visible constructor (except the current one)
6676
+ var currentClassInstance : Class | null = classInstance . base ;
6677
+ var constructorInstance : Function | null = null ;
6678
+ while ( currentClassInstance ) {
6679
+ constructorInstance = currentClassInstance . constructorInstance ;
6680
+ if ( constructorInstance ) break ; // TODO: check visibility
6681
+ currentClassInstance = currentClassInstance . base ;
6682
+ }
6683
+
6684
+ // if a constructor is present, allocate the necessary memory for `this` and call it
6685
+ var expr : ExpressionRef ;
6686
+ if ( constructorInstance ) {
6687
+ expr = this . compileCallDirect ( constructorInstance , argumentExpressions , reportNode ,
6688
+ this . makeAllocate ( classInstance , reportNode )
6689
+ ) ;
6690
+
6691
+ // otherwise simply allocate a new instance and initialize its fields
6692
+ } else {
6693
+ if ( argumentExpressions . length ) {
6694
+ this . error (
6695
+ DiagnosticCode . Expected_0_arguments_but_got_1 ,
6696
+ reportNode . range , "0" , argumentExpressions . length . toString ( 10 )
6697
+ ) ;
6698
+ }
6699
+ expr = this . makeAllocate ( classInstance , reportNode ) ;
6700
+ }
6701
+ this . currentType = classInstance . type ;
6702
+ return expr ;
6703
+ }
6704
+
6661
6705
compileParenthesizedExpression (
6662
6706
expression : ParenthesizedExpression ,
6663
6707
contextualType : Type
0 commit comments