aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjörn-Egil Dahlberg <[email protected]>2014-02-26 17:46:57 +0100
committerBjörn-Egil Dahlberg <[email protected]>2014-03-14 17:07:56 +0100
commitea699f3510b464d6b65b30342f2ddd5912e127a7 (patch)
tree1c42b3bb4d0745684b80e75d59a7c14e3704c908
parent237264bc018b0cc17afeac5d3f6030073f314f9d (diff)
downloadotp-ea699f3510b464d6b65b30342f2ddd5912e127a7.tar.gz
otp-ea699f3510b464d6b65b30342f2ddd5912e127a7.tar.bz2
otp-ea699f3510b464d6b65b30342f2ddd5912e127a7.zip
compiler: Create literal Maps in creation if possible
-rw-r--r--lib/compiler/src/cerl.erl20
-rw-r--r--lib/compiler/src/v3_core.erl7
2 files changed, 22 insertions, 5 deletions
diff --git a/lib/compiler/src/cerl.erl b/lib/compiler/src/cerl.erl
index 3c121f3b04..744ebc7aca 100644
--- a/lib/compiler/src/cerl.erl
+++ b/lib/compiler/src/cerl.erl
@@ -126,7 +126,7 @@
map_es/1,
map_val/1,
update_c_map/3,
- ann_c_map/3,
+ ann_c_map/2, ann_c_map/3,
map_pair_op/1,map_pair_key/1,map_pair_val/1,
update_c_map_pair/4,
ann_c_map_pair/4
@@ -1584,8 +1584,24 @@ map_es(#c_map{es = Es}) ->
map_val(#c_map{var = M}) ->
M.
+ann_c_map(As,Es) ->
+ ann_c_map(As, #c_literal{val=[]}, Es).
+
+ann_c_map(As,#c_literal{val=[]}=M,Es) ->
+ Pairs = [[K,V]||#c_map_pair{key=K,val=V}<-Es],
+ IsLit = lists:foldl(fun(Pair,Res) ->
+ Res andalso is_lit_list(Pair)
+ end, true, Pairs),
+ Fun = fun(Pair) -> [K,V] = lit_list_vals(Pair), {K,V} end,
+ case IsLit of
+ false ->
+ #c_map{var=M, es=Es, anno=As };
+ true ->
+ #c_literal{anno=As, val=maps:from_list(lists:map(Fun, Pairs))}
+ end;
ann_c_map(As,M,Es) ->
- #c_map{var=M,es = Es, anno = As }.
+ #c_map{var=M, es = Es, anno = As }.
+
update_c_map(Old,M,Es) ->
#c_map{var=M, es = Es, anno = get_ann(Old)}.
diff --git a/lib/compiler/src/v3_core.erl b/lib/compiler/src/v3_core.erl
index 3d17557e01..80e9aca3d0 100644
--- a/lib/compiler/src/v3_core.erl
+++ b/lib/compiler/src/v3_core.erl
@@ -77,7 +77,8 @@
splitwith/2,keyfind/3,sort/1,foreach/2,droplast/1,last/1]).
-import(ordsets, [add_element/2,del_element/2,is_element/2,
union/1,union/2,intersection/2,subtract/2]).
--import(cerl, [ann_c_cons/3,ann_c_cons_skel/3,ann_c_tuple/2,c_tuple/1]).
+-import(cerl, [ann_c_cons/3,ann_c_cons_skel/3,ann_c_tuple/2,c_tuple/1,
+ ann_c_map/2, ann_c_map/3]).
-include("core_parse.hrl").
@@ -515,12 +516,12 @@ expr({map,L,Es0}, St0) ->
% in map construction.
{Es1,Eps,St1} = map_pair_list(Es0, St0),
A = lineno_anno(L, St1),
- {#c_map{anno=A,es=Es1},Eps,St1};
+ {ann_c_map(A,Es1),Eps,St1};
expr({map,L,M0,Es0}, St0) ->
{M1,Mps,St1} = safe(M0, St0),
{Es1,Eps,St2} = map_pair_list(Es0, St1),
A = lineno_anno(L, St2),
- {#c_map{anno=A,var=M1,es=Es1},Mps++Eps,St2};
+ {ann_c_map(A,M1,Es1),Mps++Eps,St2};
expr({bin,L,Es0}, St0) ->
try expr_bin(Es0, lineno_anno(L, St0), St0) of
{_,_,_}=Res -> Res