Skip to content

Support for AOT code generation #3

Open
@hyperthunk

Description

@hyperthunk

It would be nice if we could write an annotation handler that is used to generate code at compile time (or when runtime code (re)gen is being applied). The canonical use-case I have for this at the moment is to avoid this:

starts_with(Axis, Value) ->
    binop(starts_with, Axis, Value).

eq(Axis, Value) ->
    binop(eq, Axis, Value).

%% etc
binop(Op, Axis, {literal, _}=Literal) ->
    {Axis, {operator, Op}, Literal}.

And instead to be able to do something like this:

-aliases({[eq, starts_with, ends_with|_Etc], binop, {prefix, function}}). 
binop(Op, Axis, {literal, _}=Literal) ->
    {Axis, {operator, Op}, Literal}.

such that the wrapper functions are generated for me. Quite what the callback/annotation handler module needs to look like isn't clear to me at the moment, but I suspect either a generate_code function or maybe some meta-data to indicate that we want code-gen to occur would do:

-module(aliases).
%% etc...

code_gen(#annotation{data={Aliases, Target, Options}, AST, Options) ->
    {export, [annotations:gen_function(A, redirect_here, Target) || A <- Aliases]}.

around_advice(#annotation{data=Target}, _M, F, _Inputs) ->
    annotation:call_advised(Mod, Target, [F|Inputs]).

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions