r5475: Move some existing and new type information functions to typelist.pm
[samba.git] / source4 / build / pidl / typelist.pm
1 ###################################################
2 # Samba4 parser generator for IDL structures
3 # Copyright jelmer@samba.org 2005
4 # released under the GNU GPL
5
6 package typelist;
7
8 use strict;
9
10 my %typedefs = ();
11
12 sub addType($)
13 {
14         my $t = shift;
15         $typedefs{$t->{NAME}} = $t;
16 }
17
18 sub getType($)
19 {
20         my $t = shift;
21         return undef unless(defined($typedefs{$t}));
22         return $typedefs{$t};
23 }
24
25 sub hasType($)
26 {
27         my $t = shift;
28         return 1 if defined($typedefs{$t});
29         return 0;
30 }
31
32 sub RegisterPrimitives()
33 {
34         my @primitives = (
35                 "char", "int8", "uint8", "short", "wchar_t", 
36                 "int16", "uint16", "long", "int32", "uint32", 
37                 "dlong", "udlong", "udlongr", "NTTIME", "NTTIME_1sec", 
38                 "time_t", "DATA_BLOB", "error_status_t", "WERROR", 
39                 "NTSTATUS", "boolean32", "unsigned32", "ipv4address", 
40                 "hyper", "NTTIME_hyper");
41                 
42         foreach my $k (@primitives) {
43                 $typedefs{$k} = {
44                         NAME => $k,
45                         TYPE => "TYPEDEF",
46                         DATA => {
47                                 TYPE => "SCALAR",
48                                 NAME => $k
49                         }
50                 };
51         }
52 }
53
54 sub enum_type_fn($)
55 {
56         my $enum = shift;
57         if (util::has_property($enum->{PARENT}, "enum8bit")) {
58                 return "uint8";
59         } elsif (util::has_property($enum->{PARENT}, "v1_enum")) {
60                 return "uint32";
61         }
62         return "uint16";
63 }
64
65 sub bitmap_type_fn($)
66 {
67         my $bitmap = shift;
68
69         if (util::has_property($bitmap, "bitmap8bit")) {
70                 return "uint8";
71         } elsif (util::has_property($bitmap, "bitmap16bit")) {
72                 return "uint16";
73         } elsif (util::has_property($bitmap, "bitmap64bit")) {
74                 return "uint64";
75         }
76         return "uint32";
77 }
78
79 # provide mappings between IDL base types and types in our headers
80 my %scalar_type_mappings = 
81     (
82      "int8"         => "int8_t",
83      "uint8"        => "uint8_t",
84      "short"        => "int16_t",
85      "wchar_t"      => "uint16_t",
86      "int16"        => "int16_t",
87      "uint16"       => "uint16_t",
88      "int32"        => "int32_t",
89      "uint32"       => "uint32_t",
90      "int64"        => "int64_t",
91      "uint64"       => "uint64_t",
92      "dlong"        => "int64_t",
93      "udlong"       => "uint64_t",
94      "udlongr"      => "uint64_t",
95      "hyper"        => "uint64_t",
96      "NTTIME_1sec"  => "NTTIME",
97      "NTTIME_hyper" => "NTTIME",
98      "ipv4address"  => "const char *"
99      );
100
101 # map from a IDL type to a C header type
102 sub mapScalarType($)
103 {
104         my $name = shift;
105         if (my $ret = $scalar_type_mappings{$name}) {
106                 return $ret;
107         }
108         return $name;
109 }
110
111 sub mapType($)
112 {
113         my $e = shift;
114         my $dt;
115
116         return "const char *" if ($e->{TYPE} =~ "string");
117
118         if ($e->{TYPE} eq "ENUM" or $e->{TYPE} eq "BITMAP") {
119                 $dt = getType($e->{PARENT}->{NAME});
120         }
121         
122         unless ($dt or $dt = getType($e->{TYPE})) {
123                 # Best guess
124                 return "struct $e->{TYPE}";
125         }
126         return mapScalarType($e->{TYPE}) if ($dt->{DATA}->{TYPE} eq "SCALAR");
127         return "enum $dt->{NAME}" if ($dt->{DATA}->{TYPE} eq "ENUM");
128         return "struct $dt->{NAME}" if ($dt->{DATA}->{TYPE} eq "STRUCT");
129         return "union $dt->{NAME}" if ($dt->{DATA}->{TYPE} eq "UNION");
130         return mapScalarType(bitmap_type_fn($dt->{DATA})) if ($dt->{DATA}->{TYPE} eq "BITMAP");
131
132         die("Unknown type $dt->{DATA}->{TYPE}");
133 }
134
135 RegisterPrimitives();
136
137
138 1;