Skip to content

Commit 3e0728c

Browse files
committed
Consistently use leaves
1 parent 9c0f19f commit 3e0728c

File tree

1 file changed

+35
-44
lines changed

1 file changed

+35
-44
lines changed

lib/elixir/lib/module/types/descr.ex

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ defmodule Module.Types.Descr do
2828
@bit_number @bit_integer ||| @bit_float
2929

3030
# Remark: those use AST for BDDs
31-
defmacrop map_literal(tag, fields), do: {:{}, [], [{tag, fields}, :bdd_top, :bdd_bot, :bdd_bot]}
32-
33-
defmacrop tuple_literal(tag, elements),
34-
do: {:{}, [], [{tag, elements}, :bdd_top, :bdd_bot, :bdd_bot]}
35-
36-
defmacrop list_literal(list, last),
37-
do: {:{}, [], [{list, last}, :bdd_top, :bdd_bot, :bdd_bot]}
31+
defmacrop bdd_leaf(arg1, arg2), do: {arg1, arg2}
3832

3933
defmacrop domain_key(key),
4034
do: {:domain_key, key}
@@ -57,10 +51,10 @@ defmodule Module.Types.Descr do
5751
# Remark: those are explicit BDD constructors. The functional constructors are `bdd_new/1` and `bdd_new/3`.
5852
@fun_top :bdd_top
5953
@atom_top {:negation, :sets.new(version: 2)}
60-
@map_top {{:open, %{}}, :bdd_top, :bdd_bot, :bdd_bot}
61-
@non_empty_list_top {{:term, :term}, :bdd_top, :bdd_bot, :bdd_bot}
62-
@tuple_top {{:open, []}, :bdd_top, :bdd_bot, :bdd_bot}
63-
@map_empty {{:closed, %{}}, :bdd_top, :bdd_bot, :bdd_bot}
54+
@map_top {:open, %{}}
55+
@non_empty_list_top {:term, :term}
56+
@tuple_top {:open, []}
57+
@map_empty {:closed, %{}}
6458

