clojure.tools.analyzer - tools.analyzer 1.1.2-SNAPSHOT API documentation

Full namespace name: clojure.tools.analyzer

Overview

Analyzer for clojure code, host agnostic.

Entry point:
* analyze

Platform implementers must provide dynamic bindings for:
* macroexpand-1
* parse
* create-var
* var?

Setting up the global env is also required, see clojure.tools.analyzer.env

See clojure.tools.analyzer.core-test for an example on how to setup the analyzer.

Public Variables and Functions



function

Usage: (-parse form env)
Takes a form and an env map and dispatches on the head of the form, that is
a special form.

    
    
    

Source



function

Usage: (analyze form env)
Given a form to analyze and an environment, a map containing:
* :locals     a map from binding symbol to AST of the binding value
* :context    a keyword describing the form's context from the :ctx/* hierarchy.
 ** :ctx/expr      the form is an expression: its value is used
 ** :ctx/return    the form is an expression in return position, derives :ctx/expr
 ** :ctx/statement the value of the form is not used
* :ns         a symbol representing the current namespace of the form to be
              analyzed

returns an AST for that form.

Every node in the AST is a map that is *guaranteed* to have the following keys:
* :op   a keyword describing the AST node
* :form the form represented by the AST node
* :env  the environment map of the AST node

Additionally if the AST node contains sub-nodes, it is guaranteed to have:
* :children a vector of the keys of the AST node mapping to the sub-nodes,
            ordered, when that makes sense

It is considered a node either the top-level node (marked with :top-level true)
or a node that can be reached via :children; if a node contains a node-like
map that is not reachable by :children, there's no guarantee that such a map
will contain the guaranteed keys.

    
    
    

Source



dynamic multimethod

No usage documentation available
Like analyze, but does not mark the form with :top-level true

    
    
    

Source



function

Usage: (analyze-in-env env)
Takes an env map and returns a function that analyzes a form in that env

    
    
    

Source



dynamic function

Usage: (create-var sym env)
Creates a var for sym and returns it

    
    
    

Source



function

Usage: (empty-env)
Returns an empty env

    
    
    

Source



function

Usage: (macroexpand form env)
Repeatedly calls macroexpand-1 on form until it no longer
represents a macro form, then returns it.

    
    
    

Source



dynamic function

Usage: (macroexpand-1 form env)
If form represents a macro form, returns its expansion,
else returns form.

    
    
    

Source



dynamic function

Usage: (parse [op & args] env)
Multimethod that dispatches on op, should default to -parse

    
    
    

Source



var


    
Set of special forms common to every clojure variant

    
    
    

Source



dynamic function

Usage: (var? obj)
Returns true if obj represent a var form as returned by create-var

    
    
    

Source


Utilities for AST walking/updating

Public Variables and Functions



function

Usage: (ast->eav ast)
Returns an EAV representation of the current AST that can be used by
Datomic's Datalog.

    
    
    

Source



function

Usage: (children ast)
Return a vector of the children expression of the AST node, if it has any.
The children expressions are kept in order and flattened so that the returning
vector contains only nodes and not vectors of nodes.

    
    
    

Source



function

Usage: (children* {:keys [children], :as ast})
Return a vector of vectors of the children node key and the children expression
of the AST node, if it has any.
The returned vector returns the children in the order as they appear in the
:children field of the AST, and the children expressions may be either a node
or a vector of nodes.

    
    
    

Source



function

Usage: (cycling & fns*)
Combine the given passes in a single pass that will be applied repeatedly
to the AST until applying it another time will have no effect

    
    
    

Source



function

Usage: (nodes ast)
Returns a lazy-seq of all the nodes in the given AST, in depth-first pre-order.

    
    
    

Source



postwalk

function

Usage: (postwalk ast f)
       (postwalk ast f reversed?)
Shorthand for (walk ast identity f reversed?)

    
    
    

Source



function

Usage: (prewalk ast f)
Shorthand for (walk ast f identity)

    
    
    

Source



function

Usage: (update-children ast f)
       (update-children ast f reversed?)
Applies `f` to each AST children node, replacing it with the returned value.
If reversed? is not-nil, `pre` and `post` will be applied starting from the last
children of the AST node to the first one.
Short-circuits on reduced.

    
    
    

Source



function

Usage: (update-children-reduced ast f)
       (update-children-reduced ast f reversed?)
Like update-children but returns a reduced holding the AST if f short-circuited.

    
    
    

Source



function

Usage: (walk ast pre post)
       (walk ast pre post reversed?)
Walk the ast applying `pre` when entering the nodes, and `post` when exiting.
Both functions must return a valid node since the returned value will replace
the node in the AST which was given as input to the function.
If reversed? is not-nil, `pre` and `post` will be applied starting from the last
children of the AST node to the first one.
Short-circuits on reduced.

    
    
    

Source


Utilities for querying tools.analyzer ASTs with Datomic

Public Variables and Functions



function

Usage: (db asts)
Given a list of ASTs, returns a representation of those
that can be used as a database in a Datomic Datalog query.

    
    
    

Source



