1 package ExtUtils::ParseXS;
16 use ExtUtils::ParseXS::Constants $VERSION;
17 use ExtUtils::ParseXS::CountLines $VERSION;
18 use ExtUtils::ParseXS::Utilities $VERSION;
19 $VERSION = eval $VERSION if $VERSION =~ /_/;
21 use ExtUtils::ParseXS::Utilities qw(
22 standard_typemap_locations
32 analyze_preprocessor_statements
38 check_conditional_preprocessor_statements
39 escape_file_for_line_directive
40 report_typemap_failure
43 our @ISA = qw(Exporter);
49 # The scalars in the line below remain as 'our' variables because pulling
50 # them into $self led to build problems. In most cases, strings being
51 # 'eval'-ed contain the variables' names hard-coded.
53 $Package, $func_name, $Full_func_name, $pname, $ALIAS,
56 our $self = bless {} => __PACKAGE__;
60 # Allow for $package->process_file(%hash) in the future
61 my ($pkg, %options) = @_ % 2 ? @_ : (__PACKAGE__, @_);
63 $self->{ProtoUsed} = exists $options{prototypes};
78 FH => Symbol::gensym(),
81 $args{except} = $args{except} ? ' TRY' : '';
85 my ($Is_VMS, $SymSet);
88 # Establish set of global symbols with max length 28, since xsubpp
89 # will later add the 'XS_' prefix.
90 require ExtUtils::XSSymSet;
91 $SymSet = ExtUtils::XSSymSet->new(28);
93 @{ $self->{XSStack} } = ({type => 'none'});
94 $self->{InitFileCode} = [ @ExtUtils::ParseXS::Constants::InitFileCode ];
95 $self->{Overload} = 0;
97 $self->{Fallback} = '&PL_sv_undef';
99 # Most of the 1500 lines below uses these globals. We'll have to
100 # clean this up sometime, probably. For now, we just pull them out
103 $self->{hiertype} = $args{hiertype};
104 $self->{WantPrototypes} = $args{prototypes};
105 $self->{WantVersionChk} = $args{versioncheck};
106 $self->{WantLineNumbers} = $args{linenumbers};
107 $self->{IncludedFiles} = {};
109 die "Missing required parameter 'filename'" unless $args{filename};
110 $self->{filepathname} = $args{filename};
111 ($self->{dir}, $self->{filename}) =
112 (dirname($args{filename}), basename($args{filename}));
113 $self->{filepathname} =~ s/\\/\\\\/g;
114 $self->{IncludedFiles}->{$args{filename}}++;
116 # Open the output file if given as a string. If they provide some
117 # other kind of reference, trust them that we can print to it.
118 if (not ref $args{output}) {
119 open my($fh), "> $args{output}" or die "Can't create $args{output}: $!";
120 $args{outfile} = $args{output};
124 # Really, we shouldn't have to chdir() or select() in the first
125 # place. For now, just save and restore.
126 my $orig_cwd = cwd();
127 my $orig_fh = select();
131 my $csuffix = $args{csuffix};
133 if ($self->{WantLineNumbers}) {
135 if ( $args{outfile} ) {
136 $cfile = $args{outfile};
139 $cfile = $args{filename};
140 $cfile =~ s/\.xs$/$csuffix/i or $cfile .= $csuffix;
142 tie(*PSEUDO_STDOUT, 'ExtUtils::ParseXS::CountLines', $cfile, $args{output});
143 select PSEUDO_STDOUT;
146 select $args{output};
149 $self->{typemap} = process_typemaps( $args{typemap}, $pwd );
151 my $END = "!End!\n\n"; # "impossible" keyword (multiple newline)
153 # Match an XS keyword
154 $self->{BLOCK_re} = '\s*(' .
155 join('|' => @ExtUtils::ParseXS::Constants::XSKeywords) .
158 our ($C_group_rex, $C_arg);
159 # Group in C (no support for comments or literals)
160 $C_group_rex = qr/ [({\[]
161 (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
163 # Chunk in C without comma at toplevel (no comments):
164 $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
165 | (??{ $C_group_rex })
166 | " (?: (?> [^\\"]+ )
168 )* " # String literal
169 | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
172 # Since at this point we're ready to begin printing to the output file and
173 # reading from the input file, I want to get as much data as possible into
174 # the proto-object $self. That means assigning to $self and elements of
175 # %args referenced below this point.
176 # HOWEVER: This resulted in an error when I tried:
177 # $args{'s'} ---> $self->{s}.
178 # Use of uninitialized value in quotemeta at
179 # .../blib/lib/ExtUtils/ParseXS.pm line 733
181 foreach my $datum ( qw| argtypes except inout optimize | ) {
182 $self->{$datum} = $args{$datum};
185 # Identify the version of xsubpp used
188 * This file was generated automatically by ExtUtils::ParseXS version $VERSION from the
189 * contents of $self->{filename}. Do not edit this file, edit $self->{filename} instead.
191 * ANY CHANGES MADE HERE WILL BE LOST!
198 print("#line 1 \"" . escape_file_for_line_directive($self->{filepathname}) . "\"\n")
199 if $self->{WantLineNumbers};
201 # Open the input file (using $self->{filename} which
202 # is a basename'd $args{filename} due to chdir above)
203 open($self->{FH}, '<', $self->{filename}) or die "cannot open $self->{filename}: $!\n";
206 while (readline($self->{FH})) {
208 my $podstartline = $.;
211 # We can't just write out a /* */ comment, as our embedded
212 # POD might itself be in a comment. We can't put a /**/
213 # comment inside #if 0, as the C standard says that the source
214 # file is decomposed into preprocessing characters in the stage
215 # before preprocessing commands are executed.
216 # I don't want to leave the text as barewords, because the spec
217 # isn't clear whether macros are expanded before or after
218 # preprocessing commands are executed, and someone pathological
219 # may just have defined one of the 3 words as a macro that does
220 # something strange. Multiline strings are illegal in C, so
221 # the "" we write must be a string literal. And they aren't
222 # concatenated until 2 steps later, so we are safe.
224 print("#if 0\n \"Skipped embedded POD.\"\n#endif\n");
225 printf("#line %d \"%s\"\n", $. + 1, escape_file_for_line_directive($self->{filepathname}))
226 if $self->{WantLineNumbers};
230 } while (readline($self->{FH}));
231 # At this point $. is at end of file so die won't state the start
232 # of the problem, and as we haven't yet read any lines &death won't
233 # show the correct line in the message either.
234 die ("Error: Unterminated pod in $self->{filename}, line $podstartline\n")
235 unless $self->{lastline};
237 last if ($Package, $self->{Prefix}) =
238 /^MODULE\s*=\s*[\w:]+(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
242 unless (defined $_) {
243 warn "Didn't find a 'MODULE ... PACKAGE ... PREFIX' line\n";
244 exit 0; # Not a fatal error for the caller process
247 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n" if $self->{WantLineNumbers};
251 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n" if $self->{WantLineNumbers};
253 $self->{lastline} = $_;
254 $self->{lastline_no} = $.;
256 my $BootCode_ref = [];
257 my $XSS_work_idx = 0;
258 my $cpp_next_tmp = 'XSubPPtmpAAAA';
260 while ($self->fetch_para()) {
261 my $outlist_ref = [];
262 # Print initial preprocessor statements and blank lines
263 while (@{ $self->{line} } && $self->{line}->[0] !~ /^[^\#]/) {
264 my $ln = shift(@{ $self->{line} });
266 next unless $ln =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
268 ( $self, $XSS_work_idx, $BootCode_ref ) =
269 analyze_preprocessor_statements(
270 $self, $statement, $XSS_work_idx, $BootCode_ref
274 next PARAGRAPH unless @{ $self->{line} };
276 if ($XSS_work_idx && !$self->{XSStack}->[$XSS_work_idx]{varname}) {
277 # We are inside an #if, but have not yet #defined its xsubpp variable.
278 print "#define $cpp_next_tmp 1\n\n";
279 push(@{ $self->{InitFileCode} }, "#if $cpp_next_tmp\n");
280 push(@{ $BootCode_ref }, "#if $cpp_next_tmp");
281 $self->{XSStack}->[$XSS_work_idx]{varname} = $cpp_next_tmp++;
285 "Code is not inside a function"
286 ." (maybe last function was ended by a blank line "
287 ." followed by a statement on column one?)")
288 if $self->{line}->[0] =~ /^\s/;
290 # initialize info arrays
291 foreach my $member (qw(args_match var_types defaults arg_list
292 argtype_seen in_out lengthof))
294 $self->{$member} = {};
296 $self->{proto_arg} = [];
297 $self->{processing_arg_with_types} = undef;
298 $self->{proto_in_this_xsub} = undef;
299 $self->{scope_in_this_xsub} = undef;
300 $self->{interface} = undef;
301 $self->{interface_macro} = 'XSINTERFACE_FUNC';
302 $self->{interface_macro_set} = 'XSINTERFACE_FUNC_SET';
303 $self->{ProtoThisXSUB} = $self->{WantPrototypes};
304 $self->{ScopeThisXSUB} = 0;
308 $_ = shift(@{ $self->{line} });
309 while (my $kwd = $self->check_keyword("REQUIRE|PROTOTYPES|EXPORT_XSUB_SYMBOLS|FALLBACK|VERSIONCHECK|INCLUDE(?:_COMMAND)?|SCOPE")) {
310 my $method = $kwd . "_handler";
312 next PARAGRAPH unless @{ $self->{line} };
313 $_ = shift(@{ $self->{line} });
316 if ($self->check_keyword("BOOT")) {
317 check_conditional_preprocessor_statements($self);
318 push (@{ $BootCode_ref }, "#line $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} }] \""
319 . escape_file_for_line_directive($self->{filepathname}) . "\"")
320 if $self->{WantLineNumbers} && $self->{line}->[0] !~ /^\s*#\s*line\b/;
321 push (@{ $BootCode_ref }, @{ $self->{line} }, "");
325 # extract return type, function name and arguments
326 ($self->{ret_type}) = tidy_type($_);
327 my $RETVAL_no_return = 1 if $self->{ret_type} =~ s/^NO_OUTPUT\s+//;
329 # Allow one-line ANSI-like declaration
330 unshift @{ $self->{line} }, $2
332 and $self->{ret_type} =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
334 # a function definition needs at least 2 lines
335 $self->blurt("Error: Function definition too short '$self->{ret_type}'"), next PARAGRAPH
336 unless @{ $self->{line} };
338 my $externC = 1 if $self->{ret_type} =~ s/^extern "C"\s+//;
339 my $static = 1 if $self->{ret_type} =~ s/^static\s+//;
341 my $func_header = shift(@{ $self->{line} });
342 $self->blurt("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
343 unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
345 my ($class, $orig_args);
346 ($class, $func_name, $orig_args) = ($1, $2, $3);
347 $class = "$4 $class" if $4;
348 ($pname = $func_name) =~ s/^($self->{Prefix})?/$self->{Packprefix}/;
350 ($clean_func_name = $func_name) =~ s/^$self->{Prefix}//;
351 $Full_func_name = "$self->{Packid}_$clean_func_name";
353 $Full_func_name = $SymSet->addsym($Full_func_name);
356 # Check for duplicate function definition
357 for my $tmp (@{ $self->{XSStack} }) {
358 next unless defined $tmp->{functions}{$Full_func_name};
359 Warn( $self, "Warning: duplicate function definition '$clean_func_name' detected");
362 $self->{XSStack}->[$XSS_work_idx]{functions}{$Full_func_name}++;
363 %{ $self->{XsubAliases} } = ();
364 %{ $self->{XsubAliasValues} } = ();
365 %{ $self->{Interfaces} } = ();
366 @{ $self->{Attributes} } = ();
367 $self->{DoSetMagic} = 1;
369 $orig_args =~ s/\\\s*/ /g; # process line continuations
372 my (@fake_INPUT_pre); # For length(s) generated variables
374 my $only_C_inlist_ref = {}; # Not in the signature of Perl function
375 if ($self->{argtypes} and $orig_args =~ /\S/) {
376 my $args = "$orig_args ,";
377 if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
378 @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
382 my ($arg, $default) = ($_ =~ m/ ( [^=]* ) ( (?: = .* )? ) /x);
383 my ($pre, $len_name) = ($arg =~ /(.*?) \s*
384 \b ( \w+ | length\( \s*\w+\s* \) )
386 next unless defined($pre) && length($pre);
389 if ($self->{inout} and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\b\s*//) {
391 $out_type = $type if $type ne 'IN';
392 $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\b\s*//;
393 $pre =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\b\s*//;
396 if ($len_name =~ /^length\( \s* (\w+) \s* \)\z/x) {
397 $len_name = "XSauto_length_of_$1";
399 die "Default value on length() argument: '$_'"
402 if (length $pre or $islength) { # Has a type
404 push @fake_INPUT_pre, $arg;
407 push @fake_INPUT, $arg;
409 # warn "pushing '$arg'\n";
410 $self->{argtype_seen}->{$len_name}++;
411 $_ = "$len_name$default"; # Assigns to @args
413 $only_C_inlist_ref->{$_} = 1 if $out_type eq "OUTLIST" or $islength;
414 push @{ $outlist_ref }, $len_name if $out_type =~ /OUTLIST$/;
415 $self->{in_out}->{$len_name} = $out_type if $out_type;
419 @args = split(/\s*,\s*/, $orig_args);
420 Warn( $self, "Warning: cannot parse argument list '$orig_args', fallback to split");
424 @args = split(/\s*,\s*/, $orig_args);
426 if ($self->{inout} and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\b\s*//) {
428 next if $out_type eq 'IN';
429 $only_C_inlist_ref->{$_} = 1 if $out_type eq "OUTLIST";
430 if ($out_type =~ /OUTLIST$/) {
431 push @{ $outlist_ref }, undef;
433 $self->{in_out}->{$_} = $out_type;
437 if (defined($class)) {
438 my $arg0 = ((defined($static) or $func_name eq 'new')
440 unshift(@args, $arg0);
445 my $report_args = '';
447 foreach my $i (0 .. $#args) {
448 if ($args[$i] =~ s/\.\.\.//) {
450 if ($args[$i] eq '' && $i == $#args) {
451 $report_args .= ", ...";
456 if ($only_C_inlist_ref->{$args[$i]}) {
457 push @args_num, undef;
460 push @args_num, ++$num_args;
461 $report_args .= ", $args[$i]";
463 if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
466 $self->{defaults}->{$args[$i]} = $2;
467 $self->{defaults}->{$args[$i]} =~ s/"/\\"/g;
469 $self->{proto_arg}->[$i+1] = '$';
471 my $min_args = $num_args - $extra_args;
472 $report_args =~ s/"/\\"/g;
473 $report_args =~ s/^,\s+//;
474 $self->{func_args} = assign_func_args($self, \@args, $class);
475 @{ $self->{args_match} }{@args} = @args_num;
477 my $PPCODE = grep(/^\s*PPCODE\s*:/, @{ $self->{line} });
478 my $CODE = grep(/^\s*CODE\s*:/, @{ $self->{line} });
479 # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
480 # to set explicit return values.
481 my $EXPLICIT_RETURN = ($CODE &&
482 ("@{ $self->{line} }" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
484 # The $ALIAS which follows is only explicitly called within the scope of
485 # process_file(). In principle, it ought to be a lexical, i.e., 'my
486 # $ALIAS' like the other nearby variables. However, implementing that
487 # change produced a slight difference in the resulting .c output in at
488 # least two distributions: B/BD/BDFOY/Crypt-Rijndael and
489 # G/GF/GFUJI/Hash-FieldHash. The difference is, arguably, an improvement
490 # in the resulting C code. Example:
492 # < GvNAME(CvGV(cv)),
494 # > "Crypt::Rijndael::encrypt",
495 # But at this point we're committed to generating the *same* C code that
496 # the current version of ParseXS.pm does. So we're declaring it as 'our'.
497 $ALIAS = grep(/^\s*ALIAS\s*:/, @{ $self->{line} });
499 my $INTERFACE = grep(/^\s*INTERFACE\s*:/, @{ $self->{line} });
501 $xsreturn = 1 if $EXPLICIT_RETURN;
503 $externC = $externC ? qq[extern "C"] : "";
505 # print function header
508 #XS_EUPXS(XS_${Full_func_name}); /* prototype to pass -Wmissing-prototypes */
509 #XS_EUPXS(XS_${Full_func_name})
513 print Q(<<"EOF") if $ALIAS;
516 print Q(<<"EOF") if $INTERFACE;
517 # dXSFUNCTION($self->{ret_type});
520 $self->{cond} = set_cond($ellipsis, $min_args, $num_args);
522 print Q(<<"EOF") if $self->{except};
530 # croak_xs_usage(cv, "$report_args");
534 # cv likely to be unused
536 # PERL_UNUSED_VAR(cv); /* -W */
540 #gcc -Wall: if an xsub has PPCODE is used
541 #it is possible none of ST, XSRETURN or XSprePUSH macros are used
542 #hence 'ax' (setup by dXSARGS) is unused
543 #XXX: could breakup the dXSARGS; into dSP;dMARK;dITEMS
544 #but such a move could break third-party extensions
545 print Q(<<"EOF") if $PPCODE;
546 # PERL_UNUSED_VAR(ax); /* -Wall */
549 print Q(<<"EOF") if $PPCODE;
553 # Now do a block of some sort.
555 $self->{condnum} = 0;
556 $self->{cond} = ''; # last CASE: conditional
557 push(@{ $self->{line} }, "$END:");
558 push(@{ $self->{line_no} }, $self->{line_no}->[-1]);
560 check_conditional_preprocessor_statements();
561 while (@{ $self->{line} }) {
563 $self->CASE_handler($_) if $self->check_keyword("CASE");
568 # do initialization of input variables
569 $self->{thisdone} = 0;
570 $self->{retvaldone} = 0;
571 $self->{deferred} = "";
572 %{ $self->{arg_list} } = ();
573 $self->{gotRETVAL} = 0;
574 $self->INPUT_handler($_);
575 $self->process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE|OVERLOAD");
577 print Q(<<"EOF") if $self->{ScopeThisXSUB};
582 if (!$self->{thisdone} && defined($class)) {
583 if (defined($static) or $func_name eq 'new') {
585 $self->{var_types}->{"CLASS"} = "char *";
590 printed_name => undef,
595 $self->{var_types}->{"THIS"} = "$class *";
600 printed_name => undef,
605 # These are set if OUTPUT is found and/or CODE using RETVAL
606 $self->{have_OUTPUT} = $self->{have_CODE_with_RETVAL} = 0;
610 if (/^\s*NOT_IMPLEMENTED_YET/) {
611 print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
615 if ($self->{ret_type} ne "void") {
616 print "\t" . map_type($self, $self->{ret_type}, 'RETVAL') . ";\n"
617 if !$self->{retvaldone};
618 $self->{args_match}->{"RETVAL"} = 0;
619 $self->{var_types}->{"RETVAL"} = $self->{ret_type};
620 my $outputmap = $self->{typemap}->get_outputmap( ctype => $self->{ret_type} );
622 if $self->{optimize} and $outputmap and $outputmap->targetable;
625 if (@fake_INPUT or @fake_INPUT_pre) {
626 unshift @{ $self->{line} }, @fake_INPUT_pre, @fake_INPUT, $_;
628 $self->{processing_arg_with_types} = 1;
629 $self->INPUT_handler($_);
631 print $self->{deferred};
633 $self->process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS|OVERLOAD");
635 if ($self->check_keyword("PPCODE")) {
636 $self->print_section();
637 $self->death("PPCODE must be last thing") if @{ $self->{line} };
638 print "\tLEAVE;\n" if $self->{ScopeThisXSUB};
639 print "\tPUTBACK;\n\treturn;\n";
641 elsif ($self->check_keyword("CODE")) {
642 my $consumed_code = $self->print_section();
643 if ($consumed_code =~ /\bRETVAL\b/) {
644 $self->{have_CODE_with_RETVAL} = 1;
647 elsif (defined($class) and $func_name eq "DESTROY") {
649 print "delete THIS;\n";
653 if ($self->{ret_type} ne "void") {
657 if (defined($static)) {
658 if ($func_name eq 'new') {
659 $func_name = "$class";
665 elsif (defined($class)) {
666 if ($func_name eq 'new') {
667 $func_name .= " $class";
673 $func_name =~ s/^\Q$args{'s'}//
674 if exists $args{'s'};
675 $func_name = 'XSFUNCTION' if $self->{interface};
676 print "$func_name($self->{func_args});\n";
680 # do output variables
681 $self->{gotRETVAL} = 0; # 1 if RETVAL seen in OUTPUT section;
682 undef $self->{RETVAL_code} ; # code to set RETVAL (from OUTPUT section);
683 # $wantRETVAL set if 'RETVAL =' autogenerated
684 ($wantRETVAL, $self->{ret_type}) = (0, 'void') if $RETVAL_no_return;
685 undef %{ $self->{outargs} };
687 $self->process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE|OVERLOAD");
689 # A CODE section with RETVAL, but no OUTPUT? FAIL!
690 if ($self->{have_CODE_with_RETVAL} and not $self->{have_OUTPUT} and $self->{ret_type} ne 'void') {
691 $self->Warn("Warning: Found a 'CODE' section which seems to be using 'RETVAL' but no 'OUTPUT' section.");
695 type => $self->{var_types}->{$_},
696 num => $self->{args_match}->{$_},
698 do_setmagic => $self->{DoSetMagic},
700 } ) for grep $self->{in_out}->{$_} =~ /OUT$/, keys %{ $self->{in_out} };
703 # all OUTPUT done, so now push the return value on the stack
704 if ($self->{gotRETVAL} && $self->{RETVAL_code}) {
705 print "\t$self->{RETVAL_code}\n";
707 elsif ($self->{gotRETVAL} || $wantRETVAL) {
708 my $outputmap = $self->{typemap}->get_outputmap( ctype => $self->{ret_type} );
709 my $t = $self->{optimize} && $outputmap && $outputmap->targetable;
710 # Although the '$var' declared in the next line is never explicitly
711 # used within this 'elsif' block, commenting it out leads to
712 # disaster, starting with the first 'eval qq' inside the 'elsif' block
714 # It appears that this is related to the fact that at this point the
715 # value of $t is a reference to an array whose [2] element includes
716 # '$var' as a substring:
719 my $type = $self->{ret_type};
721 if ($t and not $t->{with_size} and $t->{type} eq 'p') {
722 # PUSHp corresponds to setpvn. Treat setpv directly
723 my $what = eval qq("$t->{what}");
726 print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
730 my $what = eval qq("$t->{what}");
733 my $tsize = $t->{what_size};
734 $tsize = '' unless defined $tsize;
735 $tsize = eval qq("$tsize");
737 print "\tXSprePUSH; PUSH$t->{type}($what$tsize);\n";
741 # RETVAL almost never needs SvSETMAGIC()
743 type => $self->{ret_type},
752 $xsreturn = 1 if $self->{ret_type} ne "void";
754 my $c = @{ $outlist_ref };
755 print "\tXSprePUSH;" if $c and not $prepush_done;
756 print "\tEXTEND(SP,$c);\n" if $c;
759 type => $self->{var_types}->{$_},
764 } ) for @{ $outlist_ref };
767 $self->process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE|OVERLOAD");
769 print Q(<<"EOF") if $self->{ScopeThisXSUB};
772 print Q(<<"EOF") if $self->{ScopeThisXSUB} and not $PPCODE;
776 # print function trailer
780 print Q(<<"EOF") if $self->{except};
783 # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
786 if ($self->check_keyword("CASE")) {
787 $self->blurt("Error: No 'CASE:' at top of function")
788 unless $self->{condnum};
789 $_ = "CASE: $_"; # Restore CASE: label
792 last if $_ eq "$END:";
793 $self->death(/^$self->{BLOCK_re}/o ? "Misplaced '$1:'" : "Junk at end of function ($_)");
796 print Q(<<"EOF") if $self->{except};
798 # Perl_croak(aTHX_ errbuf);
802 print Q(<<"EOF") unless $PPCODE;
803 # XSRETURN($xsreturn);
807 print Q(<<"EOF") unless $PPCODE;
817 $self->{newXS} = "newXS";
820 # Build the prototype string for the xsub
821 if ($self->{ProtoThisXSUB}) {
822 $self->{newXS} = "newXSproto_portable";
824 if ($self->{ProtoThisXSUB} eq 2) {
825 # User has specified empty prototype
827 elsif ($self->{ProtoThisXSUB} eq 1) {
829 if ($min_args < $num_args) {
831 $self->{proto_arg}->[$min_args] .= ";";
833 push @{ $self->{proto_arg} }, "$s\@"
836 $self->{proto} = join ("", grep defined, @{ $self->{proto_arg} } );
839 # User has specified a prototype
840 $self->{proto} = $self->{ProtoThisXSUB};
842 $self->{proto} = qq{, "$self->{proto}"};
845 if (%{ $self->{XsubAliases} }) {
846 $self->{XsubAliases}->{$pname} = 0
847 unless defined $self->{XsubAliases}->{$pname};
848 while ( my ($xname, $value) = each %{ $self->{XsubAliases} }) {
849 push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
850 # cv = $self->{newXS}(\"$xname\", XS_$Full_func_name, file$self->{proto});
851 # XSANY.any_i32 = $value;
855 elsif (@{ $self->{Attributes} }) {
856 push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
857 # cv = $self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});
858 # apply_attrs_string("$Package", cv, "@{ $self->{Attributes} }", 0);
861 elsif ($self->{interface}) {
862 while ( my ($yname, $value) = each %{ $self->{Interfaces} }) {
863 $yname = "$Package\::$yname" unless $yname =~ /::/;
864 push(@{ $self->{InitFileCode} }, Q(<<"EOF"));
865 # cv = $self->{newXS}(\"$yname\", XS_$Full_func_name, file$self->{proto});
866 # $self->{interface_macro_set}(cv,$value);
870 elsif($self->{newXS} eq 'newXS'){ # work around P5NCI's empty newXS macro
871 push(@{ $self->{InitFileCode} },
872 " $self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});\n");
875 push(@{ $self->{InitFileCode} },
876 " (void)$self->{newXS}(\"$pname\", XS_$Full_func_name, file$self->{proto});\n");
878 } # END 'PARAGRAPH' 'while' loop
880 if ($self->{Overload}) { # make it findable with fetchmethod
882 #XS_EUPXS(XS_$self->{Packid}_nil); /* prototype to pass -Wmissing-prototypes */
883 #XS_EUPXS(XS_$self->{Packid}_nil)
890 unshift(@{ $self->{InitFileCode} }, <<"MAKE_FETCHMETHOD_WORK");
891 /* Making a sub named "${Package}::()" allows the package */
892 /* to be findable via fetchmethod(), and causes */
893 /* overload::Overloaded("${Package}") to return true. */
894 (void)$self->{newXS}("${Package}::()", XS_$self->{Packid}_nil, file$self->{proto});
895 MAKE_FETCHMETHOD_WORK
898 # print initialization routine
907 #XS_EXTERNAL(boot_$self->{Module_cname}); /* prototype to pass -Wmissing-prototypes */
908 #XS_EXTERNAL(boot_$self->{Module_cname})
916 #Under 5.8.x and lower, newXS is declared in proto.h as expecting a non-const
917 #file name argument. If the wrong qualifier is used, it causes breakage with
918 #C++ compilers and warnings with recent gcc.
919 #-Wall: if there is no $Full_func_name there are no xsubs in this .xs
921 print Q(<<"EOF") if $Full_func_name;
922 ##if (PERL_REVISION == 5 && PERL_VERSION < 9)
923 # char* file = __FILE__;
925 # const char* file = __FILE__;
932 # PERL_UNUSED_VAR(cv); /* -W */
933 # PERL_UNUSED_VAR(items); /* -W */
934 ##ifdef XS_APIVERSION_BOOTCHECK
935 # XS_APIVERSION_BOOTCHECK;
939 print Q(<<"EOF") if $self->{WantVersionChk};
940 # XS_VERSION_BOOTCHECK;
944 print Q(<<"EOF") if defined $self->{xsubaliases} or defined $self->{interfaces};
950 print Q(<<"EOF") if ($self->{Overload});
951 # /* register the overloading (type 'A') magic */
952 ##if (PERL_REVISION == 5 && PERL_VERSION < 9)
953 # PL_amagic_generation++;
955 # /* The magic for overload gets a GV* via gv_fetchmeth as */
956 # /* mentioned above, and looks in the SV* slot of it for */
957 # /* the "fallback" status. */
959 # get_sv( "${Package}::()", TRUE ),
964 print @{ $self->{InitFileCode} };
966 print Q(<<"EOF") if defined $self->{xsubaliases} or defined $self->{interfaces};
970 if (@{ $BootCode_ref }) {
971 print "\n /* Initialisation Section */\n\n";
972 @{ $self->{line} } = @{ $BootCode_ref };
973 $self->print_section();
974 print "\n /* End of Initialisation Section */\n\n";
978 ##if (PERL_REVISION == 5 && PERL_VERSION >= 9)
979 # if (PL_unitcheckav)
980 # call_list(PL_scopestack_ix, PL_unitcheckav);
990 warn("Please specify prototyping behavior for $self->{filename} (see perlxs manual)\n")
991 unless $self->{ProtoUsed};
995 untie *PSEUDO_STDOUT if tied *PSEUDO_STDOUT;
1001 sub report_error_count { $self->{errors} }
1003 # Input: ($self, $_, @{ $self->{line} }) == unparsed input.
1004 # Output: ($_, @{ $self->{line} }) == (rest of line, following lines).
1005 # Return: the matched keyword if found, otherwise 0
1008 $_ = shift(@{ $self->{line} }) while !/\S/ && @{ $self->{line} };
1009 s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
1015 # the "do" is required for right semantics
1016 do { $_ = shift(@{ $self->{line} }) } while !/\S/ && @{ $self->{line} };
1018 my $consumed_code = '';
1020 print("#line ", $self->{line_no}->[@{ $self->{line_no} } - @{ $self->{line} } -1], " \"",
1021 escape_file_for_line_directive($self->{filepathname}), "\"\n")
1022 if $self->{WantLineNumbers} && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
1023 for (; defined($_) && !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1025 $consumed_code .= "$_\n";
1027 print 'ExtUtils::ParseXS::CountLines'->end_marker, "\n" if $self->{WantLineNumbers};
1029 return $consumed_code;
1036 while (!/\S/ && @{ $self->{line} }) {
1037 $_ = shift(@{ $self->{line} });
1040 for (; defined($_) && !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1047 sub process_keyword {
1048 my($self, $pattern) = @_;
1050 while (my $kwd = $self->check_keyword($pattern)) {
1051 my $method = $kwd . "_handler";
1059 $self->blurt("Error: 'CASE:' after unconditional 'CASE:'")
1060 if $self->{condnum} && $self->{cond} eq '';
1062 trim_whitespace($self->{cond});
1063 print " ", ($self->{condnum}++ ? " else" : ""), ($self->{cond} ? " if ($self->{cond})\n" : "\n");
1070 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1071 last if /^\s*NOT_IMPLEMENTED_YET/;
1072 next unless /\S/; # skip blank lines
1074 trim_whitespace($_);
1077 # remove trailing semicolon if no initialisation
1078 s/\s*;$//g unless /[=;+].*\S/;
1080 # Process the length(foo) declarations
1081 if (s/^([^=]*)\blength\(\s*(\w+)\s*\)\s*$/$1 XSauto_length_of_$2=NO_INIT/x) {
1082 print "\tSTRLEN\tSTRLEN_length_of_$2;\n";
1083 $self->{lengthof}->{$2} = undef;
1084 $self->{deferred} .= "\n\tXSauto_length_of_$2 = STRLEN_length_of_$2;\n";
1087 # check for optional initialisation code
1089 $var_init = $1 if s/\s*([=;+].*)$//s;
1090 $var_init =~ s/"/\\"/g;
1091 # *sigh* It's valid to supply explicit input typemaps in the argument list...
1092 my $is_overridden_typemap = $var_init =~ /ST\s*\(|\$arg\b/;
1095 my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
1096 or $self->blurt("Error: invalid argument declaration '$ln'"), next;
1098 # Check for duplicate definitions
1099 $self->blurt("Error: duplicate definition of argument '$var_name' ignored"), next
1100 if $self->{arg_list}->{$var_name}++
1101 or defined $self->{argtype_seen}->{$var_name} and not $self->{processing_arg_with_types};
1103 $self->{thisdone} |= $var_name eq "THIS";
1104 $self->{retvaldone} |= $var_name eq "RETVAL";
1105 $self->{var_types}->{$var_name} = $var_type;
1106 # XXXX This check is a safeguard against the unfinished conversion of
1107 # generate_init(). When generate_init() is fixed,
1108 # one can use 2-args map_type() unconditionally.
1110 if ($var_type =~ / \( \s* \* \s* \) /x) {
1111 # Function pointers are not yet supported with output_init()!
1112 print "\t" . map_type($self, $var_type, $var_name);
1116 print "\t" . map_type($self, $var_type, undef);
1119 $self->{var_num} = $self->{args_match}->{$var_name};
1121 if ($self->{var_num}) {
1122 my $typemap = $self->{typemap}->get_typemap(ctype => $var_type);
1123 $self->report_typemap_failure($self->{typemap}, $var_type, "death")
1124 if not $typemap and not $is_overridden_typemap;
1125 $self->{proto_arg}->[$self->{var_num}] = ($typemap && $typemap->proto) || "\$";
1127 $self->{func_args} =~ s/\b($var_name)\b/&$1/ if $var_addr;
1128 if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
1129 or $self->{in_out}->{$var_name} and $self->{in_out}->{$var_name} =~ /^OUT/
1130 and $var_init !~ /\S/) {
1131 if ($printed_name) {
1135 print "\t$var_name;\n";
1138 elsif ($var_init =~ /\S/) {
1141 num => $self->{var_num},
1144 printed_name => $printed_name,
1147 elsif ($self->{var_num}) {
1150 num => $self->{var_num},
1152 printed_name => $printed_name,
1161 sub OUTPUT_handler {
1163 $self->{have_OUTPUT} = 1;
1166 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1168 if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
1169 $self->{DoSetMagic} = ($1 eq "ENABLE" ? 1 : 0);
1172 my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s;
1173 $self->blurt("Error: duplicate OUTPUT argument '$outarg' ignored"), next
1174 if $self->{outargs}->{$outarg}++;
1175 if (!$self->{gotRETVAL} and $outarg eq 'RETVAL') {
1176 # deal with RETVAL last
1177 $self->{RETVAL_code} = $outcode;
1178 $self->{gotRETVAL} = 1;
1181 $self->blurt("Error: OUTPUT $outarg not an argument"), next
1182 unless defined($self->{args_match}->{$outarg});
1183 $self->blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
1184 unless defined $self->{var_types}->{$outarg};
1185 $self->{var_num} = $self->{args_match}->{$outarg};
1187 print "\t$outcode\n";
1188 print "\tSvSETMAGIC(ST(" , $self->{var_num} - 1 , "));\n" if $self->{DoSetMagic};
1192 type => $self->{var_types}->{$outarg},
1193 num => $self->{var_num},
1195 do_setmagic => $self->{DoSetMagic},
1199 delete $self->{in_out}->{$outarg} # No need to auto-OUTPUT
1200 if exists $self->{in_out}->{$outarg} and $self->{in_out}->{$outarg} =~ /OUT$/;
1204 sub C_ARGS_handler {
1207 my $in = $self->merge_section();
1209 trim_whitespace($in);
1210 $self->{func_args} = $in;
1213 sub INTERFACE_MACRO_handler {
1216 my $in = $self->merge_section();
1218 trim_whitespace($in);
1219 if ($in =~ /\s/) { # two
1220 ($self->{interface_macro}, $self->{interface_macro_set}) = split ' ', $in;
1223 $self->{interface_macro} = $in;
1224 $self->{interface_macro_set} = 'UNKNOWN_CVT'; # catch later
1226 $self->{interface} = 1; # local
1227 $self->{interfaces} = 1; # global
1230 sub INTERFACE_handler {
1233 my $in = $self->merge_section();
1235 trim_whitespace($in);
1237 foreach (split /[\s,]+/, $in) {
1238 my $iface_name = $_;
1239 $iface_name =~ s/^$self->{Prefix}//;
1240 $self->{Interfaces}->{$iface_name} = $_;
1243 # XSFUNCTION = $self->{interface_macro}($self->{ret_type},cv,XSANY.any_dptr);
1245 $self->{interface} = 1; # local
1246 $self->{interfaces} = 1; # global
1249 sub CLEANUP_handler {
1251 $self->print_section();
1254 sub PREINIT_handler {
1256 $self->print_section();
1259 sub POSTCALL_handler {
1261 $self->print_section();
1266 $self->print_section();
1274 # Parse alias definitions
1276 # alias = value alias = value ...
1278 while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
1279 my ($alias, $value) = ($1, $2);
1280 my $orig_alias = $alias;
1282 # check for optional package definition in the alias
1283 $alias = $self->{Packprefix} . $alias if $alias !~ /::/;
1285 # check for duplicate alias name & duplicate value
1286 Warn( $self, "Warning: Ignoring duplicate alias '$orig_alias'")
1287 if defined $self->{XsubAliases}->{$alias};
1289 Warn( $self, "Warning: Aliases '$orig_alias' and '$self->{XsubAliasValues}->{$value}' have identical values")
1290 if $self->{XsubAliasValues}->{$value};
1292 $self->{xsubaliases} = 1;
1293 $self->{XsubAliases}->{$alias} = $value;
1294 $self->{XsubAliasValues}->{$value} = $orig_alias;
1297 blurt( $self, "Error: Cannot parse ALIAS definitions from '$orig'")
1305 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1307 trim_whitespace($_);
1308 push @{ $self->{Attributes} }, $_;
1316 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1318 trim_whitespace($_);
1319 $self->get_aliases($_) if $_;
1323 sub OVERLOAD_handler {
1327 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1329 trim_whitespace($_);
1330 while ( s/^\s*([\w:"\\)\+\-\*\/\%\<\>\.\&\|\^\!\~\{\}\=]+)\s*//) {
1331 $self->{Overload} = 1 unless $self->{Overload};
1332 my $overload = "$Package\::(".$1;
1333 push(@{ $self->{InitFileCode} },
1334 " (void)$self->{newXS}(\"$overload\", XS_$Full_func_name, file$self->{proto});\n");
1339 sub FALLBACK_handler {
1343 # the rest of the current line should contain either TRUE,
1346 trim_whitespace($_);
1348 TRUE => "&PL_sv_yes", 1 => "&PL_sv_yes",
1349 FALSE => "&PL_sv_no", 0 => "&PL_sv_no",
1350 UNDEF => "&PL_sv_undef",
1353 # check for valid FALLBACK value
1354 $self->death("Error: FALLBACK: TRUE/FALSE/UNDEF") unless exists $map{uc $_};
1356 $self->{Fallback} = $map{uc $_};
1360 sub REQUIRE_handler {
1362 # the rest of the current line should contain a version number
1365 trim_whitespace($Ver);
1367 $self->death("Error: REQUIRE expects a version number")
1370 # check that the version number is of the form n.n
1371 $self->death("Error: REQUIRE: expected a number, got '$Ver'")
1372 unless $Ver =~ /^\d+(\.\d*)?/;
1374 $self->death("Error: xsubpp $Ver (or better) required--this is only $VERSION.")
1375 unless $VERSION >= $Ver;
1378 sub VERSIONCHECK_handler {
1382 # the rest of the current line should contain either ENABLE or
1385 trim_whitespace($_);
1387 # check for ENABLE/DISABLE
1388 $self->death("Error: VERSIONCHECK: ENABLE/DISABLE")
1389 unless /^(ENABLE|DISABLE)/i;
1391 $self->{WantVersionChk} = 1 if $1 eq 'ENABLE';
1392 $self->{WantVersionChk} = 0 if $1 eq 'DISABLE';
1396 sub PROTOTYPE_handler {
1402 $self->death("Error: Only 1 PROTOTYPE definition allowed per xsub")
1403 if $self->{proto_in_this_xsub}++;
1405 for (; !/^$self->{BLOCK_re}/o; $_ = shift(@{ $self->{line} })) {
1408 trim_whitespace($_);
1409 if ($_ eq 'DISABLE') {
1410 $self->{ProtoThisXSUB} = 0;
1412 elsif ($_ eq 'ENABLE') {
1413 $self->{ProtoThisXSUB} = 1;
1416 # remove any whitespace
1418 $self->death("Error: Invalid prototype '$_'")
1419 unless valid_proto_string($_);
1420 $self->{ProtoThisXSUB} = C_string($_);
1424 # If no prototype specified, then assume empty prototype ""
1425 $self->{ProtoThisXSUB} = 2 unless $specified;
1427 $self->{ProtoUsed} = 1;
1434 $self->death("Error: Only 1 SCOPE declaration allowed per xsub")
1435 if $self->{scope_in_this_xsub}++;
1437 trim_whitespace($_);
1438 $self->death("Error: SCOPE: ENABLE/DISABLE")
1439 unless /^(ENABLE|DISABLE)\b/i;
1440 $self->{ScopeThisXSUB} = ( uc($1) eq 'ENABLE' );
1443 sub PROTOTYPES_handler {
1447 # the rest of the current line should contain either ENABLE or
1450 trim_whitespace($_);
1452 # check for ENABLE/DISABLE
1453 $self->death("Error: PROTOTYPES: ENABLE/DISABLE")
1454 unless /^(ENABLE|DISABLE)/i;
1456 $self->{WantPrototypes} = 1 if $1 eq 'ENABLE';
1457 $self->{WantPrototypes} = 0 if $1 eq 'DISABLE';
1458 $self->{ProtoUsed} = 1;
1461 sub EXPORT_XSUB_SYMBOLS_handler {
1465 # the rest of the current line should contain either ENABLE or
1468 trim_whitespace($_);
1470 # check for ENABLE/DISABLE
1471 $self->death("Error: EXPORT_XSUB_SYMBOLS: ENABLE/DISABLE")
1472 unless /^(ENABLE|DISABLE)/i;
1474 my $xs_impl = $1 eq 'ENABLE' ? 'XS_EXTERNAL' : 'XS_INTERNAL';
1478 ##if defined(PERL_EUPXS_ALWAYS_EXPORT)
1479 ## define XS_EUPXS(name) XS_EXTERNAL(name)
1480 ##elif defined(PERL_EUPXS_NEVER_EXPORT)
1481 ## define XS_EUPXS(name) XS_INTERNAL(name)
1483 ## define XS_EUPXS(name) $xs_impl(name)
1492 # Save the current file context.
1493 push(@{ $self->{XSStack} }, {
1495 LastLine => $self->{lastline},
1496 LastLineNo => $self->{lastline_no},
1497 Line => $self->{line},
1498 LineNo => $self->{line_no},
1499 Filename => $self->{filename},
1500 Filepathname => $self->{filepathname},
1501 Handle => $self->{FH},
1502 IsPipe => scalar($self->{filename} =~ /\|\s*$/),
1508 sub INCLUDE_handler {
1511 # the rest of the current line should contain a valid filename
1513 trim_whitespace($_);
1515 $self->death("INCLUDE: filename missing")
1518 $self->death("INCLUDE: output pipe is illegal")
1521 # simple minded recursion detector
1522 $self->death("INCLUDE loop detected")
1523 if $self->{IncludedFiles}->{$_};
1525 ++$self->{IncludedFiles}->{$_} unless /\|\s*$/;
1527 if (/\|\s*$/ && /^\s*perl\s/) {
1528 Warn( $self, "The INCLUDE directive with a command is discouraged." .
1529 " Use INCLUDE_COMMAND instead! In particular using 'perl'" .
1530 " in an 'INCLUDE: ... |' directive is not guaranteed to pick" .
1531 " up the correct perl. The INCLUDE_COMMAND directive allows" .
1532 " the use of \$^X as the currently running perl, see" .
1533 " 'perldoc perlxs' for details.");
1536 $self->PushXSStack();
1538 $self->{FH} = Symbol::gensym();
1541 open($self->{FH}, $_) or $self->death("Cannot open '$_': $!");
1545 #/* INCLUDE: Including '$_' from '$self->{filename}' */
1549 $self->{filename} = $_;
1550 $self->{filepathname} = ( $^O =~ /^mswin/i )
1551 ? qq($self->{dir}/$self->{filename}) # See CPAN RT #61908: gcc doesn't like backslashes on win32?
1552 : File::Spec->catfile($self->{dir}, $self->{filename});
1554 # Prime the pump by reading the first
1557 # skip leading blank lines
1558 while (readline($self->{FH})) {
1559 last unless /^\s*$/;
1562 $self->{lastline} = $_;
1563 $self->{lastline_no} = $.;
1568 my @args = split /\s+/, $cmd;
1571 $_ = q(").$_.q(") if !/^\"/ && length($_) > 0;
1573 return join (' ', ($cmd, @args));
1576 sub INCLUDE_COMMAND_handler {
1579 # the rest of the current line should contain a valid command
1581 trim_whitespace($_);
1583 $_ = QuoteArgs($_) if $^O eq 'VMS';
1585 $self->death("INCLUDE_COMMAND: command missing")
1588 $self->death("INCLUDE_COMMAND: pipes are illegal")
1589 if /^\s*\|/ or /\|\s*$/;
1591 $self->PushXSStack( IsPipe => 1 );
1593 $self->{FH} = Symbol::gensym();
1595 # If $^X is used in INCLUDE_COMMAND, we know it's supposed to be
1596 # the same perl interpreter as we're currently running
1600 open ($self->{FH}, "-|", $_)
1601 or $self->death( $self, "Cannot run command '$_' to include its output: $!");
1605 #/* INCLUDE_COMMAND: Including output of '$_' from '$self->{filename}' */
1609 $self->{filename} = $_;
1610 $self->{filepathname} = $self->{filename};
1611 #$self->{filepathname} =~ s/\"/\\"/g; # Fails? See CPAN RT #53938: MinGW Broken after 2.21
1612 $self->{filepathname} =~ s/\\/\\\\/g; # Works according to reporter of #53938
1614 # Prime the pump by reading the first
1617 # skip leading blank lines
1618 while (readline($self->{FH})) {
1619 last unless /^\s*$/;
1622 $self->{lastline} = $_;
1623 $self->{lastline_no} = $.;
1629 return 0 unless $self->{XSStack}->[-1]{type} eq 'file';
1631 my $data = pop @{ $self->{XSStack} };
1632 my $ThisFile = $self->{filename};
1633 my $isPipe = $data->{IsPipe};
1635 --$self->{IncludedFiles}->{$self->{filename}}
1640 $self->{FH} = $data->{Handle};
1641 # $filename is the leafname, which for some reason isused for diagnostic
1642 # messages, whereas $filepathname is the full pathname, and is used for
1644 $self->{filename} = $data->{Filename};
1645 $self->{filepathname} = $data->{Filepathname};
1646 $self->{lastline} = $data->{LastLine};
1647 $self->{lastline_no} = $data->{LastLineNo};
1648 @{ $self->{line} } = @{ $data->{Line} };
1649 @{ $self->{line_no} } = @{ $data->{LineNo} };
1651 if ($isPipe and $? ) {
1652 --$self->{lastline_no};
1653 print STDERR "Error reading from pipe '$ThisFile': $! in $self->{filename}, line $self->{lastline_no}\n" ;
1659 #/* INCLUDE: Returning to '$self->{filename}' from '$ThisFile' */
1669 $text =~ s/\[\[/{/g;
1670 $text =~ s/\]\]/}/g;
1674 # Read next xsub into @{ $self->{line} } from ($lastline, readline($self->{FH})).
1679 $self->death("Error: Unterminated '#if/#ifdef/#ifndef'")
1680 if !defined $self->{lastline} && $self->{XSStack}->[-1]{type} eq 'if';
1681 @{ $self->{line} } = ();
1682 @{ $self->{line_no} } = ();
1683 return $self->PopFile() if !defined $self->{lastline};
1685 if ($self->{lastline} =~
1686 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
1688 $Package = defined($2) ? $2 : ''; # keep -w happy
1689 $self->{Prefix} = defined($3) ? $3 : ''; # keep -w happy
1690 $self->{Prefix} = quotemeta $self->{Prefix};
1691 ($self->{Module_cname} = $Module) =~ s/\W/_/g;
1692 ($self->{Packid} = $Package) =~ tr/:/_/;
1693 $self->{Packprefix} = $Package;
1694 $self->{Packprefix} .= "::" if $self->{Packprefix} ne "";
1695 $self->{lastline} = "";
1699 # Skip embedded PODs
1700 while ($self->{lastline} =~ /^=/) {
1701 while ($self->{lastline} = readline($self->{FH})) {
1702 last if ($self->{lastline} =~ /^=cut\s*$/);
1704 $self->death("Error: Unterminated pod") unless $self->{lastline};
1705 $self->{lastline} = readline($self->{FH});
1706 chomp $self->{lastline};
1707 $self->{lastline} =~ s/^\s+$//;
1710 # This chunk of code strips out (and parses) embedded TYPEMAP blocks
1711 # which support a HEREdoc-alike block syntax.
1712 # This is special cased from the usual paragraph-handler logic
1713 # due to the HEREdoc-ish syntax.
1714 if ($self->{lastline} =~ /^TYPEMAP\s*:\s*<<\s*(?:(["'])(.+?)\1|([^\s'"]+))\s*;?\s*$/) {
1715 my $end_marker = quotemeta(defined($1) ? $2 : $3);
1718 $self->{lastline} = readline($self->{FH});
1719 $self->death("Error: Unterminated typemap") if not defined $self->{lastline};
1720 last if $self->{lastline} =~ /^$end_marker\s*$/;
1721 push @tmaplines, $self->{lastline};
1724 my $tmapcode = join "", @tmaplines;
1725 my $tmap = ExtUtils::Typemaps->new(
1726 string => $tmapcode,
1727 lineno_offset => ($self->current_line_number()||0)+1,
1728 fake_filename => $self->{filename},
1730 $self->{typemap}->merge(typemap => $tmap, replace => 1);
1732 $self->{lastline} = "";
1735 if ($self->{lastline} !~ /^\s*#/ ||
1737 # ANSI: if ifdef ifndef elif else endif define undef
1739 # gcc: warning include_next
1741 # others: ident (gcc notes that some cpps have this one)
1742 $self->{lastline} =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
1743 last if $self->{lastline} =~ /^\S/ && @{ $self->{line} } && $self->{line}->[-1] eq "";
1744 push(@{ $self->{line} }, $self->{lastline});
1745 push(@{ $self->{line_no} }, $self->{lastline_no});
1748 # Read next line and continuation lines
1749 last unless defined($self->{lastline} = readline($self->{FH}));
1750 $self->{lastline_no} = $.;
1752 $self->{lastline} .= $tmp_line
1753 while ($self->{lastline} =~ /\\$/ && defined($tmp_line = readline($self->{FH})));
1755 chomp $self->{lastline};
1756 $self->{lastline} =~ s/^\s+$//;
1758 pop(@{ $self->{line} }), pop(@{ $self->{line_no} }) while @{ $self->{line} } && $self->{line}->[-1] eq "";
1763 my $argsref = shift;
1764 my ($type, $num, $var, $init, $printed_name) = (
1769 $argsref->{printed_name}
1771 my $arg = $num ? "ST(" . ($num - 1) . ")" : "/* not a parameter */";
1773 if ( $init =~ /^=/ ) {
1774 if ($printed_name) {
1775 eval qq/print " $init\\n"/;
1778 eval qq/print "\\t$var $init\\n"/;
1783 if ( $init =~ s/^\+// && $num ) {
1788 printed_name => $printed_name,
1791 elsif ($printed_name) {
1796 eval qq/print "\\t$var;\\n"/;
1800 $self->{deferred} .= eval qq/"\\n\\t$init\\n"/;
1806 my $argsref = shift;
1807 my ($type, $num, $var, $printed_name) = (
1811 $argsref->{printed_name},
1813 my $arg = "ST(" . ($num - 1) . ")";
1814 my ($argoff, $ntype);
1817 my $typemaps = $self->{typemap};
1819 $type = tidy_type($type);
1820 $self->report_typemap_failure($typemaps, $type), return
1821 unless $typemaps->get_typemap(ctype => $type);
1823 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1825 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1826 my $typem = $typemaps->get_typemap(ctype => $type);
1827 my $xstype = $typem->xstype;
1828 $xstype =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
1829 if ($xstype eq 'T_PV' and exists $self->{lengthof}->{$var}) {
1830 print "\t$var" unless $printed_name;
1831 print " = ($type)SvPV($arg, STRLEN_length_of_$var);\n";
1832 die "default value not supported with length(NAME) supplied"
1833 if defined $self->{defaults}->{$var};
1836 $type =~ tr/:/_/ unless $self->{hiertype};
1838 my $inputmap = $typemaps->get_inputmap(xstype => $xstype);
1839 $self->blurt("Error: No INPUT definition for type '$type', typekind '" . $type->xstype . "' found"), return
1840 unless defined $inputmap;
1842 my $expr = $inputmap->cleaned_code;
1843 # Note: This gruesome bit either needs heavy rethinking or documentation. I vote for the former. --Steffen
1844 if ($expr =~ /DO_ARRAY_ELEM/) {
1845 my $subtypemap = $typemaps->get_typemap(ctype => $subtype);
1846 $self->report_typemap_failure($typemaps, $subtype), return
1848 my $subinputmap = $typemaps->get_inputmap(xstype => $subtypemap->xstype);
1849 $self->blurt("Error: No INPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found"), return
1850 unless $subinputmap;
1851 my $subexpr = $subinputmap->cleaned_code;
1852 $subexpr =~ s/\$type/\$subtype/g;
1853 $subexpr =~ s/ntype/subtype/g;
1854 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1855 $subexpr =~ s/\n\t/\n\t\t/g;
1856 $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
1857 $subexpr =~ s/\$var/${var}\[ix_$var - $argoff]/;
1858 $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1860 if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1861 $self->{ScopeThisXSUB} = 1;
1863 if (defined($self->{defaults}->{$var})) {
1864 $expr =~ s/(\t+)/$1 /g;
1866 if ($printed_name) {
1870 eval qq/print "\\t$var;\\n"/;
1873 if ($self->{defaults}->{$var} eq 'NO_INIT') {
1874 $self->{deferred} .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
1877 $self->{deferred} .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $self->{defaults}->{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1881 elsif ($self->{ScopeThisXSUB} or $expr !~ /^\s*\$var =/) {
1882 if ($printed_name) {
1886 eval qq/print "\\t$var;\\n"/;
1889 $self->{deferred} .= eval qq/"\\n$expr;\\n"/;
1893 die "panic: do not know how to handle this branch for function pointers"
1895 eval qq/print "$expr;\\n"/;
1900 sub generate_output {
1901 my $argsref = shift;
1902 my ($type, $num, $var, $do_setmagic, $do_push) = (
1906 $argsref->{do_setmagic},
1909 my $arg = "ST(" . ($num - ($num != 0)) . ")";
1912 my $typemaps = $self->{typemap};
1914 $type = tidy_type($type);
1915 if ($type =~ /^array\(([^,]*),(.*)\)/) {
1916 print "\t$arg = sv_newmortal();\n";
1917 print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
1918 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1921 my $typemap = $typemaps->get_typemap(ctype => $type);
1922 $self->report_typemap_failure($typemaps, $type), return
1924 my $outputmap = $typemaps->get_outputmap(xstype => $typemap->xstype);
1925 $self->blurt("Error: No OUTPUT definition for type '$type', typekind '" . $typemap->xstype . "' found"), return
1927 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1928 $ntype =~ s/\(\)//g;
1930 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1932 my $expr = $outputmap->cleaned_code;
1933 if ($expr =~ /DO_ARRAY_ELEM/) {
1934 my $subtypemap = $typemaps->get_typemap(ctype => $subtype);
1935 $self->report_typemap_failure($typemaps, $subtype), return
1937 my $suboutputmap = $typemaps->get_outputmap(xstype => $subtypemap->xstype);
1938 $self->blurt("Error: No OUTPUT definition for type '$subtype', typekind '" . $subtypemap->xstype . "' found"), return
1939 unless $suboutputmap;
1940 my $subexpr = $suboutputmap->cleaned_code;
1941 $subexpr =~ s/ntype/subtype/g;
1942 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1943 $subexpr =~ s/\$var/${var}\[ix_$var]/g;
1944 $subexpr =~ s/\n\t/\n\t\t/g;
1945 $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
1946 eval "print qq\a$expr\a";
1948 print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
1950 elsif ($var eq 'RETVAL') {
1951 if ($expr =~ /^\t\$arg = new/) {
1952 # We expect that $arg has refcnt 1, so we need to
1954 eval "print qq\a$expr\a";
1956 print "\tsv_2mortal(ST($num));\n";
1957 print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
1959 elsif ($expr =~ /^\s*\$arg\s*=/) {
1960 # We expect that $arg has refcnt >=1, so we need
1962 eval "print qq\a$expr\a";
1964 print "\tsv_2mortal(ST(0));\n";
1965 print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
1968 # Just hope that the entry would safely write it
1969 # over an already mortalized value. By
1970 # coincidence, something like $arg = &sv_undef
1972 print "\tST(0) = sv_newmortal();\n";
1973 eval "print qq\a$expr\a";
1975 # new mortals don't have set magic
1979 print "\tPUSHs(sv_newmortal());\n";
1981 eval "print qq\a$expr\a";
1983 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1985 elsif ($arg =~ /^ST\(\d+\)$/) {
1986 eval "print qq\a$expr\a";
1988 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1995 # vim: ts=2 sw=2 et: