blob: 0d34fe1a42775f3f84c8758e9bba115d32dd1d11 (
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
|
;; 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.host.generics
(:require (clojure [string :as string]
[template :refer [do-template]])
clojure.core.match
clojure.core.match.array
(lux [base :as & :refer [|do return* return fail fail* |let |case]]))
(:import java.util.regex.Pattern))
(do-template [<name> <old-sep> <new-sep>]
(let [regex (-> <old-sep> Pattern/quote re-pattern)]
(defn <name> [old]
(string/replace old regex <new-sep>)))
;; ->class
^String ->bytecode-class-name "." "/"
;; ->class-name
^String ->class-name "/" "."
)
;; ->type-signature
(defn ->type-signature [class]
(case class
"void" "V"
"boolean" "Z"
"byte" "B"
"short" "S"
"int" "I"
"long" "J"
"float" "F"
"double" "D"
"char" "C"
;; else
(let [class* (->bytecode-class-name class)]
(if (.startsWith class* "[")
class*
(str "L" class* ";")))
))
(defn super-class-name [super]
"(-> GenericSuperClassDecl Text)"
(|let [[super-name super-params] super]
super-name))
(defn class-decl-params->signature [params]
(if (&/|empty? params)
""
(str "<" (->> params (&/|interpose " ") (&/fold str "")) ">")))
(defn gclass->signature [super]
"(-> GenericClass Text)"
(|case super
(&/$GenericTypeVar name)
(str "T" name ";")
(&/$GenericWildcard)
"*"
(&/$GenericClass name params)
(case name
"void" "V"
"boolean" "Z"
"byte" "B"
"short" "S"
"int" "I"
"long" "L"
"float" "F"
"double" "D"
"char" "C"
(let [params* (if (&/|empty? params)
""
(str "<" (->> params (&/|map gclass->signature) (&/|interpose " ") (&/fold str "")) ">"))]
(str "L" (->bytecode-class-name name) params* ";")))
(&/$GenericArray param)
(str "[" (gclass->signature param))))
(defn gsuper-decl->signature [super]
"(-> GenericSuperClassDecl Text)"
(|let [[super-name super-params] super
params* (if (&/|empty? super-params)
""
(str "<" (->> super-params (&/|map gclass->signature) (&/|interpose " ") (&/fold str "")) ">"))]
(str "L" (->bytecode-class-name super-name) params* ";")))
(defn gclass-decl->signature [class-decl supers]
"(-> GenericClassDecl (List GenericSuperClassDecl) Text)"
(|let [[class-name class-vars] class-decl
vars-section (class-decl-params->signature class-vars)
super-section (->> (&/|map gsuper-decl->signature supers) (&/|interpose " ") (&/fold str ""))]
(str vars-section super-section)))
(let [object-simple-signature (->type-signature "java.lang.Object")]
(defn gclass->simple-signature [gclass]
"(-> GenericClass Text)"
(|case gclass
(&/$GenericTypeVar name)
object-simple-signature
(&/$GenericWildcard)
object-simple-signature
(&/$GenericClass name params)
(->type-signature name)
(&/$GenericArray param)
(str "[" (gclass->simple-signature param))
_
(assert false (str 'gclass->simple-signature " " (&/adt->text gclass))))))
(defn gclass->class-name [gclass]
"(-> GenericClass Text)"
(|case gclass
(&/$GenericTypeVar name)
(->bytecode-class-name "java.lang.Object")
(&/$GenericWildcard)
(->bytecode-class-name "java.lang.Object")
(&/$GenericClass name params)
(->bytecode-class-name name)
(&/$GenericArray param)
(str "[" (gclass->class-name param))
_
(assert false (str 'gclass->class-name " " (&/adt->text gclass)))))
;; TODO: CLEAN THIS UP, IT'S DOING A HACK BY TREATING GCLASSES AS GVARS
(let [object-bc-name (->bytecode-class-name "java.lang.Object")]
(defn gclass->bytecode-class-name* [gclass type-env]
"(-> GenericClass Text)"
(|case gclass
(&/$GenericTypeVar name)
object-bc-name
(&/$GenericWildcard)
object-bc-name
(&/$GenericClass name params)
(if (&/|get name type-env)
object-bc-name
(->bytecode-class-name name))
(&/$GenericArray param)
(assert false "gclass->bytecode-class-name doesn't work on arrays."))))
(let [object-bc-name (->bytecode-class-name "java.lang.Object")]
(defn gclass->bytecode-class-name [gclass]
"(-> GenericClass Text)"
(|case gclass
(&/$GenericTypeVar name)
object-bc-name
(&/$GenericWildcard)
object-bc-name
(&/$GenericClass name params)
(->bytecode-class-name name)
(&/$GenericArray param)
(assert false "gclass->bytecode-class-name doesn't work on arrays."))))
(defn method-signatures [method-decl]
(|let [[=name =anns =gvars =exceptions =inputs =output] method-decl
simple-signature (str "(" (&/fold str "" (&/|map gclass->simple-signature =inputs)) ")" (gclass->simple-signature =output))
generic-signature (str (if (&/|empty? =gvars)
""
(str "<" (->> =gvars (&/|interpose " ") (&/fold str "")) ">"))
"(" (&/fold str "" (&/|map gclass->signature =inputs)) ")"
(gclass->signature =output)
(->> =exceptions (&/|map gclass->signature) (&/|interpose " ") (&/fold str "")))]
(&/T [simple-signature generic-signature])))
|