1
1
% Holds the logic responsible for function definitions (def(p) and defmacro(p)).
2
2
-module (elixir_def ).
3
- -export ([table /1 , clauses_table /1 , setup /1 ,
4
- cleanup /1 , reset_last /1 , lookup_definition /2 ,
3
+ -export ([setup /1 , reset_last /1 , lookup_definition /2 ,
5
4
delete_definition /2 , store_definition /6 , unwrap_definitions /2 ,
6
- store_each /8 , format_error /1 ]).
5
+ store_each /7 , format_error /1 ]).
7
6
-include (" elixir.hrl" ).
8
7
9
- -define (attr , '__def_table' ).
10
- -define (clauses_attr , '__clauses_table' ).
8
+ -define (last_def , {elixir , last_def }).
9
+ -define (attr , {elixir , def_table }).
10
+ -define (clauses_attr , {elixir , clauses_table }).
11
11
12
12
% % Table management functions. Called internally.
13
13
14
- table (Module ) ->
15
- ets :lookup_element (Module , ? attr , 2 ).
16
-
17
- clauses_table (Module ) ->
18
- ets :lookup_element (Module , ? clauses_attr , 2 ).
19
-
20
14
setup (Module ) ->
21
- ets :insert (Module , {? attr , ets :new (Module , [set , public ])}),
22
- ets :insert (Module , {? clauses_attr , ets :new (Module , [bag , public ])}),
23
15
reset_last (Module ),
24
16
ok .
25
17
26
- cleanup (Module ) ->
27
- ets :delete (table (Module )),
28
- ets :delete (clauses_table (Module )).
29
-
30
- % % Reset the last item. Useful when evaling code.
31
18
reset_last (Module ) ->
32
- ets :insert (table (Module ), {last , []}).
19
+ ets :insert (elixir_module : data_table (Module ), {? last_def , []}).
33
20
34
- % % Looks up a definition from the database.
35
21
lookup_definition (Module , Tuple ) ->
36
- case ets :lookup (table (Module ), Tuple ) of
22
+ case ets :lookup (elixir_module : defs_table (Module ), Tuple ) of
37
23
[Result ] ->
38
- CTable = clauses_table (Module ),
24
+ CTable = elixir_module : clas_table (Module ),
39
25
{Result , [Clause || {_ , Clause } <- ets :lookup (CTable , Tuple )]};
40
26
_ ->
41
27
false
42
28
end .
43
29
44
30
delete_definition (Module , Tuple ) ->
45
- ets :delete (table (Module ), Tuple ),
46
- ets :delete (clauses_table (Module ), Tuple ).
31
+ ets :delete (elixir_module : defs_table (Module ), Tuple ),
32
+ ets :delete (elixir_module : clas_table (Module ), Tuple ).
47
33
48
34
% Invoked by the wrap definition with the function abstract tree.
49
35
% Each function is then added to the function table.
@@ -98,16 +84,12 @@ store_definition(Line, Kind, CheckClauses, Name, Args, Guards, Body, KeepLocatio
98
84
DefaultsLength = length (Defaults ),
99
85
elixir_locals :record_defaults (Tuple , Kind , Module , DefaultsLength ),
100
86
101
- File = ? m (E , file ),
102
- Table = table (Module ),
103
- CTable = clauses_table (Module ),
104
-
87
+ File = ? m (E , file ),
105
88
compile_super (Module , Super , E ),
106
- check_previous_defaults (Table , Line , Name , Arity , Kind , DefaultsLength , E ),
89
+ check_previous_defaults (Line , Module , Name , Arity , Kind , DefaultsLength , E ),
107
90
108
- store_each (CheckClauses , Kind , File , Location ,
109
- Table , CTable , DefaultsLength , Function ),
110
- [store_each (false , Kind , File , Location , Table , CTable , 0 ,
91
+ store_each (CheckClauses , Kind , File , Location , Module , DefaultsLength , Function ),
92
+ [store_each (false , Kind , File , Location , Module , 0 ,
111
93
default_function_for (Kind , Name , Default )) || Default <- Defaults ],
112
94
113
95
make_struct_available (Kind , Module , Name , Args ),
@@ -166,7 +148,7 @@ normalize_location(File) ->
166
148
% % Compile super
167
149
168
150
compile_super (Module , true , #{function := Function }) ->
169
- elixir_def_overridable :store (Module , Function , true );
151
+ elixir_def_overridable :super (Module , Function );
170
152
compile_super (_Module , _ , _E ) -> ok .
171
153
172
154
% % Translate the given call and expression given
@@ -229,9 +211,8 @@ is_macro(_) -> false.
229
211
% It returns a list of all functions to be exported, plus the macros,
230
212
% and the body of all functions.
231
213
unwrap_definitions (File , Module ) ->
232
- Table = table (Module ),
233
- CTable = clauses_table (Module ),
234
- ets :delete (Table , last ),
214
+ Table = elixir_module :defs_table (Module ),
215
+ CTable = elixir_module :clas_table (Module ),
235
216
236
217
{All , Private } = unwrap_definition (ets :tab2list (Table ), File , Module , CTable , [], []),
237
218
Unreachable = elixir_locals :warn_unused_local (File , Module , Private ),
@@ -327,26 +308,33 @@ warn_bodyless_function(Line, File, _Module, Kind, Tuple) ->
327
308
% % This function also checks and emit warnings in case
328
309
% % the kind, of the visibility of the function changes.
329
310
330
- store_each (Check , Kind , File , Location , Table , CTable , Defaults , {function , Line , Name , Arity , Clauses }) ->
331
- Tuple = {Name , Arity },
311
+ store_each (Check , Kind , File , Location , Module , Defaults , {function , Line , Name , Arity , Clauses }) ->
312
+ Data = elixir_module :data_table (Module ),
313
+ Defs = elixir_module :defs_table (Module ),
314
+ Clas = elixir_module :clas_table (Module ),
315
+
316
+ Tuple = {Name , Arity },
332
317
HasBody = Clauses =/= [],
333
- case ets :lookup (Table , Tuple ) of
334
- [{Tuple , StoredKind , StoredLine , StoredFile , StoredCheck , StoredLocation , {StoredDefaults , LastHasBody , LastDefaults }}] ->
318
+
319
+ case ets :lookup (Defs , Tuple ) of
320
+ [{Tuple , StoredKind , StoredLine , StoredFile , StoredCheck ,
321
+ StoredLocation , {StoredDefaults , LastHasBody , LastDefaults }}] ->
335
322
FinalLine = StoredLine ,
336
323
FinalLocation = StoredLocation ,
337
324
FinalDefaults = {max (Defaults , StoredDefaults ), HasBody , Defaults },
338
325
check_valid_kind (Line , File , Name , Arity , Kind , StoredKind ),
339
326
(Check and StoredCheck ) andalso
340
- check_valid_clause (Line , File , Name , Arity , Kind , Table , StoredLine , StoredFile ),
327
+ check_valid_clause (Line , File , Name , Arity , Kind , Data , StoredLine , StoredFile ),
341
328
check_valid_defaults (Line , File , Name , Arity , Kind , Defaults , LastDefaults , LastHasBody );
342
329
[] ->
343
330
FinalLine = Line ,
344
331
FinalLocation = Location ,
345
332
FinalDefaults = {Defaults , HasBody , Defaults }
346
333
end ,
347
- Check andalso ets :insert (Table , {last , {Name , Arity }}),
348
- ets :insert (CTable , [{Tuple , Clause } || Clause <- Clauses ]),
349
- ets :insert (Table , {Tuple , Kind , FinalLine , File , Check , FinalLocation , FinalDefaults }).
334
+
335
+ Check andalso ets :insert (Data , {? last_def , {Name , Arity }}),
336
+ ets :insert (Clas , [{Tuple , Clause } || Clause <- Clauses ]),
337
+ ets :insert (Defs , {Tuple , Kind , FinalLine , File , Check , FinalLocation , FinalDefaults }).
350
338
351
339
% % Validations
352
340
@@ -355,8 +343,8 @@ check_valid_kind(Line, File, Name, Arity, Kind, StoredKind) ->
355
343
elixir_errors :form_error ([{line , Line }], File , ? MODULE ,
356
344
{changed_kind , {Name , Arity , StoredKind , Kind }}).
357
345
358
- check_valid_clause (Line , File , Name , Arity , Kind , Table , StoredLine , StoredFile ) ->
359
- case ets :lookup_element (Table , last , 2 ) of
346
+ check_valid_clause (Line , File , Name , Arity , Kind , Data , StoredLine , StoredFile ) ->
347
+ case ets :lookup_element (Data , ? last_def , 2 ) of
360
348
{Name ,Arity } -> [];
361
349
[] -> [];
362
350
_ ->
@@ -379,8 +367,8 @@ check_valid_defaults(Line, File, Name, Arity, Kind, _, _, _) ->
379
367
elixir_errors :form_error ([{line , Line }], File , ? MODULE ,
380
368
{clauses_with_defaults , {Kind , Name , Arity }}).
381
369
382
- check_previous_defaults (Table , Line , Name , Arity , Kind , Defaults , E ) ->
383
- Matches = ets :match (Table , {{Name , '$2' }, '$1' , '_' , '_' , '_' , '_' , {'$3' , '_' , '_' }}),
370
+ check_previous_defaults (Line , Module , Name , Arity , Kind , Defaults , E ) ->
371
+ Matches = ets :match (elixir_module : defs_table ( Module ) , {{Name , '$2' }, '$1' , '_' , '_' , '_' , '_' , {'$3' , '_' , '_' }}),
384
372
[ begin
385
373
elixir_errors :form_error ([{line , Line }], ? m (E , file ), ? MODULE ,
386
374
{defs_with_defaults , Name , {Kind , Arity }, {K , A }})
0 commit comments