Skip to content

Commit ea53fd0

Browse files
frenchy64swannodette
authored andcommitted
CLJS-2800: Use tools.analyzer style :children
rename :ctor to :class in :new op use tools.analyzer-style :children
1 parent 6c8e69c commit ea53fd0

File tree

5 files changed

+89
-62
lines changed

5 files changed

+89
-62
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,7 @@
13651365
(merge
13661366
{:env env
13671367
:op :the-var
1368+
:children [:var :sym :meta]
13681369
:form form}
13691370
(var-ast env sym)))
13701371

@@ -1380,7 +1381,7 @@
13801381
{:env env :op :if :form form
13811382
:test test-expr :then then-expr :else else-expr
13821383
:unchecked *unchecked-if*
1383-
:children [test-expr then-expr else-expr]}))
1384+
:children [:test :then :else]}))
13841385

13851386
(defmethod parse 'case*
13861387
[op env [_ sym tests thens default :as form] name _]
@@ -1399,14 +1400,14 @@
13991400
:form (:form test)
14001401
:env expr-env
14011402
:test test
1402-
:children [test]})
1403+
:children [:test]})
14031404
tests)
14041405
:then {:op :case-then
14051406
:form (:form then)
14061407
:env env
14071408
:then then
1408-
:children [then]}
1409-
:children (conj tests then)})
1409+
:children [:then]}
1410+
:children [:tests :then]})
14101411
tests
14111412
thens)
14121413
default (analyze env default)]
@@ -1419,7 +1420,7 @@
14191420
"case* tests must be numbers, strings, or constants")
14201421
{:env env :op :case :form form
14211422
:test v :nodes nodes :default default
1422-
:children (vec (concat [v] nodes [default]))}))
1423+
:children [:test :nodes :default]}))
14231424

14241425
(defmethod parse 'throw
14251426
[op env [_ throw-form :as form] name _]
@@ -1433,7 +1434,7 @@
14331434
(let [throw-expr (disallowing-recur (analyze (assoc env :context :expr) throw-form))]
14341435
{:env env :op :throw :form form
14351436
:exception throw-expr
1436-
:children [throw-expr]}))
1437+
:children [:exception]}))
14371438

14381439
(defmethod parse 'try
14391440
[op env [_ & body :as form] name _]
@@ -1494,7 +1495,12 @@
14941495
:finally finally
14951496
:name e
14961497
:catch catch
1497-
:children [try catch finally]}))
1498+
:children (vec
1499+
(concat [:body]
1500+
(when catch
1501+
[:catch])
1502+
(when finally
1503+
[:finally])))}))
14981504

14991505
(defn valid-proto [x]
15001506
(when (symbol? x) x))
@@ -1506,11 +1512,19 @@
15061512
(fn [env ast opts]
15071513
(assoc ast :env new-env)))
15081514

1515+
(defn ast-children [ast]
1516+
(mapcat (fn [c]
1517+
(let [g (get ast c)]
1518+
(cond
1519+
(vector? g) g
1520+
g [g])))
1521+
(:children ast)))
1522+
15091523
(defn constant-value?
15101524
[{:keys [op] :as ast}]
15111525
(or (= :const op)
15121526
(and (#{:map :set :vector :list} op)
1513-
(every? constant-value? (:children ast)))))
1527+
(every? constant-value? (ast-children ast)))))
15141528

15151529
(defmethod parse 'def
15161530
[op env form _ _]
@@ -1673,8 +1687,7 @@
16731687
sym)
16741688
:op :var)
16751689
:doc doc
1676-
:jsdoc (:jsdoc sym-meta)
1677-
:init init-expr}
1690+
:jsdoc (:jsdoc sym-meta)}
16781691
(when (true? (:def-emits-var env))
16791692
{:var-ast (var-ast env sym)})
16801693
(when-some [test (:test sym-meta)]
@@ -1685,7 +1698,10 @@
16851698
{:tag tag}))
16861699
(when (true? dynamic) {:dynamic true})
16871700
(when (some? export-as) {:export export-as})
1688-
(when (some? init-expr) {:children [init-expr]})))))
1701+
(if (some? init-expr)
1702+
{:init init-expr
1703+
:children [:var :init]}
1704+
{:children [:var]})))))
16891705

