Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enable record rewriter inference

  • Loading branch information...
commit 82cf622e9ba66a04837b90715084d407592bb281 1 parent 088ef2f
@josevalim josevalim authored
View
7 lib/elixir/lib/kernel/record_rewriter.ex
@@ -171,8 +171,11 @@ defmodule Kernel.RecordRewriter do
end
defp optimize_expr({ :fun, line, { :clauses, clauses } }, dict) do
- tuples = lc clause inlist clauses, do: optimize_clause(clause, dict)
- clauses = lc { clause, _, _ } inlist tuples, do: clause
+ clauses = lc clause inlist clauses do
+ { clause, _, _ } = optimize_clause(clause, dict)
+ clause
+ end
+
{ { :fun, line, { :clauses, clauses } }, dict, nil }
end
View
3  lib/elixir/src/elixir_compiler.erl
@@ -178,7 +178,8 @@ core_main() ->
"lib/elixir/lib/system.ex",
"lib/elixir/lib/kernel/cli.ex",
"lib/elixir/lib/kernel/error_handler.ex",
- "lib/elixir/lib/kernel/parallel_compiler.ex"
+ "lib/elixir/lib/kernel/parallel_compiler.ex",
+ "lib/elixir/lib/kernel/record_rewriter.ex"
].
%% ERROR HANDLING
View
6 lib/elixir/src/elixir_def.erl
@@ -93,7 +93,7 @@ store_definition(Kind, Line, Module, Name, Args, Guards, Body, RawS) ->
end,
[store_each(false, Kind, File, Location, Table, 0,
- function_for_default(Kind, Name, Default)) || Default <- Defaults],
+ default_function_for(Kind, Name, Default)) || Default <- Defaults],
{ Name, Arity }.
@@ -241,11 +241,11 @@ function_for_stored_definition({{Name, Arity}, _, Line, _, _, Location, _, Claus
]
}.
-function_for_default(Kind, Name, { clause, Line, Args, _Guards, _Exprs } = Clause)
+default_function_for(Kind, Name, { clause, Line, Args, _Guards, _Exprs } = Clause)
when Kind == defmacro; Kind == defmacrop ->
{ function, Line, Name, length(Args) - 1, [Clause] };
-function_for_default(_, Name, { clause, Line, Args, _Guards, _Exprs } = Clause) ->
+default_function_for(_, Name, { clause, Line, Args, _Guards, _Exprs } = Clause) ->
{ function, Line, Name, length(Args), [Clause] }.
%% Store each definition in the table.
View
33 lib/elixir/src/elixir_module.erl
@@ -40,17 +40,8 @@ docs_table(Module) ->
%% will be passed to the invoked function.
translate(Line, Ref, Block, S) ->
- MetaBlock = elixir_tree_helpers:abstract_syntax(Block),
- MetaS = elixir_scope:serialize(S),
-
- { Vars, _ } = orddict:fold(fun({ Key, Kind }, Value, { Acc, Counter }) ->
- { { cons, Line, { tuple, Line, [
- { atom, Line, Key },
- { atom, Line, Kind },
- { atom, Line, ?ELIXIR_ATOM_CONCAT(["_@", Counter]) },
- { var, Line, Value }
- ] }, Acc }, Counter + 1 }
- end, { { nil, Line }, 0 }, S#elixir_scope.vars),
+ MetaBlock = elixir_tree_helpers:abstract_syntax(Block),
+ { MetaS, Vars } = elixir_scope:serialize_with_vars(Line, S),
Args = [{integer, Line, Line}, Ref, MetaBlock, Vars, MetaS],
?ELIXIR_WRAP_CALL(Line, ?MODULE, compile, Args).
@@ -94,7 +85,7 @@ compile(Line, Other, _Block, _Vars, #elixir_scope{file=File}) ->
compile(Line, Module, Block, Vars, RawS) ->
Dict = [{ { Name, Kind }, Value } || { Name, Kind, Value, _ } <- Vars],
- S = elixir_scope:deserialize(RawS, Dict),
+ S = elixir_scope:deserialize_with_vars(RawS, Dict),
compile(Line, Module, Block, Vars, S).
%% Hook that builds both attribute and functions and set up common hooks.
@@ -145,7 +136,12 @@ eval_form(Line, Module, Block, Vars, RawS) ->
Value.
%% Return the form with exports and function declarations.
-functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Functions, C) ->
+functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, RawFunctions, C) ->
+ Functions = case elixir_compiler:get_opt(internal, C) of
+ true -> RawFunctions;
+ false -> record_rewrite_functions(RawFunctions)
+ end,
+
{ FinalExport, FinalFunctions } =
add_info_function(Line, File, Module, Export, Functions, Def, Defmacro, C),
@@ -156,6 +152,17 @@ functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Fu
{attribute, Line, export, lists:sort(FinalExport)} | FinalFunctions
] }.
+record_rewrite_functions(Functions) ->
+ lists:map(fun
+ ({ function, Line, Name, Arity, Clauses }) ->
+ Rewriten = lists:map(fun(Clause) ->
+ { C, _, _ } = 'Elixir.Kernel.RecordRewriter':optimize_clause(Clause),
+ C
+ end, Clauses),
+ { function, Line, Name, Arity, Rewriten };
+ (Other) -> Other
+ end, Functions).
+
%% Add attributes handling to the form
attributes_form(Line, _File, Module, Current) ->
View
18 lib/elixir/src/elixir_scope.erl
@@ -5,7 +5,8 @@
build_erl_var/2, build_ex_var/2,
build_erl_var/3, build_ex_var/3,
build_erl_var/4, build_ex_var/4,
- serialize/1, deserialize/1, deserialize/2,
+ serialize/1, deserialize/1,
+ serialize_with_vars/2, deserialize_with_vars/2,
to_erl_env/1, to_ex_env/1, filename/1,
umergev/2, umergec/2, merge_clause_vars/2
]).
@@ -97,11 +98,22 @@ serialize(S) ->
S#elixir_scope.requires, S#elixir_scope.macros, S#elixir_scope.aliases, S#elixir_scope.scheduled }
).
+serialize_with_vars(Line, S) ->
+ { Vars, _ } = orddict:fold(fun({ Key, Kind }, Value, { Acc, Counter }) ->
+ { { cons, Line, { tuple, Line, [
+ { atom, Line, Key },
+ { atom, Line, Kind },
+ { atom, Line, ?ELIXIR_ATOM_CONCAT(["_@", Counter]) },
+ { var, Line, Value }
+ ] }, Acc }, Counter + 1 }
+ end, { { nil, Line }, 0 }, S#elixir_scope.vars),
+ { serialize(S), Vars }.
+
% Fill in the scope with the variables serialization set in serialize_scope.
-deserialize(Tuple) -> deserialize(Tuple, []).
+deserialize(Tuple) -> deserialize_with_vars(Tuple, []).
-deserialize({ File, Functions, CheckClauses, Requires, Macros, Aliases, Scheduled }, Vars) ->
+deserialize_with_vars({ File, Functions, CheckClauses, Requires, Macros, Aliases, Scheduled }, Vars) ->
#elixir_scope{
file=File,
functions=Functions,
Please sign in to comment.
Something went wrong with that request. Please try again.