2
2
% % between local functions and imports.
3
3
% % For imports dispatch, please check elixir_dispatch.
4
4
-module (elixir_import ).
5
- -export ([import /6 , special_form /2 , format_error /1 ]).
5
+ -export ([import /6 , import / 7 , special_form /2 , format_error /1 ]).
6
6
-compile (inline_list_funcs ).
7
7
-include (" elixir.hrl" ).
8
8
9
9
import (Meta , Ref , Opts , E , Warn , Trace ) ->
10
- case import_only_except (Meta , Ref , Opts , E , Warn ) of
10
+ import (Meta , Ref , Opts , E , Warn , Trace , nil ).
11
+
12
+ import (Meta , Ref , Opts , E , Warn , Trace , InfoCallback ) when is_function (InfoCallback , 1 ) ->
13
+ case import_only_except (Meta , Ref , Opts , E , Warn , InfoCallback ) of
11
14
{Functions , Macros , Added } ->
12
15
Trace andalso elixir_env :trace ({import , [{imported , Added } | Meta ], Ref , Opts }, E ),
13
16
EI = E #{functions := Functions , macros := Macros },
14
17
{ok , elixir_aliases :require (Meta , Ref , Opts , EI , Trace )};
15
18
16
19
{error , Reason } ->
17
20
{error , Reason }
18
- end .
21
+ end ;
22
+ import (Meta , Ref , Opts , E , Warn , Trace , _ ) ->
23
+ import (Meta , Ref , Opts , E , Warn , Trace , info_callback (Ref )).
19
24
20
- import_only_except (Meta , Ref , Opts , E , Warn ) ->
25
+ import_only_except (Meta , Ref , Opts , E , Warn , InfoCallback ) ->
21
26
MaybeOnly = lists :keyfind (only , 1 , Opts ),
22
27
23
28
case lists :keyfind (except , 1 , Opts ) of
24
29
false ->
25
- import_only_except (Meta , Ref , MaybeOnly , false , E , Warn );
30
+ import_only_except (Meta , Ref , MaybeOnly , false , E , Warn , InfoCallback );
26
31
27
32
{except , DupExcept } when is_list (DupExcept ) ->
28
33
case ensure_keyword_list (DupExcept ) of
29
34
ok ->
30
35
Except = ensure_no_duplicates (DupExcept , except , Meta , E , Warn ),
31
- import_only_except (Meta , Ref , MaybeOnly , Except , E , Warn );
36
+ import_only_except (Meta , Ref , MaybeOnly , Except , E , Warn , InfoCallback );
32
37
33
38
error ->
34
39
{error , {invalid_option , except , DupExcept }}
@@ -38,27 +43,27 @@ import_only_except(Meta, Ref, Opts, E, Warn) ->
38
43
{error , {invalid_option , except , Other }}
39
44
end .
40
45
41
- import_only_except (Meta , Ref , MaybeOnly , Except , E , Warn ) ->
46
+ import_only_except (Meta , Ref , MaybeOnly , Except , E , Warn , InfoCallback ) ->
42
47
case MaybeOnly of
43
48
{only , functions } ->
44
- {Added1 , _Used1 , Funs } = import_functions (Meta , Ref , Except , E , Warn ),
49
+ {Added1 , _Used1 , Funs } = import_functions (Meta , Ref , Except , E , Warn , InfoCallback ),
45
50
{Funs , keydelete (Ref , ? key (E , macros )), Added1 };
46
51
47
52
{only , macros } ->
48
- {Added2 , _Used2 , Macs } = import_macros (Meta , Ref , Except , E , Warn ),
53
+ {Added2 , _Used2 , Macs } = import_macros (Meta , Ref , Except , E , Warn , InfoCallback ),
49
54
{keydelete (Ref , ? key (E , functions )), Macs , Added2 };
50
55
51
56
{only , sigils } ->
52
- {Added1 , _Used1 , Funs } = import_sigil_functions (Meta , Ref , Except , E , Warn ),
53
- {Added2 , _Used2 , Macs } = import_sigil_macros (Meta , Ref , Except , E , Warn ),
57
+ {Added1 , _Used1 , Funs } = import_sigil_functions (Meta , Ref , Except , E , Warn , InfoCallback ),
58
+ {Added2 , _Used2 , Macs } = import_sigil_macros (Meta , Ref , Except , E , Warn , InfoCallback ),
54
59
{Funs , Macs , Added1 or Added2 };
55
60
56
61
{only , DupOnly } when is_list (DupOnly ) ->
57
62
case ensure_keyword_list (DupOnly ) of
58
63
ok when Except =:= false ->
59
64
Only = ensure_no_duplicates (DupOnly , only , Meta , E , Warn ),
60
- {Added1 , Used1 , Funs } = import_listed_functions (Meta , Ref , Only , E , Warn ),
61
- {Added2 , Used2 , Macs } = import_listed_macros (Meta , Ref , Only , E , Warn ),
65
+ {Added1 , Used1 , Funs } = import_listed_functions (Meta , Ref , Only , E , Warn , InfoCallback ),
66
+ {Added2 , Used2 , Macs } = import_listed_macros (Meta , Ref , Only , E , Warn , InfoCallback ),
62
67
[Warn andalso elixir_errors :file_warn (Meta , E , ? MODULE , {invalid_import , {Ref , Name , Arity }}) ||
63
68
{Name , Arity } <- (Only -- Used1 ) -- Used2 ],
64
69
{Funs , Macs , Added1 or Added2 };
@@ -74,37 +79,37 @@ import_only_except(Meta, Ref, MaybeOnly, Except, E, Warn) ->
74
79
{error , {invalid_option , only , Other }};
75
80
76
81
false ->
77
- {Added1 , _Used1 , Funs } = import_functions (Meta , Ref , Except , E , Warn ),
78
- {Added2 , _Used2 , Macs } = import_macros (Meta , Ref , Except , E , Warn ),
82
+ {Added1 , _Used1 , Funs } = import_functions (Meta , Ref , Except , E , Warn , InfoCallback ),
83
+ {Added2 , _Used2 , Macs } = import_macros (Meta , Ref , Except , E , Warn , InfoCallback ),
79
84
{Funs , Macs , Added1 or Added2 }
80
85
end .
81
86
82
- import_listed_functions (Meta , Ref , Only , E , Warn ) ->
83
- New = intersection (Only , get_functions ( Ref )),
87
+ import_listed_functions (Meta , Ref , Only , E , Warn , InfoCallback ) ->
88
+ New = intersection (Only , InfoCallback ( functions )),
84
89
calculate_key (Meta , Ref , ? key (E , functions ), New , E , Warn ).
85
90
86
- import_listed_macros (Meta , Ref , Only , E , Warn ) ->
87
- New = intersection (Only , get_macros ( Ref )),
91
+ import_listed_macros (Meta , Ref , Only , E , Warn , InfoCallback ) ->
92
+ New = intersection (Only , InfoCallback ( macros )),
88
93
calculate_key (Meta , Ref , ? key (E , macros ), New , E , Warn ).
89
94
90
- import_functions (Meta , Ref , Except , E , Warn ) ->
95
+ import_functions (Meta , Ref , Except , E , Warn , InfoCallback ) ->
91
96
calculate_except (Meta , Ref , Except , ? key (E , functions ), E , Warn , fun () ->
92
- get_functions ( Ref )
97
+ InfoCallback ( functions )
93
98
end ).
94
99
95
- import_macros (Meta , Ref , Except , E , Warn ) ->
100
+ import_macros (Meta , Ref , Except , E , Warn , InfoCallback ) ->
96
101
calculate_except (Meta , Ref , Except , ? key (E , macros ), E , Warn , fun () ->
97
- get_macros ( Ref )
102
+ InfoCallback ( macros )
98
103
end ).
99
104
100
- import_sigil_functions (Meta , Ref , Except , E , Warn ) ->
105
+ import_sigil_functions (Meta , Ref , Except , E , Warn , InfoCallback ) ->
101
106
calculate_except (Meta , Ref , Except , ? key (E , functions ), E , Warn , fun () ->
102
- filter_sigils (get_functions ( Ref ))
107
+ filter_sigils (InfoCallback ( functions ))
103
108
end ).
104
109
105
- import_sigil_macros (Meta , Ref , Except , E , Warn ) ->
110
+ import_sigil_macros (Meta , Ref , Except , E , Warn , InfoCallback ) ->
106
111
calculate_except (Meta , Ref , Except , ? key (E , macros ), E , Warn , fun () ->
107
- filter_sigils (get_macros ( Ref ))
112
+ filter_sigils (InfoCallback ( macros ))
108
113
end ).
109
114
110
115
calculate_except (Meta , Key , false , Old , E , Warn , Existing ) ->
@@ -134,14 +139,17 @@ calculate_key(Meta, Key, Old, New, E, Warn) ->
134
139
135
140
% % Retrieve functions and macros from modules
136
141
137
- get_functions (Module ) ->
142
+ info_callback (Module ) ->
143
+ fun (Kind ) -> info_callback (Module , Kind ) end .
144
+
145
+ info_callback (Module , functions ) ->
138
146
try
139
147
Module :'__info__' (functions )
140
148
catch
141
149
error :undef -> remove_internals (Module :module_info (exports ))
142
- end .
150
+ end ;
143
151
144
- get_macros (Module ) ->
152
+ info_callback (Module , macros ) ->
145
153
try
146
154
Module :'__info__' (macros )
147
155
catch
0 commit comments