|
1 | 1 | #lang racket/base |
2 | 2 | (require "contract-arrow.rkt" |
3 | | - racket/contract/private/guts |
4 | | - racket/contract/private/misc |
5 | | - racket/contract/private/prop |
6 | | - racket/private/class-internal |
7 | | - racket/private/class-c |
8 | | - racket/private/object-c |
| 3 | + (only-in racket/contract any/c coerce-contract) |
| 4 | + (only-in racket/class field) |
| 5 | + (prefix-in r/c: racket/class) |
9 | 6 | "contract-arr-checks.rkt") |
10 | 7 |
|
11 | 8 | (require (for-syntax racket/base |
|
15 | 12 |
|
16 | 13 | (define-syntax object-contract |
17 | 14 | (let () |
18 | | - (define (obj->/proc stx) (make-/proc #t ->/h stx)) |
19 | | - (define (obj->*/proc stx) (make-/proc #t ->*/h stx)) |
20 | | - (define (obj->d/proc stx) (make-/proc #t ->d/h stx)) |
21 | | - (define (obj->d*/proc stx) (make-/proc #t ->d*/h stx)) |
22 | | - (define (obj->r/proc stx) (make-/proc #t ->r/h stx)) |
23 | | - (define (obj->pp/proc stx) (make-/proc #t ->pp/h stx)) |
24 | | - (define (obj->pp-rest/proc stx) (make-/proc #t ->pp-rest/h stx)) |
25 | | - (define (obj-case->/proc stx) (make-case->/proc #t stx stx select/h)) |
| 15 | + (define ((obj->/proc mtd-name) stx) (make-/proc mtd-name ->/h stx)) |
| 16 | + (define ((obj->*/proc mtd-name) stx) (make-/proc mtd-name ->*/h stx)) |
| 17 | + (define ((obj->d/proc mtd-name) stx) (make-/proc mtd-name ->d/h stx)) |
| 18 | + (define ((obj->d*/proc mtd-name) stx) (make-/proc mtd-name ->d*/h stx)) |
| 19 | + (define ((obj->r/proc mtd-name) stx) (make-/proc mtd-name ->r/h stx)) |
| 20 | + (define ((obj->pp/proc mtd-name) stx) (make-/proc mtd-name ->pp/h stx)) |
| 21 | + (define ((obj->pp-rest/proc mtd-name) stx) (make-/proc mtd-name ->pp-rest/h stx)) |
| 22 | + (define (obj-case->/proc mtd-name stx) (make-case->/proc mtd-name stx stx select/h)) |
26 | 23 |
|
27 | 24 | ;; WARNING: select/h is copied from contract-arrow.rkt. I'm not sure how |
28 | 25 | ;; I can avoid this duplication -robby |
|
39 | 36 | [_ (raise-syntax-error err-name "malformed arrow clause" ctxt-stx stx)])) |
40 | 37 |
|
41 | 38 |
|
42 | | - (define (obj-opt->/proc stx) (make-opt->/proc #t stx select/h #'case-> #'->)) |
43 | | - (define (obj-opt->*/proc stx) (make-opt->*/proc #t stx stx select/h #'case-> #'->)) |
| 39 | + (define (obj-opt->/proc mtd-name stx) (make-opt->/proc mtd-name stx select/h #'case-> #'->)) |
| 40 | + (define (obj-opt->*/proc mtd-name stx) (make-opt->*/proc mtd-name stx stx select/h #'case-> #'->)) |
44 | 41 |
|
45 | 42 | (λ (stx) |
46 | 43 |
|
|
63 | 60 | (raise-syntax-error 'object-contract "expected name of field" stx (syntax field-name))] |
64 | 61 | [(mtd-name ctc) |
65 | 62 | (identifier? (syntax mtd-name)) |
66 | | - (let-values ([(ctc-stx proc-stx) (expand-mtd-contract (syntax ctc))]) |
| 63 | + (let-values ([(ctc-stx proc-stx) (expand-mtd-contract #'mtd-name (syntax ctc))]) |
67 | 64 | (make-mtd (syntax mtd-name) |
68 | 65 | ctc-stx |
69 | 66 | proc-stx))] |
|
72 | 69 | [_ (raise-syntax-error 'object-contract "expected field or method clause" stx f/m-stx)])) |
73 | 70 |
|
74 | 71 | ;; expand-mtd-contract : syntax -> (values syntax[expanded ctc] syntax[mtd-arg]) |
75 | | - (define (expand-mtd-contract mtd-stx) |
| 72 | + (define (expand-mtd-contract mtd-name mtd-stx) |
76 | 73 | (syntax-case mtd-stx (case-> opt-> opt->*) |
77 | 74 | [(case-> cases ...) |
78 | 75 | (let loop ([cases (syntax->list (syntax (cases ...)))] |
|
82 | 79 | [(null? cases) |
83 | 80 | (values |
84 | 81 | (with-syntax ([(x ...) (reverse ctc-stxs)]) |
85 | | - (obj-case->/proc (syntax (case-> x ...)))) |
| 82 | + (obj-case->/proc mtd-name (syntax (case-> x ...)))) |
86 | 83 | (with-syntax ([(x ...) (apply append (map syntax->list (reverse args-stxs)))]) |
87 | 84 | (syntax (x ...))))] |
88 | 85 | [else |
89 | | - (let-values ([(trans ctc-stx mtd-args) (expand-mtd-arrow (car cases))]) |
| 86 | + (let-values ([(trans ctc-stx mtd-args) (expand-mtd-arrow mtd-name (car cases))]) |
90 | 87 | (loop (cdr cases) |
91 | 88 | (cons ctc-stx ctc-stxs) |
92 | 89 | (cons mtd-args args-stxs)))]))] |
93 | 90 | [(opt->* (req-contracts ...) (opt-contracts ...) (res-contracts ...)) |
94 | 91 | (values |
95 | | - (obj-opt->*/proc (syntax (opt->* (any/c req-contracts ...) (opt-contracts ...) (res-contracts ...)))) |
| 92 | + (obj-opt->*/proc mtd-name (syntax (opt->* (any/c req-contracts ...) (opt-contracts ...) (res-contracts ...)))) |
96 | 93 | (generate-opt->vars (syntax (req-contracts ...)) |
97 | 94 | (syntax (opt-contracts ...))))] |
98 | 95 | [(opt->* (req-contracts ...) (opt-contracts ...) any) |
99 | 96 | (values |
100 | | - (obj-opt->*/proc (syntax (opt->* (any/c req-contracts ...) (opt-contracts ...) any))) |
| 97 | + (obj-opt->*/proc mtd-name (syntax (opt->* (any/c req-contracts ...) (opt-contracts ...) any))) |
101 | 98 | (generate-opt->vars (syntax (req-contracts ...)) |
102 | 99 | (syntax (opt-contracts ...))))] |
103 | 100 | [(opt-> (req-contracts ...) (opt-contracts ...) res-contract) |
104 | 101 | (values |
105 | | - (obj-opt->/proc (syntax (opt-> (any/c req-contracts ...) (opt-contracts ...) res-contract))) |
| 102 | + (obj-opt->/proc mtd-name (syntax (opt-> (any/c req-contracts ...) (opt-contracts ...) res-contract))) |
106 | 103 | (generate-opt->vars (syntax (req-contracts ...)) |
107 | 104 | (syntax (opt-contracts ...))))] |
108 | 105 | [else |
109 | | - (let-values ([(x y z) (expand-mtd-arrow mtd-stx)]) |
| 106 | + (let-values ([(x y z) (expand-mtd-arrow mtd-name mtd-stx)]) |
110 | 107 | (values (x y) z))])) |
111 | 108 |
|
112 | 109 | ;; generate-opt->vars : syntax[requried contracts] syntax[optional contracts] -> syntax[list of arg specs] |
|
122 | 119 | rests ...)))])))) |
123 | 120 |
|
124 | 121 | ;; expand-mtd-arrow : stx -> (values (syntax[ctc] -> syntax[expanded ctc]) syntax[ctc] syntax[mtd-arg]) |
125 | | - (define (expand-mtd-arrow mtd-stx) |
| 122 | + (define (expand-mtd-arrow mtd-name mtd-stx) |
126 | 123 | (syntax-case mtd-stx (-> ->* ->d ->d* ->r ->pp ->pp-rest) |
127 | 124 | [(->) (raise-syntax-error 'object-contract "-> must have arguments" stx mtd-stx)] |
128 | 125 | [(-> args ...) |
129 | 126 | ;; this case cheats a little bit -- |
130 | 127 | ;; (args ...) contains the right number of arguments |
131 | 128 | ;; to the method because it also contains one arg for the result! urgh. |
132 | 129 | (with-syntax ([(arg-vars ...) (generate-temporaries (syntax (args ...)))]) |
133 | | - (values obj->/proc |
134 | | - (syntax (-> any/c args ...)) |
| 130 | + (values (obj->/proc mtd-name) |
| 131 | + (remove-source-loc (syntax (-> any/c args ...))) |
135 | 132 | (syntax ((arg-vars ...)))))] |
136 | 133 | [(->* (doms ...) (rngs ...)) |
137 | 134 | (with-syntax ([(args-vars ...) (generate-temporaries (syntax (doms ...)))] |
138 | 135 | [(this-var) (generate-temporaries (syntax (this-var)))]) |
139 | | - (values obj->*/proc |
140 | | - (syntax (->* (any/c doms ...) (rngs ...))) |
| 136 | + (values (obj->*/proc mtd-name) |
| 137 | + (remove-source-loc (syntax (->* (any/c doms ...) (rngs ...)))) |
141 | 138 | (syntax ((this-var args-vars ...)))))] |
142 | 139 | [(->* (doms ...) rst (rngs ...)) |
143 | 140 | (with-syntax ([(args-vars ...) (generate-temporaries (syntax (doms ...)))] |
144 | 141 | [(rst-var) (generate-temporaries (syntax (rst)))] |
145 | 142 | [(this-var) (generate-temporaries (syntax (this-var)))]) |
146 | | - (values obj->*/proc |
147 | | - (syntax (->* (any/c doms ...) rst (rngs ...))) |
| 143 | + (values (obj->*/proc mtd-name) |
| 144 | + (remove-source-loc (syntax (->* (any/c doms ...) rst (rngs ...)))) |
148 | 145 | (syntax ((this-var args-vars ... . rst-var)))))] |
149 | 146 | [(->* x ...) |
150 | 147 | (raise-syntax-error 'object-contract "malformed ->*" stx mtd-stx)] |
151 | 148 | [(->d) (raise-syntax-error 'object-contract "->d must have arguments" stx mtd-stx)] |
152 | 149 | [(->d doms ... rng-proc) |
153 | 150 | (let ([doms-val (syntax->list (syntax (doms ...)))]) |
154 | 151 | (values |
155 | | - obj->d/proc |
| 152 | + (obj->d/proc mtd-name) |
156 | 153 | (with-syntax ([(arg-vars ...) (generate-temporaries doms-val)] |
157 | 154 | [arity-count (length doms-val)]) |
158 | | - (syntax |
159 | | - (->d any/c doms ... |
160 | | - (let ([f rng-proc]) |
161 | | - (check->* f arity-count) |
162 | | - (lambda (_this-var arg-vars ...) |
163 | | - (f arg-vars ...)))))) |
| 155 | + (remove-source-loc |
| 156 | + #`(->d any/c doms ... |
| 157 | + (let ([f rng-proc]) |
| 158 | + (check->* f arity-count) |
| 159 | + #,(syntax-property |
| 160 | + #'(lambda (_this-var arg-vars ...) |
| 161 | + (f arg-vars ...)) |
| 162 | + 'method-arity-error |
| 163 | + #t))))) |
164 | 164 | (with-syntax ([(args-vars ...) (generate-temporaries doms-val)]) |
165 | 165 | (syntax ((this-var args-vars ...))))))] |
166 | 166 | [(->d* (doms ...) rng-proc) |
167 | 167 | (values |
168 | | - obj->d*/proc |
| 168 | + (obj->d*/proc mtd-name) |
169 | 169 | (let ([doms-val (syntax->list (syntax (doms ...)))]) |
170 | 170 | (with-syntax ([(arg-vars ...) (generate-temporaries doms-val)] |
171 | 171 | [arity-count (length doms-val)]) |
172 | | - (syntax (->d* (any/c doms ...) |
173 | | - (let ([f rng-proc]) |
174 | | - (check->* f arity-count) |
175 | | - (lambda (_this-var arg-vars ...) |
176 | | - (f arg-vars ...))))))) |
| 172 | + (remove-source-loc |
| 173 | + #`(->d* (any/c doms ...) |
| 174 | + (let ([f rng-proc]) |
| 175 | + (check->* f arity-count) |
| 176 | + #,(syntax-property |
| 177 | + #'(lambda (_this-var arg-vars ...) |
| 178 | + (f arg-vars ...)) |
| 179 | + 'method-arity-error |
| 180 | + #t)))))) |
177 | 181 | (with-syntax ([(args-vars ...) (generate-temporaries (syntax (doms ...)))] |
178 | 182 | [(this-var) (generate-temporaries (syntax (this-var)))]) |
179 | 183 | (syntax ((this-var args-vars ...)))))] |
180 | 184 | [(->d* (doms ...) rst-ctc rng-proc) |
181 | 185 | (let ([doms-val (syntax->list (syntax (doms ...)))]) |
182 | 186 | (values |
183 | | - obj->d*/proc |
| 187 | + (obj->d*/proc mtd-name) |
184 | 188 | (with-syntax ([(arg-vars ...) (generate-temporaries doms-val)] |
185 | 189 | [(rest-var) (generate-temporaries (syntax (rst-ctc)))] |
186 | 190 | [arity-count (length doms-val)]) |
187 | | - (syntax (->d* (any/c doms ...) |
188 | | - rst-ctc |
189 | | - (let ([f rng-proc]) |
190 | | - (check->*/more f arity-count) |
191 | | - (lambda (_this-var arg-vars ... . rest-var) |
192 | | - (apply f arg-vars ... rest-var)))))) |
| 191 | + (remove-source-loc |
| 192 | + #`(->d* (any/c doms ...) |
| 193 | + rst-ctc |
| 194 | + (let ([f rng-proc]) |
| 195 | + (check->*/more f arity-count) |
| 196 | + #,(syntax-property |
| 197 | + #'(lambda (_this-var arg-vars ... . rest-var) |
| 198 | + (apply f arg-vars ... rest-var)) |
| 199 | + 'method-arity-error |
| 200 | + #t))))) |
193 | 201 | (with-syntax ([(args-vars ...) (generate-temporaries (syntax (doms ...)))] |
194 | 202 | [(rst-var) (generate-temporaries (syntax (rst-ctc)))] |
195 | 203 | [(this-var) (generate-temporaries (syntax (this-var)))]) |
|
203 | 211 | [(this-var) (generate-temporaries (syntax (this-var)))] |
204 | 212 | [this (datum->syntax mtd-stx 'this)]) |
205 | 213 | (values |
206 | | - obj->r/proc |
207 | | - (syntax (->r ([this any/c] [x dom] ...) rng)) |
| 214 | + (obj->r/proc mtd-name) |
| 215 | + (remove-source-loc (syntax (->r ([this any/c] [x dom] ...) rng))) |
208 | 216 | (syntax ((this-var arg-vars ...)))))] |
209 | 217 |
|
210 | 218 | [(->r ([x dom] ...) rest-x rest-dom rng) |
|
213 | 221 | [(this-var) (generate-temporaries (syntax (this-var)))] |
214 | 222 | [this (datum->syntax mtd-stx 'this)]) |
215 | 223 | (values |
216 | | - obj->r/proc |
217 | | - (syntax (->r ([this any/c] [x dom] ...) rest-x rest-dom rng)) |
| 224 | + (obj->r/proc mtd-name) |
| 225 | + (remove-source-loc (syntax (->r ([this any/c] [x dom] ...) rest-x rest-dom rng))) |
218 | 226 | (syntax ((this-var arg-vars ... . rest-var)))))] |
219 | 227 |
|
220 | 228 | [(->r . x) |
|
225 | 233 | [(this-var) (generate-temporaries (syntax (this-var)))] |
226 | 234 | [this (datum->syntax mtd-stx 'this)]) |
227 | 235 | (values |
228 | | - obj->pp/proc |
229 | | - (syntax (->pp ([this any/c] [x dom] ...) . other-stuff)) |
| 236 | + (obj->pp/proc mtd-name) |
| 237 | + (remove-source-loc (syntax (->pp ([this any/c] [x dom] ...) . other-stuff))) |
230 | 238 | (syntax ((this-var arg-vars ...)))))] |
231 | 239 | [(->pp . x) |
232 | 240 | (raise-syntax-error 'object-contract "malformed ->pp declaration")] |
|
237 | 245 | [(this-var) (generate-temporaries (syntax (this-var)))] |
238 | 246 | [this (datum->syntax mtd-stx 'this)]) |
239 | 247 | (values |
240 | | - obj->pp-rest/proc |
241 | | - (syntax (->pp ([this any/c] [x dom] ...) rest-id . other-stuff)) |
| 248 | + (obj->pp-rest/proc mtd-name) |
| 249 | + (remove-source-loc (syntax (->pp ([this any/c] [x dom] ...) rest-id . other-stuff))) |
242 | 250 | (syntax ((this-var arg-vars ... . rest-id)))))] |
243 | 251 | [(->pp-rest . x) |
244 | 252 | (raise-syntax-error 'object-contract "malformed ->pp-rest declaration")] |
245 | 253 | [else (raise-syntax-error 'object-contract "unknown method contract syntax" stx mtd-stx)])) |
| 254 | + |
| 255 | + (define (remove-source-loc stx) |
| 256 | + (datum->syntax stx |
| 257 | + (syntax-e stx) |
| 258 | + #f |
| 259 | + stx)) |
246 | 260 |
|
247 | 261 | (define (syntax->improper-list stx) |
248 | 262 | (define (se->il se) |
|
275 | 289 | [(field-name ...) (map fld-name flds)] |
276 | 290 | [(field-ctc-var ...) (generate-temporaries flds)]) |
277 | 291 | (quasisyntax |
278 | | - (let ([method-ctc-var method-ctc-stx] |
279 | | - ... |
280 | | - [field-ctc-var (coerce-contract 'object-contract field-ctc-stx)] |
281 | | - ...) |
282 | | - (make-object-contract (list 'method-name ...) |
283 | | - (list method-ctc-var ...) |
284 | | - #,(make-object/c-method-proc-stx (map mtd-name mtds) |
285 | | - (syntax->list #'(method-ctc-var ...))) |
286 | | - (list 'field-name ...) |
287 | | - (list field-ctc-var ...) |
288 | | - #f ;; opaque-methods |
289 | | - #f ;; opaque-fields |
290 | | - #f ;; do-not-check-class-field-accessor-or-mutator-access? |
291 | | - )))))])))) |
292 | | - |
293 | | - |
| 292 | + (let ([method-ctc-var method-ctc-stx] ... |
| 293 | + [field-ctc-var (coerce-contract 'object-contract field-ctc-stx)] ...) |
| 294 | + (r/c:object/c |
| 295 | + (method-name method-ctc-var) ... |
| 296 | + (field [field-name field-ctc-var] ...))))))])))) |
0 commit comments