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 ReturnTypeElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
42 use Parse
::Pidl
qw(warning fatal);
43 use Parse
::Pidl
::Typelist
qw(hasType getType typeIs expandAlias mapScalarType is_fixed_size_scalar);
44 use Parse
::Pidl
::Util
qw(has_property property_matches);
46 # Alignment of the built-in scalar types
47 my $scalar_alignment = {
69 'string_array' => 4, #???
81 'wrepl_nbt_name' => 4,
83 'ipv6address' => 4, #16?
89 sub GetElementLevelTable
($$$)
91 my ($e, $pointer_default, $ms_union) = @_;
95 my @bracket_array = ();
100 if (has_property
($e, "size_is")) {
101 @size_is = split /,/, has_property
($e, "size_is");
104 if (has_property
($e, "length_is")) {
105 @length_is = split /,/, has_property
($e, "length_is");
108 if (defined($e->{ARRAY_LEN
})) {
109 @bracket_array = @
{$e->{ARRAY_LEN
}};
112 if (has_property
($e, "out")) {
115 if (has_property
($e, "string") and not has_property
($e, "in")) { $needptrs++; }
116 if ($#bracket_array >= 0) { $needptrs = 0; }
118 warning
($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS
});
121 my $allow_pipe = (($e->{PARENT
}->{TYPE
} // '') eq "FUNCTION");
122 my $is_pipe = typeIs
($e->{TYPE
}, "PIPE");
125 if (not $allow_pipe) {
126 fatal
($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}");
129 if ($e->{POINTERS
} > 1) {
130 fatal
($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}");
133 if ($e->{POINTERS
} < 0) {
134 fatal
($e, "pipe element $e->{NAME} needs pointer");
137 if ($e->{POINTERS
} == 1 and pointer_type
($e) ne "ref") {
138 fatal
($e, "pointer should be 'ref' on pipe element $e->{NAME}");
141 if (scalar(@size_is) > 0) {
142 fatal
($e, "size_is() on pipe element");
145 if (scalar(@length_is) > 0) {
146 fatal
($e, "length_is() on pipe element");
149 if (scalar(@bracket_array) > 0) {
150 fatal
($e, "brackets on pipe element");
153 if (defined(has_property
($e, "subcontext"))) {
154 fatal
($e, "subcontext on pipe element");
157 if (has_property
($e, "switch_is")) {
158 fatal
($e, "switch_is on pipe element");
161 if (can_contain_deferred
($e->{TYPE
})) {
162 fatal
($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element");
166 # Parse the [][][][] style array stuff
167 for my $i (0 .. $#bracket_array) {
168 my $d = $bracket_array[$#bracket_array - $i];
171 my $is_surrounding = 0;
173 my $is_conformant = 0;
181 if ($size = shift @size_is) {
182 if ($e->{POINTERS
} < 1 and has_property
($e, "string")) {
184 delete($e->{PROPERTIES
}->{string
});
186 } elsif ((scalar(@size_is) == 0) and has_property
($e, "string")) {
188 delete($e->{PROPERTIES
}->{string
});
190 fatal
($e, "Must specify size_is() for conformant array!")
193 if (($length = shift @length_is) or $is_string) {
199 if ($e == $e->{PARENT
}->{ELEMENTS
}[-1]
200 and $e->{PARENT
}->{TYPE
} ne "FUNCTION") {
205 $is_fixed = 1 if (not $is_conformant and Parse
::Pidl
::Util
::is_constant
($size));
206 $is_inline = 1 if (not $is_conformant and not Parse
::Pidl
::Util
::is_constant
($size));
208 if ($i == 0 and $is_fixed and has_property
($e, "string")) {
212 delete($e->{PROPERTIES
}->{string
});
215 if (has_property
($e, "to_null")) {
222 LENGTH_IS
=> $length,
223 IS_DEFERRED
=> $is_deferred,
224 IS_SURROUNDING
=> $is_surrounding,
225 IS_ZERO_TERMINATED
=> $is_string,
226 IS_VARYING
=> $is_varying,
227 IS_CONFORMANT
=> $is_conformant,
228 IS_FIXED
=> $is_fixed,
229 IS_INLINE
=> $is_inline,
230 IS_TO_NULL
=> $is_to_null
234 # Next, all the pointers
235 foreach my $i (1..$e->{POINTERS
}) {
236 my $level = "EMBEDDED";
237 # Top level "ref" pointers do not have a referrent identifier
238 $level = "TOP" if ($i == 1 and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
242 # Only the first level gets the pointer type from the
243 # pointer property, the others get them from
244 # the pointer_default() interface property
246 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
247 # (Here they talk about the rightmost pointer, but testing shows
248 # they mean the leftmost pointer.)
252 $pt = pointer_type
($e);
254 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
255 $pt = $pointer_default;
261 POINTER_INDEX
=> $pointer_idx,
262 IS_DEFERRED
=> "$is_deferred",
266 warning
($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
267 if ($i == 1 and $pt ne "ref" and
268 $e->{PARENT
}->{TYPE
} eq "FUNCTION" and
269 not has_property
($e, "in"));
273 # everything that follows will be deferred
274 $is_deferred = 1 if ($level ne "TOP");
276 my $array_size = shift @size_is;
283 if ($array_length = shift @length_is) {
286 $array_length = $array_size;
291 if (scalar(@size_is) == 0 and has_property
($e, "string") and
292 $i == $e->{POINTERS
}) {
294 $is_varying = $is_conformant = has_property
($e, "noheader")?
0:1;
295 delete($e->{PROPERTIES
}->{string
});
298 if ($array_size or $is_string) {
301 SIZE_IS
=> $array_size,
302 LENGTH_IS
=> $array_length,
303 IS_DEFERRED
=> $is_deferred,
305 IS_ZERO_TERMINATED
=> $is_string,
306 IS_VARYING
=> $is_varying,
307 IS_CONFORMANT
=> $is_conformant,
320 CONTAINS_DEFERRED
=> 0,
324 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
329 if (defined(has_property
($e, "subcontext"))) {
330 my $hdr_size = has_property
($e, "subcontext");
331 my $subsize = has_property
($e, "subcontext_size");
332 if (not defined($subsize)) {
337 TYPE
=> "SUBCONTEXT",
338 HEADER_SIZE
=> $hdr_size,
339 SUBCONTEXT_SIZE
=> $subsize,
340 IS_DEFERRED
=> $is_deferred,
341 COMPRESSION
=> has_property
($e, "compression"),
345 if (my $switch = has_property
($e, "switch_is")) {
348 SWITCH_IS
=> $switch,
349 IS_DEFERRED
=> $is_deferred
353 if (scalar(@size_is) > 0) {
354 fatal
($e, "size_is() on non-array element");
357 if (scalar(@length_is) > 0) {
358 fatal
($e, "length_is() on non-array element");
361 if (has_property
($e, "string")) {
362 fatal
($e, "string() attribute on non-array element");
367 DATA_TYPE
=> $e->{TYPE
},
368 IS_DEFERRED
=> $is_deferred,
369 CONTAINS_DEFERRED
=> can_contain_deferred
($e->{TYPE
}),
370 IS_SURROUNDING
=> 0 #FIXME
374 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
379 sub GetTypedefLevelTable
($$$$)
381 my ($e, $data, $pointer_default, $ms_union) = @_;
390 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
395 #####################################################################
396 # see if a type contains any deferred data
397 sub can_contain_deferred
399 sub can_contain_deferred
;
400 my ($type, @types_visited) = @_;
402 return 1 unless (hasType
($type)); # assume the worst
404 $type = getType
($type);
406 return 0 if (Parse
::Pidl
::Typelist
::is_scalar
($type));
408 foreach (@types_visited) {
410 # we have already encountered this
411 # type, avoid recursion loop here
417 return can_contain_deferred
($type->{DATA
},
418 @types_visited) if ($type->{TYPE
} eq "TYPEDEF");
420 return 0 unless defined($type->{ELEMENTS
});
422 foreach (@
{$type->{ELEMENTS
}}) {
423 return 1 if ($_->{POINTERS
});
424 push(@types_visited,$type);
425 if (can_contain_deferred
($_->{TYPE
},@types_visited)) {
438 return undef unless $e->{POINTERS
};
440 return "ref" if (has_property
($e, "ref"));
441 return "full" if (has_property
($e, "ptr"));
442 return "sptr" if (has_property
($e, "sptr"));
443 return "unique" if (has_property
($e, "unique"));
444 return "relative" if (has_property
($e, "relative"));
445 return "relative_short" if (has_property
($e, "relative_short"));
446 return "ignore" if (has_property
($e, "ignore"));
451 #####################################################################
452 # work out the correct alignment for a structure or union
453 sub find_largest_alignment
455 my ($s, @types_visited) = @_;
458 for my $e (@
{$s->{ELEMENTS
}}) {
460 if ($e->{POINTERS
}) {
461 # this is a hack for NDR64
462 # the NDR layer translates this into
463 # an alignment of 4 for NDR and 8 for NDR64
465 } elsif (has_property
($e, "subcontext")) {
467 } elsif (has_property
($e, "transmit_as")) {
468 $a = align_type
($e->{PROPERTIES
}->{transmit_as
},
471 $a = align_type
($e->{TYPE
}, @types_visited);
474 $align = $a if ($align < $a);
480 #####################################################################
485 my ($e, @types_visited) = @_;
486 if (ref($e) eq "HASH" and $e->{TYPE
} eq "SCALAR") {
487 my $ret = $scalar_alignment->{$e->{NAME
}};
488 if (not defined $ret) {
489 warning
($e, "no scalar alignment for $e->{NAME}!");
495 return 0 if ($e eq "EMPTY");
497 unless (hasType
($e)) {
498 # it must be an external type - all we can do is guess
499 # warning($e, "assuming alignment of unknown type '$e' is 4");
502 my $dt = getType
($e);
504 foreach (@types_visited) {
506 # Chapt 14 of the DCE 1.1: Remote Procedure Call
507 # specification (available from pubs.opengroup.org)
509 # "The alignment of a structure in the octet stream is
510 # the largest of the alignments of the fields it
511 # contains. These fields may also be constructed types.
512 # The same alignment rules apply recursively to
513 # nested constructed types. "
515 # in the worst case scenario
521 # the nested struct c1 memc when encountered
522 # returns 0 ensuring the alignment will be calculated
523 # based on the other fields
529 if ($dt->{TYPE
} eq "TYPEDEF") {
530 return align_type
($dt->{DATA
}, @types_visited);
531 } elsif ($dt->{TYPE
} eq "CONFORMANCE") {
532 return $dt->{DATA
}->{ALIGN
};
533 } elsif ($dt->{TYPE
} eq "ENUM") {
534 return align_type
(Parse
::Pidl
::Typelist
::enum_type_fn
($dt),
536 } elsif ($dt->{TYPE
} eq "BITMAP") {
537 return align_type
(Parse
::Pidl
::Typelist
::bitmap_type_fn
($dt),
539 } elsif (($dt->{TYPE
} eq "STRUCT") or ($dt->{TYPE
} eq "UNION")) {
540 # Struct/union without body: assume 4
541 return 4 unless (defined($dt->{ELEMENTS
}));
543 push(@types_visited, $dt);
544 $res = find_largest_alignment
($dt, @types_visited);
547 } elsif (($dt->{TYPE
} eq "PIPE")) {
551 die("Unknown data type type $dt->{TYPE}");
554 sub ParseElement
($$$)
556 my ($e, $pointer_default, $ms_union) = @_;
558 $e->{TYPE
} = expandAlias
($e->{TYPE
});
560 if (ref($e->{TYPE
}) eq "HASH") {
561 $e->{TYPE
} = ParseType
($e->{TYPE
}, $pointer_default, $ms_union);
567 PROPERTIES
=> $e->{PROPERTIES
},
568 LEVELS
=> GetElementLevelTable
($e, $pointer_default, $ms_union),
569 REPRESENTATION_TYPE
=> ($e->{PROPERTIES
}->{represent_as
} or $e->{TYPE
}),
570 ALIGN
=> align_type
($e->{TYPE
}),
577 my ($struct, $pointer_default, $ms_union) = @_;
579 my $surrounding = undef;
583 NAME
=> $struct->{NAME
},
584 SURROUNDING_ELEMENT
=> undef,
586 PROPERTIES
=> $struct->{PROPERTIES
},
589 } unless defined($struct->{ELEMENTS
});
591 CheckPointerTypes
($struct, $pointer_default);
593 foreach my $x (@
{$struct->{ELEMENTS
}})
595 my $e = ParseElement
($x, $pointer_default, $ms_union);
596 if ($x != $struct->{ELEMENTS
}[-1] and
597 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
598 fatal
($x, "conformant member not at end of struct");
603 my $e = $elements[-1];
604 if (defined($e) and defined($e->{LEVELS
}[0]->{IS_SURROUNDING
}) and
605 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
609 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
610 && property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
611 $surrounding = $struct->{ELEMENTS
}[-1];
615 if ($struct->{NAME
}) {
616 $align = align_type
($struct->{NAME
});
621 NAME
=> $struct->{NAME
},
622 SURROUNDING_ELEMENT
=> $surrounding,
623 ELEMENTS
=> \
@elements,
624 PROPERTIES
=> $struct->{PROPERTIES
},
632 my ($e, $pointer_default, $ms_union) = @_;
634 my $is_ms_union = $ms_union;
635 $is_ms_union = 1 if has_property
($e, "ms_union");
637 my $switch_type = has_property
($e, "switch_type");
638 unless (defined($switch_type)) { $switch_type = "uint32"; }
639 if (has_property
($e, "nodiscriminant")) { $switch_type = undef; }
644 SWITCH_TYPE
=> $switch_type,
646 PROPERTIES
=> $e->{PROPERTIES
},
647 HAS_DEFAULT
=> $hasdefault,
648 IS_MS_UNION
=> $is_ms_union,
651 } unless defined($e->{ELEMENTS
});
653 CheckPointerTypes
($e, $pointer_default);
655 foreach my $x (@
{$e->{ELEMENTS
}})
658 if ($x->{TYPE
} eq "EMPTY") {
659 $t = { TYPE
=> "EMPTY" };
661 $t = ParseElement
($x, $pointer_default, $ms_union);
663 if (has_property
($x, "default")) {
664 $t->{CASE
} = "default";
666 } elsif (defined($x->{PROPERTIES
}->{case
})) {
667 $t->{CASE
} = "case $x->{PROPERTIES}->{case}";
669 die("Union element $x->{NAME} has neither default nor case property");
676 $align = align_type
($e->{NAME
});
682 SWITCH_TYPE
=> $switch_type,
683 ELEMENTS
=> \
@elements,
684 PROPERTIES
=> $e->{PROPERTIES
},
685 HAS_DEFAULT
=> $hasdefault,
686 IS_MS_UNION
=> $is_ms_union,
694 my ($e, $pointer_default, $ms_union) = @_;
699 BASE_TYPE
=> Parse
::Pidl
::Typelist
::enum_type_fn
($e),
700 ELEMENTS
=> $e->{ELEMENTS
},
701 PROPERTIES
=> $e->{PROPERTIES
},
708 my ($e, $pointer_default, $ms_union) = @_;
713 BASE_TYPE
=> Parse
::Pidl
::Typelist
::bitmap_type_fn
($e),
714 ELEMENTS
=> $e->{ELEMENTS
},
715 PROPERTIES
=> $e->{PROPERTIES
},
722 my ($pipe, $pointer_default, $ms_union) = @_;
724 my $pname = $pipe->{NAME
};
725 $pname = $pipe->{PARENT
}->{NAME
} unless defined $pname;
727 if (not defined($pipe->{PROPERTIES
})
728 and defined($pipe->{PARENT
}->{PROPERTIES
})) {
729 $pipe->{PROPERTIES
} = $pipe->{PARENT
}->{PROPERTIES
};
732 if (ref($pipe->{DATA
}) eq "HASH") {
733 if (not defined($pipe->{DATA
}->{PROPERTIES
})
734 and defined($pipe->{PROPERTIES
})) {
735 $pipe->{DATA
}->{PROPERTIES
} = $pipe->{PROPERTIES
};
739 my $struct = ParseStruct
($pipe->{DATA
}, $pointer_default, $ms_union);
740 $struct->{ALIGN
} = 5;
741 $struct->{NAME
} = "$pname\_chunk";
743 # 'count' is element [0] and 'array' [1]
744 my $e = $struct->{ELEMENTS
}[1];
745 # level [0] is of type "ARRAY"
746 my $l = $e->{LEVELS
}[1];
748 # here we check that pipe elements have a fixed size type
749 while (defined($l)) {
751 $l = GetNextLevel
($e, $cl);
752 if ($cl->{TYPE
} ne "DATA") {
753 fatal
($pipe, el_name
($pipe) . ": pipe contains non DATA level");
756 # for now we only support scalars
757 next if is_fixed_size_scalar
($cl->{DATA_TYPE
});
759 fatal
($pipe, el_name
($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]");
764 NAME
=> $pipe->{NAME
},
766 PROPERTIES
=> $pipe->{PROPERTIES
},
773 my ($d, $pointer_default, $ms_union) = @_;
776 STRUCT
=> \
&ParseStruct
,
777 UNION
=> \
&ParseUnion
,
779 BITMAP
=> \
&ParseBitmap
,
780 TYPEDEF
=> \
&ParseTypedef
,
782 }->{$d->{TYPE
}}->($d, $pointer_default, $ms_union);
789 my ($d, $pointer_default, $ms_union) = @_;
793 if (ref($d->{DATA
}) eq "HASH") {
794 if (defined($d->{DATA
}->{PROPERTIES
})
795 and not defined($d->{PROPERTIES
})) {
796 $d->{PROPERTIES
} = $d->{DATA
}->{PROPERTIES
};
799 $data = ParseType
($d->{DATA
}, $pointer_default, $ms_union);
800 $data->{ALIGN
} = align_type
($d->{NAME
});
802 $data = getType
($d->{DATA
});
808 PROPERTIES
=> $d->{PROPERTIES
},
809 LEVELS
=> GetTypedefLevelTable
($d, $data, $pointer_default, $ms_union),
822 sub ParseFunction
($$$$)
824 my ($ndr,$d,$opnum,$ms_union) = @_;
827 my $thisopnum = undef;
829 CheckPointerTypes
($d, "ref");
831 if (not defined($d->{PROPERTIES
}{noopnum
})) {
832 $thisopnum = ${$opnum};
836 foreach my $x (@
{$d->{ELEMENTS
}}) {
837 my $e = ParseElement
($x, $ndr->{PROPERTIES
}->{pointer_default
}, $ms_union);
838 push (@
{$e->{DIRECTION
}}, "in") if (has_property
($x, "in"));
839 push (@
{$e->{DIRECTION
}}, "out") if (has_property
($x, "out"));
841 push (@elements, $e);
844 if ($d->{RETURN_TYPE
} ne "void") {
845 $rettype = expandAlias
($d->{RETURN_TYPE
});
852 RETURN_TYPE
=> $rettype,
853 PROPERTIES
=> $d->{PROPERTIES
},
854 ELEMENTS
=> \
@elements,
859 sub ReturnTypeElement
($)
863 return undef unless defined($fn->{RETURN_TYPE
});
867 "TYPE" => $fn->{RETURN_TYPE
},
868 "PROPERTIES" => undef,
871 "FILE" => $fn->{FILE
},
872 "LINE" => $fn->{LINE
},
875 return ParseElement
($e, 0, 0);
878 sub CheckPointerTypes
($$)
880 my ($s,$default) = @_;
882 return unless defined($s->{ELEMENTS
});
884 foreach my $e (@
{$s->{ELEMENTS
}}) {
885 if ($e->{POINTERS
} and not defined(pointer_type
($e))) {
886 $e->{PROPERTIES
}->{$default} = '1';
891 sub FindNestedTypes
($$)
893 sub FindNestedTypes
($$);
896 return unless defined($t->{ELEMENTS
});
897 return if ($t->{TYPE
} eq "ENUM");
898 return if ($t->{TYPE
} eq "BITMAP");
900 foreach (@
{$t->{ELEMENTS
}}) {
901 if (ref($_->{TYPE
}) eq "HASH") {
902 push (@
$l, $_->{TYPE
}) if (defined($_->{TYPE
}->{NAME
}));
903 FindNestedTypes
($l, $_->{TYPE
});
908 sub ParseInterface
($)
918 $ms_union = 1 if has_property
($idl, "ms_union");
920 if (not has_property
($idl, "pointer_default")) {
921 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
922 # and "unique" in Microsoft Extensions mode (default)
923 $idl->{PROPERTIES
}->{pointer_default
} = "unique";
926 foreach my $d (@
{$idl->{DATA
}}) {
927 if ($d->{TYPE
} eq "FUNCTION") {
928 push (@functions, ParseFunction
($idl, $d, \
$opnum, $ms_union));
929 } elsif ($d->{TYPE
} eq "CONST") {
930 push (@consts, ParseConst
($idl, $d));
932 push (@types, ParseType
($d, $idl->{PROPERTIES
}->{pointer_default
}, $ms_union));
933 FindNestedTypes
(\
@types, $d);
939 if(defined $idl->{PROPERTIES
}->{version
}) {
940 my @if_version = split(/\./, $idl->{PROPERTIES
}->{version
});
941 if ($if_version[0] == $idl->{PROPERTIES
}->{version
}) {
942 $version = $idl->{PROPERTIES
}->{version
};
944 $version = $if_version[1] << 16 | $if_version[0];
948 # If no endpoint is set, default to the interface name as a named pipe
949 if (!defined $idl->{PROPERTIES
}->{endpoint
}) {
950 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME
} . "]\"";
952 @endpoints = split /,/, $idl->{PROPERTIES
}->{endpoint
};
956 NAME
=> $idl->{NAME
},
957 UUID
=> lc(has_property
($idl, "uuid") // ''),
960 PROPERTIES
=> $idl->{PROPERTIES
},
961 FUNCTIONS
=> \
@functions,
964 ENDPOINTS
=> \
@endpoints,
969 # Convert a IDL tree to a NDR tree
970 # Gives a result tree describing all that's necessary for easily generating
971 # NDR parsers / generators
976 return undef unless (defined($idl));
978 Parse
::Pidl
::NDR
::Validate
($idl);
983 ($_->{TYPE
} eq "CPP_QUOTE") && push(@ndr, $_);
984 ($_->{TYPE
} eq "INTERFACE") && push(@ndr, ParseInterface
($_));
985 ($_->{TYPE
} eq "IMPORT") && push(@ndr, $_);
998 foreach my $l (@
{$e->{LEVELS
}}) {
999 return $l if ($seen);
1000 ($seen = 1) if ($l == $fl);
1006 sub GetPrevLevel
($$)
1011 foreach my $l (@
{$e->{LEVELS
}}) {
1012 (return $prev) if ($l == $fl);
1019 sub ContainsString
($)
1023 if (property_matches
($e, "flag", ".*STR_NULLTERM.*")) {
1026 if (exists($e->{LEVELS
}) and $e->{LEVELS
}->[0]->{TYPE
} eq "ARRAY" and
1027 ($e->{LEVELS
}->[0]->{IS_FIXED
} or $e->{LEVELS
}->[0]->{IS_INLINE
}) and
1028 has_property
($e, "charset"))
1033 foreach my $l (@
{$e->{LEVELS
}}) {
1034 return 1 if ($l->{TYPE
} eq "ARRAY" and $l->{IS_ZERO_TERMINATED
});
1036 if (property_matches
($e, "charset", ".*DOS.*")) {
1043 sub ContainsDeferred
($$)
1047 return 1 if ($l->{CONTAINS_DEFERRED
});
1049 while ($l = GetNextLevel
($e,$l))
1051 return 1 if ($l->{IS_DEFERRED
});
1052 return 1 if ($l->{CONTAINS_DEFERRED
});
1058 sub ContainsPipe
($$)
1062 return 1 if ($l->{TYPE
} eq "PIPE");
1064 while ($l = GetNextLevel
($e,$l))
1066 return 1 if ($l->{TYPE
} eq "PIPE");
1075 my $name = "<ANONYMOUS>";
1077 $name = $e->{NAME
} if defined($e->{NAME
});
1079 if (defined($e->{PARENT
}) and defined($e->{PARENT
}->{NAME
})) {
1080 return "$e->{PARENT}->{NAME}.$name";
1083 if (defined($e->{PARENT
}) and
1084 defined($e->{PARENT
}->{PARENT
}) and
1085 defined($e->{PARENT
}->{PARENT
}->{NAME
})) {
1086 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
1092 ###################################
1093 # find a sibling var in a structure
1094 sub find_sibling
($$)
1097 my($fn) = $e->{PARENT
};
1099 if ($name =~ /\*(.*)/) {
1103 for my $e2 (@
{$fn->{ELEMENTS
}}) {
1104 return $e2 if ($e2->{NAME
} eq $name);
1110 my %property_list = (
1112 "helpstring" => ["INTERFACE", "FUNCTION"],
1113 "version" => ["INTERFACE"],
1114 "uuid" => ["INTERFACE"],
1115 "endpoint" => ["INTERFACE"],
1116 "pointer_default" => ["INTERFACE"],
1117 "helper" => ["INTERFACE"],
1118 "pyhelper" => ["INTERFACE"],
1119 "authservice" => ["INTERFACE"],
1120 "restricted" => ["INTERFACE"],
1121 "no_srv_register" => ["INTERFACE"],
1124 "object" => ["INTERFACE"],
1125 "local" => ["INTERFACE", "FUNCTION"],
1126 "iid_is" => ["ELEMENT"],
1127 "call_as" => ["FUNCTION"],
1128 "idempotent" => ["FUNCTION"],
1131 "noopnum" => ["FUNCTION"],
1132 "in" => ["ELEMENT"],
1133 "out" => ["ELEMENT"],
1136 "ref" => ["ELEMENT", "TYPEDEF"],
1137 "ptr" => ["ELEMENT", "TYPEDEF"],
1138 "unique" => ["ELEMENT", "TYPEDEF"],
1139 "ignore" => ["ELEMENT"],
1140 "relative" => ["ELEMENT", "TYPEDEF"],
1141 "relative_short" => ["ELEMENT", "TYPEDEF"],
1142 "null_is_ffffffff" => ["ELEMENT"],
1143 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
1145 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
1146 "value" => ["ELEMENT"],
1147 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1148 "max_recursion" => ["ELEMENT"],
1151 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1152 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1153 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1154 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1155 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"],
1156 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1157 "todo" => ["FUNCTION"],
1158 "skip" => ["ELEMENT"],
1159 "skip_noinit" => ["ELEMENT"],
1162 "switch_is" => ["ELEMENT"],
1163 "switch_type" => ["ELEMENT", "UNION"],
1164 "nodiscriminant" => ["UNION"],
1165 "ms_union" => ["INTERFACE", "UNION"],
1166 "case" => ["ELEMENT"],
1167 "default" => ["ELEMENT"],
1169 "represent_as" => ["ELEMENT"],
1170 "transmit_as" => ["ELEMENT"],
1173 "subcontext" => ["ELEMENT"],
1174 "subcontext_size" => ["ELEMENT"],
1175 "compression" => ["ELEMENT"],
1178 "enum8bit" => ["ENUM"],
1179 "enum16bit" => ["ENUM"],
1180 "v1_enum" => ["ENUM"],
1183 "bitmap8bit" => ["BITMAP"],
1184 "bitmap16bit" => ["BITMAP"],
1185 "bitmap32bit" => ["BITMAP"],
1186 "bitmap64bit" => ["BITMAP"],
1189 "range" => ["ELEMENT", "PIPE"],
1190 "size_is" => ["ELEMENT"],
1191 "string" => ["ELEMENT"],
1192 "noheader" => ["ELEMENT"],
1193 "charset" => ["ELEMENT"],
1194 "length_is" => ["ELEMENT"],
1195 "to_null" => ["ELEMENT"],
1198 #####################################################################
1199 # check for unknown properties
1200 sub ValidProperties
($$)
1204 return unless defined $e->{PROPERTIES
};
1206 foreach my $key (keys %{$e->{PROPERTIES
}}) {
1207 warning
($e, el_name
($e) . ": unknown property '$key'")
1208 unless defined($property_list{$key});
1210 fatal
($e, el_name
($e) . ": property '$key' not allowed on '$t'")
1211 unless grep(/^$t$/, @
{$property_list{$key}});
1219 return $t->{NAME
} if (ref($t) eq "HASH" and $t->{TYPE
} eq "SCALAR");
1220 my $ti = getType
($t);
1222 if (not defined ($ti)) {
1224 } elsif ($ti->{TYPE
} eq "TYPEDEF") {
1225 return mapToScalar
($ti->{DATA
});
1226 } elsif ($ti->{TYPE
} eq "ENUM") {
1227 return Parse
::Pidl
::Typelist
::enum_type_fn
($ti);
1228 } elsif ($ti->{TYPE
} eq "BITMAP") {
1229 return Parse
::Pidl
::Typelist
::bitmap_type_fn
($ti);
1235 #####################################################################
1236 # validate an element
1241 ValidProperties
($e,"ELEMENT");
1243 # Check whether switches are used correctly.
1244 if (my $switch = has_property
($e, "switch_is")) {
1245 my $e2 = find_sibling
($e, $switch);
1246 my $type = getType
($e->{TYPE
});
1248 if (defined($type) and $type->{DATA
}->{TYPE
} ne "UNION") {
1249 fatal
($e, el_name
($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1252 if (not has_property
($type->{DATA
}, "nodiscriminant") and defined($e2)) {
1253 my $discriminator_type = has_property
($type->{DATA
}, "switch_type");
1254 $discriminator_type = "uint32" unless defined ($discriminator_type);
1256 my $t1 = mapScalarType
(mapToScalar
($discriminator_type));
1258 if (not defined($t1)) {
1259 fatal
($e, el_name
($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1262 my $t2 = mapScalarType
(mapToScalar
($e2->{TYPE
}));
1263 if (not defined($t2)) {
1264 fatal
($e, el_name
($e) . ": unable to map variable used for switch_is() to scalar");
1268 warning
($e, el_name
($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1273 if (has_property
($e, "subcontext") and has_property
($e, "represent_as")) {
1274 fatal
($e, el_name
($e) . " : subcontext() and represent_as() can not be used on the same element");
1277 if (has_property
($e, "subcontext") and has_property
($e, "transmit_as")) {
1278 fatal
($e, el_name
($e) . " : subcontext() and transmit_as() can not be used on the same element");
1281 if (has_property
($e, "represent_as") and has_property
($e, "transmit_as")) {
1282 fatal
($e, el_name
($e) . " : represent_as() and transmit_as() can not be used on the same element");
1285 if (has_property
($e, "represent_as") and has_property
($e, "value")) {
1286 fatal
($e, el_name
($e) . " : represent_as() and value() can not be used on the same element");
1289 if (has_property
($e, "subcontext")) {
1290 warning
($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1293 if (defined (has_property
($e, "subcontext_size")) and not defined(has_property
($e, "subcontext"))) {
1294 fatal
($e, el_name
($e) . " : subcontext_size() on non-subcontext element");
1297 if (defined (has_property
($e, "compression")) and not defined(has_property
($e, "subcontext"))) {
1298 fatal
($e, el_name
($e) . " : compression() on non-subcontext element");
1301 if (!$e->{POINTERS
} && (
1302 has_property
($e, "ptr") or
1303 has_property
($e, "unique") or
1304 has_property
($e, "relative") or
1305 has_property
($e, "relative_short") or
1306 has_property
($e, "ref"))) {
1307 fatal
($e, el_name
($e) . " : pointer properties on non-pointer element\n");
1311 #####################################################################
1317 ValidProperties
($enum, "ENUM");
1320 #####################################################################
1326 ValidProperties
($bitmap, "BITMAP");
1329 #####################################################################
1333 my($struct) = shift;
1335 ValidProperties
($struct, "STRUCT");
1337 return unless defined($struct->{ELEMENTS
});
1339 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1340 $e->{PARENT
} = $struct;
1345 #####################################################################
1351 ValidProperties
($union,"UNION");
1353 if (has_property
($union->{PARENT
}, "nodiscriminant") and
1354 has_property
($union->{PARENT
}, "switch_type")) {
1355 fatal
($union->{PARENT
}, $union->{PARENT
}->{NAME
} . ": switch_type(" . $union->{PARENT
}->{PROPERTIES
}->{switch_type
} . ") on union without discriminant");
1358 return unless defined($union->{ELEMENTS
});
1360 foreach my $e (@
{$union->{ELEMENTS
}}) {
1361 $e->{PARENT
} = $union;
1363 if (defined($e->{PROPERTIES
}->{default}) and
1364 defined($e->{PROPERTIES
}->{case
})) {
1365 fatal
($e, "Union member $e->{NAME} can not have both default and case properties!");
1368 unless (defined ($e->{PROPERTIES
}->{default}) or
1369 defined ($e->{PROPERTIES
}->{case
})) {
1370 fatal
($e, "Union member $e->{NAME} must have default or case property");
1373 if (has_property
($e, "ref")) {
1374 fatal
($e, el_name
($e) . ": embedded ref pointers are not supported yet\n");
1382 #####################################################################
1387 my $struct = $pipe->{DATA
};
1389 ValidProperties
($pipe, "PIPE");
1391 $struct->{PARENT
} = $pipe;
1393 $struct->{FILE
} = $pipe->{FILE
} unless defined($struct->{FILE
});
1394 $struct->{LINE
} = $pipe->{LINE
} unless defined($struct->{LINE
});
1399 #####################################################################
1403 my($typedef) = shift;
1404 my $data = $typedef->{DATA
};
1406 ValidProperties
($typedef, "TYPEDEF");
1408 return unless (ref($data) eq "HASH");
1410 $data->{PARENT
} = $typedef;
1412 $data->{FILE
} = $typedef->{FILE
} unless defined($data->{FILE
});
1413 $data->{LINE
} = $typedef->{LINE
} unless defined($data->{LINE
});
1418 #####################################################################
1419 # validate a function
1420 sub ValidFunction
($)
1424 ValidProperties
($fn,"FUNCTION");
1426 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1428 if (has_property
($e, "ref") && !$e->{POINTERS
}) {
1429 fatal
($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1435 #####################################################################
1442 TYPEDEF
=> \
&ValidTypedef
,
1443 STRUCT
=> \
&ValidStruct
,
1444 UNION
=> \
&ValidUnion
,
1445 ENUM
=> \
&ValidEnum
,
1446 BITMAP
=> \
&ValidBitmap
,
1448 }->{$t->{TYPE
}}->($t);
1451 #####################################################################
1452 # parse the interface definitions
1453 sub ValidInterface
($)
1455 my($interface) = shift;
1456 my($data) = $interface->{DATA
};
1458 if (has_property
($interface, "helper")) {
1459 warning
($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1462 ValidProperties
($interface,"INTERFACE");
1464 if (has_property
($interface, "pointer_default")) {
1465 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1466 ("ref", "unique", "ptr"))) {
1467 fatal
($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1471 if (has_property
($interface, "object")) {
1472 if (has_property
($interface, "version") &&
1473 $interface->{PROPERTIES
}->{version
} != 0) {
1474 fatal
($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1477 if (!defined($interface->{BASE
}) &&
1478 not ($interface->{NAME
} eq "IUnknown")) {
1479 fatal
($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1483 foreach my $d (@
{$data}) {
1484 ($d->{TYPE
} eq "FUNCTION") && ValidFunction
($d);
1485 ($d->{TYPE
} eq "TYPEDEF" or
1486 $d->{TYPE
} eq "STRUCT" or
1487 $d->{TYPE
} eq "UNION" or
1488 $d->{TYPE
} eq "ENUM" or
1489 $d->{TYPE
} eq "BITMAP" or
1490 $d->{TYPE
} eq "PIPE") && ValidType
($d);
1495 #####################################################################
1496 # Validate an IDL structure
1501 foreach my $x (@
{$idl}) {
1502 ($x->{TYPE
} eq "INTERFACE") &&
1504 ($x->{TYPE
} eq "IMPORTLIB") &&
1505 fatal
($x, "importlib() not supported");
1509 sub is_charset_array
($$)
1513 return 0 if ($l->{TYPE
} ne "ARRAY");
1515 my $nl = GetNextLevel
($e,$l);
1517 return 0 unless ($nl->{TYPE
} eq "DATA");
1519 return has_property
($e, "charset");