1%% Licensed under the Apache License, Version 2.0 (the "License");
2%% you may not use this file except in compliance with the License.
3%% You may obtain a copy of the License at
4%%
5%%     http://www.apache.org/licenses/LICENSE-2.0
6%%
7%% Unless required by applicable law or agreed to in writing, software
8%% distributed under the License is distributed on an "AS IS" BASIS,
9%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10%% See the License for the specific language governing permissions and
11%% limitations under the License.
12%%
13%% @copyright 1999-2002 Richard Carlsson.
14%% @author Richard Carlsson <carlsson.richard@gmail.com>
15%% @doc Basic functions on Core Erlang abstract syntax trees.
16%%
17%% <p>Syntax trees are defined in the module <a
18%% href="cerl"><code>cerl</code></a>.</p>
19%%
20%% @type cerl() = cerl:cerl()
21
22-module(cerl_trees).
23
24-export([depth/1, fold/3, free_variables/1, get_label/1, label/1, label/2,
25	 map/2, mapfold/3, mapfold/4, next_free_variable_name/1,
26         size/1, variables/1]).
27
28-import(cerl, [alias_pat/1, alias_var/1, ann_c_alias/3, ann_c_apply/3,
29	       ann_c_binary/2, ann_c_bitstr/6, ann_c_call/4,
30	       ann_c_case/3, ann_c_catch/2, ann_c_clause/4,
31	       ann_c_cons_skel/3, ann_c_fun/3, ann_c_let/4,
32	       ann_c_letrec/3, ann_c_module/5, ann_c_primop/3,
33	       ann_c_receive/4, ann_c_seq/3, ann_c_try/6,
34	       ann_c_tuple_skel/2, ann_c_values/2, apply_args/1,
35	       apply_op/1, binary_segments/1, bitstr_val/1,
36	       bitstr_size/1, bitstr_unit/1, bitstr_type/1,
37	       bitstr_flags/1, call_args/1, call_module/1, call_name/1,
38	       case_arg/1, case_clauses/1, catch_body/1, clause_body/1,
39	       clause_guard/1, clause_pats/1, clause_vars/1, concrete/1,
40	       cons_hd/1, cons_tl/1, fun_body/1, fun_vars/1, get_ann/1,
41	       let_arg/1, let_body/1, let_vars/1, letrec_body/1,
42	       letrec_defs/1, letrec_vars/1, module_attrs/1,
43	       module_defs/1, module_exports/1, module_name/1,
44	       module_vars/1, primop_args/1, primop_name/1,
45	       receive_action/1, receive_clauses/1, receive_timeout/1,
46	       seq_arg/1, seq_body/1, set_ann/2, subtrees/1, try_arg/1,
47	       try_body/1, try_vars/1, try_evars/1, try_handler/1,
48	       tuple_es/1, type/1, update_c_alias/3, update_c_apply/3,
49	       update_c_binary/2, update_c_bitstr/6, update_c_call/4,
50	       update_c_case/3, update_c_catch/2, update_c_clause/4,
51	       update_c_cons/3, update_c_cons_skel/3, update_c_fun/3,
52	       update_c_let/4, update_c_letrec/3, update_c_module/5,
53	       update_c_primop/3, update_c_receive/4, update_c_seq/3,
54	       update_c_try/6, update_c_tuple/2, update_c_tuple_skel/2,
55	       update_c_values/2, values_es/1, var_name/1,
56
57	       map_arg/1, map_es/1,
58	       ann_c_map/3,
59	       update_c_map/3,
60	       is_c_map_pattern/1, ann_c_map_pattern/2,
61	       map_pair_key/1,map_pair_val/1,map_pair_op/1,
62	       ann_c_map_pair/4,
63	       update_c_map_pair/4
64	   ]).
65
66
67%% ---------------------------------------------------------------------
68
69%% @spec depth(Tree::cerl()) -> integer()
70%%
71%% @doc Returns the length of the longest path in the tree.  A leaf
72%% node has depth zero, the tree representing "<code>{foo,
73%% bar}</code>" has depth one, etc.
74
75-spec depth(cerl:cerl()) -> non_neg_integer().
76
77depth(T) ->
78    case subtrees(T) of
79	[] ->
80	    0;
81	Gs ->
82	    1 + lists:foldl(fun (G, A) -> erlang:max(depth_1(G), A) end, 0, Gs)
83    end.
84
85depth_1(Ts) ->
86    lists:foldl(fun (T, A) -> erlang:max(depth(T), A) end, 0, Ts).
87
88
89
90%% @spec size(Tree::cerl()) -> integer()
91%%
92%% @doc Returns the number of nodes in <code>Tree</code>.
93
94-spec size(cerl:cerl()) -> non_neg_integer().
95
96size(T) ->
97    fold(fun (_, S) -> S + 1 end, 0, T).
98
99
100%% ---------------------------------------------------------------------
101
102%% @spec map(Function, Tree::cerl()) -> cerl()
103%%
104%%	   Function = (cerl()) -> cerl()
105%%
106%% @doc Maps a function onto the nodes of a tree. This replaces each
107%% node in the tree by the result of applying the given function on
108%% the original node, bottom-up.
109%%
110%% @see mapfold/3
111
112-spec map(fun((cerl:cerl()) -> cerl:cerl()), cerl:cerl()) -> cerl:cerl().
113
114map(F, T) ->
115    F(map_1(F, T)).
116
117map_1(F, T) ->
118    case type(T) of
119 	literal ->
120	    case concrete(T) of
121		[_ | _] ->
122		    update_c_cons(T, map(F, cons_hd(T)),
123				  map(F, cons_tl(T)));
124		V when tuple_size(V) > 0 ->
125		    update_c_tuple(T, map_list(F, tuple_es(T)));
126		_ ->
127		    T
128	    end;
129 	var ->
130 	    T;
131	values ->
132 	    update_c_values(T, map_list(F, values_es(T)));
133	cons ->
134	    update_c_cons_skel(T, map(F, cons_hd(T)),
135			       map(F, cons_tl(T)));
136 	tuple ->
137	    update_c_tuple_skel(T, map_list(F, tuple_es(T)));
138 	map ->
139	    update_c_map(T, map(F, map_arg(T)), map_list(F, map_es(T)));
140	map_pair ->
141	    update_c_map_pair(T, map(F, map_pair_op(T)),
142                                 map(F, map_pair_key(T)),
143                                 map(F, map_pair_val(T)));
144 	'let' ->
145	    update_c_let(T, map_list(F, let_vars(T)),
146			 map(F, let_arg(T)),
147			 map(F, let_body(T)));
148	seq ->
149 	    update_c_seq(T, map(F, seq_arg(T)),
150			 map(F, seq_body(T)));
151 	apply ->
152	    update_c_apply(T, map(F, apply_op(T)),
153			   map_list(F, apply_args(T)));
154 	call ->
155 	    update_c_call(T, map(F, call_module(T)),
156			  map(F, call_name(T)),
157			  map_list(F, call_args(T)));
158 	primop ->
159	    update_c_primop(T, map(F, primop_name(T)),
160			    map_list(F, primop_args(T)));
161 	'case' ->
162 	    update_c_case(T, map(F, case_arg(T)),
163			  map_list(F, case_clauses(T)));
164 	clause ->
165 	    update_c_clause(T, map_list(F, clause_pats(T)),
166			    map(F, clause_guard(T)),
167			    map(F, clause_body(T)));
168 	alias ->
169	    update_c_alias(T, map(F, alias_var(T)),
170			   map(F, alias_pat(T)));
171 	'fun' ->
172	    update_c_fun(T, map_list(F, fun_vars(T)),
173			 map(F, fun_body(T)));
174 	'receive' ->
175	    update_c_receive(T, map_list(F, receive_clauses(T)),
176			     map(F, receive_timeout(T)),
177			     map(F, receive_action(T)));
178 	'try' ->
179 	    update_c_try(T, map(F, try_arg(T)),
180			 map_list(F, try_vars(T)),
181			 map(F, try_body(T)),
182			 map_list(F, try_evars(T)),
183			 map(F, try_handler(T)));
184 	'catch' ->
185	    update_c_catch(T, map(F, catch_body(T)));
186	binary ->
187	    update_c_binary(T, map_list(F, binary_segments(T)));
188	bitstr ->
189	    update_c_bitstr(T, map(F, bitstr_val(T)),
190			    map(F, bitstr_size(T)),
191			    map(F, bitstr_unit(T)),
192			    map(F, bitstr_type(T)),
193			    map(F, bitstr_flags(T)));
194	letrec ->
195	    update_c_letrec(T, map_pairs(F, letrec_defs(T)),
196			    map(F, letrec_body(T)));
197	module ->
198	    update_c_module(T, map(F, module_name(T)),
199			    map_list(F, module_exports(T)),
200			    map_pairs(F, module_attrs(T)),
201			    map_pairs(F, module_defs(T)))
202    end.
203
204map_list(F, [T | Ts]) ->
205    [map(F, T) | map_list(F, Ts)];
206map_list(_, []) ->
207    [].
208
209map_pairs(F, [{T1, T2} | Ps]) ->
210    [{map(F, T1), map(F, T2)} | map_pairs(F, Ps)];
211map_pairs(_, []) ->
212    [].
213
214
215%% @spec fold(Function, Unit::term(), Tree::cerl()) -> term()
216%%
217%%    Function = (cerl(), term()) -> term()
218%%
219%% @doc Does a fold operation over the nodes of the tree. The result
220%% is the value of <code>Function(X1, Function(X2, ... Function(Xn,
221%% Unit) ... ))</code>, where <code>X1, ..., Xn</code> are the nodes
222%% of <code>Tree</code> in a post-order traversal.
223%%
224%% @see mapfold/3
225
226-spec fold(fun((cerl:cerl(), term()) -> term()), term(), cerl:cerl()) -> term().
227
228fold(F, S, T) ->
229    F(T, fold_1(F, S, T)).
230
231fold_1(F, S, T) ->
232    case type(T) of
233 	literal ->
234	    case concrete(T) of
235		[_ | _] ->
236		    fold(F, fold(F, S, cons_hd(T)), cons_tl(T));
237		V when tuple_size(V) > 0 ->
238		    fold_list(F, S, tuple_es(T));
239		_ ->
240		    S
241	    end;
242 	var ->
243 	    S;
244	values ->
245 	    fold_list(F, S, values_es(T));
246	cons ->
247	    fold(F, fold(F, S, cons_hd(T)), cons_tl(T));
248	tuple ->
249	    fold_list(F, S, tuple_es(T));
250	map ->
251	    fold_list(F, S, map_es(T));
252	map_pair ->
253	    fold(F,
254		fold(F,
255		    fold(F, S, map_pair_op(T)),
256		    map_pair_key(T)),
257		map_pair_val(T));
258 	'let' ->
259	    fold(F, fold(F, fold_list(F, S, let_vars(T)),
260			 let_arg(T)),
261		 let_body(T));
262	seq ->
263	    fold(F, fold(F, S, seq_arg(T)), seq_body(T));
264	apply ->
265	    fold_list(F, fold(F, S, apply_op(T)), apply_args(T));
266 	call ->
267	    fold_list(F, fold(F, fold(F, S, call_module(T)),
268			      call_name(T)),
269		      call_args(T));
270 	primop ->
271	    fold_list(F, fold(F, S, primop_name(T)), primop_args(T));
272 	'case' ->
273	    fold_list(F, fold(F, S, case_arg(T)), case_clauses(T));
274 	clause ->
275	    fold(F, fold(F, fold_list(F, S, clause_pats(T)),
276			 clause_guard(T)),
277		 clause_body(T));
278 	alias ->
279	    fold(F, fold(F, S, alias_var(T)), alias_pat(T));
280 	'fun' ->
281	    fold(F, fold_list(F, S, fun_vars(T)), fun_body(T));
282 	'receive' ->
283	    fold(F, fold(F, fold_list(F, S, receive_clauses(T)),
284			 receive_timeout(T)),
285		 receive_action(T));
286 	'try' ->
287	    fold(F, fold_list(F, fold(F, fold_list(F, fold(F, S, try_arg(T)),
288						   try_vars(T)),
289				      try_body(T)),
290			      try_evars(T)),
291		 try_handler(T));
292 	'catch' ->
293	    fold(F, S, catch_body(T));
294	binary ->
295	    fold_list(F, S, binary_segments(T));
296	bitstr ->
297	    fold(F,
298		 fold(F,
299		      fold(F,
300			   fold(F,
301				fold(F, S, bitstr_val(T)),
302				bitstr_size(T)),
303			   bitstr_unit(T)),
304		      bitstr_type(T)),
305		 bitstr_flags(T));
306	letrec ->
307	    fold(F, fold_pairs(F, S, letrec_defs(T)), letrec_body(T));
308	module ->
309	    fold_pairs(F,
310		       fold_pairs(F,
311				  fold_list(F,
312					    fold(F, S, module_name(T)),
313					    module_exports(T)),
314				  module_attrs(T)),
315		       module_defs(T))
316    end.
317
318fold_list(F, S, [T | Ts]) ->
319    fold_list(F, fold(F, S, T), Ts);
320fold_list(_, S, []) ->
321    S.
322
323fold_pairs(F, S, [{T1, T2} | Ps]) ->
324    fold_pairs(F, fold(F, fold(F, S, T1), T2), Ps);
325fold_pairs(_, S, []) ->
326    S.
327
328
329%% @spec mapfold(Function, Initial::term(), Tree::cerl()) ->
330%%           {cerl(), term()}
331%%
332%%    Function = (cerl(), term()) -> {cerl(), term()}
333%%
334%% @doc Does a combined map/fold operation on the nodes of the
335%% tree. This is similar to <code>map/2</code>, but also propagates a
336%% value from each application of <code>Function</code> to the next,
337%% starting with the given value <code>Initial</code>, while doing a
338%% post-order traversal of the tree, much like <code>fold/3</code>.
339%%
340%% This is the same as mapfold/4, with an identity function as the
341%% pre-operation.
342%%
343%% @see map/2
344%% @see fold/3
345%% @see mapfold/4
346
347-spec mapfold(fun((cerl:cerl(), term()) -> {cerl:cerl(), term()}),
348	      term(), cerl:cerl()) -> {cerl:cerl(), term()}.
349
350mapfold(F, S0, T) ->
351  mapfold(fun(T0, A) -> {T0, A} end, F, S0, T).
352
353
354%% @spec mapfold(Pre, Post, Initial::term(), Tree::cerl()) -> {cerl(), term()}
355%%       Pre  = (cerl(), term()) -> {cerl(), term()}
356%%       Post = (cerl(), term()) -> {cerl(), term()}
357%%
358%% @doc Does a combined map/fold operation on the nodes of the
359%% tree. It begins by calling <code>Pre</code> on the tree, using the
360%% <code>Initial</code> value. It then deconstructs the top node of
361%% the returned tree and recurses on the children, using the returned
362%% value as the new initial and carrying the returned values from one
363%% call to the next. Finally it reassembles the top node from the
364%% children, calls <code>Post</code> on it and returns the result.
365
366-spec mapfold(fun((cerl:cerl(), term()) -> {cerl:cerl(), term()}),
367              fun((cerl:cerl(), term()) -> {cerl:cerl(), term()}),
368	      term(), cerl:cerl()) -> {cerl:cerl(), term()}.
369
370mapfold(Pre, Post, S00, T0) ->
371    {T, S0} = Pre(T0, S00),
372    case type(T) of
373 	literal ->
374	    case concrete(T) of
375		[_ | _] ->
376		    {T1, S1} = mapfold(Pre, Post, S0, cons_hd(T)),
377		    {T2, S2} = mapfold(Pre, Post, S1, cons_tl(T)),
378		    Post(update_c_cons(T, T1, T2), S2);
379		V when tuple_size(V) > 0 ->
380		    {Ts, S1} = mapfold_list(Pre, Post, S0, tuple_es(T)),
381		    Post(update_c_tuple(T, Ts), S1);
382		_ ->
383		    Post(T, S0)
384	    end;
385 	var ->
386	    Post(T, S0);
387	values ->
388	    {Ts, S1} = mapfold_list(Pre, Post, S0, values_es(T)),
389	    Post(update_c_values(T, Ts), S1);
390	cons ->
391	    {T1, S1} = mapfold(Pre, Post, S0, cons_hd(T)),
392	    {T2, S2} = mapfold(Pre, Post, S1, cons_tl(T)),
393	    Post(update_c_cons_skel(T, T1, T2), S2);
394 	tuple ->
395	    {Ts, S1} = mapfold_list(Pre, Post, S0, tuple_es(T)),
396	    Post(update_c_tuple_skel(T, Ts), S1);
397	map ->
398	    {M , S1} = mapfold(Pre, Post, S0, map_arg(T)),
399	    {Ts, S2} = mapfold_list(Pre, Post, S1, map_es(T)),
400	    Post(update_c_map(T, M, Ts), S2);
401	map_pair ->
402	    {Op,  S1} = mapfold(Pre, Post, S0, map_pair_op(T)),
403	    {Key, S2} = mapfold(Pre, Post, S1, map_pair_key(T)),
404	    {Val, S3} = mapfold(Pre, Post, S2, map_pair_val(T)),
405	    Post(update_c_map_pair(T,Op,Key,Val), S3);
406 	'let' ->
407	    {Vs, S1} = mapfold_list(Pre, Post, S0, let_vars(T)),
408	    {A, S2} = mapfold(Pre, Post, S1, let_arg(T)),
409	    {B, S3} = mapfold(Pre, Post, S2, let_body(T)),
410	    Post(update_c_let(T, Vs, A, B), S3);
411	seq ->
412	    {A, S1} = mapfold(Pre, Post, S0, seq_arg(T)),
413	    {B, S2} = mapfold(Pre, Post, S1, seq_body(T)),
414	    Post(update_c_seq(T, A, B), S2);
415 	apply ->
416	    {E, S1} = mapfold(Pre, Post, S0, apply_op(T)),
417	    {As, S2} = mapfold_list(Pre, Post, S1, apply_args(T)),
418	    Post(update_c_apply(T, E, As), S2);
419 	call ->
420	    {M, S1} = mapfold(Pre, Post, S0, call_module(T)),
421	    {N, S2} = mapfold(Pre, Post, S1, call_name(T)),
422	    {As, S3} = mapfold_list(Pre, Post, S2, call_args(T)),
423	    Post(update_c_call(T, M, N, As), S3);
424 	primop ->
425	    {N, S1} = mapfold(Pre, Post, S0, primop_name(T)),
426	    {As, S2} = mapfold_list(Pre, Post, S1, primop_args(T)),
427	    Post(update_c_primop(T, N, As), S2);
428 	'case' ->
429	    {A, S1} = mapfold(Pre, Post, S0, case_arg(T)),
430	    {Cs, S2} = mapfold_list(Pre, Post, S1, case_clauses(T)),
431	    Post(update_c_case(T, A, Cs), S2);
432 	clause ->
433	    {Ps, S1} = mapfold_list(Pre, Post, S0, clause_pats(T)),
434	    {G, S2} = mapfold(Pre, Post, S1, clause_guard(T)),
435	    {B, S3} = mapfold(Pre, Post, S2, clause_body(T)),
436	    Post(update_c_clause(T, Ps, G, B), S3);
437 	alias ->
438	    {V, S1} = mapfold(Pre, Post, S0, alias_var(T)),
439	    {P, S2} = mapfold(Pre, Post, S1, alias_pat(T)),
440	    Post(update_c_alias(T, V, P), S2);
441 	'fun' ->
442	    {Vs, S1} = mapfold_list(Pre, Post, S0, fun_vars(T)),
443	    {B, S2} = mapfold(Pre, Post, S1, fun_body(T)),
444	    Post(update_c_fun(T, Vs, B), S2);
445 	'receive' ->
446	    {Cs, S1} = mapfold_list(Pre, Post, S0, receive_clauses(T)),
447	    {E, S2} = mapfold(Pre, Post, S1, receive_timeout(T)),
448	    {A, S3} = mapfold(Pre, Post, S2, receive_action(T)),
449	    Post(update_c_receive(T, Cs, E, A), S3);
450 	'try' ->
451	    {E, S1} = mapfold(Pre, Post, S0, try_arg(T)),
452	    {Vs, S2} = mapfold_list(Pre, Post, S1, try_vars(T)),
453	    {B, S3} = mapfold(Pre, Post, S2, try_body(T)),
454	    {Evs, S4} = mapfold_list(Pre, Post, S3, try_evars(T)),
455	    {H, S5} = mapfold(Pre, Post, S4, try_handler(T)),
456	    Post(update_c_try(T, E, Vs, B, Evs, H), S5);
457 	'catch' ->
458	    {B, S1} = mapfold(Pre, Post, S0, catch_body(T)),
459	    Post(update_c_catch(T, B), S1);
460	binary ->
461	    {Ds, S1} = mapfold_list(Pre, Post, S0, binary_segments(T)),
462	    Post(update_c_binary(T, Ds), S1);
463	bitstr ->
464	    {Val, S1} = mapfold(Pre, Post, S0, bitstr_val(T)),
465	    {Size, S2} = mapfold(Pre, Post, S1, bitstr_size(T)),
466	    {Unit, S3} = mapfold(Pre, Post, S2, bitstr_unit(T)),
467	    {Type, S4} = mapfold(Pre, Post, S3, bitstr_type(T)),
468	    {Flags, S5} = mapfold(Pre, Post, S4, bitstr_flags(T)),
469	    Post(update_c_bitstr(T, Val, Size, Unit, Type, Flags), S5);
470	letrec ->
471	    {Ds, S1} = mapfold_pairs(Pre, Post, S0, letrec_defs(T)),
472	    {B, S2} = mapfold(Pre, Post, S1, letrec_body(T)),
473	    Post(update_c_letrec(T, Ds, B), S2);
474	module ->
475	    {N, S1} = mapfold(Pre, Post, S0, module_name(T)),
476	    {Es, S2} = mapfold_list(Pre, Post, S1, module_exports(T)),
477	    {As, S3} = mapfold_pairs(Pre, Post, S2, module_attrs(T)),
478	    {Ds, S4} = mapfold_pairs(Pre, Post, S3, module_defs(T)),
479	    Post(update_c_module(T, N, Es, As, Ds), S4)
480    end.
481
482mapfold_list(Pre, Post, S0, [T | Ts]) ->
483    {T1, S1} = mapfold(Pre, Post, S0, T),
484    {Ts1, S2} = mapfold_list(Pre, Post, S1, Ts),
485    {[T1 | Ts1], S2};
486mapfold_list(_, _, S, []) ->
487    {[], S}.
488
489mapfold_pairs(Pre, Post, S0, [{T1, T2} | Ps]) ->
490    {T3, S1} = mapfold(Pre, Post, S0, T1),
491    {T4, S2} = mapfold(Pre, Post, S1, T2),
492    {Ps1, S3} = mapfold_pairs(Pre, Post, S2, Ps),
493    {[{T3, T4} | Ps1], S3};
494mapfold_pairs(_, _, S, []) ->
495    {[], S}.
496
497
498%% ---------------------------------------------------------------------
499
500%% @spec variables(Tree::cerl()) -> [var_name()]
501%%
502%%	    var_name() = integer() | atom() | {atom(), integer()}
503%%
504%% @doc Returns an ordered-set list of the names of all variables in
505%% the syntax tree. (This includes function name variables.) An
506%% exception is thrown if <code>Tree</code> does not represent a
507%% well-formed Core Erlang syntax tree.
508%%
509%% @see free_variables/1
510%% @see next_free_variable_name/1
511
512-spec variables(cerl:cerl()) -> [cerl:var_name()].
513
514variables(T) ->
515    variables(T, false).
516
517
518%% @spec free_variables(Tree::cerl()) -> [var_name()]
519%%
520%% @doc Like <code>variables/1</code>, but only includes variables
521%% that are free in the tree.
522%%
523%% @see next_free_variable_name/1
524%% @see variables/1
525
526-spec free_variables(cerl:cerl()) -> [cerl:var_name()].
527
528free_variables(T) ->
529    variables(T, true).
530
531
532%% This is not exported
533
534variables(T, S) ->
535    case type(T) of
536	literal ->
537	    [];
538	var ->
539	    [var_name(T)];
540	values ->
541	    vars_in_list(values_es(T), S);
542	cons ->
543	    ordsets:union(variables(cons_hd(T), S),
544			  variables(cons_tl(T), S));
545	tuple ->
546	    vars_in_list(tuple_es(T), S);
547	map ->
548	    vars_in_list([map_arg(T)|map_es(T)], S);
549	map_pair ->
550	    vars_in_list([map_pair_op(T),map_pair_key(T),map_pair_val(T)], S);
551	'let' ->
552	    Vs = variables(let_body(T), S),
553	    Vs1 = var_list_names(let_vars(T)),
554	    Vs2 = case S of
555		      true ->
556			  ordsets:subtract(Vs, Vs1);
557		      false ->
558			  ordsets:union(Vs, Vs1)
559		  end,
560	    ordsets:union(variables(let_arg(T), S), Vs2);
561	seq ->
562	    ordsets:union(variables(seq_arg(T), S),
563			  variables(seq_body(T), S));
564	apply ->
565	    ordsets:union(
566	      variables(apply_op(T), S),
567	      vars_in_list(apply_args(T), S));
568	call ->
569	    ordsets:union(variables(call_module(T), S),
570			  ordsets:union(
571			    variables(call_name(T), S),
572			    vars_in_list(call_args(T), S)));
573	primop ->
574	    vars_in_list(primop_args(T), S);
575	'case' ->
576	    ordsets:union(variables(case_arg(T), S),
577			  vars_in_list(case_clauses(T), S));
578	clause ->
579	    Vs = ordsets:union(variables(clause_guard(T), S),
580			       variables(clause_body(T), S)),
581	    Vs1 = vars_in_list(clause_pats(T), S),
582	    case S of
583		true ->
584		    ordsets:subtract(Vs, Vs1);
585		false ->
586		    ordsets:union(Vs, Vs1)
587	    end;
588	alias ->
589	    ordsets:add_element(var_name(alias_var(T)),
590				variables(alias_pat(T)));
591	'fun' ->
592	    Vs = variables(fun_body(T), S),
593	    Vs1 = var_list_names(fun_vars(T)),
594	    case S of
595		true ->
596		    ordsets:subtract(Vs, Vs1);
597		false ->
598		    ordsets:union(Vs, Vs1)
599	    end;
600	'receive' ->
601	    ordsets:union(
602	      vars_in_list(receive_clauses(T), S),
603	      ordsets:union(variables(receive_timeout(T), S),
604			    variables(receive_action(T), S)));
605	'try' ->
606	    Vs = variables(try_body(T), S),
607	    Vs1 = var_list_names(try_vars(T)),
608	    Vs2 = case S of
609		      true ->
610			  ordsets:subtract(Vs, Vs1);
611		      false ->
612			  ordsets:union(Vs, Vs1)
613		  end,
614	    Vs3 = variables(try_handler(T), S),
615	    Vs4 = var_list_names(try_evars(T)),
616	    Vs5 = case S of
617		      true ->
618			  ordsets:subtract(Vs3, Vs4);
619		      false ->
620			  ordsets:union(Vs3, Vs4)
621		  end,
622	    ordsets:union(variables(try_arg(T), S),
623			  ordsets:union(Vs2, Vs5));
624	'catch' ->
625	    variables(catch_body(T), S);
626	binary ->
627	    vars_in_list(binary_segments(T), S);
628	bitstr ->
629	    ordsets:union(variables(bitstr_val(T), S),
630			  variables(bitstr_size(T), S));
631	letrec ->
632	    Vs = vars_in_defs(letrec_defs(T), S),
633	    Vs1 = ordsets:union(variables(letrec_body(T), S), Vs),
634	    Vs2 = var_list_names(letrec_vars(T)),
635	    case S of
636		true ->
637		    ordsets:subtract(Vs1, Vs2);
638		false ->
639		    ordsets:union(Vs1, Vs2)
640	    end;
641	module ->
642	    Vs = vars_in_defs(module_defs(T), S),
643	    Vs1 = ordsets:union(vars_in_list(module_exports(T), S), Vs),
644	    Vs2 = var_list_names(module_vars(T)),
645	    case S of
646		true ->
647		    ordsets:subtract(Vs1, Vs2);
648		false ->
649		    ordsets:union(Vs1, Vs2)
650	    end
651    end.
652
653vars_in_list(Ts, S) ->
654    vars_in_list(Ts, S, []).
655
656vars_in_list([T | Ts], S, A) ->
657    vars_in_list(Ts, S, ordsets:union(variables(T, S), A));
658vars_in_list([], _, A) ->
659    A.
660
661%% Note that this function only visits the right-hand side of function
662%% definitions.
663
664vars_in_defs(Ds, S) ->
665    vars_in_defs(Ds, S, []).
666
667vars_in_defs([{_, Post} | Ds], S, A) ->
668    vars_in_defs(Ds, S, ordsets:union(variables(Post, S), A));
669vars_in_defs([], _, A) ->
670    A.
671
672%% This amounts to insertion sort. Since the lists are generally short,
673%% it is hardly worthwhile to use an asymptotically better sort.
674
675var_list_names(Vs) ->
676    var_list_names(Vs, []).
677
678var_list_names([V | Vs], A) ->
679    var_list_names(Vs, ordsets:add_element(var_name(V), A));
680var_list_names([], A) ->
681    A.
682
683%% ---------------------------------------------------------------------
684
685%% @spec next_free_variable_name(Tree::cerl()) -> var_name()
686%%
687%%	    var_name() = integer()
688%%
689%% @doc Returns a integer variable name higher than any other integer
690%% variable name in the syntax tree. An exception is thrown if
691%% <code>Tree</code> does not represent a well-formed Core Erlang
692%% syntax tree.
693%%
694%% @see variables/1
695%% @see free_variables/1
696
697-spec next_free_variable_name(cerl:cerl()) -> integer().
698
699next_free_variable_name(T) ->
700    1 + next_free(T, -1).
701
702next_free(T, Max) ->
703    case type(T) of
704        literal ->
705            Max;
706        var ->
707            case var_name(T) of
708                Int when is_integer(Int) ->
709                    max(Int, Max);
710                _ ->
711                    Max
712            end;
713        values ->
714            next_free_in_list(values_es(T), Max);
715        cons ->
716            next_free(cons_hd(T), next_free(cons_tl(T), Max));
717        tuple ->
718            next_free_in_list(tuple_es(T), Max);
719        map ->
720            next_free_in_list([map_arg(T)|map_es(T)], Max);
721        map_pair ->
722            next_free_in_list([map_pair_op(T),map_pair_key(T),
723                               map_pair_val(T)], Max);
724        'let' ->
725            Max1 = next_free(let_body(T), Max),
726            Max2 = next_free_in_list(let_vars(T), Max1),
727            next_free(let_arg(T), Max2);
728        seq ->
729            next_free(seq_arg(T),
730                      next_free(seq_body(T), Max));
731        apply ->
732            next_free(apply_op(T),
733                      next_free_in_list(apply_args(T), Max));
734        call ->
735            next_free(call_module(T),
736                      next_free(call_name(T),
737                                next_free_in_list(
738                                  call_args(T), Max)));
739        primop ->
740            next_free_in_list(primop_args(T), Max);
741        'case' ->
742            next_free(case_arg(T),
743                      next_free_in_list(case_clauses(T), Max));
744        clause ->
745            Max1 = next_free(clause_guard(T),
746                             next_free(clause_body(T), Max)),
747            next_free_in_list(clause_pats(T), Max1);
748        alias ->
749            next_free(alias_var(T),
750                      next_free(alias_pat(T), Max));
751        'fun' ->
752            next_free(fun_body(T),
753                      next_free_in_list(fun_vars(T), Max));
754        'receive' ->
755            Max1 = next_free_in_list(receive_clauses(T),
756                                     next_free(receive_timeout(T), Max)),
757            next_free(receive_action(T), Max1);
758        'try' ->
759            Max1 = next_free(try_body(T), Max),
760            Max2 = next_free_in_list(try_vars(T), Max1),
761            Max3 = next_free(try_handler(T), Max2),
762            Max4 = next_free_in_list(try_evars(T), Max3),
763            next_free(try_arg(T), Max4);
764        'catch' ->
765            next_free(catch_body(T), Max);
766        binary ->
767            next_free_in_list(binary_segments(T), Max);
768        bitstr ->
769            next_free(bitstr_val(T), next_free(bitstr_size(T), Max));
770        letrec ->
771            Max1 = next_free_in_defs(letrec_defs(T), Max),
772            Max2 = next_free(letrec_body(T), Max1),
773            next_free_in_list(letrec_vars(T), Max2);
774        module ->
775            next_free_in_defs(module_defs(T), Max)
776    end.
777
778next_free_in_list([H | T], Max) ->
779    next_free_in_list(T, next_free(H, Max));
780next_free_in_list([], Max) ->
781    Max.
782
783next_free_in_defs([{_, Post} | Ds], Max) ->
784    next_free_in_defs(Ds, next_free(Post, Max));
785next_free_in_defs([], Max) ->
786    Max.
787
788%% ---------------------------------------------------------------------
789
790%% label(Tree::cerl()) -> {cerl(), integer()}
791%%
792%% @equiv label(Tree, 0)
793
794-spec label(cerl:cerl()) -> {cerl:cerl(), integer()}.
795
796label(T) ->
797    label(T, 0).
798
799%% @spec label(Tree::cerl(), N::integer()) -> {cerl(), integer()}
800%%
801%% @doc Labels each expression in the tree. A term <code>{label,
802%% L}</code> is prefixed to the annotation list of each expression node,
803%% where L is a unique number for every node, except for variables (and
804%% function name variables) which get the same label if they represent
805%% the same variable. Constant literal nodes are not labeled.
806%%
807%% <p>The returned value is a tuple <code>{NewTree, Max}</code>, where
808%% <code>NewTree</code> is the labeled tree and <code>Max</code> is 1
809%% plus the largest label value used. All previous annotation terms on
810%% the form <code>{label, X}</code> are deleted.</p>
811%%
812%% <p>The values of L used in the tree is a dense range from
813%% <code>N</code> to <code>Max - 1</code>, where <code>N =&lt; Max
814%% =&lt; N + size(Tree)</code>. Note that it is possible that no
815%% labels are used at all, i.e., <code>N = Max</code>.</p>
816%%
817%% <p>Note: All instances of free variables will be given distinct
818%% labels.</p>
819%%
820%% @see label/1
821%% @see size/1
822
823-spec label(cerl:cerl(), integer()) -> {cerl:cerl(), integer()}.
824
825label(T, N) ->
826    label(T, N, dict:new()).
827
828label(T, N, Env) ->
829    case type(T) of
830 	literal ->
831	    %% Constant literals are not labeled.
832	    {T, N};
833	var ->
834            {As, N1} =
835                case dict:find(var_name(T), Env) of
836		    {ok, L} ->
837		        {A, _} = label_ann(T, L),
838		        {A, N};
839                    error ->
840		        label_ann(T, N)
841                end,
842	    {set_ann(T, As), N1};
843	values ->
844	    {Ts, N1} = label_list(values_es(T), N, Env),
845	    {As, N2} = label_ann(T, N1),
846	    {ann_c_values(As, Ts), N2};
847	cons ->
848	    {T1, N1} = label(cons_hd(T), N, Env),
849	    {T2, N2} = label(cons_tl(T), N1, Env),
850	    {As, N3} = label_ann(T, N2),
851	    {ann_c_cons_skel(As, T1, T2), N3};
852 	tuple ->
853	    {Ts, N1} = label_list(tuple_es(T), N, Env),
854	    {As, N2} = label_ann(T, N1),
855	    {ann_c_tuple_skel(As, Ts), N2};
856 	map ->
857	    case is_c_map_pattern(T) of
858		false ->
859		    {M,  N1} = label(map_arg(T), N, Env),
860		    {Ts, N2} = label_list(map_es(T), N1, Env),
861		    {As, N3} = label_ann(T, N2),
862		    {ann_c_map(As, M, Ts), N3};
863		true ->
864		    {Ts, N1} = label_list(map_es(T), N, Env),
865		    {As, N2} = label_ann(T, N1),
866		    {ann_c_map_pattern(As, Ts), N2}
867	    end;
868	map_pair ->
869	    {Op,  N1} = label(map_pair_op(T), N, Env),
870	    {Key, N2} = label(map_pair_key(T), N1, Env),
871	    {Val, N3} = label(map_pair_val(T), N2, Env),
872	    {As,  N4} = label_ann(T, N3),
873	    {ann_c_map_pair(As,Op,Key,Val), N4};
874 	'let' ->
875	    {A, N1} = label(let_arg(T), N, Env),
876	    {Vs, N2, Env1} = label_vars(let_vars(T), N1, Env),
877	    {B, N3} = label(let_body(T), N2, Env1),
878	    {As, N4} = label_ann(T, N3),
879	    {ann_c_let(As, Vs, A, B), N4};
880	seq ->
881	    {A, N1} = label(seq_arg(T), N, Env),
882	    {B, N2} = label(seq_body(T), N1, Env),
883	    {As, N3} = label_ann(T, N2),
884 	    {ann_c_seq(As, A, B), N3};
885 	apply ->
886	    {E, N1} = label(apply_op(T), N, Env),
887	    {Es, N2} = label_list(apply_args(T), N1, Env),
888	    {As, N3} = label_ann(T, N2),
889	    {ann_c_apply(As, E, Es), N3};
890 	call ->
891	    {M, N1} = label(call_module(T), N, Env),
892	    {F, N2} = label(call_name(T), N1, Env),
893	    {Es, N3} = label_list(call_args(T), N2, Env),
894	    {As, N4} = label_ann(T, N3),
895 	    {ann_c_call(As, M, F, Es), N4};
896 	primop ->
897	    {F, N1} = label(primop_name(T), N, Env),
898	    {Es, N2} = label_list(primop_args(T), N1, Env),
899	    {As, N3} = label_ann(T, N2),
900	    {ann_c_primop(As, F, Es), N3};
901 	'case' ->
902	    {A, N1} = label(case_arg(T), N, Env),
903	    {Cs, N2} = label_list(case_clauses(T), N1, Env),
904	    {As, N3} = label_ann(T, N2),
905 	    {ann_c_case(As, A, Cs), N3};
906 	clause ->
907	    {_, N1, Env1} = label_vars(clause_vars(T), N, Env),
908	    {Ps, N2} = label_list(clause_pats(T), N1, Env1),
909	    {G, N3} = label(clause_guard(T), N2, Env1),
910	    {B, N4} = label(clause_body(T), N3, Env1),
911	    {As, N5} = label_ann(T, N4),
912	    {ann_c_clause(As, Ps, G, B), N5};
913 	alias ->
914	    {V, N1} = label(alias_var(T), N, Env),
915	    {P, N2} = label(alias_pat(T), N1, Env),
916	    {As, N3} = label_ann(T, N2),
917	    {ann_c_alias(As, V, P), N3};
918 	'fun' ->
919	    {Vs, N1, Env1} = label_vars(fun_vars(T), N, Env),
920	    {B, N2} = label(fun_body(T), N1, Env1),
921	    {As, N3} = label_ann(T, N2),
922	    {ann_c_fun(As, Vs, B), N3};
923 	'receive' ->
924	    {Cs, N1} = label_list(receive_clauses(T), N, Env),
925	    {E, N2} = label(receive_timeout(T), N1, Env),
926	    {A, N3} = label(receive_action(T), N2, Env),
927	    {As, N4} = label_ann(T, N3),
928	    {ann_c_receive(As, Cs, E, A), N4};
929 	'try' ->
930	    {E, N1} = label(try_arg(T), N, Env),
931	    {Vs, N2, Env1} = label_vars(try_vars(T), N1, Env),
932	    {B, N3} = label(try_body(T), N2, Env1),
933	    {Evs, N4, Env2} = label_vars(try_evars(T), N3, Env),
934	    {H, N5} = label(try_handler(T), N4, Env2),
935	    {As, N6} = label_ann(T, N5),
936	    {ann_c_try(As, E, Vs, B, Evs, H), N6};
937 	'catch' ->
938	    {B, N1} = label(catch_body(T), N, Env),
939	    {As, N2} = label_ann(T, N1),
940	    {ann_c_catch(As, B), N2};
941	binary ->
942	    {Ds, N1} = label_list(binary_segments(T), N, Env),
943	    {As, N2} = label_ann(T, N1),
944	    {ann_c_binary(As, Ds), N2};
945	bitstr ->
946	    {Val, N1} = label(bitstr_val(T), N, Env),
947	    {Size, N2} = label(bitstr_size(T), N1, Env),
948	    {Unit, N3} = label(bitstr_unit(T), N2, Env),
949	    {Type, N4} = label(bitstr_type(T), N3, Env),
950	    {Flags, N5} = label(bitstr_flags(T), N4, Env),
951	    {As, N6} = label_ann(T, N5),
952	    {ann_c_bitstr(As, Val, Size, Unit, Type, Flags), N6};
953	letrec ->
954	    {_, N1, Env1} = label_vars(letrec_vars(T), N, Env),
955	    {Ds, N2} = label_defs(letrec_defs(T), N1, Env1),
956	    {B, N3} = label(letrec_body(T), N2, Env1),
957	    {As, N4} = label_ann(T, N3),
958	    {ann_c_letrec(As, Ds, B), N4};
959	module ->
960	    %% The module name is not labeled.
961	    {_, N1, Env1} = label_vars(module_vars(T), N, Env),
962	    {Ts, N2} = label_defs(module_attrs(T), N1, Env1),
963	    {Ds, N3} = label_defs(module_defs(T), N2, Env1),
964	    {Es, N4} = label_list(module_exports(T), N3, Env1),
965	    {As, N5} = label_ann(T, N4),
966	    {ann_c_module(As, module_name(T), Es, Ts, Ds), N5}
967    end.
968
969label_list([T | Ts], N, Env) ->
970    {T1, N1} = label(T, N, Env),
971    {Ts1, N2} = label_list(Ts, N1, Env),
972    {[T1 | Ts1], N2};
973label_list([], N, _Env) ->
974    {[], N}.
975
976label_vars([T | Ts], N, Env) ->
977    Env1 = dict:store(var_name(T), N, Env),
978    {As, N1} = label_ann(T, N),
979    T1 = set_ann(T, As),
980    {Ts1, N2, Env2} = label_vars(Ts, N1, Env1),
981    {[T1 | Ts1], N2, Env2};
982label_vars([], N, Env) ->
983    {[], N, Env}.
984
985label_defs([{F, T} | Ds], N, Env) ->
986    {F1, N1} = label(F, N, Env),
987    {T1, N2} = label(T, N1, Env),
988    {Ds1, N3} = label_defs(Ds, N2, Env),
989    {[{F1, T1} | Ds1], N3};
990label_defs([], N, _Env) ->
991    {[], N}.
992
993label_ann(T, N) ->
994    {[{label, N} | filter_labels(get_ann(T))], N + 1}.
995
996filter_labels([{label, _} | As]) ->
997    filter_labels(As);
998filter_labels([A | As]) ->
999    [A | filter_labels(As)];
1000filter_labels([]) ->
1001    [].
1002
1003-spec get_label(cerl:cerl()) -> 'top' | integer().
1004
1005get_label(T) ->
1006    case get_ann(T) of
1007	[{label, L} | _] -> L;
1008	_ -> throw({missing_label, T})
1009    end.
1010