Debian Codegraph @

src/compile-t.scm

raw
1 ;;;
2 ;;; compile-t.scm - Type handling during compilation
3 ;;;
4 ;;; Copyright (c) 2021-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Descriptive types should be created during compilation.
35
36 ;; These identifiers are recognized in type constructor expressions.
37 ;; (We don't use the convention adding '.' after the name, for we have
38 ; a procedure named '*.'.
39 (define id:-> (global-id '->))
40 (define id:* (global-id '*))
41
42 (define-in-module gauche -> (undefined)) ; need to be bound for identifier match
43
44 ;; Ensure the result of pass1 is a type, or returns #f.
45 ;; Called from pass1/type-expression to compile type expression.
46 ;; Always returns a ($const TYPE).
47 ;; Classes are wrapped with proxy-type.
48 (define (type/ensure iform cenv)
49 (or (and-let* ([ ($const? iform) ]
50 [v ($const-value iform) ]
51 [ (is-a? v <type>) ])
52 v)
53 (and-let* ([ (has-tag? iform $GREF) ]
54 [gloc (gref-inlinable-gloc iform)]
55 [v (gloc-ref gloc)])
56 (cond [(is-a? v <class>) (wrap-with-proxy-type ($gref-id iform) gloc)]
57 [(is-a? v <type>) v]
58 [else #f]))))
59
60 ;; Called from pass1/global-call, when we detect (<type-ctor> arg ...)
61 ;; CTOR is the gloval value of type constructor, IFORM is the $CALL node
62 ;; represents the ctor invocation.
63 ;; Since arguments for the type constructor have already gone through pass1,
64 ;; constant variable reference and type constructor calls are already
65 ;; handled.
66
67 (define (type/construct ctor iform cenv)
68 (define (get-arg-value arg)
69 (cond [($const? arg) ($const-value arg)]
70 [(has-tag? arg $GREF)
71 ;; We recognize some "reserved keywords".
72 (cond [(global-identifier=? ($gref-id arg) id:*) '*]
73 [(global-identifier=? ($gref-id arg) id:->) '->]
74 [(gref-inlinable-gloc arg)
75 => (^[gloc]
76 (let1 v (gloc-ref gloc)
77 (if (is-a? v <class>)
78 (wrap-with-proxy-type ($gref-id arg) gloc)
79 v)))]
80 [else
81 (errorf "Can't use non-inlinable global variable `~s' in \
82 type constructor expression: ~s"
83 ($gref-id arg)
84 ($*-src iform))])]
85 [else
86 ;; we can run constant folding here, but for the time being...
87 (error "Arguments of type constructor expression must be \
88 a compile-time constant:" ($*-src iform))]))
89 (define (check-arg-value val)
90 ;; For now, we restrict type ctor arguments to simple values
91 (unless (or (is-a? val <type>)
92 (number? val)
93 (boolean? val)
94 (string? val)
95 (symbol? val))
96 (error "Invalid value as type constructor argument:" val))
97 val)
98
99 ;; Call type constructor
100 (let1 type ($ construct-type ctor
101 (map ($ check-arg-value $ get-arg-value $) ($call-args iform)))
102 (unless (is-a? type <descriptive-type>)
103 (errorf "Type costructor ~s returned an object other than a \
104 type instance: ~s"
105 ($*-src iform) type))
106 ($const type)))