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