Instructing the compiler (the :meta
mechanism)
Sometimes it may be necessary to indicate that a given block of code has special properties: you may always need to embed it or include special compiler optimization passes. Since version 0.4 in Julia, there is an agreement that these instructions can be placed inside the expression :meta
, which is usually (but not necessarily) the first expression in the function body.
The expressions :meta
are created using macros. As an example, consider the implementation of the macro @inline
.
macro inline(ex)
esc(isa(ex, Expr) ? pushmeta!(ex, :inline) : ex)
end
Here, ex
is expected to be an expression defining a function. An operator similar to this:
@inline function myfunction(x)
x*(x+3)
end
is converted to an expression similar to this:
quote
function myfunction(x)
Expr(:meta, :inline)
x*(x+3)
end
end
Base.pushmeta!(ex, tag::Union{Symbol,Expr})
adds :tag
to the end of the expression :meta
, creating a new expression :meta
if necessary.
To use metadata, it is necessary to analyze these expressions :meta'. If the implementation can be done in Julia, it is quite convenient to use `Base.popmeta!
. Base.popmeta!(body, :symbol)
scans the function body expression (without the function signature) for the first :meta
expression containing :symbol
, extracts all arguments, and returns the tuple (found::Bool, args::Array{Any})
. If the metadata does not contain arguments or :symbol
couldn’t be found, the `args' array will be empty.
Efficient infrastructure for parsing expressions :meta
from C++ currently missing.