Merge branch 'master' of ssh://git.samba.org/data/git/samba into arc4
[kai/samba-autobuild/.git] / pidl / lib / Parse / Pidl / ODL.pm
1 ##########################################
2 # Converts ODL stuctures to IDL structures
3 # (C) 2004-2005, 2008 Jelmer Vernooij <jelmer@samba.org>
4
5 package Parse::Pidl::ODL;
6
7 use Parse::Pidl qw(error);
8 use Parse::Pidl::IDL;
9 use Parse::Pidl::Util qw(has_property unmake_str);
10 use Parse::Pidl::Typelist qw(hasType getType);
11 use strict;
12
13 use vars qw($VERSION);
14 $VERSION = '0.01';
15
16 sub FunctionAddObjArgs($)
17 {
18         my $e = shift;
19         
20         unshift(@{$e->{ELEMENTS}}, {
21                 'NAME' => 'ORPCthis',
22                 'POINTERS' => 0,
23                 'PROPERTIES' => { 'in' => '1' },
24                 'TYPE' => 'ORPCTHIS',
25                 'FILE' => $e->{FILE},
26                 'LINE' => $e->{LINE}
27         });
28         unshift(@{$e->{ELEMENTS}}, {
29                 'NAME' => 'ORPCthat',
30                 'POINTERS' => 1,
31                 'PROPERTIES' => { 'out' => '1', 'ref' => '1' },
32                 'TYPE' => 'ORPCTHAT',
33                 'FILE' => $e->{FILE},
34                 'LINE' => $e->{LINE}
35         });
36 }
37
38 sub ReplaceInterfacePointers($)
39 {
40         my ($e) = @_;
41         foreach my $x (@{$e->{ELEMENTS}}) {
42                 next unless (hasType($x->{TYPE}));
43                 next unless getType($x->{TYPE})->{DATA}->{TYPE} eq "INTERFACE";
44                 
45                 $x->{TYPE} = "MInterfacePointer";
46         }
47 }
48
49 # Add ORPC specific bits to an interface.
50 sub ODL2IDL
51 {
52         my ($odl, $basedir, $opt_incdirs) = (@_);
53         my $addedorpc = 0;
54         my $interfaces = {};
55
56         foreach my $x (@$odl) {
57                 if ($x->{TYPE} eq "IMPORT") {
58                         foreach my $idl_file (@{$x->{PATHS}}) {
59                                 $idl_file = unmake_str($idl_file);
60                                 unless (-f "$basedir/$idl_file") {
61                                         error($x, "Unable to open include file `$idl_file'");
62                                         next;
63                                 }
64                                 my $podl = Parse::Pidl::IDL::parse_file("$basedir/$idl_file", $opt_incdirs);
65                                 if (defined(@$podl)) {
66                                         require Parse::Pidl::Typelist;
67
68                                         Parse::Pidl::Typelist::LoadIdl($podl);
69                                         my $pidl = ODL2IDL($podl, $basedir, $opt_incdirs);
70
71                                         foreach my $y (@$pidl) {
72                                                 if ($y->{TYPE} eq "INTERFACE") {
73                                                         $interfaces->{$y->{NAME}} = $y;
74                                                 }
75                                         }
76                                 } else {
77                                         error($x, "Failed to parse $idl_file");
78                                 }
79                         }
80                 }
81
82                 if ($x->{TYPE} eq "INTERFACE") {
83                         $interfaces->{$x->{NAME}} = $x;
84                         # Add [in] ORPCTHIS *this, [out] ORPCTHAT *that
85                         # and replace interfacepointers with MInterfacePointer
86                         # for 'object' interfaces
87                         if (has_property($x, "object")) {
88                                 foreach my $e (@{$x->{DATA}}) {
89                                         ($e->{TYPE} eq "FUNCTION") && FunctionAddObjArgs($e);
90                                         ReplaceInterfacePointers($e);
91                                 }
92                                 $addedorpc = 1;
93                         }
94
95                         if ($x->{BASE}) {
96                                 my $base = $interfaces->{$x->{BASE}};
97
98                                 unless (defined($base)) {
99                                         error($x, "Undefined base interface `$x->{BASE}'");
100                                 } else {
101                                         foreach my $fn (reverse @{$base->{DATA}}) {
102                                                 next unless ($fn->{TYPE} eq "FUNCTION");
103                                                 push (@{$x->{INHERITED_FUNCTIONS}}, $fn);
104                                         }
105                                 }
106                         }
107                 }
108         }
109
110         unshift (@$odl, {
111                 TYPE => "IMPORT", 
112                 PATHS => [ "\"orpc.idl\"" ],
113                 FILE => undef,
114                 LINE => undef
115         }) if ($addedorpc);
116
117
118         return $odl;
119 }
120
121 1;