blob: 38acf81623af32d92397b923508ce25f46afdf91 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
;; Copyright (c) Eduardo Julian. All rights reserved.
;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
;; If a copy of the MPL was not distributed with this file,
;; You can obtain one at http://mozilla.org/MPL/2.0/.
(ns lux.type.host
(:require clojure.core.match
clojure.core.match.array
(lux [base :as & :refer [|do return* return fail fail* assert! |let |case]]))
(:import (java.lang.reflect GenericArrayType
ParameterizedType
TypeVariable
WildcardType)))
;; [Exports]
(def array-data-tag "#Array")
(def null-data-tag "#Null")
;; [Utils]
(defn ^:private trace-lineage* [^Class super-class ^Class sub-class]
"(-> Class Class (List Class))"
;; Either they're both interfaces, of they're both classes
(cond (.isInterface sub-class)
(let [interface<=interface? #(if (or (= super-class %)
(.isAssignableFrom super-class %))
%
nil)]
(loop [sub-class sub-class
stack (&/|list)]
(let [super-interface (some interface<=interface?
(.getInterfaces sub-class))]
(if (= super-class super-interface)
(&/Cons$ super-interface stack)
(let [super* (.getSuperclass sub-class)]
(recur super* (&/Cons$ super* stack)))))))
(.isInterface super-class)
(let [class<=interface? #(if (= super-class %) % nil)]
(loop [sub-class sub-class
stack (&/|list)]
(if-let [super-interface (some class<=interface? (.getInterfaces sub-class))]
(&/Cons$ super-interface stack)
(let [super* (.getSuperclass sub-class)]
(recur super* (&/Cons$ super* stack))))))
:else
(loop [sub-class sub-class
stack (&/|list)]
(let [super* (.getSuperclass sub-class)]
(if (= super* super-class)
(&/Cons$ super* stack)
(recur super* (&/Cons$ super* stack)))))))
(defn ^:private trace-lineage [^Class sub-class ^Class super-class]
"(-> Class Class (List Class))"
(if (= sub-class super-class)
(&/|list)
(&/|reverse (trace-lineage* super-class sub-class))))
(let [matcher (fn [m ^TypeVariable jt lt] (&/Cons$ (&/T (.getName jt) lt) m))]
(defn ^:private match-params [sub-type-params params]
(assert (and (= (&/|length sub-type-params) (&/|length params))
(&/|every? (partial instance? TypeVariable) sub-type-params)))
(&/fold2 matcher (&/|table) sub-type-params params)))
;; [Exports]
(let [class-name-re #"((\[+)L([\.a-zA-Z0-9]+);|([\.a-zA-Z0-9]+))"
Unit (&/V &/$TupleT (&/|list))]
(defn class->type [^Class class]
"(-> Class Type)"
(if-let [[_ _ arr-brackets arr-base simple-base] (re-find class-name-re (.getName class))]
(let [base (or arr-base simple-base)]
(if (.equals "void" base)
Unit
(reduce (fn [inner _] (&/V &/$DataT (&/T array-data-tag (&/|list inner))))
(&/V &/$DataT (&/T base &/Nil$))
(range (count (or arr-brackets "")))))
))))
(defn instance-param [existential matchings refl-type]
"(-> (List (, Text Type)) (^ java.lang.reflect.Type) (Lux Type))"
(cond (instance? Class refl-type)
(return (class->type refl-type))
(instance? GenericArrayType refl-type)
(let [inner-type (instance-param existential matchings (.getGenericComponentType ^GenericArrayType refl-type))]
(return (&/V &/$DataT (&/T array-data-tag (&/|list inner-type)))))
(instance? ParameterizedType refl-type)
(|do [:let [refl-type* ^ParameterizedType refl-type]
params* (->> refl-type*
.getActualTypeArguments
seq &/->list
(&/map% (partial instance-param existential matchings)))]
(return (&/V &/$DataT (&/T (->> refl-type* ^Class (.getRawType) .getName)
params*))))
(instance? TypeVariable refl-type)
(let [gvar (.getName ^TypeVariable refl-type)]
(if-let [m-type (&/|get gvar matchings)]
(return m-type)
(fail (str "[Type Error] Unknown generic type variable: " gvar))))
(instance? WildcardType refl-type)
(if-let [bound (->> ^WildcardType refl-type .getUpperBounds seq first)]
(instance-param existential matchings bound)
existential)))
;; [Utils]
(defn ^:private translate-params [existential super-type-params sub-type-params params]
"(-> (List (^ java.lang.reflect.Type)) (List (^ java.lang.reflect.Type)) (List Type) (Lux (List Type)))"
(|let [matchings (match-params sub-type-params params)]
(&/map% (partial instance-param existential matchings) super-type-params)))
(defn ^:private raise* [existential sub+params ^Class super]
"(-> (, Class (List Type)) Class (Lux (, Class (List Type))))"
(|let [[^Class sub params] sub+params]
(if (.isInterface super)
(|do [:let [super-params (->> sub
.getGenericInterfaces
(some #(if (= super (if (instance? Class %) % (.getRawType ^ParameterizedType %)))
(if (instance? Class %)
(&/|list)
(->> ^ParameterizedType % .getActualTypeArguments seq &/->list))
nil)))]
params* (translate-params existential
super-params
(->> sub .getTypeParameters seq &/->list)
params)]
(return (&/T super params*)))
(let [super* (.getGenericSuperclass sub)]
(cond (instance? Class super*)
(return (&/T super* (&/|list)))
(instance? ParameterizedType super*)
(|do [params* (translate-params existential
(->> ^ParameterizedType super* .getActualTypeArguments seq &/->list)
(->> sub .getTypeParameters seq &/->list)
params)]
(return (&/T super params*)))
:else
(assert false (prn-str super* (class super*) [sub super])))))))
(defn ^:private raise [existential lineage class params]
"(-> (List Class) Class (List Type) (Lux (, Class (List Type))))"
(&/fold% (partial raise* existential) (&/T class params) lineage))
;; [Exports]
(defn ->super-type [existential class-loader super-class sub-class sub-params]
"(-> Text Text (List Type) (Lux Type))"
(let [super-class+ (Class/forName super-class true class-loader)
sub-class+ (Class/forName sub-class true class-loader)]
(if (.isAssignableFrom super-class+ sub-class+)
(let [lineage (trace-lineage sub-class+ super-class+)]
(|do [[^Class sub-class* sub-params*] (raise existential lineage sub-class+ sub-params)]
(return (&/V &/$DataT (&/T (.getName sub-class*) sub-params*)))))
(fail (str "[Type Error] Classes don't have a subtyping relationship: " sub-class " </= " super-class)))))
(defn as-obj [class]
(case class
"boolean" "java.lang.Boolean"
"byte" "java.lang.Byte"
"short" "java.lang.Short"
"int" "java.lang.Integer"
"long" "java.lang.Long"
"float" "java.lang.Float"
"double" "java.lang.Double"
"char" "java.lang.Character"
;; else
class))
(let [primitive-types #{"boolean" "byte" "short" "int" "long" "float" "double" "char"}]
(defn primitive-type? [type-name]
(contains? primitive-types type-name)))
(defn check-host-types [check check-error fixpoints existential class-loader invariant?? expected actual]
(|let [[e!name e!params] expected
[a!name a!params] actual]
(cond (= "java.lang.Object" e!name)
(return (&/T fixpoints nil))
(= null-data-tag a!name)
(if (not (primitive-type? e!name))
(return (&/T fixpoints nil))
(fail (check-error (&/V &/$DataT expected) (&/V &/$DataT actual))))
(= null-data-tag e!name)
(if (= null-data-tag a!name)
(return (&/T fixpoints nil))
(fail (check-error (&/V &/$DataT expected) (&/V &/$DataT actual))))
(and (= array-data-tag e!name)
(not= array-data-tag a!name))
(fail (check-error (&/V &/$DataT expected) (&/V &/$DataT actual)))
:else
(let [e!name (as-obj e!name)
a!name (as-obj a!name)]
(cond (.equals ^Object e!name a!name)
(if (= (&/|length e!params) (&/|length a!params))
(|do [_ (&/map2% check e!params a!params)]
(return (&/T fixpoints nil)))
(fail (str "[Type Error] Amounts of generic parameters don't match: " e!name "(" (&/|length e!params) ")" " vs " a!name "(" (&/|length a!params) ")")))
(not invariant??)
(|do [actual* (->super-type existential class-loader e!name a!name a!params)]
(check (&/V &/$DataT expected) actual*))
:else
(fail (str "[Type Error] Names don't match: " e!name " =/= " a!name)))))))
(let [Void$ (&/V &/$VariantT (&/|list))
gen-type (constantly Void$)]
(defn dummy-gtype [class]
(|do [class-loader &/loader]
(try (|let [=class (Class/forName class true class-loader)
params (->> =class .getTypeParameters seq &/->list (&/|map gen-type))]
(return (&/V &/$DataT (&/T class params))))
(catch Exception e
(fail (str "[Type Error] Unknown type: " class)))))))
|