|
791 | 791 | (define (num-non-varargs args)
|
792 | 792 | (count (lambda (a) (not (vararg? a))) args))
|
793 | 793 |
|
794 |
| -(define (new-call Tname type-params sparams params args field-names field-types) |
| 794 | +;; selftype?: tells us whether the called object is the type being constructed, |
| 795 | +;; i.e. `new()` and not `new{...}()`. |
| 796 | +(define (new-call Tname type-params sparams params args field-names field-types selftype?) |
795 | 797 | (if (any kwarg? args)
|
796 | 798 | (error "\"new\" does not accept keyword arguments"))
|
797 | 799 | (let ((nnv (num-non-varargs type-params)))
|
|
801 | 803 | (error "too many type parameters specified in \"new{...}\"")))
|
802 | 804 | (let* ((Texpr (if (null? type-params)
|
803 | 805 | `(outerref ,Tname)
|
804 |
| - `(curly (outerref ,Tname) |
805 |
| - ,@type-params))) |
806 |
| - (tn (make-ssavalue)) |
| 806 | + (if selftype? |
| 807 | + '|#self#| |
| 808 | + `(curly (outerref ,Tname) |
| 809 | + ,@type-params)))) |
| 810 | + (tn (if (symbol? Texpr) Texpr (make-ssavalue))) |
807 | 811 | (field-convert (lambda (fld fty val)
|
808 | 812 | (if (equal? fty '(core Any))
|
809 | 813 | val
|
|
823 | 827 | (let ((argt (make-ssavalue))
|
824 | 828 | (nf (make-ssavalue)))
|
825 | 829 | `(block
|
826 |
| - (= ,tn ,Texpr) |
| 830 | + ,@(if (symbol? tn) '() `((= ,tn ,Texpr))) |
827 | 831 | (= ,argt (call (core tuple) ,@args))
|
828 | 832 | (= ,nf (call (core nfields) ,argt))
|
829 | 833 | (if (call (top ult_int) ,nf ,(length field-names))
|
|
835 | 839 | (new ,tn ,@(map (lambda (fld fty) (field-convert fld fty `(call (core getfield) ,argt ,(+ fld 1) (false))))
|
836 | 840 | (iota (length field-names)) (list-head field-types (length field-names))))))))
|
837 | 841 | (else
|
838 |
| - `(block |
839 |
| - (= ,tn ,Texpr) |
840 |
| - (new ,tn ,@(map field-convert (iota (length args)) (list-head field-types (length args)) args))))))) |
| 842 | + `(block |
| 843 | + ,@(if (symbol? tn) '() `((= ,tn ,Texpr))) |
| 844 | + (new ,tn ,@(map field-convert (iota (length args)) (list-head field-types (length args)) args))))))) |
841 | 845 |
|
842 | 846 | ;; insert item at start of arglist
|
843 | 847 | (define (arglist-unshift sig item)
|
|
881 | 885 | (call (-/ new) . args)
|
882 | 886 | (new-call Tname type-params sparams params
|
883 | 887 | (map (lambda (a) (ctor-body a type-params sparams)) args)
|
884 |
| - field-names field-types)) |
| 888 | + field-names field-types #t)) |
885 | 889 | (pattern-lambda
|
886 | 890 | (call (curly (-/ new) . p) . args)
|
887 | 891 | (new-call Tname p sparams params
|
888 | 892 | (map (lambda (a) (ctor-body a type-params sparams)) args)
|
889 |
| - field-names field-types))) |
| 893 | + field-names field-types #f))) |
890 | 894 | body))
|
891 | 895 | (pattern-replace
|
892 | 896 | (pattern-set
|
|
0 commit comments