16901706
(defn analyze-fn-method-param [env]
16911707
(fn [[locals params] name]
@@ -1738,15 +1754,19 @@
17381754
expr (when analyze-body?
17391755
(analyze-fn-method-body body-env body-form recur-frames))
17401756
recurs @(:flag recur-frame)]
1741-
{:env env
1742-
:op :fn-method
1743-
:variadic? variadic
1744-
:params params
1745-
:fixed-arity fixed-arity
1746-
:type type
1747-
:form form
1748-
:body expr
1749-
:recurs recurs}))
1757+
(merge
1758+
{:env env
1759+
:op :fn-method
1760+
:variadic? variadic
1761+
:params params
1762+
:fixed-arity fixed-arity
1763+
:type type
1764+
:form form
1765+
:recurs recurs}
1766+
(if (some? expr)
1767+
{:body expr
1768+
:children [:params :body]}
1769+
{:children [:params]}))))
17501770

17511771
(declare analyze-wrap-meta)
17521772

@@ -1768,7 +1788,7 @@
17681788
(merge name-var ret-tag))))
17691789

17701790
(defn analyze-fn-methods-pass2* [menv locals type meths]
1771-
(doall (map #(analyze-fn-method menv locals % type true) meths)))
1791+
(mapv #(analyze-fn-method menv locals % type true) meths))
17721792

17731793
(defn analyze-fn-methods-pass2 [menv locals type meths]
17741794
(analyze-fn-methods-pass2* menv locals type meths))
@@ -1817,12 +1837,14 @@
18171837
;; a second pass with knowledge of our function-ness/arity
18181838
;; lets us optimize self calls
18191839
(disallowing-ns* (analyze-fn-methods-pass2 menv locals type meths))
1820-
methods)
1840+
(vec methods))
18211841
form (vary-meta form dissoc ::protocol-impl ::protocol-inline ::type)
18221842
js-doc (when (true? variadic)
18231843
"@param {...*} var_args")
1824-
children (mapv :body methods)
1825-
ast {:op :fn
1844+
children (if (some? name-var)
1845+
[:local :methods]
1846+
[:methods])
1847+
ast (merge {:op :fn
18261848
:env env
18271849
:form form
18281850
:name name-var
@@ -1835,7 +1857,9 @@
18351857
:max-fixed-arity mfa
18361858
:protocol-impl proto-impl
18371859
:protocol-inline proto-inline
1838-
:children children}]
1860+
:children children}
1861+
(when (some? name-var)
1862+
{:local name-var}))]
18391863
(let [variadic-methods (filter :variadic? methods)
18401864
variadic-params (count (:params (first variadic-methods)))
18411865
param-counts (map (comp count :params) methods)]
@@ -1889,10 +1913,10 @@
18891913
[meth-env []] bes)
18901914
expr (analyze (assoc meth-env :context (if (= :expr context) :return context)) `(do ~@exprs))]
18911915
{:env env :op :letfn :bindings bes :body expr :form form
1892-
:children (conj (vec (map :init bes)) expr)}))
1916+
:children [:bindings :body]}))
18931917

18941918
(defn analyze-do-statements* [env exprs]
1895-
(seq (doall (map #(analyze (assoc env :context :statement) %) (butlast exprs)))))
1919+
(mapv #(analyze (assoc env :context :statement) %) (butlast exprs)))
18961920

18971921
(defn analyze-do-statements [env exprs]
18981922
(disallowing-recur (analyze-do-statements* env exprs)))
@@ -1902,7 +1926,7 @@
19021926
(let [statements (analyze-do-statements env exprs)]
19031927
(if (<= (count exprs) 1)
19041928
(let [ret (analyze env (first exprs))
1905-
children (conj (vec statements) ret)]
1929+
children [:statements :ret]]
19061930
{:op :do
19071931
:env env
19081932
:form form
@@ -1912,7 +1936,7 @@
19121936
(assoc env :context :statement)
19131937
(assoc env :context :return))
19141938
ret (analyze ret-env (last exprs))
1915-
children (conj (vec statements) ret)]
1939+
children [:statements :ret]]
19161940
{:op :do
19171941
:env env
19181942
:form form
@@ -1999,7 +2023,7 @@
19992023
(some? *loop-lets*) (cons {:params bes} *loop-lets*))
20002024
expr (analyze-let-body env context exprs recur-frames loop-lets)
20012025
op (if (true? is-loop) :loop :let)
2002-
children (conj (vec (map :init bes)) expr)]
2026+
children [:bindings :body]]
20032027
{:op op
20042028
:env encl-env
20052029
:bindings bes
@@ -2036,7 +2060,7 @@
20362060
(assoc {:env env :op :recur :form form}
20372061
:frame frame
20382062
:exprs exprs
2039-
:children exprs)))
2063+
:children [:exprs])))
20402064

20412065
(defmethod parse 'quote
20422066
[_ env [_ x] _ _]
@@ -2058,8 +2082,8 @@
20582082
(when (and (not (-> ctor meta :internal-ctor))
20592083
(some? known-num-fields) (not= known-num-fields argc))
20602084
(warning :fn-arity env {:argc argc :ctor ctor}))
2061-
{:env env :op :new :form form :ctor ctorexpr :args argexprs
2062-
:children (into [ctorexpr] argexprs)
2085+
{:env env :op :new :form form :class ctorexpr :args argexprs
2086+
:children [:class :args]
20632087
:tag (let [name (-> ctorexpr :info :name)]
20642088
(or ('{js/Object object
20652089
js/String string
@@ -2118,7 +2142,7 @@
21182142

21192143
:else
21202144
{:env env :op :set! :form form :target texpr :val vexpr
2121-
:children [texpr vexpr]})))))
2145+
:children [:target :val]})))))
21222146

21232147
#?(:clj (declare analyze-file))
21242148

@@ -2862,6 +2886,7 @@
28622886
{:op op :env env :form form :t t :fields fields :pmasks pmasks
28632887
:tag 'function
28642888
:protocols (disj protocols 'cljs.core/Object)
2889+
:children [#_:fields :body]
28652890
:body (analyze (assoc env :locals locals) body)}))
28662891

28672892
(defmethod parse 'deftype*
@@ -2977,7 +3002,7 @@
29773002
(swap! env/*compiler* update-in
29783003
(into [::namespaces (-> env :ns :name) :externs] pre) merge {}))))
29793004
(case dot-action
2980-
::access (let [children [targetexpr]]
3005+
::access (let [children [:target]]
29813006
{:op :host-field
29823007
:env env
29833008
:form form
@@ -2987,8 +3012,8 @@
29873012
:tag (if (js-tag? tag)
29883013
(or (js-tag (-> tag meta :prefix) :tag) tag)
29893014
tag)})
2990-
::call (let [argexprs (map #(analyze enve %) args)
2991-
children (into [targetexpr] argexprs)]
3015+
::call (let [argexprs (mapv #(analyze enve %) args)
3016+
children [:target :args]]
29923017
{:op :host-call
29933018
:env env
29943019
:form form
@@ -3103,7 +3128,7 @@
31033128
:args argexprs
31043129
:tag tag
31053130
:form form
3106-
:children argexprs
3131+
:children [:args]
31073132
:js-op js-op
31083133
:numeric numeric}))
31093134

@@ -3206,9 +3231,9 @@
32063231
(~(analyzed (if bind-f-expr? f-sym f))
32073232
~@(if bind-args? arg-syms args)))))
32083233
(let [ana-expr #(analyze enve %)
3209-
argexprs (map ana-expr args)]
3210-
{:env env :op :invoke :form form :fn fexpr :args (vec argexprs)
3211-
:children (into [fexpr] argexprs)}))))
3234+
argexprs (mapv ana-expr args)]
3235+
{:env env :op :invoke :form form :fn fexpr :args argexprs
3236+
:children [:fn :args]}))))
32123237

32133238
(defn parse-invoke
32143239
[env form]
@@ -3434,30 +3459,30 @@
34343459
(defn analyze-map
34353460
[env form]
34363461
(let [expr-env (assoc env :context :expr)
3437-
ks (disallowing-recur (vec (map #(analyze expr-env %) (keys form))))
3438-
vs (disallowing-recur (vec (map #(analyze expr-env %) (vals form))))]
3462+
ks (disallowing-recur (mapv #(analyze expr-env %) (keys form)))
3463+
vs (disallowing-recur (mapv #(analyze expr-env %) (vals form)))]
34393464
(analyze-wrap-meta {:op :map :env env :form form
34403465
:keys ks :vals vs
3441-
:children (vec (interleave ks vs))
3466+
:children [:keys :vals]
34423467
:tag 'cljs.core/IMap})))
34433468

34443469
(defn analyze-list
34453470
[env form]
34463471
(let [expr-env (assoc env :context :expr)
3447-
items (disallowing-recur (doall (map #(analyze expr-env %) form)))]
3448-
(analyze-wrap-meta {:op :list :env env :form form :items items :children items :tag 'cljs.core/IList})))
3472+
items (disallowing-recur (mapv #(analyze expr-env %) form))]
3473+
(analyze-wrap-meta {:op :list :env env :form form :items items :children [:items] :tag 'cljs.core/IList})))
34493474

34503475
(defn analyze-vector
34513476
[env form]
34523477
(let [expr-env (assoc env :context :expr)
3453-
items (disallowing-recur (vec (map #(analyze expr-env %) form)))]
3454-
(analyze-wrap-meta {:op :vector :env env :form form :items items :children items :tag 'cljs.core/IVector})))
3478+
items (disallowing-recur (mapv #(analyze expr-env %) form))]
3479+
(analyze-wrap-meta {:op :vector :env env :form form :items items :children [:items] :tag 'cljs.core/IVector})))
34553480

34563481
(defn analyze-set
34573482
[env form ]
34583483
(let [expr-env (assoc env :context :expr)
3459-
items (disallowing-recur (vec (map #(analyze expr-env %) form)))]
3460-
(analyze-wrap-meta {:op :set :env env :form form :items items :children items :tag 'cljs.core/ISet})))
3484+
items (disallowing-recur (mapv #(analyze expr-env %) form))]
3485+
(analyze-wrap-meta {:op :set :env env :form form :items items :children [:items] :tag 'cljs.core/ISet})))
34613486

34623487
(defn analyze-js-value
34633488
[env ^JSValue form]
@@ -3472,15 +3497,15 @@
34723497
:form form
34733498
:keys keys
34743499
:vals vals
3475-
:children vals
3500+
:children [:vals]
34763501
:tag 'object})
34773502
(let [items (disallowing-recur
34783503
(mapv #(analyze expr-env %) val))]
34793504
{:op :js-array
34803505
:env env
34813506
:form form
34823507
:items items
3483-
:children items
3508+
:children [:items]
34843509
:tag 'array}))))
34853510

34863511
(defn analyze-record
@@ -3499,7 +3524,7 @@
34993524
:env env
35003525
:form x
35013526
:items items
3502-
:children [items]
3527+
:children [:items]
35033528
:tag (symbol (str ns) (str name))}))
35043529

35053530
(defn elide-reader-meta [m]
@@ -3516,7 +3541,7 @@
35163541
expr (assoc-in expr [:env :context] :expr) ; change expr to :expr
35173542
meta-expr (analyze-map (:env expr) m)]
35183543
{:op :with-meta :env env :form form
3519-
:meta meta-expr :expr expr :children [meta-expr expr]})
3544+
:meta meta-expr :expr expr :children [:meta :expr]})
35203545
expr)))
35213546

35223547
(defn infer-type [env ast _]
@@ -3662,7 +3687,8 @@
36623687
(one of :statement, :expr, :return), :ns (a symbol naming the
36633688
compilation ns)}, and form, returns an expression object (a map
36643689
containing at least :form, :op and :env keys). If expr has any (immediately)
3665-
nested exprs, must have :children [exprs...] entry. This will
3690+
nested exprs, must have a :children entry. This must be a vector of keywords naming
3691+
the immediately nested fields mapped to an expr or vector of exprs. This will
36663692
facilitate code walking without knowing the details of the op set."
36673693
([env form] (analyze env form nil))
36683694
([env form name]

src/main/clojure/cljs/analyzer/api.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
(one of :statement, :expr, :return), :ns (a symbol naming the
7272
compilation ns)}, and form, returns an expression object (a map
7373
containing at least :form, :op and :env keys). If expr has any (immediately)
74-
nested exprs, must have :children [exprs...] entry. This will
74+
nested exprs, must have :children entry. This must be a vector of keywords naming
75+
the immediately nested fields mapped to an expr or vector of exprs. This will
7576
facilitate code walking without knowing the details of the op set."
7677
([env form] (analyze env form nil))
7778
([env form name] (analyze env form name nil))

0 commit comments

Comments
 (0)