Bump version numbers of moules affected by change #22258
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Constant.pm
index 03f42e9..9730d91 100644 (file)
@@ -1,6 +1,6 @@
 package ExtUtils::Constant;
 use vars qw (@ISA $VERSION %XS_Constant %XS_TypeSet @EXPORT_OK %EXPORT_TAGS);
-$VERSION = '0.08';
+$VERSION = '0.14';
 
 =head1 NAME
 
@@ -12,8 +12,6 @@ ExtUtils::Constant - generate XS code to import C header constants
     WriteConstants(
         NAME => 'Foo',
         NAMES => [qw(FOO BAR BAZ)],
-        C_FILE => 'constants.c',
-        XS_FILE => 'constants.xs',
     );
     # Generates wrapper code to make the values of the constants FOO BAR BAZ
     #  available to perl
@@ -30,11 +28,11 @@ constants.
 
 Generally one only needs to call the C<WriteConstants> function, and then
 
-    #include "constants.c"
+    #include "const-c.inc"
 
 in the C section of C<Foo.xs>
 
-    INCLUDE constants.xs
+    INCLUDE const-xs.inc
 
 in the XS section of C<Foo.xs>.
 
@@ -68,7 +66,7 @@ NUL terminated string, length will be determined with C<strlen>
 A fixed length thing, given as a [pointer, length] pair. If you know the
 length of a string at compile time you may use this instead of I<PV>
 
-=item PVN
+=item SV
 
 A B<mortal> SV.
 
@@ -92,11 +90,15 @@ C<undef>.  The value of the macro is not needed.
 
 =cut
 
-require 5.006; # I think, for [:cntrl:] in REGEXP
-use warnings;
+if ($] >= 5.006) {
+  eval "use warnings; 1" or die $@;
+}
 use strict;
+use vars '$is_perl56';
 use Carp;
 
+$is_perl56 = ($] < 5.007 && $] > 5.005_50);
+
 use Exporter;
 use Text::Wrap;
 $Text::Wrap::huge = 'overflow';
@@ -106,12 +108,15 @@ $Text::Wrap::columns = 80;
 
 %EXPORT_TAGS = ( 'all' => [ qw(
        XS_constant constant_types return_clause memEQ_clause C_stringify
-       C_constant autoload WriteConstants
+       C_constant autoload WriteConstants WriteMakefileSnippet
 ) ] );
 
 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
 
