-
Notifications
You must be signed in to change notification settings - Fork 65
/
utils.clj
382 lines (341 loc) · 12.6 KB
/
utils.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
;; 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 eastwood.copieddeps.dep2.clojure.tools.analyzer.jvm.utils
(:require [eastwood.copieddeps.dep1.clojure.tools.analyzer.utils :as u]
[eastwood.copieddeps.dep1.clojure.tools.analyzer.env :as env]
[clojure.reflect :as reflect]
[clojure.string :as s]
[eastwood.copieddeps.dep3.clojure.core.memoize :refer [lru]]
[clojure.java.io :as io])
(:import (clojure.lang RT Symbol Var)
org.objectweb.asm.Type))
(defn ^:private type-reflect
[typeref & options]
(apply reflect/type-reflect typeref
:reflector (reflect/->JavaReflector (RT/baseLoader))
options))
(defn macro? [sym env]
(when-let [v (u/resolve-sym sym env)]
(and (not (-> env :locals (get sym)))
(u/macro? v)
v)))
(defn inline? [sym args env]
(when-let [v (u/resolve-sym sym env)]
(let [inline-arities-f (:inline-arities (meta v))]
(and (not (-> env :locals (get sym)))
(or (not inline-arities-f)
(inline-arities-f (count args)))
(:inline (meta v))))))
(defn specials [c]
(case c
"byte" Byte/TYPE
"boolean" Boolean/TYPE
"char" Character/TYPE
"int" Integer/TYPE
"long" Long/TYPE
"float" Float/TYPE
"double" Double/TYPE
"short" Short/TYPE
"void" Void/TYPE
"object" Object
nil))
(defn special-arrays [c]
(case c
"bytes" (Class/forName "[B")
"booleans" (Class/forName "[Z")
"chars" (Class/forName "[C")
"ints" (Class/forName "[I")
"longs" (Class/forName "[J")
"floats" (Class/forName "[F")
"doubles" (Class/forName "[D")
"shorts" (Class/forName "[S")
"objects" (Class/forName "[Ljava.lang.Object;")
nil))
(defmulti ^Class maybe-class
"Takes a Symbol, String or Class and tires to resolve to a matching Class"
class)
(defn array-class [element-type]
(RT/classForName
(str "[" (-> element-type
maybe-class
Type/getType
.getDescriptor
(.replace \/ \.)))))
(defn maybe-class-from-string [^String s]
(or (when-let [maybe-class (and (neg? (.indexOf s "."))
(not= \[ (first s))
(if env/*env*
(u/resolve-sym (symbol s) {:ns (ns-name *ns*)})
((ns-map *ns*) (symbol s))))]
(when (class? maybe-class) maybe-class))
(try (RT/classForName s)
(catch ClassNotFoundException _))))
(defmethod maybe-class :default [_] nil)
(defmethod maybe-class Class [c] c)
(defmethod maybe-class String [s]
(maybe-class (symbol s)))
(defmethod maybe-class Symbol [sym]
(when-not (namespace sym)
(let [sname (name sym)
snamec (count sname)]
(if-let [base-type (and (.endsWith sname "<>")
(maybe-class (subs sname 0 (- snamec 2))))]
(array-class base-type)
(if-let [ret (or (specials sname)
(special-arrays sname))]
ret
(maybe-class-from-string sname))))))
(defn maybe-class-literal [x]
(cond
(class? x) x
(symbol? x) (and (not (namespace x))
(maybe-class-from-string (name x)))
(string? x) (maybe-class-from-string x)))
(def primitive?
"Returns non-nil if the argument represents a primitive Class other than Void"
#{Double/TYPE Character/TYPE Byte/TYPE Boolean/TYPE
Short/TYPE Float/TYPE Long/TYPE Integer/TYPE})
(def ^:private convertible-primitives
"If the argument is a primitive Class, returns a set of Classes
to which the primitive Class can be casted"
{Integer/TYPE #{Integer Long/TYPE Long Short/TYPE Byte/TYPE Object Number}
Float/TYPE #{Float Double/TYPE Object Number}
Double/TYPE #{Double Float/TYPE Object Number}
Long/TYPE #{Long Integer/TYPE Short/TYPE Byte/TYPE Object Number}
Character/TYPE #{Character Object}
Short/TYPE #{Short Object Number}
Byte/TYPE #{Byte Object Number}
Boolean/TYPE #{Boolean Object}
Void/TYPE #{Void}})
(defn ^Class box
"If the argument is a primitive Class, returns its boxed equivalent,
otherwise returns the argument"
[c]
({Integer/TYPE Integer
Float/TYPE Float
Double/TYPE Double
Long/TYPE Long
Character/TYPE Character
Short/TYPE Short
Byte/TYPE Byte
Boolean/TYPE Boolean
Void/TYPE Void}
c c))
(defn ^Class unbox
"If the argument is a Class with a primitive equivalent, returns that,
otherwise returns the argument"
[c]
({Integer Integer/TYPE,
Long Long/TYPE,
Float Float/TYPE,
Short Short/TYPE,
Boolean Boolean/TYPE,
Byte Byte/TYPE,
Character Character/TYPE,
Double Double/TYPE,
Void Void/TYPE}
c c))
(defn numeric?
"Returns true if the given class is numeric"
[c]
(when c
(.isAssignableFrom Number (box c))))
(defn subsumes?
"Returns true if c2 is subsumed by c1"
[c1 c2]
(let [c1 (maybe-class c1)
c2 (maybe-class c2)]
(and (not= c1 c2)
(or (and (not (primitive? c1))
(primitive? c2))
(.isAssignableFrom c2 c1)))))
(defn convertible?
"Returns true if it's possible to convert from c1 to c2"
[c1 c2]
(let [c1 (maybe-class c1)
c2 (maybe-class c2)]
(if (nil? c1)
(not (primitive? c2))
(or
(= c1 c2)
(.isAssignableFrom c2 c1)
(and (primitive? c2)
((convertible-primitives c2) c1))
(and (primitive? c1)
(.isAssignableFrom (box c1) c2))))))
(def wider-than
"If the argument is a numeric primitive Class, returns a set of primitive Classes
that are narrower than the given one"
{Long/TYPE #{Integer/TYPE Short/TYPE Byte/TYPE}
Integer/TYPE #{Short/TYPE Byte/TYPE}
Float/TYPE #{Integer/TYPE Short/TYPE Byte/TYPE Long/TYPE}
Double/TYPE #{Integer/TYPE Short/TYPE Byte/TYPE Long/TYPE Float/TYPE}
Short/TYPE #{Byte/TYPE}
Byte/TYPE #{}})
(defn wider-primitive
"Given two numeric primitive Classes, returns the wider one"
[from to]
(if ((wider-than from) to)
from
to))
(defn wider-tag*
"Given two Classes returns the wider one"
[from to]
(if (not= from to)
(if (primitive? from)
(if (primitive? to)
(wider-primitive from to)
(or (and (numeric? from)
(numeric? to)
to)
((convertible-primitives from) to)))
(if (primitive? to)
(or (and (numeric? from)
(numeric? to)
from)
((convertible-primitives to) from))
(if (convertible? from to)
to
(when (convertible? to from)
from))))
from))
(defn wider-tag
"Given a collection of Classes returns the wider one"
[tags]
(let [tags* (filter identity tags)
wider (loop [wider (first tags*) tags* (rest tags*)]
(if (seq tags*)
(if-let [t (wider-tag* wider (first tags*))]
(recur t (rest tags*)))
wider))]
(when (or (= tags* tags)
(not (primitive? wider)))
wider)))
(defn name-matches?
[member]
(let [member-name (str member)
i (.lastIndexOf member-name ".")
member-name* (when (pos? i)
(str (s/replace (subs member-name 0 i) "-" "_") (subs member-name i)))
member-name** (s/replace member-name "-" "_")
member-name*** (munge member-name)]
(fn [name]
(let [name (str name)]
(or (= member-name name)
(= member-name* name)
(= member-name** name)
(= member-name*** name))))))
(def object-members
(:members (type-reflect Object)))
(def members*
(lru (fn members*
([class]
(into object-members
(remove (fn [{:keys [flags]}]
(not-any? #{:public :protected} flags))
(-> class
maybe-class
^Class (box)
.getName
symbol
(type-reflect :ancestors true)
:members)))))))
(defn members
([class] (members* class))
([class member]
(when-let [members (filter #((name-matches? member) (:name %))
(members* class))]
members)))
(defn static-members [class f]
(when-let [members (members class f)]
(when-let [statics (filter (comp :static :flags) members)]
statics)))
(defn instance-members [class f]
(when-let [members (members class f)]
(when-let [i-members (remove (comp :static :flags) members)]
i-members)))
(defn static-methods [class method argc]
(filter #(= argc (count (:parameter-types %)))
(filter :return-type (static-members class method))))
(defn instance-methods [class method argc]
(filter #(= argc (count (:parameter-types %)))
(filter :return-type (instance-members class method))))
(defn static-field [class f]
(when-let [statics (static-members class f)]
(when-let [[member] (filter (every-pred (comp nil? seq :parameter-types)
(comp nil? :return-type))
statics)]
member)))
(defn instance-field [class f]
(when-let [i-members (instance-members class f)]
(when-let [[member] (filter (every-pred (comp nil? seq :parameter-types)
(comp nil? :return-type))
i-members)]
member)))
(defn static-method [class method]
(first (static-methods class method 0)))
(defn instance-method [class method]
(first (instance-methods class method 0)))
(defn prim-or-obj
"If the given Class is a primitive, returns that Class, otherwise returns Object"
[tag]
(if (and tag (primitive? tag))
tag
java.lang.Object))
(defn prim-interface [tags]
(when (some primitive? tags)
(let [sig (apply str (mapv #(.toUpperCase (subs (.getSimpleName ^Class %) 0 1)) tags))]
(maybe-class (str "clojure.lang.IFn$" sig)))))
(defn tag-match? [arg-tags meth]
(every? identity (map convertible? arg-tags (:parameter-types meth))))
(defn try-best-match
"Given a vector of arg tags and a collection of methods, tries to return the
subset of methods that match best the given tags"
[tags methods]
(let [o-tags (mapv #(or (maybe-class %) Object) tags)]
(if-let [methods (or (seq (filter
#(= o-tags (mapv maybe-class (:parameter-types %))) methods))
(seq (filter #(tag-match? tags %) methods)))]
(reduce (fn [[prev & _ :as p] next]
(let [prev-params (mapv maybe-class (:parameter-types prev))
next-params (mapv maybe-class (:parameter-types next))
prev-ret (maybe-class (:return-type prev))
next-ret (maybe-class (:return-type next))
prev-decl (maybe-class (:declaring-class prev))
next-decl (maybe-class (:declaring-class next))]
(cond
(not prev)
[next]
(= prev-params next-params)
(cond
(= prev-ret next-ret)
(cond
(.isAssignableFrom prev-decl next-decl)
[next]
(.isAssignableFrom next-decl prev-decl)
p
:else
(conj p next))
(.isAssignableFrom prev-ret next-ret)
[next]
(.isAssignableFrom next-ret prev-ret)
p
:else
(conj p next))
(and (some true? (map subsumes? next-params prev-params))
(not-any? true? (map subsumes? prev-params next-params)))
[next]
:else
(conj p next)))) [] methods)
methods)))
(defn ns->relpath [s]
(-> s str (s/replace \. \/) (s/replace \- \_) (str ".clj")))
(defn ns-url [ns]
(let [f (ns->relpath ns)]
(or (io/resource f)
(io/resource (str f "c")))))