diff options
author | John Högberg <[email protected]> | 2019-06-14 13:30:08 +0200 |
---|---|---|
committer | John Högberg <[email protected]> | 2019-06-14 13:31:13 +0200 |
commit | d7b6e2ef74c28bc60058b110bdefe5c54d1da058 (patch) | |
tree | 165dea58be40ba405a80e554c06c065eb9fab43f /lib/compiler/src/beam_types.erl | |
parent | 5bfa7146c04f3c7b7492cb7660db425d7d34c5a6 (diff) | |
download | otp-d7b6e2ef74c28bc60058b110bdefe5c54d1da058.tar.gz otp-d7b6e2ef74c28bc60058b110bdefe5c54d1da058.tar.bz2 otp-d7b6e2ef74c28bc60058b110bdefe5c54d1da058.zip |
compiler: Add common method for literal -> type conversion
Diffstat (limited to 'lib/compiler/src/beam_types.erl')
-rw-r--r-- | lib/compiler/src/beam_types.erl | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/compiler/src/beam_types.erl b/lib/compiler/src/beam_types.erl index c3ad594304..07d3c3d3f2 100644 --- a/lib/compiler/src/beam_types.erl +++ b/lib/compiler/src/beam_types.erl @@ -22,6 +22,8 @@ -include("beam_types.hrl"). +-import(lists, [foldl/3]). + -export([meet/1, meet/2, join/1, join/2, subtract/2]). -export([get_singleton_value/1, @@ -31,6 +33,8 @@ -export([get_element_type/2, set_element_type/3]). +-export([make_type_from_value/1]). + -export([make_atom/1, make_boolean/0, make_integer/1, @@ -348,6 +352,31 @@ get_element_type(Index, Es) -> %%% Type constructors %%% +-spec make_type_from_value(term()) -> type(). +make_type_from_value(Value) -> + mtfv_1(Value). + +mtfv_1([]) -> nil; +mtfv_1([_|_]) -> cons; +mtfv_1(A) when is_atom(A) -> #t_atom{elements=[A]}; +mtfv_1(B) when is_binary(B) -> #t_bitstring{unit=8}; +mtfv_1(B) when is_bitstring(B) -> #t_bitstring{}; +mtfv_1(F) when is_float(F) -> float; +mtfv_1(F) when is_function(F) -> + {arity, Arity} = erlang:fun_info(F, arity), + #t_fun{arity=Arity}; +mtfv_1(I) when is_integer(I) -> make_integer(I); +mtfv_1(M) when is_map(M) -> #t_map{}; +mtfv_1(T) when is_tuple(T) -> + {Es,_} = foldl(fun(Val, {Es0, Index}) -> + Type = mtfv_1(Val), + Es = set_element_type(Index, Type, Es0), + {Es, Index + 1} + end, {#{}, 1}, tuple_to_list(T)), + #t_tuple{exact=true,size=tuple_size(T),elements=Es}; +mtfv_1(_Term) -> + any. + -spec make_atom(atom()) -> type(). make_atom(Atom) when is_atom(Atom) -> #t_atom{elements=[Atom]}. |