+# '' is used as a flag to indicate non-ascii macro names, and hence the need
+# to pass in the utf8 on/off flag.
 %XS_Constant = (
+               ''    => '',
                IV    => 'PUSHi(iv)',
                UV    => 'PUSHu((UV)iv)',
                NV    => 'PUSHn(nv)',
@@ -138,8 +143,9 @@ $Text::Wrap::columns = 80;
 
 =item C_stringify NAME
 
-A function which returns a correctly \ escaped version of the string passed
-suitable for C's "" or ''.  It will also be valid as a perl "" string.
+A function which returns a 7 bit ASCII correctly \ escaped version of the
+string passed suitable for C's "" or ''. It will die if passed Unicode
+characters.
 
 =cut
 
@@ -147,6 +153,43 @@ suitable for C's "" or ''.  It will also be valid as a perl "" string.
 sub C_stringify {
   local $_ = shift;
   return unless defined $_;
+  # grr 5.6.1
+  confess "Wide character in '$_' intended as a C identifier"
+    if tr/\0-\377// != length;
+  # grr 5.6.1 moreso because its regexps will break on data that happens to
+  # be utf8, which includes my 8 bit test cases.
+  $_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if $is_perl56;
+  s/\\/\\\\/g;
+  s/([\"\'])/\\$1/g;   # Grr. fix perl mode.
+  s/\n/\\n/g;          # Ensure newlines don't end up in octal
+  s/\r/\\r/g;
+  s/\t/\\t/g;
+  s/\f/\\f/g;
+  s/\a/\\a/g;
+  s/([^\0-\177])/sprintf "\\%03o", ord $1/ge;
+  unless ($] < 5.006) {
+    # This will elicit a warning on 5.005_03 about [: :] being reserved unless
+    # I cheat
+    my $cheat = '([[:^print:]])';
+    s/$cheat/sprintf "\\%03o", ord $1/ge;
+  } else {
+    require POSIX;
+    s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
+  }
+  $_;
+}
+
+=item perl_stringify NAME
+
+A function which returns a 7 bit ASCII correctly \ escaped version of the
+string passed suitable for a perl "" string.
+
+=cut
+
+# Hopefully make a happy perl identifier.
+sub perl_stringify {
+  local $_ = shift;
+  return unless defined $_;
   s/\\/\\\\/g;
   s/([\"\'])/\\$1/g;   # Grr. fix perl mode.
   s/\n/\\n/g;          # Ensure newlines don't end up in octal
@@ -154,8 +197,30 @@ sub C_stringify {
   s/\t/\\t/g;
   s/\f/\\f/g;
   s/\a/\\a/g;
-  s/([[:cntrl:]])/sprintf "\\%03o", ord $1/ge;
-  s/\177/\\177/g;      # DEL doesn't seem to be a [:cntrl:]
+  unless ($] < 5.006) {
+    if ($] > 5.007) {
+      s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
+    } else {
+      # Grr 5.6.1. And I don't think I can use utf8; to force the regexp
+      # because 5.005_03 will fail.
+      # This is grim, but I also can't split on //
+      my $copy;
+      foreach my $index (0 .. length ($_) - 1) {
+        my $char = substr ($_, $index, 1);
+        $copy .= ($char le "\177") ? $char : sprintf "\\x{%X}", ord $char;
+      }
+      $_ = $copy;
+    }
+    # This will elicit a warning on 5.005_03 about [: :] being reserved unless
+    # I cheat
+    my $cheat = '([[:^print:]])';
+    s/$cheat/sprintf "\\%03o", ord $1/ge;
+  } else {
+    # Turns out "\x{}" notation only arrived with 5.6
+    s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge;
+    require POSIX;
+    s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
+  }
   $_;
 }
 
@@ -172,6 +237,7 @@ sub constant_types () {
   push @lines, "#define PERL_constant_NOTFOUND\t$start\n"; $start++;
   push @lines, "#define PERL_constant_NOTDEF\t$start\n"; $start++;
   foreach (sort keys %XS_Constant) {
+    next if $_ eq '';
     push @lines, "#define PERL_constant_IS$_\t$start\n"; $start++;
   }
   push @lines, << 'EOT';
@@ -179,6 +245,12 @@ sub constant_types () {
 #ifndef NVTYPE
 typedef double NV; /* 5.6 and later define NVTYPE, and typedef NV to it.  */
 #endif
+#ifndef aTHX_
+#define aTHX_ /* 5.6 or later define this for threading support.  */
+#endif
+#ifndef pTHX_
+#define pTHX_ /* 5.6 or later define this for threading support.  */
+#endif
 EOT
 
   return join '', @lines;
@@ -191,6 +263,11 @@ is equal to the C variable C<name>. If I<CHECKED_AT> is defined, then it
 is used to avoid C<memEQ> for short names, or to generate a comment to
 highlight the position of the character in the C<switch> statement.
 
+If I<CHECKED_AT> is a reference to a scalar, then instead it gives
+the characters pre-checked at the beginning, (and the number of chars by
+which the C variable name has been advanced. These need to be chopped from
+the front of I<NAME>).
+
 =cut
 
 sub memEQ_clause {
@@ -198,6 +275,14 @@ sub memEQ_clause {
   # Which could actually be a character comparison or even ""
   my ($name, $checked_at, $indent) = @_;
   $indent = ' ' x ($indent || 4);
+  my $front_chop;
+  if (ref $checked_at) {
+    # regexp won't work on 5.6.1 without use utf8; in turn that won't work
+    # on 5.005_03.
+    substr ($name, 0, length $$checked_at,) = '';
+    $front_chop = C_stringify ($$checked_at);
+    undef $checked_at;
+  }
   my $len = length $name;
 
   if ($len < 2) {
@@ -217,12 +302,38 @@ sub memEQ_clause {
       return $indent . "if (name[$check] == '$char') {\n";
     }
   }
-  # Could optimise a memEQ on 3 to 2 single character checks here
+  if (($len == 2 and !defined $checked_at)
+     or ($len == 3 and defined ($checked_at) and $checked_at == 2)) {
+    my $char1 = C_stringify (substr $name, 0, 1);
+    my $char2 = C_stringify (substr $name, 1, 1);
+    return $indent . "if (name[0] == '$char1' && name[1] == '$char2') {\n";
+  }
+  if (($len == 3 and defined ($checked_at) and $checked_at == 1)) {
+    my $char1 = C_stringify (substr $name, 0, 1);
+    my $char2 = C_stringify (substr $name, 2, 1);
+    return $indent . "if (name[0] == '$char1' && name[2] == '$char2') {\n";
+  }
+
+  my $pointer = '^';
+  my $have_checked_last = defined ($checked_at) && $len == $checked_at + 1;
+  if ($have_checked_last) {
+    # Checked at the last character, so no need to memEQ it.
+    $pointer = C_stringify (chop $name);
+    $len--;
+  }
+
   $name = C_stringify ($name);
   my $body = $indent . "if (memEQ(name, \"$name\", $len)) {\n";
-    $body .= $indent . "/*               ". (' ' x $checked_at) . '^'
-      . (' ' x ($len - $checked_at + length $len)) . "    */\n"
-        if defined $checked_at;
+  # Put a little ^ under the letter we checked at
+  # Screws up for non printable and non-7 bit stuff, but that's too hard to
+  # get right.
+  if (defined $checked_at) {
+    $body .= $indent . "/*               ". (' ' x $checked_at) . $pointer
+      . (' ' x ($len - $checked_at + length $len)) . "    */\n";
+  } elsif (defined $front_chop) {
+    $body .= $indent . "/*              $front_chop"
+      . (' ' x ($len + 1 + length $len)) . "    */\n";
+  }
   return $body;
 }
 
@@ -231,7 +342,7 @@ sub memEQ_clause {
 A function to return a suitable assignment clause. If I<TYPE> is aggregate
 (eg I<PVN> expects both pointer and length) then there should be multiple
 I<VALUE>s for the components. I<PRE> and I<POST> if defined give snippets
-of C code to preceed and follow the assignment. I<PRE> will be at the start
+of C code to proceed and follow the assignment. I<PRE> will be at the start
 of a block, so variables may be defined in it.
 
 =cut
@@ -253,7 +364,8 @@ sub assign {
     $close = "$indent}\n";
     $indent .= "  ";
   }
-  die "Can't generate code for type $type" unless exists $XS_TypeSet{$type};
+  confess "undef \$type" unless defined $type;
+  confess "Can't generate code for type $type" unless exists $XS_TypeSet{$type};
   my $typeset = $XS_TypeSet{$type};
   if (ref $typeset) {
     die "Type $type is aggregate, but only single value given"
@@ -277,31 +389,36 @@ sub assign {
   return $clause;
 }
 
-=item return_clause VALUE, TYPE, INDENT, MACRO, DEFAULT, PRE, POST, PRE, POST
+=item return_clause
 
-A function to return a suitable C<#ifdef> clause. I<MACRO> defaults to
-I<VALUE> when not defined.  If I<TYPE> is aggregate (eg I<PVN> expects both
-pointer and length) then I<VALUE> should be a reference to an array of
-values in the order expected by the type.  C<C_constant> will always call
-this function with I<MACRO> defined, defaulting to the constant's name.
-I<DEFAULT> if defined is an array reference giving default type and and
-value(s) if the clause generated by I<MACRO> doesn't evaluate to true.
-The two pairs I<PRE> and I<POST> if defined give C code snippets to proceed
-and follow the value, and the default value.
+return_clause ITEM, INDENT
+
+A function to return a suitable C<#ifdef> clause. I<ITEM> is a hashref
+(as passed to C<C_constant> and C<match_clause>. I<INDENT> is the number
+of spaces to indent, defaulting to 6.
 
 =cut
 
-sub return_clause ($$$$$$$$$) {
+sub return_clause ($$) {
 ##ifdef thingy
 #      *iv_return = thingy;
 #      return PERL_constant_ISIV;
 ##else
 #      return PERL_constant_NOTDEF;
 ##endif
-  my ($value, $type, $indent, $macro, $default, $pre, $post,
-      $def_pre, $def_post) = @_;
+  my ($item, $indent) = @_;
+
+  my ($name, $value, $macro, $default, $pre, $post, $def_pre, $def_post, $type)
+    = @$item{qw (name value macro default pre post def_pre def_post type)};
+  $value = $name unless defined $value;
+  $macro = $name unless defined $macro;
+
   $macro = $value unless defined $macro;
   $indent = ' ' x ($indent || 6);
+  unless ($type) {
+    # use Data::Dumper; print STDERR Dumper ($item);
+    confess "undef \$type";
+  }
 
   my $clause;
 
@@ -337,7 +454,51 @@ sub return_clause ($$$$$$$$$) {
       $clause .= "#endif\n";
     }
   }
-  return $clause
+  return $clause;
+}
+
+=pod
+
+XXX document me
+
+=cut
+
+sub match_clause {
+  # $offset defined if we have checked an offset.
+  my ($item, $offset, $indent) = @_;
+  $indent = ' ' x ($indent || 4);
+  my $body = '';
+  my ($no, $yes, $either, $name, $inner_indent);
+  if (ref $item eq 'ARRAY') {
+    ($yes, $no) = @$item;
+    $either = $yes || $no;
+    confess "$item is $either expecting hashref in [0] || [1]"
+      unless ref $either eq 'HASH';
+    $name = $either->{name};
+  } else {
+    confess "$item->{name} has utf8 flag '$item->{utf8}', should be false"
+      if $item->{utf8};
+    $name = $item->{name};
+    $inner_indent = $indent;
+  }
+
+  $body .= memEQ_clause ($name, $offset, length $indent);
+  if ($yes) {
+    $body .= $indent . "  if (utf8) {\n";
+  } elsif ($no) {
+    $body .= $indent . "  if (!utf8) {\n";
+  }
+  if ($either) {
+    $body .= return_clause ($either, 4 + length $indent);
+    if ($yes and $no) {
+      $body .= $indent . "  } else {\n";
+      $body .= return_clause ($no, 4 + length $indent); 
+    }
+    $body .= $indent . "  }\n";
+  } else {
+    $body .= return_clause ($item, 2 + length $indent);
+  }
+  $body .= $indent . "}\n";
 }
 
 =item switch_clause INDENT, NAMELEN, ITEMHASH, ITEM...
@@ -364,16 +525,38 @@ sub switch_clause {
     $body = wrap ($leader, $follower, $comment) . "\n";
     $leader = $follower;
   }
-  $body .= wrap ($leader, $follower, join (" ", @names) . " */") . "\n";
+  my @safe_names = @names;
+  foreach (@safe_names) {
+    confess sprintf "Name '$_' is length %d, not $namelen", length
+      unless length == $namelen;
+    # Argh. 5.6.1
+    # next unless tr/A-Za-z0-9_//c;
+    next if tr/A-Za-z0-9_// == length;
+    $_ = '"' . perl_stringify ($_) . '"';
+    # Ensure that the enclosing C comment doesn't end
+    # by turning */  into *" . "/
+    s!\*\/!\*"."/!gs;
+    # gcc -Wall doesn't like finding /* inside a comment
+    s!\/\*!/"."\*!gs;
+  }
+  $body .= wrap ($leader, $follower, join (" ", @safe_names) . " */") . "\n";
   # Figure out what to switch on.
   # (RMS, Spread of jump table, Position, Hashref)
   my @best = (1e38, ~0);
-  foreach my $i (0 .. ($namelen - 1)) {
+  # Prefer the last character over the others. (As it lets us shortern the
+  # memEQ clause at no cost).
+  foreach my $i ($namelen - 1, 0 .. ($namelen - 2)) {
     my ($min, $max) = (~0, 0);
     my %spread;
+    if ($is_perl56) {
+      # Need proper Unicode preserving hash keys for bytes in range 128-255
+      # here too, for some reason. grr 5.6.1 yet again.
+      tie %spread, 'ExtUtils::Constant::Aaargh56Hash';
+    }
     foreach (@names) {
       my $char = substr $_, $i, 1;
       my $ord = ord $char;
+      confess "char $ord is out of range" if $ord > 255;
       $max = $ord if $ord > $max;
       $min = $ord if $ord < $min;
       push @{$spread{$char}}, $_;
@@ -391,6 +574,8 @@ sub switch_clause {
     # the string wins. Because if that passes but the memEQ fails, it may
     # only need the start of the string to bin the choice.
     # I think. But I'm micro-optimising. :-)
+    # OK. Trump that. Now favour the last character of the string, before the
+    # rest.
     my $ss;
     $ss += @$_ * @$_ foreach values %spread;
     my $rms = sqrt ($ss / keys %spread);
@@ -398,27 +583,31 @@ sub switch_clause {
       @best = ($rms, $max - $min, $i, \%spread);
     }
   }
-  die "Internal error. Failed to pick a switch point for @names"
+  confess "Internal error. Failed to pick a switch point for @names"
     unless defined $best[2];
   # use Data::Dumper; print Dumper (@best);
   my ($offset, $best) = @best[2,3];
   $body .= $indent . "/* Offset $offset gives the best switch position.  */\n";
-  $body .= $indent . "switch (name[$offset]) {\n";
+
+  my $do_front_chop = $offset == 0 && $namelen > 2;
+  if ($do_front_chop) {
+    $body .= $indent . "switch (*name++) {\n";
+  } else {
+    $body .= $indent . "switch (name[$offset]) {\n";
+  }
   foreach my $char (sort keys %$best) {
+    confess sprintf "'$char' is %d bytes long, not 1", length $char
+      if length ($char) != 1;
+    confess sprintf "char %#X is out of range", ord $char if ord ($char) > 255;
     $body .= $indent . "case '" . C_stringify ($char) . "':\n";
     foreach my $name (sort @{$best->{$char}}) {
       my $thisone = $items->{$name};
-      my ($value, $macro, $default, $pre, $post, $def_pre, $def_post)
-        = @$thisone{qw (value macro default pre post def_pre def_post)};
-      $value = $name unless defined $value;
-      $macro = $name unless defined $macro;
-
-      # We have checked this offset.
-      $body .= memEQ_clause ($name, $offset, 2 + length $indent);
-      $body .= return_clause ($value, $thisone->{type},  4 + length $indent,
-                              $macro, $default, $pre, $post,
-                              $def_pre, $def_post);
-      $body .= $indent . "  }\n";
+      # warn "You are here";
+      if ($do_front_chop) {
+        $body .= match_clause ($thisone, \$char, 2 + length $indent);
+      } else {
+        $body .= match_clause ($thisone, $offset, 2 + length $indent);
+      }
     }
     $body .= $indent . "  break;\n";
   }
@@ -440,6 +629,7 @@ sub params {
     warn "ExtUtils::Constant doesn't know how to handle values of type $_" unless defined $XS_Constant{$_};
   }
   my $params = {};
+  $params->{''} = 1 if $what->{''};
   $params->{IV} = 1 if $what->{IV} || $what->{UV} || $what->{PVN};
   $params->{NV} = 1 if $what->{NV};
   $params->{PV} = 1 if $what->{PV} || $what->{PVN};
@@ -449,20 +639,51 @@ sub params {
 
 =item dump_names
 
-dump_names  PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
+dump_names DEFAULT_TYPE, TYPES, INDENT, OPTIONS, ITEM...
 
 An internal function to generate the embedded perl code that will regenerate
-the constant subroutines.  Parameters are the same as for C_constant.
+the constant subroutines.  I<DEFAULT_TYPE>, I<TYPES> and I<ITEM>s are the
+same as for C_constant.  I<INDENT> is treated as number of spaces to indent
+by.  I<OPTIONS> is a hashref of options. Currently only C<declare_types> is
+recognised.  If the value is true a C<$types> is always declared in the perl
+code generated, if defined and false never declared, and if undefined C<$types>
+is only declared if the values in I<TYPES> as passed in cannot be inferred from
+I<DEFAULT_TYPES> and the I<ITEM>s.
 
 =cut
 
 sub dump_names {
-  my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
-    = @_;
-  my (@simple, @complex);
+  my ($default_type, $what, $indent, $options, @items) = @_;
+  my $declare_types = $options->{declare_types};
+  $indent = ' ' x ($indent || 0);
+
+  my $result;
+  my (@simple, @complex, %used_types);
   foreach (@items) {
-    my $type = $_->{type} || $default_type;
-    if ($type eq $default_type and 0 == ($_->{name} =~ tr/A-Za-z0-9_//c)
+    my $type;
+    if (ref $_) {
+      $type = $_->{type} || $default_type;
+      if ($_->{utf8}) {
+        # For simplicity always skip the bytes case, and reconstitute this entry
+        # from its utf8 twin.
+        next if $_->{utf8} eq 'no';
+        # Copy the hashref, as we don't want to mess with the caller's hashref.
+        $_ = {%$_};
+        unless ($is_perl56) {
+          utf8::decode ($_->{name});
+        } else {
+          $_->{name} = pack 'U*', unpack 'U0U*', $_->{name};
+        }
+        delete $_->{utf8};
+      }
+    } else {
+      $_ = {name=>$_};
+      $type = $default_type;
+    }
+    $used_types{$type}++;
+    if ($type eq $default_type
+        # grr 5.6.1
+        and length $_->{name} == ($_->{name} =~ tr/A-Za-z0-9_//)
         and !defined ($_->{macro}) and !defined ($_->{value})
         and !defined ($_->{default}) and !defined ($_->{pre})
         and !defined ($_->{post}) and !defined ($_->{def_pre})
@@ -473,38 +694,34 @@ sub dump_names {
       push @complex, $_;
     }
   }
-  my $result = <<"EOT";
-  /* When generated this function returned values for the list of names given
-     in this section of perl code.  Rather than manually editing these functions
-     to add or remove constants, which would result in this comment and section
-     of code becoming inaccurate, we recommend that you edit this section of
-     code, and use it to regenerate a new set of constant functions which you
-     then use to replace the originals.
-
-     Regenerate these constant functions by feeding this entire source file to
-     perl -x
 
-#!$^X -w
-use ExtUtils::Constant qw (constant_types C_constant XS_constant);
-
-EOT
-  $result .= 'my $types = {map {($_, 1)} qw(' . join (" ", sort keys %$what)
-    . ")};\n";
-  $result .= wrap ("my \@names = (qw(",
-                  "               ", join (" ", sort @simple) . ")");
+  if (!defined $declare_types) {
+    # Do they pass in any types we weren't already using?
+    foreach (keys %$what) {
+      next if $used_types{$_};
+      $declare_types++; # Found one in $what that wasn't used.
+      last; # And one is enough to terminate this loop
+    }
+  }
+  if ($declare_types) {
+    $result = $indent . 'my $types = {map {($_, 1)} qw('
+      . join (" ", sort keys %$what) . ")};\n";
+  }
+  $result .= wrap ($indent . "my \@names = (qw(",
+                  $indent . "               ", join (" ", sort @simple) . ")");
   if (@complex) {
     foreach my $item (sort {$a->{name} cmp $b->{name}} @complex) {
-      my $name = C_stringify $item->{name};
-      my $line = ",\n            {name=>\"$name\"";
+      my $name = perl_stringify $item->{name};
+      my $line = ",\n$indent            {name=>\"$name\"";
       $line .= ", type=>\"$item->{type}\"" if defined $item->{type};
       foreach my $thing (qw (macro value default pre post def_pre def_post)) {
         my $value = $item->{$thing};
         if (defined $value) {
           if (ref $value) {
             $line .= ", $thing=>[\""
-              . join ('", "', map {C_stringify $_} @$value) . '"]';
+              . join ('", "', map {perl_stringify $_} @$value) . '"]';
           } else {
-            $line .= ", $thing=>\"" . C_stringify($value) . "\"";
+            $line .= ", $thing=>\"" . perl_stringify($value) . "\"";
           }
         }
       }
@@ -519,11 +736,43 @@ EOT
   }
   $result .= ");\n";
 
+  $result;
+}
+
+
+=item dogfood
+
+dogfood PACKAGE, SUBNAME, DEFAULT_TYPE, TYPES, INDENT, BREAKOUT, ITEM...
+
+An internal function to generate the embedded perl code that will regenerate
+the constant subroutines.  Parameters are the same as for C_constant.
+
+=cut
+
+sub dogfood {
+  my ($package, $subname, $default_type, $what, $indent, $breakout, @items)
+    = @_;
+  my $result = <<"EOT";
+  /* When generated this function returned values for the list of names given
+     in this section of perl code.  Rather than manually editing these functions
+     to add or remove constants, which would result in this comment and section
+     of code becoming inaccurate, we recommend that you edit this section of
+     code, and use it to regenerate a new set of constant functions which you
+     then use to replace the originals.
+
+     Regenerate these constant functions by feeding this entire source file to
+     perl -x
+
+#!$^X -w
+use ExtUtils::Constant qw (constant_types C_constant XS_constant);
+
+EOT
+  $result .= dump_names ($default_type, $what, 0, {declare_types=>1}, @items);
   $result .= <<'EOT';
 
 print constant_types(); # macro defs
 EOT
-  $package = C_stringify($package);
+  $package = perl_stringify($package);
   $result .=
     "foreach (C_constant (\"$package\", '$subname', '$default_type', \$types, ";
   # The form of the indent parameter isn't defined. (Yet)
@@ -617,6 +866,16 @@ Rarely needed.
 
 Equivalents of I<pre> and I<post> for the default value.
 
+=item utf8
+
+Generated internally. Is zero or undefined if name is 7 bit ASCII,
+"no" if the name is 8 bit (and so should only match if SvUTF8() is false),
+"yes" if the name is utf8 encoded.
+
+The internals automatically clone any name with characters 128-255 but none
+256+ (ie one that could be either in bytes or utf8) into a second entry
+which is utf8 encoded.
+
 =back
 
 I<PACKAGE> is the name of the package, and is only used in comments inside the
@@ -671,6 +930,11 @@ sub C_constant {
     # be a hashref, and pinch %$items from our parent to save recalculation.
     ($namelen, $items) = @$breakout;
   } else {
+    if ($is_perl56) {
+      # Need proper Unicode preserving hash keys.
+      $items = {};
+      tie %$items, 'ExtUtils::Constant::Aaargh56Hash';
+    }
     $breakout ||= 3;
     $default_type ||= 'IV';
     if (!ref $what) {
@@ -679,42 +943,85 @@ sub C_constant {
       # Figure out what types we're dealing with, and assign all unknowns to the
       # default type
     }
-    foreach (@items) {
-      my $name;
-      if (ref $_) {
-        my $orig = $_;
+    my @new_items;
+    foreach my $orig (@items) {
+      my ($name, $item);
+      if (ref $orig) {
         # Make a copy which is a normalised version of the ref passed in.
-        $name = $_->{name};
-        my ($type, $macro, $value) = @$_{qw (type macro value)};
+        $name = $orig->{name};
+        my ($type, $macro, $value) = @$orig{qw (type macro value)};
         $type ||= $default_type;
         $what->{$type} = 1;
-        $_ = {name=>$name, type=>$type};
+        $item = {name=>$name, type=>$type};
 
         undef $macro if defined $macro and $macro eq $name;
-        $_->{macro} = $macro if defined $macro;
+        $item->{macro} = $macro if defined $macro;
         undef $value if defined $value and $value eq $name;
-        $_->{value} = $value if defined $value;
+        $item->{value} = $value if defined $value;
         foreach my $key (qw(default pre post def_pre def_post)) {
           my $value = $orig->{$key};
-          $_->{$key} = $value if defined $value;
+          $item->{$key} = $value if defined $value;
           # warn "$key $value";
         }
       } else {
-        $name = $_;
-        $_ = {name=>$_, type=>$default_type};
+        $name = $orig;
+        $item = {name=>$name, type=>$default_type};
         $what->{$default_type} = 1;
       }
-      warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$_->{type}};
-      if (exists $items->{$name}) {
-        die "Multiple definitions for macro $name";
+      warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$item->{type}};
+      # tr///c is broken on 5.6.1 for utf8, so my original tr/\0-\177//c
+      # doesn't work. Upgrade to 5.8
+      # if ($name !~ tr/\0-\177//c || $] < 5.005_50) {
+      if ($name =~ tr/\0-\177// == length $name || $] < 5.005_50) {
+        # No characters outside 7 bit ASCII.
+        if (exists $items->{$name}) {
+          die "Multiple definitions for macro $name";
+        }
+        $items->{$name} = $item;
+      } else {
+        # No characters outside 8 bit. This is hardest.
+        if (exists $items->{$name} and ref $items->{$name} ne 'ARRAY') {
+          confess "Unexpected ASCII definition for macro $name";
+        }
+        # Again, 5.6.1 tr broken, so s/5\.6.*/5\.8\.0/;
+        # if ($name !~ tr/\0-\377//c) {
+        if ($name =~ tr/\0-\377// == length $name) {
+#          if ($] < 5.007) {
+#            $name = pack "C*", unpack "U*", $name;
+#          }
+          $item->{utf8} = 'no';
+          $items->{$name}[1] = $item;
+          push @new_items, $item;
+          # Copy item, to create the utf8 variant.
+          $item = {%$item};
+        }
+        # Encode the name as utf8 bytes.
+        unless ($is_perl56) {
+          utf8::encode($name);
+        } else {
+#          warn "Was >$name< " . length ${name};
+          $name = pack 'C*', unpack 'C*', $name . pack 'U*';
+#          warn "Now '${name}' " . length ${name};
+        }
+        if ($items->{$name}[0]) {
+          die "Multiple definitions for macro $name";
+        }
+        $item->{utf8} = 'yes';
+        $item->{name} = $name;
+        $items->{$name}[0] = $item;
+        # We have need for the utf8 flag.
+        $what->{''} = 1;
       }
-      $items->{$name} = $_;
+      push @new_items, $item;
     }
+    @items = @new_items;
+    # use Data::Dumper; print Dumper @items;
   }
   my $params = params ($what);
 
   my ($body, @subs) = "static int\n$subname (pTHX_ const char *name";
   $body .= ", STRLEN len" unless defined $namelen;
+  $body .= ", int utf8" if $params->{''};
   $body .= ", IV *iv_return" if $params->{IV};
   $body .= ", NV *nv_return" if $params->{NV};
   $body .= ", const char **pv_return" if $params->{PV};
@@ -730,8 +1037,8 @@ sub C_constant {
   } else {
     # We are the top level.
     $body .= "  /* Initially switch on the length of the name.  */\n";
-    $body .= dump_names ($package, $subname, $default_type, $what, $indent,
-                         $breakout, @items);
+    $body .= dogfood ($package, $subname, $default_type, $what, $indent,
+                      $breakout, @items);
     $body .= "  switch (len) {\n";
     # Need to group names of the same length
     my @by_length;
@@ -742,16 +1049,18 @@ sub C_constant {
       next unless $by_length[$i];      # None of this length
       $body .= "  case $i:\n";
       if (@{$by_length[$i]} == 1) {
-        my $thisone = $by_length[$i]->[0];
-        my ($name, $value, $macro, $default, $pre, $post, $def_pre, $def_post)
-          = @$thisone{qw (name value macro default pre post def_pre def_post)};
-        $value = $name unless defined $value;
-        $macro = $name unless defined $macro;
-
-        $body .= memEQ_clause ($name);
-        $body .= return_clause ($value, $thisone->{type}, undef, $macro,
-                                $default, $pre, $post, $def_pre, $def_post);
-        $body .= "    }\n";
+        my $only_thing = $by_length[$i]->[0];
+        if ($only_thing->{utf8}) {
+          if ($only_thing->{utf8} eq 'yes') {
+            # With utf8 on flag item is passed in element 0
+            $body .= match_clause ([$only_thing]);
+          } else {
+            # With utf8 off flag item is passed in element 1
+            $body .= match_clause ([undef, $only_thing]);
+          }
+        } else {
+          $body .= match_clause ($only_thing);
+        }
       } elsif (@{$by_length[$i]} < $breakout) {
         $body .= switch_clause (4, '', $i, $items, @{$by_length[$i]});
       } else {
@@ -760,11 +1069,13 @@ sub C_constant {
         my $what = {};
         foreach (@{$by_length[$i]}) {
           $what->{$_->{type}} = 1;
+          $what->{''} = 1 if $_->{utf8};
         }
         $params = params ($what);
         push @subs, C_constant ($package, "${subname}_$i", $default_type, $what,
                                 $indent, [$i, $items], @{$by_length[$i]});
         $body .= "    return ${subname}_$i (aTHX_ name";
+        $body .= ", utf8" if $params->{''};
         $body .= ", iv_return" if $params->{IV};
         $body .= ", nv_return" if $params->{NV};
         $body .= ", pv_return" if $params->{PV};
@@ -793,7 +1104,7 @@ be the same list of types as C<C_constant> was given.
 the number of parameters passed to the C function C<constant>]
 
 You can call the perl visible subroutine something other than C<constant> if
-you give the parameter I<SUBNAME>. The C subroutine it calls defaults to the
+you give the parameter I<SUBNAME>. The C subroutine it calls defaults to
 the name of the perl visible subroutine, unless you give the parameter
 I<C_SUBNAME>.
 
@@ -848,6 +1159,14 @@ EOT
     INPUT:
        SV *            sv;
         const char *   s = SvPV(sv, len);
+EOT
+  if ($params->{''}) {
+  $xs .= << 'EOT';
+    INPUT:
+       int             utf8 = SvUTF8(sv);
+EOT
+  }
+  $xs .= << 'EOT';
     PPCODE:
 EOT
 
@@ -858,6 +1177,7 @@ EOT
 EOT
   }
   $xs .= "     type = $C_subname(aTHX_ s, len";
+  $xs .= ', utf8' if $params->{''};
   $xs .= ', &iv' if $params->{IV};
   $xs .= ', &nv' if $params->{NV};
   $xs .= ', &pv' if $params->{PV};
@@ -880,6 +1200,8 @@ EOT
 EOT
 
   foreach $type (sort keys %XS_Constant) {
+    # '' marks utf8 flag needed.
+    next if $type eq '';
     $xs .= "\t/* Uncomment this if you need to return ${type}s\n"
       unless $what->{$type};
     $xs .= "        case PERL_constant_IS$type:\n";
@@ -983,11 +1305,50 @@ END
 }
 
 
+=item WriteMakefileSnippet
+
+WriteMakefileSnippet ATTRIBUTE =E<gt> VALUE [, ...] 
+
+A function to generate perl code for Makefile.PL that will regenerate
+the constant subroutines.  Parameters are named as passed to C<WriteConstants>,
+with the addition of C<INDENT> to specify the number of leading spaces
+(default 2).
+
+Currently only C<INDENT>, C<NAME>, C<DEFAULT_TYPE>, C<NAMES>, C<C_FILE> and
+C<XS_FILE> are recognised.
+
+=cut
+
+sub WriteMakefileSnippet {
+  my %args = @_;
+  my $indent = $args{INDENT} || 2;
+
+  my $result = <<"EOT";
+ExtUtils::Constant::WriteConstants(
+                                   NAME         => '$args{NAME}',
+                                   NAMES        => \\\@names,
+                                   DEFAULT_TYPE => '$args{DEFAULT_TYPE}',
+EOT
+  foreach (qw (C_FILE XS_FILE)) {
+    next unless exists $args{$_};
+    $result .= sprintf "                                   %-12s => '%s',\n",
+      $_, $args{$_};
+  }
+  $result .= <<'EOT';
+                                );
+EOT
+
+  $result =~ s/^/' 'x$indent/gem;
+  return dump_names ($args{DEFAULT_TYPE}, undef, $indent, undef,
+                           @{$args{NAMES}})
+          . $result;
+}
+
 =item WriteConstants ATTRIBUTE =E<gt> VALUE [, ...]
 
 Writes a file of C code and a file of XS code which you should C<#include>
 and C<INCLUDE> in the C and XS sections respectively of your module's XS
-code.  You probaby want to do this in your C<Makefile.PL>, so that you can
+code.  You probably want to do this in your C<Makefile.PL>, so that you can
 easily edit the list of constants without touching the rest of your module.
 The attributes supported are
 
@@ -1014,17 +1375,20 @@ as detailed in L<"C_constant">.
 =item C_FILE
 
 The name of the file to write containing the C code.  The default is
-C<constants.c>.
+C<const-c.inc>.  The C<-> in the name ensures that the file can't be
+mistaken for anything related to a legitimate perl package name, and
+not naming the file C<.c> avoids having to override Makefile.PL's
+C<.xs> to C<.c> rules.
 
 =item XS_FILE
 
 The name of the file to write containing the XS code.  The default is
-C<constants.xs>.
+C<const-xs.inc>.
 
 =item SUBNAME
 
 The perl visible name of the XS subroutine generated which will return the
-constants. The default is C<constant>.  
+constants. The default is C<constant>.
 
 =item C_SUBNAME
 
@@ -1040,8 +1404,8 @@ C<constant_10> with the default I<XS_SUBNAME>.
 sub WriteConstants {
   my %ARGS =
     ( # defaults
-     C_FILE =>       'constants.c',
-     XS_FILE =>      'constants.xs',
+     C_FILE =>       'const-c.inc',
+     XS_FILE =>      'const-xs.inc',
      SUBNAME =>      'constant',
      DEFAULT_TYPE => 'IV',
      @_);
@@ -1061,7 +1425,7 @@ sub WriteConstants {
   print $c_fh constant_types(); # macro defs
   print $c_fh "\n";
 
-  # indent is still undef. Until anyone implents indent style rules with it.
+  # indent is still undef. Until anyone implements indent style rules with it.
   foreach (C_constant ($ARGS{NAME}, $ARGS{C_SUBNAME}, $ARGS{DEFAULT_TYPE},
                        $types, undef, $ARGS{BREAKOUT_AT}, @{$ARGS{NAMES}})) {
     print $c_fh $_, "\n"; # C constant subs
@@ -1073,6 +1437,28 @@ sub WriteConstants {
   close $xs_fh or warn "Error closing $ARGS{XS_FILE}: $!";
 }
 
+package ExtUtils::Constant::Aaargh56Hash;
+# A support module (hack) to provide sane Unicode hash keys on 5.6.x perl
+use strict;
+require Tie::Hash if $ExtUtils::Constant::is_perl56;
+use vars '@ISA';
+@ISA = 'Tie::StdHash';
+
+#my $a;
+# Storing the values as concatenated BER encoded numbers is actually going to
+# be terser than using UTF8 :-)
+# And the tests are slightly faster. Ops are bad, m'kay
+sub to_key {pack "w*", unpack "U*", ($_[0] . pack "U*")};
+sub from_key {defined $_[0] ? pack "U*", unpack 'w*', $_[0] : undef};
+
+sub STORE    { $_[0]->{to_key($_[1])} = $_[2] }
+sub FETCH    { $_[0]->{to_key($_[1])} }
+sub FIRSTKEY { my $a = scalar keys %{$_[0]}; from_key (each %{$_[0]}) }
+sub NEXTKEY  { from_key (each %{$_[0]}) }
+sub EXISTS   { exists $_[0]->{to_key($_[1])} }
+sub DELETE   { delete $_[0]->{to_key($_[1])} }
+
+#END {warn "$a accesses";}
 1;
 __END__