Commit | Line | Data |
2304df62 |
1 | #!./miniperl |
75f92628 |
2 | |
3 | =head1 NAME |
4 | |
5 | xsubpp - compiler to convert Perl XS code into C code |
6 | |
7 | =head1 SYNOPSIS |
8 | |
b26a54d0 |
9 | B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs |
75f92628 |
10 | |
11 | =head1 DESCRIPTION |
12 | |
b26a54d0 |
13 | This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>. |
14 | |
75f92628 |
15 | I<xsubpp> will compile XS code into C code by embedding the constructs |
16 | necessary to let C functions manipulate Perl values and creates the glue |
17 | necessary to let Perl access those functions. The compiler uses typemaps to |
18 | determine how to map C function parameters and variables to Perl values. |
19 | |
20 | The compiler will search for typemap files called I<typemap>. It will use |
21 | the following search path to find default typemaps, with the rightmost |
22 | typemap taking precedence. |
23 | |
24 | ../../../typemap:../../typemap:../typemap:typemap |
25 | |
26 | =head1 OPTIONS |
27 | |
b26a54d0 |
28 | Note that the C<XSOPT> MakeMaker option may be used to add these options to |
29 | any makefiles generated by MakeMaker. |
30 | |
75f92628 |
31 | =over 5 |
32 | |
33 | =item B<-C++> |
34 | |
35 | Adds ``extern "C"'' to the C code. |
36 | |
75f92628 |
37 | =item B<-except> |
38 | |
39 | Adds exception handling stubs to the C code. |
40 | |
41 | =item B<-typemap typemap> |
42 | |
43 | Indicates that a user-supplied typemap should take precedence over the |
44 | default typemaps. This option may be used multiple times, with the last |
45 | typemap having the highest precedence. |
46 | |
8e07c86e |
47 | =item B<-v> |
48 | |
49 | Prints the I<xsubpp> version number to standard output, then exits. |
50 | |
8fc38fda |
51 | =item B<-prototypes> |
382b8d97 |
52 | |
8fc38fda |
53 | By default I<xsubpp> will not automatically generate prototype code for |
54 | all xsubs. This flag will enable prototypes. |
55 | |
56 | =item B<-noversioncheck> |
57 | |
58 | Disables the run time test that determines if the object file (derived |
59 | from the C<.xs> file) and the C<.pm> files have the same version |
60 | number. |
382b8d97 |
61 | |
6f1abe2b |
62 | =item B<-nolinenumbers> |
63 | |
64 | Prevents the inclusion of `#line' directives in the output. |
65 | |
b26a54d0 |
66 | =item B<-nooptimize> |
67 | |
68 | Disables certain optimizations. The only optimization that is currently |
69 | affected is the use of I<target>s by the output C code (see L<perlguts>). |
70 | This may significantly slow down the generated code, but this is the way |
71 | B<xsubpp> of 5.005 and earlier operated. |
72 | |
11416672 |
73 | =item B<-noinout> |
74 | |
75 | Disable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations. |
76 | |
77 | =item B<-noargtypes> |
78 | |
79 | Disable recognition of ANSI-like descriptions of function signature. |
80 | |
c5be433b |
81 | =back |
75f92628 |
82 | |
83 | =head1 ENVIRONMENT |
84 | |
85 | No environment variables are used. |
86 | |
87 | =head1 AUTHOR |
88 | |
89 | Larry Wall |
90 | |
f06db76b |
91 | =head1 MODIFICATION HISTORY |
92 | |
8e07c86e |
93 | See the file F<changes.pod>. |
e50aee73 |
94 | |
75f92628 |
95 | =head1 SEE ALSO |
96 | |
55a00e51 |
97 | perl(1), perlxs(1), perlxstut(1) |
75f92628 |
98 | |
99 | =cut |
93a17b20 |
100 | |
382b8d97 |
101 | require 5.002; |
774d564b |
102 | use Cwd; |
4230ab3f |
103 | use vars '$cplusplus'; |
7ad6fb0b |
104 | use vars '%v'; |
382b8d97 |
105 | |
01f988be |
106 | use Config; |
107 | |
aa689395 |
108 | sub Q ; |
109 | |
774d564b |
110 | # Global Constants |
774d564b |
111 | |
cfc02341 |
112 | $XSUBPP_version = "1.9507"; |
aa689395 |
113 | |
114 | my ($Is_VMS, $SymSet); |
115 | if ($^O eq 'VMS') { |
116 | $Is_VMS = 1; |
117 | # Establish set of global symbols with max length 28, since xsubpp |
118 | # will later add the 'XS_' prefix. |
119 | require ExtUtils::XSSymSet; |
120 | $SymSet = new ExtUtils::XSSymSet 28; |
121 | } |
8fc38fda |
122 | |
c07a80fd |
123 | $FH = 'File0000' ; |
8fc38fda |
124 | |
11416672 |
125 | $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n"; |
f06db76b |
126 | |
382b8d97 |
127 | $proto_re = "[" . quotemeta('\$%&*@;') . "]" ; |
01f988be |
128 | # mjn |
129 | $OBJ = 1 if $Config{'ccflags'} =~ /PERL_OBJECT/i; |
93a17b20 |
130 | |
8e07c86e |
131 | $except = ""; |
8fc38fda |
132 | $WantPrototypes = -1 ; |
133 | $WantVersionChk = 1 ; |
134 | $ProtoUsed = 0 ; |
6f1abe2b |
135 | $WantLineNumbers = 1 ; |
b26a54d0 |
136 | $WantOptimize = 1 ; |
11416672 |
137 | |
138 | my $process_inout = 1; |
139 | my $process_argtypes = 1; |
140 | |
8e07c86e |
141 | SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) { |
93a17b20 |
142 | $flag = shift @ARGV; |
e50aee73 |
143 | $flag =~ s/^-// ; |
ff68c719 |
144 | $spat = quotemeta shift, next SWITCH if $flag eq 's'; |
8990e307 |
145 | $cplusplus = 1, next SWITCH if $flag eq 'C++'; |
382b8d97 |
146 | $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes'; |
147 | $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes'; |
8fc38fda |
148 | $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck'; |
149 | $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck'; |
c5be433b |
150 | # XXX left this in for compat |
b207eff1 |
151 | $WantCAPI = 1, next SWITCH if $flag eq 'object_capi'; |
8e07c86e |
152 | $except = " TRY", next SWITCH if $flag eq 'except'; |
8990e307 |
153 | push(@tm,shift), next SWITCH if $flag eq 'typemap'; |
6f1abe2b |
154 | $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers'; |
155 | $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers'; |
b26a54d0 |
156 | $WantOptimize = 0, next SWITCH if $flag eq 'nooptimize'; |
157 | $WantOptimize = 1, next SWITCH if $flag eq 'optimize'; |
11416672 |
158 | $process_inout = 0, next SWITCH if $flag eq 'noinout'; |
159 | $process_inout = 1, next SWITCH if $flag eq 'inout'; |
160 | $process_argtypes = 0, next SWITCH if $flag eq 'noargtypes'; |
161 | $process_argtypes = 1, next SWITCH if $flag eq 'argtypes'; |
b26a54d0 |
162 | (print "xsubpp version $XSUBPP_version\n"), exit |
8e07c86e |
163 | if $flag eq 'v'; |
93a17b20 |
164 | die $usage; |
165 | } |
8fc38fda |
166 | if ($WantPrototypes == -1) |
167 | { $WantPrototypes = 0} |
168 | else |
169 | { $ProtoUsed = 1 } |
170 | |
171 | |
8990e307 |
172 | @ARGV == 1 or die $usage; |
c2960299 |
173 | ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)# |
57497940 |
174 | or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)# |
c2960299 |
175 | or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)# |
8990e307 |
176 | or ($dir, $filename) = ('.', $ARGV[0]); |
177 | chdir($dir); |
774d564b |
178 | $pwd = cwd(); |
8fc38fda |
179 | |
180 | ++ $IncludedFiles{$ARGV[0]} ; |
93a17b20 |
181 | |
4230ab3f |
182 | my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs |
183 | my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA"); |
aa689395 |
184 | |
4230ab3f |
185 | |
f06db76b |
186 | sub TrimWhitespace |
187 | { |
188 | $_[0] =~ s/^\s+|\s+$//go ; |
189 | } |
190 | |
191 | sub TidyType |
192 | { |
193 | local ($_) = @_ ; |
194 | |
195 | # rationalise any '*' by joining them into bunches and removing whitespace |
196 | s#\s*(\*+)\s*#$1#g; |
e50aee73 |
197 | s#(\*+)# $1 #g ; |
f06db76b |
198 | |
199 | # change multiple whitespace into a single space |
200 | s/\s+/ /g ; |
201 | |
202 | # trim leading & trailing whitespace |
203 | TrimWhitespace($_) ; |
204 | |
205 | $_ ; |
206 | } |
207 | |
93a17b20 |
208 | $typemap = shift @ARGV; |
8990e307 |
209 | foreach $typemap (@tm) { |
210 | die "Can't find $typemap in $pwd\n" unless -r $typemap; |
93a17b20 |
211 | } |
748a9306 |
212 | unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap |
213 | ../../lib/ExtUtils/typemap ../../../typemap ../../typemap |
214 | ../typemap typemap); |
8990e307 |
215 | foreach $typemap (@tm) { |
f06db76b |
216 | next unless -e $typemap ; |
217 | # skip directories, binary files etc. |
218 | warn("Warning: ignoring non-text typemap file '$typemap'\n"), next |
219 | unless -T $typemap ; |
220 | open(TYPEMAP, $typemap) |
221 | or warn ("Warning: could not open typemap file '$typemap': $!\n"), next; |
e50aee73 |
222 | $mode = 'Typemap'; |
c2960299 |
223 | $junk = "" ; |
8990e307 |
224 | $current = \$junk; |
225 | while (<TYPEMAP>) { |
e50aee73 |
226 | next if /^\s*#/; |
e1f0c0aa |
227 | my $line_no = $. + 1; |
8e07c86e |
228 | if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; } |
229 | if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; } |
230 | if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; } |
e50aee73 |
231 | if ($mode eq 'Typemap') { |
232 | chomp; |
f06db76b |
233 | my $line = $_ ; |
234 | TrimWhitespace($_) ; |
235 | # skip blank lines and comment lines |
236 | next if /^$/ or /^#/ ; |
382b8d97 |
237 | my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or |
238 | warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next; |
239 | $type = TidyType($type) ; |
240 | $type_kind{$type} = $kind ; |
241 | # prototype defaults to '$' |
93d3b392 |
242 | $proto = "\$" unless $proto ; |
382b8d97 |
243 | warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") |
244 | unless ValidProtoString($proto) ; |
245 | $proto_letter{$type} = C_string($proto) ; |
8e07c86e |
246 | } |
247 | elsif (/^\s/) { |
248 | $$current .= $_; |
463ee0b2 |
249 | } |
e50aee73 |
250 | elsif ($mode eq 'Input') { |
8e07c86e |
251 | s/\s+$//; |
252 | $input_expr{$_} = ''; |
253 | $current = \$input_expr{$_}; |
93a17b20 |
254 | } |
8990e307 |
255 | else { |
8e07c86e |
256 | s/\s+$//; |
257 | $output_expr{$_} = ''; |
258 | $current = \$output_expr{$_}; |
93a17b20 |
259 | } |
8990e307 |
260 | } |
261 | close(TYPEMAP); |
262 | } |
93a17b20 |
263 | |
8990e307 |
264 | foreach $key (keys %input_expr) { |
265 | $input_expr{$key} =~ s/\n+$//; |
266 | } |
93a17b20 |
267 | |
14455d6c |
268 | $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced |
b26a54d0 |
269 | $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast |
14455d6c |
270 | $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn) |
b26a54d0 |
271 | |
272 | foreach $key (keys %output_expr) { |
273 | use re 'eval'; |
274 | |
275 | my ($t, $with_size, $arg, $sarg) = |
276 | ($output_expr{$key} =~ |
277 | m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn |
278 | \s* \( \s* $cast \$arg \s* , |
14455d6c |
279 | \s* ( (??{ $bal }) ) # Set from |
280 | ( (??{ $size }) )? # Possible sizeof set-from |
b26a54d0 |
281 | \) \s* ; \s* $ |
282 | ]x); |
283 | $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t; |
284 | } |
285 | |
8e07c86e |
286 | $END = "!End!\n\n"; # "impossible" keyword (multiple newline) |
287 | |
288 | # Match an XS keyword |
382b8d97 |
289 | $BLOCK_re= '\s*(' . join('|', qw( |
290 | REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT |
be3174d2 |
291 | CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE |
63385af5 |
292 | SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL |
382b8d97 |
293 | )) . "|$END)\\s*:"; |
8e07c86e |
294 | |
295 | # Input: ($_, @line) == unparsed input. |
296 | # Output: ($_, @line) == (rest of line, following lines). |
297 | # Return: the matched keyword if found, otherwise 0 |
298 | sub check_keyword { |
299 | $_ = shift(@line) while !/\S/ && @line; |
300 | s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2; |
301 | } |
302 | |
f8b8e0a4 |
303 | my ($C_group_rex, $C_arg); |
304 | # Group in C (no support for comments or literals) |
305 | $C_group_rex = qr/ [({\[] |
14455d6c |
306 | (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )* |
f8b8e0a4 |
307 | [)}\]] /x ; |
308 | # Chunk in C without comma at toplevel (no comments): |
309 | $C_arg = qr/ (?: (?> [^()\[\]{},"']+ ) |
14455d6c |
310 | | (??{ $C_group_rex }) |
f8b8e0a4 |
311 | | " (?: (?> [^\\"]+ ) |
312 | | \\. |
313 | )* " # String literal |
314 | | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal |
315 | )* /xs; |
8e07c86e |
316 | |
6f1abe2b |
317 | if ($WantLineNumbers) { |
318 | { |
319 | package xsubpp::counter; |
320 | sub TIEHANDLE { |
321 | my ($class, $cfile) = @_; |
322 | my $buf = ""; |
323 | $SECTION_END_MARKER = "#line --- \"$cfile\""; |
324 | $line_no = 1; |
325 | bless \$buf; |
326 | } |
327 | |
328 | sub PRINT { |
329 | my $self = shift; |
330 | for (@_) { |
331 | $$self .= $_; |
332 | while ($$self =~ s/^([^\n]*\n)//) { |
333 | my $line = $1; |
334 | ++ $line_no; |
335 | $line =~ s|^\#line\s+---(?=\s)|#line $line_no|; |
336 | print STDOUT $line; |
337 | } |
338 | } |
339 | } |
340 | |
341 | sub PRINTF { |
342 | my $self = shift; |
343 | my $fmt = shift; |
344 | $self->PRINT(sprintf($fmt, @_)); |
345 | } |
346 | |
347 | sub DESTROY { |
348 | # Not necessary if we're careful to end with a "\n" |
349 | my $self = shift; |
350 | print STDOUT $$self; |
351 | } |
352 | } |
353 | |
354 | my $cfile = $filename; |
355 | $cfile =~ s/\.xs$/.c/i or $cfile .= ".c"; |
356 | tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile); |
357 | select PSEUDO_STDOUT; |
358 | } |
359 | |
8e07c86e |
360 | sub print_section { |
6f1abe2b |
361 | # the "do" is required for right semantics |
362 | do { $_ = shift(@line) } while !/\S/ && @line; |
363 | |
364 | print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n") |
d3308daf |
365 | if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/; |
8e07c86e |
366 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { |
367 | print "$_\n"; |
368 | } |
6f1abe2b |
369 | print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers; |
8e07c86e |
370 | } |
371 | |
cfc02341 |
372 | sub merge_section { |
373 | my $in = ''; |
374 | |
375 | while (!/\S/ && @line) { |
376 | $_ = shift(@line); |
377 | } |
378 | |
379 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { |
380 | $in .= "$_\n"; |
381 | } |
382 | chomp $in; |
383 | return $in; |
384 | } |
385 | |
8fc38fda |
386 | sub process_keyword($) |
387 | { |
388 | my($pattern) = @_ ; |
389 | my $kwd ; |
390 | |
391 | &{"${kwd}_handler"}() |
392 | while $kwd = check_keyword($pattern) ; |
393 | } |
394 | |
8e07c86e |
395 | sub CASE_handler { |
396 | blurt ("Error: `CASE:' after unconditional `CASE:'") |
397 | if $condnum && $cond eq ''; |
398 | $cond = $_; |
399 | TrimWhitespace($cond); |
400 | print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n"); |
401 | $_ = '' ; |
402 | } |
403 | |
404 | sub INPUT_handler { |
405 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
406 | last if /^\s*NOT_IMPLEMENTED_YET/; |
407 | next unless /\S/; # skip blank lines |
408 | |
409 | TrimWhitespace($_) ; |
410 | my $line = $_ ; |
411 | |
412 | # remove trailing semicolon if no initialisation |
7ad6fb0b |
413 | s/\s*;$//g unless /[=;+].*\S/ ; |
8e07c86e |
414 | |
415 | # check for optional initialisation code |
416 | my $var_init = '' ; |
7ad6fb0b |
417 | $var_init = $1 if s/\s*([=;+].*)$//s ; |
8e07c86e |
418 | $var_init =~ s/"/\\"/g; |
419 | |
420 | s/\s+/ /g; |
421 | my ($var_type, $var_addr, $var_name) = /^(.*?[^& ]) *(\&?) *\b(\w+)$/s |
422 | or blurt("Error: invalid argument declaration '$line'"), next; |
423 | |
424 | # Check for duplicate definitions |
425 | blurt ("Error: duplicate definition of argument '$var_name' ignored"), next |
f8b8e0a4 |
426 | if $arg_list{$var_name}++ |
427 | or defined $arg_types{$var_name} and not $processing_arg_with_types; |
8e07c86e |
428 | |
429 | $thisdone |= $var_name eq "THIS"; |
430 | $retvaldone |= $var_name eq "RETVAL"; |
431 | $var_types{$var_name} = $var_type; |
ddf6bed1 |
432 | # XXXX This check is a safeguard against the unfinished conversion of |
433 | # generate_init(). When generate_init() is fixed, |
434 | # one can use 2-args map_type() unconditionally. |
435 | if ($var_type =~ / \( \s* \* \s* \) /x) { |
436 | # Function pointers are not yet supported with &output_init! |
437 | print "\t" . &map_type($var_type, $var_name); |
438 | $name_printed = 1; |
439 | } else { |
440 | print "\t" . &map_type($var_type); |
441 | $name_printed = 0; |
442 | } |
8e07c86e |
443 | $var_num = $args_match{$var_name}; |
382b8d97 |
444 | |
8fc38fda |
445 | $proto_arg[$var_num] = ProtoString($var_type) |
446 | if $var_num ; |
8e07c86e |
447 | if ($var_addr) { |
448 | $var_addr{$var_name} = 1; |
449 | $func_args =~ s/\b($var_name)\b/&$1/; |
450 | } |
f8b8e0a4 |
451 | if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/ |
63385af5 |
452 | or $in_out{$var_name} and $in_out{$var_name} eq 'OUTLIST' |
f8b8e0a4 |
453 | and $var_init !~ /\S/) { |
ddf6bed1 |
454 | if ($name_printed) { |
455 | print ";\n"; |
456 | } else { |
9bea678f |
457 | print "\t$var_name;\n"; |
ddf6bed1 |
458 | } |
8e07c86e |
459 | } elsif ($var_init =~ /\S/) { |
ddf6bed1 |
460 | &output_init($var_type, $var_num, $var_name, $var_init, $name_printed); |
8e07c86e |
461 | } elsif ($var_num) { |
462 | # generate initialization code |
ddf6bed1 |
463 | &generate_init($var_type, $var_num, $var_name, $name_printed); |
8e07c86e |
464 | } else { |
465 | print ";\n"; |
466 | } |
467 | } |
468 | } |
469 | |
470 | sub OUTPUT_handler { |
471 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
472 | next unless /\S/; |
ef50df4b |
473 | if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) { |
474 | $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0); |
475 | next; |
476 | } |
8e07c86e |
477 | my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ; |
478 | blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next |
479 | if $outargs{$outarg} ++ ; |
480 | if (!$gotRETVAL and $outarg eq 'RETVAL') { |
481 | # deal with RETVAL last |
482 | $RETVAL_code = $outcode ; |
483 | $gotRETVAL = 1 ; |
484 | next ; |
485 | } |
486 | blurt ("Error: OUTPUT $outarg not an argument"), next |
487 | unless defined($args_match{$outarg}); |
488 | blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next |
489 | unless defined $var_types{$outarg} ; |
f78230ad |
490 | $var_num = $args_match{$outarg}; |
8e07c86e |
491 | if ($outcode) { |
492 | print "\t$outcode\n"; |
f78230ad |
493 | print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic; |
8e07c86e |
494 | } else { |
ef50df4b |
495 | &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic); |
8e07c86e |
496 | } |
497 | } |
498 | } |
499 | |
cfc02341 |
500 | sub C_ARGS_handler() { |
501 | my $in = merge_section(); |
502 | |
503 | TrimWhitespace($in); |
504 | $func_args = $in; |
505 | } |
506 | |
507 | sub INTERFACE_MACRO_handler() { |
508 | my $in = merge_section(); |
509 | |
510 | TrimWhitespace($in); |
511 | if ($in =~ /\s/) { # two |
512 | ($interface_macro, $interface_macro_set) = split ' ', $in; |
513 | } else { |
514 | $interface_macro = $in; |
515 | $interface_macro_set = 'UNKNOWN_CVT'; # catch later |
516 | } |
517 | $interface = 1; # local |
518 | $Interfaces = 1; # global |
519 | } |
520 | |
521 | sub INTERFACE_handler() { |
522 | my $in = merge_section(); |
523 | |
524 | TrimWhitespace($in); |
525 | |
526 | foreach (split /[\s,]+/, $in) { |
527 | $Interfaces{$_} = $_; |
528 | } |
529 | print Q<<"EOF"; |
530 | # XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr); |
531 | EOF |
532 | $interface = 1; # local |
533 | $Interfaces = 1; # global |
534 | } |
535 | |
8fc38fda |
536 | sub CLEANUP_handler() { print_section() } |
537 | sub PREINIT_handler() { print_section() } |
63385af5 |
538 | sub POSTCALL_handler() { print_section() } |
8fc38fda |
539 | sub INIT_handler() { print_section() } |
540 | |
8e07c86e |
541 | sub GetAliases |
542 | { |
543 | my ($line) = @_ ; |
544 | my ($orig) = $line ; |
545 | my ($alias) ; |
546 | my ($value) ; |
547 | |
548 | # Parse alias definitions |
549 | # format is |
550 | # alias = value alias = value ... |
551 | |
552 | while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) { |
553 | $alias = $1 ; |
554 | $orig_alias = $alias ; |
555 | $value = $2 ; |
556 | |
557 | # check for optional package definition in the alias |
558 | $alias = $Packprefix . $alias if $alias !~ /::/ ; |
559 | |
560 | # check for duplicate alias name & duplicate value |
561 | Warn("Warning: Ignoring duplicate alias '$orig_alias'") |
4230ab3f |
562 | if defined $XsubAliases{$alias} ; |
8e07c86e |
563 | |
4230ab3f |
564 | Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values") |
565 | if $XsubAliasValues{$value} ; |
8e07c86e |
566 | |
4230ab3f |
567 | $XsubAliases = 1; |
568 | $XsubAliases{$alias} = $value ; |
569 | $XsubAliasValues{$value} = $orig_alias ; |
8e07c86e |
570 | } |
571 | |
572 | blurt("Error: Cannot parse ALIAS definitions from '$orig'") |
573 | if $line ; |
574 | } |
575 | |
be3174d2 |
576 | sub ATTRS_handler () |
577 | { |
578 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
579 | next unless /\S/; |
580 | TrimWhitespace($_) ; |
581 | push @Attributes, $_; |
582 | } |
583 | } |
584 | |
382b8d97 |
585 | sub ALIAS_handler () |
8e07c86e |
586 | { |
587 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
588 | next unless /\S/; |
589 | TrimWhitespace($_) ; |
590 | GetAliases($_) if $_ ; |
591 | } |
592 | } |
593 | |
382b8d97 |
594 | sub REQUIRE_handler () |
8e07c86e |
595 | { |
596 | # the rest of the current line should contain a version number |
597 | my ($Ver) = $_ ; |
598 | |
599 | TrimWhitespace($Ver) ; |
600 | |
601 | death ("Error: REQUIRE expects a version number") |
602 | unless $Ver ; |
603 | |
604 | # check that the version number is of the form n.n |
605 | death ("Error: REQUIRE: expected a number, got '$Ver'") |
606 | unless $Ver =~ /^\d+(\.\d*)?/ ; |
607 | |
608 | death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.") |
609 | unless $XSUBPP_version >= $Ver ; |
610 | } |
611 | |
8fc38fda |
612 | sub VERSIONCHECK_handler () |
613 | { |
614 | # the rest of the current line should contain either ENABLE or |
615 | # DISABLE |
616 | |
617 | TrimWhitespace($_) ; |
618 | |
619 | # check for ENABLE/DISABLE |
620 | death ("Error: VERSIONCHECK: ENABLE/DISABLE") |
621 | unless /^(ENABLE|DISABLE)/i ; |
622 | |
623 | $WantVersionChk = 1 if $1 eq 'ENABLE' ; |
624 | $WantVersionChk = 0 if $1 eq 'DISABLE' ; |
625 | |
626 | } |
627 | |
382b8d97 |
628 | sub PROTOTYPE_handler () |
629 | { |
7d41bd0a |
630 | my $specified ; |
631 | |
c07a80fd |
632 | death("Error: Only 1 PROTOTYPE definition allowed per xsub") |
633 | if $proto_in_this_xsub ++ ; |
634 | |
382b8d97 |
635 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
636 | next unless /\S/; |
7d41bd0a |
637 | $specified = 1 ; |
382b8d97 |
638 | TrimWhitespace($_) ; |
639 | if ($_ eq 'DISABLE') { |
640 | $ProtoThisXSUB = 0 |
641 | } |
642 | elsif ($_ eq 'ENABLE') { |
643 | $ProtoThisXSUB = 1 |
644 | } |
645 | else { |
646 | # remove any whitespace |
647 | s/\s+//g ; |
648 | death("Error: Invalid prototype '$_'") |
649 | unless ValidProtoString($_) ; |
650 | $ProtoThisXSUB = C_string($_) ; |
651 | } |
652 | } |
c07a80fd |
653 | |
7d41bd0a |
654 | # If no prototype specified, then assume empty prototype "" |
655 | $ProtoThisXSUB = 2 unless $specified ; |
656 | |
8fc38fda |
657 | $ProtoUsed = 1 ; |
c07a80fd |
658 | |
382b8d97 |
659 | } |
660 | |
db3b9414 |
661 | sub SCOPE_handler () |
662 | { |
663 | death("Error: Only 1 SCOPE declaration allowed per xsub") |
664 | if $scope_in_this_xsub ++ ; |
665 | |
666 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
667 | next unless /\S/; |
668 | TrimWhitespace($_) ; |
669 | if ($_ =~ /^DISABLE/i) { |
670 | $ScopeThisXSUB = 0 |
671 | } |
672 | elsif ($_ =~ /^ENABLE/i) { |
673 | $ScopeThisXSUB = 1 |
674 | } |
675 | } |
676 | |
677 | } |
678 | |
382b8d97 |
679 | sub PROTOTYPES_handler () |
680 | { |
681 | # the rest of the current line should contain either ENABLE or |
682 | # DISABLE |
683 | |
684 | TrimWhitespace($_) ; |
685 | |
686 | # check for ENABLE/DISABLE |
687 | death ("Error: PROTOTYPES: ENABLE/DISABLE") |
688 | unless /^(ENABLE|DISABLE)/i ; |
689 | |
690 | $WantPrototypes = 1 if $1 eq 'ENABLE' ; |
691 | $WantPrototypes = 0 if $1 eq 'DISABLE' ; |
8fc38fda |
692 | $ProtoUsed = 1 ; |
382b8d97 |
693 | |
694 | } |
695 | |
8fc38fda |
696 | sub INCLUDE_handler () |
697 | { |
698 | # the rest of the current line should contain a valid filename |
699 | |
700 | TrimWhitespace($_) ; |
701 | |
8fc38fda |
702 | death("INCLUDE: filename missing") |
703 | unless $_ ; |
704 | |
705 | death("INCLUDE: output pipe is illegal") |
706 | if /^\s*\|/ ; |
707 | |
708 | # simple minded recursion detector |
709 | death("INCLUDE loop detected") |
710 | if $IncludedFiles{$_} ; |
711 | |
712 | ++ $IncludedFiles{$_} unless /\|\s*$/ ; |
713 | |
714 | # Save the current file context. |
4230ab3f |
715 | push(@XSStack, { |
716 | type => 'file', |
8fc38fda |
717 | LastLine => $lastline, |
718 | LastLineNo => $lastline_no, |
719 | Line => \@line, |
720 | LineNo => \@line_no, |
721 | Filename => $filename, |
c07a80fd |
722 | Handle => $FH, |
8fc38fda |
723 | }) ; |
724 | |
c07a80fd |
725 | ++ $FH ; |
8fc38fda |
726 | |
727 | # open the new file |
c07a80fd |
728 | open ($FH, "$_") or death("Cannot open '$_': $!") ; |
8fc38fda |
729 | |
730 | print Q<<"EOF" ; |
731 | # |
732 | #/* INCLUDE: Including '$_' from '$filename' */ |
733 | # |
734 | EOF |
735 | |
8fc38fda |
736 | $filename = $_ ; |
737 | |
c07a80fd |
738 | # Prime the pump by reading the first |
739 | # non-blank line |
740 | |
741 | # skip leading blank lines |
742 | while (<$FH>) { |
743 | last unless /^\s*$/ ; |
744 | } |
745 | |
746 | $lastline = $_ ; |
8fc38fda |
747 | $lastline_no = $. ; |
748 | |
749 | } |
750 | |
751 | sub PopFile() |
752 | { |
4230ab3f |
753 | return 0 unless $XSStack[-1]{type} eq 'file' ; |
754 | |
755 | my $data = pop @XSStack ; |
8fc38fda |
756 | my $ThisFile = $filename ; |
757 | my $isPipe = ($filename =~ /\|\s*$/) ; |
758 | |
759 | -- $IncludedFiles{$filename} |
760 | unless $isPipe ; |
761 | |
c07a80fd |
762 | close $FH ; |
8fc38fda |
763 | |
c07a80fd |
764 | $FH = $data->{Handle} ; |
8fc38fda |
765 | $filename = $data->{Filename} ; |
766 | $lastline = $data->{LastLine} ; |
767 | $lastline_no = $data->{LastLineNo} ; |
768 | @line = @{ $data->{Line} } ; |
769 | @line_no = @{ $data->{LineNo} } ; |
4230ab3f |
770 | |
8fc38fda |
771 | if ($isPipe and $? ) { |
772 | -- $lastline_no ; |
773 | print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ; |
774 | exit 1 ; |
775 | } |
776 | |
777 | print Q<<"EOF" ; |
778 | # |
779 | #/* INCLUDE: Returning to '$filename' from '$ThisFile' */ |
780 | # |
781 | EOF |
782 | |
783 | return 1 ; |
784 | } |
785 | |
382b8d97 |
786 | sub ValidProtoString ($) |
787 | { |
788 | my($string) = @_ ; |
789 | |
790 | if ( $string =~ /^$proto_re+$/ ) { |
791 | return $string ; |
792 | } |
793 | |
794 | return 0 ; |
795 | } |
796 | |
797 | sub C_string ($) |
798 | { |
799 | my($string) = @_ ; |
800 | |
801 | $string =~ s[\\][\\\\]g ; |
802 | $string ; |
803 | } |
804 | |
805 | sub ProtoString ($) |
806 | { |
807 | my ($type) = @_ ; |
808 | |
93d3b392 |
809 | $proto_letter{$type} or "\$" ; |
382b8d97 |
810 | } |
811 | |
8e07c86e |
812 | sub check_cpp { |
813 | my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line); |
814 | if (@cpp) { |
815 | my ($cpp, $cpplevel); |
816 | for $cpp (@cpp) { |
817 | if ($cpp =~ /^\#\s*if/) { |
818 | $cpplevel++; |
819 | } elsif (!$cpplevel) { |
820 | Warn("Warning: #else/elif/endif without #if in this function"); |
4230ab3f |
821 | print STDERR " (precede it with a blank line if the matching #if is outside the function)\n" |
822 | if $XSStack[-1]{type} eq 'if'; |
8e07c86e |
823 | return; |
824 | } elsif ($cpp =~ /^\#\s*endif/) { |
825 | $cpplevel--; |
826 | } |
827 | } |
828 | Warn("Warning: #if without #endif in this function") if $cpplevel; |
829 | } |
830 | } |
831 | |
832 | |
8990e307 |
833 | sub Q { |
e50aee73 |
834 | my($text) = @_; |
4633a7c4 |
835 | $text =~ s/^#//gm; |
2304df62 |
836 | $text =~ s/\[\[/{/g; |
837 | $text =~ s/\]\]/}/g; |
8990e307 |
838 | $text; |
93a17b20 |
839 | } |
840 | |
c07a80fd |
841 | open($FH, $filename) or die "cannot open $filename: $!\n"; |
c2960299 |
842 | |
f06db76b |
843 | # Identify the version of xsubpp used |
f06db76b |
844 | print <<EOM ; |
e50aee73 |
845 | /* |
846 | * This file was generated automatically by xsubpp version $XSUBPP_version from the |
93d3b392 |
847 | * contents of $filename. Do not edit this file, edit $filename instead. |
e50aee73 |
848 | * |
849 | * ANY CHANGES MADE HERE WILL BE LOST! |
f06db76b |
850 | * |
851 | */ |
e50aee73 |
852 | |
f06db76b |
853 | EOM |
6f1abe2b |
854 | |
855 | |
856 | print("#line 1 \"$filename\"\n") |
857 | if $WantLineNumbers; |
f06db76b |
858 | |
e03d20b3 |
859 | firstmodule: |
c07a80fd |
860 | while (<$FH>) { |
e03d20b3 |
861 | if (/^=/) { |
862 | do { |
863 | next firstmodule if /^=cut\s*$/; |
864 | } while (<$FH>); |
865 | &Exit; |
866 | } |
e50aee73 |
867 | last if ($Module, $Package, $Prefix) = |
868 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/; |
01f988be |
869 | |
870 | if ($OBJ) { |
b85ee828 |
871 | s/#if(?:def\s|\s+defined)\s*(\(__cplusplus\)|__cplusplus)/#if defined(__cplusplus) && !defined(PERL_OBJECT)/; |
01f988be |
872 | } |
a0d0e21e |
873 | print $_; |
93a17b20 |
874 | } |
e50aee73 |
875 | &Exit unless defined $_; |
876 | |
cfc02341 |
877 | print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers; |
878 | |
8fc38fda |
879 | $lastline = $_; |
880 | $lastline_no = $.; |
93a17b20 |
881 | |
c07a80fd |
882 | # Read next xsub into @line from ($lastline, <$FH>). |
2304df62 |
883 | sub fetch_para { |
884 | # parse paragraph |
4230ab3f |
885 | death ("Error: Unterminated `#if/#ifdef/#ifndef'") |
886 | if !defined $lastline && $XSStack[-1]{type} eq 'if'; |
2304df62 |
887 | @line = (); |
c2960299 |
888 | @line_no = () ; |
4230ab3f |
889 | return PopFile() if !defined $lastline; |
e50aee73 |
890 | |
891 | if ($lastline =~ |
892 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) { |
893 | $Module = $1; |
8e07c86e |
894 | $Package = defined($2) ? $2 : ''; # keep -w happy |
895 | $Prefix = defined($3) ? $3 : ''; # keep -w happy |
ff68c719 |
896 | $Prefix = quotemeta $Prefix ; |
e50aee73 |
897 | ($Module_cname = $Module) =~ s/\W/_/g; |
8e07c86e |
898 | ($Packid = $Package) =~ tr/:/_/; |
e50aee73 |
899 | $Packprefix = $Package; |
8e07c86e |
900 | $Packprefix .= "::" if $Packprefix ne ""; |
2304df62 |
901 | $lastline = ""; |
e50aee73 |
902 | } |
903 | |
904 | for(;;) { |
e03d20b3 |
905 | # Skip embedded PODs |
413e5597 |
906 | while ($lastline =~ /^=/) { |
e03d20b3 |
907 | while ($lastline = <$FH>) { |
908 | last if ($lastline =~ /^=cut\s*$/); |
909 | } |
910 | death ("Error: Unterminated pod") unless $lastline; |
911 | $lastline = <$FH>; |
912 | chomp $lastline; |
913 | $lastline =~ s/^\s+$//; |
914 | } |
e50aee73 |
915 | if ($lastline !~ /^\s*#/ || |
4230ab3f |
916 | # CPP directives: |
917 | # ANSI: if ifdef ifndef elif else endif define undef |
918 | # line error pragma |
919 | # gcc: warning include_next |
920 | # obj-c: import |
921 | # others: ident (gcc notes that some cpps have this one) |
922 | $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) { |
e50aee73 |
923 | last if $lastline =~ /^\S/ && @line && $line[-1] eq ""; |
924 | push(@line, $lastline); |
925 | push(@line_no, $lastline_no) ; |
93a17b20 |
926 | } |
e50aee73 |
927 | |
928 | # Read next line and continuation lines |
c07a80fd |
929 | last unless defined($lastline = <$FH>); |
e50aee73 |
930 | $lastline_no = $.; |
931 | my $tmp_line; |
932 | $lastline .= $tmp_line |
c07a80fd |
933 | while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>)); |
f8b8e0a4 |
934 | |
8e07c86e |
935 | chomp $lastline; |
e50aee73 |
936 | $lastline =~ s/^\s+$//; |
2304df62 |
937 | } |
e50aee73 |
938 | pop(@line), pop(@line_no) while @line && $line[-1] eq ""; |
e50aee73 |
939 | 1; |
2304df62 |
940 | } |
93a17b20 |
941 | |
c2960299 |
942 | PARAGRAPH: |
8e07c86e |
943 | while (fetch_para()) { |
e50aee73 |
944 | # Print initial preprocessor statements and blank lines |
4230ab3f |
945 | while (@line && $line[0] !~ /^[^\#]/) { |
946 | my $line = shift(@line); |
947 | print $line, "\n"; |
948 | next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/; |
949 | my $statement = $+; |
950 | if ($statement eq 'if') { |
951 | $XSS_work_idx = @XSStack; |
952 | push(@XSStack, {type => 'if'}); |
953 | } else { |
954 | death ("Error: `$statement' with no matching `if'") |
955 | if $XSStack[-1]{type} ne 'if'; |
956 | if ($XSStack[-1]{varname}) { |
957 | push(@InitFileCode, "#endif\n"); |
958 | push(@BootCode, "#endif"); |
959 | } |
960 | |
961 | my(@fns) = keys %{$XSStack[-1]{functions}}; |
962 | if ($statement ne 'endif') { |
963 | # Hide the functions defined in other #if branches, and reset. |
964 | @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns; |
965 | @{$XSStack[-1]}{qw(varname functions)} = ('', {}); |
966 | } else { |
967 | my($tmp) = pop(@XSStack); |
968 | 0 while (--$XSS_work_idx |
969 | && $XSStack[$XSS_work_idx]{type} ne 'if'); |
970 | # Keep all new defined functions |
971 | push(@fns, keys %{$tmp->{other_functions}}); |
972 | @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns; |
973 | } |
974 | } |
975 | } |
e50aee73 |
976 | |
977 | next PARAGRAPH unless @line; |
978 | |
4230ab3f |
979 | if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) { |
980 | # We are inside an #if, but have not yet #defined its xsubpp variable. |
981 | print "#define $cpp_next_tmp 1\n\n"; |
982 | push(@InitFileCode, "#if $cpp_next_tmp\n"); |
983 | push(@BootCode, "#if $cpp_next_tmp"); |
984 | $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++; |
985 | } |
986 | |
55497cff |
987 | death ("Code is not inside a function" |
988 | ." (maybe last function was ended by a blank line " |
989 | ." followed by a a statement on column one?)") |
e50aee73 |
990 | if $line[0] =~ /^\s/; |
991 | |
2304df62 |
992 | # initialize info arrays |
993 | undef(%args_match); |
994 | undef(%var_types); |
995 | undef(%var_addr); |
996 | undef(%defaults); |
997 | undef($class); |
998 | undef($static); |
999 | undef($elipsis); |
f06db76b |
1000 | undef($wantRETVAL) ; |
f8b8e0a4 |
1001 | undef($RETVAL_no_return) ; |
f06db76b |
1002 | undef(%arg_list) ; |
382b8d97 |
1003 | undef(@proto_arg) ; |
f8b8e0a4 |
1004 | undef(@arg_with_types) ; |
1005 | undef($processing_arg_with_types) ; |
1006 | undef(%arg_types) ; |
1007 | undef(@in_out) ; |
1008 | undef(%in_out) ; |
c07a80fd |
1009 | undef($proto_in_this_xsub) ; |
db3b9414 |
1010 | undef($scope_in_this_xsub) ; |
cfc02341 |
1011 | undef($interface); |
f8b8e0a4 |
1012 | undef($prepush_done); |
cfc02341 |
1013 | $interface_macro = 'XSINTERFACE_FUNC' ; |
1014 | $interface_macro_set = 'XSINTERFACE_FUNC_SET' ; |
382b8d97 |
1015 | $ProtoThisXSUB = $WantPrototypes ; |
db3b9414 |
1016 | $ScopeThisXSUB = 0; |
f8b8e0a4 |
1017 | $xsreturn = 0; |
2304df62 |
1018 | |
8e07c86e |
1019 | $_ = shift(@line); |
8fc38fda |
1020 | while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) { |
1021 | &{"${kwd}_handler"}() ; |
8e07c86e |
1022 | next PARAGRAPH unless @line ; |
1023 | $_ = shift(@line); |
1024 | } |
c2960299 |
1025 | |
8e07c86e |
1026 | if (check_keyword("BOOT")) { |
1027 | &check_cpp; |
6f1abe2b |
1028 | push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"") |
1029 | if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/; |
1030 | push (@BootCode, @line, "") ; |
c2960299 |
1031 | next PARAGRAPH ; |
a0d0e21e |
1032 | } |
c2960299 |
1033 | |
8e07c86e |
1034 | |
1035 | # extract return type, function name and arguments |
cfc02341 |
1036 | ($ret_type) = TidyType($_); |
f8b8e0a4 |
1037 | $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//; |
8e07c86e |
1038 | |
11416672 |
1039 | # Allow one-line ANSI-like declaration |
1040 | unshift @line, $2 |
1041 | if $process_argtypes |
1042 | and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s; |
1043 | |
c2960299 |
1044 | # a function definition needs at least 2 lines |
1045 | blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH |
1046 | unless @line ; |
1047 | |
8e07c86e |
1048 | $static = 1 if $ret_type =~ s/^static\s+//; |
1049 | |
2304df62 |
1050 | $func_header = shift(@line); |
c2960299 |
1051 | blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH |
f8b8e0a4 |
1052 | unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s; |
c2960299 |
1053 | |
8e07c86e |
1054 | ($class, $func_name, $orig_args) = ($1, $2, $3) ; |
f480b56a |
1055 | $class = "$4 $class" if $4; |
2304df62 |
1056 | ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/; |
ff68c719 |
1057 | ($clean_func_name = $func_name) =~ s/^$Prefix//; |
1058 | $Full_func_name = "${Packid}_$clean_func_name"; |
ff0cee69 |
1059 | if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); } |
c2960299 |
1060 | |
1061 | # Check for duplicate function definition |
4230ab3f |
1062 | for $tmp (@XSStack) { |
1063 | next unless defined $tmp->{functions}{$Full_func_name}; |
ff68c719 |
1064 | Warn("Warning: duplicate function definition '$clean_func_name' detected"); |
4230ab3f |
1065 | last; |
8e07c86e |
1066 | } |
4230ab3f |
1067 | $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ; |
be3174d2 |
1068 | %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = (); |
ef50df4b |
1069 | $DoSetMagic = 1; |
c2960299 |
1070 | |
f8b8e0a4 |
1071 | $orig_args =~ s/\\\s*/ /g; # process line continuations |
1072 | |
1073 | my %out_vars; |
1074 | if ($process_argtypes and $orig_args =~ /\S/) { |
1075 | my $args = "$orig_args ,"; |
14455d6c |
1076 | if ($args =~ /^( (??{ $C_arg }) , )* $ /x) { |
1077 | @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg); |
f8b8e0a4 |
1078 | for ( @args ) { |
1079 | s/^\s+//; |
1080 | s/\s+$//; |
1081 | my $arg = $_; |
1082 | my $default; |
1083 | ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x; |
1084 | my ($pre, $name) = ($arg =~ /(.*?) \s* \b(\w+) \s* $ /x); |
1085 | next unless length $pre; |
1086 | my $out_type; |
1087 | my $inout_var; |
63385af5 |
1088 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST)\s+//) { |
f8b8e0a4 |
1089 | my $type = $1; |
63385af5 |
1090 | $out_type = $type if $type ne 'IN'; |
1091 | $arg =~ s/^(IN|IN_OUTLIST|OUTLIST)\s+//; |
f8b8e0a4 |
1092 | } |
1093 | if (/\W/) { # Has a type |
1094 | push @arg_with_types, $arg; |
1095 | # warn "pushing '$arg'\n"; |
1096 | $arg_types{$name} = $arg; |
1097 | $_ = "$name$default"; |
1098 | } |
63385af5 |
1099 | $out_vars{$_} = 1 if $out_type eq 'OUTLIST'; |
f8b8e0a4 |
1100 | push @in_out, $name if $out_type; |
1101 | $in_out{$name} = $out_type if $out_type; |
1102 | } |
1103 | } else { |
1104 | @args = split(/\s*,\s*/, $orig_args); |
1105 | Warn("Warning: cannot parse argument list '$orig_args', fallback to split"); |
1106 | } |
1107 | } else { |
1108 | @args = split(/\s*,\s*/, $orig_args); |
1109 | for (@args) { |
63385af5 |
1110 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST)\s+//) { |
f8b8e0a4 |
1111 | my $out_type = $1; |
63385af5 |
1112 | next if $out_type eq 'IN'; |
1113 | $out_vars{$_} = 1 if $out_type eq 'OUTLIST'; |
f8b8e0a4 |
1114 | push @in_out, $name; |
1115 | $in_out{$_} = $out_type; |
1116 | } |
1117 | } |
1118 | } |
a0d0e21e |
1119 | if (defined($class)) { |
683d4eee |
1120 | my $arg0 = ((defined($static) or $func_name eq 'new') |
1121 | ? "CLASS" : "THIS"); |
8e07c86e |
1122 | unshift(@args, $arg0); |
f8b8e0a4 |
1123 | ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/; |
2304df62 |
1124 | } |
f8b8e0a4 |
1125 | my $extra_args = 0; |
1126 | @args_num = (); |
1127 | $num_args = 0; |
1128 | my $report_args = ''; |
1129 | foreach $i (0 .. $#args) { |
2304df62 |
1130 | if ($args[$i] =~ s/\.\.\.//) { |
1131 | $elipsis = 1; |
f8b8e0a4 |
1132 | if ($args[$i] eq '' && $i == $#args) { |
1133 | $report_args .= ", ..."; |
2304df62 |
1134 | pop(@args); |
1135 | last; |
1136 | } |
1137 | } |
f8b8e0a4 |
1138 | if ($out_vars{$args[$i]}) { |
1139 | push @args_num, undef; |
1140 | } else { |
1141 | push @args_num, ++$num_args; |
1142 | $report_args .= ", $args[$i]"; |
1143 | } |
8e07c86e |
1144 | if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) { |
f8b8e0a4 |
1145 | $extra_args++; |
2304df62 |
1146 | $args[$i] = $1; |
1147 | $defaults{$args[$i]} = $2; |
1148 | $defaults{$args[$i]} =~ s/"/\\"/g; |
1149 | } |
93d3b392 |
1150 | $proto_arg[$i+1] = "\$" ; |
2304df62 |
1151 | } |
f8b8e0a4 |
1152 | $min_args = $num_args - $extra_args; |
1153 | $report_args =~ s/"/\\"/g; |
1154 | $report_args =~ s/^,\s+//; |
1155 | my @func_args = @args; |
1156 | shift @func_args if defined($class); |
1157 | |
1158 | for (@func_args) { |
1159 | s/^/&/ if $in_out{$_}; |
2304df62 |
1160 | } |
f8b8e0a4 |
1161 | $func_args = join(", ", @func_args); |
1162 | @args_match{@args} = @args_num; |
2304df62 |
1163 | |
8e07c86e |
1164 | $PPCODE = grep(/^\s*PPCODE\s*:/, @line); |
93d3b392 |
1165 | $CODE = grep(/^\s*CODE\s*:/, @line); |
6c5fb52b |
1166 | # Detect CODE: blocks which use ST(n)= or XST_m*(n,v) |
1167 | # to set explicit return values. |
1168 | $EXPLICIT_RETURN = ($CODE && |
1169 | ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x )); |
8e07c86e |
1170 | $ALIAS = grep(/^\s*ALIAS\s*:/, @line); |
cfc02341 |
1171 | $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line); |
8e07c86e |
1172 | |
f8b8e0a4 |
1173 | $xsreturn = 1 if $EXPLICIT_RETURN; |
1174 | |
2304df62 |
1175 | # print function header |
a0d0e21e |
1176 | print Q<<"EOF"; |
ff68c719 |
1177 | #XS(XS_${Full_func_name}) |
2304df62 |
1178 | #[[ |
a0d0e21e |
1179 | # dXSARGS; |
93a17b20 |
1180 | EOF |
8e07c86e |
1181 | print Q<<"EOF" if $ALIAS ; |
1182 | # dXSI32; |
1183 | EOF |
cfc02341 |
1184 | print Q<<"EOF" if $INTERFACE ; |
1185 | # dXSFUNCTION($ret_type); |
1186 | EOF |
2304df62 |
1187 | if ($elipsis) { |
8e07c86e |
1188 | $cond = ($min_args ? qq(items < $min_args) : 0); |
2304df62 |
1189 | } |
1190 | elsif ($min_args == $num_args) { |
1191 | $cond = qq(items != $min_args); |
1192 | } |
1193 | else { |
1194 | $cond = qq(items < $min_args || items > $num_args); |
1195 | } |
8990e307 |
1196 | |
2304df62 |
1197 | print Q<<"EOF" if $except; |
1198 | # char errbuf[1024]; |
1199 | # *errbuf = '\0'; |
1200 | EOF |
1201 | |
8e07c86e |
1202 | if ($ALIAS) |
1203 | { print Q<<"EOF" if $cond } |
1204 | # if ($cond) |
f8b8e0a4 |
1205 | # Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv))); |
8e07c86e |
1206 | EOF |
1207 | else |
1208 | { print Q<<"EOF" if $cond } |
1209 | # if ($cond) |
f8b8e0a4 |
1210 | # Perl_croak(aTHX_ "Usage: $pname($report_args)"); |
93a17b20 |
1211 | EOF |
1212 | |
a0d0e21e |
1213 | print Q<<"EOF" if $PPCODE; |
1214 | # SP -= items; |
1215 | EOF |
1216 | |
2304df62 |
1217 | # Now do a block of some sort. |
93a17b20 |
1218 | |
2304df62 |
1219 | $condnum = 0; |
8e07c86e |
1220 | $cond = ''; # last CASE: condidional |
1221 | push(@line, "$END:"); |
1222 | push(@line_no, $line_no[-1]); |
1223 | $_ = ''; |
1224 | &check_cpp; |
2304df62 |
1225 | while (@line) { |
8e07c86e |
1226 | &CASE_handler if check_keyword("CASE"); |
1227 | print Q<<"EOF"; |
1228 | # $except [[ |
93a17b20 |
1229 | EOF |
1230 | |
1231 | # do initialization of input variables |
1232 | $thisdone = 0; |
1233 | $retvaldone = 0; |
463ee0b2 |
1234 | $deferred = ""; |
c2960299 |
1235 | %arg_list = () ; |
1236 | $gotRETVAL = 0; |
f06db76b |
1237 | |
8fc38fda |
1238 | INPUT_handler() ; |
be3174d2 |
1239 | process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE") ; |
8fc38fda |
1240 | |
db3b9414 |
1241 | print Q<<"EOF" if $ScopeThisXSUB; |
1242 | # ENTER; |
1243 | # [[ |
1244 | EOF |
1245 | |
a0d0e21e |
1246 | if (!$thisdone && defined($class)) { |
683d4eee |
1247 | if (defined($static) or $func_name eq 'new') { |
a0d0e21e |
1248 | print "\tchar *"; |
1249 | $var_types{"CLASS"} = "char *"; |
1250 | &generate_init("char *", 1, "CLASS"); |
1251 | } |
1252 | else { |
93a17b20 |
1253 | print "\t$class *"; |
1254 | $var_types{"THIS"} = "$class *"; |
1255 | &generate_init("$class *", 1, "THIS"); |
a0d0e21e |
1256 | } |
93a17b20 |
1257 | } |
1258 | |
1259 | # do code |
1260 | if (/^\s*NOT_IMPLEMENTED_YET/) { |
cea2e8a9 |
1261 | print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n"; |
4633a7c4 |
1262 | $_ = '' ; |
93a17b20 |
1263 | } else { |
1264 | if ($ret_type ne "void") { |
ddf6bed1 |
1265 | print "\t" . &map_type($ret_type, 'RETVAL') . ";\n" |
93a17b20 |
1266 | if !$retvaldone; |
1267 | $args_match{"RETVAL"} = 0; |
1268 | $var_types{"RETVAL"} = $ret_type; |
b26a54d0 |
1269 | print "\tdXSTARG;\n" |
1270 | if $WantOptimize and $targetable{$type_kind{$ret_type}}; |
93a17b20 |
1271 | } |
db3b9414 |
1272 | |
f8b8e0a4 |
1273 | if (@arg_with_types) { |
1274 | unshift @line, @arg_with_types, $_; |
1275 | $_ = ""; |
1276 | $processing_arg_with_types = 1; |
1277 | INPUT_handler() ; |
1278 | } |
8e07c86e |
1279 | print $deferred; |
db3b9414 |
1280 | |
be3174d2 |
1281 | process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS") ; |
8e07c86e |
1282 | |
1283 | if (check_keyword("PPCODE")) { |
8fc38fda |
1284 | print_section(); |
8e07c86e |
1285 | death ("PPCODE must be last thing") if @line; |
db3b9414 |
1286 | print "\tLEAVE;\n" if $ScopeThisXSUB; |
a0d0e21e |
1287 | print "\tPUTBACK;\n\treturn;\n"; |
8e07c86e |
1288 | } elsif (check_keyword("CODE")) { |
8fc38fda |
1289 | print_section() ; |
1290 | } elsif (defined($class) and $func_name eq "DESTROY") { |
a0d0e21e |
1291 | print "\n\t"; |
8e07c86e |
1292 | print "delete THIS;\n"; |
93a17b20 |
1293 | } else { |
1294 | print "\n\t"; |
1295 | if ($ret_type ne "void") { |
463ee0b2 |
1296 | print "RETVAL = "; |
e50aee73 |
1297 | $wantRETVAL = 1; |
93a17b20 |
1298 | } |
1299 | if (defined($static)) { |
683d4eee |
1300 | if ($func_name eq 'new') { |
8fc38fda |
1301 | $func_name = "$class"; |
8e07c86e |
1302 | } else { |
1303 | print "${class}::"; |
a0d0e21e |
1304 | } |
93a17b20 |
1305 | } elsif (defined($class)) { |
683d4eee |
1306 | if ($func_name eq 'new') { |
8fc38fda |
1307 | $func_name .= " $class"; |
1308 | } else { |
93a17b20 |
1309 | print "THIS->"; |
8fc38fda |
1310 | } |
93a17b20 |
1311 | } |
e50aee73 |
1312 | $func_name =~ s/^($spat)// |
1313 | if defined($spat); |
cfc02341 |
1314 | $func_name = 'XSFUNCTION' if $interface; |
93a17b20 |
1315 | print "$func_name($func_args);\n"; |
93a17b20 |
1316 | } |
1317 | } |
1318 | |
1319 | # do output variables |
f8b8e0a4 |
1320 | $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section; |
1321 | undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section); |
1322 | # $wantRETVAL set if 'RETVAL =' autogenerated |
1323 | ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return; |
8e07c86e |
1324 | undef %outargs ; |
be3174d2 |
1325 | process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE"); |
f06db76b |
1326 | |
1327 | # all OUTPUT done, so now push the return value on the stack |
8e07c86e |
1328 | if ($gotRETVAL && $RETVAL_code) { |
1329 | print "\t$RETVAL_code\n"; |
1330 | } elsif ($gotRETVAL || $wantRETVAL) { |
b26a54d0 |
1331 | my $t = $WantOptimize && $targetable{$type_kind{$ret_type}}; |
1332 | my $var = 'RETVAL'; |
1333 | my $type = $ret_type; |
1334 | |
1335 | # 0: type, 1: with_size, 2: how, 3: how_size |
1336 | if ($t and not $t->[1] and $t->[0] eq 'p') { |
1337 | # PUSHp corresponds to setpvn. Treate setpv directly |
1338 | my $what = eval qq("$t->[2]"); |
1339 | warn $@ if $@; |
1340 | |
1341 | print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n"; |
f8b8e0a4 |
1342 | $prepush_done = 1; |
b26a54d0 |
1343 | } |
1344 | elsif ($t) { |
1345 | my $what = eval qq("$t->[2]"); |
1346 | warn $@ if $@; |
1347 | |
1348 | my $size = $t->[3]; |
1349 | $size = '' unless defined $size; |
1350 | $size = eval qq("$size"); |
1351 | warn $@ if $@; |
1352 | print "\tXSprePUSH; PUSH$t->[0]($what$size);\n"; |
f8b8e0a4 |
1353 | $prepush_done = 1; |
b26a54d0 |
1354 | } |
1355 | else { |
1356 | # RETVAL almost never needs SvSETMAGIC() |
1357 | &generate_output($ret_type, 0, 'RETVAL', 0); |
1358 | } |
8e07c86e |
1359 | } |
f06db76b |
1360 | |
f8b8e0a4 |
1361 | $xsreturn = 1 if $ret_type ne "void"; |
1362 | my $num = $xsreturn; |
1363 | my $c = @in_out; |
1364 | print "\tXSprePUSH;" if $c and not $prepush_done; |
1365 | print "\tEXTEND(SP,$c);\n" if $c; |
1366 | $xsreturn += $c; |
1367 | generate_output($var_types{$_}, $num++, $_, 0, 1) for @in_out; |
1368 | |
93a17b20 |
1369 | # do cleanup |
be3174d2 |
1370 | process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE") ; |
8e07c86e |
1371 | |
db3b9414 |
1372 | print Q<<"EOF" if $ScopeThisXSUB; |
1373 | # ]] |
1374 | EOF |
1375 | print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE; |
1376 | # LEAVE; |
1377 | EOF |
1378 | |
93a17b20 |
1379 | # print function trailer |
8e07c86e |
1380 | print Q<<EOF; |
2304df62 |
1381 | # ]] |
8e07c86e |
1382 | EOF |
1383 | print Q<<EOF if $except; |
8990e307 |
1384 | # BEGHANDLERS |
1385 | # CATCHALL |
1386 | # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason); |
1387 | # ENDHANDLERS |
93a17b20 |
1388 | EOF |
8e07c86e |
1389 | if (check_keyword("CASE")) { |
1390 | blurt ("Error: No `CASE:' at top of function") |
1391 | unless $condnum; |
1392 | $_ = "CASE: $_"; # Restore CASE: label |
1393 | next; |
8990e307 |
1394 | } |
8e07c86e |
1395 | last if $_ eq "$END:"; |
1396 | death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function"); |
2304df62 |
1397 | } |
a0d0e21e |
1398 | |
2304df62 |
1399 | print Q<<EOF if $except; |
1400 | # if (errbuf[0]) |
cea2e8a9 |
1401 | # Perl_croak(aTHX_ errbuf); |
2304df62 |
1402 | EOF |
a0d0e21e |
1403 | |
f8b8e0a4 |
1404 | if ($xsreturn) { |
93d3b392 |
1405 | print Q<<EOF unless $PPCODE; |
f8b8e0a4 |
1406 | # XSRETURN($xsreturn); |
a0d0e21e |
1407 | EOF |
93d3b392 |
1408 | } else { |
1409 | print Q<<EOF unless $PPCODE; |
1410 | # XSRETURN_EMPTY; |
1411 | EOF |
1412 | } |
a0d0e21e |
1413 | |
2304df62 |
1414 | print Q<<EOF; |
2304df62 |
1415 | #]] |
8990e307 |
1416 | # |
93a17b20 |
1417 | EOF |
382b8d97 |
1418 | |
4230ab3f |
1419 | my $newXS = "newXS" ; |
1420 | my $proto = "" ; |
1421 | |
382b8d97 |
1422 | # Build the prototype string for the xsub |
1423 | if ($ProtoThisXSUB) { |
4230ab3f |
1424 | $newXS = "newXSproto"; |
1425 | |
6f1abe2b |
1426 | if ($ProtoThisXSUB eq 2) { |
4230ab3f |
1427 | # User has specified empty prototype |
1428 | $proto = ', ""' ; |
1429 | } |
6f1abe2b |
1430 | elsif ($ProtoThisXSUB ne 1) { |
7d41bd0a |
1431 | # User has specified a prototype |
4230ab3f |
1432 | $proto = ', "' . $ProtoThisXSUB . '"'; |
382b8d97 |
1433 | } |
1434 | else { |
1435 | my $s = ';'; |
1436 | if ($min_args < $num_args) { |
1437 | $s = ''; |
1438 | $proto_arg[$min_args] .= ";" ; |
1439 | } |
4230ab3f |
1440 | push @proto_arg, "$s\@" |
382b8d97 |
1441 | if $elipsis ; |
1442 | |
4230ab3f |
1443 | $proto = ', "' . join ("", @proto_arg) . '"'; |
382b8d97 |
1444 | } |
1445 | } |
1446 | |
4230ab3f |
1447 | if (%XsubAliases) { |
1448 | $XsubAliases{$pname} = 0 |
1449 | unless defined $XsubAliases{$pname} ; |
1450 | while ( ($name, $value) = each %XsubAliases) { |
1451 | push(@InitFileCode, Q<<"EOF"); |
1452 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1453 | # XSANY.any_i32 = $value ; |
1454 | EOF |
1455 | push(@InitFileCode, Q<<"EOF") if $proto; |
1456 | # sv_setpv((SV*)cv$proto) ; |
1457 | EOF |
1458 | } |
cfc02341 |
1459 | } |
be3174d2 |
1460 | elsif (@Attributes) { |
1461 | push(@InitFileCode, Q<<"EOF"); |
1462 | # cv = newXS(\"$pname\", XS_$Full_func_name, file); |
1463 | # apply_attrs_string("$Package", cv, "@Attributes", 0); |
1464 | EOF |
1465 | } |
cfc02341 |
1466 | elsif ($interface) { |
1467 | while ( ($name, $value) = each %Interfaces) { |
1468 | $name = "$Package\::$name" unless $name =~ /::/; |
1469 | push(@InitFileCode, Q<<"EOF"); |
1470 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1471 | # $interface_macro_set(cv,$value) ; |
1472 | EOF |
1473 | push(@InitFileCode, Q<<"EOF") if $proto; |
1474 | # sv_setpv((SV*)cv$proto) ; |
1475 | EOF |
1476 | } |
4230ab3f |
1477 | } |
1478 | else { |
1479 | push(@InitFileCode, |
1480 | " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n"); |
1481 | } |
93a17b20 |
1482 | } |
1483 | |
1484 | # print initialization routine |
7ee8c957 |
1485 | |
e3b8966e |
1486 | print Q<<"EOF"; |
e3b8966e |
1487 | ##ifdef __cplusplus |
1488 | #extern "C" |
1489 | ##endif |
7ee8c957 |
1490 | EOF |
1491 | |
8990e307 |
1492 | print Q<<"EOF"; |
a0d0e21e |
1493 | #XS(boot_$Module_cname) |
7ee8c957 |
1494 | EOF |
1495 | |
7ee8c957 |
1496 | print Q<<"EOF"; |
2304df62 |
1497 | #[[ |
a0d0e21e |
1498 | # dXSARGS; |
8990e307 |
1499 | # char* file = __FILE__; |
1500 | # |
93a17b20 |
1501 | EOF |
1502 | |
8fc38fda |
1503 | print Q<<"EOF" if $WantVersionChk ; |
1504 | # XS_VERSION_BOOTCHECK ; |
1505 | # |
1506 | EOF |
1507 | |
cfc02341 |
1508 | print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ; |
8e07c86e |
1509 | # { |
1510 | # CV * cv ; |
1511 | # |
1512 | EOF |
1513 | |
4230ab3f |
1514 | print @InitFileCode; |
a0d0e21e |
1515 | |
cfc02341 |
1516 | print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ; |
8e07c86e |
1517 | # } |
1518 | EOF |
1519 | |
a0d0e21e |
1520 | if (@BootCode) |
1521 | { |
6f1abe2b |
1522 | print "\n /* Initialisation Section */\n\n" ; |
1523 | @line = @BootCode; |
1524 | print_section(); |
8e07c86e |
1525 | print "\n /* End of Initialisation Section */\n\n" ; |
93a17b20 |
1526 | } |
a0d0e21e |
1527 | |
e50aee73 |
1528 | print Q<<"EOF";; |
3280af22 |
1529 | # XSRETURN_YES; |
e50aee73 |
1530 | #]] |
e3b8966e |
1531 | # |
1532 | EOF |
1533 | |
8fc38fda |
1534 | warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") |
1535 | unless $ProtoUsed ; |
e50aee73 |
1536 | &Exit; |
1537 | |
93a17b20 |
1538 | sub output_init { |
ddf6bed1 |
1539 | local($type, $num, $var, $init, $name_printed) = @_; |
a0d0e21e |
1540 | local($arg) = "ST(" . ($num - 1) . ")"; |
93a17b20 |
1541 | |
7ad6fb0b |
1542 | if( $init =~ /^=/ ) { |
ddf6bed1 |
1543 | if ($name_printed) { |
1544 | eval qq/print " $init\\n"/; |
1545 | } else { |
1546 | eval qq/print "\\t$var $init\\n"/; |
1547 | } |
7ad6fb0b |
1548 | warn $@ if $@; |
1549 | } else { |
1550 | if( $init =~ s/^\+// && $num ) { |
ddf6bed1 |
1551 | &generate_init($type, $num, $var, $name_printed); |
1552 | } elsif ($name_printed) { |
1553 | print ";\n"; |
1554 | $init =~ s/^;//; |
7ad6fb0b |
1555 | } else { |
1556 | eval qq/print "\\t$var;\\n"/; |
1557 | warn $@ if $@; |
1558 | $init =~ s/^;//; |
1559 | } |
1560 | $deferred .= eval qq/"\\n\\t$init\\n"/; |
1561 | warn $@ if $@; |
1562 | } |
93a17b20 |
1563 | } |
1564 | |
c2960299 |
1565 | sub Warn |
1566 | { |
1567 | # work out the line number |
1568 | my $line_no = $line_no[@line_no - @line -1] ; |
1569 | |
1570 | print STDERR "@_ in $filename, line $line_no\n" ; |
1571 | } |
1572 | |
1573 | sub blurt |
1574 | { |
1575 | Warn @_ ; |
1576 | $errors ++ |
1577 | } |
1578 | |
1579 | sub death |
1580 | { |
1581 | Warn @_ ; |
1582 | exit 1 ; |
1583 | } |
8990e307 |
1584 | |
93a17b20 |
1585 | sub generate_init { |
2304df62 |
1586 | local($type, $num, $var) = @_; |
a0d0e21e |
1587 | local($arg) = "ST(" . ($num - 1) . ")"; |
2304df62 |
1588 | local($argoff) = $num - 1; |
1589 | local($ntype); |
1590 | local($tk); |
93a17b20 |
1591 | |
f06db76b |
1592 | $type = TidyType($type) ; |
c2960299 |
1593 | blurt("Error: '$type' not in typemap"), return |
1594 | unless defined($type_kind{$type}); |
1595 | |
2304df62 |
1596 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
8e07c86e |
1597 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1598 | $tk = $type_kind{$type}; |
1599 | $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/; |
8e07c86e |
1600 | $type =~ tr/:/_/; |
615ca1f4 |
1601 | blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return |
c2960299 |
1602 | unless defined $input_expr{$tk} ; |
2304df62 |
1603 | $expr = $input_expr{$tk}; |
1604 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1605 | blurt("Error: '$subtype' not in typemap"), return |
1606 | unless defined($type_kind{$subtype}); |
615ca1f4 |
1607 | blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return |
c2960299 |
1608 | unless defined $input_expr{$type_kind{$subtype}} ; |
2304df62 |
1609 | $subexpr = $input_expr{$type_kind{$subtype}}; |
1610 | $subexpr =~ s/ntype/subtype/g; |
1611 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1612 | $subexpr =~ s/\n\t/\n\t\t/g; |
93d3b392 |
1613 | $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g; |
a0d0e21e |
1614 | $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/; |
2304df62 |
1615 | $expr =~ s/DO_ARRAY_ELEM/$subexpr/; |
1616 | } |
db3b9414 |
1617 | if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments |
1618 | $ScopeThisXSUB = 1; |
1619 | } |
2304df62 |
1620 | if (defined($defaults{$var})) { |
1621 | $expr =~ s/(\t+)/$1 /g; |
1622 | $expr =~ s/ /\t/g; |
ddf6bed1 |
1623 | if ($name_printed) { |
1624 | print ";\n"; |
1625 | } else { |
1626 | eval qq/print "\\t$var;\\n"/; |
1627 | warn $@ if $@; |
1628 | } |
a104f515 |
1629 | if ($defaults{$var} eq 'NO_INIT') { |
4628e4f8 |
1630 | $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/; |
1631 | } else { |
1632 | $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/; |
1633 | } |
7ad6fb0b |
1634 | warn $@ if $@; |
db3b9414 |
1635 | } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) { |
ddf6bed1 |
1636 | if ($name_printed) { |
1637 | print ";\n"; |
1638 | } else { |
1639 | eval qq/print "\\t$var;\\n"/; |
1640 | warn $@ if $@; |
1641 | } |
2304df62 |
1642 | $deferred .= eval qq/"\\n$expr;\\n"/; |
7ad6fb0b |
1643 | warn $@ if $@; |
2304df62 |
1644 | } else { |
ddf6bed1 |
1645 | die "panic: do not know how to handle this branch for function pointers" |
1646 | if $name_printed; |
2304df62 |
1647 | eval qq/print "$expr;\\n"/; |
7ad6fb0b |
1648 | warn $@ if $@; |
2304df62 |
1649 | } |
93a17b20 |
1650 | } |
1651 | |
1652 | sub generate_output { |
f8b8e0a4 |
1653 | local($type, $num, $var, $do_setmagic, $do_push) = @_; |
a0d0e21e |
1654 | local($arg) = "ST(" . ($num - ($num != 0)) . ")"; |
2304df62 |
1655 | local($argoff) = $num - 1; |
1656 | local($ntype); |
93a17b20 |
1657 | |
f06db76b |
1658 | $type = TidyType($type) ; |
2304df62 |
1659 | if ($type =~ /^array\(([^,]*),(.*)\)/) { |
4bd3d083 |
1660 | print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n"; |
ef50df4b |
1661 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
2304df62 |
1662 | } else { |
f06db76b |
1663 | blurt("Error: '$type' not in typemap"), return |
2304df62 |
1664 | unless defined($type_kind{$type}); |
615ca1f4 |
1665 | blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return |
c2960299 |
1666 | unless defined $output_expr{$type_kind{$type}} ; |
2304df62 |
1667 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
1668 | $ntype =~ s/\(\)//g; |
8e07c86e |
1669 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1670 | $expr = $output_expr{$type_kind{$type}}; |
1671 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1672 | blurt("Error: '$subtype' not in typemap"), return |
1673 | unless defined($type_kind{$subtype}); |
615ca1f4 |
1674 | blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return |
c2960299 |
1675 | unless defined $output_expr{$type_kind{$subtype}} ; |
2304df62 |
1676 | $subexpr = $output_expr{$type_kind{$subtype}}; |
1677 | $subexpr =~ s/ntype/subtype/g; |
1678 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1679 | $subexpr =~ s/\$var/${var}[ix_$var]/g; |
1680 | $subexpr =~ s/\n\t/\n\t\t/g; |
1681 | $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/; |
a0d0e21e |
1682 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1683 | warn $@ if $@; |
ef50df4b |
1684 | print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic; |
2304df62 |
1685 | } |
a0d0e21e |
1686 | elsif ($var eq 'RETVAL') { |
a2baab1c |
1687 | if ($expr =~ /^\t\$arg = new/) { |
1688 | # We expect that $arg has refcnt 1, so we need to |
1689 | # mortalize it. |
a0d0e21e |
1690 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1691 | warn $@ if $@; |
f8b8e0a4 |
1692 | print "\tsv_2mortal(ST($num));\n"; |
1693 | print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic; |
93a17b20 |
1694 | } |
a2baab1c |
1695 | elsif ($expr =~ /^\s*\$arg\s*=/) { |
1696 | # We expect that $arg has refcnt >=1, so we need |
d689ffdd |
1697 | # to mortalize it! |
a2baab1c |
1698 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1699 | warn $@ if $@; |
d689ffdd |
1700 | print "\tsv_2mortal(ST(0));\n"; |
ef50df4b |
1701 | print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; |
a2baab1c |
1702 | } |
2304df62 |
1703 | else { |
a2baab1c |
1704 | # Just hope that the entry would safely write it |
1705 | # over an already mortalized value. By |
1706 | # coincidence, something like $arg = &sv_undef |
1707 | # works too. |
8990e307 |
1708 | print "\tST(0) = sv_newmortal();\n"; |
a0d0e21e |
1709 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1710 | warn $@ if $@; |
ef50df4b |
1711 | # new mortals don't have set magic |
463ee0b2 |
1712 | } |
2304df62 |
1713 | } |
f8b8e0a4 |
1714 | elsif ($do_push) { |
1715 | print "\tPUSHs(sv_newmortal());\n"; |
1716 | $arg = "ST($num)"; |
1717 | eval "print qq\a$expr\a"; |
1718 | warn $@ if $@; |
1719 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
1720 | } |
a0d0e21e |
1721 | elsif ($arg =~ /^ST\(\d+\)$/) { |
1722 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1723 | warn $@ if $@; |
ef50df4b |
1724 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
a0d0e21e |
1725 | } |
2304df62 |
1726 | } |
93a17b20 |
1727 | } |
1728 | |
1729 | sub map_type { |
ddf6bed1 |
1730 | my($type, $varname) = @_; |
93a17b20 |
1731 | |
8e07c86e |
1732 | $type =~ tr/:/_/; |
1733 | $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s; |
ddf6bed1 |
1734 | if ($varname) { |
1735 | if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) { |
1736 | (substr $type, pos $type, 0) = " $varname "; |
1737 | } else { |
1738 | $type .= "\t$varname"; |
1739 | } |
1740 | } |
8e07c86e |
1741 | $type; |
93a17b20 |
1742 | } |
8990e307 |
1743 | |
e50aee73 |
1744 | |
1745 | sub Exit { |
ff0cee69 |
1746 | # If this is VMS, the exit status has meaning to the shell, so we |
1747 | # use a predictable value (SS$_Normal or SS$_Abort) rather than an |
1748 | # arbitrary number. |
1749 | # exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ; |
1750 | exit ($errors ? 1 : 0); |
e50aee73 |
1751 | } |