Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested inline fn expansion #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((nil . ((indent-tabs-mode . nil)
(require-final-newline . t)))
(clojure-mode . ((cljr-favor-prefix-notation . nil))))
Comment on lines +1 to +3
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a bit of editor config settings for people who use Emacs

10 changes: 7 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject riddley "0.2.0"
(defproject riddley "0.2.1-SNAPSHOT"
:description "code-walking without caveats"
:license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"}
Expand All @@ -9,6 +9,10 @@
:defaults {:doc/format :markdown}
:include [riddley.walk riddley.compiler]
:output-dir "doc"}
:profiles {:provided {:dependencies [[org.clojure/clojure "1.8.0"]]}}
:profiles {:provided {:dependencies [[org.clojure/clojure "1.8.0"]]}
:dev {:dependencies [[org.clojure/clojure "1.10.1"]
[pjstadig/humane-test-output "0.10.0"]]
:injections [(require 'pjstadig.humane-test-output)
(pjstadig.humane-test-output/activate!)]}}
Comment on lines +13 to +16
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a :dev profile that uses Clojure 1.10.1 by default and adds humane-test-output to make the tests easier to read

:java-source-paths ["src/riddley"]
:javac-options ["-target" "1.6" "-source" "1.6"])
:javac-options ["-target" "1.7" "-source" "1.7"])
Comment on lines -14 to +18
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using Java 14 locally and it doesn't support compiling to Java 1.6 bytecode. I think dropping 1.6 support makes sense -- Clojure 1.10+ requires Java 8+ and Clojure 1.5+ requires Java 7 for all features to work out-of-the-box

289 changes: 166 additions & 123 deletions src/riddley/walk.clj
Original file line number Diff line number Diff line change
@@ -1,57 +1,93 @@
(ns riddley.walk
(:refer-clojure :exclude [macroexpand])
(:require
[riddley.compiler :as cmp]))
[riddley.compiler :as cmp]))

(declare macroexpand)

(defn merge-meta
"If `form` can have metadata, merge other `metadatas` into its metadata. Keys in `form`'s metadata are preferred over
those in `metadatas`.

(meta (merge-meta {} {:a 1})) ;-> {:a 1}
(meta (merge-meta nil {:a 1})) ;-> nil
(meta (merge-meta (with-meta {} {:a 2}) {:a 1, :b 1})) ;-> {:a 2, :b 1}"
[form & metadatas]
(cond-> form
(instance? clojure.lang.IObj form) (vary-meta (apply partial merge metadatas))))

(defn- head=
"True if `form` is list-like and the first element is `x`.

(head= '(a b c) 'a) ;-> true"
[form x]
(and (seq? form)
(= (first form) x)))

