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 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 expandAlias mapScalarType);
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, #???
76 'wrepl_nbt_name' => 4,
78 'ipv6address' => 4, #16?
83 sub GetElementLevelTable
($$)
85 my ($e, $pointer_default) = @_;
89 my @bracket_array = ();
94 if (has_property
($e, "size_is")) {
95 @size_is = split /,/, has_property
($e, "size_is");
98 if (has_property
($e, "length_is")) {
99 @length_is = split /,/, has_property
($e, "length_is");
102 if (defined($e->{ARRAY_LEN
})) {
103 @bracket_array = @
{$e->{ARRAY_LEN
}};
106 if (has_property
($e, "out")) {
109 if (has_property
($e, "string") and not has_property
($e, "in")) { $needptrs++; }
110 if ($#bracket_array >= 0) { $needptrs = 0; }
112 warning
($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS
});
115 # Parse the [][][][] style array stuff
116 for my $i (0 .. $#bracket_array) {
117 my $d = $bracket_array[$#bracket_array - $i];
120 my $is_surrounding = 0;
122 my $is_conformant = 0;
129 if ($size = shift @size_is) {
130 if ($e->{POINTERS
} < 1 and has_property
($e, "string")) {
132 delete($e->{PROPERTIES
}->{string
});
134 } elsif ((scalar(@size_is) == 0) and has_property
($e, "string")) {
136 delete($e->{PROPERTIES
}->{string
});
138 fatal
($e, "Must specify size_is() for conformant array!")
141 if (($length = shift @length_is) or $is_string) {
147 if ($e == $e->{PARENT
}->{ELEMENTS
}[-1]
148 and $e->{PARENT
}->{TYPE
} ne "FUNCTION") {
153 $is_fixed = 1 if (not $is_conformant and Parse
::Pidl
::Util
::is_constant
($size));
154 $is_inline = 1 if (not $is_conformant and not Parse
::Pidl
::Util
::is_constant
($size));
156 if ($i == 0 and $is_fixed and has_property
($e, "string")) {
160 delete($e->{PROPERTIES
}->{string
});
166 LENGTH_IS
=> $length,
167 IS_DEFERRED
=> $is_deferred,
168 IS_SURROUNDING
=> $is_surrounding,
169 IS_ZERO_TERMINATED
=> $is_string,
170 IS_VARYING
=> $is_varying,
171 IS_CONFORMANT
=> $is_conformant,
172 IS_FIXED
=> $is_fixed,
173 IS_INLINE
=> $is_inline
177 # Next, all the pointers
178 foreach my $i (1..$e->{POINTERS
}) {
179 my $level = "EMBEDDED";
180 # Top level "ref" pointers do not have a referrent identifier
181 $level = "TOP" if ($i == 1 and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
185 # Only the first level gets the pointer type from the
186 # pointer property, the others get them from
187 # the pointer_default() interface property
189 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
190 # (Here they talk about the rightmost pointer, but testing shows
191 # they mean the leftmost pointer.)
195 $pt = pointer_type
($e);
197 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT
}->{TYPE
} eq "FUNCTION");
198 $pt = $pointer_default;
204 POINTER_INDEX
=> $pointer_idx,
205 IS_DEFERRED
=> "$is_deferred",
209 warning
($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
210 if ($i == 1 and $pt ne "ref" and
211 $e->{PARENT
}->{TYPE
} eq "FUNCTION" and
212 not has_property
($e, "in"));
216 # everything that follows will be deferred
217 $is_deferred = 1 if ($level ne "TOP");
219 my $array_size = shift @size_is;
226 if ($array_length = shift @length_is) {
229 $array_length = $array_size;
234 if (scalar(@size_is) == 0 and has_property
($e, "string") and
235 $i == $e->{POINTERS
}) {
237 $is_varying = $is_conformant = has_property
($e, "noheader")?
0:1;
238 delete($e->{PROPERTIES
}->{string
});
241 if ($array_size or $is_string) {
244 SIZE_IS
=> $array_size,
245 LENGTH_IS
=> $array_length,
246 IS_DEFERRED
=> $is_deferred,
248 IS_ZERO_TERMINATED
=> $is_string,
249 IS_VARYING
=> $is_varying,
250 IS_CONFORMANT
=> $is_conformant,
259 if (defined(has_property
($e, "subcontext"))) {
260 my $hdr_size = has_property
($e, "subcontext");
261 my $subsize = has_property
($e, "subcontext_size");
262 if (not defined($subsize)) {
267 TYPE
=> "SUBCONTEXT",
268 HEADER_SIZE
=> $hdr_size,
269 SUBCONTEXT_SIZE
=> $subsize,
270 IS_DEFERRED
=> $is_deferred,
271 COMPRESSION
=> has_property
($e, "compression"),
275 if (my $switch = has_property
($e, "switch_is")) {
278 SWITCH_IS
=> $switch,
279 IS_DEFERRED
=> $is_deferred
283 if (scalar(@size_is) > 0) {
284 fatal
($e, "size_is() on non-array element");
287 if (scalar(@length_is) > 0) {
288 fatal
($e, "length_is() on non-array element");
291 if (has_property
($e, "string")) {
292 fatal
($e, "string() attribute on non-array element");
297 DATA_TYPE
=> $e->{TYPE
},
298 IS_DEFERRED
=> $is_deferred,
299 CONTAINS_DEFERRED
=> can_contain_deferred
($e->{TYPE
}),
300 IS_SURROUNDING
=> 0 #FIXME
304 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
309 sub GetTypedefLevelTable
($$$)
311 my ($e, $data, $pointer_default) = @_;
320 foreach (@
$order) { $_->{LEVEL_INDEX
} = $i; $i+=1; }
325 #####################################################################
326 # see if a type contains any deferred data
327 sub can_contain_deferred
($)
329 sub can_contain_deferred
($);
332 return 1 unless (hasType
($type)); # assume the worst
334 $type = getType
($type);
336 return 0 if (Parse
::Pidl
::Typelist
::is_scalar
($type));
338 return can_contain_deferred
($type->{DATA
}) if ($type->{TYPE
} eq "TYPEDEF");
340 return 0 unless defined($type->{ELEMENTS
});
342 foreach (@
{$type->{ELEMENTS
}}) {
343 return 1 if ($_->{POINTERS
});
344 return 1 if (can_contain_deferred
($_->{TYPE
}));
354 return undef unless $e->{POINTERS
};
356 return "ref" if (has_property
($e, "ref"));
357 return "full" if (has_property
($e, "ptr"));
358 return "sptr" if (has_property
($e, "sptr"));
359 return "unique" if (has_property
($e, "unique"));
360 return "relative" if (has_property
($e, "relative"));
361 return "relative_short" if (has_property
($e, "relative_short"));
362 return "ignore" if (has_property
($e, "ignore"));
367 #####################################################################
368 # work out the correct alignment for a structure or union
369 sub find_largest_alignment
($)
374 for my $e (@
{$s->{ELEMENTS
}}) {
377 if ($e->{POINTERS
}) {
378 # this is a hack for NDR64
379 # the NDR layer translates this into
380 # an alignment of 4 for NDR and 8 for NDR64
382 } elsif (has_property
($e, "subcontext")) {
384 } elsif (has_property
($e, "transmit_as")) {
385 $a = align_type
($e->{PROPERTIES
}->{transmit_as
});
387 $a = align_type
($e->{TYPE
});
390 $align = $a if ($align < $a);
396 #####################################################################
403 if (ref($e) eq "HASH" and $e->{TYPE
} eq "SCALAR") {
404 return $scalar_alignment->{$e->{NAME
}};
407 return 0 if ($e eq "EMPTY");
409 unless (hasType
($e)) {
410 # it must be an external type - all we can do is guess
411 # warning($e, "assuming alignment of unknown type '$e' is 4");
415 my $dt = getType
($e);
417 if ($dt->{TYPE
} eq "TYPEDEF") {
418 return align_type
($dt->{DATA
});
419 } elsif ($dt->{TYPE
} eq "CONFORMANCE") {
420 return $dt->{DATA
}->{ALIGN
};
421 } elsif ($dt->{TYPE
} eq "ENUM") {
422 return align_type
(Parse
::Pidl
::Typelist
::enum_type_fn
($dt));
423 } elsif ($dt->{TYPE
} eq "BITMAP") {
424 return align_type
(Parse
::Pidl
::Typelist
::bitmap_type_fn
($dt));
425 } elsif (($dt->{TYPE
} eq "STRUCT") or ($dt->{TYPE
} eq "UNION")) {
426 # Struct/union without body: assume 4
427 return 4 unless (defined($dt->{ELEMENTS
}));
428 return find_largest_alignment
($dt);
431 die("Unknown data type type $dt->{TYPE}");
436 my ($e, $pointer_default) = @_;
438 $e->{TYPE
} = expandAlias
($e->{TYPE
});
440 if (ref($e->{TYPE
}) eq "HASH") {
441 $e->{TYPE
} = ParseType
($e->{TYPE
}, $pointer_default);
447 PROPERTIES
=> $e->{PROPERTIES
},
448 LEVELS
=> GetElementLevelTable
($e, $pointer_default),
449 REPRESENTATION_TYPE
=> ($e->{PROPERTIES
}->{represent_as
} or $e->{TYPE
}),
450 ALIGN
=> align_type
($e->{TYPE
}),
457 my ($struct, $pointer_default) = @_;
459 my $surrounding = undef;
463 NAME
=> $struct->{NAME
},
464 SURROUNDING_ELEMENT
=> undef,
466 PROPERTIES
=> $struct->{PROPERTIES
},
469 } unless defined($struct->{ELEMENTS
});
471 CheckPointerTypes
($struct, $pointer_default);
473 foreach my $x (@
{$struct->{ELEMENTS
}})
475 my $e = ParseElement
($x, $pointer_default);
476 if ($x != $struct->{ELEMENTS
}[-1] and
477 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
478 fatal
($x, "conformant member not at end of struct");
483 my $e = $elements[-1];
484 if (defined($e) and defined($e->{LEVELS
}[0]->{IS_SURROUNDING
}) and
485 $e->{LEVELS
}[0]->{IS_SURROUNDING
}) {
489 if (defined $e->{TYPE
} && $e->{TYPE
} eq "string"
490 && property_matches
($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
491 $surrounding = $struct->{ELEMENTS
}[-1];
495 if ($struct->{NAME
}) {
496 $align = align_type
($struct->{NAME
});
501 NAME
=> $struct->{NAME
},
502 SURROUNDING_ELEMENT
=> $surrounding,
503 ELEMENTS
=> \
@elements,
504 PROPERTIES
=> $struct->{PROPERTIES
},
512 my ($e, $pointer_default) = @_;
515 my $switch_type = has_property
($e, "switch_type");
516 unless (defined($switch_type)) { $switch_type = "uint32"; }
517 if (has_property
($e, "nodiscriminant")) { $switch_type = undef; }
522 SWITCH_TYPE
=> $switch_type,
524 PROPERTIES
=> $e->{PROPERTIES
},
525 HAS_DEFAULT
=> $hasdefault,
528 } unless defined($e->{ELEMENTS
});
530 CheckPointerTypes
($e, $pointer_default);
532 foreach my $x (@
{$e->{ELEMENTS
}})
535 if ($x->{TYPE
} eq "EMPTY") {
536 $t = { TYPE
=> "EMPTY" };
538 $t = ParseElement
($x, $pointer_default);
540 if (has_property
($x, "default")) {
541 $t->{CASE
} = "default";
543 } elsif (defined($x->{PROPERTIES
}->{case
})) {
544 $t->{CASE
} = "case $x->{PROPERTIES}->{case}";
546 die("Union element $x->{NAME} has neither default nor case property");
553 $align = align_type
($e->{NAME
});
559 SWITCH_TYPE
=> $switch_type,
560 ELEMENTS
=> \
@elements,
561 PROPERTIES
=> $e->{PROPERTIES
},
562 HAS_DEFAULT
=> $hasdefault,
570 my ($e, $pointer_default) = @_;
575 BASE_TYPE
=> Parse
::Pidl
::Typelist
::enum_type_fn
($e),
576 ELEMENTS
=> $e->{ELEMENTS
},
577 PROPERTIES
=> $e->{PROPERTIES
},
584 my ($e, $pointer_default) = @_;
589 BASE_TYPE
=> Parse
::Pidl
::Typelist
::bitmap_type_fn
($e),
590 ELEMENTS
=> $e->{ELEMENTS
},
591 PROPERTIES
=> $e->{PROPERTIES
},
598 my ($d, $pointer_default) = @_;
601 STRUCT
=> \
&ParseStruct
,
602 UNION
=> \
&ParseUnion
,
604 BITMAP
=> \
&ParseBitmap
,
605 TYPEDEF
=> \
&ParseTypedef
,
606 }->{$d->{TYPE
}}->($d, $pointer_default);
613 my ($d, $pointer_default) = @_;
617 if (ref($d->{DATA
}) eq "HASH") {
618 if (defined($d->{DATA
}->{PROPERTIES
})
619 and not defined($d->{PROPERTIES
})) {
620 $d->{PROPERTIES
} = $d->{DATA
}->{PROPERTIES
};
623 $data = ParseType
($d->{DATA
}, $pointer_default);
624 $data->{ALIGN
} = align_type
($d->{NAME
});
626 $data = getType
($d->{DATA
});
632 PROPERTIES
=> $d->{PROPERTIES
},
633 LEVELS
=> GetTypedefLevelTable
($d, $data, $pointer_default),
646 sub ParseFunction
($$$)
648 my ($ndr,$d,$opnum) = @_;
651 my $thisopnum = undef;
653 CheckPointerTypes
($d, "ref");
655 if (not defined($d->{PROPERTIES
}{noopnum
})) {
656 $thisopnum = ${$opnum};
660 foreach my $x (@
{$d->{ELEMENTS
}}) {
661 my $e = ParseElement
($x, $ndr->{PROPERTIES
}->{pointer_default
});
662 push (@
{$e->{DIRECTION
}}, "in") if (has_property
($x, "in"));
663 push (@
{$e->{DIRECTION
}}, "out") if (has_property
($x, "out"));
665 push (@elements, $e);
668 if ($d->{RETURN_TYPE
} ne "void") {
669 $rettype = expandAlias
($d->{RETURN_TYPE
});
676 RETURN_TYPE
=> $rettype,
677 PROPERTIES
=> $d->{PROPERTIES
},
678 ELEMENTS
=> \
@elements,
683 sub CheckPointerTypes
($$)
685 my ($s,$default) = @_;
687 return unless defined($s->{ELEMENTS
});
689 foreach my $e (@
{$s->{ELEMENTS
}}) {
690 if ($e->{POINTERS
} and not defined(pointer_type
($e))) {
691 $e->{PROPERTIES
}->{$default} = '1';
696 sub FindNestedTypes
($$)
698 sub FindNestedTypes
($$);
701 return unless defined($t->{ELEMENTS
});
702 return if ($t->{TYPE
} eq "ENUM");
703 return if ($t->{TYPE
} eq "BITMAP");
705 foreach (@
{$t->{ELEMENTS
}}) {
706 if (ref($_->{TYPE
}) eq "HASH") {
707 push (@
$l, $_->{TYPE
}) if (defined($_->{TYPE
}->{NAME
}));
708 FindNestedTypes
($l, $_->{TYPE
});
713 sub ParseInterface
($)
723 if (not has_property
($idl, "pointer_default")) {
724 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
725 # and "unique" in Microsoft Extensions mode (default)
726 $idl->{PROPERTIES
}->{pointer_default
} = "unique";
729 foreach my $d (@
{$idl->{DATA
}}) {
730 if ($d->{TYPE
} eq "FUNCTION") {
731 push (@functions, ParseFunction
($idl, $d, \
$opnum));
732 } elsif ($d->{TYPE
} eq "CONST") {
733 push (@consts, ParseConst
($idl, $d));
735 push (@types, ParseType
($d, $idl->{PROPERTIES
}->{pointer_default
}));
736 FindNestedTypes
(\
@types, $d);
742 if(defined $idl->{PROPERTIES
}->{version
}) {
743 my @if_version = split(/\./, $idl->{PROPERTIES
}->{version
});
744 if ($if_version[0] == $idl->{PROPERTIES
}->{version
}) {
745 $version = $idl->{PROPERTIES
}->{version
};
747 $version = $if_version[1] << 16 | $if_version[0];
751 # If no endpoint is set, default to the interface name as a named pipe
752 if (!defined $idl->{PROPERTIES
}->{endpoint
}) {
753 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME
} . "]\"";
755 @endpoints = split /,/, $idl->{PROPERTIES
}->{endpoint
};
759 NAME
=> $idl->{NAME
},
760 UUID
=> lc(has_property
($idl, "uuid")),
763 PROPERTIES
=> $idl->{PROPERTIES
},
764 FUNCTIONS
=> \
@functions,
767 ENDPOINTS
=> \
@endpoints
771 # Convert a IDL tree to a NDR tree
772 # Gives a result tree describing all that's necessary for easily generating
773 # NDR parsers / generators
778 return undef unless (defined($idl));
780 Parse
::Pidl
::NDR
::Validate
($idl);
785 ($_->{TYPE
} eq "CPP_QUOTE") && push(@ndr, $_);
786 ($_->{TYPE
} eq "INTERFACE") && push(@ndr, ParseInterface
($_));
787 ($_->{TYPE
} eq "IMPORT") && push(@ndr, $_);
800 foreach my $l (@
{$e->{LEVELS
}}) {
801 return $l if ($seen);
802 ($seen = 1) if ($l == $fl);
813 foreach my $l (@
{$e->{LEVELS
}}) {
814 (return $prev) if ($l == $fl);
821 sub ContainsString
($)
825 foreach my $l (@
{$e->{LEVELS
}}) {
826 return 1 if ($l->{TYPE
} eq "ARRAY" and $l->{IS_ZERO_TERMINATED
});
832 sub ContainsDeferred
($$)
836 return 1 if ($l->{CONTAINS_DEFERRED
});
838 while ($l = GetNextLevel
($e,$l))
840 return 1 if ($l->{IS_DEFERRED
});
841 return 1 if ($l->{CONTAINS_DEFERRED
});
850 my $name = "<ANONYMOUS>";
852 $name = $e->{NAME
} if defined($e->{NAME
});
854 if (defined($e->{PARENT
}) and defined($e->{PARENT
}->{NAME
})) {
855 return "$e->{PARENT}->{NAME}.$name";
858 if (defined($e->{PARENT
}) and
859 defined($e->{PARENT
}->{PARENT
}) and
860 defined($e->{PARENT
}->{PARENT
}->{NAME
})) {
861 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
867 ###################################
868 # find a sibling var in a structure
872 my($fn) = $e->{PARENT
};
874 if ($name =~ /\*(.*)/) {
878 for my $e2 (@
{$fn->{ELEMENTS
}}) {
879 return $e2 if ($e2->{NAME
} eq $name);
885 my %property_list = (
887 "helpstring" => ["INTERFACE", "FUNCTION"],
888 "version" => ["INTERFACE"],
889 "uuid" => ["INTERFACE"],
890 "endpoint" => ["INTERFACE"],
891 "pointer_default" => ["INTERFACE"],
892 "helper" => ["INTERFACE"],
893 "pyhelper" => ["INTERFACE"],
894 "authservice" => ["INTERFACE"],
895 "restricted" => ["INTERFACE"],
896 "no_srv_register" => ["INTERFACE"],
899 "object" => ["INTERFACE"],
900 "local" => ["INTERFACE", "FUNCTION"],
901 "iid_is" => ["ELEMENT"],
902 "call_as" => ["FUNCTION"],
903 "idempotent" => ["FUNCTION"],
906 "noopnum" => ["FUNCTION"],
908 "out" => ["ELEMENT"],
911 "ref" => ["ELEMENT", "TYPEDEF"],
912 "ptr" => ["ELEMENT", "TYPEDEF"],
913 "unique" => ["ELEMENT", "TYPEDEF"],
914 "ignore" => ["ELEMENT"],
915 "relative" => ["ELEMENT", "TYPEDEF"],
916 "relative_short" => ["ELEMENT", "TYPEDEF"],
917 "null_is_ffffffff" => ["ELEMENT"],
918 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
920 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
921 "value" => ["ELEMENT"],
922 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
925 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
926 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
927 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
928 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
929 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
930 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
931 "todo" => ["FUNCTION"],
934 "switch_is" => ["ELEMENT"],
935 "switch_type" => ["ELEMENT", "UNION"],
936 "nodiscriminant" => ["UNION"],
937 "case" => ["ELEMENT"],
938 "default" => ["ELEMENT"],
940 "represent_as" => ["ELEMENT"],
941 "transmit_as" => ["ELEMENT"],
944 "subcontext" => ["ELEMENT"],
945 "subcontext_size" => ["ELEMENT"],
946 "compression" => ["ELEMENT"],
949 "enum8bit" => ["ENUM"],
950 "enum16bit" => ["ENUM"],
951 "v1_enum" => ["ENUM"],
954 "bitmap8bit" => ["BITMAP"],
955 "bitmap16bit" => ["BITMAP"],
956 "bitmap32bit" => ["BITMAP"],
957 "bitmap64bit" => ["BITMAP"],
960 "range" => ["ELEMENT", "PIPE"],
961 "size_is" => ["ELEMENT"],
962 "string" => ["ELEMENT"],
963 "noheader" => ["ELEMENT"],
964 "charset" => ["ELEMENT"],
965 "length_is" => ["ELEMENT"],
968 #####################################################################
969 # check for unknown properties
970 sub ValidProperties
($$)
974 return unless defined $e->{PROPERTIES
};
976 foreach my $key (keys %{$e->{PROPERTIES
}}) {
977 warning
($e, el_name
($e) . ": unknown property '$key'")
978 unless defined($property_list{$key});
980 fatal
($e, el_name
($e) . ": property '$key' not allowed on '$t'")
981 unless grep(/^$t$/, @
{$property_list{$key}});
989 return $t->{NAME
} if (ref($t) eq "HASH" and $t->{TYPE
} eq "SCALAR");
990 my $ti = getType
($t);
992 if (not defined ($ti)) {
994 } elsif ($ti->{TYPE
} eq "TYPEDEF") {
995 return mapToScalar
($ti->{DATA
});
996 } elsif ($ti->{TYPE
} eq "ENUM") {
997 return Parse
::Pidl
::Typelist
::enum_type_fn
($ti);
998 } elsif ($ti->{TYPE
} eq "BITMAP") {
999 return Parse
::Pidl
::Typelist
::bitmap_type_fn
($ti);
1005 #####################################################################
1006 # validate an element
1011 ValidProperties
($e,"ELEMENT");
1013 # Check whether switches are used correctly.
1014 if (my $switch = has_property
($e, "switch_is")) {
1015 my $e2 = find_sibling
($e, $switch);
1016 my $type = getType
($e->{TYPE
});
1018 if (defined($type) and $type->{DATA
}->{TYPE
} ne "UNION") {
1019 fatal
($e, el_name
($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1022 if (not has_property
($type->{DATA
}, "nodiscriminant") and defined($e2)) {
1023 my $discriminator_type = has_property
($type->{DATA
}, "switch_type");
1024 $discriminator_type = "uint32" unless defined ($discriminator_type);
1026 my $t1 = mapScalarType
(mapToScalar
($discriminator_type));
1028 if (not defined($t1)) {
1029 fatal
($e, el_name
($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1032 my $t2 = mapScalarType
(mapToScalar
($e2->{TYPE
}));
1033 if (not defined($t2)) {
1034 fatal
($e, el_name
($e) . ": unable to map variable used for switch_is() to scalar");
1038 warning
($e, el_name
($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1043 if (has_property
($e, "subcontext") and has_property
($e, "represent_as")) {
1044 fatal
($e, el_name
($e) . " : subcontext() and represent_as() can not be used on the same element");
1047 if (has_property
($e, "subcontext") and has_property
($e, "transmit_as")) {
1048 fatal
($e, el_name
($e) . " : subcontext() and transmit_as() can not be used on the same element");
1051 if (has_property
($e, "represent_as") and has_property
($e, "transmit_as")) {
1052 fatal
($e, el_name
($e) . " : represent_as() and transmit_as() can not be used on the same element");
1055 if (has_property
($e, "represent_as") and has_property
($e, "value")) {
1056 fatal
($e, el_name
($e) . " : represent_as() and value() can not be used on the same element");
1059 if (has_property
($e, "subcontext")) {
1060 warning
($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1063 if (defined (has_property
($e, "subcontext_size")) and not defined(has_property
($e, "subcontext"))) {
1064 fatal
($e, el_name
($e) . " : subcontext_size() on non-subcontext element");
1067 if (defined (has_property
($e, "compression")) and not defined(has_property
($e, "subcontext"))) {
1068 fatal
($e, el_name
($e) . " : compression() on non-subcontext element");
1071 if (!$e->{POINTERS
} && (
1072 has_property
($e, "ptr") or
1073 has_property
($e, "unique") or
1074 has_property
($e, "relative") or
1075 has_property
($e, "relative_short") or
1076 has_property
($e, "ref"))) {
1077 fatal
($e, el_name
($e) . " : pointer properties on non-pointer element\n");
1081 #####################################################################
1087 ValidProperties
($enum, "ENUM");
1090 #####################################################################
1096 ValidProperties
($bitmap, "BITMAP");
1099 #####################################################################
1103 my($struct) = shift;
1105 ValidProperties
($struct, "STRUCT");
1107 return unless defined($struct->{ELEMENTS
});
1109 foreach my $e (@
{$struct->{ELEMENTS
}}) {
1110 $e->{PARENT
} = $struct;
1115 #####################################################################
1121 ValidProperties
($union,"UNION");
1123 if (has_property
($union->{PARENT
}, "nodiscriminant") and
1124 has_property
($union->{PARENT
}, "switch_type")) {
1125 fatal
($union->{PARENT
}, $union->{PARENT
}->{NAME
} . ": switch_type(" . $union->{PARENT
}->{PROPERTIES
}->{switch_type
} . ") on union without discriminant");
1128 return unless defined($union->{ELEMENTS
});
1130 foreach my $e (@
{$union->{ELEMENTS
}}) {
1131 $e->{PARENT
} = $union;
1133 if (defined($e->{PROPERTIES
}->{default}) and
1134 defined($e->{PROPERTIES
}->{case
})) {
1135 fatal
($e, "Union member $e->{NAME} can not have both default and case properties!");
1138 unless (defined ($e->{PROPERTIES
}->{default}) or
1139 defined ($e->{PROPERTIES
}->{case
})) {
1140 fatal
($e, "Union member $e->{NAME} must have default or case property");
1143 if (has_property
($e, "ref")) {
1144 fatal
($e, el_name
($e) . ": embedded ref pointers are not supported yet\n");
1152 #####################################################################
1157 my $data = $pipe->{DATA
};
1159 ValidProperties
($pipe, "PIPE");
1161 fatal
($pipe, $pipe->{NAME
} . ": 'pipe' is not yet supported by pidl");
1164 #####################################################################
1168 my($typedef) = shift;
1169 my $data = $typedef->{DATA
};
1171 ValidProperties
($typedef, "TYPEDEF");
1173 return unless (ref($data) eq "HASH");
1175 $data->{PARENT
} = $typedef;
1177 $data->{FILE
} = $typedef->{FILE
} unless defined($data->{FILE
});
1178 $data->{LINE
} = $typedef->{LINE
} unless defined($data->{LINE
});
1183 #####################################################################
1184 # validate a function
1185 sub ValidFunction
($)
1189 ValidProperties
($fn,"FUNCTION");
1191 foreach my $e (@
{$fn->{ELEMENTS
}}) {
1193 if (has_property
($e, "ref") && !$e->{POINTERS
}) {
1194 fatal
($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1200 #####################################################################
1207 TYPEDEF
=> \
&ValidTypedef
,
1208 STRUCT
=> \
&ValidStruct
,
1209 UNION
=> \
&ValidUnion
,
1210 ENUM
=> \
&ValidEnum
,
1211 BITMAP
=> \
&ValidBitmap
,
1213 }->{$t->{TYPE
}}->($t);
1216 #####################################################################
1217 # parse the interface definitions
1218 sub ValidInterface
($)
1220 my($interface) = shift;
1221 my($data) = $interface->{DATA
};
1223 if (has_property
($interface, "helper")) {
1224 warning
($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1227 ValidProperties
($interface,"INTERFACE");
1229 if (has_property
($interface, "pointer_default")) {
1230 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1231 ("ref", "unique", "ptr"))) {
1232 fatal
($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1236 if (has_property
($interface, "object")) {
1237 if (has_property
($interface, "version") &&
1238 $interface->{PROPERTIES
}->{version
} != 0) {
1239 fatal
($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1242 if (!defined($interface->{BASE
}) &&
1243 not ($interface->{NAME
} eq "IUnknown")) {
1244 fatal
($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1248 foreach my $d (@
{$data}) {
1249 ($d->{TYPE
} eq "FUNCTION") && ValidFunction
($d);
1250 ($d->{TYPE
} eq "TYPEDEF" or
1251 $d->{TYPE
} eq "STRUCT" or
1252 $d->{TYPE
} eq "UNION" or
1253 $d->{TYPE
} eq "ENUM" or
1254 $d->{TYPE
} eq "BITMAP" or
1255 $d->{TYPE
} eq "PIPE") && ValidType
($d);
1260 #####################################################################
1261 # Validate an IDL structure
1266 foreach my $x (@
{$idl}) {
1267 ($x->{TYPE
} eq "INTERFACE") &&
1269 ($x->{TYPE
} eq "IMPORTLIB") &&
1270 fatal
($x, "importlib() not supported");
1274 sub is_charset_array
($$)
1278 return 0 if ($l->{TYPE
} ne "ARRAY");
1280 my $nl = GetNextLevel
($e,$l);
1282 return 0 unless ($nl->{TYPE
} eq "DATA");
1284 return has_property
($e, "charset");