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 | |
6f1abe2b |
9 | B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-typemap typemap>]... file.xs |
75f92628 |
10 | |
11 | =head1 DESCRIPTION |
12 | |
13 | I<xsubpp> will compile XS code into C code by embedding the constructs |
14 | necessary to let C functions manipulate Perl values and creates the glue |
15 | necessary to let Perl access those functions. The compiler uses typemaps to |
16 | determine how to map C function parameters and variables to Perl values. |
17 | |
18 | The compiler will search for typemap files called I<typemap>. It will use |
19 | the following search path to find default typemaps, with the rightmost |
20 | typemap taking precedence. |
21 | |
22 | ../../../typemap:../../typemap:../typemap:typemap |
23 | |
24 | =head1 OPTIONS |
25 | |
26 | =over 5 |
27 | |
28 | =item B<-C++> |
29 | |
30 | Adds ``extern "C"'' to the C code. |
31 | |
32 | |
33 | =item B<-except> |
34 | |
35 | Adds exception handling stubs to the C code. |
36 | |
37 | =item B<-typemap typemap> |
38 | |
39 | Indicates that a user-supplied typemap should take precedence over the |
40 | default typemaps. This option may be used multiple times, with the last |
41 | typemap having the highest precedence. |
42 | |
8e07c86e |
43 | =item B<-v> |
44 | |
45 | Prints the I<xsubpp> version number to standard output, then exits. |
46 | |
8fc38fda |
47 | =item B<-prototypes> |
382b8d97 |
48 | |
8fc38fda |
49 | By default I<xsubpp> will not automatically generate prototype code for |
50 | all xsubs. This flag will enable prototypes. |
51 | |
52 | =item B<-noversioncheck> |
53 | |
54 | Disables the run time test that determines if the object file (derived |
55 | from the C<.xs> file) and the C<.pm> files have the same version |
56 | number. |
382b8d97 |
57 | |
6f1abe2b |
58 | =item B<-nolinenumbers> |
59 | |
60 | Prevents the inclusion of `#line' directives in the output. |
61 | |
75f92628 |
62 | =back |
63 | |
64 | =head1 ENVIRONMENT |
65 | |
66 | No environment variables are used. |
67 | |
68 | =head1 AUTHOR |
69 | |
70 | Larry Wall |
71 | |
f06db76b |
72 | =head1 MODIFICATION HISTORY |
73 | |
8e07c86e |
74 | See the file F<changes.pod>. |
e50aee73 |
75 | |
75f92628 |
76 | =head1 SEE ALSO |
77 | |
55a00e51 |
78 | perl(1), perlxs(1), perlxstut(1) |
75f92628 |
79 | |
80 | =cut |
93a17b20 |
81 | |
382b8d97 |
82 | require 5.002; |
774d564b |
83 | use Cwd; |
4230ab3f |
84 | use vars '$cplusplus'; |
382b8d97 |
85 | |
01f988be |
86 | use Config; |
87 | |
aa689395 |
88 | sub Q ; |
89 | |
774d564b |
90 | # Global Constants |
774d564b |
91 | |
ef50df4b |
92 | $XSUBPP_version = "1.9506"; |
aa689395 |
93 | |
94 | my ($Is_VMS, $SymSet); |
95 | if ($^O eq 'VMS') { |
96 | $Is_VMS = 1; |
97 | # Establish set of global symbols with max length 28, since xsubpp |
98 | # will later add the 'XS_' prefix. |
99 | require ExtUtils::XSSymSet; |
100 | $SymSet = new ExtUtils::XSSymSet 28; |
101 | } |
8fc38fda |
102 | |
c07a80fd |
103 | $FH = 'File0000' ; |
8fc38fda |
104 | |
6f1abe2b |
105 | $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-s pattern] [-typemap typemap]... file.xs\n"; |
f06db76b |
106 | |
382b8d97 |
107 | $proto_re = "[" . quotemeta('\$%&*@;') . "]" ; |
01f988be |
108 | # mjn |
109 | $OBJ = 1 if $Config{'ccflags'} =~ /PERL_OBJECT/i; |
93a17b20 |
110 | |
8e07c86e |
111 | $except = ""; |
8fc38fda |
112 | $WantPrototypes = -1 ; |
113 | $WantVersionChk = 1 ; |
114 | $ProtoUsed = 0 ; |
6f1abe2b |
115 | $WantLineNumbers = 1 ; |
8e07c86e |
116 | SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) { |
93a17b20 |
117 | $flag = shift @ARGV; |
e50aee73 |
118 | $flag =~ s/^-// ; |
ff68c719 |
119 | $spat = quotemeta shift, next SWITCH if $flag eq 's'; |
8990e307 |
120 | $cplusplus = 1, next SWITCH if $flag eq 'C++'; |
382b8d97 |
121 | $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes'; |
122 | $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes'; |
8fc38fda |
123 | $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck'; |
124 | $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck'; |
8e07c86e |
125 | $except = " TRY", next SWITCH if $flag eq 'except'; |
8990e307 |
126 | push(@tm,shift), next SWITCH if $flag eq 'typemap'; |
6f1abe2b |
127 | $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers'; |
128 | $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers'; |
8e07c86e |
129 | (print "xsubpp version $XSUBPP_version\n"), exit |
130 | if $flag eq 'v'; |
93a17b20 |
131 | die $usage; |
132 | } |
8fc38fda |
133 | if ($WantPrototypes == -1) |
134 | { $WantPrototypes = 0} |
135 | else |
136 | { $ProtoUsed = 1 } |
137 | |
138 | |
8990e307 |
139 | @ARGV == 1 or die $usage; |
c2960299 |
140 | ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)# |
57497940 |
141 | or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)# |
c2960299 |
142 | or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)# |
8990e307 |
143 | or ($dir, $filename) = ('.', $ARGV[0]); |
144 | chdir($dir); |
774d564b |
145 | $pwd = cwd(); |
8fc38fda |
146 | |
147 | ++ $IncludedFiles{$ARGV[0]} ; |
93a17b20 |
148 | |
4230ab3f |
149 | my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs |
150 | my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA"); |
aa689395 |
151 | |
4230ab3f |
152 | |
f06db76b |
153 | sub TrimWhitespace |
154 | { |
155 | $_[0] =~ s/^\s+|\s+$//go ; |
156 | } |
157 | |
158 | sub TidyType |
159 | { |
160 | local ($_) = @_ ; |
161 | |
162 | # rationalise any '*' by joining them into bunches and removing whitespace |
163 | s#\s*(\*+)\s*#$1#g; |
e50aee73 |
164 | s#(\*+)# $1 #g ; |
f06db76b |
165 | |
166 | # change multiple whitespace into a single space |
167 | s/\s+/ /g ; |
168 | |
169 | # trim leading & trailing whitespace |
170 | TrimWhitespace($_) ; |
171 | |
172 | $_ ; |
173 | } |
174 | |
93a17b20 |
175 | $typemap = shift @ARGV; |
8990e307 |
176 | foreach $typemap (@tm) { |
177 | die "Can't find $typemap in $pwd\n" unless -r $typemap; |
93a17b20 |
178 | } |
748a9306 |
179 | unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap |
180 | ../../lib/ExtUtils/typemap ../../../typemap ../../typemap |
181 | ../typemap typemap); |
8990e307 |
182 | foreach $typemap (@tm) { |
f06db76b |
183 | next unless -e $typemap ; |
184 | # skip directories, binary files etc. |
185 | warn("Warning: ignoring non-text typemap file '$typemap'\n"), next |
186 | unless -T $typemap ; |
187 | open(TYPEMAP, $typemap) |
188 | or warn ("Warning: could not open typemap file '$typemap': $!\n"), next; |
e50aee73 |
189 | $mode = 'Typemap'; |
c2960299 |
190 | $junk = "" ; |
8990e307 |
191 | $current = \$junk; |
192 | while (<TYPEMAP>) { |
e50aee73 |
193 | next if /^\s*#/; |
e1f0c0aa |
194 | my $line_no = $. + 1; |
8e07c86e |
195 | if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; } |
196 | if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; } |
197 | if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; } |
e50aee73 |
198 | if ($mode eq 'Typemap') { |
199 | chomp; |
f06db76b |
200 | my $line = $_ ; |
201 | TrimWhitespace($_) ; |
202 | # skip blank lines and comment lines |
203 | next if /^$/ or /^#/ ; |
382b8d97 |
204 | my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or |
205 | warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next; |
206 | $type = TidyType($type) ; |
207 | $type_kind{$type} = $kind ; |
208 | # prototype defaults to '$' |
93d3b392 |
209 | $proto = "\$" unless $proto ; |
382b8d97 |
210 | warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") |
211 | unless ValidProtoString($proto) ; |
212 | $proto_letter{$type} = C_string($proto) ; |
8e07c86e |
213 | } |
214 | elsif (/^\s/) { |
215 | $$current .= $_; |
463ee0b2 |
216 | } |
e50aee73 |
217 | elsif ($mode eq 'Input') { |
8e07c86e |
218 | s/\s+$//; |
219 | $input_expr{$_} = ''; |
220 | $current = \$input_expr{$_}; |
93a17b20 |
221 | } |
8990e307 |
222 | else { |
8e07c86e |
223 | s/\s+$//; |
224 | $output_expr{$_} = ''; |
225 | $current = \$output_expr{$_}; |
93a17b20 |
226 | } |
8990e307 |
227 | } |
228 | close(TYPEMAP); |
229 | } |
93a17b20 |
230 | |
8990e307 |
231 | foreach $key (keys %input_expr) { |
232 | $input_expr{$key} =~ s/\n+$//; |
233 | } |
93a17b20 |
234 | |
8e07c86e |
235 | $END = "!End!\n\n"; # "impossible" keyword (multiple newline) |
236 | |
237 | # Match an XS keyword |
382b8d97 |
238 | $BLOCK_re= '\s*(' . join('|', qw( |
239 | REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT |
8fc38fda |
240 | CLEANUP ALIAS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE |
db3b9414 |
241 | SCOPE |
382b8d97 |
242 | )) . "|$END)\\s*:"; |
8e07c86e |
243 | |
244 | # Input: ($_, @line) == unparsed input. |
245 | # Output: ($_, @line) == (rest of line, following lines). |
246 | # Return: the matched keyword if found, otherwise 0 |
247 | sub check_keyword { |
248 | $_ = shift(@line) while !/\S/ && @line; |
249 | s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2; |
250 | } |
251 | |
252 | |
6f1abe2b |
253 | if ($WantLineNumbers) { |
254 | { |
255 | package xsubpp::counter; |
256 | sub TIEHANDLE { |
257 | my ($class, $cfile) = @_; |
258 | my $buf = ""; |
259 | $SECTION_END_MARKER = "#line --- \"$cfile\""; |
260 | $line_no = 1; |
261 | bless \$buf; |
262 | } |
263 | |
264 | sub PRINT { |
265 | my $self = shift; |
266 | for (@_) { |
267 | $$self .= $_; |
268 | while ($$self =~ s/^([^\n]*\n)//) { |
269 | my $line = $1; |
270 | ++ $line_no; |
271 | $line =~ s|^\#line\s+---(?=\s)|#line $line_no|; |
272 | print STDOUT $line; |
273 | } |
274 | } |
275 | } |
276 | |
277 | sub PRINTF { |
278 | my $self = shift; |
279 | my $fmt = shift; |
280 | $self->PRINT(sprintf($fmt, @_)); |
281 | } |
282 | |
283 | sub DESTROY { |
284 | # Not necessary if we're careful to end with a "\n" |
285 | my $self = shift; |
286 | print STDOUT $$self; |
287 | } |
288 | } |
289 | |
290 | my $cfile = $filename; |
291 | $cfile =~ s/\.xs$/.c/i or $cfile .= ".c"; |
292 | tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile); |
293 | select PSEUDO_STDOUT; |
294 | } |
295 | |
8e07c86e |
296 | sub print_section { |
6f1abe2b |
297 | # the "do" is required for right semantics |
298 | do { $_ = shift(@line) } while !/\S/ && @line; |
299 | |
300 | print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n") |
d3308daf |
301 | if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/; |
8e07c86e |
302 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { |
303 | print "$_\n"; |
304 | } |
6f1abe2b |
305 | print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers; |
8e07c86e |
306 | } |
307 | |
8fc38fda |
308 | sub process_keyword($) |
309 | { |
310 | my($pattern) = @_ ; |
311 | my $kwd ; |
312 | |
313 | &{"${kwd}_handler"}() |
314 | while $kwd = check_keyword($pattern) ; |
315 | } |
316 | |
8e07c86e |
317 | sub CASE_handler { |
318 | blurt ("Error: `CASE:' after unconditional `CASE:'") |
319 | if $condnum && $cond eq ''; |
320 | $cond = $_; |
321 | TrimWhitespace($cond); |
322 | print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n"); |
323 | $_ = '' ; |
324 | } |
325 | |
326 | sub INPUT_handler { |
327 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
328 | last if /^\s*NOT_IMPLEMENTED_YET/; |
329 | next unless /\S/; # skip blank lines |
330 | |
331 | TrimWhitespace($_) ; |
332 | my $line = $_ ; |
333 | |
334 | # remove trailing semicolon if no initialisation |
335 | s/\s*;$//g unless /=/ ; |
336 | |
337 | # check for optional initialisation code |
338 | my $var_init = '' ; |
339 | $var_init = $1 if s/\s*(=.*)$//s ; |
340 | $var_init =~ s/"/\\"/g; |
341 | |
342 | s/\s+/ /g; |
343 | my ($var_type, $var_addr, $var_name) = /^(.*?[^& ]) *(\&?) *\b(\w+)$/s |
344 | or blurt("Error: invalid argument declaration '$line'"), next; |
345 | |
346 | # Check for duplicate definitions |
347 | blurt ("Error: duplicate definition of argument '$var_name' ignored"), next |
348 | if $arg_list{$var_name} ++ ; |
349 | |
350 | $thisdone |= $var_name eq "THIS"; |
351 | $retvaldone |= $var_name eq "RETVAL"; |
352 | $var_types{$var_name} = $var_type; |
353 | print "\t" . &map_type($var_type); |
354 | $var_num = $args_match{$var_name}; |
382b8d97 |
355 | |
8fc38fda |
356 | $proto_arg[$var_num] = ProtoString($var_type) |
357 | if $var_num ; |
8e07c86e |
358 | if ($var_addr) { |
359 | $var_addr{$var_name} = 1; |
360 | $func_args =~ s/\b($var_name)\b/&$1/; |
361 | } |
362 | if ($var_init =~ /^=\s*NO_INIT\s*;?\s*$/) { |
363 | print "\t$var_name;\n"; |
364 | } elsif ($var_init =~ /\S/) { |
365 | &output_init($var_type, $var_num, "$var_name $var_init"); |
366 | } elsif ($var_num) { |
367 | # generate initialization code |
368 | &generate_init($var_type, $var_num, $var_name); |
369 | } else { |
370 | print ";\n"; |
371 | } |
372 | } |
373 | } |
374 | |
375 | sub OUTPUT_handler { |
376 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
377 | next unless /\S/; |
ef50df4b |
378 | if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) { |
379 | $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0); |
380 | next; |
381 | } |
8e07c86e |
382 | my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ; |
383 | blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next |
384 | if $outargs{$outarg} ++ ; |
385 | if (!$gotRETVAL and $outarg eq 'RETVAL') { |
386 | # deal with RETVAL last |
387 | $RETVAL_code = $outcode ; |
388 | $gotRETVAL = 1 ; |
389 | next ; |
390 | } |
391 | blurt ("Error: OUTPUT $outarg not an argument"), next |
392 | unless defined($args_match{$outarg}); |
393 | blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next |
394 | unless defined $var_types{$outarg} ; |
f78230ad |
395 | $var_num = $args_match{$outarg}; |
8e07c86e |
396 | if ($outcode) { |
397 | print "\t$outcode\n"; |
f78230ad |
398 | print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic; |
8e07c86e |
399 | } else { |
ef50df4b |
400 | &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic); |
8e07c86e |
401 | } |
402 | } |
403 | } |
404 | |
8fc38fda |
405 | sub CLEANUP_handler() { print_section() } |
406 | sub PREINIT_handler() { print_section() } |
407 | sub INIT_handler() { print_section() } |
408 | |
8e07c86e |
409 | sub GetAliases |
410 | { |
411 | my ($line) = @_ ; |
412 | my ($orig) = $line ; |
413 | my ($alias) ; |
414 | my ($value) ; |
415 | |
416 | # Parse alias definitions |
417 | # format is |
418 | # alias = value alias = value ... |
419 | |
420 | while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) { |
421 | $alias = $1 ; |
422 | $orig_alias = $alias ; |
423 | $value = $2 ; |
424 | |
425 | # check for optional package definition in the alias |
426 | $alias = $Packprefix . $alias if $alias !~ /::/ ; |
427 | |
428 | # check for duplicate alias name & duplicate value |
429 | Warn("Warning: Ignoring duplicate alias '$orig_alias'") |
4230ab3f |
430 | if defined $XsubAliases{$alias} ; |
8e07c86e |
431 | |
4230ab3f |
432 | Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values") |
433 | if $XsubAliasValues{$value} ; |
8e07c86e |
434 | |
4230ab3f |
435 | $XsubAliases = 1; |
436 | $XsubAliases{$alias} = $value ; |
437 | $XsubAliasValues{$value} = $orig_alias ; |
8e07c86e |
438 | } |
439 | |
440 | blurt("Error: Cannot parse ALIAS definitions from '$orig'") |
441 | if $line ; |
442 | } |
443 | |
382b8d97 |
444 | sub ALIAS_handler () |
8e07c86e |
445 | { |
446 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
447 | next unless /\S/; |
448 | TrimWhitespace($_) ; |
449 | GetAliases($_) if $_ ; |
450 | } |
451 | } |
452 | |
382b8d97 |
453 | sub REQUIRE_handler () |
8e07c86e |
454 | { |
455 | # the rest of the current line should contain a version number |
456 | my ($Ver) = $_ ; |
457 | |
458 | TrimWhitespace($Ver) ; |
459 | |
460 | death ("Error: REQUIRE expects a version number") |
461 | unless $Ver ; |
462 | |
463 | # check that the version number is of the form n.n |
464 | death ("Error: REQUIRE: expected a number, got '$Ver'") |
465 | unless $Ver =~ /^\d+(\.\d*)?/ ; |
466 | |
467 | death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.") |
468 | unless $XSUBPP_version >= $Ver ; |
469 | } |
470 | |
8fc38fda |
471 | sub VERSIONCHECK_handler () |
472 | { |
473 | # the rest of the current line should contain either ENABLE or |
474 | # DISABLE |
475 | |
476 | TrimWhitespace($_) ; |
477 | |
478 | # check for ENABLE/DISABLE |
479 | death ("Error: VERSIONCHECK: ENABLE/DISABLE") |
480 | unless /^(ENABLE|DISABLE)/i ; |
481 | |
482 | $WantVersionChk = 1 if $1 eq 'ENABLE' ; |
483 | $WantVersionChk = 0 if $1 eq 'DISABLE' ; |
484 | |
485 | } |
486 | |
382b8d97 |
487 | sub PROTOTYPE_handler () |
488 | { |
7d41bd0a |
489 | my $specified ; |
490 | |
c07a80fd |
491 | death("Error: Only 1 PROTOTYPE definition allowed per xsub") |
492 | if $proto_in_this_xsub ++ ; |
493 | |
382b8d97 |
494 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
495 | next unless /\S/; |
7d41bd0a |
496 | $specified = 1 ; |
382b8d97 |
497 | TrimWhitespace($_) ; |
498 | if ($_ eq 'DISABLE') { |
499 | $ProtoThisXSUB = 0 |
500 | } |
501 | elsif ($_ eq 'ENABLE') { |
502 | $ProtoThisXSUB = 1 |
503 | } |
504 | else { |
505 | # remove any whitespace |
506 | s/\s+//g ; |
507 | death("Error: Invalid prototype '$_'") |
508 | unless ValidProtoString($_) ; |
509 | $ProtoThisXSUB = C_string($_) ; |
510 | } |
511 | } |
c07a80fd |
512 | |
7d41bd0a |
513 | # If no prototype specified, then assume empty prototype "" |
514 | $ProtoThisXSUB = 2 unless $specified ; |
515 | |
8fc38fda |
516 | $ProtoUsed = 1 ; |
c07a80fd |
517 | |
382b8d97 |
518 | } |
519 | |
db3b9414 |
520 | sub SCOPE_handler () |
521 | { |
522 | death("Error: Only 1 SCOPE declaration allowed per xsub") |
523 | if $scope_in_this_xsub ++ ; |
524 | |
525 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { |
526 | next unless /\S/; |
527 | TrimWhitespace($_) ; |
528 | if ($_ =~ /^DISABLE/i) { |
529 | $ScopeThisXSUB = 0 |
530 | } |
531 | elsif ($_ =~ /^ENABLE/i) { |
532 | $ScopeThisXSUB = 1 |
533 | } |
534 | } |
535 | |
536 | } |
537 | |
382b8d97 |
538 | sub PROTOTYPES_handler () |
539 | { |
540 | # the rest of the current line should contain either ENABLE or |
541 | # DISABLE |
542 | |
543 | TrimWhitespace($_) ; |
544 | |
545 | # check for ENABLE/DISABLE |
546 | death ("Error: PROTOTYPES: ENABLE/DISABLE") |
547 | unless /^(ENABLE|DISABLE)/i ; |
548 | |
549 | $WantPrototypes = 1 if $1 eq 'ENABLE' ; |
550 | $WantPrototypes = 0 if $1 eq 'DISABLE' ; |
8fc38fda |
551 | $ProtoUsed = 1 ; |
382b8d97 |
552 | |
553 | } |
554 | |
8fc38fda |
555 | sub INCLUDE_handler () |
556 | { |
557 | # the rest of the current line should contain a valid filename |
558 | |
559 | TrimWhitespace($_) ; |
560 | |
8fc38fda |
561 | death("INCLUDE: filename missing") |
562 | unless $_ ; |
563 | |
564 | death("INCLUDE: output pipe is illegal") |
565 | if /^\s*\|/ ; |
566 | |
567 | # simple minded recursion detector |
568 | death("INCLUDE loop detected") |
569 | if $IncludedFiles{$_} ; |
570 | |
571 | ++ $IncludedFiles{$_} unless /\|\s*$/ ; |
572 | |
573 | # Save the current file context. |
4230ab3f |
574 | push(@XSStack, { |
575 | type => 'file', |
8fc38fda |
576 | LastLine => $lastline, |
577 | LastLineNo => $lastline_no, |
578 | Line => \@line, |
579 | LineNo => \@line_no, |
580 | Filename => $filename, |
c07a80fd |
581 | Handle => $FH, |
8fc38fda |
582 | }) ; |
583 | |
c07a80fd |
584 | ++ $FH ; |
8fc38fda |
585 | |
586 | # open the new file |
c07a80fd |
587 | open ($FH, "$_") or death("Cannot open '$_': $!") ; |
8fc38fda |
588 | |
589 | print Q<<"EOF" ; |
590 | # |
591 | #/* INCLUDE: Including '$_' from '$filename' */ |
592 | # |
593 | EOF |
594 | |
8fc38fda |
595 | $filename = $_ ; |
596 | |
c07a80fd |
597 | # Prime the pump by reading the first |
598 | # non-blank line |
599 | |
600 | # skip leading blank lines |
601 | while (<$FH>) { |
602 | last unless /^\s*$/ ; |
603 | } |
604 | |
605 | $lastline = $_ ; |
8fc38fda |
606 | $lastline_no = $. ; |
607 | |
608 | } |
609 | |
610 | sub PopFile() |
611 | { |
4230ab3f |
612 | return 0 unless $XSStack[-1]{type} eq 'file' ; |
613 | |
614 | my $data = pop @XSStack ; |
8fc38fda |
615 | my $ThisFile = $filename ; |
616 | my $isPipe = ($filename =~ /\|\s*$/) ; |
617 | |
618 | -- $IncludedFiles{$filename} |
619 | unless $isPipe ; |
620 | |
c07a80fd |
621 | close $FH ; |
8fc38fda |
622 | |
c07a80fd |
623 | $FH = $data->{Handle} ; |
8fc38fda |
624 | $filename = $data->{Filename} ; |
625 | $lastline = $data->{LastLine} ; |
626 | $lastline_no = $data->{LastLineNo} ; |
627 | @line = @{ $data->{Line} } ; |
628 | @line_no = @{ $data->{LineNo} } ; |
4230ab3f |
629 | |
8fc38fda |
630 | if ($isPipe and $? ) { |
631 | -- $lastline_no ; |
632 | print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ; |
633 | exit 1 ; |
634 | } |
635 | |
636 | print Q<<"EOF" ; |
637 | # |
638 | #/* INCLUDE: Returning to '$filename' from '$ThisFile' */ |
639 | # |
640 | EOF |
641 | |
642 | return 1 ; |
643 | } |
644 | |
382b8d97 |
645 | sub ValidProtoString ($) |
646 | { |
647 | my($string) = @_ ; |
648 | |
649 | if ( $string =~ /^$proto_re+$/ ) { |
650 | return $string ; |
651 | } |
652 | |
653 | return 0 ; |
654 | } |
655 | |
656 | sub C_string ($) |
657 | { |
658 | my($string) = @_ ; |
659 | |
660 | $string =~ s[\\][\\\\]g ; |
661 | $string ; |
662 | } |
663 | |
664 | sub ProtoString ($) |
665 | { |
666 | my ($type) = @_ ; |
667 | |
93d3b392 |
668 | $proto_letter{$type} or "\$" ; |
382b8d97 |
669 | } |
670 | |
8e07c86e |
671 | sub check_cpp { |
672 | my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line); |
673 | if (@cpp) { |
674 | my ($cpp, $cpplevel); |
675 | for $cpp (@cpp) { |
676 | if ($cpp =~ /^\#\s*if/) { |
677 | $cpplevel++; |
678 | } elsif (!$cpplevel) { |
679 | Warn("Warning: #else/elif/endif without #if in this function"); |
4230ab3f |
680 | print STDERR " (precede it with a blank line if the matching #if is outside the function)\n" |
681 | if $XSStack[-1]{type} eq 'if'; |
8e07c86e |
682 | return; |
683 | } elsif ($cpp =~ /^\#\s*endif/) { |
684 | $cpplevel--; |
685 | } |
686 | } |
687 | Warn("Warning: #if without #endif in this function") if $cpplevel; |
688 | } |
689 | } |
690 | |
691 | |
8990e307 |
692 | sub Q { |
e50aee73 |
693 | my($text) = @_; |
4633a7c4 |
694 | $text =~ s/^#//gm; |
2304df62 |
695 | $text =~ s/\[\[/{/g; |
696 | $text =~ s/\]\]/}/g; |
8990e307 |
697 | $text; |
93a17b20 |
698 | } |
699 | |
c07a80fd |
700 | open($FH, $filename) or die "cannot open $filename: $!\n"; |
c2960299 |
701 | |
f06db76b |
702 | # Identify the version of xsubpp used |
f06db76b |
703 | print <<EOM ; |
e50aee73 |
704 | /* |
705 | * This file was generated automatically by xsubpp version $XSUBPP_version from the |
93d3b392 |
706 | * contents of $filename. Do not edit this file, edit $filename instead. |
e50aee73 |
707 | * |
708 | * ANY CHANGES MADE HERE WILL BE LOST! |
f06db76b |
709 | * |
710 | */ |
e50aee73 |
711 | |
f06db76b |
712 | EOM |
6f1abe2b |
713 | |
714 | |
715 | print("#line 1 \"$filename\"\n") |
716 | if $WantLineNumbers; |
f06db76b |
717 | |
c07a80fd |
718 | while (<$FH>) { |
e50aee73 |
719 | last if ($Module, $Package, $Prefix) = |
720 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/; |
01f988be |
721 | |
722 | if ($OBJ) { |
723 | s/#if(?:def|\s+defined)\s+(\(__cplusplus\)|__cplusplus)/#if defined(__cplusplus) && !defined(PERL_OBJECT)/; |
724 | } |
a0d0e21e |
725 | print $_; |
93a17b20 |
726 | } |
e50aee73 |
727 | &Exit unless defined $_; |
728 | |
8fc38fda |
729 | $lastline = $_; |
730 | $lastline_no = $.; |
93a17b20 |
731 | |
c07a80fd |
732 | # Read next xsub into @line from ($lastline, <$FH>). |
2304df62 |
733 | sub fetch_para { |
734 | # parse paragraph |
4230ab3f |
735 | death ("Error: Unterminated `#if/#ifdef/#ifndef'") |
736 | if !defined $lastline && $XSStack[-1]{type} eq 'if'; |
2304df62 |
737 | @line = (); |
c2960299 |
738 | @line_no = () ; |
4230ab3f |
739 | return PopFile() if !defined $lastline; |
e50aee73 |
740 | |
741 | if ($lastline =~ |
742 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) { |
743 | $Module = $1; |
8e07c86e |
744 | $Package = defined($2) ? $2 : ''; # keep -w happy |
745 | $Prefix = defined($3) ? $3 : ''; # keep -w happy |
ff68c719 |
746 | $Prefix = quotemeta $Prefix ; |
e50aee73 |
747 | ($Module_cname = $Module) =~ s/\W/_/g; |
8e07c86e |
748 | ($Packid = $Package) =~ tr/:/_/; |
e50aee73 |
749 | $Packprefix = $Package; |
8e07c86e |
750 | $Packprefix .= "::" if $Packprefix ne ""; |
2304df62 |
751 | $lastline = ""; |
e50aee73 |
752 | } |
753 | |
754 | for(;;) { |
755 | if ($lastline !~ /^\s*#/ || |
4230ab3f |
756 | # CPP directives: |
757 | # ANSI: if ifdef ifndef elif else endif define undef |
758 | # line error pragma |
759 | # gcc: warning include_next |
760 | # obj-c: import |
761 | # others: ident (gcc notes that some cpps have this one) |
762 | $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) { |
e50aee73 |
763 | last if $lastline =~ /^\S/ && @line && $line[-1] eq ""; |
764 | push(@line, $lastline); |
765 | push(@line_no, $lastline_no) ; |
93a17b20 |
766 | } |
e50aee73 |
767 | |
768 | # Read next line and continuation lines |
c07a80fd |
769 | last unless defined($lastline = <$FH>); |
e50aee73 |
770 | $lastline_no = $.; |
771 | my $tmp_line; |
772 | $lastline .= $tmp_line |
c07a80fd |
773 | while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>)); |
e50aee73 |
774 | |
8e07c86e |
775 | chomp $lastline; |
e50aee73 |
776 | $lastline =~ s/^\s+$//; |
2304df62 |
777 | } |
e50aee73 |
778 | pop(@line), pop(@line_no) while @line && $line[-1] eq ""; |
e50aee73 |
779 | 1; |
2304df62 |
780 | } |
93a17b20 |
781 | |
c2960299 |
782 | PARAGRAPH: |
8e07c86e |
783 | while (fetch_para()) { |
e50aee73 |
784 | # Print initial preprocessor statements and blank lines |
4230ab3f |
785 | while (@line && $line[0] !~ /^[^\#]/) { |
786 | my $line = shift(@line); |
787 | print $line, "\n"; |
788 | next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/; |
789 | my $statement = $+; |
790 | if ($statement eq 'if') { |
791 | $XSS_work_idx = @XSStack; |
792 | push(@XSStack, {type => 'if'}); |
793 | } else { |
794 | death ("Error: `$statement' with no matching `if'") |
795 | if $XSStack[-1]{type} ne 'if'; |
796 | if ($XSStack[-1]{varname}) { |
797 | push(@InitFileCode, "#endif\n"); |
798 | push(@BootCode, "#endif"); |
799 | } |
800 | |
801 | my(@fns) = keys %{$XSStack[-1]{functions}}; |
802 | if ($statement ne 'endif') { |
803 | # Hide the functions defined in other #if branches, and reset. |
804 | @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns; |
805 | @{$XSStack[-1]}{qw(varname functions)} = ('', {}); |
806 | } else { |
807 | my($tmp) = pop(@XSStack); |
808 | 0 while (--$XSS_work_idx |
809 | && $XSStack[$XSS_work_idx]{type} ne 'if'); |
810 | # Keep all new defined functions |
811 | push(@fns, keys %{$tmp->{other_functions}}); |
812 | @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns; |
813 | } |
814 | } |
815 | } |
e50aee73 |
816 | |
817 | next PARAGRAPH unless @line; |
818 | |
4230ab3f |
819 | if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) { |
820 | # We are inside an #if, but have not yet #defined its xsubpp variable. |
821 | print "#define $cpp_next_tmp 1\n\n"; |
822 | push(@InitFileCode, "#if $cpp_next_tmp\n"); |
823 | push(@BootCode, "#if $cpp_next_tmp"); |
824 | $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++; |
825 | } |
826 | |
55497cff |
827 | death ("Code is not inside a function" |
828 | ." (maybe last function was ended by a blank line " |
829 | ." followed by a a statement on column one?)") |
e50aee73 |
830 | if $line[0] =~ /^\s/; |
831 | |
2304df62 |
832 | # initialize info arrays |
833 | undef(%args_match); |
834 | undef(%var_types); |
835 | undef(%var_addr); |
836 | undef(%defaults); |
837 | undef($class); |
838 | undef($static); |
839 | undef($elipsis); |
f06db76b |
840 | undef($wantRETVAL) ; |
841 | undef(%arg_list) ; |
382b8d97 |
842 | undef(@proto_arg) ; |
c07a80fd |
843 | undef($proto_in_this_xsub) ; |
db3b9414 |
844 | undef($scope_in_this_xsub) ; |
382b8d97 |
845 | $ProtoThisXSUB = $WantPrototypes ; |
db3b9414 |
846 | $ScopeThisXSUB = 0; |
2304df62 |
847 | |
8e07c86e |
848 | $_ = shift(@line); |
8fc38fda |
849 | while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) { |
850 | &{"${kwd}_handler"}() ; |
8e07c86e |
851 | next PARAGRAPH unless @line ; |
852 | $_ = shift(@line); |
853 | } |
c2960299 |
854 | |
8e07c86e |
855 | if (check_keyword("BOOT")) { |
856 | &check_cpp; |
6f1abe2b |
857 | push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"") |
858 | if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/; |
859 | push (@BootCode, @line, "") ; |
c2960299 |
860 | next PARAGRAPH ; |
a0d0e21e |
861 | } |
c2960299 |
862 | |
8e07c86e |
863 | |
864 | # extract return type, function name and arguments |
865 | my($ret_type) = TidyType($_); |
866 | |
c2960299 |
867 | # a function definition needs at least 2 lines |
868 | blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH |
869 | unless @line ; |
870 | |
8e07c86e |
871 | $static = 1 if $ret_type =~ s/^static\s+//; |
872 | |
2304df62 |
873 | $func_header = shift(@line); |
c2960299 |
874 | blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH |
8e07c86e |
875 | unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*$/s; |
c2960299 |
876 | |
8e07c86e |
877 | ($class, $func_name, $orig_args) = ($1, $2, $3) ; |
2304df62 |
878 | ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/; |
ff68c719 |
879 | ($clean_func_name = $func_name) =~ s/^$Prefix//; |
880 | $Full_func_name = "${Packid}_$clean_func_name"; |
ff0cee69 |
881 | if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); } |
c2960299 |
882 | |
883 | # Check for duplicate function definition |
4230ab3f |
884 | for $tmp (@XSStack) { |
885 | next unless defined $tmp->{functions}{$Full_func_name}; |
ff68c719 |
886 | Warn("Warning: duplicate function definition '$clean_func_name' detected"); |
4230ab3f |
887 | last; |
8e07c86e |
888 | } |
4230ab3f |
889 | $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ; |
890 | %XsubAliases = %XsubAliasValues = (); |
ef50df4b |
891 | $DoSetMagic = 1; |
c2960299 |
892 | |
2304df62 |
893 | @args = split(/\s*,\s*/, $orig_args); |
a0d0e21e |
894 | if (defined($class)) { |
683d4eee |
895 | my $arg0 = ((defined($static) or $func_name eq 'new') |
896 | ? "CLASS" : "THIS"); |
8e07c86e |
897 | unshift(@args, $arg0); |
898 | ($orig_args = "$arg0, $orig_args") =~ s/^$arg0, $/$arg0/; |
2304df62 |
899 | } |
900 | $orig_args =~ s/"/\\"/g; |
901 | $min_args = $num_args = @args; |
902 | foreach $i (0..$num_args-1) { |
903 | if ($args[$i] =~ s/\.\.\.//) { |
904 | $elipsis = 1; |
905 | $min_args--; |
c2960299 |
906 | if ($args[$i] eq '' && $i == $num_args - 1) { |
2304df62 |
907 | pop(@args); |
908 | last; |
909 | } |
910 | } |
8e07c86e |
911 | if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) { |
2304df62 |
912 | $min_args--; |
913 | $args[$i] = $1; |
914 | $defaults{$args[$i]} = $2; |
915 | $defaults{$args[$i]} =~ s/"/\\"/g; |
916 | } |
93d3b392 |
917 | $proto_arg[$i+1] = "\$" ; |
2304df62 |
918 | } |
a0d0e21e |
919 | if (defined($class)) { |
2304df62 |
920 | $func_args = join(", ", @args[1..$#args]); |
921 | } else { |
922 | $func_args = join(", ", @args); |
923 | } |
924 | @args_match{@args} = 1..@args; |
925 | |
8e07c86e |
926 | $PPCODE = grep(/^\s*PPCODE\s*:/, @line); |
93d3b392 |
927 | $CODE = grep(/^\s*CODE\s*:/, @line); |
6c5fb52b |
928 | # Detect CODE: blocks which use ST(n)= or XST_m*(n,v) |
929 | # to set explicit return values. |
930 | $EXPLICIT_RETURN = ($CODE && |
931 | ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x )); |
8e07c86e |
932 | $ALIAS = grep(/^\s*ALIAS\s*:/, @line); |
933 | |
2304df62 |
934 | # print function header |
a0d0e21e |
935 | print Q<<"EOF"; |
ff68c719 |
936 | #XS(XS_${Full_func_name}) |
2304df62 |
937 | #[[ |
a0d0e21e |
938 | # dXSARGS; |
93a17b20 |
939 | EOF |
8e07c86e |
940 | print Q<<"EOF" if $ALIAS ; |
941 | # dXSI32; |
942 | EOF |
2304df62 |
943 | if ($elipsis) { |
8e07c86e |
944 | $cond = ($min_args ? qq(items < $min_args) : 0); |
2304df62 |
945 | } |
946 | elsif ($min_args == $num_args) { |
947 | $cond = qq(items != $min_args); |
948 | } |
949 | else { |
950 | $cond = qq(items < $min_args || items > $num_args); |
951 | } |
8990e307 |
952 | |
2304df62 |
953 | print Q<<"EOF" if $except; |
954 | # char errbuf[1024]; |
955 | # *errbuf = '\0'; |
956 | EOF |
957 | |
8e07c86e |
958 | if ($ALIAS) |
959 | { print Q<<"EOF" if $cond } |
960 | # if ($cond) |
961 | # croak("Usage: %s($orig_args)", GvNAME(CvGV(cv))); |
962 | EOF |
963 | else |
964 | { print Q<<"EOF" if $cond } |
965 | # if ($cond) |
8990e307 |
966 | # croak("Usage: $pname($orig_args)"); |
93a17b20 |
967 | EOF |
968 | |
a0d0e21e |
969 | print Q<<"EOF" if $PPCODE; |
970 | # SP -= items; |
971 | EOF |
972 | |
2304df62 |
973 | # Now do a block of some sort. |
93a17b20 |
974 | |
2304df62 |
975 | $condnum = 0; |
8e07c86e |
976 | $cond = ''; # last CASE: condidional |
977 | push(@line, "$END:"); |
978 | push(@line_no, $line_no[-1]); |
979 | $_ = ''; |
980 | &check_cpp; |
2304df62 |
981 | while (@line) { |
8e07c86e |
982 | &CASE_handler if check_keyword("CASE"); |
983 | print Q<<"EOF"; |
984 | # $except [[ |
93a17b20 |
985 | EOF |
986 | |
987 | # do initialization of input variables |
988 | $thisdone = 0; |
989 | $retvaldone = 0; |
463ee0b2 |
990 | $deferred = ""; |
c2960299 |
991 | %arg_list = () ; |
992 | $gotRETVAL = 0; |
f06db76b |
993 | |
8fc38fda |
994 | INPUT_handler() ; |
db3b9414 |
995 | process_keyword("INPUT|PREINIT|ALIAS|PROTOTYPE|SCOPE") ; |
8fc38fda |
996 | |
db3b9414 |
997 | print Q<<"EOF" if $ScopeThisXSUB; |
998 | # ENTER; |
999 | # [[ |
1000 | EOF |
1001 | |
a0d0e21e |
1002 | if (!$thisdone && defined($class)) { |
683d4eee |
1003 | if (defined($static) or $func_name eq 'new') { |
a0d0e21e |
1004 | print "\tchar *"; |
1005 | $var_types{"CLASS"} = "char *"; |
1006 | &generate_init("char *", 1, "CLASS"); |
1007 | } |
1008 | else { |
93a17b20 |
1009 | print "\t$class *"; |
1010 | $var_types{"THIS"} = "$class *"; |
1011 | &generate_init("$class *", 1, "THIS"); |
a0d0e21e |
1012 | } |
93a17b20 |
1013 | } |
1014 | |
1015 | # do code |
1016 | if (/^\s*NOT_IMPLEMENTED_YET/) { |
4633a7c4 |
1017 | print "\n\tcroak(\"$pname: not implemented yet\");\n"; |
1018 | $_ = '' ; |
93a17b20 |
1019 | } else { |
1020 | if ($ret_type ne "void") { |
1021 | print "\t" . &map_type($ret_type) . "\tRETVAL;\n" |
1022 | if !$retvaldone; |
1023 | $args_match{"RETVAL"} = 0; |
1024 | $var_types{"RETVAL"} = $ret_type; |
1025 | } |
db3b9414 |
1026 | |
8e07c86e |
1027 | print $deferred; |
db3b9414 |
1028 | |
1029 | process_keyword("INIT|ALIAS|PROTOTYPE") ; |
8e07c86e |
1030 | |
1031 | if (check_keyword("PPCODE")) { |
8fc38fda |
1032 | print_section(); |
8e07c86e |
1033 | death ("PPCODE must be last thing") if @line; |
db3b9414 |
1034 | print "\tLEAVE;\n" if $ScopeThisXSUB; |
a0d0e21e |
1035 | print "\tPUTBACK;\n\treturn;\n"; |
8e07c86e |
1036 | } elsif (check_keyword("CODE")) { |
8fc38fda |
1037 | print_section() ; |
1038 | } elsif (defined($class) and $func_name eq "DESTROY") { |
a0d0e21e |
1039 | print "\n\t"; |
8e07c86e |
1040 | print "delete THIS;\n"; |
93a17b20 |
1041 | } else { |
1042 | print "\n\t"; |
1043 | if ($ret_type ne "void") { |
463ee0b2 |
1044 | print "RETVAL = "; |
e50aee73 |
1045 | $wantRETVAL = 1; |
93a17b20 |
1046 | } |
1047 | if (defined($static)) { |
683d4eee |
1048 | if ($func_name eq 'new') { |
8fc38fda |
1049 | $func_name = "$class"; |
8e07c86e |
1050 | } else { |
1051 | print "${class}::"; |
a0d0e21e |
1052 | } |
93a17b20 |
1053 | } elsif (defined($class)) { |
683d4eee |
1054 | if ($func_name eq 'new') { |
8fc38fda |
1055 | $func_name .= " $class"; |
1056 | } else { |
93a17b20 |
1057 | print "THIS->"; |
8fc38fda |
1058 | } |
93a17b20 |
1059 | } |
e50aee73 |
1060 | $func_name =~ s/^($spat)// |
1061 | if defined($spat); |
93a17b20 |
1062 | print "$func_name($func_args);\n"; |
93a17b20 |
1063 | } |
1064 | } |
1065 | |
1066 | # do output variables |
8e07c86e |
1067 | $gotRETVAL = 0; |
1068 | undef $RETVAL_code ; |
1069 | undef %outargs ; |
8fc38fda |
1070 | process_keyword("OUTPUT|ALIAS|PROTOTYPE"); |
f06db76b |
1071 | |
1072 | # all OUTPUT done, so now push the return value on the stack |
8e07c86e |
1073 | if ($gotRETVAL && $RETVAL_code) { |
1074 | print "\t$RETVAL_code\n"; |
1075 | } elsif ($gotRETVAL || $wantRETVAL) { |
ef50df4b |
1076 | # RETVAL almost never needs SvSETMAGIC() |
1077 | &generate_output($ret_type, 0, 'RETVAL', 0); |
8e07c86e |
1078 | } |
f06db76b |
1079 | |
93a17b20 |
1080 | # do cleanup |
8fc38fda |
1081 | process_keyword("CLEANUP|ALIAS|PROTOTYPE") ; |
8e07c86e |
1082 | |
db3b9414 |
1083 | print Q<<"EOF" if $ScopeThisXSUB; |
1084 | # ]] |
1085 | EOF |
1086 | print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE; |
1087 | # LEAVE; |
1088 | EOF |
1089 | |
93a17b20 |
1090 | # print function trailer |
8e07c86e |
1091 | print Q<<EOF; |
2304df62 |
1092 | # ]] |
8e07c86e |
1093 | EOF |
1094 | print Q<<EOF if $except; |
8990e307 |
1095 | # BEGHANDLERS |
1096 | # CATCHALL |
1097 | # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason); |
1098 | # ENDHANDLERS |
93a17b20 |
1099 | EOF |
8e07c86e |
1100 | if (check_keyword("CASE")) { |
1101 | blurt ("Error: No `CASE:' at top of function") |
1102 | unless $condnum; |
1103 | $_ = "CASE: $_"; # Restore CASE: label |
1104 | next; |
8990e307 |
1105 | } |
8e07c86e |
1106 | last if $_ eq "$END:"; |
1107 | death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function"); |
2304df62 |
1108 | } |
a0d0e21e |
1109 | |
2304df62 |
1110 | print Q<<EOF if $except; |
1111 | # if (errbuf[0]) |
1112 | # croak(errbuf); |
1113 | EOF |
a0d0e21e |
1114 | |
7c5b83de |
1115 | if ($ret_type ne "void" or $EXPLICIT_RETURN) { |
93d3b392 |
1116 | print Q<<EOF unless $PPCODE; |
a0d0e21e |
1117 | # XSRETURN(1); |
1118 | EOF |
93d3b392 |
1119 | } else { |
1120 | print Q<<EOF unless $PPCODE; |
1121 | # XSRETURN_EMPTY; |
1122 | EOF |
1123 | } |
a0d0e21e |
1124 | |
2304df62 |
1125 | print Q<<EOF; |
2304df62 |
1126 | #]] |
8990e307 |
1127 | # |
93a17b20 |
1128 | EOF |
382b8d97 |
1129 | |
4230ab3f |
1130 | my $newXS = "newXS" ; |
1131 | my $proto = "" ; |
1132 | |
382b8d97 |
1133 | # Build the prototype string for the xsub |
1134 | if ($ProtoThisXSUB) { |
4230ab3f |
1135 | $newXS = "newXSproto"; |
1136 | |
6f1abe2b |
1137 | if ($ProtoThisXSUB eq 2) { |
4230ab3f |
1138 | # User has specified empty prototype |
1139 | $proto = ', ""' ; |
1140 | } |
6f1abe2b |
1141 | elsif ($ProtoThisXSUB ne 1) { |
7d41bd0a |
1142 | # User has specified a prototype |
4230ab3f |
1143 | $proto = ', "' . $ProtoThisXSUB . '"'; |
382b8d97 |
1144 | } |
1145 | else { |
1146 | my $s = ';'; |
1147 | if ($min_args < $num_args) { |
1148 | $s = ''; |
1149 | $proto_arg[$min_args] .= ";" ; |
1150 | } |
4230ab3f |
1151 | push @proto_arg, "$s\@" |
382b8d97 |
1152 | if $elipsis ; |
1153 | |
4230ab3f |
1154 | $proto = ', "' . join ("", @proto_arg) . '"'; |
382b8d97 |
1155 | } |
1156 | } |
1157 | |
4230ab3f |
1158 | if (%XsubAliases) { |
1159 | $XsubAliases{$pname} = 0 |
1160 | unless defined $XsubAliases{$pname} ; |
1161 | while ( ($name, $value) = each %XsubAliases) { |
1162 | push(@InitFileCode, Q<<"EOF"); |
1163 | # cv = newXS(\"$name\", XS_$Full_func_name, file); |
1164 | # XSANY.any_i32 = $value ; |
1165 | EOF |
1166 | push(@InitFileCode, Q<<"EOF") if $proto; |
1167 | # sv_setpv((SV*)cv$proto) ; |
1168 | EOF |
1169 | } |
1170 | } |
1171 | else { |
1172 | push(@InitFileCode, |
1173 | " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n"); |
1174 | } |
93a17b20 |
1175 | } |
1176 | |
1177 | # print initialization routine |
8990e307 |
1178 | print Q<<"EOF"; |
4633a7c4 |
1179 | ##ifdef __cplusplus |
1180 | #extern "C" |
1181 | ##endif |
a0d0e21e |
1182 | #XS(boot_$Module_cname) |
2304df62 |
1183 | #[[ |
a0d0e21e |
1184 | # dXSARGS; |
8990e307 |
1185 | # char* file = __FILE__; |
1186 | # |
93a17b20 |
1187 | EOF |
1188 | |
8fc38fda |
1189 | print Q<<"EOF" if $WantVersionChk ; |
1190 | # XS_VERSION_BOOTCHECK ; |
1191 | # |
1192 | EOF |
1193 | |
4230ab3f |
1194 | print Q<<"EOF" if defined $XsubAliases ; |
8e07c86e |
1195 | # { |
1196 | # CV * cv ; |
1197 | # |
1198 | EOF |
1199 | |
4230ab3f |
1200 | print @InitFileCode; |
a0d0e21e |
1201 | |
4230ab3f |
1202 | print Q<<"EOF" if defined $XsubAliases ; |
8e07c86e |
1203 | # } |
1204 | EOF |
1205 | |
a0d0e21e |
1206 | if (@BootCode) |
1207 | { |
6f1abe2b |
1208 | print "\n /* Initialisation Section */\n\n" ; |
1209 | @line = @BootCode; |
1210 | print_section(); |
8e07c86e |
1211 | print "\n /* End of Initialisation Section */\n\n" ; |
93a17b20 |
1212 | } |
a0d0e21e |
1213 | |
e50aee73 |
1214 | print Q<<"EOF";; |
1215 | # ST(0) = &sv_yes; |
1216 | # XSRETURN(1); |
1217 | #]] |
1218 | EOF |
1219 | |
8fc38fda |
1220 | warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") |
1221 | unless $ProtoUsed ; |
e50aee73 |
1222 | &Exit; |
1223 | |
93a17b20 |
1224 | |
1225 | sub output_init { |
2304df62 |
1226 | local($type, $num, $init) = @_; |
a0d0e21e |
1227 | local($arg) = "ST(" . ($num - 1) . ")"; |
93a17b20 |
1228 | |
2304df62 |
1229 | eval qq/print " $init\\\n"/; |
93a17b20 |
1230 | } |
1231 | |
c2960299 |
1232 | sub Warn |
1233 | { |
1234 | # work out the line number |
1235 | my $line_no = $line_no[@line_no - @line -1] ; |
1236 | |
1237 | print STDERR "@_ in $filename, line $line_no\n" ; |
1238 | } |
1239 | |
1240 | sub blurt |
1241 | { |
1242 | Warn @_ ; |
1243 | $errors ++ |
1244 | } |
1245 | |
1246 | sub death |
1247 | { |
1248 | Warn @_ ; |
1249 | exit 1 ; |
1250 | } |
8990e307 |
1251 | |
93a17b20 |
1252 | sub generate_init { |
2304df62 |
1253 | local($type, $num, $var) = @_; |
a0d0e21e |
1254 | local($arg) = "ST(" . ($num - 1) . ")"; |
2304df62 |
1255 | local($argoff) = $num - 1; |
1256 | local($ntype); |
1257 | local($tk); |
93a17b20 |
1258 | |
f06db76b |
1259 | $type = TidyType($type) ; |
c2960299 |
1260 | blurt("Error: '$type' not in typemap"), return |
1261 | unless defined($type_kind{$type}); |
1262 | |
2304df62 |
1263 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
8e07c86e |
1264 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1265 | $tk = $type_kind{$type}; |
1266 | $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/; |
8e07c86e |
1267 | $type =~ tr/:/_/; |
c2960299 |
1268 | blurt("Error: No INPUT definition for type '$type' found"), return |
1269 | unless defined $input_expr{$tk} ; |
2304df62 |
1270 | $expr = $input_expr{$tk}; |
1271 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1272 | blurt("Error: '$subtype' not in typemap"), return |
1273 | unless defined($type_kind{$subtype}); |
1274 | blurt("Error: No INPUT definition for type '$subtype' found"), return |
1275 | unless defined $input_expr{$type_kind{$subtype}} ; |
2304df62 |
1276 | $subexpr = $input_expr{$type_kind{$subtype}}; |
1277 | $subexpr =~ s/ntype/subtype/g; |
1278 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1279 | $subexpr =~ s/\n\t/\n\t\t/g; |
93d3b392 |
1280 | $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g; |
a0d0e21e |
1281 | $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/; |
2304df62 |
1282 | $expr =~ s/DO_ARRAY_ELEM/$subexpr/; |
1283 | } |
db3b9414 |
1284 | if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments |
1285 | $ScopeThisXSUB = 1; |
1286 | } |
2304df62 |
1287 | if (defined($defaults{$var})) { |
1288 | $expr =~ s/(\t+)/$1 /g; |
1289 | $expr =~ s/ /\t/g; |
1290 | eval qq/print "\\t$var;\\n"/; |
1291 | $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/; |
db3b9414 |
1292 | } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) { |
2304df62 |
1293 | eval qq/print "\\t$var;\\n"/; |
1294 | $deferred .= eval qq/"\\n$expr;\\n"/; |
1295 | } else { |
1296 | eval qq/print "$expr;\\n"/; |
1297 | } |
93a17b20 |
1298 | } |
1299 | |
1300 | sub generate_output { |
ef50df4b |
1301 | local($type, $num, $var, $do_setmagic) = @_; |
a0d0e21e |
1302 | local($arg) = "ST(" . ($num - ($num != 0)) . ")"; |
2304df62 |
1303 | local($argoff) = $num - 1; |
1304 | local($ntype); |
93a17b20 |
1305 | |
f06db76b |
1306 | $type = TidyType($type) ; |
2304df62 |
1307 | if ($type =~ /^array\(([^,]*),(.*)\)/) { |
1308 | print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1)), XFree((char *)$var);\n"; |
ef50df4b |
1309 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
2304df62 |
1310 | } else { |
f06db76b |
1311 | blurt("Error: '$type' not in typemap"), return |
2304df62 |
1312 | unless defined($type_kind{$type}); |
c2960299 |
1313 | blurt("Error: No OUTPUT definition for type '$type' found"), return |
1314 | unless defined $output_expr{$type_kind{$type}} ; |
2304df62 |
1315 | ($ntype = $type) =~ s/\s*\*/Ptr/g; |
1316 | $ntype =~ s/\(\)//g; |
8e07c86e |
1317 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; |
2304df62 |
1318 | $expr = $output_expr{$type_kind{$type}}; |
1319 | if ($expr =~ /DO_ARRAY_ELEM/) { |
c2960299 |
1320 | blurt("Error: '$subtype' not in typemap"), return |
1321 | unless defined($type_kind{$subtype}); |
1322 | blurt("Error: No OUTPUT definition for type '$subtype' found"), return |
1323 | unless defined $output_expr{$type_kind{$subtype}} ; |
2304df62 |
1324 | $subexpr = $output_expr{$type_kind{$subtype}}; |
1325 | $subexpr =~ s/ntype/subtype/g; |
1326 | $subexpr =~ s/\$arg/ST(ix_$var)/g; |
1327 | $subexpr =~ s/\$var/${var}[ix_$var]/g; |
1328 | $subexpr =~ s/\n\t/\n\t\t/g; |
1329 | $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/; |
a0d0e21e |
1330 | eval "print qq\a$expr\a"; |
ef50df4b |
1331 | print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic; |
2304df62 |
1332 | } |
a0d0e21e |
1333 | elsif ($var eq 'RETVAL') { |
a2baab1c |
1334 | if ($expr =~ /^\t\$arg = new/) { |
1335 | # We expect that $arg has refcnt 1, so we need to |
1336 | # mortalize it. |
a0d0e21e |
1337 | eval "print qq\a$expr\a"; |
2304df62 |
1338 | print "\tsv_2mortal(ST(0));\n"; |
ef50df4b |
1339 | print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; |
93a17b20 |
1340 | } |
a2baab1c |
1341 | elsif ($expr =~ /^\s*\$arg\s*=/) { |
1342 | # We expect that $arg has refcnt >=1, so we need |
1343 | # to mortalize it. However, the extension may have |
1344 | # returned the built-in perl value, which is |
1345 | # read-only, thus not mortalizable. However, it is |
1346 | # safe to leave it as it is, since it would be |
1347 | # ignored by REFCNT_dec. Builtin values have REFCNT==0. |
1348 | eval "print qq\a$expr\a"; |
1349 | print "\tif (SvREFCNT(ST(0))) sv_2mortal(ST(0));\n"; |
ef50df4b |
1350 | print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; |
a2baab1c |
1351 | } |
2304df62 |
1352 | else { |
a2baab1c |
1353 | # Just hope that the entry would safely write it |
1354 | # over an already mortalized value. By |
1355 | # coincidence, something like $arg = &sv_undef |
1356 | # works too. |
8990e307 |
1357 | print "\tST(0) = sv_newmortal();\n"; |
a0d0e21e |
1358 | eval "print qq\a$expr\a"; |
ef50df4b |
1359 | # new mortals don't have set magic |
463ee0b2 |
1360 | } |
2304df62 |
1361 | } |
a0d0e21e |
1362 | elsif ($arg =~ /^ST\(\d+\)$/) { |
1363 | eval "print qq\a$expr\a"; |
ef50df4b |
1364 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; |
a0d0e21e |
1365 | } |
2304df62 |
1366 | } |
93a17b20 |
1367 | } |
1368 | |
1369 | sub map_type { |
e50aee73 |
1370 | my($type) = @_; |
93a17b20 |
1371 | |
8e07c86e |
1372 | $type =~ tr/:/_/; |
1373 | $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s; |
1374 | $type; |
93a17b20 |
1375 | } |
8990e307 |
1376 | |
e50aee73 |
1377 | |
1378 | sub Exit { |
ff0cee69 |
1379 | # If this is VMS, the exit status has meaning to the shell, so we |
1380 | # use a predictable value (SS$_Normal or SS$_Abort) rather than an |
1381 | # arbitrary number. |
1382 | # exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ; |
1383 | exit ($errors ? 1 : 0); |
e50aee73 |
1384 | } |