(defn inline-fn
"If `form` represents a call to a function that will be replaced with an inlined version by the compiler, returns the
`:inline` function used to create the replacement form; otherwise returns `nil`.

(inline-fn '(+ 1 2)) ; -> #function[clojure.core/nary-inline/fn--5541]
(inline-fn '(+ 1 2)) ; -> nil"
[form]
(when (seq? form)
(let [[fn-symbol & args] form]
(when (symbol? fn-symbol)
;; a function is inlineable if it has an `:inline` function in its metadata, and, if it has an
;; `:inline-arities` function in its metadata, returns truthy when passed the number of args in this form
(let [{:keys [inline inline-arities]} (meta (resolve fn-symbol))]
(when (and inline
(or (not inline-arities)
(inline-arities (count args))))
inline))))))

(defn expand-inline-fn
"Expand an inline function call `form` into the inlined version."
([form]
(expand-inline-fn form nil))

([form special-form?]
(let [inline-fn (or (inline-fn form)
(throw (ex-info "Form is not an inlineable function call." {:form form})))]
(macroexpand
(with-meta (apply inline-fn (rest form)) (meta form))
special-form?))))

(defn- expand-list-like
"Expand a list-like `form`."
([form]
(expand-list-like form nil))

([[head :as form] special-form?]
(if (or (and special-form? (special-form? head))
(contains? (cmp/locals) head))
;; might look like a macro, but for our purposes it isn't
form
;; otherwise attempt to macroexpand
(let [expanded (macroexpand-1 form)]
(cond
(not (identical? form expanded))
(macroexpand expanded special-form?)

;; if we can't macroexpand any further, check if it's an inlined function
(inline-fn expanded)
(expand-inline-fn expanded special-form?)

:else
form)))))

(defn macroexpand
"Expands both macros and inline functions. Optionally takes a `special-form?` predicate which
identifies first elements of expressions that shouldn't be macroexpanded, and honors local
bindings."
([x]
(macroexpand x nil))
([x special-form?]
(cmp/with-base-env
(if (seq? x)
(let [frst (first x)]

(if (or
(and special-form? (special-form? frst))
(contains? (cmp/locals) frst))

;; might look like a macro, but for our purposes it isn't
x

(let [x' (macroexpand-1 x)]
(if-not (identical? x x')
(macroexpand x' special-form?)

;; if we can't macroexpand any further, check if it's an inlined function
(if-let [inline-fn (and (seq? x')
(symbol? (first x'))
(-> x' meta ::transformed not)
(or
(-> x' first resolve meta :inline-arities not)
((-> x' first resolve meta :inline-arities)
(count (rest x'))))
(-> x' first resolve meta :inline))]
(let [x'' (with-meta (apply inline-fn (rest x')) (meta x'))]
(macroexpand
;; unfortunately, static function calls can look a lot like what we just
;; expanded, so prevent infinite expansion
(if (= '. (first x''))
(with-meta
(concat (butlast x'')
[(if (instance? clojure.lang.IObj (last x''))
(with-meta (last x'')
(merge
(meta (last x''))
{::transformed true}))
(last x''))])
(meta x''))
x'')
special-form?))
x')))))
x))))
"Expands both macros and inline functions. Optionally takes a `special-form?` predicate which identifies first
elements of expressions that shouldn't be macroexpanded, and honors local bindings."
([form]
(macroexpand form nil))

([form special-form?]
(cmp/with-base-env
(if-not (seq? form)
form
(expand-list-like form special-form?)))))

;;;

Expand Down Expand Up @@ -172,23 +208,31 @@
(let [[_ type var & body] x]
(cmp/with-lexical-scoping
(when var
(cmp/register-arg (with-meta var (merge (meta var) {:tag type}))))
(cmp/register-arg (vary-meta var assoc :tag type)))
(list* 'catch type var
(doall (map f body))))))

(defn- try-handler [f x]
(let [[_ & body] x]
(list* 'try (doall (map #(f % :try-clause? true) body)))))

(defn- dot-handler [f x]
(let [[_ hostexpr mem-or-meth & remainder] x]
(list* '.
(f hostexpr)
(if (seq? mem-or-meth)
(list* (first mem-or-meth)
(doall (map f (rest mem-or-meth))))
(f mem-or-meth))
(doall (map f remainder)))))
(defn- dot-handler
"Handle java interop forms."
[f [_ class-or-instance & more]]
;; form is either of the syntax
;;
;; (. class-or-instance method & args)
;; or
;; (. class-or-instance (method & args))
;;
;; both syntaxes are possible and equivalent
(if (seq? (first more))
;; (. class-or-instance (method & args))
(let [[[method & args]] more]
(list '. (f class-or-instance) (cons method (doall (map f args)))))
;; (. class-or-instance method & args)
(let [[method & args] more]
(list* '. (f class-or-instance) method (doall (map f args))))))

(defn walk-exprs
"A walk function which only traverses valid Clojure expressions. The `predicate` describes
Expand All @@ -206,71 +250,70 @@
The :try-clause? option indicates that a `try` clause is being walked. The special forms
`catch` and `finally` are only special in `try` clauses."
([predicate handler x]
(walk-exprs predicate handler nil x))
(walk-exprs predicate handler nil x))

([predicate handler special-form? x & {:keys [try-clause?]}]
(cmp/with-base-env
(let [x (try
(macroexpand x special-form?)
(catch ClassNotFoundException _
x))
walk-exprs' (partial walk-exprs predicate handler special-form?)
x' (cond

(and (seq? x) (= 'var (first x)) (predicate x))
(handler (eval x))

(and (seq? x) (= 'quote (first x)) (not (predicate x)))
x

(predicate x)
(handler x)

(seq? x)
(if (or (and (not try-clause?)
(#{'catch 'finally} (first x)))
(not (contains? special-forms (first x))))
(doall (map walk-exprs' x))
((condp = (first x)
'do do-handler
'def def-handler
'fn* fn-handler
'let* let-handler
'loop* let-handler
'letfn* letfn-handler
'case* case-handler
'try try-handler
'catch catch-handler
'reify* reify-handler
'deftype* deftype-handler
'. dot-handler
#(doall (map %1 %2)))
walk-exprs' (special-meta x)))

(instance? java.util.Map$Entry x)
(clojure.lang.MapEntry.
(walk-exprs' (key x))
(walk-exprs' (val x)))

(or
(set? x)
(vector? x))
(into (empty x) (map walk-exprs' x))

(instance? clojure.lang.IRecord x)
x

(map? x)
(into (empty x) (map walk-exprs' x))

;; special case to handle clojure.test
(and (symbol? x) (-> x meta :test))
(vary-meta x update-in [:test] walk-exprs')

:else
x)]
(if (instance? clojure.lang.IObj x')
(with-meta x' (merge (meta x) (meta x')))
x')))))
(cmp/with-base-env
(let [x (try
(macroexpand x special-form?)
(catch ClassNotFoundException _
x))
walk-exprs' (partial walk-exprs predicate handler special-form?)
x' (cond

(and (head= x 'var) (predicate x))
(handler (eval x))

(and (head= x 'quote) (not (predicate x)))
x

(predicate x)
(handler x)

(seq? x)
(if (or (and (not try-clause?)
(#{'catch 'finally} (first x)))
(not (contains? special-forms (first x))))
(doall (map walk-exprs' x))
((condp = (first x)
'do do-handler
'def def-handler
'fn* fn-handler
'let* let-handler
'loop* let-handler
'letfn* letfn-handler
'case* case-handler
'try try-handler
'catch catch-handler
'reify* reify-handler
'deftype* deftype-handler
'. dot-handler
#(doall (map %1 %2)))
walk-exprs' (special-meta x)))

(instance? java.util.Map$Entry x)
(clojure.lang.MapEntry.
(walk-exprs' (key x))
(walk-exprs' (val x)))

(or
(set? x)
(vector? x))
(into (empty x) (map walk-exprs' x))

(instance? clojure.lang.IRecord x)
x

(map? x)
(into (empty x) (map walk-exprs' x))

;; special case to handle clojure.test
(and (symbol? x) (-> x meta :test))
(vary-meta x update-in [:test] walk-exprs')

:else
x)]
(merge-meta x' (meta x))))))
Comment on lines -209 to +316
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change here was to fix the indentation and use the merge-meta helper function I wrote to simplify things a bit


;;;

Expand Down
Loading