Commit | Line | Data |
---|---|---|
6b09c160 YST |
1 | package ExtUtils::ParseXS; |
2 | ||
3 | use 5.006; # We use /??{}/ in regexes | |
4 | use Cwd; | |
5 | use Config; | |
6 | use File::Basename; | |
7 | use File::Spec; | |
907ce46c | 8 | use Symbol; |
6b09c160 YST |
9 | |
10 | require Exporter; | |
11 | ||
12 | @ISA = qw(Exporter); | |
13 | @EXPORT_OK = qw(process_file); | |
14 | ||
15 | # use strict; # One of these days... | |
16 | ||
17 | my(@XSStack); # Stack of conditionals and INCLUDEs | |
18 | my($XSS_work_idx, $cpp_next_tmp); | |
19 | ||
20 | use vars qw($VERSION); | |
27856418 | 21 | $VERSION = '2.15'; |
6b09c160 YST |
22 | |
23 | use vars qw(%input_expr %output_expr $ProtoUsed @InitFileCode $FH $proto_re $Overload $errors $Fallback | |
24 | $cplusplus $hiertype $WantPrototypes $WantVersionChk $except $WantLineNumbers | |
25 | $WantOptimize $process_inout $process_argtypes @tm | |
26 | $dir $filename $filepathname %IncludedFiles | |
27 | %type_kind %proto_letter | |
28 | %targetable $BLOCK_re $lastline $lastline_no | |
29 | $Package $Prefix @line @BootCode %args_match %defaults %var_types %arg_list @proto_arg | |
30 | $processing_arg_with_types %argtype_seen @outlist %in_out %lengthof | |
31 | $proto_in_this_xsub $scope_in_this_xsub $interface $prepush_done $interface_macro $interface_macro_set | |
32 | $ProtoThisXSUB $ScopeThisXSUB $xsreturn | |
33 | @line_no $ret_type $func_header $orig_args | |
34 | ); # Add these just to get compilation to happen. | |
35 | ||
36 | ||
37 | sub process_file { | |
38 | ||
39 | # Allow for $package->process_file(%hash) in the future | |
40 | my ($pkg, %args) = @_ % 2 ? @_ : (__PACKAGE__, @_); | |
41 | ||
42 | $ProtoUsed = exists $args{prototypes}; | |
43 | ||
44 | # Set defaults. | |
45 | %args = ( | |
46 | # 'C++' => 0, # Doesn't seem to *do* anything... | |
47 | hiertype => 0, | |
48 | except => 0, | |
49 | prototypes => 0, | |
50 | versioncheck => 1, | |
51 | linenumbers => 1, | |
52 | optimize => 1, | |
53 | prototypes => 0, | |
54 | inout => 1, | |
55 | argtypes => 1, | |
56 | typemap => [], | |
57 | output => \*STDOUT, | |
008fb49c | 58 | csuffix => '.c', |
6b09c160 YST |
59 | %args, |
60 | ); | |
61 | ||
62 | # Global Constants | |
63 | ||
64 | my ($Is_VMS, $SymSet); | |
65 | if ($^O eq 'VMS') { | |
66 | $Is_VMS = 1; | |
67 | # Establish set of global symbols with max length 28, since xsubpp | |
68 | # will later add the 'XS_' prefix. | |
69 | require ExtUtils::XSSymSet; | |
70 | $SymSet = new ExtUtils::XSSymSet 28; | |
71 | } | |
72 | @XSStack = ({type => 'none'}); | |
73 | ($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA"); | |
74 | @InitFileCode = (); | |
907ce46c | 75 | $FH = Symbol::gensym(); |
6b09c160 YST |
76 | $proto_re = "[" . quotemeta('\$%&*@;[]') . "]" ; |
77 | $Overload = 0; | |
78 | $errors = 0; | |
79 | $Fallback = 'PL_sv_undef'; | |
80 | ||
81 | # Most of the 1500 lines below uses these globals. We'll have to | |
82 | # clean this up sometime, probably. For now, we just pull them out | |
83 | # of %args. -Ken | |
84 | ||
85 | $cplusplus = $args{'C++'}; | |
86 | $hiertype = $args{hiertype}; | |
87 | $WantPrototypes = $args{prototypes}; | |
88 | $WantVersionChk = $args{versioncheck}; | |
89 | $except = $args{except} ? ' TRY' : ''; | |
90 | $WantLineNumbers = $args{linenumbers}; | |
91 | $WantOptimize = $args{optimize}; | |
92 | $process_inout = $args{inout}; | |
93 | $process_argtypes = $args{argtypes}; | |
94 | @tm = ref $args{typemap} ? @{$args{typemap}} : ($args{typemap}); | |
95 | ||
96 | for ($args{filename}) { | |
97 | die "Missing required parameter 'filename'" unless $_; | |
98 | $filepathname = $_; | |
99 | ($dir, $filename) = (dirname($_), basename($_)); | |
100 | $filepathname =~ s/\\/\\\\/g; | |
101 | $IncludedFiles{$_}++; | |
102 | } | |
103 | ||
104 | # Open the input file | |
105 | open($FH, $args{filename}) or die "cannot open $args{filename}: $!\n"; | |
106 | ||
107 | # Open the output file if given as a string. If they provide some | |
108 | # other kind of reference, trust them that we can print to it. | |
109 | if (not ref $args{output}) { | |
110 | open my($fh), "> $args{output}" or die "Can't create $args{output}: $!"; | |
111 | $args{outfile} = $args{output}; | |
112 | $args{output} = $fh; | |
113 | } | |
114 | ||
115 | # Really, we shouldn't have to chdir() or select() in the first | |
116 | # place. For now, just save & restore. | |
117 | my $orig_cwd = cwd(); | |
118 | my $orig_fh = select(); | |
119 | ||
120 | chdir($dir); | |
121 | my $pwd = cwd(); | |
008fb49c | 122 | my $csuffix = $args{csuffix}; |
6b09c160 YST |
123 | |
124 | if ($WantLineNumbers) { | |
125 | my $cfile; | |
126 | if ( $args{outfile} ) { | |
127 | $cfile = $args{outfile}; | |
128 | } else { | |
129 | $cfile = $args{filename}; | |
008fb49c | 130 | $cfile =~ s/\.xs$/$csuffix/i or $cfile .= $csuffix; |
6b09c160 YST |
131 | } |
132 | tie(*PSEUDO_STDOUT, 'ExtUtils::ParseXS::CountLines', $cfile, $args{output}); | |
133 | select PSEUDO_STDOUT; | |
134 | } else { | |
135 | select $args{output}; | |
136 | } | |
137 | ||
138 | foreach my $typemap (@tm) { | |
139 | die "Can't find $typemap in $pwd\n" unless -r $typemap; | |
140 | } | |
141 | ||
142 | push @tm, standard_typemap_locations(); | |
143 | ||
144 | foreach my $typemap (@tm) { | |
145 | next unless -f $typemap ; | |
146 | # skip directories, binary files etc. | |
147 | warn("Warning: ignoring non-text typemap file '$typemap'\n"), next | |
148 | unless -T $typemap ; | |
149 | open(TYPEMAP, $typemap) | |
150 | or warn ("Warning: could not open typemap file '$typemap': $!\n"), next; | |
151 | my $mode = 'Typemap'; | |
152 | my $junk = "" ; | |
153 | my $current = \$junk; | |
154 | while (<TYPEMAP>) { | |
155 | next if /^\s* #/; | |
156 | my $line_no = $. + 1; | |
157 | if (/^INPUT\s*$/) { | |
158 | $mode = 'Input'; $current = \$junk; next; | |
159 | } | |
160 | if (/^OUTPUT\s*$/) { | |
161 | $mode = 'Output'; $current = \$junk; next; | |
162 | } | |
163 | if (/^TYPEMAP\s*$/) { | |
164 | $mode = 'Typemap'; $current = \$junk; next; | |
165 | } | |
166 | if ($mode eq 'Typemap') { | |
167 | chomp; | |
168 | my $line = $_ ; | |
169 | TrimWhitespace($_) ; | |
170 | # skip blank lines and comment lines | |
171 | next if /^$/ or /^#/ ; | |
172 | my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or | |
173 | warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next; | |
174 | $type = TidyType($type) ; | |
175 | $type_kind{$type} = $kind ; | |
176 | # prototype defaults to '$' | |
177 | $proto = "\$" unless $proto ; | |
178 | warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n") | |
179 | unless ValidProtoString($proto) ; | |
180 | $proto_letter{$type} = C_string($proto) ; | |
181 | } elsif (/^\s/) { | |
182 | $$current .= $_; | |
183 | } elsif ($mode eq 'Input') { | |
184 | s/\s+$//; | |
185 | $input_expr{$_} = ''; | |
186 | $current = \$input_expr{$_}; | |
187 | } else { | |
188 | s/\s+$//; | |
189 | $output_expr{$_} = ''; | |
190 | $current = \$output_expr{$_}; | |
191 | } | |
192 | } | |
193 | close(TYPEMAP); | |
194 | } | |
195 | ||
196 | foreach my $key (keys %input_expr) { | |
197 | $input_expr{$key} =~ s/;*\s+\z//; | |
198 | } | |
199 | ||
a8adbc99 RGS |
200 | my ($cast, $size); |
201 | our $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced | |
6b09c160 YST |
202 | $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast |
203 | $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn) | |
204 | ||
205 | foreach my $key (keys %output_expr) { | |
206 | use re 'eval'; | |
207 | ||
208 | my ($t, $with_size, $arg, $sarg) = | |
209 | ($output_expr{$key} =~ | |
210 | m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn | |
211 | \s* \( \s* $cast \$arg \s* , | |
212 | \s* ( (??{ $bal }) ) # Set from | |
213 | ( (??{ $size }) )? # Possible sizeof set-from | |
214 | \) \s* ; \s* $ | |
215 | ]x); | |
216 | $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t; | |
217 | } | |
218 | ||
219 | my $END = "!End!\n\n"; # "impossible" keyword (multiple newline) | |
220 | ||
221 | # Match an XS keyword | |
222 | $BLOCK_re= '\s*(' . join('|', qw( | |
223 | REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT | |
224 | CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE | |
225 | SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL OVERLOAD FALLBACK | |
226 | )) . "|$END)\\s*:"; | |
227 | ||
228 | ||
a8adbc99 | 229 | our ($C_group_rex, $C_arg); |
6b09c160 YST |
230 | # Group in C (no support for comments or literals) |
231 | $C_group_rex = qr/ [({\[] | |
232 | (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )* | |
233 | [)}\]] /x ; | |
234 | # Chunk in C without comma at toplevel (no comments): | |
235 | $C_arg = qr/ (?: (?> [^()\[\]{},"']+ ) | |
236 | | (??{ $C_group_rex }) | |
237 | | " (?: (?> [^\\"]+ ) | |
238 | | \\. | |
239 | )* " # String literal | |
240 | | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal | |
241 | )* /xs; | |
242 | ||
243 | # Identify the version of xsubpp used | |
244 | print <<EOM ; | |
245 | /* | |
246 | * This file was generated automatically by ExtUtils::ParseXS version $VERSION from the | |
247 | * contents of $filename. Do not edit this file, edit $filename instead. | |
248 | * | |
249 | * ANY CHANGES MADE HERE WILL BE LOST! | |
250 | * | |
251 | */ | |
252 | ||
253 | EOM | |
254 | ||
255 | ||
256 | print("#line 1 \"$filepathname\"\n") | |
257 | if $WantLineNumbers; | |
258 | ||
259 | firstmodule: | |
260 | while (<$FH>) { | |
261 | if (/^=/) { | |
262 | my $podstartline = $.; | |
263 | do { | |
264 | if (/^=cut\s*$/) { | |
265 | # We can't just write out a /* */ comment, as our embedded | |
266 | # POD might itself be in a comment. We can't put a /**/ | |
267 | # comment inside #if 0, as the C standard says that the source | |
268 | # file is decomposed into preprocessing characters in the stage | |
269 | # before preprocessing commands are executed. | |
270 | # I don't want to leave the text as barewords, because the spec | |
271 | # isn't clear whether macros are expanded before or after | |
272 | # preprocessing commands are executed, and someone pathological | |
273 | # may just have defined one of the 3 words as a macro that does | |
274 | # something strange. Multiline strings are illegal in C, so | |
275 | # the "" we write must be a string literal. And they aren't | |
276 | # concatenated until 2 steps later, so we are safe. | |
277 | # - Nicholas Clark | |
278 | print("#if 0\n \"Skipped embedded POD.\"\n#endif\n"); | |
279 | printf("#line %d \"$filepathname\"\n", $. + 1) | |
280 | if $WantLineNumbers; | |
281 | next firstmodule | |
282 | } | |
283 | ||
284 | } while (<$FH>); | |
285 | # At this point $. is at end of file so die won't state the start | |
286 | # of the problem, and as we haven't yet read any lines &death won't | |
287 | # show the correct line in the message either. | |
288 | die ("Error: Unterminated pod in $filename, line $podstartline\n") | |
289 | unless $lastline; | |
290 | } | |
291 | last if ($Package, $Prefix) = | |
292 | /^MODULE\s*=\s*[\w:]+(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/; | |
293 | ||
294 | print $_; | |
295 | } | |
296 | unless (defined $_) { | |
297 | warn "Didn't find a 'MODULE ... PACKAGE ... PREFIX' line\n"; | |
298 | exit 0; # Not a fatal error for the caller process | |
299 | } | |
300 | ||
301 | print <<"EOF"; | |
302 | #ifndef PERL_UNUSED_VAR | |
303 | # define PERL_UNUSED_VAR(var) if (0) var = var | |
304 | #endif | |
305 | ||
306 | EOF | |
307 | ||
308 | print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n" if $WantLineNumbers; | |
309 | ||
310 | $lastline = $_; | |
311 | $lastline_no = $.; | |
312 | ||
313 | PARAGRAPH: | |
314 | while (fetch_para()) { | |
315 | # Print initial preprocessor statements and blank lines | |
316 | while (@line && $line[0] !~ /^[^\#]/) { | |
317 | my $line = shift(@line); | |
318 | print $line, "\n"; | |
319 | next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/; | |
320 | my $statement = $+; | |
321 | if ($statement eq 'if') { | |
322 | $XSS_work_idx = @XSStack; | |
323 | push(@XSStack, {type => 'if'}); | |
324 | } else { | |
325 | death ("Error: `$statement' with no matching `if'") | |
326 | if $XSStack[-1]{type} ne 'if'; | |
327 | if ($XSStack[-1]{varname}) { | |
328 | push(@InitFileCode, "#endif\n"); | |
329 | push(@BootCode, "#endif"); | |
330 | } | |
331 | ||
332 | my(@fns) = keys %{$XSStack[-1]{functions}}; | |
333 | if ($statement ne 'endif') { | |
334 | # Hide the functions defined in other #if branches, and reset. | |
335 | @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns; | |
336 | @{$XSStack[-1]}{qw(varname functions)} = ('', {}); | |
337 | } else { | |
338 | my($tmp) = pop(@XSStack); | |
339 | 0 while (--$XSS_work_idx | |
340 | && $XSStack[$XSS_work_idx]{type} ne 'if'); | |
341 | # Keep all new defined functions | |
342 | push(@fns, keys %{$tmp->{other_functions}}); | |
343 | @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | next PARAGRAPH unless @line; | |
349 | ||
350 | if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) { | |
351 | # We are inside an #if, but have not yet #defined its xsubpp variable. | |
352 | print "#define $cpp_next_tmp 1\n\n"; | |
353 | push(@InitFileCode, "#if $cpp_next_tmp\n"); | |
354 | push(@BootCode, "#if $cpp_next_tmp"); | |
355 | $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++; | |
356 | } | |
357 | ||
358 | death ("Code is not inside a function" | |
359 | ." (maybe last function was ended by a blank line " | |
360 | ." followed by a statement on column one?)") | |
361 | if $line[0] =~ /^\s/; | |
362 | ||
008fb49c | 363 | my ($class, $externC, $static, $elipsis, $wantRETVAL, $RETVAL_no_return); |
6b09c160 YST |
364 | my (@fake_INPUT_pre); # For length(s) generated variables |
365 | my (@fake_INPUT); | |
366 | ||
367 | # initialize info arrays | |
368 | undef(%args_match); | |
369 | undef(%var_types); | |
370 | undef(%defaults); | |
371 | undef(%arg_list) ; | |
372 | undef(@proto_arg) ; | |
373 | undef($processing_arg_with_types) ; | |
374 | undef(%argtype_seen) ; | |
375 | undef(@outlist) ; | |
376 | undef(%in_out) ; | |
377 | undef(%lengthof) ; | |
378 | undef($proto_in_this_xsub) ; | |
379 | undef($scope_in_this_xsub) ; | |
380 | undef($interface); | |
381 | undef($prepush_done); | |
382 | $interface_macro = 'XSINTERFACE_FUNC' ; | |
383 | $interface_macro_set = 'XSINTERFACE_FUNC_SET' ; | |
384 | $ProtoThisXSUB = $WantPrototypes ; | |
385 | $ScopeThisXSUB = 0; | |
386 | $xsreturn = 0; | |
387 | ||
388 | $_ = shift(@line); | |
389 | while (my $kwd = check_keyword("REQUIRE|PROTOTYPES|FALLBACK|VERSIONCHECK|INCLUDE")) { | |
390 | &{"${kwd}_handler"}() ; | |
391 | next PARAGRAPH unless @line ; | |
392 | $_ = shift(@line); | |
393 | } | |
394 | ||
395 | if (check_keyword("BOOT")) { | |
396 | &check_cpp; | |
397 | push (@BootCode, "#line $line_no[@line_no - @line] \"$filepathname\"") | |
398 | if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/; | |
399 | push (@BootCode, @line, "") ; | |
400 | next PARAGRAPH ; | |
401 | } | |
402 | ||
403 | ||
404 | # extract return type, function name and arguments | |
405 | ($ret_type) = TidyType($_); | |
406 | $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//; | |
407 | ||
408 | # Allow one-line ANSI-like declaration | |
409 | unshift @line, $2 | |
410 | if $process_argtypes | |
411 | and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s; | |
412 | ||
413 | # a function definition needs at least 2 lines | |
414 | blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH | |
415 | unless @line ; | |
416 | ||
008fb49c JH |
417 | $externC = 1 if $ret_type =~ s/^extern "C"\s+//; |
418 | $static = 1 if $ret_type =~ s/^static\s+//; | |
6b09c160 YST |
419 | |
420 | $func_header = shift(@line); | |
421 | blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH | |
422 | unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s; | |
423 | ||
424 | ($class, $func_name, $orig_args) = ($1, $2, $3) ; | |
425 | $class = "$4 $class" if $4; | |
426 | ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/; | |
427 | ($clean_func_name = $func_name) =~ s/^$Prefix//; | |
428 | $Full_func_name = "${Packid}_$clean_func_name"; | |
429 | if ($Is_VMS) { | |
430 | $Full_func_name = $SymSet->addsym($Full_func_name); | |
431 | } | |
432 | ||
433 | # Check for duplicate function definition | |
434 | for my $tmp (@XSStack) { | |
435 | next unless defined $tmp->{functions}{$Full_func_name}; | |
436 | Warn("Warning: duplicate function definition '$clean_func_name' detected"); | |
437 | last; | |
438 | } | |
439 | $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ; | |
440 | %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = (); | |
441 | $DoSetMagic = 1; | |
442 | ||
443 | $orig_args =~ s/\\\s*/ /g; # process line continuations | |
444 | my @args; | |
445 | ||
446 | my %only_C_inlist; # Not in the signature of Perl function | |
447 | if ($process_argtypes and $orig_args =~ /\S/) { | |
448 | my $args = "$orig_args ,"; | |
449 | if ($args =~ /^( (??{ $C_arg }) , )* $ /x) { | |
450 | @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg); | |
451 | for ( @args ) { | |
452 | s/^\s+//; | |
453 | s/\s+$//; | |
454 | my ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x; | |
455 | my ($pre, $name) = ($arg =~ /(.*?) \s* | |
456 | \b ( \w+ | length\( \s*\w+\s* \) ) | |
457 | \s* $ /x); | |
458 | next unless defined($pre) && length($pre); | |
459 | my $out_type; | |
460 | my $inout_var; | |
461 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) { | |
462 | my $type = $1; | |
463 | $out_type = $type if $type ne 'IN'; | |
464 | $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//; | |
465 | $pre =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//; | |
466 | } | |
467 | my $islength; | |
468 | if ($name =~ /^length\( \s* (\w+) \s* \)\z/x) { | |
469 | $name = "XSauto_length_of_$1"; | |
470 | $islength = 1; | |
471 | die "Default value on length() argument: `$_'" | |
472 | if length $default; | |
473 | } | |
474 | if (length $pre or $islength) { # Has a type | |
475 | if ($islength) { | |
476 | push @fake_INPUT_pre, $arg; | |
477 | } else { | |
478 | push @fake_INPUT, $arg; | |
479 | } | |
480 | # warn "pushing '$arg'\n"; | |
481 | $argtype_seen{$name}++; | |
482 | $_ = "$name$default"; # Assigns to @args | |
483 | } | |
484 | $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST" or $islength; | |
485 | push @outlist, $name if $out_type =~ /OUTLIST$/; | |
486 | $in_out{$name} = $out_type if $out_type; | |
487 | } | |
488 | } else { | |
489 | @args = split(/\s*,\s*/, $orig_args); | |
490 | Warn("Warning: cannot parse argument list '$orig_args', fallback to split"); | |
491 | } | |
492 | } else { | |
493 | @args = split(/\s*,\s*/, $orig_args); | |
494 | for (@args) { | |
495 | if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) { | |
496 | my $out_type = $1; | |
497 | next if $out_type eq 'IN'; | |
498 | $only_C_inlist{$_} = 1 if $out_type eq "OUTLIST"; | |
499 | push @outlist, $name if $out_type =~ /OUTLIST$/; | |
500 | $in_out{$_} = $out_type; | |
501 | } | |
502 | } | |
503 | } | |
504 | if (defined($class)) { | |
505 | my $arg0 = ((defined($static) or $func_name eq 'new') | |
506 | ? "CLASS" : "THIS"); | |
507 | unshift(@args, $arg0); | |
508 | ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/; | |
509 | } | |
510 | my $extra_args = 0; | |
511 | @args_num = (); | |
512 | $num_args = 0; | |
513 | my $report_args = ''; | |
514 | foreach my $i (0 .. $#args) { | |
515 | if ($args[$i] =~ s/\.\.\.//) { | |
516 | $elipsis = 1; | |
517 | if ($args[$i] eq '' && $i == $#args) { | |
518 | $report_args .= ", ..."; | |
519 | pop(@args); | |
520 | last; | |
521 | } | |
522 | } | |
523 | if ($only_C_inlist{$args[$i]}) { | |
524 | push @args_num, undef; | |
525 | } else { | |
526 | push @args_num, ++$num_args; | |
527 | $report_args .= ", $args[$i]"; | |
528 | } | |
529 | if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) { | |
530 | $extra_args++; | |
531 | $args[$i] = $1; | |
532 | $defaults{$args[$i]} = $2; | |
533 | $defaults{$args[$i]} =~ s/"/\\"/g; | |
534 | } | |
535 | $proto_arg[$i+1] = '$' ; | |
536 | } | |
537 | $min_args = $num_args - $extra_args; | |
538 | $report_args =~ s/"/\\"/g; | |
539 | $report_args =~ s/^,\s+//; | |
540 | my @func_args = @args; | |
541 | shift @func_args if defined($class); | |
542 | ||
543 | for (@func_args) { | |
544 | s/^/&/ if $in_out{$_}; | |
545 | } | |
546 | $func_args = join(", ", @func_args); | |
547 | @args_match{@args} = @args_num; | |
548 | ||
549 | $PPCODE = grep(/^\s*PPCODE\s*:/, @line); | |
550 | $CODE = grep(/^\s*CODE\s*:/, @line); | |
551 | # Detect CODE: blocks which use ST(n)= or XST_m*(n,v) | |
552 | # to set explicit return values. | |
553 | $EXPLICIT_RETURN = ($CODE && | |
554 | ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x )); | |
555 | $ALIAS = grep(/^\s*ALIAS\s*:/, @line); | |
556 | $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line); | |
557 | ||
558 | $xsreturn = 1 if $EXPLICIT_RETURN; | |
559 | ||
008fb49c JH |
560 | $externC = $externC ? qq[extern "C"] : ""; |
561 | ||
6b09c160 YST |
562 | # print function header |
563 | print Q(<<"EOF"); | |
008fb49c | 564 | #$externC |
6b09c160 YST |
565 | #XS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */ |
566 | #XS(XS_${Full_func_name}) | |
567 | #[[ | |
568 | # dXSARGS; | |
569 | EOF | |
570 | print Q(<<"EOF") if $ALIAS ; | |
571 | # dXSI32; | |
572 | EOF | |
573 | print Q(<<"EOF") if $INTERFACE ; | |
574 | # dXSFUNCTION($ret_type); | |
575 | EOF | |
576 | if ($elipsis) { | |
577 | $cond = ($min_args ? qq(items < $min_args) : 0); | |
578 | } elsif ($min_args == $num_args) { | |
579 | $cond = qq(items != $min_args); | |
580 | } else { | |
581 | $cond = qq(items < $min_args || items > $num_args); | |
582 | } | |
583 | ||
584 | print Q(<<"EOF") if $except; | |
585 | # char errbuf[1024]; | |
586 | # *errbuf = '\0'; | |
587 | EOF | |
588 | ||
589 | if ($ALIAS) | |
590 | { print Q(<<"EOF") if $cond } | |
591 | # if ($cond) | |
592 | # Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv))); | |
593 | EOF | |
594 | else | |
595 | { print Q(<<"EOF") if $cond } | |
596 | # if ($cond) | |
597 | # Perl_croak(aTHX_ "Usage: $pname($report_args)"); | |
598 | EOF | |
599 | ||
600 | # cv doesn't seem to be used, in most cases unless we go in | |
601 | # the if of this else | |
602 | print Q(<<"EOF"); | |
603 | # PERL_UNUSED_VAR(cv); /* -W */ | |
604 | EOF | |
605 | ||
606 | #gcc -Wall: if an xsub has PPCODE is used | |
607 | #it is possible none of ST, XSRETURN or XSprePUSH macros are used | |
608 | #hence `ax' (setup by dXSARGS) is unused | |
609 | #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS | |
610 | #but such a move could break third-party extensions | |
611 | print Q(<<"EOF") if $PPCODE; | |
612 | # PERL_UNUSED_VAR(ax); /* -Wall */ | |
613 | EOF | |
614 | ||
615 | print Q(<<"EOF") if $PPCODE; | |
616 | # SP -= items; | |
617 | EOF | |
618 | ||
619 | # Now do a block of some sort. | |
620 | ||
621 | $condnum = 0; | |
622 | $cond = ''; # last CASE: condidional | |
623 | push(@line, "$END:"); | |
624 | push(@line_no, $line_no[-1]); | |
625 | $_ = ''; | |
626 | &check_cpp; | |
627 | while (@line) { | |
628 | &CASE_handler if check_keyword("CASE"); | |
629 | print Q(<<"EOF"); | |
630 | # $except [[ | |
631 | EOF | |
632 | ||
633 | # do initialization of input variables | |
634 | $thisdone = 0; | |
635 | $retvaldone = 0; | |
636 | $deferred = ""; | |
637 | %arg_list = () ; | |
638 | $gotRETVAL = 0; | |
639 | ||
640 | INPUT_handler() ; | |
641 | process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE|OVERLOAD") ; | |
642 | ||
643 | print Q(<<"EOF") if $ScopeThisXSUB; | |
644 | # ENTER; | |
645 | # [[ | |
646 | EOF | |
647 | ||
648 | if (!$thisdone && defined($class)) { | |
649 | if (defined($static) or $func_name eq 'new') { | |
650 | print "\tchar *"; | |
651 | $var_types{"CLASS"} = "char *"; | |
652 | &generate_init("char *", 1, "CLASS"); | |
653 | } | |
654 | else { | |
655 | print "\t$class *"; | |
656 | $var_types{"THIS"} = "$class *"; | |
657 | &generate_init("$class *", 1, "THIS"); | |
658 | } | |
659 | } | |
660 | ||
661 | # do code | |
662 | if (/^\s*NOT_IMPLEMENTED_YET/) { | |
663 | print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n"; | |
664 | $_ = '' ; | |
665 | } else { | |
666 | if ($ret_type ne "void") { | |
667 | print "\t" . &map_type($ret_type, 'RETVAL') . ";\n" | |
668 | if !$retvaldone; | |
669 | $args_match{"RETVAL"} = 0; | |
670 | $var_types{"RETVAL"} = $ret_type; | |
671 | print "\tdXSTARG;\n" | |
672 | if $WantOptimize and $targetable{$type_kind{$ret_type}}; | |
673 | } | |
674 | ||
675 | if (@fake_INPUT or @fake_INPUT_pre) { | |
676 | unshift @line, @fake_INPUT_pre, @fake_INPUT, $_; | |
677 | $_ = ""; | |
678 | $processing_arg_with_types = 1; | |
679 | INPUT_handler() ; | |
680 | } | |
681 | print $deferred; | |
682 | ||
683 | process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS|OVERLOAD") ; | |
684 | ||
685 | if (check_keyword("PPCODE")) { | |
686 | print_section(); | |
687 | death ("PPCODE must be last thing") if @line; | |
688 | print "\tLEAVE;\n" if $ScopeThisXSUB; | |
689 | print "\tPUTBACK;\n\treturn;\n"; | |
690 | } elsif (check_keyword("CODE")) { | |
691 | print_section() ; | |
692 | } elsif (defined($class) and $func_name eq "DESTROY") { | |
693 | print "\n\t"; | |
694 | print "delete THIS;\n"; | |
695 | } else { | |
696 | print "\n\t"; | |
697 | if ($ret_type ne "void") { | |
698 | print "RETVAL = "; | |
699 | $wantRETVAL = 1; | |
700 | } | |
701 | if (defined($static)) { | |
702 | if ($func_name eq 'new') { | |
703 | $func_name = "$class"; | |
704 | } else { | |
705 | print "${class}::"; | |
706 | } | |
707 | } elsif (defined($class)) { | |
708 | if ($func_name eq 'new') { | |
709 | $func_name .= " $class"; | |
710 | } else { | |
711 | print "THIS->"; | |
712 | } | |
713 | } | |
714 | $func_name =~ s/^\Q$args{'s'}// | |
715 | if exists $args{'s'}; | |
716 | $func_name = 'XSFUNCTION' if $interface; | |
717 | print "$func_name($func_args);\n"; | |
718 | } | |
719 | } | |
720 | ||
721 | # do output variables | |
722 | $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section; | |
723 | undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section); | |
724 | # $wantRETVAL set if 'RETVAL =' autogenerated | |
725 | ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return; | |
726 | undef %outargs ; | |
727 | process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE|OVERLOAD"); | |
728 | ||
729 | &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic) | |
730 | for grep $in_out{$_} =~ /OUT$/, keys %in_out; | |
731 | ||
732 | # all OUTPUT done, so now push the return value on the stack | |
733 | if ($gotRETVAL && $RETVAL_code) { | |
734 | print "\t$RETVAL_code\n"; | |
735 | } elsif ($gotRETVAL || $wantRETVAL) { | |
736 | my $t = $WantOptimize && $targetable{$type_kind{$ret_type}}; | |
737 | my $var = 'RETVAL'; | |
738 | my $type = $ret_type; | |
739 | ||
740 | # 0: type, 1: with_size, 2: how, 3: how_size | |
741 | if ($t and not $t->[1] and $t->[0] eq 'p') { | |
742 | # PUSHp corresponds to setpvn. Treate setpv directly | |
743 | my $what = eval qq("$t->[2]"); | |
744 | warn $@ if $@; | |
745 | ||
746 | print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n"; | |
747 | $prepush_done = 1; | |
748 | } | |
749 | elsif ($t) { | |
750 | my $what = eval qq("$t->[2]"); | |
751 | warn $@ if $@; | |
752 | ||
753 | my $size = $t->[3]; | |
754 | $size = '' unless defined $size; | |
755 | $size = eval qq("$size"); | |
756 | warn $@ if $@; | |
757 | print "\tXSprePUSH; PUSH$t->[0]($what$size);\n"; | |
758 | $prepush_done = 1; | |
759 | } | |
760 | else { | |
761 | # RETVAL almost never needs SvSETMAGIC() | |
762 | &generate_output($ret_type, 0, 'RETVAL', 0); | |
763 | } | |
764 | } | |
765 | ||
766 | $xsreturn = 1 if $ret_type ne "void"; | |
767 | my $num = $xsreturn; | |
768 | my $c = @outlist; | |
769 | print "\tXSprePUSH;" if $c and not $prepush_done; | |
770 | print "\tEXTEND(SP,$c);\n" if $c; | |
771 | $xsreturn += $c; | |
772 | generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist; | |
773 | ||
774 | # do cleanup | |
775 | process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE|OVERLOAD") ; | |
776 | ||
777 | print Q(<<"EOF") if $ScopeThisXSUB; | |
778 | # ]] | |
779 | EOF | |
780 | print Q(<<"EOF") if $ScopeThisXSUB and not $PPCODE; | |
781 | # LEAVE; | |
782 | EOF | |
783 | ||
784 | # print function trailer | |
785 | print Q(<<"EOF"); | |
786 | # ]] | |
787 | EOF | |
788 | print Q(<<"EOF") if $except; | |
789 | # BEGHANDLERS | |
790 | # CATCHALL | |
791 | # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason); | |
792 | # ENDHANDLERS | |
793 | EOF | |
794 | if (check_keyword("CASE")) { | |
795 | blurt ("Error: No `CASE:' at top of function") | |
796 | unless $condnum; | |
797 | $_ = "CASE: $_"; # Restore CASE: label | |
798 | next; | |
799 | } | |
800 | last if $_ eq "$END:"; | |
801 | death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function"); | |
802 | } | |
803 | ||
804 | print Q(<<"EOF") if $except; | |
805 | # if (errbuf[0]) | |
806 | # Perl_croak(aTHX_ errbuf); | |
807 | EOF | |
808 | ||
809 | if ($xsreturn) { | |
810 | print Q(<<"EOF") unless $PPCODE; | |
811 | # XSRETURN($xsreturn); | |
812 | EOF | |
813 | } else { | |
814 | print Q(<<"EOF") unless $PPCODE; | |
815 | # XSRETURN_EMPTY; | |
816 | EOF | |
817 | } | |
818 | ||
819 | print Q(<<"EOF"); | |
820 | #]] | |
821 | # | |
822 | EOF | |
823 | ||
824 | my $newXS = "newXS" ; | |
825 | my $proto = "" ; | |
826 | ||
827 | # Build the prototype string for the xsub | |
828 | if ($ProtoThisXSUB) { | |
829 | $newXS = "newXSproto"; | |
830 | ||
831 | if ($ProtoThisXSUB eq 2) { | |
832 | # User has specified empty prototype | |
833 | } | |
834 | elsif ($ProtoThisXSUB eq 1) { | |
835 | my $s = ';'; | |
836 | if ($min_args < $num_args) { | |
837 | $s = ''; | |
838 | $proto_arg[$min_args] .= ";" ; | |
839 | } | |
840 | push @proto_arg, "$s\@" | |
841 | if $elipsis ; | |
842 | ||
843 | $proto = join ("", grep defined, @proto_arg); | |
844 | } | |
845 | else { | |
846 | # User has specified a prototype | |
847 | $proto = $ProtoThisXSUB; | |
848 | } | |
849 | $proto = qq{, "$proto"}; | |
850 | } | |
851 | ||
852 | if (%XsubAliases) { | |
853 | $XsubAliases{$pname} = 0 | |
854 | unless defined $XsubAliases{$pname} ; | |
855 | while ( ($name, $value) = each %XsubAliases) { | |
856 | push(@InitFileCode, Q(<<"EOF")); | |
857 | # cv = newXS(\"$name\", XS_$Full_func_name, file); | |
858 | # XSANY.any_i32 = $value ; | |
859 | EOF | |
860 | push(@InitFileCode, Q(<<"EOF")) if $proto; | |
861 | # sv_setpv((SV*)cv$proto) ; | |
862 | EOF | |
863 | } | |
864 | } | |
865 | elsif (@Attributes) { | |
866 | push(@InitFileCode, Q(<<"EOF")); | |
867 | # cv = newXS(\"$pname\", XS_$Full_func_name, file); | |
868 | # apply_attrs_string("$Package", cv, "@Attributes", 0); | |
869 | EOF | |
870 | } | |
871 | elsif ($interface) { | |
872 | while ( ($name, $value) = each %Interfaces) { | |
873 | $name = "$Package\::$name" unless $name =~ /::/; | |
874 | push(@InitFileCode, Q(<<"EOF")); | |
875 | # cv = newXS(\"$name\", XS_$Full_func_name, file); | |
876 | # $interface_macro_set(cv,$value) ; | |
877 | EOF | |
878 | push(@InitFileCode, Q(<<"EOF")) if $proto; | |
879 | # sv_setpv((SV*)cv$proto) ; | |
880 | EOF | |
881 | } | |
882 | } | |
883 | else { | |
884 | push(@InitFileCode, | |
885 | " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n"); | |
886 | } | |
887 | } | |
888 | ||
889 | if ($Overload) # make it findable with fetchmethod | |
890 | { | |
891 | print Q(<<"EOF"); | |
892 | #XS(XS_${Packid}_nil); /* prototype to pass -Wmissing-prototypes */ | |
893 | #XS(XS_${Packid}_nil) | |
894 | #{ | |
895 | # XSRETURN_EMPTY; | |
896 | #} | |
897 | # | |
898 | EOF | |
899 | unshift(@InitFileCode, <<"MAKE_FETCHMETHOD_WORK"); | |
900 | /* Making a sub named "${Package}::()" allows the package */ | |
901 | /* to be findable via fetchmethod(), and causes */ | |
902 | /* overload::Overloaded("${Package}") to return true. */ | |
903 | newXS("${Package}::()", XS_${Packid}_nil, file$proto); | |
904 | MAKE_FETCHMETHOD_WORK | |
905 | } | |
906 | ||
907 | # print initialization routine | |
908 | ||
909 | print Q(<<"EOF"); | |
910 | ##ifdef __cplusplus | |
911 | #extern "C" | |
912 | ##endif | |
913 | EOF | |
914 | ||
915 | print Q(<<"EOF"); | |
916 | #XS(boot_$Module_cname); /* prototype to pass -Wmissing-prototypes */ | |
917 | #XS(boot_$Module_cname) | |
918 | EOF | |
919 | ||
920 | print Q(<<"EOF"); | |
921 | #[[ | |
922 | # dXSARGS; | |
923 | EOF | |
924 | ||
925 | #-Wall: if there is no $Full_func_name there are no xsubs in this .xs | |
926 | #so `file' is unused | |
927 | print Q(<<"EOF") if $Full_func_name; | |
928 | # char* file = __FILE__; | |
929 | EOF | |
930 | ||
931 | print Q("#\n"); | |
932 | ||
933 | print Q(<<"EOF"); | |
934 | # PERL_UNUSED_VAR(cv); /* -W */ | |
935 | # PERL_UNUSED_VAR(items); /* -W */ | |
936 | EOF | |
937 | ||
938 | print Q(<<"EOF") if $WantVersionChk ; | |
939 | # XS_VERSION_BOOTCHECK ; | |
940 | # | |
941 | EOF | |
942 | ||
943 | print Q(<<"EOF") if defined $XsubAliases or defined $Interfaces ; | |
944 | # { | |
945 | # CV * cv ; | |
946 | # | |
947 | EOF | |
948 | ||
949 | print Q(<<"EOF") if ($Overload); | |
950 | # /* register the overloading (type 'A') magic */ | |
951 | # PL_amagic_generation++; | |
952 | # /* The magic for overload gets a GV* via gv_fetchmeth as */ | |
953 | # /* mentioned above, and looks in the SV* slot of it for */ | |
954 | # /* the "fallback" status. */ | |
955 | # sv_setsv( | |
956 | # get_sv( "${Package}::()", TRUE ), | |
957 | # $Fallback | |
958 | # ); | |
959 | EOF | |
960 | ||
961 | print @InitFileCode; | |
962 | ||
963 | print Q(<<"EOF") if defined $XsubAliases or defined $Interfaces ; | |
964 | # } | |
965 | EOF | |
966 | ||
967 | if (@BootCode) | |
968 | { | |
969 | print "\n /* Initialisation Section */\n\n" ; | |
970 | @line = @BootCode; | |
971 | print_section(); | |
972 | print "\n /* End of Initialisation Section */\n\n" ; | |
973 | } | |
974 | ||
975 | print Q(<<"EOF"); | |
976 | # XSRETURN_YES; | |
977 | #]] | |
978 | # | |
979 | EOF | |
980 | ||
981 | warn("Please specify prototyping behavior for $filename (see perlxs manual)\n") | |
982 | unless $ProtoUsed ; | |
983 | ||
984 | chdir($orig_cwd); | |
985 | select($orig_fh); | |
986 | untie *PSEUDO_STDOUT if tied *PSEUDO_STDOUT; | |
907ce46c | 987 | close $FH; |
6b09c160 YST |
988 | |
989 | return 1; | |
990 | } | |
991 | ||
992 | sub errors { $errors } | |
993 | ||
994 | sub standard_typemap_locations { | |
995 | # Add all the default typemap locations to the search path | |
996 | my @tm = qw(typemap); | |
997 | ||
998 | my $updir = File::Spec->updir; | |
999 | foreach my $dir (File::Spec->catdir(($updir) x 1), File::Spec->catdir(($updir) x 2), | |
1000 | File::Spec->catdir(($updir) x 3), File::Spec->catdir(($updir) x 4)) { | |
1001 | ||
1002 | unshift @tm, File::Spec->catfile($dir, 'typemap'); | |
1003 | unshift @tm, File::Spec->catfile($dir, lib => ExtUtils => 'typemap'); | |
1004 | } | |
1005 | foreach my $dir (@INC) { | |
1006 | my $file = File::Spec->catfile($dir, ExtUtils => 'typemap'); | |
1007 | unshift @tm, $file if -e $file; | |
1008 | } | |
1009 | return @tm; | |
1010 | } | |
1011 | ||
1012 | sub TrimWhitespace | |
1013 | { | |
1014 | $_[0] =~ s/^\s+|\s+$//go ; | |
1015 | } | |
1016 | ||
1017 | sub TidyType | |
1018 | { | |
1019 | local ($_) = @_ ; | |
1020 | ||
1021 | # rationalise any '*' by joining them into bunches and removing whitespace | |
1022 | s#\s*(\*+)\s*#$1#g; | |
1023 | s#(\*+)# $1 #g ; | |
1024 | ||
1025 | # change multiple whitespace into a single space | |
1026 | s/\s+/ /g ; | |
1027 | ||
1028 | # trim leading & trailing whitespace | |
1029 | TrimWhitespace($_) ; | |
1030 | ||
1031 | $_ ; | |
1032 | } | |
1033 | ||
1034 | # Input: ($_, @line) == unparsed input. | |
1035 | # Output: ($_, @line) == (rest of line, following lines). | |
1036 | # Return: the matched keyword if found, otherwise 0 | |
1037 | sub check_keyword { | |
1038 | $_ = shift(@line) while !/\S/ && @line; | |
1039 | s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2; | |
1040 | } | |
1041 | ||
1042 | sub print_section { | |
1043 | # the "do" is required for right semantics | |
1044 | do { $_ = shift(@line) } while !/\S/ && @line; | |
1045 | ||
1046 | print("#line ", $line_no[@line_no - @line -1], " \"$filepathname\"\n") | |
1047 | if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/; | |
1048 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1049 | print "$_\n"; | |
1050 | } | |
1051 | print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n" if $WantLineNumbers; | |
1052 | } | |
1053 | ||
1054 | sub merge_section { | |
1055 | my $in = ''; | |
1056 | ||
1057 | while (!/\S/ && @line) { | |
1058 | $_ = shift(@line); | |
1059 | } | |
1060 | ||
1061 | for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1062 | $in .= "$_\n"; | |
1063 | } | |
1064 | chomp $in; | |
1065 | return $in; | |
1066 | } | |
1067 | ||
1068 | sub process_keyword($) | |
1069 | { | |
1070 | my($pattern) = @_ ; | |
1071 | my $kwd ; | |
1072 | ||
1073 | &{"${kwd}_handler"}() | |
1074 | while $kwd = check_keyword($pattern) ; | |
1075 | } | |
1076 | ||
1077 | sub CASE_handler { | |
1078 | blurt ("Error: `CASE:' after unconditional `CASE:'") | |
1079 | if $condnum && $cond eq ''; | |
1080 | $cond = $_; | |
1081 | TrimWhitespace($cond); | |
1082 | print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n"); | |
1083 | $_ = '' ; | |
1084 | } | |
1085 | ||
1086 | sub INPUT_handler { | |
1087 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1088 | last if /^\s*NOT_IMPLEMENTED_YET/; | |
1089 | next unless /\S/; # skip blank lines | |
1090 | ||
1091 | TrimWhitespace($_) ; | |
1092 | my $line = $_ ; | |
1093 | ||
1094 | # remove trailing semicolon if no initialisation | |
1095 | s/\s*;$//g unless /[=;+].*\S/ ; | |
1096 | ||
1097 | # Process the length(foo) declarations | |
1098 | if (s/^([^=]*)\blength\(\s*(\w+)\s*\)\s*$/$1 XSauto_length_of_$2=NO_INIT/x) { | |
1099 | print "\tSTRLEN\tSTRLEN_length_of_$2;\n"; | |
1100 | $lengthof{$2} = $name; | |
1101 | # $islengthof{$name} = $1; | |
1102 | $deferred .= "\n\tXSauto_length_of_$2 = STRLEN_length_of_$2;"; | |
1103 | } | |
1104 | ||
1105 | # check for optional initialisation code | |
1106 | my $var_init = '' ; | |
1107 | $var_init = $1 if s/\s*([=;+].*)$//s ; | |
1108 | $var_init =~ s/"/\\"/g; | |
1109 | ||
1110 | s/\s+/ /g; | |
1111 | my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s | |
1112 | or blurt("Error: invalid argument declaration '$line'"), next; | |
1113 | ||
1114 | # Check for duplicate definitions | |
1115 | blurt ("Error: duplicate definition of argument '$var_name' ignored"), next | |
1116 | if $arg_list{$var_name}++ | |
1117 | or defined $argtype_seen{$var_name} and not $processing_arg_with_types; | |
1118 | ||
1119 | $thisdone |= $var_name eq "THIS"; | |
1120 | $retvaldone |= $var_name eq "RETVAL"; | |
1121 | $var_types{$var_name} = $var_type; | |
1122 | # XXXX This check is a safeguard against the unfinished conversion of | |
1123 | # generate_init(). When generate_init() is fixed, | |
1124 | # one can use 2-args map_type() unconditionally. | |
1125 | if ($var_type =~ / \( \s* \* \s* \) /x) { | |
1126 | # Function pointers are not yet supported with &output_init! | |
1127 | print "\t" . &map_type($var_type, $var_name); | |
1128 | $name_printed = 1; | |
1129 | } else { | |
1130 | print "\t" . &map_type($var_type); | |
1131 | $name_printed = 0; | |
1132 | } | |
1133 | $var_num = $args_match{$var_name}; | |
1134 | ||
1135 | $proto_arg[$var_num] = ProtoString($var_type) | |
1136 | if $var_num ; | |
1137 | $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr; | |
1138 | if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/ | |
1139 | or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/ | |
1140 | and $var_init !~ /\S/) { | |
1141 | if ($name_printed) { | |
1142 | print ";\n"; | |
1143 | } else { | |
1144 | print "\t$var_name;\n"; | |
1145 | } | |
1146 | } elsif ($var_init =~ /\S/) { | |
1147 | &output_init($var_type, $var_num, $var_name, $var_init, $name_printed); | |
1148 | } elsif ($var_num) { | |
1149 | # generate initialization code | |
1150 | &generate_init($var_type, $var_num, $var_name, $name_printed); | |
1151 | } else { | |
1152 | print ";\n"; | |
1153 | } | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | sub OUTPUT_handler { | |
1158 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1159 | next unless /\S/; | |
1160 | if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) { | |
1161 | $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0); | |
1162 | next; | |
1163 | } | |
1164 | my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ; | |
1165 | blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next | |
1166 | if $outargs{$outarg} ++ ; | |
1167 | if (!$gotRETVAL and $outarg eq 'RETVAL') { | |
1168 | # deal with RETVAL last | |
1169 | $RETVAL_code = $outcode ; | |
1170 | $gotRETVAL = 1 ; | |
1171 | next ; | |
1172 | } | |
1173 | blurt ("Error: OUTPUT $outarg not an argument"), next | |
1174 | unless defined($args_match{$outarg}); | |
1175 | blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next | |
1176 | unless defined $var_types{$outarg} ; | |
1177 | $var_num = $args_match{$outarg}; | |
1178 | if ($outcode) { | |
1179 | print "\t$outcode\n"; | |
1180 | print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic; | |
1181 | } else { | |
1182 | &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic); | |
1183 | } | |
1184 | delete $in_out{$outarg} # No need to auto-OUTPUT | |
1185 | if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/; | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | sub C_ARGS_handler() { | |
1190 | my $in = merge_section(); | |
1191 | ||
1192 | TrimWhitespace($in); | |
1193 | $func_args = $in; | |
1194 | } | |
1195 | ||
1196 | sub INTERFACE_MACRO_handler() { | |
1197 | my $in = merge_section(); | |
1198 | ||
1199 | TrimWhitespace($in); | |
1200 | if ($in =~ /\s/) { # two | |
1201 | ($interface_macro, $interface_macro_set) = split ' ', $in; | |
1202 | } else { | |
1203 | $interface_macro = $in; | |
1204 | $interface_macro_set = 'UNKNOWN_CVT'; # catch later | |
1205 | } | |
1206 | $interface = 1; # local | |
1207 | $Interfaces = 1; # global | |
1208 | } | |
1209 | ||
1210 | sub INTERFACE_handler() { | |
1211 | my $in = merge_section(); | |
1212 | ||
1213 | TrimWhitespace($in); | |
1214 | ||
1215 | foreach (split /[\s,]+/, $in) { | |
1216 | $Interfaces{$_} = $_; | |
1217 | } | |
1218 | print Q(<<"EOF"); | |
1219 | # XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr); | |
1220 | EOF | |
1221 | $interface = 1; # local | |
1222 | $Interfaces = 1; # global | |
1223 | } | |
1224 | ||
1225 | sub CLEANUP_handler() { print_section() } | |
1226 | sub PREINIT_handler() { print_section() } | |
1227 | sub POSTCALL_handler() { print_section() } | |
1228 | sub INIT_handler() { print_section() } | |
1229 | ||
1230 | sub GetAliases | |
1231 | { | |
1232 | my ($line) = @_ ; | |
1233 | my ($orig) = $line ; | |
1234 | my ($alias) ; | |
1235 | my ($value) ; | |
1236 | ||
1237 | # Parse alias definitions | |
1238 | # format is | |
1239 | # alias = value alias = value ... | |
1240 | ||
1241 | while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) { | |
1242 | $alias = $1 ; | |
1243 | $orig_alias = $alias ; | |
1244 | $value = $2 ; | |
1245 | ||
1246 | # check for optional package definition in the alias | |
1247 | $alias = $Packprefix . $alias if $alias !~ /::/ ; | |
1248 | ||
1249 | # check for duplicate alias name & duplicate value | |
1250 | Warn("Warning: Ignoring duplicate alias '$orig_alias'") | |
1251 | if defined $XsubAliases{$alias} ; | |
1252 | ||
1253 | Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values") | |
1254 | if $XsubAliasValues{$value} ; | |
1255 | ||
1256 | $XsubAliases = 1; | |
1257 | $XsubAliases{$alias} = $value ; | |
1258 | $XsubAliasValues{$value} = $orig_alias ; | |
1259 | } | |
1260 | ||
1261 | blurt("Error: Cannot parse ALIAS definitions from '$orig'") | |
1262 | if $line ; | |
1263 | } | |
1264 | ||
1265 | sub ATTRS_handler () | |
1266 | { | |
1267 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1268 | next unless /\S/; | |
1269 | TrimWhitespace($_) ; | |
1270 | push @Attributes, $_; | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | sub ALIAS_handler () | |
1275 | { | |
1276 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1277 | next unless /\S/; | |
1278 | TrimWhitespace($_) ; | |
1279 | GetAliases($_) if $_ ; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | sub OVERLOAD_handler() | |
1284 | { | |
1285 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1286 | next unless /\S/; | |
1287 | TrimWhitespace($_) ; | |
1288 | while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) { | |
1289 | $Overload = 1 unless $Overload; | |
1290 | my $overload = "$Package\::(".$1 ; | |
1291 | push(@InitFileCode, | |
1292 | " newXS(\"$overload\", XS_$Full_func_name, file$proto);\n"); | |
1293 | } | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | sub FALLBACK_handler() | |
1298 | { | |
1299 | # the rest of the current line should contain either TRUE, | |
1300 | # FALSE or UNDEF | |
1301 | ||
1302 | TrimWhitespace($_) ; | |
1303 | my %map = ( | |
1304 | TRUE => "PL_sv_yes", 1 => "PL_sv_yes", | |
1305 | FALSE => "PL_sv_no", 0 => "PL_sv_no", | |
1306 | UNDEF => "PL_sv_undef", | |
1307 | ) ; | |
1308 | ||
1309 | # check for valid FALLBACK value | |
1310 | death ("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{uc $_} ; | |
1311 | ||
1312 | $Fallback = $map{uc $_} ; | |
1313 | } | |
1314 | ||
1315 | ||
1316 | sub REQUIRE_handler () | |
1317 | { | |
1318 | # the rest of the current line should contain a version number | |
1319 | my ($Ver) = $_ ; | |
1320 | ||
1321 | TrimWhitespace($Ver) ; | |
1322 | ||
1323 | death ("Error: REQUIRE expects a version number") | |
1324 | unless $Ver ; | |
1325 | ||
1326 | # check that the version number is of the form n.n | |
1327 | death ("Error: REQUIRE: expected a number, got '$Ver'") | |
1328 | unless $Ver =~ /^\d+(\.\d*)?/ ; | |
1329 | ||
1330 | death ("Error: xsubpp $Ver (or better) required--this is only $VERSION.") | |
1331 | unless $VERSION >= $Ver ; | |
1332 | } | |
1333 | ||
1334 | sub VERSIONCHECK_handler () | |
1335 | { | |
1336 | # the rest of the current line should contain either ENABLE or | |
1337 | # DISABLE | |
1338 | ||
1339 | TrimWhitespace($_) ; | |
1340 | ||
1341 | # check for ENABLE/DISABLE | |
1342 | death ("Error: VERSIONCHECK: ENABLE/DISABLE") | |
1343 | unless /^(ENABLE|DISABLE)/i ; | |
1344 | ||
1345 | $WantVersionChk = 1 if $1 eq 'ENABLE' ; | |
1346 | $WantVersionChk = 0 if $1 eq 'DISABLE' ; | |
1347 | ||
1348 | } | |
1349 | ||
1350 | sub PROTOTYPE_handler () | |
1351 | { | |
1352 | my $specified ; | |
1353 | ||
1354 | death("Error: Only 1 PROTOTYPE definition allowed per xsub") | |
1355 | if $proto_in_this_xsub ++ ; | |
1356 | ||
1357 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1358 | next unless /\S/; | |
1359 | $specified = 1 ; | |
1360 | TrimWhitespace($_) ; | |
1361 | if ($_ eq 'DISABLE') { | |
1362 | $ProtoThisXSUB = 0 | |
1363 | } elsif ($_ eq 'ENABLE') { | |
1364 | $ProtoThisXSUB = 1 | |
1365 | } else { | |
1366 | # remove any whitespace | |
1367 | s/\s+//g ; | |
1368 | death("Error: Invalid prototype '$_'") | |
1369 | unless ValidProtoString($_) ; | |
1370 | $ProtoThisXSUB = C_string($_) ; | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | # If no prototype specified, then assume empty prototype "" | |
1375 | $ProtoThisXSUB = 2 unless $specified ; | |
1376 | ||
1377 | $ProtoUsed = 1 ; | |
1378 | ||
1379 | } | |
1380 | ||
1381 | sub SCOPE_handler () | |
1382 | { | |
1383 | death("Error: Only 1 SCOPE declaration allowed per xsub") | |
1384 | if $scope_in_this_xsub ++ ; | |
1385 | ||
1386 | for (; !/^$BLOCK_re/o; $_ = shift(@line)) { | |
1387 | next unless /\S/; | |
1388 | TrimWhitespace($_) ; | |
1389 | if ($_ =~ /^DISABLE/i) { | |
1390 | $ScopeThisXSUB = 0 | |
1391 | } elsif ($_ =~ /^ENABLE/i) { | |
1392 | $ScopeThisXSUB = 1 | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | } | |
1397 | ||
1398 | sub PROTOTYPES_handler () | |
1399 | { | |
1400 | # the rest of the current line should contain either ENABLE or | |
1401 | # DISABLE | |
1402 | ||
1403 | TrimWhitespace($_) ; | |
1404 | ||
1405 | # check for ENABLE/DISABLE | |
1406 | death ("Error: PROTOTYPES: ENABLE/DISABLE") | |
1407 | unless /^(ENABLE|DISABLE)/i ; | |
1408 | ||
1409 | $WantPrototypes = 1 if $1 eq 'ENABLE' ; | |
1410 | $WantPrototypes = 0 if $1 eq 'DISABLE' ; | |
1411 | $ProtoUsed = 1 ; | |
1412 | ||
1413 | } | |
1414 | ||
1415 | sub INCLUDE_handler () | |
1416 | { | |
1417 | # the rest of the current line should contain a valid filename | |
1418 | ||
1419 | TrimWhitespace($_) ; | |
1420 | ||
1421 | death("INCLUDE: filename missing") | |
1422 | unless $_ ; | |
1423 | ||
1424 | death("INCLUDE: output pipe is illegal") | |
1425 | if /^\s*\|/ ; | |
1426 | ||
1427 | # simple minded recursion detector | |
1428 | death("INCLUDE loop detected") | |
1429 | if $IncludedFiles{$_} ; | |
1430 | ||
1431 | ++ $IncludedFiles{$_} unless /\|\s*$/ ; | |
1432 | ||
1433 | # Save the current file context. | |
1434 | push(@XSStack, { | |
1435 | type => 'file', | |
1436 | LastLine => $lastline, | |
1437 | LastLineNo => $lastline_no, | |
1438 | Line => \@line, | |
1439 | LineNo => \@line_no, | |
1440 | Filename => $filename, | |
1441 | Handle => $FH, | |
1442 | }) ; | |
1443 | ||
907ce46c | 1444 | $FH = Symbol::gensym(); |
6b09c160 YST |
1445 | |
1446 | # open the new file | |
1447 | open ($FH, "$_") or death("Cannot open '$_': $!") ; | |
1448 | ||
1449 | print Q(<<"EOF"); | |
1450 | # | |
1451 | #/* INCLUDE: Including '$_' from '$filename' */ | |
1452 | # | |
1453 | EOF | |
1454 | ||
1455 | $filename = $_ ; | |
1456 | ||
1457 | # Prime the pump by reading the first | |
1458 | # non-blank line | |
1459 | ||
1460 | # skip leading blank lines | |
1461 | while (<$FH>) { | |
1462 | last unless /^\s*$/ ; | |
1463 | } | |
1464 | ||
1465 | $lastline = $_ ; | |
1466 | $lastline_no = $. ; | |
1467 | ||
1468 | } | |
1469 | ||
1470 | sub PopFile() | |
1471 | { | |
1472 | return 0 unless $XSStack[-1]{type} eq 'file' ; | |
1473 | ||
1474 | my $data = pop @XSStack ; | |
1475 | my $ThisFile = $filename ; | |
1476 | my $isPipe = ($filename =~ /\|\s*$/) ; | |
1477 | ||
1478 | -- $IncludedFiles{$filename} | |
1479 | unless $isPipe ; | |
1480 | ||
1481 | close $FH ; | |
1482 | ||
1483 | $FH = $data->{Handle} ; | |
1484 | $filename = $data->{Filename} ; | |
1485 | $lastline = $data->{LastLine} ; | |
1486 | $lastline_no = $data->{LastLineNo} ; | |
1487 | @line = @{ $data->{Line} } ; | |
1488 | @line_no = @{ $data->{LineNo} } ; | |
1489 | ||
1490 | if ($isPipe and $? ) { | |
1491 | -- $lastline_no ; | |
1492 | print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ; | |
1493 | exit 1 ; | |
1494 | } | |
1495 | ||
1496 | print Q(<<"EOF"); | |
1497 | # | |
1498 | #/* INCLUDE: Returning to '$filename' from '$ThisFile' */ | |
1499 | # | |
1500 | EOF | |
1501 | ||
1502 | return 1 ; | |
1503 | } | |
1504 | ||
1505 | sub ValidProtoString ($) | |
1506 | { | |
1507 | my($string) = @_ ; | |
1508 | ||
1509 | if ( $string =~ /^$proto_re+$/ ) { | |
1510 | return $string ; | |
1511 | } | |
1512 | ||
1513 | return 0 ; | |
1514 | } | |
1515 | ||
1516 | sub C_string ($) | |
1517 | { | |
1518 | my($string) = @_ ; | |
1519 | ||
1520 | $string =~ s[\\][\\\\]g ; | |
1521 | $string ; | |
1522 | } | |
1523 | ||
1524 | sub ProtoString ($) | |
1525 | { | |
1526 | my ($type) = @_ ; | |
1527 | ||
1528 | $proto_letter{$type} or "\$" ; | |
1529 | } | |
1530 | ||
1531 | sub check_cpp { | |
1532 | my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line); | |
1533 | if (@cpp) { | |
1534 | my ($cpp, $cpplevel); | |
1535 | for $cpp (@cpp) { | |
1536 | if ($cpp =~ /^\#\s*if/) { | |
1537 | $cpplevel++; | |
1538 | } elsif (!$cpplevel) { | |
1539 | Warn("Warning: #else/elif/endif without #if in this function"); | |
1540 | print STDERR " (precede it with a blank line if the matching #if is outside the function)\n" | |
1541 | if $XSStack[-1]{type} eq 'if'; | |
1542 | return; | |
1543 | } elsif ($cpp =~ /^\#\s*endif/) { | |
1544 | $cpplevel--; | |
1545 | } | |
1546 | } | |
1547 | Warn("Warning: #if without #endif in this function") if $cpplevel; | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | ||
1552 | sub Q { | |
1553 | my($text) = @_; | |
1554 | $text =~ s/^#//gm; | |
1555 | $text =~ s/\[\[/{/g; | |
1556 | $text =~ s/\]\]/}/g; | |
1557 | $text; | |
1558 | } | |
1559 | ||
1560 | # Read next xsub into @line from ($lastline, <$FH>). | |
1561 | sub fetch_para { | |
1562 | # parse paragraph | |
1563 | death ("Error: Unterminated `#if/#ifdef/#ifndef'") | |
1564 | if !defined $lastline && $XSStack[-1]{type} eq 'if'; | |
1565 | @line = (); | |
1566 | @line_no = () ; | |
1567 | return PopFile() if !defined $lastline; | |
1568 | ||
1569 | if ($lastline =~ | |
1570 | /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) { | |
1571 | $Module = $1; | |
1572 | $Package = defined($2) ? $2 : ''; # keep -w happy | |
1573 | $Prefix = defined($3) ? $3 : ''; # keep -w happy | |
1574 | $Prefix = quotemeta $Prefix ; | |
1575 | ($Module_cname = $Module) =~ s/\W/_/g; | |
1576 | ($Packid = $Package) =~ tr/:/_/; | |
1577 | $Packprefix = $Package; | |
1578 | $Packprefix .= "::" if $Packprefix ne ""; | |
1579 | $lastline = ""; | |
1580 | } | |
1581 | ||
1582 | for (;;) { | |
1583 | # Skip embedded PODs | |
1584 | while ($lastline =~ /^=/) { | |
1585 | while ($lastline = <$FH>) { | |
1586 | last if ($lastline =~ /^=cut\s*$/); | |
1587 | } | |
1588 | death ("Error: Unterminated pod") unless $lastline; | |
1589 | $lastline = <$FH>; | |
1590 | chomp $lastline; | |
1591 | $lastline =~ s/^\s+$//; | |
1592 | } | |
1593 | if ($lastline !~ /^\s*#/ || | |
1594 | # CPP directives: | |
1595 | # ANSI: if ifdef ifndef elif else endif define undef | |
1596 | # line error pragma | |
1597 | # gcc: warning include_next | |
1598 | # obj-c: import | |
1599 | # others: ident (gcc notes that some cpps have this one) | |
1600 | $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) { | |
1601 | last if $lastline =~ /^\S/ && @line && $line[-1] eq ""; | |
1602 | push(@line, $lastline); | |
1603 | push(@line_no, $lastline_no) ; | |
1604 | } | |
1605 | ||
1606 | # Read next line and continuation lines | |
1607 | last unless defined($lastline = <$FH>); | |
1608 | $lastline_no = $.; | |
1609 | my $tmp_line; | |
1610 | $lastline .= $tmp_line | |
1611 | while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>)); | |
1612 | ||
1613 | chomp $lastline; | |
1614 | $lastline =~ s/^\s+$//; | |
1615 | } | |
1616 | pop(@line), pop(@line_no) while @line && $line[-1] eq ""; | |
1617 | 1; | |
1618 | } | |
1619 | ||
1620 | sub output_init { | |
1621 | local($type, $num, $var, $init, $name_printed) = @_; | |
1622 | local($arg) = "ST(" . ($num - 1) . ")"; | |
1623 | ||
1624 | if ( $init =~ /^=/ ) { | |
1625 | if ($name_printed) { | |
1626 | eval qq/print " $init\\n"/; | |
1627 | } else { | |
1628 | eval qq/print "\\t$var $init\\n"/; | |
1629 | } | |
1630 | warn $@ if $@; | |
1631 | } else { | |
1632 | if ( $init =~ s/^\+// && $num ) { | |
1633 | &generate_init($type, $num, $var, $name_printed); | |
1634 | } elsif ($name_printed) { | |
1635 | print ";\n"; | |
1636 | $init =~ s/^;//; | |
1637 | } else { | |
1638 | eval qq/print "\\t$var;\\n"/; | |
1639 | warn $@ if $@; | |
1640 | $init =~ s/^;//; | |
1641 | } | |
1642 | $deferred .= eval qq/"\\n\\t$init\\n"/; | |
1643 | warn $@ if $@; | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | sub Warn | |
1648 | { | |
1649 | # work out the line number | |
1650 | my $line_no = $line_no[@line_no - @line -1] ; | |
1651 | ||
1652 | print STDERR "@_ in $filename, line $line_no\n" ; | |
1653 | } | |
1654 | ||
1655 | sub blurt | |
1656 | { | |
1657 | Warn @_ ; | |
1658 | $errors ++ | |
1659 | } | |
1660 | ||
1661 | sub death | |
1662 | { | |
1663 | Warn @_ ; | |
1664 | exit 1 ; | |
1665 | } | |
1666 | ||
1667 | sub generate_init { | |
1668 | local($type, $num, $var) = @_; | |
1669 | local($arg) = "ST(" . ($num - 1) . ")"; | |
1670 | local($argoff) = $num - 1; | |
1671 | local($ntype); | |
1672 | local($tk); | |
1673 | ||
1674 | $type = TidyType($type) ; | |
1675 | blurt("Error: '$type' not in typemap"), return | |
1676 | unless defined($type_kind{$type}); | |
1677 | ||
1678 | ($ntype = $type) =~ s/\s*\*/Ptr/g; | |
1679 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; | |
1680 | $tk = $type_kind{$type}; | |
1681 | $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/; | |
1682 | if ($tk eq 'T_PV' and exists $lengthof{$var}) { | |
1683 | print "\t$var" unless $name_printed; | |
1684 | print " = ($type)SvPV($arg, STRLEN_length_of_$var);\n"; | |
1685 | die "default value not supported with length(NAME) supplied" | |
1686 | if defined $defaults{$var}; | |
1687 | return; | |
1688 | } | |
1689 | $type =~ tr/:/_/ unless $hiertype; | |
1690 | blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return | |
1691 | unless defined $input_expr{$tk} ; | |
1692 | $expr = $input_expr{$tk}; | |
1693 | if ($expr =~ /DO_ARRAY_ELEM/) { | |
1694 | blurt("Error: '$subtype' not in typemap"), return | |
1695 | unless defined($type_kind{$subtype}); | |
1696 | blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return | |
1697 | unless defined $input_expr{$type_kind{$subtype}} ; | |
1698 | $subexpr = $input_expr{$type_kind{$subtype}}; | |
1699 | $subexpr =~ s/\$type/\$subtype/g; | |
1700 | $subexpr =~ s/ntype/subtype/g; | |
1701 | $subexpr =~ s/\$arg/ST(ix_$var)/g; | |
1702 | $subexpr =~ s/\n\t/\n\t\t/g; | |
1703 | $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g; | |
1704 | $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/; | |
1705 | $expr =~ s/DO_ARRAY_ELEM/$subexpr/; | |
1706 | } | |
1707 | if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments | |
1708 | $ScopeThisXSUB = 1; | |
1709 | } | |
1710 | if (defined($defaults{$var})) { | |
1711 | $expr =~ s/(\t+)/$1 /g; | |
1712 | $expr =~ s/ /\t/g; | |
1713 | if ($name_printed) { | |
1714 | print ";\n"; | |
1715 | } else { | |
1716 | eval qq/print "\\t$var;\\n"/; | |
1717 | warn $@ if $@; | |
1718 | } | |
1719 | if ($defaults{$var} eq 'NO_INIT') { | |
1720 | $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/; | |
1721 | } else { | |
1722 | $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/; | |
1723 | } | |
1724 | warn $@ if $@; | |
1725 | } elsif ($ScopeThisXSUB or $expr !~ /^\s*\$var =/) { | |
1726 | if ($name_printed) { | |
1727 | print ";\n"; | |
1728 | } else { | |
1729 | eval qq/print "\\t$var;\\n"/; | |
1730 | warn $@ if $@; | |
1731 | } | |
1732 | $deferred .= eval qq/"\\n$expr;\\n"/; | |
1733 | warn $@ if $@; | |
1734 | } else { | |
1735 | die "panic: do not know how to handle this branch for function pointers" | |
1736 | if $name_printed; | |
1737 | eval qq/print "$expr;\\n"/; | |
1738 | warn $@ if $@; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | sub generate_output { | |
1743 | local($type, $num, $var, $do_setmagic, $do_push) = @_; | |
1744 | local($arg) = "ST(" . ($num - ($num != 0)) . ")"; | |
1745 | local($argoff) = $num - 1; | |
1746 | local($ntype); | |
1747 | ||
1748 | $type = TidyType($type) ; | |
1749 | if ($type =~ /^array\(([^,]*),(.*)\)/) { | |
1750 | print "\t$arg = sv_newmortal();\n"; | |
1751 | print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n"; | |
1752 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; | |
1753 | } else { | |
1754 | blurt("Error: '$type' not in typemap"), return | |
1755 | unless defined($type_kind{$type}); | |
1756 | blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return | |
1757 | unless defined $output_expr{$type_kind{$type}} ; | |
1758 | ($ntype = $type) =~ s/\s*\*/Ptr/g; | |
1759 | $ntype =~ s/\(\)//g; | |
1760 | ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//; | |
1761 | $expr = $output_expr{$type_kind{$type}}; | |
1762 | if ($expr =~ /DO_ARRAY_ELEM/) { | |
1763 | blurt("Error: '$subtype' not in typemap"), return | |
1764 | unless defined($type_kind{$subtype}); | |
1765 | blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return | |
1766 | unless defined $output_expr{$type_kind{$subtype}} ; | |
1767 | $subexpr = $output_expr{$type_kind{$subtype}}; | |
1768 | $subexpr =~ s/ntype/subtype/g; | |
1769 | $subexpr =~ s/\$arg/ST(ix_$var)/g; | |
1770 | $subexpr =~ s/\$var/${var}[ix_$var]/g; | |
1771 | $subexpr =~ s/\n\t/\n\t\t/g; | |
1772 | $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/; | |
1773 | eval "print qq\a$expr\a"; | |
1774 | warn $@ if $@; | |
1775 | print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic; | |
1776 | } elsif ($var eq 'RETVAL') { | |
1777 | if ($expr =~ /^\t\$arg = new/) { | |
1778 | # We expect that $arg has refcnt 1, so we need to | |
1779 | # mortalize it. | |
1780 | eval "print qq\a$expr\a"; | |
1781 | warn $@ if $@; | |
1782 | print "\tsv_2mortal(ST($num));\n"; | |
1783 | print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic; | |
1784 | } elsif ($expr =~ /^\s*\$arg\s*=/) { | |
1785 | # We expect that $arg has refcnt >=1, so we need | |
1786 | # to mortalize it! | |
1787 | eval "print qq\a$expr\a"; | |
1788 | warn $@ if $@; | |
1789 | print "\tsv_2mortal(ST(0));\n"; | |
1790 | print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; | |
1791 | } else { | |
1792 | # Just hope that the entry would safely write it | |
1793 | # over an already mortalized value. By | |
1794 | # coincidence, something like $arg = &sv_undef | |
1795 | # works too. | |
1796 | print "\tST(0) = sv_newmortal();\n"; | |
1797 | eval "print qq\a$expr\a"; | |
1798 | warn $@ if $@; | |
1799 | # new mortals don't have set magic | |
1800 | } | |
1801 | } elsif ($do_push) { | |
1802 | print "\tPUSHs(sv_newmortal());\n"; | |
1803 | $arg = "ST($num)"; | |
1804 | eval "print qq\a$expr\a"; | |
1805 | warn $@ if $@; | |
1806 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; | |
1807 | } elsif ($arg =~ /^ST\(\d+\)$/) { | |
1808 | eval "print qq\a$expr\a"; | |
1809 | warn $@ if $@; | |
1810 | print "\tSvSETMAGIC($arg);\n" if $do_setmagic; | |
1811 | } | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | sub map_type { | |
1816 | my($type, $varname) = @_; | |
1817 | ||
1818 | # C++ has :: in types too so skip this | |
1819 | $type =~ tr/:/_/ unless $hiertype; | |
1820 | $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s; | |
1821 | if ($varname) { | |
1822 | if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) { | |
1823 | (substr $type, pos $type, 0) = " $varname "; | |
1824 | } else { | |
1825 | $type .= "\t$varname"; | |
1826 | } | |
1827 | } | |
1828 | $type; | |
1829 | } | |
1830 | ||
1831 | ||
1832 | ######################################################### | |
1833 | package | |
1834 | ExtUtils::ParseXS::CountLines; | |
1835 | use strict; | |
1836 | use vars qw($SECTION_END_MARKER); | |
1837 | ||
1838 | sub TIEHANDLE { | |
1839 | my ($class, $cfile, $fh) = @_; | |
1840 | $cfile =~ s/\\/\\\\/g; | |
1841 | $SECTION_END_MARKER = qq{#line --- "$cfile"}; | |
1842 | ||
1843 | return bless {buffer => '', | |
1844 | fh => $fh, | |
1845 | line_no => 1, | |
1846 | }, $class; | |
1847 | } | |
1848 | ||
1849 | sub PRINT { | |
1850 | my $self = shift; | |
1851 | for (@_) { | |
1852 | $self->{buffer} .= $_; | |
1853 | while ($self->{buffer} =~ s/^([^\n]*\n)//) { | |
1854 | my $line = $1; | |
1855 | ++ $self->{line_no}; | |
1856 | $line =~ s|^\#line\s+---(?=\s)|#line $self->{line_no}|; | |
1857 | print {$self->{fh}} $line; | |
1858 | } | |
1859 | } | |
1860 | } | |
1861 | ||
1862 | sub PRINTF { | |
1863 | my $self = shift; | |
1864 | my $fmt = shift; | |
1865 | $self->PRINT(sprintf($fmt, @_)); | |
1866 | } | |
1867 | ||
1868 | sub DESTROY { | |
1869 | # Not necessary if we're careful to end with a "\n" | |
1870 | my $self = shift; | |
1871 | print {$self->{fh}} $self->{buffer}; | |
1872 | } | |
1873 | ||
1874 | sub UNTIE { | |
2f47ab8e | 1875 | # This sub does nothing, but is neccessary for references to be released. |
6b09c160 YST |
1876 | } |
1877 | ||
1878 | sub end_marker { | |
1879 | return $SECTION_END_MARKER; | |
1880 | } | |
1881 | ||
1882 | ||
1883 | 1; | |
1884 | __END__ | |
1885 | ||
1886 | =head1 NAME | |
1887 | ||
1888 | ExtUtils::ParseXS - converts Perl XS code into C code | |
1889 | ||
1890 | =head1 SYNOPSIS | |
1891 | ||
1892 | use ExtUtils::ParseXS qw(process_file); | |
1893 | ||
1894 | process_file( filename => 'foo.xs' ); | |
1895 | ||
1896 | process_file( filename => 'foo.xs', | |
1897 | output => 'bar.c', | |
1898 | 'C++' => 1, | |
1899 | typemap => 'path/to/typemap', | |
1900 | hiertype => 1, | |
1901 | except => 1, | |
1902 | prototypes => 1, | |
1903 | versioncheck => 1, | |
1904 | linenumbers => 1, | |
1905 | optimize => 1, | |
1906 | prototypes => 1, | |
1907 | ); | |
1908 | =head1 DESCRIPTION | |
1909 | ||
1910 | C<ExtUtils::ParseXS> will compile XS code into C code by embedding the constructs | |
1911 | necessary to let C functions manipulate Perl values and creates the glue | |
1912 | necessary to let Perl access those functions. The compiler uses typemaps to | |
1913 | determine how to map C function parameters and variables to Perl values. | |
1914 | ||
1915 | The compiler will search for typemap files called I<typemap>. It will use | |
1916 | the following search path to find default typemaps, with the rightmost | |
1917 | typemap taking precedence. | |
1918 | ||
1919 | ../../../typemap:../../typemap:../typemap:typemap | |
1920 | ||
1921 | =head1 EXPORT | |
1922 | ||
1923 | None by default. C<process_file()> may be exported upon request. | |
1924 | ||
1925 | ||
1926 | =head1 FUNCTIONS | |
1927 | ||
1928 | =over 4 | |
1929 | ||
1930 | =item process_xs() | |
1931 | ||
1932 | This function processes an XS file and sends output to a C file. | |
1933 | Named parameters control how the processing is done. The following | |
1934 | parameters are accepted: | |
1935 | ||
1936 | =over 4 | |
1937 | ||
1938 | =item B<C++> | |
1939 | ||
1940 | Adds C<extern "C"> to the C code. Default is false. | |
1941 | ||
1942 | =item B<hiertype> | |
1943 | ||
2f47ab8e | 1944 | Retains C<::> in type names so that C++ hierachical types can be |
6b09c160 YST |
1945 | mapped. Default is false. |
1946 | ||
1947 | =item B<except> | |
1948 | ||
1949 | Adds exception handling stubs to the C code. Default is false. | |
1950 | ||
1951 | =item B<typemap> | |
1952 | ||
1953 | Indicates that a user-supplied typemap should take precedence over the | |
1954 | default typemaps. A single typemap may be specified as a string, or | |
1955 | multiple typemaps can be specified in an array reference, with the | |
1956 | last typemap having the highest precedence. | |
1957 | ||
1958 | =item B<prototypes> | |
1959 | ||
1960 | Generates prototype code for all xsubs. Default is false. | |
1961 | ||
1962 | =item B<versioncheck> | |
1963 | ||
1964 | Makes sure at run time that the object file (derived from the C<.xs> | |
1965 | file) and the C<.pm> files have the same version number. Default is | |
1966 | true. | |
1967 | ||
1968 | =item B<linenumbers> | |
1969 | ||
1970 | Adds C<#line> directives to the C output so error messages will look | |
1971 | like they came from the original XS file. Default is true. | |
1972 | ||
1973 | =item B<optimize> | |
1974 | ||
1975 | Enables certain optimizations. The only optimization that is currently | |
1976 | affected is the use of I<target>s by the output C code (see L<perlguts>). | |
1977 | Not optimizing may significantly slow down the generated code, but this is the way | |
1978 | B<xsubpp> of 5.005 and earlier operated. Default is to optimize. | |
1979 | ||
1980 | =item B<inout> | |
1981 | ||
1982 | Enable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> | |
1983 | declarations. Default is true. | |
1984 | ||
1985 | =item B<argtypes> | |
1986 | ||
1987 | Enable recognition of ANSI-like descriptions of function signature. | |
1988 | Default is true. | |
1989 | ||
1990 | =item B<s> | |
1991 | ||
1992 | I have no clue what this does. Strips function prefixes? | |
1993 | ||
1994 | =back | |
1995 | ||
1996 | =item errors() | |
1997 | ||
1998 | This function returns the number of [a certain kind of] errors | |
1999 | encountered during processing of the XS file. | |
2000 | ||
2001 | =back | |
2002 | ||
2003 | =head1 AUTHOR | |
2004 | ||
2005 | Based on xsubpp code, written by Larry Wall. | |
2006 | ||
2007 | Maintained by Ken Williams, <ken@mathforum.org> | |
2008 | ||
2009 | =head1 COPYRIGHT | |
2010 | ||
2011 | Copyright 2002-2003 Ken Williams. All rights reserved. | |
2012 | ||
2013 | This library is free software; you can redistribute it and/or | |
2014 | modify it under the same terms as Perl itself. | |
2015 | ||
2016 | Based on the ExtUtils::xsubpp code by Larry Wall and the Perl 5 | |
2017 | Porters, which was released under the same license terms. | |
2018 | ||
2019 | =head1 SEE ALSO | |
2020 | ||
2021 | L<perl>, ExtUtils::xsubpp, ExtUtils::MakeMaker, L<perlxs>, L<perlxstut>. | |
2022 | ||
2023 | =cut |