6559
@none %{}
6660
@term %{
@@ -1096,7 +1090,7 @@ defmodule Module.Types.Descr do
10961090
# Note: Function domains are expressed as tuple types. We use separate representations
10971091
# rather than unary functions with tuple domains to handle special cases like representing
10981092
# functions of a specific arity (e.g., (none,none->term) for arity 2).
1099-
defp fun_new(inputs, output), do: bdd_new({inputs, output})
1093+
defp fun_new(inputs, output), do: bdd_leaf(inputs, output)
11001094

11011095
# Creates a function type from a list of inputs and an output
11021096
# where the inputs and/or output may be dynamic.
@@ -1853,7 +1847,7 @@ defmodule Module.Types.Descr do
18531847
end
18541848
end
18551849

1856-
defp list_new(list_type, last_type), do: bdd_new({list_type, last_type})
1850+
defp list_new(list_type, last_type), do: bdd_leaf(list_type, last_type)
18571851

18581852
defp non_empty_list_literals_intersection(list_literals) do
18591853
try do
@@ -1918,14 +1912,14 @@ defmodule Module.Types.Descr do
19181912
@compile {:inline, list_union: 2}
19191913
defp list_union(bdd1, bdd2), do: bdd_union(bdd1, bdd2)
19201914

1921-
defp is_list_top?(list_literal(list, tail)), do: list == :term and tail == :term
1915+
defp is_list_top?(bdd_leaf(list, tail)), do: list == :term and tail == :term
19221916
defp is_list_top?(_), do: false
19231917

1924-
defp list_intersection(list_literal(list1, last1), list_literal(list2, last2)) do
1918+
defp list_intersection(bdd_leaf(list1, last1), bdd_leaf(list2, last2)) do
19251919
try do
19261920
list = non_empty_intersection!(list1, list2)
19271921
last = non_empty_intersection!(last1, last2)
1928-
list_literal(list, last)
1922+
bdd_leaf(list, last)
19291923
catch
19301924
:empty -> :bdd_bot
19311925
end
@@ -1948,11 +1942,11 @@ defmodule Module.Types.Descr do
19481942
# b) If only the last type differs, subtracts it
19491943
# 3. Base case: adds bdd2 type to negations of bdd1 type
19501944
# The result may be larger than the initial bdd1, which is maintained in the accumulator.
1951-
defp list_difference(list_literal(list1, last1) = bdd1, list_literal(list2, last2) = bdd2) do
1945+
defp list_difference(bdd_leaf(list1, last1) = bdd1, bdd_leaf(list2, last2) = bdd2) do
19521946
cond do
1953-
disjoint?(list1, list2) or disjoint?(last1, last2) -> list_literal(list1, last1)
1947+
disjoint?(list1, list2) or disjoint?(last1, last2) -> bdd_leaf(list1, last1)
19541948
subtype?(list1, list2) and subtype?(last1, last2) -> :bdd_bot
1955-
equal?(list1, list2) -> list_literal(list1, difference(last1, last2))
1949+
equal?(list1, list2) -> bdd_leaf(list1, difference(last1, last2))
19561950
true -> bdd_difference(bdd1, bdd2)
19571951
end
19581952
end
@@ -2386,7 +2380,7 @@ defmodule Module.Types.Descr do
23862380
defguardp is_optional_static(map)
23872381
when is_map(map) and is_map_key(map, :optional)
23882382

2389-
defp map_new(tag, fields = %{}), do: bdd_new({tag, fields})
2383+
defp map_new(tag, fields = %{}), do: bdd_leaf(tag, fields)
23902384

23912385
defp map_only?(descr), do: empty?(Map.delete(descr, :map))
23922386

@@ -2397,10 +2391,10 @@ defmodule Module.Types.Descr do
23972391
end
23982392
end
23992393

2400-
defp map_union(map_literal(tag1, fields1), map_literal(tag2, fields2)) do
2394+
defp map_union(bdd_leaf(tag1, fields1), bdd_leaf(tag2, fields2)) do
24012395
case maybe_optimize_map_union({tag1, fields1, []}, {tag2, fields2, []}) do
2402-
{tag, fields, []} -> map_literal(tag, fields)
2403-
nil -> bdd_union(map_literal(tag1, fields1), map_literal(tag2, fields2))
2396+
{tag, fields, []} -> bdd_leaf(tag, fields)
2397+
nil -> bdd_union(bdd_leaf(tag1, fields1), bdd_leaf(tag2, fields2))
24042398
end
24052399
end
24062400

@@ -2500,13 +2494,13 @@ defmodule Module.Types.Descr do
25002494
if subtype?(v2, v1), do: :right_subtype_of_left
25012495
end
25022496

2503-
defp is_map_top?(map_literal(:open, fields)) when map_size(fields) == 0, do: true
2497+
defp is_map_top?(bdd_leaf(:open, fields)) when map_size(fields) == 0, do: true
25042498
defp is_map_top?(_), do: false
25052499

2506-
defp map_intersection(map_literal(tag1, fields1), map_literal(tag2, fields2)) do
2500+
defp map_intersection(bdd_leaf(tag1, fields1), bdd_leaf(tag2, fields2)) do
25072501
try do
2508-
map_literal = map_literal_intersection(tag1, fields1, tag2, fields2)
2509-
bdd_new(map_literal)
2502+
{tag, fields} = map_literal_intersection(tag1, fields1, tag2, fields2)
2503+
bdd_leaf(tag, fields)
25102504
catch
25112505
:empty -> :bdd_bot
25122506
end
@@ -2522,7 +2516,7 @@ defmodule Module.Types.Descr do
25222516
end
25232517

25242518
# Optimizations on single maps.
2525-
defp map_difference(map_literal(tag, fields) = map1, map_literal(neg_tag, neg_fields) = map2) do
2519+
defp map_difference(bdd_leaf(tag, fields) = map1, bdd_leaf(neg_tag, neg_fields) = map2) do
25262520
# Case 1: we are removing an open map with one field. Just do the difference of that field.
25272521
if neg_tag == :open and map_size(neg_fields) == 1 do
25282522
[{key, value}] = Map.to_list(neg_fields)
@@ -2531,13 +2525,13 @@ defmodule Module.Types.Descr do
25312525
if empty?(t_diff) do
25322526
:bdd_bot
25332527
else
2534-
map_literal(tag, Map.put(fields, key, t_diff))
2528+
bdd_leaf(tag, Map.put(fields, key, t_diff))
25352529
end
25362530
else
25372531
# Case 2: the maps have all but one key in common. Do the difference of that key.
25382532
case map_all_but_one(tag, fields, neg_tag, neg_fields) do
25392533
{:one, diff_key} ->
2540-
map_literal(tag, Map.update!(fields, diff_key, &difference(&1, neg_fields[diff_key])))
2534+
bdd_leaf(tag, Map.update!(fields, diff_key, &difference(&1, neg_fields[diff_key])))
25412535

25422536
_ ->
25432537
bdd_difference(map1, map2)
@@ -2718,7 +2712,7 @@ defmodule Module.Types.Descr do
27182712

27192713
# Optimization: if the key does not exist in the map, avoid building
27202714
# if_set/not_set pairs and return the popped value directly.
2721-
defp map_fetch_static(%{map: map_literal(tag_or_domains, fields)}, key)
2715+
defp map_fetch_static(%{map: bdd_leaf(tag_or_domains, fields)}, key)
27222716
when not is_map_key(fields, key) do
27232717
map_key_tag_to_type(tag_or_domains) |> pop_optional_static()
27242718
end
@@ -2892,8 +2886,8 @@ defmodule Module.Types.Descr do
28922886
end
28932887
end
28942888

2895-
def map_refresh_domain(%{map: map_literal(tag, fields)}, domain, type) do
2896-
%{map: bdd_new({map_refresh_tag(tag, domain, type), fields})}
2889+
def map_refresh_domain(%{map: bdd_leaf(tag, fields)}, domain, type) do
2890+
%{map: bdd_leaf(map_refresh_tag(tag, domain, type), fields)}
28972891
end
28982892

28992893
def map_refresh_domain(%{map: bdd}, domain, type) do
@@ -3076,7 +3070,7 @@ defmodule Module.Types.Descr do
30763070

30773071
defp unfold_domains(domains = %{}), do: domains
30783072

3079-
defp map_get_static(%{map: map_literal(tag_or_domains, fields)}, key_descr) do
3073+
defp map_get_static(%{map: bdd_leaf(tag_or_domains, fields)}, key_descr) do
30803074
# For each non-empty kind of type in the key_descr, we add the corresponding key domain in a union.
30813075
domains = unfold_domains(tag_or_domains)
30823076

@@ -3261,7 +3255,7 @@ defmodule Module.Types.Descr do
32613255

32623256
# Takes a static map type and removes a key from it.
32633257
# This allows the key to be put or deleted later on.
3264-
defp map_take_static(%{map: map_literal(tag, fields)} = descr, key, initial)
3258+
defp map_take_static(%{map: bdd_leaf(tag, fields)} = descr, key, initial)
32653259
when not is_map_key(fields, key) do
32663260
case tag do
32673261
:open -> {true, maybe_union(initial, fn -> term() end), descr}
@@ -3733,11 +3727,11 @@ defmodule Module.Types.Descr do
37333727
{acc, dynamic?}
37343728
end
37353729

3736-
defp tuple_new(tag, elements), do: tuple_literal(tag, elements)
3730+
defp tuple_new(tag, elements), do: bdd_leaf(tag, elements)
37373731

3738-
defp tuple_intersection(tuple_literal(tag1, elements1), tuple_literal(tag2, elements2)) do
3732+
defp tuple_intersection(bdd_leaf(tag1, elements1), bdd_leaf(tag2, elements2)) do
37393733
case tuple_literal_intersection(tag1, elements1, tag2, elements2) do
3740-
{tag, elements} -> tuple_literal(tag, elements)
3734+
{tag, elements} -> bdd_leaf(tag, elements)
37413735
:empty -> :bdd_bot
37423736
end
37433737
end
@@ -3973,11 +3967,11 @@ defmodule Module.Types.Descr do
39733967
end
39743968

39753969
defp tuple_union(
3976-
tuple_literal(tag1, elements1) = tuple1,
3977-
tuple_literal(tag2, elements2) = tuple2
3970+
bdd_leaf(tag1, elements1) = tuple1,
3971+
bdd_leaf(tag2, elements2) = tuple2
39783972
) do
39793973
case maybe_optimize_tuple_union({tag1, elements1}, {tag2, elements2}) do
3980-
{tag, elements} -> tuple_literal(tag, elements)
3974+
{tag, elements} -> bdd_leaf(tag, elements)
39813975
nil -> bdd_union(tuple1, tuple2)
39823976
end
39833977
end
@@ -4418,9 +4412,6 @@ defmodule Module.Types.Descr do
44184412

44194413
## BDD helpers
44204414

4421-
@compile {:inline, bdd_new: 1}
4422-
defp bdd_new(literal), do: {literal, :bdd_top, :bdd_bot, :bdd_bot}
4423-
44244415
defp bdd_map(bdd, fun) do
44254416
case bdd do
44264417
:bdd_bot ->

0 commit comments

Comments
 (0)