Skip to content

Commit 9dd8663

Browse files
committed
import internal passes
1 parent e4a5728 commit 9dd8663

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/main/clojure/clojure/tools/emitter/jvm.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
[clojure.tools.emitter.jvm.transform :as t]
1818
[clojure.tools.analyzer.passes.collect-closed-overs :refer [collect-closed-overs]]
1919
[clojure.tools.analyzer.passes.jvm
20-
[annotate-class-id :refer [annotate-class-id]]
21-
[annotate-internal-name :refer [annotate-internal-name]]
2220
[infer-tag :refer [ensure-tag]]
2321
[clear-locals :refer [clear-locals]]
2422
[collect :refer [collect]]]
25-
[clojure.tools.emitter.passes.jvm.collect-internal-methods :refer :all]
23+
[clojure.tools.emitter.passes.jvm
24+
[collect-internal-methods :refer :all]
25+
[annotate-class-id :refer [annotate-class-id]]
26+
[annotate-internal-name :refer [annotate-internal-name]]
27+
[ensure-tag :refer [ensure-tag]]]
2628
[clojure.java.io :as io]
2729
[clojure.string :as s]
2830
[clojure.tools.reader :as r]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;; Copyright (c) Nicola Mometto, Rich Hickey & contributors.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.tools.emitter.passes.jvm.annotate-class-id)
10+
11+
(defn annotate-class-id
12+
"Adds a unique class id to :reify/:fn nodes"
13+
{:pass-info {:walk :any :depends #{}}}
14+
[ast]
15+
(if (#{:reify :fn} (:op ast))
16+
(assoc ast :class-id (gensym))
17+
ast))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
;; Copyright (c) Nicola Mometto, Rich Hickey & contributors.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.tools.emitter.passes.jvm.annotate-internal-name
10+
(:require [clojure.tools.analyzer.ast :refer [update-children]]
11+
[clojure.string :as s]))
12+
13+
(defmulti annotate-internal-name
14+
"Adds a :internal-name to :fn nodes containing a string that represents
15+
the name of the class that will be generated for that fn, not including
16+
the ns prefix"
17+
{:pass-info {:walk :pre :depends #{}}}
18+
:op)
19+
20+
(defn propagate-internal-name
21+
[ast internal-name]
22+
(update-children ast (fn [ast] (assoc-in ast [:env :internal-name] internal-name))))
23+
24+
(defmethod annotate-internal-name :default
25+
[{:keys [env] :as ast}]
26+
(if-let [internal-name (:internal-name env)]
27+
(propagate-internal-name ast internal-name)
28+
ast))
29+
30+
(defmethod annotate-internal-name :def
31+
[{:keys [name] :as ast}]
32+
(propagate-internal-name ast (s/replace (str name) "." "_DOT_")))
33+
34+
(defmethod annotate-internal-name :fn
35+
[{:keys [env local] :as ast}]
36+
(let [internal-name (str (when-let [n (:internal-name env)]
37+
(str n "$"))
38+
(s/replace (or (:form local) "fn") "." "_DOT_")
39+
(gensym "__"))]
40+
(-> ast
41+
(assoc :internal-name internal-name)
42+
(propagate-internal-name internal-name))))
43+
44+
(defmethod annotate-internal-name :binding
45+
[{:keys [form env] :as ast}]
46+
(let [internal-name (str (when-let [n (:internal-name env)]
47+
(str n "$"))
48+
form)]
49+
(propagate-internal-name ast internal-name)))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;; Copyright (c) Nicola Mometto, Rich Hickey & contributors.
2+
;; The use and distribution terms for this software are covered by the
3+
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
;; which can be found in the file epl-v10.html at the root of this distribution.
5+
;; By using this software in any fashion, you are agreeing to be bound by
6+
;; the terms of this license.
7+
;; You must not remove this notice, or any other, from this software.
8+
9+
(ns clojure.tools.emitter.passes.jvm.ensure-tag
10+
(:require [clojure.tools.analyzer.passes.jvm.infer-tag :refer [infer-tag]]))
11+
12+
(defn ensure-tag
13+
{:pass-info {:walk :any :depends #{#'infer-tag}}}
14+
[{:keys [o-tag tag] :as ast}]
15+
(assoc ast
16+
:tag (or tag Object)
17+
:o-tag (or o-tag Object)))

0 commit comments

Comments
 (0)