Skip to content

Commit ffc91e3

Browse files
author
José Valim
committed
Rename Kernel.LocalsTracker to Module.DispatchTracker
1 parent 32cc997 commit ffc91e3

15 files changed

+97
-94
lines changed

lib/elixir/lib/kernel/locals_tracker.ex renamed to lib/elixir/lib/module/dispatch_tracker.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#
4343
# Note that since this is required for bootstrap, we can't use
4444
# any of the `GenServer.Behaviour` conveniences.
45-
defmodule Kernel.LocalsTracker do
45+
defmodule Module.DispatchTracker do
4646
@moduledoc false
4747

4848
@timeout 30_000
@@ -81,9 +81,9 @@ defmodule Kernel.LocalsTracker do
8181
end
8282

8383
defp reduce_reachable(d, vertex, vertices) do
84-
neighbours = :digraph.out_neighbours(d, vertex)
85-
remaining = neighbours -- vertices
86-
vertices = neighbours ++ vertices
84+
neighbours = :digraph.out_neighbours(d, vertex) |> :ordsets.from_list
85+
remaining = :ordsets.subtract(neighbours, vertices)
86+
vertices = :ordsets.union(neighbours, vertices)
8787
:lists.foldl(reduce_reachable(d, &1, &2), vertices, remaining)
8888
end
8989

