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