hyperthunk/annotations — Issue #3: Support for AOT code generation
Author: hyperthunk
Created: 2012-01-13 17:11:21+00:00
State: open

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:

```erlang
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:

```erlang
-aliases({[eq, starts_with, ends_with|_Etc], {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:

```erlang
-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]).
```