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