function

Usage: (q query asts & inputs)
Execute a Datomic Datalog query against the ASTs.
The first input is always assumed to be an AST database, if more
are required, it's required to call `db` on them.
`unfold-expression-clauses` is automatically applied to the
query.

    
    
    

Source



function

Usage: (query-map query)
Transforms a Datomic query from its vector representation to its map one.
If the given query is already in its map representation, the original query
is returned.

    
    
    

Source



function

Usage: (resolve-calls query)
Automatically replace fn name symbols in expression clauses with
their namespace qualified one if the symbol can be resolved in the
current namespace.

    
    
    

Source



function

Usage: (unfold-expression-clauses query)
Given a Datomic query, walk the :where clauses searching for
expression clauses with nested calls, unnesting those calls.

E.g {:where [[(inc (dec ?foo)) ?bar] ..] ..} will be transformed into
{:where [[(dec ?foo) ?1234] [(inc ?1234) ?bar] ..] ..}

    
    
    

Source






Public Variables and Functions



dynamic var


    
Global env atom containing a map.
Required options:
 * :namespaces a map from namespace symbol to namespace map,
   the namespace map contains at least the following keys:
  ** :mappings a map of mappings of the namespace, symbol to var/class
  ** :aliases a map of the aliases of the namespace, symbol to symbol
  ** :ns a symbol representing the namespace

    
    
    

Source



function

Usage: (deref-env)
Returns the value of the current global env if bound, otherwise
throws an exception.

    
    
    

Source



macro

Usage: (ensure env & body)
If *env* is not bound it binds it to env before executing the body

    
    
    

Source



macro

Usage: (with-env env & body)
Binds the global env to env, then executes the body

    
    
    

Source


Utilities for pass scheduling

Public Variables and Functions



function

Usage: (calculate-deps passes)
Takes a map of pass-name -> pass-info and adds to each pass-info :dependencies and
:dependants info, which also contains the transitive dependencies

    
    
    

Source



function

Usage: (desugar-deps passes)
Takes a map of pass-name -> pass deps and puts the :after :affects and :before passes
in the appropriate pass :depends

    
    
    

Source



function

Usage: (group state)
Takes a scheduler state and returns a vector of three elements (or nil):
* the :walk of the current group
* a vector of consecutive passes that can be collapsed in a single pass (the current group)
* the remaining scheduler state

E.g. given:
[{:walk :any ..} {:walk :pre ..} {:walk :post ..} {:walk :pre ..}]
it will return:
[:pre [{:walk :any ..} {:walk :pre ..}] [{:walk :post ..} {:walk :pre ..}]]

    
    
    

Source



function

Usage: (schedule passes & [opts])
Takes a set of Vars that represent tools.analyzer passes and returns a function
that takes an AST and applies all the passes and their dependencies to the AST,
trying to compose together as many passes as possible to reduce the number of
full tree traversals.

Each pass must have a :pass-info element in its Var's metadata and it must point
to a map with the following parameters (:before, :after, :affects and :state are
optional):
* :after    a set of Vars, the passes that must be run before this pass
* :before   a set of Vars, the passes that must be run after this pass
* :depends  a set of Vars, the passes this pass depends on, implies :after
* :walk     a keyword, one of:
              - :none if the pass does its own tree walking and cannot be composed
                      with other passes
              - :post if the pass requires a postwalk and can be composed with other
                      passes
              - :pre  if the pass requires a prewalk and can be composed with other
                      passes
              - :any  if the pass can be composed with other passes in both a prewalk
                      or a postwalk
* :affects  a set of Vars, this pass must be the last in the same tree traversal that all
            the specified passes must participate in
            This pass must take a function as argument and return the actual pass, the
            argument represents the reified tree traversal which the pass can use to
            control a recursive traversal, implies :depends
* :state    a no-arg function that should return an atom holding an init value that will be
            passed as the first argument to the pass (the pass will thus take the ast
            as the second parameter), the atom will be the same for the whole tree traversal
            and thus can be used to preserve state across the traversal
An opts map might be provided, valid parameters:
* :debug?   if true, returns a vector of the scheduled passes rather than the concrete
            function

    
    
    

Source






Public Variables and Functions



function

Usage: (add-binding-atom ast)
       (add-binding-atom state ast)
Adds an atom-backed-map to every local binding,the same
atom will be shared between all occurences of that local.

The atom is put in the :atom field of the node.

    
    
    

Source






Public Variables and Functions



function

Usage: (collect-closed-overs ast)
Attach closed-overs info to the AST as specified by the passes opts:
* :where       set of :op nodes where to attach the closed-overs
* :top-level?  if true attach closed-overs info to the top-level node

The info will be attached in the :closed-overs field of the AST node
and will be a map of local name -> binding AST node

    
    
    

Source






Public Variables and Functions



multimethod

No usage documentation available
If the node represents a collection with no metadata, and every item of that
collection is a literal, transform the node to an equivalent :const node.

    
    
    

Source






Public Variables and Functions



function

Usage: (elide-meta ast)
If elides is not empty and the AST node contains metadata,
dissoc all the keys in elides from the metadata.

    
    
    

