/
code_helpers.ex
168 lines (141 loc) · 4.56 KB
/
code_helpers.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
defmodule Spark.CodeHelpers do
@moduledoc false
def code_identifier(code) do
code
|> strip_meta()
|> :erlang.term_to_binary()
|> :erlang.md5()
|> Base.encode16()
end
def strip_meta(code) do
Macro.prewalk(code, fn
{foo, _, bar} when is_atom(foo) and is_atom(bar) ->
{foo, [], nil}
{foo, _, bar} ->
{foo, [], bar}
other ->
other
end)
end
# sobelow_skip ["DOS.BinToAtom"]
def lift_functions(arg_value, key, caller) do
case arg_value do
{:&, _, [{:/, _, [{{:., _, _}, _, _}, _]}]} = value ->
{value, nil}
{:&, context1, [{:/, context2, [{_, _, _}, arity]}]} = value
when is_integer(arity) ->
fn_args = Macro.generate_unique_arguments(arity, caller.module)
fun_name = Spark.CodeHelpers.code_identifier(value)
fun_name =
:"#{key}_#{Spark.Dsl.Extension.monotonic_number({key, fun_name})}_generated_#{fun_name}"
function =
{:&, context1,
[
{:/, context2,
[
{{:., [], [{:__aliases__, [alias: false], [caller.module]}, fun_name]},
[no_parens: true], []},
Enum.count(fn_args)
]}
]}
{function,
quote generated: true do
@doc false
def unquote(fun_name)(unquote_splicing(fn_args)) do
unquote(value).(unquote_splicing(fn_args))
end
end}
{:&, _, [{name, _, fn_args}]} = value when is_atom(name) ->
fn_args =
Macro.prewalk(fn_args, [], fn
{:&, _, [v]} = ast, acc when is_integer(v) ->
{ast, [v | acc]}
ast, acc ->
{ast, acc}
end)
|> elem(1)
|> Enum.uniq()
|> Enum.count()
|> Macro.generate_unique_arguments(caller.module)
fun_name = Spark.CodeHelpers.code_identifier(value)
fun_name =
:"#{key}_#{Spark.Dsl.Extension.monotonic_number({key, fun_name})}_generated_#{fun_name}"
function =
{:&, [],
[
{:/, [context: Elixir, imports: [{2, Kernel}]],
[
{{:., [], [{:__aliases__, [alias: false], [caller.module]}, fun_name]},
[no_parens: true], []},
Enum.count(fn_args)
]}
]}
{function,
quote generated: true do
@doc false
def unquote(fun_name)(unquote_splicing(fn_args)) do
unquote(value).(unquote_splicing(fn_args))
end
end}
{:&, _,
[
{{:., _, [{:__aliases__, _, _aliases}, name]}, _, fn_args}
]} = value
when is_atom(name) ->
fn_args =
Macro.prewalk(fn_args, [], fn
{:&, _, [v]} = ast, acc when is_integer(v) ->
{ast, [v | acc]}
ast, acc ->
{ast, acc}
end)
|> elem(1)
|> Enum.uniq()
|> Enum.count()
|> Macro.generate_unique_arguments(caller.module)
fun_name = Spark.CodeHelpers.code_identifier(value)
fun_name =
:"#{key}_#{Spark.Dsl.Extension.monotonic_number({key, fun_name})}_generated_#{fun_name}"
function =
{:&, [],
[
{:/, [context: Elixir, imports: [{2, Kernel}]],
[
{{:., [], [{:__aliases__, [alias: false], [caller.module]}, fun_name]},
[no_parens: true], []},
Enum.count(fn_args)
]}
]}
{function,
quote generated: true do
@doc false
def unquote(fun_name)(unquote_splicing(fn_args)) do
unquote(value).(unquote_splicing(fn_args))
end
end}
{:fn, _, [{:->, _, [fn_args, body]}]} = quoted_fn when is_list(fn_args) ->
fun_name = Spark.CodeHelpers.code_identifier(quoted_fn)
fun_name =
:"#{key}_#{Spark.Dsl.Extension.monotonic_number({key, fun_name})}_generated_#{fun_name}"
function =
{:&, [],
[
{:/, [context: Elixir, imports: [{2, Kernel}]],
[
{{:., [], [{:__aliases__, [alias: false], [caller.module]}, fun_name]},
[no_parens: true], []},
Enum.count(fn_args)
]}
]}
{function,
quote generated: true do
@doc false
def unquote(fun_name)(unquote_splicing(fn_args)) do
unquote(body)
end
end}
value ->
{value, nil}
end
end
end