1 ###################################################
2 # Samba4 NDR info tree generator
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001
5 # Copyright jelmer@samba.org 2004-2006
6 # released under the GNU GPL
12 Parse::Pidl::NDR - NDR parsing information generator
16 Return a table describing the order in which the parts of an element
27 Jelmer Vernooij <jelmer@samba.org>
31 package Parse
::Pidl
::NDR
;
34 use vars
qw($VERSION);
37 @EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe ContainsString);
38 @EXPORT_OK = qw(GetElementLevelTable ParseElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
41 use Parse
::Pidl
qw(warning fatal);
42 use Parse
::Pidl
::Typelist
qw(hasType getType typeIs expandAlias mapScalarType is_fixed_size_scalar);
43 use Parse
::Pidl
::Util
qw(has_property property_matches);
45 # Alignment of the built-in scalar types
46 my $scalar_alignment = {
67 'string_array' => 4, #???
79 'wrepl_nbt_name' => 4,
81 'ipv6address' => 4, #16?
86 sub GetElementLevelTable
($$$)
88 my ($e, $pointer_default, $ms_union) = @_;
92 my @bracket_array = ();
97 if (has_property
($e, "size_is")) {
98 @size_is = split /,/, has_property
($e, "size_is");
101 if (has_property
($e, "length_is")) {
102 @length_is = split /,/, has_property
($e, "length_is");
105 if (defined($e->{ARRAY_LEN
})) {
106 @bracket_array = @
{$e->{ARRAY_LEN
}};
109 if (has_property
($e, "out")) {
112 if (has_property
($e, "string") and not has_property
($e, "in")) { $needptrs++; }
113 if ($#bracket_array >= 0) { $needptrs = 0; }
115 warning
($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS
});
118 my $allow_pipe = ($e->{PARENT
}->{TYPE
} eq "FUNCTION");
119 my $is_pipe = typeIs
($e->{TYPE
}, "PIPE");
122 if (not $allow_pipe) {
123 fatal
($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}");
126 if ($e->{POINTERS
} > 1) {
127 fatal
($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}");
130 if ($e->{POINTERS
} < 0) {
131 fatal
($e, "pipe element $e->{NAME} needs pointer");
134 if ($e->{POINTERS
} == 1 and pointer_type
($e) ne "ref") {
135 fatal
($e, "pointer should be 'ref' on pipe element $e->{NAME}");
138 if (scalar(@size_is) > 0) {
139 fatal
($e, "size_is() on pipe element");
142 if (scalar(@length_is) > 0) {
143 fatal
($e, "length_is() on pipe element");
146 if (scalar(@bracket_array) > 0) {
147 fatal
($e, "brackets on pipe element");
150 if (defined(has_property
($e, "subcontext"))) {
151 fatal
($e, "subcontext on pipe element");
154 if (has_property
($e, "switch_is")) {
155 fatal
($e, "switch_is on pipe element");
158 if (can_contain_deferred
($e->{TYPE
})) {
159 fatal
($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element");
163 # Parse the [][][][] style array stuff
164 for my $i (0 .. $#bracket_array) {
165 my $d = $bracket_array[$#bracket_array - $i];
168 my $is_surrounding = 0;
170 my $is_conformant = 0;
177 if ($size = shift @size_is) {
178 if ($e->{POINTERS
} < 1 and has_property
($e, "string")) {
180 delete($e->{PROPERTIES
}->{string
});
182 } elsif ((scalar(@size_is) == 0) and has_property
($e, "string")) {
184 delete($e->{PROPERTIES
}->{string
});
186 fatal
($e, "Must specify size_is() for conformant array!")
189 if (($length = shift @length_is) or $is_string) {
195 if ($e == $e->{PARENT
}->{ELEMENTS
}[-1]
196 and $e->{PARENT
}->{TYPE
} ne "FUNCTION") {
201 $is_fixed = 1 if (not $is_conformant and Parse
::Pidl
::Util
::is_constant
($size));
202 $is_inline = 1 if (not $is_conformant and not Parse
::Pidl
::Util
::is_constant
($size));
204 if ($i == 0 and $is_fixed and has_property
($e, "string")) {
208 delete($e->{PROPERTIES
}->{string
});
214 LENGTH_IS
=> $length,
215 IS_DEFERRED
=> $is_deferred,
216 IS_SURROUNDING
=> $is_surrounding,
217 IS_ZERO_TERMINATED
=> $is_string,
218 IS_VARYING
=> $is_varying,
219 IS_CONFORMANT
=> $is_conformant,
220 IS_FIXED
=> $is_fixed,
221 IS_INLINE
=> $is_inline
225 # Next, all the pointers
226 foreach my $i (1..$e->{POINTERS
}) {
227 my $level = "EMBEDDED";
228 # Top level "ref" pointers do not have a referrent identifier
229 $level = "TOP" if ($i == 1 and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
233 # Only the first level gets the pointer type from the
234 # pointer property, the others get them from
235 # the pointer_default() interface property
237 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
238 # (Here they talk about the rightmost pointer, but testing shows
239 # they mean the leftmost pointer.)
243 $pt = pointer_type
($e);
245 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
246 $pt = $pointer_default;
252 POINTER_INDEX
=> $pointer_idx,
253 IS_DEFERRED
=> "$is_deferred",
257 warning
($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
258 if ($i == 1 and $pt ne "ref" and
259 $e->{PARENT
}->{TYPE
} eq "FUNCTION" and
260 not has_property
($e, "in"));
264 # everything that follows will be deferred
265 $is_deferred = 1 if ($level ne "TOP");
267 my $array_size = shift @size_is;
274 if ($array_length = shift @length_is) {
277 $array_length = $array_size;
282 if (scalar(@size_is) == 0 and has_property
($e, "string") and
283 $i == $e->{POINTERS
}) {
285 $is_varying = $is_conformant = has_property
($e, "noheader")?
0:1;
286 delete($e->{PROPERTIES
}->{string
});
289 if ($array_size or $is_string) {
292 SIZE_IS
=> $array_size,
293 LENGTH_IS
=> $array_length,
294 IS_DEFERRED
=> $is_deferred,
296 IS_ZERO_TERMINATED
=> $is_string,
297 IS_VARYING
=> $is_varying,
298 IS_CONFORMANT
=> $is_conformant,
311 CONTAINS_DEFERRED
=> 0,
315 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
320 if (defined(has_property
($e, "subcontext"))) {
321 my $hdr_size = has_property
($e, "subcontext");
322 my $subsize = has_property
($e, "subcontext_size");
323 if (not defined($subsize)) {
328 TYPE
=> "SUBCONTEXT",
329 HEADER_SIZE
=> $hdr_size,
330 SUBCONTEXT_SIZE
=> $subsize,
331 IS_DEFERRED
=> $is_deferred,
332 COMPRESSION
=> has_property
($e, "compression"),
336 if (my $switch = has_property
($e, "switch_is")) {
339 SWITCH_IS
=> $switch,
340 IS_DEFERRED
=> $is_deferred
344 if (scalar(@size_is) > 0) {
345 fatal
($e, "size_is() on non-array element");
348 if (scalar(@length_is) > 0) {
349 fatal
($e, "length_is() on non-array element");
352 if (has_property
($e, "string")) {
353 fatal
($e, "string() attribute on non-array element");
358 DATA_TYPE
=> $e->{TYPE
},
359 IS_DEFERRED
=> $is_deferred,
360 CONTAINS_DEFERRED
=> can_contain_deferred
($e->{TYPE
}),
361 IS_SURROUNDING
=> 0 #FIXME
365 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
370 sub GetTypedefLevelTable
($$$$)
372 my ($e, $data, $pointer_default, $ms_union) = @_;
381 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
386 #####################################################################
387 # see if a type contains any deferred data
388 sub can_contain_deferred
($)
390 sub can_contain_deferred
($);
393 return 1 unless (hasType
($type)); # assume the worst
395 $type = getType
($type);
397 return 0 if (Parse
::Pidl
::Typelist
::is_scalar
($type));
399 return can_contain_deferred
($type->{DATA
}) if ($type->{TYPE
} eq "TYPEDEF");
401 return 0 unless defined($type->{ELEMENTS
});
403 foreach (@
{$type->{ELEMENTS
}}) {
404 return 1 if ($_->{POINTERS
});
405 return 1 if (can_contain_deferred
($_->{TYPE
}));
415 return undef unless $e->{POINTERS
};
417 return "ref" if (has_property
($e, "ref"));
418 return "full" if (has_property
($e, "ptr"));
419 return "sptr" if (has_property
($e, "sptr"));
420 return "unique" if (has_property
($e, "unique"));
421 return "relative" if (has_property
($e, "relative"));
422 return "relative_short" if (has_property
($e, "relative_short"));
423 return "ignore" if (has_property
($e, "ignore"));
428 #####################################################################
429 # work out the correct alignment for a structure or union
430 sub find_largest_alignment
($)
435 for my $e (@
{$s->{ELEMENTS
}}) {
438 if ($e->{POINTERS
}) {
439 # this is a hack for NDR64
440 # the NDR layer translates this into
441 # an alignment of 4 for NDR and 8 for NDR64
443 } elsif (has_property
($e, "subcontext")) {
445 } elsif (has_property
($e, "transmit_as")) {
446 $a = align_type
($e->{PROPERTIES
}->{transmit_as
});
448 $a = align_type
($e->{TYPE
});
451 $align = $a if ($align < $a);
457 #####################################################################
464 if (ref($e) eq "HASH" and $e->{TYPE
} eq "SCALAR") {
465 return $scalar_alignment->{$e->{NAME
}};
468 return 0 if ($e eq "EMPTY");
470 unless (hasType
($e)) {
471 # it must be an external type - all we can do is guess
472 # warning($e, "assuming alignment of unknown type '$e' is 4");
476 my $dt = getType
($e);
478 if ($dt->{TYPE
} eq "TYPEDEF") {
479 return align_type
($dt->{DATA
});
480 } elsif ($dt->{TYPE
} eq "CONFORMANCE") {
481 return $dt->{DATA
}->{ALIGN
};
482 } elsif ($dt->{TYPE
} eq "ENUM") {
483 return align_type
(Parse
::Pidl
::Typelist
::enum_type_fn
($dt));
484 } elsif ($dt->{TYPE
} eq "BITMAP") {
485 return align_type
(Parse
::Pidl
::Typelist
::bitmap_type_fn
($dt));
486 } elsif (($dt->{TYPE
} eq "STRUCT") or ($dt->{TYPE
} eq "UNION")) {
487 # Struct/union without body: assume 4
488 return 4 unless (defined($dt->{ELEMENTS
}));
489 return find_largest_alignment
($dt);
490 } elsif (($dt->{TYPE
} eq "PIPE")) {
494 die("Unknown data type type $dt->{TYPE}");
497 sub ParseElement
($$$)
499 my ($e, $pointer_default, $ms_union) = @_;
501 $e->{TYPE
} = expandAlias
($e->{TYPE
});
503 if (ref($e->{TYPE
}) eq "HASH") {
504 $e->{TYPE
} = ParseType
($e->{TYPE
}, $pointer_default, $ms_union);
510 PROPERTIES
=> $e->{PROPERTIES
},
511 LEVELS
=> GetElementLevelTable
($e, $pointer_default, $ms_union),
512 REPRESENTATION_TYPE
=> ($e->{PROPERTIES
}->{represent_as
} or $e->{TYPE
}),
513 ALIGN
=> align_type
($e->{TYPE
}),
520 my ($struct, $pointer_default, $ms_union) = @_;
522 my $surrounding = undef;
526 NAME
=> $struct->{NAME
},
527 SURROUNDING_ELEMENT
=> undef,
529 PROPERTIES
=> $struct->{PROPERTIES
},
532 } unless defined($struct->{ELEMENTS
});
534 CheckPointerTypes
($struct, $pointer_default);
536 foreach my $x (@
{$struct->{ELEMENTS
}})
538 my $e = ParseElement
($x, $pointer_default, $ms_union);
539 if ($x != $struct->{ELEMENTS
}[-1] and
540 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
541 fatal
($x, "conformant member not at end of struct");
546 my $e = $elements[-1];
547 if (defined($e) and defined($e->{LEVELS
}[0]->{IS_SURROUNDING
}) and
548 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
552 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
553 && property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
554 $surrounding = $struct->{ELEMENTS
}[-1];
558 if ($struct->{NAME
}) {
559 $align = align_type
($struct->{NAME
});
564 NAME
=> $struct->{NAME
},
565 SURROUNDING_ELEMENT
=> $surrounding,
566 ELEMENTS
=> \
@elements,
567 PROPERTIES
=> $struct->{PROPERTIES
},
575 my ($e, $pointer_default, $ms_union) = @_;
577 my $is_ms_union = $ms_union;
578 $is_ms_union = 1 if has_property
($e, "ms_union");
580 my $switch_type = has_property
($e, "switch_type");
581 unless (defined($switch_type)) { $switch_type = "uint32"; }
582 if (has_property
($e, "nodiscriminant")) { $switch_type = undef; }
587 SWITCH_TYPE
=> $switch_type,
589 PROPERTIES
=> $e->{PROPERTIES
},
590 HAS_DEFAULT
=> $hasdefault,
591 IS_MS_UNION
=> $is_ms_union,
594 } unless defined($e->{ELEMENTS
});
596 CheckPointerTypes
($e, $pointer_default);
598 foreach my $x (@
{$e->{ELEMENTS
}})
601 if ($x->{TYPE
} eq "EMPTY") {
602 $t = { TYPE
=> "EMPTY" };
604 $t = ParseElement
($x, $pointer_default, $ms_union);
606 if (has_property
($x, "default")) {
607 $t->{CASE
} = "default";
609 } elsif (defined($x->{PROPERTIES
}->{case
})) {
610 $t->{CASE
} = "case $x->{PROPERTIES}->{case}";
612 die("Union element $x->{NAME} has neither default nor case property");
619 $align = align_type
($e->{NAME
});
625 SWITCH_TYPE
=> $switch_type,
626 ELEMENTS
=> \
@elements,
627 PROPERTIES
=> $e->{PROPERTIES
},
628 HAS_DEFAULT
=> $hasdefault,
629 IS_MS_UNION
=> $is_ms_union,
637 my ($e, $pointer_default, $ms_union) = @_;
642 BASE_TYPE
=> Parse
::Pidl
::Typelist
::enum_type_fn
($e),
643 ELEMENTS
=> $e->{ELEMENTS
},
644 PROPERTIES
=> $e->{PROPERTIES
},
651 my ($e, $pointer_default, $ms_union) = @_;
656 BASE_TYPE
=> Parse
::Pidl
::Typelist
::bitmap_type_fn
($e),
657 ELEMENTS
=> $e->{ELEMENTS
},
658 PROPERTIES
=> $e->{PROPERTIES
},
665 my ($pipe, $pointer_default, $ms_union) = @_;
667 my $pname = $pipe->{NAME
};
668 $pname = $pipe->{PARENT
}->{NAME
} unless defined $pname;
670 if (not defined($pipe->{PROPERTIES
})
671 and defined($pipe->{PARENT
}->{PROPERTIES
})) {
672 $pipe->{PROPERTIES
} = $pipe->{PARENT
}->{PROPERTIES
};
675 if (ref($pipe->{DATA
}) eq "HASH") {
676 if (not defined($pipe->{DATA
}->{PROPERTIES
})
677 and defined($pipe->{PROPERTIES
})) {
678 $pipe->{DATA
}->{PROPERTIES
} = $pipe->{PROPERTIES
};
682 my $struct = ParseStruct
($pipe->{DATA
}, $pointer_default, $ms_union);
683 $struct->{ALIGN
} = 5;
684 $struct->{NAME
} = "$pname\_chunk";
686 # 'count' is element [0] and 'array' [1]
687 my $e = $struct->{ELEMENTS
}[1];
688 # level [0] is of type "ARRAY"
689 my $l = $e->{LEVELS
}[1];
691 # here we check that pipe elements have a fixed size type
692 while (defined($l)) {
694 $l = GetNextLevel
($e, $cl);
695 if ($cl->{TYPE
} ne "DATA") {
696 fatal
($pipe, el_name
($pipe) . ": pipe contains non DATA level");
699 # for now we only support scalars
700 next if is_fixed_size_scalar
($cl->{DATA_TYPE
});
702 fatal
($pipe, el_name
($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]");
707 NAME
=> $pipe->{NAME
},
709 PROPERTIES
=> $pipe->{PROPERTIES
},
716 my ($d, $pointer_default, $ms_union) = @_;
719 STRUCT
=> \
&ParseStruct
,
720 UNION
=> \
&ParseUnion
,
722 BITMAP
=> \
&ParseBitmap
,
723 TYPEDEF
=> \
&ParseTypedef
,
725 }->{$d->{TYPE
}}->($d, $pointer_default, $ms_union);
732 my ($d, $pointer_default, $ms_union) = @_;
736 if (ref($d->{DATA
}) eq "HASH") {
737 if (defined($d->{DATA
}->{PROPERTIES
})
738 and not defined($d->{PROPERTIES
})) {
739 $d->{PROPERTIES
} = $d->{DATA
}->{PROPERTIES
};
742 $data = ParseType
($d->{DATA
}, $pointer_default, $ms_union);
743 $data->{ALIGN
} = align_type
($d->{NAME
});
745 $data = getType
($d->{DATA
});
751 PROPERTIES
=> $d->{PROPERTIES
},
752 LEVELS
=> GetTypedefLevelTable
($d, $data, $pointer_default, $ms_union),
765 sub ParseFunction
($$$$)
767 my ($ndr,$d,$opnum,$ms_union) = @_;
770 my $thisopnum = undef;
772 CheckPointerTypes
($d, "ref");
774 if (not defined($d->{PROPERTIES
}{noopnum
})) {
775 $thisopnum = ${$opnum};
779 foreach my $x (@
{$d->{ELEMENTS
}}) {
780 my $e = ParseElement
($x, $ndr->{PROPERTIES
}->{pointer_default
}, $ms_union);
781 push (@
{$e->{DIRECTION
}}, "in") if (has_property
($x, "in"));
782 push (@
{$e->{DIRECTION
}}, "out") if (has_property
($x, "out"));
784 push (@elements, $e);
787 if ($d->{RETURN_TYPE
} ne "void") {
788 $rettype = expandAlias
($d->{RETURN_TYPE
});
795 RETURN_TYPE
=> $rettype,
796 PROPERTIES
=> $d->{PROPERTIES
},
797 ELEMENTS
=> \
@elements,
802 sub CheckPointerTypes
($$)
804 my ($s,$default) = @_;
806 return unless defined($s->{ELEMENTS
});
808 foreach my $e (@
{$s->{ELEMENTS
}}) {
809 if ($e->{POINTERS
} and not defined(pointer_type
($e))) {
810 $e->{PROPERTIES
}->{$default} = '1';
815 sub FindNestedTypes
($$)
817 sub FindNestedTypes
($$);
820 return unless defined($t->{ELEMENTS
});
821 return if ($t->{TYPE
} eq "ENUM");
822 return if ($t->{TYPE
} eq "BITMAP");
824 foreach (@
{$t->{ELEMENTS
}}) {
825 if (ref($_->{TYPE
}) eq "HASH") {
826 push (@
$l, $_->{TYPE
}) if (defined($_->{TYPE
}->{NAME
}));
827 FindNestedTypes
($l, $_->{TYPE
});
832 sub ParseInterface
($)
842 $ms_union = 1 if has_property
($idl, "ms_union");
844 if (not has_property
($idl, "pointer_default")) {
845 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
846 # and "unique" in Microsoft Extensions mode (default)
847 $idl->{PROPERTIES
}->{pointer_default
} = "unique";
850 foreach my $d (@
{$idl->{DATA
}}) {
851 if ($d->{TYPE
} eq "FUNCTION") {
852 push (@functions, ParseFunction
($idl, $d, \
$opnum, $ms_union));
853 } elsif ($d->{TYPE
} eq "CONST") {
854 push (@consts, ParseConst
($idl, $d));
856 push (@types, ParseType
($d, $idl->{PROPERTIES
}->{pointer_default
}, $ms_union));
857 FindNestedTypes
(\
@types, $d);
863 if(defined $idl->{PROPERTIES
}->{version
}) {
864 my @if_version = split(/\./, $idl->{PROPERTIES
}->{version
});
865 if ($if_version[0] == $idl->{PROPERTIES
}->{version
}) {
866 $version = $idl->{PROPERTIES
}->{version
};
868 $version = $if_version[1] << 16 | $if_version[0];
872 # If no endpoint is set, default to the interface name as a named pipe
873 if (!defined $idl->{PROPERTIES
}->{endpoint
}) {
874 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME
} . "]\"";
876 @endpoints = split /,/, $idl->{PROPERTIES
}->{endpoint
};
880 NAME
=> $idl->{NAME
},
881 UUID
=> lc(has_property
($idl, "uuid")),
884 PROPERTIES
=> $idl->{PROPERTIES
},
885 FUNCTIONS
=> \
@functions,
888 ENDPOINTS
=> \
@endpoints
892 # Convert a IDL tree to a NDR tree
893 # Gives a result tree describing all that's necessary for easily generating
894 # NDR parsers / generators
899 return undef unless (defined($idl));
901 Parse
::Pidl
::NDR
::Validate
($idl);
906 ($_->{TYPE
} eq "CPP_QUOTE") && push(@ndr, $_);
907 ($_->{TYPE
} eq "INTERFACE") && push(@ndr, ParseInterface
($_));
908 ($_->{TYPE
} eq "IMPORT") && push(@ndr, $_);
921 foreach my $l (@
{$e->{LEVELS
}}) {
922 return $l if ($seen);
923 ($seen = 1) if ($l == $fl);
934 foreach my $l (@
{$e->{LEVELS
}}) {
935 (return $prev) if ($l == $fl);
942 sub ContainsString
($)
946 if (property_matches
($e, "flag", ".*STR_NULLTERM.*")) {
949 foreach my $l (@
{$e->{LEVELS
}}) {
950 return 1 if ($l->{TYPE
} eq "ARRAY" and $l->{IS_ZERO_TERMINATED
});
956 sub ContainsDeferred
($$)
960 return 1 if ($l->{CONTAINS_DEFERRED
});
962 while ($l = GetNextLevel
($e,$l))
964 return 1 if ($l->{IS_DEFERRED
});
965 return 1 if ($l->{CONTAINS_DEFERRED
});
975 return 1 if ($l->{TYPE
} eq "PIPE");
977 while ($l = GetNextLevel
($e,$l))
979 return 1 if ($l->{TYPE
} eq "PIPE");
988 my $name = "<ANONYMOUS>";
990 $name = $e->{NAME
} if defined($e->{NAME
});
992 if (defined($e->{PARENT
}) and defined($e->{PARENT
}->{NAME
})) {
993 return "$e->{PARENT}->{NAME}.$name";
996 if (defined($e->{PARENT
}) and
997 defined($e->{PARENT
}->{PARENT
}) and
998 defined($e->{PARENT
}->{PARENT
}->{NAME
})) {
999 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
1005 ###################################
1006 # find a sibling var in a structure
1007 sub find_sibling
($$)
1010 my($fn) = $e->{PARENT
};
1012 if ($name =~ /\*(.*)/) {
1016 for my $e2 (@
{$fn->{ELEMENTS
}}) {
1017 return $e2 if ($e2->{NAME
} eq $name);
1023 my %property_list = (
1025 "helpstring" => ["INTERFACE", "FUNCTION"],
1026 "version" => ["INTERFACE"],
1027 "uuid" => ["INTERFACE"],
1028 "endpoint" => ["INTERFACE"],
1029 "pointer_default" => ["INTERFACE"],
1030 "helper" => ["INTERFACE"],
1031 "pyhelper" => ["INTERFACE"],
1032 "authservice" => ["INTERFACE"],
1033 "restricted" => ["INTERFACE"],
1034 "no_srv_register" => ["INTERFACE"],
1037 "object" => ["INTERFACE"],
1038 "local" => ["INTERFACE", "FUNCTION"],
1039 "iid_is" => ["ELEMENT"],
1040 "call_as" => ["FUNCTION"],
1041 "idempotent" => ["FUNCTION"],
1044 "noopnum" => ["FUNCTION"],
1045 "in" => ["ELEMENT"],
1046 "out" => ["ELEMENT"],
1049 "ref" => ["ELEMENT", "TYPEDEF"],
1050 "ptr" => ["ELEMENT", "TYPEDEF"],
1051 "unique" => ["ELEMENT", "TYPEDEF"],
1052 "ignore" => ["ELEMENT"],
1053 "relative" => ["ELEMENT", "TYPEDEF"],
1054 "relative_short" => ["ELEMENT", "TYPEDEF"],
1055 "null_is_ffffffff" => ["ELEMENT"],
1056 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
1058 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
1059 "value" => ["ELEMENT"],
1060 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1063 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1064 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1065 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1066 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1067 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"],
1068 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1069 "todo" => ["FUNCTION"],
1072 "switch_is" => ["ELEMENT"],
1073 "switch_type" => ["ELEMENT", "UNION"],
1074 "nodiscriminant" => ["UNION"],
1075 "ms_union" => ["INTERFACE", "UNION"],
1076 "case" => ["ELEMENT"],
1077 "default" => ["ELEMENT"],
1079 "represent_as" => ["ELEMENT"],
1080 "transmit_as" => ["ELEMENT"],
1083 "subcontext" => ["ELEMENT"],
1084 "subcontext_size" => ["ELEMENT"],
1085 "compression" => ["ELEMENT"],
1088 "enum8bit" => ["ENUM"],
1089 "enum16bit" => ["ENUM"],
1090 "v1_enum" => ["ENUM"],
1093 "bitmap8bit" => ["BITMAP"],
1094 "bitmap16bit" => ["BITMAP"],
1095 "bitmap32bit" => ["BITMAP"],
1096 "bitmap64bit" => ["BITMAP"],
1099 "range" => ["ELEMENT", "PIPE"],
1100 "size_is" => ["ELEMENT"],
1101 "string" => ["ELEMENT"],
1102 "noheader" => ["ELEMENT"],
1103 "charset" => ["ELEMENT"],
1104 "length_is" => ["ELEMENT"],
1107 #####################################################################
1108 # check for unknown properties
1109 sub ValidProperties
($$)
1113 return unless defined $e->{PROPERTIES
};
1115 foreach my $key (keys %{$e->{PROPERTIES
}}) {
1116 warning
($e, el_name
($e) . ": unknown property '$key'")
1117 unless defined($property_list{$key});
1119 fatal
($e, el_name
($e) . ": property '$key' not allowed on '$t'")
1120 unless grep(/^$t$/, @
{$property_list{$key}});
1128 return $t->{NAME
} if (ref($t) eq "HASH" and $t->{TYPE
} eq "SCALAR");
1129 my $ti = getType
($t);
1131 if (not defined ($ti)) {
1133 } elsif ($ti->{TYPE
} eq "TYPEDEF") {
1134 return mapToScalar
($ti->{DATA
});
1135 } elsif ($ti->{TYPE
} eq "ENUM") {
1136 return Parse
::Pidl
::Typelist
::enum_type_fn
($ti);
1137 } elsif ($ti->{TYPE
} eq "BITMAP") {
1138 return Parse
::Pidl
::Typelist
::bitmap_type_fn
($ti);
1144 #####################################################################
1145 # validate an element
1150 ValidProperties
($e,"ELEMENT");
1152 # Check whether switches are used correctly.
1153 if (my $switch = has_property
($e, "switch_is")) {
1154 my $e2 = find_sibling
($e, $switch);
1155 my $type = getType
($e->{TYPE
});
1157 if (defined($type) and $type->{DATA
}->{TYPE
} ne "UNION") {
1158 fatal
($e, el_name
($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1161 if (not has_property
($type->{DATA
}, "nodiscriminant") and defined($e2)) {
1162 my $discriminator_type = has_property
($type->{DATA
}, "switch_type");
1163 $discriminator_type = "uint32" unless defined ($discriminator_type);
1165 my $t1 = mapScalarType
(mapToScalar
($discriminator_type));
1167 if (not defined($t1)) {
1168 fatal
($e, el_name
($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1171 my $t2 = mapScalarType
(mapToScalar
($e2->{TYPE
}));
1172 if (not defined($t2)) {
1173 fatal
($e, el_name
($e) . ": unable to map variable used for switch_is() to scalar");
1177 warning
($e, el_name
($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1182 if (has_property
($e, "subcontext") and has_property
($e, "represent_as")) {
1183 fatal
($e, el_name
($e) . " : subcontext() and represent_as() can not be used on the same element");
1186 if (has_property
($e, "subcontext") and has_property
($e, "transmit_as")) {
1187 fatal
($e, el_name
($e) . " : subcontext() and transmit_as() can not be used on the same element");
1190 if (has_property
($e, "represent_as") and has_property
($e, "transmit_as")) {
1191 fatal
($e, el_name
($e) . " : represent_as() and transmit_as() can not be used on the same element");
1194 if (has_property
($e, "represent_as") and has_property
($e, "value")) {
1195 fatal
($e, el_name
($e) . " : represent_as() and value() can not be used on the same element");
1198 if (has_property
($e, "subcontext")) {
1199 warning
($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1202 if (defined (has_property
($e, "subcontext_size")) and not defined(has_property
($e, "subcontext"))) {
1203 fatal
($e, el_name
($e) . " : subcontext_size() on non-subcontext element");
1206 if (defined (has_property
($e, "compression")) and not defined(has_property
($e, "subcontext"))) {
1207 fatal
($e, el_name
($e) . " : compression() on non-subcontext element");
1210 if (!$e->{POINTERS
} && (
1211 has_property
($e, "ptr") or
1212 has_property
($e, "unique") or
1213 has_property
($e, "relative") or
1214 has_property
($e, "relative_short") or
1215 has_property
($e, "ref"))) {
1216 fatal
($e, el_name
($e) . " : pointer properties on non-pointer element\n");
1220 #####################################################################
1226 ValidProperties
($enum, "ENUM");
1229 #####################################################################
1235 ValidProperties
($bitmap, "BITMAP");
1238 #####################################################################
1242 my($struct) = shift;
1244 ValidProperties
($struct, "STRUCT");
1246 return unless defined($struct->{ELEMENTS
});
1248 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1249 $e->{PARENT
} = $struct;
1254 #####################################################################
1260 ValidProperties
($union,"UNION");
1262 if (has_property
($union->{PARENT
}, "nodiscriminant") and
1263 has_property
($union->{PARENT
}, "switch_type")) {
1264 fatal
($union->{PARENT
}, $union->{PARENT
}->{NAME
} . ": switch_type(" . $union->{PARENT
}->{PROPERTIES
}->{switch_type
} . ") on union without discriminant");
1267 return unless defined($union->{ELEMENTS
});
1269 foreach my $e (@
{$union->{ELEMENTS
}}) {
1270 $e->{PARENT
} = $union;
1272 if (defined($e->{PROPERTIES
}->{default}) and
1273 defined($e->{PROPERTIES
}->{case
})) {
1274 fatal
($e, "Union member $e->{NAME} can not have both default and case properties!");
1277 unless (defined ($e->{PROPERTIES
}->{default}) or
1278 defined ($e->{PROPERTIES
}->{case
})) {
1279 fatal
($e, "Union member $e->{NAME} must have default or case property");
1282 if (has_property
($e, "ref")) {
1283 fatal
($e, el_name
($e) . ": embedded ref pointers are not supported yet\n");
1291 #####################################################################
1296 my $struct = $pipe->{DATA
};
1298 ValidProperties
($pipe, "PIPE");
1300 $struct->{PARENT
} = $pipe;
1302 $struct->{FILE
} = $pipe->{FILE
} unless defined($struct->{FILE
});
1303 $struct->{LINE
} = $pipe->{LINE
} unless defined($struct->{LINE
});
1308 #####################################################################
1312 my($typedef) = shift;
1313 my $data = $typedef->{DATA
};
1315 ValidProperties
($typedef, "TYPEDEF");
1317 return unless (ref($data) eq "HASH");
1319 $data->{PARENT
} = $typedef;
1321 $data->{FILE
} = $typedef->{FILE
} unless defined($data->{FILE
});
1322 $data->{LINE
} = $typedef->{LINE
} unless defined($data->{LINE
});
1327 #####################################################################
1328 # validate a function
1329 sub ValidFunction
($)
1333 ValidProperties
($fn,"FUNCTION");
1335 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1337 if (has_property
($e, "ref") && !$e->{POINTERS
}) {
1338 fatal
($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1344 #####################################################################
1351 TYPEDEF
=> \
&ValidTypedef
,
1352 STRUCT
=> \
&ValidStruct
,
1353 UNION
=> \
&ValidUnion
,
1354 ENUM
=> \
&ValidEnum
,
1355 BITMAP
=> \
&ValidBitmap
,
1357 }->{$t->{TYPE
}}->($t);
1360 #####################################################################
1361 # parse the interface definitions
1362 sub ValidInterface
($)
1364 my($interface) = shift;
1365 my($data) = $interface->{DATA
};
1367 if (has_property
($interface, "helper")) {
1368 warning
($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1371 ValidProperties
($interface,"INTERFACE");
1373 if (has_property
($interface, "pointer_default")) {
1374 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1375 ("ref", "unique", "ptr"))) {
1376 fatal
($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1380 if (has_property
($interface, "object")) {
1381 if (has_property
($interface, "version") &&
1382 $interface->{PROPERTIES
}->{version
} != 0) {
1383 fatal
($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1386 if (!defined($interface->{BASE
}) &&
1387 not ($interface->{NAME
} eq "IUnknown")) {
1388 fatal
($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1392 foreach my $d (@
{$data}) {
1393 ($d->{TYPE
} eq "FUNCTION") && ValidFunction
($d);
1394 ($d->{TYPE
} eq "TYPEDEF" or
1395 $d->{TYPE
} eq "STRUCT" or
1396 $d->{TYPE
} eq "UNION" or
1397 $d->{TYPE
} eq "ENUM" or
1398 $d->{TYPE
} eq "BITMAP" or
1399 $d->{TYPE
} eq "PIPE") && ValidType
($d);
1404 #####################################################################
1405 # Validate an IDL structure
1410 foreach my $x (@
{$idl}) {
1411 ($x->{TYPE
} eq "INTERFACE") &&
1413 ($x->{TYPE
} eq "IMPORTLIB") &&
1414 fatal
($x, "importlib() not supported");
1418 sub is_charset_array
($$)
1422 return 0 if ($l->{TYPE
} ne "ARRAY");
1424 my $nl = GetNextLevel
($e,$l);
1426 return 0 unless ($nl->{TYPE
} eq "DATA");
1428 return has_property
($e, "charset");