Source



dynamic var


    
A map of op keywords to predicate IFns.
The predicate will be used to indicate what map keys should be elided on
metadata of nodes for that op.
:all can be used to indicate what should be elided for every node with
metadata.
Defaults to {:all (set (:elide-meta *compiler-options*))}

    
    
    

Source






Public Variables and Functions



dynamic function

Usage: (-emit-form* {:keys [form], :as ast} opts)
Extension point for custom emit-form implementations, should be rebound
to a multimethod with custom emit-form :opts.

    
    
    

Source



function

Usage: (emit-form ast)
       (emit-form ast opts)
Return the form represented by the given AST.
Opts is a set of options, valid options are:
 * :hygienic

    
    
    

Source



function

Usage: (emit-hygienic-form ast)
Return an hygienic form represented by the given AST

    
    
    

Source






Public Variables and Functions



function

Usage: (index-vector-nodes ast)
Adds an :idx attribute to nodes in a vector children, representing the position
of the node vector.

    
    
    

Source






Public Variables and Functions



function

Usage: (source-info ast)
Adds (when avaliable) :line, :column, :end-line, :end-column and :file info to the AST :env

    
    
    

Source






Public Variables and Functions



function

Usage: (trim ast)
Trims the AST of unnecessary nodes, e.g. (do (do 1)) -> 1

    
    
    

Source






Public Variables and Functions



function

Usage: (uniquify-locals ast)
Walks the AST performing alpha-conversion on the :name field
 of :local/:binding nodes, invalidates :local map in :env field

Passes opts:
* :uniquify/uniquify-env  If true, uniquifies the :env :locals map

    
    
    

Source






Public Variables and Functions



function

Usage: (warn-earmuff ast)
Prints a warning to *err* if the AST node is a :def node and the
var name contains earmuffs but the var is not marked dynamic

    
    
    

Source






Public Variables and Functions



function

Usage: (-source-info x env)
Returns the source-info of x

    
    
    

Source



function

Usage: (arglist-for-arity fn argc)
Takes a fn node and an argc and returns the matching arglist

    
    
    

Source



function

Usage: (boolean? x)
Returns true if x is a boolean

    
    
    

Source



function

Usage: (butlast+last s)
Returns same value as (juxt butlast last), but slightly more
efficient since it only traverses the input sequence s once, not
twice.

    
    
    

Source



function

Usage: (classify form)
Returns a keyword describing the form type

    
    
    

Source



function

Usage: (const-val {:keys [form val]})
Returns the value of a constant node (either :quote or :const)

    
    
    

Source



function

Usage: (constant? var)
       (constant? var m)
Returns true if the var is a const

    
    
    

Source



function

Usage: (ctx env ctx)
Returns a copy of the passed environment with :context set to ctx

    
    
    

Source



function

Usage: (dissoc-env ast)
Dissocs :env from the ast

    
    
    

Source



function

Usage: (dynamic? var)
       (dynamic? var m)
Returns true if the var is dynamic

    
    
    

Source



function

Usage: (into! to from)
Like into, but for transients

    
    
    

Source



function

Usage: (macro? var)
       (macro? var m)
Returns true if the var maps to a macro

    
    
    

Source



function

Usage: (mapv' f v)
Like mapv, but short-circuits on reduced

    
    
    

Source



function

Usage: (merge' m & mms)
Like merge, but uses transients

    
    
    

Source



var


    
Same as (fn [m1 m2] (merge-with merge m2 m1))

    
    
    

Source



function

Usage: (obj? x)
Returns true if x implements IObj

    
    
    

Source



function

Usage: (private? var)
       (private? var m)
Returns true if the var is private

    
    
    

Source



function

Usage: (protocol-node? var)
       (protocol-node? var m)
Returns true if the var maps to a protocol function

    
    
    

Source



function

Usage: (record? x)
Returns true if x is a record

    
    
    

Source



function

Usage: (reference? x)
Returns true if x implements IReference

    
    
    

Source



function

Usage: (regex? x)
Returns true if x is a regex

    
    
    

Source



function

Usage: (resolve-ns ns-sym {:keys [ns]})
Resolves the ns mapped by the given sym in the global env

    
    
    

Source



function

Usage: (resolve-sym sym {:keys [ns], :as env})
Resolves the value mapped by the given sym in the global env

    
    
    

Source



function

Usage: (rseqv v)
Same as (comp vec rseq)

    
    
    

Source



function

Usage: (select-keys' map keyseq)
Like clojure.core/select-keys, but uses transients and doesn't preserve meta

    
    
    

Source



function

Usage: (source-info m)
Returns the available source-info keys from a map

    
    
    

Source



function

Usage: (type? x)
Returns true if x is a type

    
    
    

Source



function

Usage: (update-keys m f)
Applies f to all the keys in the map

    
    
    

Source



function

Usage: (update-kv m f)
Applies f to all the keys and vals in the map

    
    
    

Source



function

Usage: (update-vals m f)
Applies f to all the vals in the map

    
    
    

Source