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 | |
7817ba4d |
112 | $XSUBPP_version = "1.9508"; |
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} =~ |
438cc608 |
277 | m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn |
b26a54d0 |
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 | |
efbca139 |
303 | my ($C_group_rex, $C_arg); |
f8b8e0a4 |
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; |
0f568861 |
421 | my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s |
8e07c86e |
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 ; |
0f568861 |
447 | $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr; |
f8b8e0a4 |
448 | if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/ |
0f568861 |
449 | or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/ |
f8b8e0a4 |
450 | and $var_init !~ /\S/) { |
ddf6bed1 |
451 | if ($name_printed) { |
452 | print ";\n"; |
453 | } else { |
9bea678f |
454 | print "\t$var_name;\n"; |
ddf6bed1 |
455 | } |
8e07c86e |
456 | } elsif ($var_init =~ /\S/) { |
ddf6bed1 |
457 | &output_init($var_type, $var_num, $var_name, $var_init, $name_printed); |
8e07c86e |
458 | } elsif ($var_num) { |
459 | # generate initialization code |
ddf6bed1 |
460 | &generate_init($var_type, $var_num, $var_name, $name_printed); |
8e07c86e |
461 | } else { |
462 | print ";\n"; |
463 | } |
464 | } |
465 | } |
466 | |
467 | sub OUTPUT_handler { |
468 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
469 | next unless /\S/; |
ef50df4b |
470 | if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) { |
471 | $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0); |
472 | next; |
473 | } |
8e07c86e |
474 | my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ; |
475 | blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next |
476 | if $outargs{$outarg} ++ ; |
477 | if (!$gotRETVAL and $outarg eq 'RETVAL') { |
478 | # deal with RETVAL last |
479 | $RETVAL_code = $outcode ; |
480 | $gotRETVAL = 1 ; |
481 | next ; |
482 | } |
483 | blurt ("Error: OUTPUT $outarg not an argument"), next |
484 | unless defined($args_match{$outarg}); |
485 | blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next |
486 | unless defined $var_types{$outarg} ; |
f78230ad |
487 | $var_num = $args_match{$outarg}; |
8e07c86e |
488 | if ($outcode) { |
489 | print "\t$outcode\n"; |
f78230ad |
490 | print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic; |
8e07c86e |
491 | } else { |
ef50df4b |
492 | &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic); |
8e07c86e |
493 | } |
0f568861 |
494 | delete $in_out{$outarg} # No need to auto-OUTPUT |
495 | if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/; |
8e07c86e |
496 | } |
497 | } |
498 | |
cfc02341 |
499 | sub C_ARGS_handler() { |
500 | my $in = merge_section(); |
501 | |
502 | TrimWhitespace($in); |
503 | $func_args = $in; |
504 | } |
505 | |
506 | sub INTERFACE_MACRO_handler() { |
507 | my $in = merge_section(); |
508 | |
509 | TrimWhitespace($in); |
510 | if ($in =~ /\s/) { # two |
511 | ($interface_macro, $interface_macro_set) = split ' ', $in; |
512 | } else { |
513 | $interface_macro = $in; |
514 | $interface_macro_set = 'UNKNOWN_CVT'; # catch later |
515 | } |
516 | $interface = 1; # local |
517 | $Interfaces = 1; # global |
518 | } |
519 | |
520 | sub INTERFACE_handler() { |
521 | my $in = merge_section(); |
522 | |
523 | TrimWhitespace($in); |
524 | |
525 | foreach (split /[\s,]+/, $in) { |
526 | $Interfaces{$_} = $_; |
527 | } |
528 | print Q<<"EOF"; |
529 | # XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr); |
530 | EOF |
531 | $interface = 1; # local |
532 | $Interfaces = 1; # global |
533 | } |
534 | |
8fc38fda |
535 | sub CLEANUP_handler() { print_section() } |
536 | sub PREINIT_handler() { print_section() } |
63385af5 |
537 | sub POSTCALL_handler() { print_section() } |
8fc38fda |
538 | sub INIT_handler() { print_section() } |
539 | |
8e07c86e |
540 | sub GetAliases |
541 | { |
542 | my ($line) = @_ ; |
543 | my ($orig) = $line ; |
544 | my ($alias) ; |
545 | my ($value) ; |
546 | |
547 | # Parse alias definitions |
548 | # format is |
549 | # alias = value alias = value ... |
550 | |
551 | while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) { |
552 | $alias = $1 ; |
553 | $orig_alias = $alias ; |
554 | $value = $2 ; |
555 | |
556 | # check for optional package definition in the alias |
557 | $alias = $Packprefix . $alias if $alias !~ /::/ ; |
558 | |
559 | # check for duplicate alias name & duplicate value |
560 | Warn("Warning: Ignoring duplicate alias '$orig_alias'") |
4230ab3f |
561 | if defined $XsubAliases{$alias} ; |
8e07c86e |
562 | |
4230ab3f |
563 | Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values") |
564 | if $XsubAliasValues{$value} ; |
8e07c86e |
565 | |
4230ab3f |
566 | $XsubAliases = 1; |
567 | $XsubAliases{$alias} = $value ; |
568 | $XsubAliasValues{$value} = $orig_alias ; |
8e07c86e |
569 | } |
570 | |
571 | blurt("Error: Cannot parse ALIAS definitions from '$orig'") |
572 | if $line ; |
573 | } |
574 | |
be3174d2 |
575 | sub ATTRS_handler () |
576 | { |
577 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
578 | next unless /\S/; |
579 | TrimWhitespace($_) ; |
580 | push @Attributes, $_; |
581 | } |
582 | } |
583 | |
382b8d97 |
584 | sub ALIAS_handler () |
8e07c86e |
585 | { |
586 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
587 | next unless /\S/; |
588 | TrimWhitespace($_) ; |
589 | GetAliases($_) if $_ ; |
590 | } |
591 | } |
592 | |
382b8d97 |
593 | sub REQUIRE_handler () |
8e07c86e |
594 | { |
595 | # the rest of the current line should contain a version number |
596 | my ($Ver) = $_ ; |
597 | |
598 | TrimWhitespace($Ver) ; |
599 | |
600 | death ("Error: REQUIRE expects a version number") |
601 | unless $Ver ; |
602 | |
603 | # check that the version number is of the form n.n |
604 | death ("Error: REQUIRE: expected a number, got '$Ver'") |
605 | unless $Ver =~ /^\d+(\.\d*)?/ ; |
606 | |
607 | death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.") |
608 | unless $XSUBPP_version >= $Ver ; |
609 | } |
610 | |
8fc38fda |
611 | sub VERSIONCHECK_handler () |
612 | { |
613 | # the rest of the current line should contain either ENABLE or |
614 | # DISABLE |
615 | |
616 | TrimWhitespace($_) ; |
617 | |
618 | # check for ENABLE/DISABLE |
619 | death ("Error: VERSIONCHECK: ENABLE/DISABLE") |
620 | unless /^(ENABLE|DISABLE)/i ; |
621 | |
622 | $WantVersionChk = 1 if $1 eq 'ENABLE' ; |
623 | $WantVersionChk = 0 if $1 eq 'DISABLE' ; |
624 | |
625 | } |
626 | |
382b8d97 |
627 | sub PROTOTYPE_handler () |
628 | { |
7d41bd0a |
629 | my $specified ; |
630 | |
c07a80fd |
631 | death("Error: Only 1 PROTOTYPE definition allowed per xsub") |
632 | if $proto_in_this_xsub ++ ; |
633 | |
382b8d97 |
634 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
635 | next unless /\S/; |
7d41bd0a |
636 | $specified = 1 ; |
382b8d97 |
637 | TrimWhitespace($_) ; |
638 | if ($_ eq 'DISABLE') { |
639 | $ProtoThisXSUB = 0 |
640 | } |
641 | elsif ($_ eq 'ENABLE') { |
642 | $ProtoThisXSUB = 1 |
643 | } |
644 | else { |
645 | # remove any whitespace |
646 | s/\s+//g ; |
647 | death("Error: Invalid prototype '$_'") |
648 | unless ValidProtoString($_) ; |
649 | $ProtoThisXSUB = C_string($_) ; |
650 | } |
651 | } |
c07a80fd |
652 | |
7d41bd0a |
653 | # If no prototype specified, then assume empty prototype "" |
654 | $ProtoThisXSUB = 2 unless $specified ; |
655 | |
8fc38fda |
656 | $ProtoUsed = 1 ; |
c07a80fd |
657 | |
382b8d97 |
658 | } |
659 | |
db3b9414 |
660 | sub SCOPE_handler () |
661 | { |
662 | death("Error: Only 1 SCOPE declaration allowed per xsub") |
663 | if $scope_in_this_xsub ++ ; |
664 | |
665 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
666 | next unless /\S/; |
667 | TrimWhitespace($_) ; |
668 | if ($_ =~ /^DISABLE/i) { |
669 | $ScopeThisXSUB = 0 |
670 | } |
671 | elsif ($_ =~ /^ENABLE/i) { |
672 | $ScopeThisXSUB = 1 |
673 | } |
674 | } |
675 | |
676 | } |
677 | |
382b8d97 |
678 | sub PROTOTYPES_handler () |
679 | { |
680 | # the rest of the current line should contain either ENABLE or |
681 | # DISABLE |
682 | |
683 | TrimWhitespace($_) ; |
684 | |
685 | # check for ENABLE/DISABLE |
686 | death ("Error: PROTOTYPES: ENABLE/DISABLE") |
687 | unless /^(ENABLE|DISABLE)/i ; |
688 | |
689 | $WantPrototypes = 1 if $1 eq 'ENABLE' ; |
690 | $WantPrototypes = 0 if $1 eq 'DISABLE' ; |
8fc38fda |
691 | $ProtoUsed = 1 ; |
382b8d97 |
692 | |
693 | } |
694 | |
8fc38fda |
695 | sub INCLUDE_handler () |
696 | { |
697 | # the rest of the current line should contain a valid filename |
698 | |
699 | TrimWhitespace($_) ; |
700 | |
8fc38fda |
701 | death("INCLUDE: filename missing") |
702 | unless $_ ; |
703 | |
704 | death("INCLUDE: output pipe is illegal") |
705 | if /^\s*\|/ ; |
706 | |
707 | # simple minded recursion detector |
708 | death("INCLUDE loop detected") |
709 | if $IncludedFiles{$_} ; |
710 | |
711 | ++ $IncludedFiles{$_} unless /\|\s*$/ ; |
712 | |
713 | # Save the current file context. |
4230ab3f |
714 | push(@XSStack, { |
715 | type => 'file', |
8fc38fda |
716 | LastLine => $lastline, |
717 | LastLineNo => $lastline_no, |
718 | Line => \@line, |
719 | LineNo => \@line_no, |
720 | Filename => $filename, |
c07a80fd |
721 | Handle => $FH, |
8fc38fda |
722 | }) ; |
723 | |
c07a80fd |
724 | ++ $FH ; |
8fc38fda |
725 | |
726 | # open the new file |
c07a80fd |
727 | open ($FH, "$_") or death("Cannot open '$_': $!") ; |
8fc38fda |
728 | |
729 | print Q<<"EOF" ; |
730 | # |
731 | #/* INCLUDE: Including '$_' from '$filename' */ |
732 | # |
733 | EOF |
734 | |
8fc38fda |
735 | $filename = $_ ; |
736 | |
c07a80fd |
737 | # Prime the pump by reading the first |
738 | # non-blank line |
739 | |
740 | # skip leading blank lines |
741 | while (<$FH>) { |
742 | last unless /^\s*$/ ; |
743 | } |
744 | |
745 | $lastline = $_ ; |
8fc38fda |
746 | $lastline_no = $. ; |
747 | |
748 | } |
749 | |
750 | sub PopFile() |
751 | { |
4230ab3f |
752 | return 0 unless $XSStack[-1]{type} eq 'file' ; |
753 | |
754 | my $data = pop @XSStack ; |
8fc38fda |
755 | my $ThisFile = $filename ; |
756 | my $isPipe = ($filename =~ /\|\s*$/) ; |
757 | |
758 | -- $IncludedFiles{$filename} |
759 | unless $isPipe ; |
760 | |
c07a80fd |
761 | close $FH ; |
8fc38fda |
762 | |
c07a80fd |
763 | $FH = $data->{Handle} ; |
8fc38fda |
764 | $filename = $data->{Filename} ; |
765 | $lastline = $data->{LastLine} ; |
766 | $lastline_no = $data->{LastLineNo} ; |
767 | @line = @{ $data->{Line} } ; |
768 | @line_no = @{ $data->{LineNo} } ; |
4230ab3f |
769 | |
8fc38fda |
770 | if ($isPipe and $? ) { |
771 | -- $lastline_no ; |
772 | print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ; |
773 | exit 1 ; |
774 | } |
775 | |
776 | print Q<<"EOF" ; |
777 | # |
778 | #/* INCLUDE: Returning to '$filename' from '$ThisFile' */ |
779 | # |
780 | EOF |
781 | |
782 | return 1 ; |
783 | } |
784 | |
382b8d97 |
785 | sub ValidProtoString ($) |
786 | { |
787 | my($string) = @_ ; |
788 | |
789 | if ( $string =~ /^$proto_re+$/ ) { |
790 | return $string ; |
791 | } |
792 | |
793 | return 0 ; |
794 | } |
795 | |
796 | sub C_string ($) |
797 | { |
798 | my($string) = @_ ; |
799 | |
800 | $string =~ s[\\][\\\\]g ; |
801 | $string ; |
802 | } |
803 | |
804 | sub ProtoString ($) |
805 | { |
806 | my ($type) = @_ ; |
807 | |
93d3b392 |
808 | $proto_letter{$type} or "\$" ; |
382b8d97 |
809 | } |
810 | |
8e07c86e |
811 | sub check_cpp { |
812 | my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line); |
813 | if (@cpp) { |
814 | my ($cpp, $cpplevel); |
815 | for $cpp (@cpp) { |
816 | if ($cpp =~ /^\#\s*if/) { |
817 | $cpplevel++; |
818 | } elsif (!$cpplevel) { |
819 | Warn("Warning: #else/elif/endif without #if in this function"); |
4230ab3f |
820 | print STDERR " (precede it with a blank line if the matching #if is outside the function)\n" |
821 | if $XSStack[-1]{type} eq 'if'; |
8e07c86e |
822 | return; |
823 | } elsif ($cpp =~ /^\#\s*endif/) { |
824 | $cpplevel--; |
825 | } |
826 | } |
827 | Warn("Warning: #if without #endif in this function") if $cpplevel; |
828 | } |
829 | } |
830 | |
831 | |
8990e307 |
832 | sub Q { |
e50aee73 |
833 | my($text) = @_; |
4633a7c4 |
834 | $text =~ s/^#//gm; |
2304df62 |
835 | $text =~ s/\[\[/{/g; |
836 | $text =~ s/\]\]/}/g; |
8990e307 |
837 | $text; |
93a17b20 |
838 | } |
839 | |
c07a80fd |
840 | open($FH, $filename) or die "cannot open $filename: $!\n"; |
c2960299 |
841 | |
f06db76b |
842 | # Identify the version of xsubpp used |
f06db76b |
843 | print <<EOM ; |
e50aee73 |
844 | /* |
845 | * This file was generated automatically by xsubpp version $XSUBPP_version from the |
93d3b392 |
846 | * contents of $filename. Do not edit this file, edit $filename instead. |
e50aee73 |
847 | * |
848 | * ANY CHANGES MADE HERE WILL BE LOST! |
f06db76b |
849 | * |
850 | */ |
e50aee73 |
851 | |
f06db76b |
852 | EOM |
6f1abe2b |
853 | |
854 | |
855 | print("#line 1 \"$filename\"\n") |
856 | if $WantLineNumbers; |
f06db76b |
857 | |
e03d20b3 |
858 | firstmodule: |
c07a80fd |
859 | while (<$FH>) { |
e03d20b3 |
860 | if (/^=/) { |
7817ba4d |
861 | my $podstartline = $.; |
e03d20b3 |
862 | do { |
7817ba4d |
863 | if (/^=cut\s*$/) { |
864 | print("/* Skipped embedded POD. */\n"); |
865 | printf("#line %d \"$filename\"\n", $. + 1) |
866 | if $WantLineNumbers; |
867 | next firstmodule |
868 | } |
869 | |
e03d20b3 |
870 | } while (<$FH>); |
7817ba4d |
871 | # At this point $. is at end of file so die won't state the start |
872 | # of the problem, and as we haven't yet read any lines &death won't |
873 | # show the correct line in the message either. |
874 | die ("Error: Unterminated pod in $filename, line $podstartline\n") |
875 | unless $lastline; |
e03d20b3 |
876 | } |
e50aee73 |
877 | last if ($Module, $Package, $Prefix) = |
878 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/; |
01f988be |
879 | |
880 | if ($OBJ) { |
b85ee828 |
881 | s/#if(?:def\s|\s+defined)\s*(\(__cplusplus\)|__cplusplus)/#if defined(__cplusplus) && !defined(PERL_OBJECT)/; |
01f988be |
882 | } |
a0d0e21e |
883 | print $_; |
93a17b20 |
884 | } |
e50aee73 |
885 | &Exit unless defined $_; |
886 | |
cfc02341 |
887 | print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers; |
888 | |
8fc38fda |
889 | $lastline = $_; |
890 | $lastline_no = $.; |
93a17b20 |
891 | |
c07a80fd |
892 | # Read next xsub into @line from ($lastline, <$FH>). |
2304df62 |
893 | sub fetch_para { |
894 | # parse paragraph |
4230ab3f |
895 | death ("Error: Unterminated `#if/#ifdef/#ifndef'") |
896 | if !defined $lastline && $XSStack[-1]{type} eq 'if'; |
2304df62 |
897 | @line = (); |
c2960299 |
898 | @line_no = () ; |
4230ab3f |
899 | return PopFile() if !defined $lastline; |
e50aee73 |
900 | |
901 | if ($lastline =~ |
902 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) { |
903 | $Module = $1; |
8e07c86e |
904 | $Package = defined($2) ? $2 : ''; # keep -w happy |
905 | $Prefix = defined($3) ? $3 : ''; # keep -w happy |
ff68c719 |
906 | $Prefix = quotemeta $Prefix ; |
e50aee73 |
907 | ($Module_cname = $Module) =~ s/\W/_/g; |
8e07c86e |
908 | ($Packid = $Package) =~ tr/:/_/; |
e50aee73 |
909 | $Packprefix = $Package; |
8e07c86e |
910 | $Packprefix .= "::" if $Packprefix ne ""; |
2304df62 |
911 | $lastline = ""; |
e50aee73 |
912 | } |
913 | |
914 | for(;;) { |
e03d20b3 |
915 | # Skip embedded PODs |
413e5597 |
916 | while ($lastline =~ /^=/) { |
e03d20b3 |
917 | while ($lastline = <$FH>) { |
918 | last if ($lastline =~ /^=cut\s*$/); |
919 | } |
920 | death ("Error: Unterminated pod") unless $lastline; |
921 | $lastline = <$FH>; |
922 | chomp $lastline; |
923 | $lastline =~ s/^\s+$//; |
924 | } |
e50aee73 |
925 | if ($lastline !~ /^\s*#/ || |
4230ab3f |
926 | # CPP directives: |
927 | # ANSI: if ifdef ifndef elif else endif define undef |
928 | # line error pragma |
929 | # gcc: warning include_next |
930 | # obj-c: import |
931 | # others: ident (gcc notes that some cpps have this one) |
932 | $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) { |
e50aee73 |
933 | last if $lastline =~ /^\S/ && @line && $line[-1] eq ""; |
934 | push(@line, $lastline); |
935 | push(@line_no, $lastline_no) ; |
93a17b20 |
936 | } |
e50aee73 |
937 | |
938 | # Read next line and continuation lines |
c07a80fd |
939 | last unless defined($lastline = <$FH>); |
e50aee73 |
940 | $lastline_no = $.; |
941 | my $tmp_line; |
942 | $lastline .= $tmp_line |
c07a80fd |
943 | while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>)); |
f8b8e0a4 |
944 | |
8e07c86e |
945 | chomp $lastline; |
e50aee73 |
946 | $lastline =~ s/^\s+$//; |
2304df62 |
947 | } |
e50aee73 |
948 | pop(@line), pop(@line_no) while @line && $line[-1] eq ""; |
e50aee73 |
949 | 1; |
2304df62 |
950 | } |
93a17b20 |
951 | |
c2960299 |
952 | PARAGRAPH: |
8e07c86e |
953 | while (fetch_para()) { |
e50aee73 |
954 | # Print initial preprocessor statements and blank lines |
4230ab3f |
955 | while (@line && $line[0] !~ /^[^\#]/) { |
956 | my $line = shift(@line); |
957 | print $line, "\n"; |
958 | next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/; |
959 | my $statement = $+; |
960 | if ($statement eq 'if') { |
961 | $XSS_work_idx = @XSStack; |
962 | push(@XSStack, {type => 'if'}); |
963 | } else { |
964 | death ("Error: `$statement' with no matching `if'") |
965 | if $XSStack[-1]{type} ne 'if'; |
966 | if ($XSStack[-1]{varname}) { |
967 | push(@InitFileCode, "#endif\n"); |
968 | push(@BootCode, "#endif"); |
969 | } |
970 | |
971 | my(@fns) = keys %{$XSStack[-1]{functions}}; |
972 | if ($statement ne 'endif') { |
973 | # Hide the functions defined in other #if branches, and reset. |
974 | @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns; |
975 | @{$XSStack[-1]}{qw(varname functions)} = ('', {}); |
976 | } else { |
977 | my($tmp) = pop(@XSStack); |
978 | 0 while (--$XSS_work_idx |
979 | && $XSStack[$XSS_work_idx]{type} ne 'if'); |
980 | # Keep all new defined functions |
981 | push(@fns, keys %{$tmp->{other_functions}}); |
982 | @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns; |
983 | } |
984 | } |
985 | } |
e50aee73 |
986 | |
987 | next PARAGRAPH unless @line; |
988 | |
4230ab3f |
989 | if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) { |
990 | # We are inside an #if, but have not yet #defined its xsubpp variable. |
991 | print "#define $cpp_next_tmp 1\n\n"; |
992 | push(@InitFileCode, "#if $cpp_next_tmp\n"); |
993 | push(@BootCode, "#if $cpp_next_tmp"); |
994 | $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++; |
995 | } |
996 | |
55497cff |
997 | death ("Code is not inside a function" |
998 | ." (maybe last function was ended by a blank line " |
6d087280 |
999 | ." followed by a statement on column one?)") |
e50aee73 |
1000 | if $line[0] =~ /^\s/; |
1001 | |
2304df62 |
1002 | # initialize info arrays |
1003 | undef(%args_match); |
1004 | undef(%var_types); |
2304df62 |
1005 | undef(%defaults); |
1006 | undef($class); |
1007 | undef($static); |
1008 | undef($elipsis); |
f06db76b |
1009 | undef($wantRETVAL) ; |
f8b8e0a4 |
1010 | undef($RETVAL_no_return) ; |
f06db76b |
1011 | undef(%arg_list) ; |
382b8d97 |
1012 | undef(@proto_arg) ; |
f8b8e0a4 |
1013 | undef(@arg_with_types) ; |
1014 | undef($processing_arg_with_types) ; |
1015 | undef(%arg_types) ; |
0f568861 |
1016 | undef(@outlist) ; |
f8b8e0a4 |
1017 | undef(%in_out) ; |
c07a80fd |
1018 | undef($proto_in_this_xsub) ; |
db3b9414 |
1019 | undef($scope_in_this_xsub) ; |
cfc02341 |
1020 | undef($interface); |
f8b8e0a4 |
1021 | undef($prepush_done); |
cfc02341 |
1022 | $interface_macro = 'XSINTERFACE_FUNC' ; |
1023 | $interface_macro_set = 'XSINTERFACE_FUNC_SET' ; |
382b8d97 |
1024 | $ProtoThisXSUB = $WantPrototypes ; |
db3b9414 |
1025 | $ScopeThisXSUB = 0; |
f8b8e0a4 |
1026 | $xsreturn = 0; |
2304df62 |
1027 | |
8e07c86e |
1028 | $_ = shift(@line); |
8fc38fda |
1029 | while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) { |
1030 | &{"${kwd}_handler"}() ; |
8e07c86e |
1031 | next PARAGRAPH unless @line ; |
1032 | $_ = shift(@line); |
1033 | } |
c2960299 |
1034 | |
8e07c86e |
1035 | if (check_keyword("BOOT")) { |
1036 | &check_cpp; |
6f1abe2b |
1037 | push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"") |
1038 | if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/; |
1039 | push (@BootCode, @line, "") ; |
c2960299 |
1040 | next PARAGRAPH ; |
a0d0e21e |
1041 | } |
c2960299 |
1042 | |
8e07c86e |
1043 | |
1044 | # extract return type, function name and arguments |
cfc02341 |
1045 | ($ret_type) = TidyType($_); |
f8b8e0a4 |
1046 | $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//; |
8e07c86e |
1047 | |
11416672 |
1048 | # Allow one-line ANSI-like declaration |
1049 | unshift @line, $2 |
1050 | if $process_argtypes |
1051 | and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s; |
1052 | |
c2960299 |
1053 | # a function definition needs at least 2 lines |
1054 | blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH |
1055 | unless @line ; |
1056 | |
8e07c86e |
1057 | $static = 1 if $ret_type =~ s/^static\s+//; |
1058 | |
2304df62 |
1059 | $func_header = shift(@line); |
c2960299 |
1060 | blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH |
f8b8e0a4 |
1061 | unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s; |
c2960299 |
1062 | |
8e07c86e |
1063 | ($class, $func_name, $orig_args) = ($1, $2, $3) ; |
f480b56a |
1064 | $class = "$4 $class" if $4; |
2304df62 |
1065 | ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/; |
ff68c719 |
1066 | ($clean_func_name = $func_name) =~ s/^$Prefix//; |
1067 | $Full_func_name = "${Packid}_$clean_func_name"; |
ff0cee69 |
1068 | if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); } |
c2960299 |
1069 | |
1070 | # Check for duplicate function definition |
4230ab3f |
1071 | for $tmp (@XSStack) { |
1072 | next unless defined $tmp->{functions}{$Full_func_name}; |
ff68c719 |
1073 | Warn("Warning: duplicate function definition '$clean_func_name' detected"); |
4230ab3f |
1074 | last; |
8e07c86e |
1075 | } |
4230ab3f |
1076 | $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ; |
be3174d2 |
1077 | %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = (); |
ef50df4b |
1078 | $DoSetMagic = 1; |
c2960299 |
1079 | |
f8b8e0a4 |
1080 | $orig_args =~ s/\\\s*/ /g; # process line continuations |
1081 | |
cb79badd |
1082 | my %only_outlist; |
f8b8e0a4 |
1083 | if ($process_argtypes and $orig_args =~ /\S/) { |
1084 | my $args = "$orig_args ,"; |
14455d6c |
1085 | if ($args =~ /^( (??{ $C_arg }) , )* $ /x) { |
1086 | @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg); |
f8b8e0a4 |
1087 | for ( @args ) { |
1088 | s/^\s+//; |
1089 | s/\s+$//; |
1090 | my $arg = $_; |
1091 | my $default; |
1092 | ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x; |
1093 | my ($pre, $name) = ($arg =~ /(.*?) \s* \b(\w+) \s* $ /x); |
1094 | next unless length $pre; |
1095 | my $out_type; |
1096 | my $inout_var; |
0f568861 |
1097 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) { |
f8b8e0a4 |
1098 | my $type = $1; |
63385af5 |
1099 | $out_type = $type if $type ne 'IN'; |
0f568861 |
1100 | $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//; |
f8b8e0a4 |
1101 | } |
1102 | if (/\W/) { # Has a type |
1103 | push @arg_with_types, $arg; |
1104 | # warn "pushing '$arg'\n"; |
1105 | $arg_types{$name} = $arg; |
1106 | $_ = "$name$default"; |
1107 | } |
cb79badd |
1108 | $only_outlist{$_} = 1 if $out_type eq "OUTLIST"; |
0f568861 |
1109 | push @outlist, $name if $out_type =~ /OUTLIST$/; |
f8b8e0a4 |
1110 | $in_out{$name} = $out_type if $out_type; |
1111 | } |
1112 | } else { |
1113 | @args = split(/\s*,\s*/, $orig_args); |
1114 | Warn("Warning: cannot parse argument list '$orig_args', fallback to split"); |
1115 | } |
1116 | } else { |
1117 | @args = split(/\s*,\s*/, $orig_args); |
1118 | for (@args) { |
0f568861 |
1119 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) { |
f8b8e0a4 |
1120 | my $out_type = $1; |
63385af5 |
1121 | next if $out_type eq 'IN'; |
cb79badd |
1122 | $only_outlist{$_} = 1 if $out_type eq "OUTLIST"; |
0f568861 |
1123 | push @outlist, $name if $out_type =~ /OUTLIST$/; |
f8b8e0a4 |
1124 | $in_out{$_} = $out_type; |
1125 | } |
1126 | } |
1127 | } |
a0d0e21e |
1128 | if (defined($class)) { |
683d4eee |
1129 | my $arg0 = ((defined($static) or $func_name eq 'new') |
1130 | ? "CLASS" : "THIS"); |
8e07c86e |
1131 | unshift(@args, $arg0); |
f8b8e0a4 |
1132 | ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/; |
2304df62 |
1133 | } |
f8b8e0a4 |
1134 | my $extra_args = 0; |
1135 | @args_num = (); |
1136 | $num_args = 0; |
1137 | my $report_args = ''; |
1138 | foreach $i (0 .. $#args) { |
2304df62 |
1139 | if ($args[$i] =~ s/\.\.\.//) { |
1140 | $elipsis = 1; |
f8b8e0a4 |
1141 | if ($args[$i] eq '' && $i == $#args) { |
1142 | $report_args .= ", ..."; |
2304df62 |
1143 | pop(@args); |
1144 | last; |
1145 | } |
1146 | } |
cb79badd |
1147 | if ($only_outlist{$args[$i]}) { |
f8b8e0a4 |
1148 | push @args_num, undef; |
1149 | } else { |
1150 | push @args_num, ++$num_args; |
1151 | $report_args .= ", $args[$i]"; |
1152 | } |
8e07c86e |
1153 | if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) { |
f8b8e0a4 |
1154 | $extra_args++; |
2304df62 |
1155 | $args[$i] = $1; |
1156 | $defaults{$args[$i]} = $2; |
1157 | $defaults{$args[$i]} =~ s/"/\\"/g; |
1158 | } |
93d3b392 |
1159 | $proto_arg[$i+1] = "\$" ; |
2304df62 |
1160 | } |
f8b8e0a4 |
1161 | $min_args = $num_args - $extra_args; |
1162 | $report_args =~ s/"/\\"/g; |
1163 | $report_args =~ s/^,\s+//; |
1164 | my @func_args = @args; |
1165 | shift @func_args if defined($class); |
1166 | |
1167 | for (@func_args) { |
1168 | s/^/&/ if $in_out{$_}; |
2304df62 |
1169 | } |
f8b8e0a4 |
1170 | $func_args = join(", ", @func_args); |
1171 | @args_match{@args} = @args_num; |
2304df62 |
1172 | |
8e07c86e |
1173 | $PPCODE = grep(/^\s*PPCODE\s*:/, @line); |
93d3b392 |
1174 | $CODE = grep(/^\s*CODE\s*:/, @line); |
6c5fb52b |
1175 | # Detect CODE: blocks which use ST(n)= or XST_m*(n,v) |
1176 | # to set explicit return values. |
1177 | $EXPLICIT_RETURN = ($CODE && |
1178 | ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x )); |
8e07c86e |
1179 | $ALIAS = grep(/^\s*ALIAS\s*:/, @line); |
cfc02341 |
1180 | $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line); |
8e07c86e |
1181 | |
f8b8e0a4 |
1182 | $xsreturn = 1 if $EXPLICIT_RETURN; |
1183 | |
2304df62 |
1184 | # print function header |
a0d0e21e |
1185 | print Q<<"EOF"; |
05ceb97a |
1186 | #XS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */ |
ff68c719 |
1187 | #XS(XS_${Full_func_name}) |
2304df62 |
1188 | #[[ |
a0d0e21e |
1189 | # dXSARGS; |
93a17b20 |
1190 | EOF |
8e07c86e |
1191 | print Q<<"EOF" if $ALIAS ; |
1192 | # dXSI32; |
1193 | EOF |
cfc02341 |
1194 | print Q<<"EOF" if $INTERFACE ; |
1195 | # dXSFUNCTION($ret_type); |
1196 | EOF |
2304df62 |
1197 | if ($elipsis) { |
8e07c86e |
1198 | $cond = ($min_args ? qq(items < $min_args) : 0); |
2304df62 |
1199 | } |
1200 | elsif ($min_args == $num_args) { |
1201 | $cond = qq(items != $min_args); |
1202 | } |
1203 | else { |
1204 | $cond = qq(items < $min_args || items > $num_args); |
1205 | } |
8990e307 |
1206 | |
2304df62 |
1207 | print Q<<"EOF" if $except; |
1208 | # char errbuf[1024]; |
1209 | # *errbuf = '\0'; |
1210 | EOF |
1211 | |
8e07c86e |
1212 | if ($ALIAS) |
1213 | { print Q<<"EOF" if $cond } |
1214 | # if ($cond) |
f8b8e0a4 |
1215 | # Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv))); |
8e07c86e |
1216 | EOF |
1217 | else |
1218 | { print Q<<"EOF" if $cond } |
1219 | # if ($cond) |
f8b8e0a4 |
1220 | # Perl_croak(aTHX_ "Usage: $pname($report_args)"); |
93a17b20 |
1221 | EOF |
1222 | |
349b520e |
1223 | #gcc -Wall: if an xsub has no arguments and PPCODE is used |
1224 | #it is likely none of ST, XSRETURN or XSprePUSH macros are used |
1225 | #hence `ax' (setup by dXSARGS) is unused |
1226 | #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS |
1227 | #but such a move could break third-party extensions |
1228 | print Q<<"EOF" if $PPCODE and $num_args == 0; |
1229 | # PERL_UNUSED_VAR(ax); /* -Wall */ |
1230 | EOF |
1231 | |
a0d0e21e |
1232 | print Q<<"EOF" if $PPCODE; |
1233 | # SP -= items; |
1234 | EOF |
1235 | |
2304df62 |
1236 | # Now do a block of some sort. |
93a17b20 |
1237 | |
2304df62 |
1238 | $condnum = 0; |
8e07c86e |
1239 | $cond = ''; # last CASE: condidional |
1240 | push(@line, "$END:"); |
1241 | push(@line_no, $line_no[-1]); |
1242 | $_ = ''; |
1243 | &check_cpp; |
2304df62 |
1244 | while (@line) { |
8e07c86e |
1245 | &CASE_handler if check_keyword("CASE"); |
1246 | print Q<<"EOF"; |
1247 | # $except [[ |
93a17b20 |
1248 | EOF |
1249 | |
1250 | # do initialization of input variables |
1251 | $thisdone = 0; |
1252 | $retvaldone = 0; |
463ee0b2 |
1253 | $deferred = ""; |
c2960299 |
1254 | %arg_list = () ; |
1255 | $gotRETVAL = 0; |
f06db76b |
1256 | |
8fc38fda |
1257 | INPUT_handler() ; |
be3174d2 |
1258 | process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE") ; |
8fc38fda |
1259 | |
db3b9414 |
1260 | print Q<<"EOF" if $ScopeThisXSUB; |
1261 | # ENTER; |
1262 | # [[ |
1263 | EOF |
1264 | |
a0d0e21e |
1265 | if (!$thisdone && defined($class)) { |
683d4eee |
1266 | if (defined($static) or $func_name eq 'new') { |
a0d0e21e |
1267 | print "\tchar *"; |
1268 | $var_types{"CLASS"} = "char *"; |
1269 | &generate_init("char *", 1, "CLASS"); |
1270 | } |
1271 | else { |
93a17b20 |
1272 | print "\t$class *"; |
1273 | $var_types{"THIS"} = "$class *"; |
1274 | &generate_init("$class *", 1, "THIS"); |
a0d0e21e |
1275 | } |
93a17b20 |
1276 | } |
1277 | |
1278 | # do code |
1279 | if (/^\s*NOT_IMPLEMENTED_YET/) { |
cea2e8a9 |
1280 | print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n"; |
4633a7c4 |
1281 | $_ = '' ; |
93a17b20 |
1282 | } else { |
1283 | if ($ret_type ne "void") { |
ddf6bed1 |
1284 | print "\t" . &map_type($ret_type, 'RETVAL') . ";\n" |
93a17b20 |
1285 | if !$retvaldone; |
1286 | $args_match{"RETVAL"} = 0; |
1287 | $var_types{"RETVAL"} = $ret_type; |
b26a54d0 |
1288 | print "\tdXSTARG;\n" |
1289 | if $WantOptimize and $targetable{$type_kind{$ret_type}}; |
93a17b20 |
1290 | } |
db3b9414 |
1291 | |
f8b8e0a4 |
1292 | if (@arg_with_types) { |
1293 | unshift @line, @arg_with_types, $_; |
1294 | $_ = ""; |
1295 | $processing_arg_with_types = 1; |
1296 | INPUT_handler() ; |
1297 | } |
8e07c86e |
1298 | print $deferred; |
db3b9414 |
1299 | |
be3174d2 |
1300 | process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS") ; |
8e07c86e |
1301 | |
1302 | if (check_keyword("PPCODE")) { |
8fc38fda |
1303 | print_section(); |
8e07c86e |
1304 | death ("PPCODE must be last thing") if @line; |
db3b9414 |
1305 | print "\tLEAVE;\n" if $ScopeThisXSUB; |
a0d0e21e |
1306 | print "\tPUTBACK;\n\treturn;\n"; |
8e07c86e |
1307 | } elsif (check_keyword("CODE")) { |
8fc38fda |
1308 | print_section() ; |
1309 | } elsif (defined($class) and $func_name eq "DESTROY") { |
a0d0e21e |
1310 | print "\n\t"; |
8e07c86e |
1311 | print "delete THIS;\n"; |
93a17b20 |
1312 | } else { |
1313 | print "\n\t"; |
1314 | if ($ret_type ne "void") { |
463ee0b2 |
1315 | print "RETVAL = "; |
e50aee73 |
1316 | $wantRETVAL = 1; |
93a17b20 |
1317 | } |
1318 | if (defined($static)) { |
683d4eee |
1319 | if ($func_name eq 'new') { |
8fc38fda |
1320 | $func_name = "$class"; |
8e07c86e |
1321 | } else { |
1322 | print "${class}::"; |
a0d0e21e |
1323 | } |
93a17b20 |
1324 | } elsif (defined($class)) { |
683d4eee |
1325 | if ($func_name eq 'new') { |
8fc38fda |
1326 | $func_name .= " $class"; |
1327 | } else { |
93a17b20 |
1328 | print "THIS->"; |
8fc38fda |
1329 | } |
93a17b20 |
1330 | } |
e50aee73 |
1331 | $func_name =~ s/^($spat)// |
1332 | if defined($spat); |
cfc02341 |
1333 | $func_name = 'XSFUNCTION' if $interface; |
93a17b20 |
1334 | print "$func_name($func_args);\n"; |
93a17b20 |
1335 | } |
1336 | } |
1337 | |
1338 | # do output variables |
f8b8e0a4 |
1339 | $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section; |
1340 | undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section); |
1341 | # $wantRETVAL set if 'RETVAL =' autogenerated |
1342 | ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return; |
8e07c86e |
1343 | undef %outargs ; |
be3174d2 |
1344 | process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE"); |
f06db76b |
1345 | |
0f568861 |
1346 | &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic) |
1347 | for grep $in_out{$_} =~ /OUT$/, keys %in_out; |
1348 | |
f06db76b |
1349 | # all OUTPUT done, so now push the return value on the stack |
8e07c86e |
1350 | if ($gotRETVAL && $RETVAL_code) { |
1351 | print "\t$RETVAL_code\n"; |
1352 | } elsif ($gotRETVAL || $wantRETVAL) { |
b26a54d0 |
1353 | my $t = $WantOptimize && $targetable{$type_kind{$ret_type}}; |
1354 | my $var = 'RETVAL'; |
1355 | my $type = $ret_type; |
1356 | |
1357 | # 0: type, 1: with_size, 2: how, 3: how_size |
1358 | if ($t and not $t->[1] and $t->[0] eq 'p') { |
1359 | # PUSHp corresponds to setpvn. Treate setpv directly |
1360 | my $what = eval qq("$t->[2]"); |
1361 | warn $@ if $@; |
1362 | |
1363 | print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n"; |
f8b8e0a4 |
1364 | $prepush_done = 1; |
b26a54d0 |
1365 | } |
1366 | elsif ($t) { |
1367 | my $what = eval qq("$t->[2]"); |
1368 | warn $@ if $@; |
1369 | |
1370 | my $size = $t->[3]; |
1371 | $size = '' unless defined $size; |
1372 | $size = eval qq("$size"); |
1373 | warn $@ if $@; |
1374 | print "\tXSprePUSH; PUSH$t->[0]($what$size);\n"; |
f8b8e0a4 |
1375 | $prepush_done = 1; |
b26a54d0 |
1376 | } |
1377 | else { |
1378 | # RETVAL almost never needs SvSETMAGIC() |
1379 | &generate_output($ret_type, 0, 'RETVAL', 0); |
1380 | } |
8e07c86e |
1381 | } |
f06db76b |
1382 | |
f8b8e0a4 |
1383 | $xsreturn = 1 if $ret_type ne "void"; |
1384 | my $num = $xsreturn; |
0f568861 |
1385 | my $c = @outlist; |
f8b8e0a4 |
1386 | print "\tXSprePUSH;" if $c and not $prepush_done; |
1387 | print "\tEXTEND(SP,$c);\n" if $c; |
1388 | $xsreturn += $c; |
0f568861 |
1389 | generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist; |
f8b8e0a4 |
1390 | |
93a17b20 |
1391 | # do cleanup |
be3174d2 |
1392 | process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE") ; |
8e07c86e |
1393 | |
db3b9414 |
1394 | print Q<<"EOF" if $ScopeThisXSUB; |
1395 | # ]] |
1396 | EOF |
1397 | print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE; |
1398 | # LEAVE; |
1399 | EOF |
1400 | |
93a17b20 |
1401 | # print function trailer |
8e07c86e |
1402 | print Q<<EOF; |
2304df62 |
1403 | # ]] |
8e07c86e |
1404 | EOF |
1405 | print Q<<EOF if $except; |
8990e307 |
1406 | # BEGHANDLERS |
1407 | # CATCHALL |
1408 | # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason); |
1409 | # ENDHANDLERS |
93a17b20 |
1410 | EOF |
8e07c86e |
1411 | if (check_keyword("CASE")) { |
1412 | blurt ("Error: No `CASE:' at top of function") |
1413 | unless $condnum; |
1414 | $_ = "CASE: $_"; # Restore CASE: label |
1415 | next; |
8990e307 |
1416 | } |
8e07c86e |
1417 | last if $_ eq "$END:"; |
1418 | death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function"); |
2304df62 |
1419 | } |
a0d0e21e |
1420 | |
2304df62 |
1421 | print Q<<EOF if $except; |
1422 | # if (errbuf[0]) |
cea2e8a9 |
1423 | # Perl_croak(aTHX_ errbuf); |
2304df62 |
1424 | EOF |
a0d0e21e |
1425 | |
f8b8e0a4 |
1426 | if ($xsreturn) { |
93d3b392 |
1427 | print Q<<EOF unless $PPCODE; |
f8b8e0a4 |
1428 | # XSRETURN($xsreturn); |
a0d0e21e |
1429 | EOF |
93d3b392 |
1430 | } else { |
1431 | print Q<<EOF unless $PPCODE; |
1432 | # XSRETURN_EMPTY; |
1433 | EOF |
1434 | } |
a0d0e21e |
1435 | |
2304df62 |
1436 | print Q<<EOF; |
2304df62 |
1437 | #]] |
8990e307 |
1438 | # |
93a17b20 |
1439 | EOF |
382b8d97 |
1440 | |
4230ab3f |
1441 | my $newXS = "newXS" ; |
1442 | my $proto = "" ; |
1443 | |
382b8d97 |
1444 | # Build the prototype string for the xsub |
1445 | if ($ProtoThisXSUB) { |
4230ab3f |
1446 | $newXS = "newXSproto"; |
1447 | |
6f1abe2b |
1448 | if ($ProtoThisXSUB eq 2) { |
4230ab3f |
1449 | # User has specified empty prototype |
1450 | $proto = ', ""' ; |
1451 | } |
6f1abe2b |
1452 | elsif ($ProtoThisXSUB ne 1) { |
7d41bd0a |
1453 | # User has specified a prototype |
4230ab3f |
1454 | $proto = ', "' . $ProtoThisXSUB . '"'; |
382b8d97 |
1455 | } |
1456 | else { |
1457 | my $s = ';'; |
1458 | if ($min_args < $num_args) { |
1459 | $s = ''; |
1460 | $proto_arg[$min_args] .= ";" ; |
1461 | } |
4230ab3f |
1462 | push @proto_arg, "$s\@" |
382b8d97 |
1463 | if $elipsis ; |
1464 | |
4230ab3f |
1465 | $proto = ', "' . join ("", @proto_arg) . '"'; |
382b8d97 |
1466 | } |
1467 | } |
1468 | |
4230ab3f |
1469 | if (%XsubAliases) { |
1470 | $XsubAliases{$pname} = 0 |
1471 | unless defined $XsubAliases{$pname} ; |
1472 | while ( ($name, $value) = each %XsubAliases) { |
1473 | push(@InitFileCode, Q<<"EOF"); |
1474 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1475 | # XSANY.any_i32 = $value ; |
1476 | EOF |
1477 | push(@InitFileCode, Q<<"EOF") if $proto; |
1478 | # sv_setpv((SV*)cv$proto) ; |
1479 | EOF |
1480 | } |
cfc02341 |
1481 | } |
be3174d2 |
1482 | elsif (@Attributes) { |
1483 | push(@InitFileCode, Q<<"EOF"); |
1484 | # cv = newXS(\"$pname\", XS_$Full_func_name, file); |
1485 | # apply_attrs_string("$Package", cv, "@Attributes", 0); |
1486 | EOF |
1487 | } |
cfc02341 |
1488 | elsif ($interface) { |
1489 | while ( ($name, $value) = each %Interfaces) { |
1490 | $name = "$Package\::$name" unless $name =~ /::/; |
1491 | push(@InitFileCode, Q<<"EOF"); |
1492 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1493 | # $interface_macro_set(cv,$value) ; |
1494 | EOF |
1495 | push(@InitFileCode, Q<<"EOF") if $proto; |
1496 | # sv_setpv((SV*)cv$proto) ; |
1497 | EOF |
1498 | } |
4230ab3f |
1499 | } |
1500 | else { |
1501 | push(@InitFileCode, |
1502 | " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n"); |
1503 | } |
93a17b20 |
1504 | } |
1505 | |
1506 | # print initialization routine |
7ee8c957 |
1507 | |
e3b8966e |
1508 | print Q<<"EOF"; |
e3b8966e |
1509 | ##ifdef __cplusplus |
1510 | #extern "C" |
1511 | ##endif |
7ee8c957 |
1512 | EOF |
1513 | |
8990e307 |
1514 | print Q<<"EOF"; |
05ceb97a |
1515 | #XS(boot_$Module_cname); /* prototype to pass -Wmissing-prototypes */ |
a0d0e21e |
1516 | #XS(boot_$Module_cname) |
7ee8c957 |
1517 | EOF |
1518 | |
7ee8c957 |
1519 | print Q<<"EOF"; |
2304df62 |
1520 | #[[ |
a0d0e21e |
1521 | # dXSARGS; |
c6c619a9 |
1522 | EOF |
1523 | |
1524 | #-Wall: if there is no $Full_func_name there are no xsubs in this .xs |
1525 | #so `file' is unused |
1526 | print Q<<"EOF" if $Full_func_name; |
8990e307 |
1527 | # char* file = __FILE__; |
93a17b20 |
1528 | EOF |
1529 | |
c6c619a9 |
1530 | print Q "#\n"; |
1531 | |
8fc38fda |
1532 | print Q<<"EOF" if $WantVersionChk ; |
1533 | # XS_VERSION_BOOTCHECK ; |
1534 | # |
1535 | EOF |
1536 | |
cfc02341 |
1537 | print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ; |
8e07c86e |
1538 | # { |
1539 | # CV * cv ; |
1540 | # |
1541 | EOF |
1542 | |
4230ab3f |
1543 | print @InitFileCode; |
a0d0e21e |
1544 | |
cfc02341 |
1545 | print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ; |
8e07c86e |
1546 | # } |
1547 | EOF |
1548 | |
a0d0e21e |
1549 | if (@BootCode) |
1550 | { |
6f1abe2b |
1551 | print "\n /* Initialisation Section */\n\n" ; |
1552 | @line = @BootCode; |
1553 | print_section(); |
8e07c86e |
1554 | print "\n /* End of Initialisation Section */\n\n" ; |
93a17b20 |
1555 | } |
a0d0e21e |
1556 | |
e50aee73 |
1557 | print Q<<"EOF";; |
3280af22 |
1558 | # XSRETURN_YES; |
e50aee73 |
1559 | #]] |
e3b8966e |
1560 | # |
1561 | EOF |
1562 | |
8fc38fda |
1563 | warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") |
1564 | unless $ProtoUsed ; |
e50aee73 |
1565 | &Exit; |
1566 | |
93a17b20 |
1567 | sub output_init { |
ddf6bed1 |
1568 | local($type, $num, $var, $init, $name_printed) = @_; |
a0d0e21e |
1569 | local($arg) = "ST(" . ($num - 1) . ")"; |
93a17b20 |
1570 | |
7ad6fb0b |
1571 | if( $init =~ /^=/ ) { |
ddf6bed1 |
1572 | if ($name_printed) { |
1573 | eval qq/print " $init\\n"/; |
1574 | } else { |
1575 | eval qq/print "\\t$var $init\\n"/; |
1576 | } |
7ad6fb0b |
1577 | warn $@ if $@; |
1578 | } else { |
1579 | if( $init =~ s/^\+// && $num ) { |
ddf6bed1 |
1580 | &generate_init($type, $num, $var, $name_printed); |
1581 | } elsif ($name_printed) { |
1582 | print ";\n"; |
1583 | $init =~ s/^;//; |
7ad6fb0b |
1584 | } else { |
1585 | eval qq/print "\\t$var;\\n"/; |
1586 | warn $@ if $@; |
1587 | $init =~ s/^;//; |
1588 | } |
1589 | $deferred .= eval qq/"\\n\\t$init\\n"/; |
1590 | warn $@ if $@; |
1591 | } |
93a17b20 |
1592 | } |
1593 | |
c2960299 |
1594 | sub Warn |
1595 | { |
1596 | # work out the line number |
1597 | my $line_no = $line_no[@line_no - @line -1] ; |
1598 | |
1599 | print STDERR "@_ in $filename, line $line_no\n" ; |
1600 | } |
1601 | |
1602 | sub blurt |
1603 | { |
1604 | Warn @_ ; |
1605 | $errors ++ |
1606 | } |
1607 | |
1608 | sub death |
1609 | { |
1610 | Warn @_ ; |
1611 | exit 1 ; |
1612 | } |
8990e307 |
1613 | |
93a17b20 |
1614 | sub generate_init { |
2304df62 |
1615 | local($type, $num, $var) = @_; |
a0d0e21e |
1616 | local($arg) = "ST(" . ($num - 1) . ")"; |
2304df62 |
1617 | local($argoff) = $num - 1; |
1618 | local($ntype); |
1619 | local($tk); |
93a17b20 |
1620 | |
f06db76b |
1621 | $type = TidyType($type) ; |
c2960299 |
1622 | blurt("Error: '$type' not in typemap"), return |
1623 | unless defined($type_kind{$type}); |
1624 | |
2304df62 |
1625 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
8e07c86e |
1626 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1627 | $tk = $type_kind{$type}; |
1628 | $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/; |
8e07c86e |
1629 | $type =~ tr/:/_/; |
615ca1f4 |
1630 | blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return |
c2960299 |
1631 | unless defined $input_expr{$tk} ; |
2304df62 |
1632 | $expr = $input_expr{$tk}; |
1633 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1634 | blurt("Error: '$subtype' not in typemap"), return |
1635 | unless defined($type_kind{$subtype}); |
615ca1f4 |
1636 | blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return |
c2960299 |
1637 | unless defined $input_expr{$type_kind{$subtype}} ; |
2304df62 |
1638 | $subexpr = $input_expr{$type_kind{$subtype}}; |
f8c11764 |
1639 | $subexpr =~ s/\$type/\$subtype/g; |
2304df62 |
1640 | $subexpr =~ s/ntype/subtype/g; |
1641 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1642 | $subexpr =~ s/\n\t/\n\t\t/g; |
93d3b392 |
1643 | $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g; |
a0d0e21e |
1644 | $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/; |
2304df62 |
1645 | $expr =~ s/DO_ARRAY_ELEM/$subexpr/; |
1646 | } |
db3b9414 |
1647 | if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments |
1648 | $ScopeThisXSUB = 1; |
1649 | } |
2304df62 |
1650 | if (defined($defaults{$var})) { |
1651 | $expr =~ s/(\t+)/$1 /g; |
1652 | $expr =~ s/ /\t/g; |
ddf6bed1 |
1653 | if ($name_printed) { |
1654 | print ";\n"; |
1655 | } else { |
1656 | eval qq/print "\\t$var;\\n"/; |
1657 | warn $@ if $@; |
1658 | } |
a104f515 |
1659 | if ($defaults{$var} eq 'NO_INIT') { |
4628e4f8 |
1660 | $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/; |
1661 | } else { |
1662 | $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/; |
1663 | } |
7ad6fb0b |
1664 | warn $@ if $@; |
db3b9414 |
1665 | } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) { |
ddf6bed1 |
1666 | if ($name_printed) { |
1667 | print ";\n"; |
1668 | } else { |
1669 | eval qq/print "\\t$var;\\n"/; |
1670 | warn $@ if $@; |
1671 | } |
2304df62 |
1672 | $deferred .= eval qq/"\\n$expr;\\n"/; |
7ad6fb0b |
1673 | warn $@ if $@; |
2304df62 |
1674 | } else { |
ddf6bed1 |
1675 | die "panic: do not know how to handle this branch for function pointers" |
1676 | if $name_printed; |
2304df62 |
1677 | eval qq/print "$expr;\\n"/; |
7ad6fb0b |
1678 | warn $@ if $@; |
2304df62 |
1679 | } |
93a17b20 |
1680 | } |
1681 | |
1682 | sub generate_output { |
f8b8e0a4 |
1683 | local($type, $num, $var, $do_setmagic, $do_push) = @_; |
a0d0e21e |
1684 | local($arg) = "ST(" . ($num - ($num != 0)) . ")"; |
2304df62 |
1685 | local($argoff) = $num - 1; |
1686 | local($ntype); |
93a17b20 |
1687 | |
f06db76b |
1688 | $type = TidyType($type) ; |
2304df62 |
1689 | if ($type =~ /^array\(([^,]*),(.*)\)/) { |
f8c11764 |
1690 | print "\t$arg = sv_newmortal();\n"; |
4bd3d083 |
1691 | print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n"; |
ef50df4b |
1692 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
2304df62 |
1693 | } else { |
f06db76b |
1694 | blurt("Error: '$type' not in typemap"), return |
2304df62 |
1695 | unless defined($type_kind{$type}); |
615ca1f4 |
1696 | blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return |
c2960299 |
1697 | unless defined $output_expr{$type_kind{$type}} ; |
2304df62 |
1698 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
1699 | $ntype =~ s/\(\)//g; |
8e07c86e |
1700 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1701 | $expr = $output_expr{$type_kind{$type}}; |
1702 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1703 | blurt("Error: '$subtype' not in typemap"), return |
1704 | unless defined($type_kind{$subtype}); |
615ca1f4 |
1705 | blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return |
c2960299 |
1706 | unless defined $output_expr{$type_kind{$subtype}} ; |
2304df62 |
1707 | $subexpr = $output_expr{$type_kind{$subtype}}; |
1708 | $subexpr =~ s/ntype/subtype/g; |
1709 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1710 | $subexpr =~ s/\$var/${var}[ix_$var]/g; |
1711 | $subexpr =~ s/\n\t/\n\t\t/g; |
1712 | $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/; |
a0d0e21e |
1713 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1714 | warn $@ if $@; |
ef50df4b |
1715 | print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic; |
2304df62 |
1716 | } |
a0d0e21e |
1717 | elsif ($var eq 'RETVAL') { |
a2baab1c |
1718 | if ($expr =~ /^\t\$arg = new/) { |
1719 | # We expect that $arg has refcnt 1, so we need to |
1720 | # mortalize it. |
a0d0e21e |
1721 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1722 | warn $@ if $@; |
f8b8e0a4 |
1723 | print "\tsv_2mortal(ST($num));\n"; |
1724 | print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic; |
93a17b20 |
1725 | } |
a2baab1c |
1726 | elsif ($expr =~ /^\s*\$arg\s*=/) { |
1727 | # We expect that $arg has refcnt >=1, so we need |
d689ffdd |
1728 | # to mortalize it! |
a2baab1c |
1729 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1730 | warn $@ if $@; |
d689ffdd |
1731 | print "\tsv_2mortal(ST(0));\n"; |
ef50df4b |
1732 | print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; |
a2baab1c |
1733 | } |
2304df62 |
1734 | else { |
a2baab1c |
1735 | # Just hope that the entry would safely write it |
1736 | # over an already mortalized value. By |
1737 | # coincidence, something like $arg = &sv_undef |
1738 | # works too. |
8990e307 |
1739 | print "\tST(0) = sv_newmortal();\n"; |
a0d0e21e |
1740 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1741 | warn $@ if $@; |
ef50df4b |
1742 | # new mortals don't have set magic |
463ee0b2 |
1743 | } |
2304df62 |
1744 | } |
f8b8e0a4 |
1745 | elsif ($do_push) { |
1746 | print "\tPUSHs(sv_newmortal());\n"; |
1747 | $arg = "ST($num)"; |
1748 | eval "print qq\a$expr\a"; |
1749 | warn $@ if $@; |
1750 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
1751 | } |
a0d0e21e |
1752 | elsif ($arg =~ /^ST\(\d+\)$/) { |
1753 | eval "print qq\a$expr\a"; |
7ad6fb0b |
1754 | warn $@ if $@; |
ef50df4b |
1755 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
a0d0e21e |
1756 | } |
2304df62 |
1757 | } |
93a17b20 |
1758 | } |
1759 | |
1760 | sub map_type { |
ddf6bed1 |
1761 | my($type, $varname) = @_; |
93a17b20 |
1762 | |
8e07c86e |
1763 | $type =~ tr/:/_/; |
1764 | $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s; |
ddf6bed1 |
1765 | if ($varname) { |
1766 | if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) { |
1767 | (substr $type, pos $type, 0) = " $varname "; |
1768 | } else { |
1769 | $type .= "\t$varname"; |
1770 | } |
1771 | } |
8e07c86e |
1772 | $type; |
93a17b20 |
1773 | } |
8990e307 |
1774 | |
e50aee73 |
1775 | |
1776 | sub Exit { |
ff0cee69 |
1777 | # If this is VMS, the exit status has meaning to the shell, so we |
1778 | # use a predictable value (SS$_Normal or SS$_Abort) rather than an |
1779 | # arbitrary number. |
1780 | # exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ; |
1781 | exit ($errors ? 1 : 0); |
e50aee73 |
1782 | } |