-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
cerl.erl
4129 lines (2984 loc) · 112 KB
/
cerl.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @copyright 1999-2002 Richard Carlsson
%% @author Richard Carlsson <carlsson.richard@gmail.com>
%%
-module(cerl).
-moduledoc """
Core Erlang abstract syntax trees.
> #### Note {: .info }
>
> The public interface of the Erlang compiler can be found in
> module `m:compile`.
>
> This module is an internal part of the compiler. Its API is not guaranteed
> to remain compatible between releases.
This module defines an abstract data type for representing Core Erlang source
code as syntax trees.
A recommended starting point for the first-time user is the documentation of the
function `type/1`.
> #### Note {: .info }
>
> This module deals with the composition and decomposition of _syntactic_ entities
> (as opposed to semantic ones); its purpose is to hide all direct references to
> the data structures used to represent these entities. With few exceptions, the
> functions in this module perform no semantic interpretation of their inputs, and
> in general, the user is assumed to pass type-correct arguments - if this is not
> done, the effects are not defined.
>
> Currently, the internal data structure used is the same as the record-based data
> structures used traditionally in the Beam compiler.
>
> The internal representations of abstract syntax trees are subject to change
> without notice, and should not be documented outside this module. Furthermore,
> we do not give any guarantees on how an abstract syntax tree may or may not be
> represented, _with the following exceptions_: no syntax tree is represented by a
> single atom, such as `none`, by a list constructor `[X | Y]`, or by the empty
> list `[]`. This can be relied on when writing functions that operate on syntax
> trees.
""".
-export([abstract/1, add_ann/2, alias_pat/1, alias_var/1,
ann_abstract/2, ann_c_alias/3, ann_c_apply/3, ann_c_atom/2,
ann_c_bitstr/5, ann_c_bitstr/6,
ann_c_call/4, ann_c_case/3, ann_c_catch/2, ann_c_char/2,
ann_c_clause/3, ann_c_clause/4, ann_c_cons/3, ann_c_float/2,
ann_c_fname/3, ann_c_fun/3, ann_c_int/2,
ann_c_let/4, ann_c_letrec/3,
ann_c_map/2, ann_c_map/3, ann_c_map_pattern/2, ann_c_map_pair/4,
ann_c_module/4, ann_c_module/5, ann_c_nil/1,
ann_c_cons_skel/3, ann_c_tuple_skel/2, ann_c_primop/3,
ann_c_receive/2, ann_c_receive/4, ann_c_seq/3, ann_c_string/2,
ann_c_try/6, ann_c_tuple/2, ann_c_values/2, ann_c_var/2,
ann_make_data/3, ann_make_list/2, ann_make_list/3,
ann_make_data_skel/3, ann_make_tree/3, apply_args/1,
apply_arity/1, apply_op/1, atom_lit/1, atom_name/1, atom_val/1,
bitstr_val/1, bitstr_size/1, bitstr_bitsize/1,
bitstr_unit/1, bitstr_type/1, bitstr_flags/1,
c_alias/2, c_apply/2, c_atom/1, c_call/3, c_case/2, c_catch/1,
c_char/1, c_clause/2, c_clause/3, c_cons/2, c_float/1,
c_fname/2, c_fun/2, c_int/1, c_let/3, c_letrec/2,
c_map/1, c_map/2, c_map_pattern/1, c_map_pair/2, c_map_pair_exact/2,
c_module/3, c_module/4, c_nil/0,
c_cons_skel/2, c_tuple_skel/1, c_primop/2,
c_receive/1, c_receive/3, c_seq/2, c_string/1, c_try/5,
c_tuple/1, c_values/1, c_var/1, call_args/1, call_arity/1,
call_module/1, call_name/1, case_arg/1, case_arity/1,
case_clauses/1, catch_body/1, char_lit/1, char_val/1,
clause_arity/1, clause_body/1, clause_guard/1, clause_pats/1,
clause_vars/1, concrete/1, cons_hd/1, cons_tl/1, copy_ann/2,
data_arity/1, data_es/1, data_type/1, float_lit/1, float_val/1,
fname_arity/1, fname_id/1, fold_literal/1, from_records/1,
fun_arity/1, fun_body/1, fun_vars/1, get_ann/1, int_lit/1,
int_val/1, is_c_alias/1, is_c_apply/1, is_c_atom/1,
is_c_bitstr/1,
is_c_call/1, is_c_case/1, is_c_catch/1, is_c_char/1,
is_c_clause/1, is_c_cons/1, is_c_float/1, is_c_fname/1,
is_c_fun/1, is_c_int/1, is_c_let/1, is_c_letrec/1, is_c_list/1,
is_c_map/1, is_c_map_empty/1, is_c_map_pattern/1,
is_c_module/1, is_c_nil/1, is_c_primop/1, is_c_receive/1,
is_c_seq/1, is_c_string/1, is_c_try/1, is_c_tuple/1,
is_c_values/1, is_c_var/1, is_data/1, is_leaf/1, is_literal/1,
is_literal_term/1, is_print_char/1, is_print_string/1,
let_arg/1, let_arity/1, let_body/1, let_vars/1, letrec_body/1,
letrec_defs/1, letrec_vars/1, list_elements/1, list_length/1,
make_data/2, make_list/1, make_list/2,
make_data_skel/2, make_tree/2,
map_arg/1, map_es/1,
map_pair_key/1, map_pair_op/1, map_pair_val/1,
meta/1, module_attrs/1, module_defs/1,
module_exports/1, module_name/1, module_vars/1,
pat_list_vars/1, pat_vars/1, primop_args/1, primop_arity/1,
primop_name/1, receive_action/1, receive_clauses/1,
receive_timeout/1, seq_arg/1, seq_body/1, set_ann/2,
string_lit/1, string_val/1, subtrees/1, to_records/1,
try_arg/1, try_body/1, try_vars/1, try_evars/1, try_handler/1,
tuple_arity/1, tuple_es/1, type/1, unfold_literal/1,
update_c_alias/3, update_c_apply/3, update_c_call/4,
update_c_case/3, update_c_catch/2, update_c_clause/4,
update_c_cons/3, update_c_cons_skel/3, update_c_fname/2,
update_c_fname/3, update_c_fun/3, update_c_let/4,
update_c_letrec/3, update_c_map/3, update_c_map_pair/4,
update_c_module/5, update_c_primop/3,
update_c_receive/4, update_c_seq/3, update_c_try/6,
update_c_tuple/2, update_c_tuple_skel/2, update_c_values/2,
update_c_var/2, update_data/3, update_list/2, update_list/3,
update_data_skel/3, update_tree/2, update_tree/3,
values_arity/1, values_es/1, var_name/1, c_binary/1,
update_c_binary/2, ann_c_binary/2, is_c_binary/1,
binary_segments/1, c_bitstr/3, c_bitstr/4, c_bitstr/5,
update_c_bitstr/5, update_c_bitstr/6
]).
-export_type([c_binary/0, c_bitstr/0, c_call/0, c_clause/0, c_cons/0, c_fun/0,
c_let/0, c_literal/0, c_map/0, c_map_pair/0,
c_module/0, c_tuple/0,
c_values/0, c_var/0, cerl/0, var_name/0]).
-include("core_parse.hrl").
-type c_alias() :: #c_alias{}.
-type c_apply() :: #c_apply{}.
-type c_binary() :: #c_binary{}.
-type c_bitstr() :: #c_bitstr{}.
-type c_call() :: #c_call{}.
-type c_case() :: #c_case{}.
-type c_catch() :: #c_catch{}.
-type c_clause() :: #c_clause{}.
-type c_cons() :: #c_cons{}.
-type c_fun() :: #c_fun{}.
-type c_let() :: #c_let{}.
-type c_letrec() :: #c_letrec{}.
-type c_literal() :: #c_literal{}.
-type c_map() :: #c_map{}.
-type c_map_pair() :: #c_map_pair{}.
-type c_module() :: #c_module{}.
-type c_opaque() :: #c_opaque{}.
-type c_primop() :: #c_primop{}.
-type c_receive() :: #c_receive{}.
-type c_seq() :: #c_seq{}.
-type c_try() :: #c_try{}.
-type c_tuple() :: #c_tuple{}.
-type c_values() :: #c_values{}.
-type c_var() :: #c_var{}.
-type cerl() :: c_alias() | c_apply() | c_binary() | c_bitstr()
| c_call() | c_case() | c_catch() | c_clause() | c_cons()
| c_fun() | c_let() | c_letrec() | c_literal()
| c_map() | c_map_pair()
| c_module() | c_opaque()
| c_primop() | c_receive() | c_seq()
| c_try() | c_tuple() | c_values() | c_var().
-type var_name() :: integer() | atom() | {atom(), integer()}.
%% =====================================================================
%% Representation (general)
%%
%% All nodes are represented by tuples of arity 2 or (generally)
%% greater, whose first element is an atom which uniquely identifies the
%% type of the node, and whose second element is a (proper) list of
%% annotation terms associated with the node - this is by default empty.
%%
%% For most node constructor functions, there are analogous functions
%% named 'ann_...', taking one extra argument 'As' (always the first
%% argument), specifying an annotation list at node creation time.
%% Similarly, there are also functions named 'update_...', taking one
%% extra argument 'Old', specifying a node from which all fields not
%% explicitly given as arguments should be copied (generally, this is
%% the annotation field only).
%% =====================================================================
-type ctype() :: 'alias' | 'apply' | 'binary' | 'bitstr' | 'call' | 'case'
| 'catch' | 'clause' | 'cons' | 'fun' | 'let' | 'letrec'
| 'literal' | 'map' | 'map_pair' | 'module' | 'primop'
| 'receive' | 'seq' | 'try' | 'tuple' | 'values' | 'var'.
-doc """
Returns the type tag of `Node`.
Current node types are:
- `alias`
- `apply`
- `binary`
- `bitstr`
- `call`
- `case`
- `catch`
- `clause`
- `cons`
- `fun`
- `let`
- `letrec`
- `literal`
- `map`
- `map_pair`
- `module`
- `opaque`
- `primop`
- `receive`
- `seq`
- `try`
- `tuple`
- `values`
- `var`
> #### Note {: .info }
> The name of the primary constructor function for a node type is always the
> name of the type itself, prefixed by "`c_`"; recognizer predicates are
> correspondingly prefixed by "`is_c_`". Furthermore, to simplify preservation of
> annotations (cf. [`get_ann/1`](`get_ann/1`)), there are analogous constructor
> functions prefixed by "`ann_c_`| and "`update_c_`", for setting the annotation
> list of the new node to either a specific value or to the annotations of an
> existing node, respectively.
The only purpose of the `opaque` type is to facilitate testing of the compiler.
_See also: _`abstract/1`, `c_alias/2`, `c_apply/2`, `c_binary/1`, `c_bitstr/5`,
`c_call/3`, `c_case/2`, `c_catch/1`, `c_clause/3`, `c_cons/2`, `c_fun/2`,
`c_let/3`, `c_letrec/2`, `c_module/3`, `c_primop/2`, `c_receive/1`, `c_seq/2`,
`c_try/5`, `c_tuple/1`, `c_values/1`, `c_var/1`, `data_type/1`,
`from_records/1`, `get_ann/1`, `meta/1`, `subtrees/1`, `to_records/1`.
""".
-spec type(Node :: cerl()) -> ctype().
type(#c_alias{}) -> alias;
type(#c_apply{}) -> apply;
type(#c_binary{}) -> binary;
type(#c_bitstr{}) -> bitstr;
type(#c_call{}) -> call;
type(#c_case{}) -> 'case';
type(#c_catch{}) -> 'catch';
type(#c_clause{}) -> clause;
type(#c_cons{}) -> cons;
type(#c_fun{}) -> 'fun';
type(#c_let{}) -> 'let';
type(#c_letrec{}) -> letrec;
type(#c_literal{}) -> literal;
type(#c_map{}) -> map;
type(#c_map_pair{}) -> map_pair;
type(#c_module{}) -> module;
type(#c_primop{}) -> primop;
type(#c_receive{}) -> 'receive';
type(#c_seq{}) -> seq;
type(#c_try{}) -> 'try';
type(#c_tuple{}) -> tuple;
type(#c_values{}) -> values;
type(#c_var{}) -> var;
type(#c_opaque{}) -> opaque.
-doc """
Returns `true` if `Node` is a leaf node, otherwise `false`.
The current leaf node types are `literal` and `var`.
Note: all literals (cf. [`is_literal/1`](`is_literal/1`)) are leaf nodes, even
if they represent structured (constant) values such as `{foo, [bar, baz]}`. Also
note that variables are leaf nodes but not literals.
_See also: _`is_literal/1`, `type/1`.
""".
-spec is_leaf(Node :: cerl()) -> boolean().
is_leaf(Node) ->
case type(Node) of
literal -> true;
var -> true;
_ -> false
end.
-doc """
Returns the list of user annotations associated with a syntax tree node.
For a newly created node, this is the empty list. The annotations may
be any terms.
_See also: _`set_ann/2`.
""".
-spec get_ann(Node :: cerl()) -> [term()].
get_ann(Node) ->
element(2, Node).
-doc """
Sets the list of user annotations of `Node` to `Annotations`.
_See also: _`add_ann/2`, `copy_ann/2`, `get_ann/1`.
""".
-spec set_ann(Node :: cerl(), Annotations :: [term()]) -> cerl().
set_ann(Node, List) ->
setelement(2, Node, List).
-doc """
Appends `Annotations` to the list of user annotations of `Node`.
Note: this is equivalent to
[`set_ann(Node, Annotations ++ get_ann(Node))`](`set_ann/2`), but potentially
more efficient.
_See also: _`get_ann/1`, `set_ann/2`.
""".
-spec add_ann(Annotations :: [term()], Node :: cerl()) -> cerl().
add_ann(Terms, Node) ->
set_ann(Node, Terms ++ get_ann(Node)).
-doc """
Copies the list of user annotations from `Source` to `Target`.
Note: this is equivalent to [`set_ann(Target, get_ann(Source))`](`set_ann/2`),
but potentially more efficient.
_See also: _`get_ann/1`, `set_ann/2`.
""".
-spec copy_ann(Source :: cerl(), Target :: cerl()) -> cerl().
copy_ann(Source, Target) ->
set_ann(Target, get_ann(Source)).
-doc """
Creates a syntax tree corresponding to an Erlang term.
`Term` must be a literal term, that is, one that can be represented as
a source code literal. Thus, it may not contain a process identifier,
port, reference, binary or function value as a subterm.
Note: This is a constant time operation.
_See also: _`ann_abstract/2`, `concrete/1`, `is_literal/1`, `is_literal_term/1`.
""".
-spec abstract(Term :: term()) -> c_literal().
abstract(T) ->
#c_literal{val = T}.
-doc "_See also: _`abstract/1`.".
-spec ann_abstract(Annotations :: [term()], Term :: term()) -> c_literal().
ann_abstract(As, T) ->
#c_literal{val = T, anno = As}.
-doc """
Returns `true` if `Term` can be represented as a literal, otherwise `false`.
This function takes time proportional to the size of `Term`.
_See also: _`abstract/1`.
""".
-spec is_literal_term(Term :: term()) -> boolean().
is_literal_term(T) when is_integer(T) -> true;
is_literal_term(T) when is_float(T) -> true;
is_literal_term(T) when is_atom(T) -> true;
is_literal_term([]) -> true;
is_literal_term([H | T]) ->
is_literal_term(H) andalso is_literal_term(T);
is_literal_term(T) when is_tuple(T) ->
is_literal_term_list(tuple_to_list(T));
is_literal_term(B) when is_bitstring(B) -> true;
is_literal_term(M) when is_map(M) ->
is_literal_term_list(maps:to_list(M));
is_literal_term(F) when is_function(F) ->
erlang:fun_info(F, type) =:= {type,external};
is_literal_term(_) ->
false.
-spec is_literal_term_list([term()]) -> boolean().
is_literal_term_list([T | Ts]) ->
case is_literal_term(T) of
true ->
is_literal_term_list(Ts);
false ->
false
end;
is_literal_term_list([]) ->
true.
-doc """
Returns the Erlang term represented by a syntax tree.
An exception is thrown if `Node` does not represent a literal term.
Note: This is a constant time operation.
_See also: _`abstract/1`, `is_literal/1`.
""".
-spec concrete(Node :: c_literal()) -> term().
concrete(#c_literal{val = V}) ->
V.
-doc """
Returns `true` if `Node` represents a literal term, otherwise `false`.
This function returns `true` if and only if the value of
[`concrete(Node)`](`concrete/1`) is defined.
Note: This is a constant time operation.
_See also: _`abstract/1`, `concrete/1`, `fold_literal/1`.
""".
-spec is_literal(Node :: cerl()) -> boolean().
is_literal(#c_literal{}) ->
true;
is_literal(_) ->
false.
-doc """
Ensures that literals have a compact representation.
This is occasionally useful if
[`c_cons_skel/2`](`c_cons_skel/2`), [`c_tuple_skel/1`](`c_tuple_skel/1`) or
[`unfold_literal/1`](`unfold_literal/1`) were used in the construction of
`Node`, and you want to revert to the normal "folded" representation of
literals. If `Node` represents a tuple or list constructor, its elements are
rewritten recursively, and the node is reconstructed using
[`c_cons/2`](`c_cons/2`) or [`c_tuple/1`](`c_tuple/1`), respectively; otherwise,
`Node` is not changed.
_See also: _`c_cons/2`, `c_cons_skel/2`, `c_tuple/1`, `c_tuple_skel/1`,
`is_literal/1`, `unfold_literal/1`.
""".
-spec fold_literal(Node :: cerl()) -> cerl().
fold_literal(Node) ->
case type(Node) of
tuple ->
update_c_tuple(Node, fold_literal_list(tuple_es(Node)));
cons ->
update_c_cons(Node, fold_literal(cons_hd(Node)),
fold_literal(cons_tl(Node)));
_ ->
Node
end.
fold_literal_list([E | Es]) ->
[fold_literal(E) | fold_literal_list(Es)];
fold_literal_list([]) ->
[].
-doc """
Ensures that literals have a fully expanded representation.
If `Node` represents a literal tuple or list constructor, its elements
are rewritten recursively, and the node is reconstructed using
[`c_cons_skel/2`](`c_cons_skel/2`) or
[`c_tuple_skel/1`](`c_tuple_skel/1`), respectively; otherwise, `Node`
is not changed. The `fold_literal/1` can be used to revert to the
normal compact representation.
_See also: _`c_cons/2`, `c_cons_skel/2`, `c_tuple/1`, `c_tuple_skel/1`,
`fold_literal/1`, `is_literal/1`.
""".
-spec unfold_literal(Node :: cerl()) -> cerl().
unfold_literal(Node) ->
case type(Node) of
literal ->
copy_ann(Node, unfold_concrete(concrete(Node)));
_ ->
Node
end.
unfold_concrete(Val) ->
case Val of
_ when is_tuple(Val) ->
c_tuple_skel(unfold_concrete_list(tuple_to_list(Val)));
[H|T] ->
c_cons_skel(unfold_concrete(H), unfold_concrete(T));
_ ->
abstract(Val)
end.
unfold_concrete_list([E | Es]) ->
[unfold_concrete(E) | unfold_concrete_list(Es)];
unfold_concrete_list([]) ->
[].
%% ---------------------------------------------------------------------
-doc #{equiv => c_module(Name, Exports, [], Definitions)}.
-spec c_module(Name :: cerl(),
Exports :: [cerl()],
Definitions :: [{cerl(), cerl()}]) -> c_module().
c_module(Name, Exports, Es) ->
#c_module{name = Name, exports = Exports, attrs = [], defs = Es}.
-doc """
Creates an abstract module definition.
The result represents
```text
module Name [E1, ..., Ek]
attributes [K1 = T1, ...,
Km = Tm]
V1 = F1
...
Vn = Fn
end
```
if `Exports` = `[E1, ..., Ek]`, `Attributes` = `[{K1, T1}, ..., {Km, Tm}]`, and
`Definitions` = `[{V1, F1}, ..., {Vn, Fn}]`.
`Name` and all the `Ki` must be atom literals, and all the `Ti` must be constant
literals. All the `Vi` and `Ei` must have type `var` and represent function
names. All the `Fi` must have type `'fun'`.
_See also: _`ann_c_module/4`, `ann_c_module/5`, `c_atom/1`, `c_fun/2`,
`c_module/3`, `c_var/1`, `is_literal/1`, `module_attrs/1`, `module_defs/1`,
`module_exports/1`, `module_name/1`, `module_vars/1`, `update_c_module/5`.
""".
-spec c_module(Name :: cerl(), Exports :: [cerl()],
Attributes :: [{cerl(), cerl()}],
Definitions :: [{cerl(), cerl()}]) ->
c_module().
c_module(Name, Exports, Attrs, Es) ->
#c_module{name = Name, exports = Exports, attrs = Attrs, defs = Es}.
-doc "_See also: _`ann_c_module/5`, `c_module/3`.".
-spec ann_c_module(Annotations :: [term()], Name :: cerl(),
Exports :: [cerl()], Definitions :: [{cerl(), cerl()}]) ->
c_module().
ann_c_module(As, Name, Exports, Es) ->
#c_module{name = Name, exports = Exports, attrs = [], defs = Es,
anno = As}.
-doc "_See also: _`ann_c_module/4`, `c_module/4`.".
-spec ann_c_module(Annotations :: [term()], Name :: cerl(),
Exports :: [cerl()],
Attributes :: [{cerl(), cerl()}],
Definitions :: [{cerl(), cerl()}]) -> c_module().
ann_c_module(As, Name, Exports, Attrs, Es) ->
#c_module{name = Name, exports = Exports, attrs = Attrs, defs = Es,
anno = As}.
-doc "_See also: _`c_module/4`.".
-spec update_c_module(Node :: c_module(), Name :: cerl(), Exports ::[cerl()],
Attributes :: [{cerl(), cerl()}],
Definitions :: [{cerl(), cerl()}]) -> c_module().
update_c_module(Node, Name, Exports, Attrs, Es) ->
#c_module{name = Name, exports = Exports, attrs = Attrs, defs = Es,
anno = get_ann(Node)}.
-doc """
Returns `true` if `Node` is an abstract module definition, otherwise `false`.
_See also: _`type/1`.
""".
-spec is_c_module(Node :: cerl()) -> boolean().
is_c_module(#c_module{}) ->
true;
is_c_module(_) ->
false.
-doc """
Returns the name subtree of an abstract module definition.
_See also: _`c_module/4`.
""".
-spec module_name(Node :: c_module()) -> cerl().
module_name(Node) ->
Node#c_module.name.
-doc """
Returns the list of exports subtrees of an abstract module definition.
_See also: _`c_module/4`.
""".
-spec module_exports(Node :: c_module()) -> [cerl()].
module_exports(Node) ->
Node#c_module.exports.
-doc """
Returns the list of pairs of attribute key/value subtrees of an abstract module
definition.
_See also: _`c_module/4`.
""".
-spec module_attrs(Node :: c_module()) -> [{cerl(), cerl()}].
module_attrs(Node) ->
Node#c_module.attrs.
-doc """
Returns the list of function definitions of an abstract module definition.
_See also: _`c_module/4`.
""".
-spec module_defs(Node :: c_module()) -> [{cerl(), cerl()}].
module_defs(Node) ->
Node#c_module.defs.
-doc """
Returns the list of left-hand side function variable subtrees of an abstract
module definition.
_See also: _`c_module/4`.
""".
-spec module_vars(Node :: c_module()) -> [cerl()].
module_vars(Node) ->
[F || {F, _} <- module_defs(Node)].
%% ---------------------------------------------------------------------
-doc """
Creates an abstract integer literal.
The lexical representation is the canonical decimal numeral of `Value`.
_See also: _`ann_c_int/2`, `c_char/1`, `int_lit/1`, `int_val/1`, `is_c_int/1`.
""".
-spec c_int(Value :: integer()) -> c_literal().
c_int(Value) ->
#c_literal{val = Value}.
-doc "_See also: _`c_int/1`.".
-spec ann_c_int(Annotations :: [term()], Value :: integer()) -> c_literal().
ann_c_int(As, Value) ->
#c_literal{val = Value, anno = As}.
-doc """
Returns `true` if `Node` represents an integer literal, otherwise `false`.
_See also: _`c_int/1`.
""".
-spec is_c_int(Node :: cerl()) -> boolean().
is_c_int(#c_literal{val = V}) when is_integer(V) ->
true;
is_c_int(_) ->
false.
-doc """
Returns the value represented by an integer literal node.
_See also: _`c_int/1`.
""".
-spec int_val(Node :: c_literal()) -> integer().
int_val(Node) ->
Node#c_literal.val.
-doc """
Returns the numeral string represented by an integer literal node.
_See also: _`c_int/1`.
""".
-spec int_lit(Node :: c_literal()) -> string().
int_lit(Node) ->
integer_to_list(int_val(Node)).
%% ---------------------------------------------------------------------
-doc """
Creates an abstract floating-point literal.
The lexical representation is the decimal floating-point numeral of
`Value`.
_See also: _`ann_c_float/2`, `float_lit/1`, `float_val/1`, `is_c_float/1`.
""".
-spec c_float(Value :: float()) -> c_literal().
c_float(Value) ->
#c_literal{val = Value}.
-doc "_See also: _`c_float/1`.".
-spec ann_c_float(Annotations :: [term()], Value :: float()) -> c_literal().
ann_c_float(As, Value) ->
#c_literal{val = Value, anno = As}.
-doc """
Returns `true` if `Node` represents a floating-point literal, otherwise `false`.
_See also: _`c_float/1`.
""".
-spec is_c_float(Node :: cerl()) -> boolean().
is_c_float(#c_literal{val = V}) when is_float(V) ->
true;
is_c_float(_) ->
false.
-doc """
Returns the value represented by a floating-point literal node.
_See also: _`c_float/1`.
""".
-spec float_val(Node :: c_literal()) -> float().
float_val(Node) ->
Node#c_literal.val.
-doc """
Returns the numeral string represented by a floating-point literal node.
_See also: _`c_float/1`.
""".
-spec float_lit(Node :: c_literal()) -> string().
float_lit(Node) ->
float_to_list(float_val(Node)).
%% ---------------------------------------------------------------------
-doc """
Creates an abstract atom literal.
The print name of the atom is the character sequence represented by
`Name`.
Note: passing a string as argument to this function causes a corresponding atom
to be created for the internal representation.
_See also: _`ann_c_atom/2`, `atom_lit/1`, `atom_name/1`, `atom_val/1`,
`is_c_atom/1`.
""".
-spec c_atom(Name :: atom() | string()) -> c_literal().
c_atom(Name) when is_atom(Name) ->
#c_literal{val = Name};
c_atom(Name) ->
#c_literal{val = list_to_atom(Name)}.
-doc "_See also: _`c_atom/1`.".
-spec ann_c_atom(Annotations :: [term()], Name :: atom() | string()) -> c_literal().
ann_c_atom(As, Name) when is_atom(Name) ->
#c_literal{val = Name, anno = As};
ann_c_atom(As, Name) ->
#c_literal{val = list_to_atom(Name), anno = As}.
-doc """
Returns `true` if `Node` represents an atom literal, otherwise `false`.
_See also: _`c_atom/1`.
""".
-spec is_c_atom(Node :: cerl()) -> boolean().
is_c_atom(#c_literal{val = V}) when is_atom(V) ->
true;
is_c_atom(_) ->
false.
-doc """
Returns the value represented by an abstract atom.
_See also: _`c_atom/1`.
""".
-spec atom_val(Node :: c_literal()) -> atom().
atom_val(Node) ->
Node#c_literal.val.
-doc """
Returns the printname of an abstract atom.
_See also: _`c_atom/1`.
""".
-spec atom_name(Node :: c_literal()) -> string().
atom_name(Node) ->
atom_to_list(atom_val(Node)).
%% TODO: replace the use of the unofficial 'write_string/2'.
-doc """
Returns the literal string represented by an abstract atom. This always includes
surrounding single-quote characters.
Note that an abstract atom may have several literal representations, and that
the representation yielded by this function is not fixed; for example,
[`atom_lit(c_atom("a\012b"))`](`atom_lit/1`) could yield the string
`"\'a\\nb\'"`.
_See also: _`c_atom/1`.
""".
-spec atom_lit(Node :: cerl()) -> nonempty_string().
atom_lit(Node) ->
io_lib:write_string(atom_name(Node), $'). %' stupid Emacs.
%% ---------------------------------------------------------------------
-doc """
Creates an abstract character literal.
If the local implementation of Erlang defines `t:char/0` as a subset
of `t:integer/0`, this function is equivalent to
[`c_int/1`](`c_int/1`). Otherwise, if the given value is an integer,
it will be converted to the character with the corresponding code. The
lexical representation of a character is "`$Char`", where `Char` is a
single printing character or an escape sequence.
_See also: _`ann_c_char/2`, `c_int/1`, `c_string/1`, `char_lit/1`, `char_val/1`,
`is_c_char/1`, `is_print_char/1`.
""".
-spec c_char(Value :: non_neg_integer()) -> c_literal().
c_char(Value) when is_integer(Value), Value >= 0 ->
#c_literal{val = Value}.
-doc "_See also: _`c_char/1`.".
-spec ann_c_char(Annotations :: [term()], Value :: char()) -> c_literal().
ann_c_char(As, Value) ->
#c_literal{val = Value, anno = As}.
-doc """
Returns `true` if `Node` may represent a character literal, otherwise `false`.
If the local implementation of Erlang defines `t:char/0` as a subset of
`t:integer/0`, then `is_c_int(Node)` will also yield `true`.
_See also: _`c_char/1`, `is_print_char/1`.
""".
-spec is_c_char(Node :: c_literal()) -> boolean().
is_c_char(#c_literal{val = V}) when is_integer(V), V >= 0 ->
is_char_value(V);
is_c_char(_) ->
false.
-doc """
Returns `true` if `Node` may represent a "printing" character, otherwise
`false`. (Cf. [`is_c_char/1`](`is_c_char/1`).)
A "printing" character has either a given graphical representation, or
a "named" escape sequence such as "`\n`". Currently, only ISO 8859-1
(Latin-1) character values are recognized.
_See also: _`c_char/1`, `is_c_char/1`.
""".
-spec is_print_char(Node :: cerl()) -> boolean().
is_print_char(#c_literal{val = V}) when is_integer(V), V >= 0 ->
is_print_char_value(V);
is_print_char(_) ->
false.
-doc """
Returns the value represented by an abstract character literal.
_See also: _`c_char/1`.
""".
-spec char_val(Node :: c_literal()) -> char().
char_val(Node) ->
Node#c_literal.val.
-doc """
Returns the literal string represented by an abstract character. This includes a
leading `$` character.
Currently, all characters that are not in the set of ISO 8859-1
(Latin-1) "printing" characters will be escaped.
_See also: _`c_char/1`.
""".
-spec char_lit(Node :: c_literal()) -> nonempty_string().
char_lit(Node) ->
io_lib:write_char(char_val(Node)).
%% ---------------------------------------------------------------------
-doc """
Creates an abstract string literal.
Equivalent to creating an abstract list of the corresponding character
literals (cf. [`is_c_string/1`](`is_c_string/1`)), but is typically
more efficient. The lexical representation of a string is "`"Chars"`",
where `Chars` is a sequence of printing characters or spaces.
_See also: _`ann_c_string/2`, `c_char/1`, `is_c_string/1`, `is_print_string/1`,
`string_lit/1`, `string_val/1`.
""".
-spec c_string(Value :: string()) -> c_literal().
c_string(Value) ->
#c_literal{val = Value}.
-doc "_See also: _`c_string/1`.".
-spec ann_c_string(Annotations :: [term()], Value :: string()) -> c_literal().
ann_c_string(As, Value) ->
#c_literal{val = Value, anno = As}.
-doc """
Returns `true` if `Node` may represent a string literal, otherwise `false`.
Strings are defined as lists of characters; see [`is_c_char/1`](`is_c_char/1`)
for details.
_See also: _`c_string/1`, `is_c_char/1`, `is_print_string/1`.
""".
-spec is_c_string(Node :: cerl()) -> boolean().
is_c_string(#c_literal{val = V}) ->
is_char_list(V);
is_c_string(_) ->
false.
-doc """
Returns `true` if `Node` may represent a string literal containing only
"printing" characters, otherwise `false`.
See [`is_c_string/1`](`is_c_string/1`) and
[`is_print_char/1`](`is_print_char/1`) for details. Currently, only
ISO 8859-1 (Latin-1) character values are recognized.
_See also: _`c_string/1`, `is_c_string/1`, `is_print_char/1`.
""".
-spec is_print_string(Node :: cerl()) -> boolean().
is_print_string(#c_literal{val = V}) ->
is_print_char_list(V);
is_print_string(_) ->
false.