lib/elixir/src/elixir_clauses.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
%% Handle code related to rocket args, guard and -> matching
2-
%% for case, fn, receive and friends. try is handled in elixir_try.
1+
%% Handle code related to args, guard and -> matching for case,
2+
%% fn, receive and friends. try is handled in elixir_try.
33
-module(elixir_clauses).
44
-export([
55
assigns/3, assigns_block/5, assigns_block/6, extract_last_guards/1,

lib/elixir/src/elixir_compiler.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ core_main() ->
225225
"lib/elixir/lib/kernel/cli.ex",
226226
"lib/elixir/lib/kernel/error_handler.ex",
227227
"lib/elixir/lib/kernel/parallel_compiler.ex",
228-
"lib/elixir/lib/kernel/locals_tracker.ex",
229-
"lib/elixir/lib/kernel/record_rewriter.ex"
228+
"lib/elixir/lib/kernel/record_rewriter.ex",
229+
"lib/elixir/lib/module/dispatch_tracker.ex"
230230
].
231231

232232
%% ERROR HANDLING

lib/elixir/src/elixir_def_local.erl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
%% Module responsible for local invocation of macros and functions.
2+
-module(elixir_def_local).
3+
-export([macro_for/3, function_for/3]).
4+
-include("elixir.hrl").
5+
6+
macro_for(_Tuple, _All, #elixir_scope{module=nil}) -> false;
7+
8+
macro_for(Tuple, All, #elixir_scope{module=Module} = S) ->
9+
try elixir_def:lookup_definition(Module, Tuple) of
10+
{ { Tuple, Kind, Line, _, _, _, _ }, Clauses } when Kind == defmacro; All, Kind == defmacrop ->
11+
elixir_tracker:record_local(Tuple, S),
12+
get_function(Line, Module, Clauses);
13+
_ ->
14+
false
15+
catch
16+
error:badarg -> false
17+
end.
18+
19+
function_for(Module, Name, Arity) ->
20+
Tuple = { Name, Arity },
21+
case elixir_def:lookup_definition(Module, Tuple) of
22+
{ { Tuple, _, Line, _, _, _, _ }, Clauses } ->
23+
%% There is no need to record such calls
24+
%% since they come from funtions that were
25+
%% already analyzed at compilation time.
26+
get_function(Line, Module, Clauses);
27+
_ ->
28+
[_|T] = erlang:get_stacktrace(),
29+
erlang:raise(error, undef, [{Module,Name,Arity,[]}|T])
30+
end.
31+
32+
get_function(Line, Module, Clauses) ->
33+
RewrittenClauses = [rewrite_clause(Clause, Module) || Clause <- Clauses],
34+
Fun = { 'fun', Line, {clauses, RewrittenClauses } },
35+
{ value, Result, _Binding } = erl_eval:exprs([Fun], []),
36+
Result.
37+
38+
rewrite_clause({ call, Line, { atom, Line, RawName }, Args }, Module) ->
39+
Remote = { remote, Line,
40+
{ atom, Line, ?MODULE },
41+
{ atom, Line, function_for }
42+
},
43+
44+
%% If we have a macro, its arity in the table is
45+
%% actually one less than in the function call
46+
{ Name, Arity } = case atom_to_list(RawName) of
47+
"MACRO-" ++ Rest -> { list_to_atom(Rest), length(Args) - 1 };
48+
_ -> { RawName, length(Args) }
49+
end,
50+
51+
FunCall = { call, Line, Remote, [
52+
{ atom, Line, Module }, { atom, Line, Name }, { integer, Line, Arity }
53+
] },
54+
{ call, Line, FunCall, Args };
55+
56+
rewrite_clause(Tuple, Module) when is_tuple(Tuple) ->
57+
list_to_tuple(rewrite_clause(tuple_to_list(Tuple), Module));
58+
59+
rewrite_clause(List, Module) when is_list(List) ->
60+
[rewrite_clause(Item, Module) || Item <- List];
61+
62+
rewrite_clause(Else, _) -> Else.

lib/elixir/src/elixir_dispatch.erl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import_function(Meta, Name, Arity, S) ->
3434
Tuple = { Name, Arity },
3535
case find_dispatch(Meta, Tuple, S) of
3636
{ function, Receiver } ->
37-
elixir_locals:record_import(Tuple, Receiver, S#elixir_scope.module),
37+
elixir_tracker:record_import(Tuple, Receiver, S#elixir_scope.module),
3838
remote_function(Meta, Receiver, Name, Arity, S);
3939
{ macro, _Receiver } ->
4040
false;
4141
{ import, Receiver } ->
4242
require_function(Meta, Receiver, Name, Arity, S);
4343
nomatch ->
44-
elixir_locals:record_local(Tuple, S),
44+
elixir_tracker:record_local(Tuple, S),
4545
{ { 'fun', ?line(Meta), { function, Name, Arity } }, S }
4646
end.
4747

@@ -62,7 +62,7 @@ dispatch_import(Meta, Name, Args, S, Callback) ->
6262

6363
case find_dispatch(Meta, Tuple, S) of
6464
{ function, Receiver } ->
65-
elixir_locals:record_import(Tuple, Receiver, Module),
65+
elixir_tracker:record_import(Tuple, Receiver, Module),
6666
Endpoint = case (Receiver == ?builtin) andalso is_element(Tuple, in_erlang_functions()) of
6767
true -> erlang;
6868
false -> Receiver
@@ -75,7 +75,7 @@ dispatch_import(Meta, Name, Args, S, Callback) ->
7575
{ error, noexpansion } ->
7676
Callback();
7777
{ error, internal } ->
78-
elixir_locals:record_import(Tuple, ?builtin, Module),
78+
elixir_tracker:record_import(Tuple, ?builtin, Module),
7979
elixir_macros:translate({ Name, Meta, Args }, S);
8080
{ ok, _Receiver, Tree } ->
8181
translate_expansion(Meta, Tree, S)
@@ -114,17 +114,17 @@ do_expand_import(Meta, { Name, Arity } = Tuple, Args, Module, S, Result) ->
114114
case is_element(Tuple, in_erlang_macros()) of
115115
true -> { error, internal };
116116
false ->
117-
elixir_locals:record_import(Tuple, ?builtin, Module),
117+
elixir_tracker:record_import(Tuple, ?builtin, Module),
118118
{ ok, ?builtin, expand_macro_named(Meta, ?builtin, Name, Arity, Args, Module, S) }
119119
end;
120120
{ macro, Receiver } ->
121-
elixir_locals:record_import(Tuple, Receiver, Module),
121+
elixir_tracker:record_import(Tuple, Receiver, Module),
122122
{ ok, Receiver, expand_macro_named(Meta, Receiver, Name, Arity, Args, Module, S) };
123123
{ import, Receiver } ->
124124
expand_require(Meta, Receiver, Tuple, Args, Module, S);
125125
_ ->
126126
Fun = (S#elixir_scope.function /= Tuple) andalso
127-
elixir_locals:macro_for(Tuple, true, S),
127+
elixir_def_local:macro_for(Tuple, true, S),
128128
case Fun of
129129
false -> { error, noexpansion };
130130
_ ->
@@ -144,7 +144,7 @@ expand_require(Meta, ?builtin, { Name, Arity } = Tuple, Args, Module, S) ->
144144

145145
expand_require(Meta, Receiver, { Name, Arity } = Tuple, Args, Module, S) ->
146146
Fun = (Module == Receiver) andalso (S#elixir_scope.function /= Tuple) andalso
147-
elixir_locals:macro_for(Tuple, false, S),
147+
elixir_def_local:macro_for(Tuple, false, S),
148148

149149
case Fun of
150150
false ->

lib/elixir/src/elixir_import.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ record_warn(Meta, Ref, Opts, #elixir_scope{module=Module}) ->
4949
false -> not lists:keymember(context, 1, Meta)
5050
end,
5151

52-
elixir_locals:record_warn(Ref, Warn, ?line(Meta), Module).
52+
elixir_tracker:record_warn(Ref, Warn, ?line(Meta), Module).
5353

5454
%% Calculates the imports based on only and except
5555

lib/elixir/src/elixir_literal.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% Handle translation of Elixir literals to Erlang AST.
12
-module(elixir_literal).
23
-export([translate/2]).
34
-import(elixir_translator, [translate_each/2, translate_args/2]).

lib/elixir/src/elixir_module.erl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ compile(Line, Module, Block, Vars, #elixir_scope{context_modules=FileModules} =
7777
case ets:lookup(data_table(Module), 'on_load') of
7878
[] -> ok;
7979
[{on_load,OnLoad}] ->
80-
[elixir_locals:record_local(Tuple, Module) || Tuple <- OnLoad]
80+
[elixir_tracker:record_local(Tuple, Module) || Tuple <- OnLoad]
8181
end,
8282

83-
elixir_locals:warn_unused_local(File, Module, Private),
84-
elixir_locals:ensure_no_import_conflict(Line, File, Module, All),
85-
elixir_locals:ensure_all_imports_used(Line, File, Module),
83+
elixir_tracker:warn_unused_local(File, Module, Private),
84+
elixir_tracker:ensure_no_import_conflict(Line, File, Module, All),
85+
elixir_tracker:ensure_all_imports_used(Line, File, Module),
8686

8787
elixir_import:ensure_no_local_conflict(Line, File, Module, All),
8888

@@ -94,7 +94,7 @@ compile(Line, Module, Block, Vars, #elixir_scope{context_modules=FileModules} =
9494
Binary = load_form(Line, Final, S),
9595
{ module, Module, Binary, Result }
9696
after
97-
elixir_locals:cleanup(Module),
97+
elixir_tracker:cleanup(Module),
9898
elixir_def:cleanup(Module),
9999
ets:delete(docs_table(Module)),
100100
ets:delete(data_table(Module))
@@ -136,7 +136,7 @@ build(Line, File, Module) ->
136136

137137
%% Setup other modules
138138
elixir_def:setup(Module),
139-
elixir_locals:setup(Module).
139+
elixir_tracker:setup(Module).
140140

141141
%% Receives the module representation and evaluates it.
142142

lib/elixir/src/elixir_partials.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% Responsible handling Elixir partials with &1, &2 and so on.
12
-module(elixir_partials).
23
-export([handle/2, handle/3, is_sequential/1]).
34
-include("elixir.hrl").

lib/elixir/src/elixir_quote.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% Implements Elixir quote.
12
-module(elixir_quote).
23
-export([escape/2, erl_escape/3, erl_quote/3, linify/2, unquote/5]).
34
-include("elixir.hrl").

0 commit comments

Comments
 (0)