~ubuntu-branches/ubuntu/karmic/erlang/karmic-security

« back to all changes in this revision

Viewing changes to lib/compiler/src/cerl.erl

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
329
329
%%
330
330
%% @see abstract/1
331
331
 
 
332
-spec is_literal_term(_) -> bool().
 
333
 
332
334
is_literal_term(T) when is_integer(T) -> true;
333
335
is_literal_term(T) when is_float(T) -> true;
334
336
is_literal_term(T) when is_atom(T) -> true;
345
347
is_literal_term(_) ->
346
348
    false.
347
349
 
 
350
-spec is_literal_term_list([_]) -> bool().
 
351
 
348
352
is_literal_term_list([T | Ts]) ->
349
353
    case is_literal_term(T) of
350
354
        true ->
647
651
 
648
652
%% @spec c_int(Value::integer()) -> cerl()
649
653
%%
650
 
%%
651
654
%% @doc Creates an abstract integer literal. The lexical
652
655
%% representation is the canonical decimal numeral of
653
656
%% <code>Value</code>.
686
689
%% @doc Returns the value represented by an integer literal node.
687
690
%% @see c_int/1
688
691
 
 
692
-spec int_val(#literal{}) -> integer().
689
693
int_val(Node) ->
690
694
    Node#literal.val.
691
695
 
696
700
%% node.
697
701
%% @see c_int/1
698
702
 
 
703
-spec int_lit(#literal{}) -> string().
699
704
int_lit(Node) ->
700
705
    integer_to_list(int_val(Node)).
701
706
 
716
721
%% Note that not all floating-point numerals can be represented with
717
722
%% full precision.
718
723
 
 
724
-spec c_float(float()) -> #literal{}.
719
725
c_float(Value) ->
720
726
    #literal{val = Value}.
721
727
 
745
751
%% node.
746
752
%% @see c_float/1
747
753
 
 
754
-spec float_val(#literal{}) -> float().
748
755
float_val(Node) ->
749
756
    Node#literal.val.
750
757
 
755
762
%% literal node.
756
763
%% @see c_float/1
757
764
 
 
765
-spec float_lit(#literal{}) -> string().
758
766
float_lit(Node) ->
759
767
    float_to_list(float_val(Node)).
760
768
 
776
784
%% @see atom_name/1
777
785
%% @see atom_lit/1
778
786
 
 
787
-spec c_atom(atom() | string()) -> #literal{}.
779
788
c_atom(Name) when is_atom(Name) ->
780
789
    #literal{val = Name};
781
790
c_atom(Name) ->
804
813
is_c_atom(_) ->
805
814
    false.
806
815
 
807
 
%% @spec atom_val(cerl())-> atom()
 
816
%% @spec atom_val(cerl()) -> atom()
808
817
%%
809
818
%% @doc Returns the value represented by an abstract atom.
810
819
%%
811
820
%% @see c_atom/1
812
821
 
 
822
-spec atom_val(#literal{}) -> atom().
813
823
atom_val(Node) ->
814
824
    Node#literal.val.
815
825
 
820
830
%%
821
831
%% @see c_atom/1
822
832
 
 
833
-spec atom_name(#literal{}) -> string().
823
834
atom_name(Node) ->
824
835
    atom_to_list(atom_val(Node)).
825
836
 
866
877
%% @see char_lit/1
867
878
%% @see is_print_char/1
868
879
 
 
880
-spec c_char(non_neg_integer()) -> #literal{}.
869
881
c_char(Value) when is_integer(Value), Value >= 0 ->
870
882
    #literal{val = Value}.
871
883
 
920
932
%%
921
933
%% @see c_char/1
922
934
 
 
935
-spec char_val(#literal{}) -> char().
923
936
char_val(Node) ->
924
937
    Node#literal.val.
925
938
 
933
946
%%
934
947
%% @see c_char/1
935
948
 
 
949
-spec char_lit(#literal{}) -> string().
936
950
char_lit(Node) ->
937
951
    io_lib:write_char(char_val(Node)).
938
952
 
955
969
%% @see string_lit/1
956
970
%% @see is_print_string/1
957
971
 
 
972
-spec c_string(string()) -> #literal{}.
958
973
c_string(Value) ->
959
974
    #literal{val = Value}.
960
975
 
1006
1021
%%
1007
1022
%% @see c_string/1
1008
1023
 
 
1024
-spec string_val(#literal{}) -> string().
1009
1025
string_val(Node) ->
1010
1026
    Node#literal.val.
1011
1027
 
1020
1036
%%
1021
1037
%% @see c_string/1
1022
1038
 
 
1039
-spec string_lit(#literal{}) -> string().
1023
1040
string_lit(Node) ->
1024
1041
    io_lib:write_string(string_val(Node)).
1025
1042
 
1035
1052
%% @see is_c_list/1
1036
1053
%% @see c_cons/2
1037
1054
 
 
1055
-spec c_nil() -> #literal{}.
1038
1056
c_nil() ->
1039
1057
    #literal{val = []}.
1040
1058
 
1281
1299
%% @see is_c_list/1
1282
1300
%% @see list_elements/1
1283
1301
 
 
1302
-spec list_length(#cons{} | #literal{}) -> non_neg_integer().
1284
1303
list_length(L) ->
1285
1304
    list_length(L, 0).
1286
1305
 
 
1306
-spec list_length(#cons{} | #literal{}, non_neg_integer()) -> non_neg_integer().
1287
1307
list_length(#cons{tl = Tail}, A) ->
1288
1308
    list_length(Tail, A + 1);
1289
1309
list_length(#literal{val = V}, A) ->
1499
1519
%% @see tuple_es/1
1500
1520
%% @see c_tuple/1
1501
1521
 
 
1522
-spec tuple_arity(#tuple{} | #literal{}) -> non_neg_integer().
1502
1523
tuple_arity(#tuple{es = Es}) ->
1503
1524
    length(Es);
1504
1525
tuple_arity(#literal{val = V}) when is_tuple(V) ->
1546
1567
%% @see c_module/4
1547
1568
%% @see c_letrec/2
1548
1569
 
1549
 
-record(var, {ann = [], name}).
1550
 
 
 
1570
-type var_name() :: integer() | atom() | {atom(), integer()}.
 
1571
 
 
1572
-record(var, {ann = [], name :: var_name()}).
 
1573
 
 
1574
-spec c_var(var_name()) -> #var{}.
1551
1575
c_var(Name) ->
1552
1576
    #var{name = Name}.
1553
1577
 
1556
1580
%%
1557
1581
%% @see c_var/1
1558
1582
 
 
1583
-spec ann_c_var([_], var_name()) -> #var{}.
1559
1584
ann_c_var(As, Name) ->
1560
1585
    #var{name = Name, ann = As}.
1561
1586
 
1563
1588
%%
1564
1589
%% @see c_var/1
1565
1590
 
 
1591
-spec update_c_var(#var{}, var_name()) -> #var{}.
1566
1592
update_c_var(Node, Name) ->
1567
1593
    #var{name = Name, ann = get_ann(Node)}.
1568
1594
 
1588
1614
%% @see ann_c_fname/3
1589
1615
%% @see update_c_fname/3
1590
1616
 
 
1617
-spec c_fname(atom(), integer()) -> #var{}.
1591
1618
c_fname(Atom, Arity) ->
1592
1619
    c_var({Atom, Arity}).
1593
1620
 
1597
1624
%% @equiv ann_c_var(As, {Atom, Arity})
1598
1625
%% @see c_fname/2
1599
1626
 
 
1627
-spec ann_c_fname([_], atom(), integer()) -> #var{}.
1600
1628
ann_c_fname(As, Atom, Arity) ->
1601
1629
    ann_c_var(As, {Atom, Arity}).
1602
1630
 
1607
1635
%% @see update_c_fname/3
1608
1636
%% @see c_fname/2
1609
1637
 
 
1638
-spec update_c_fname(#var{}, atom()) -> #var{}.
1610
1639
update_c_fname(#var{name = {_, Arity}, ann = As}, Atom) ->
1611
1640
    #var{name = {Atom, Arity}, ann = As}.
1612
1641
 
1617
1646
%% @see update_c_fname/2
1618
1647
%% @see c_fname/2
1619
1648
 
 
1649
-spec update_c_fname(#var{}, atom(), integer()) -> #var{}.
1620
1650
update_c_fname(Node, Atom, Arity) ->
1621
1651
    update_c_var(Node, {Atom, Arity}).
1622
1652
 
1642
1672
%%
1643
1673
%% @see c_var/1
1644
1674
 
 
1675
-spec var_name(#var{}) -> var_name().
1645
1676
var_name(Node) ->
1646
1677
    Node#var.name.
1647
1678
 
1654
1685
%% @see fname_arity/1
1655
1686
%% @see c_fname/2
1656
1687
 
 
1688
-spec fname_id(#var{}) -> atom().
1657
1689
fname_id(#var{name={A,_}}) ->
1658
1690
    A.
1659
1691
 
1660
1692
 
1661
 
%% @spec fname_arity(cerl()) -> integer()
 
1693
%% @spec fname_arity(cerl()) -> byte()
1662
1694
%%
1663
1695
%% @doc Returns the arity part of an abstract function name variable.
1664
1696
%%
1665
1697
%% @see fname_id/1
1666
1698
%% @see c_fname/2
1667
1699
 
 
1700
-spec fname_arity(#var{}) -> byte().
1668
1701
fname_arity(#var{name={_,N}}) ->
1669
1702
    N.
1670
1703
 
1740
1773
%% @see c_values/1
1741
1774
%% @see values_es/1
1742
1775
 
 
1776
-spec values_arity(#values{}) -> non_neg_integer().
1743
1777
values_arity(Node) ->
1744
1778
    length(values_es(Node)).
1745
1779
 
1931
1965
%%
1932
1966
%% @see c_bitstr/5
1933
1967
 
 
1968
-spec bitstr_bitsize(#bitstr{}) -> 'all' | 'any' | integer().
1934
1969
bitstr_bitsize(Node) ->
1935
1970
    Size = Node#bitstr.size,
1936
1971
    case is_literal(Size) of
4041
4076
split_list(Node, L) ->
4042
4077
    A = get_ann(Node),
4043
4078
    case type(Node) of
4044
 
        cons when A == [] ->
 
4079
        cons when A =:= [] ->
4045
4080
            split_list(cons_tl(Node), [cons_hd(Node) | L]);
4046
 
        nil when A == [] ->
 
4081
        nil when A =:= [] ->
4047
4082
            {lists:reverse(L), none};
4048
4083
        _ ->
4049
4084
            {lists:reverse(L), Node}
4066
4101
lit_list_vals([]) ->
4067
4102
    [].
4068
4103
 
 
4104
-spec make_lit_list([_]) -> [#literal{}].  % XXX: cerl() instead of _ ?
4069
4105
make_lit_list([V | Vs]) ->
4070
4106
    [#literal{val = V} | make_lit_list(Vs)];
4071
4107
make_lit_list([]) ->