/
infer_tag.clj
286 lines (253 loc) · 9.78 KB
/
infer_tag.clj
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
;; Copyright (c) Nicola Mometto, Rich Hickey & contributors.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns clojure.tools.analyzer.passes.jvm.infer-tag
(:require [clojure.tools.analyzer.utils :refer [arglist-for-arity]]
[clojure.tools.analyzer.jvm.utils :as u]
[clojure.tools.analyzer.env :as env]
[clojure.set :refer [rename-keys]]
[clojure.tools.analyzer.passes.jvm
[annotate-tag :refer [annotate-tag]]
[annotate-methods :refer [annotate-methods]]
[analyze-host-expr :refer [analyze-host-expr]]
[fix-case-test :refer [fix-case-test]]]))
(defmulti -infer-tag :op)
(defmethod -infer-tag :default [ast] ast)
(defmethod -infer-tag :binding
[{:keys [init atom] :as ast}]
(if init
(let [info (select-keys init [:return-tag :arglists])]
(swap! atom merge info)
(merge ast info))
ast))
(defmethod -infer-tag :local
[ast]
(let [atom @(:atom ast)]
(merge atom
ast
{:o-tag (:tag atom)})))
(defmethod -infer-tag :var
[{:keys [var form] :as ast}]
(let [{:keys [tag arglists]} (meta var)
arglists (if (= 'quote (first arglists))
(second arglists)
arglists)
form-tag (:tag (meta form))]
;;if (not dynamic)
(merge ast
{:o-tag Object}
(when-let [tag (or form-tag tag)]
(if (fn? @var)
{:tag clojure.lang.AFunction :return-tag tag}
{:tag tag}))
(when arglists
{:arglists arglists}))))
(defmethod -infer-tag :def
[{:keys [var init name] :as ast}]
(let [info (merge (select-keys init [:return-tag :arglists :tag])
(select-keys (meta name) [:tag :arglists]))]
(when (and (seq info)
(not (:dynamic (meta name)))
(= :global (-> (env/deref-env) :passes-opts :infer-tag/level)))
(alter-meta! var merge (rename-keys info {:return-tag :tag})))
(merge ast info {:tag clojure.lang.Var :o-tag clojure.lang.Var})))
(defmethod -infer-tag :quote
[ast]
(let [tag (-> ast :expr :tag)]
(assoc ast :tag tag :o-tag tag)))
(defmethod -infer-tag :new
[ast]
(let [t (-> ast :class :val)]
(assoc ast :o-tag t :tag t)))
(defmethod -infer-tag :with-meta
[{:keys [expr] :as ast}]
(merge ast (select-keys expr [:return-tag :arglists])
{:tag (or (:tag expr) Object) :o-tag Object})) ;;trying to be smart here
(defmethod -infer-tag :recur
[ast]
(assoc ast :ignore-tag true))
(defmethod -infer-tag :do
[{:keys [ret] :as ast}]
(merge ast (select-keys ret [:return-tag :arglists :ignore-tag :tag])
{:o-tag (:tag ret)}))
(defmethod -infer-tag :let
[{:keys [body] :as ast}]
(merge ast (select-keys body [:return-tag :arglists :ignore-tag :tag])
{:o-tag (:tag body)}))
(defmethod -infer-tag :letfn
[{:keys [body] :as ast}]
(merge ast (select-keys body [:return-tag :arglists :ignore-tag :tag])
{:o-tag (:tag body)}))
(defmethod -infer-tag :loop
[{:keys [body] :as ast}]
(merge ast (select-keys body [:return-tag :arglists])
{:o-tag (:tag body)}
(let [tag (:tag body)]
(if (#{Void Void/TYPE} tag)
(assoc ast :tag Object)
(assoc ast :tag tag)))))
(defn =-arglists? [a1 a2]
(let [tag (fn [x] (-> x meta :tag u/maybe-class))]
(and (= a1 a2)
(every? true? (mapv (fn [a1 a2]
(and (= (tag a1) (tag a2))
(= (mapv tag a1)
(mapv tag a2))))
a1 a2)))))
(defmethod -infer-tag :if
[{:keys [then else] :as ast}]
(let [then-tag (:tag then)
else-tag (:tag else)
ignore-then? (:ignore-tag then)
ignore-else? (:ignore-tag else)]
(cond
(and then-tag
(or ignore-else? (= then-tag else-tag)))
(merge ast
{:tag then-tag :o-tag then-tag}
(when-let [return-tag (:return-tag then)]
(when (or ignore-else?
(= return-tag (:return-tag else)))
{:return-tag return-tag}))
(when-let [arglists (:arglists then)]
(when (or ignore-else?
(=-arglists? arglists (:arglists else)))
{:arglists arglists})))
(and else-tag ignore-then?)
(merge ast
{:tag else-tag :o-tag else-tag}
(when-let [return-tag (:return-tag else)]
{:return-tag return-tag})
(when-let [arglists (:arglists else)]
{:arglists arglists}))
(and (:ignore-tag then) (:ignore-tag else))
(assoc ast :ignore-tag true)
:else
ast)))
(defmethod -infer-tag :throw
[ast]
(assoc ast :ignore-tag true))
(defmethod -infer-tag :case
[{:keys [thens default] :as ast}]
(let [thens (conj (mapv :then thens) default)
exprs (seq (remove :ignore-tag thens))
tag (:tag (first exprs))]
(cond
(and tag
(every? #(= (:tag %) tag) exprs))
(merge ast
{:tag tag :o-tag tag}
(when-let [return-tag (:return-tag (first exprs))]
(when (every? #(= (:return-tag %) return-tag) exprs)
{:return-tag return-tag}))
(when-let [arglists (:arglists (first exprs))]
(when (every? #(=-arglists? (:arglists %) arglists) exprs)
{:arglists arglists})))
(every? :ignore-tag thens)
(assoc ast :ignore-tag true)
:else
ast)))
(defmethod -infer-tag :try
[{:keys [body catches] :as ast}]
(let [{:keys [tag return-tag arglists]} body
catches (remove :ignore-tag (mapv :body catches))]
(merge ast
(when (and tag (every? #(= % tag) (mapv :tag catches)))
{:tag tag :o-tag tag})
(when (and return-tag (every? #(= % return-tag) (mapv :return-tag catches)))
{:return-tag return-tag})
(when (and arglists (every? #(=-arglists? % arglists) (mapv :arglists catches)))
{:arglists arglists}))))
(defmethod -infer-tag :fn-method
[{:keys [form body params local] :as ast}]
(let [annotated-tag (or (:tag (meta (first form)))
(:tag (meta (:form local))))
body-tag (:tag body)
tag (or annotated-tag body-tag)
tag (if (#{Void Void/TYPE} tag)
Object
tag)]
(merge (if (not= tag body-tag)
(assoc-in ast [:body :tag] (u/maybe-class tag))
ast)
(when tag
{:tag tag
:o-tag tag})
{:arglist (with-meta (vec (mapcat (fn [{:keys [form variadic?]}]
(if variadic? ['& form] [form]))
params))
(when tag {:tag tag}))})))
(defmethod -infer-tag :fn
[{:keys [local methods] :as ast}]
(merge ast
{:arglists (seq (mapv :arglist methods))
:tag clojure.lang.AFunction
:o-tag clojure.lang.AFunction}
(when-let [tag (or (:tag (meta (:form local)))
(and (apply = (mapv :tag methods))
(:tag (first methods))))]
{:return-tag tag})))
(defmethod -infer-tag :invoke
[{:keys [fn args] :as ast}]
(if (:arglists fn)
(let [argc (count args)
arglist (arglist-for-arity fn argc)
tag (or (:tag (meta arglist))
(:return-tag fn)
(and (= :var (:op fn))
(:tag (meta (:var fn)))))]
(merge ast
(when tag
{:tag tag
:o-tag tag})))
(if-let [tag (:return-tag fn)]
(assoc ast :tag tag :o-tag tag)
ast)))
(defmethod -infer-tag :method
[{:keys [form body params] :as ast}]
(let [tag (or (:tag (meta (first form)))
(:tag (meta (second form))))
body-tag (:tag body)]
(assoc ast :tag (or tag body-tag) :o-tag body-tag)))
(defmethod -infer-tag :reify
[{:keys [class-name] :as ast}]
(assoc ast :tag class-name :o-tag class-name))
(defmethod -infer-tag :set!
[ast]
(let [t (:tag (:target ast))]
(assoc ast :tag t :o-tag t)))
(defn infer-tag
"Performs local type inference on the AST adds, when possible,
one or more of the following keys to the AST:
* :o-tag represents the current type of the
expression represented by the node
* :tag represents the type the expression represented by the
node is required to have, possibly the same as :o-tag
* :return-tag implies that the node will return a function whose
invocation will result in a object of this type
* :arglists implies that the node will return a function with
this arglists
* :ignore-tag true when the node is untyped, does not imply that
all untyped node will have this
Passes opts:
* :infer-tag/level If :global, infer-tag will perform Var tag
inference"
{:pass-info {:walk :post :depends #{#'annotate-tag #'annotate-methods #'fix-case-test #'analyze-host-expr}}}
[{:keys [tag form] :as ast}]
(let [tag (or tag (:tag (meta form)))
ast (-infer-tag ast)]
(merge ast
(when tag
{:tag tag})
(when-let [o-tag (:o-tag ast)]
{:o-tag o-tag}))))
(defn ensure-tag
{:pass-info {:walk :any :depends #{#'infer-tag}}}
[{:keys [o-tag tag] :as ast}]
(assoc ast
:tag (or tag Object)
:o-tag (or o-tag Object)))