OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [java/] [mangle_name.c] - Diff between revs 816 and 826

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* Shared functions related to mangling names for the GNU compiler
/* Shared functions related to mangling names for the GNU compiler
   for the Java(TM) language.
   for the Java(TM) language.
   Copyright (C) 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
   Copyright (C) 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful,
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
 
 
Java and all Java-based marks are trademarks or registered trademarks
Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */
The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 
 
/* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
/* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "tm.h"
#include "tm.h"
#include "jcf.h"
#include "jcf.h"
#include "tree.h"
#include "tree.h"
#include "java-tree.h"
#include "java-tree.h"
#include "obstack.h"
#include "obstack.h"
#include "toplev.h"
#include "toplev.h"
 
 
static void append_unicode_mangled_name (const char *, int);
static void append_unicode_mangled_name (const char *, int);
#ifndef HAVE_AS_UTF8
#ifndef HAVE_AS_UTF8
static int  unicode_mangling_length (const char *, int);
static int  unicode_mangling_length (const char *, int);
#endif
#endif
 
 
extern struct obstack *mangle_obstack;
extern struct obstack *mangle_obstack;
 
 
static int
static int
utf8_cmp (const unsigned char *str, int length, const char *name)
utf8_cmp (const unsigned char *str, int length, const char *name)
{
{
  const unsigned char *limit = str + length;
  const unsigned char *limit = str + length;
  int i;
  int i;
 
 
  for (i = 0; name[i]; ++i)
  for (i = 0; name[i]; ++i)
    {
    {
      int ch = UTF8_GET (str, limit);
      int ch = UTF8_GET (str, limit);
      if (ch != name[i])
      if (ch != name[i])
        return ch - name[i];
        return ch - name[i];
    }
    }
 
 
  return str == limit ? 0 : 1;
  return str == limit ? 0 : 1;
}
}
 
 
/* A sorted list of all C++ keywords.  If you change this, be sure
/* A sorted list of all C++ keywords.  If you change this, be sure
   also to change the list in
   also to change the list in
   libjava/classpath/tools/gnu/classpath/tools/javah/Keywords.java.  */
   libjava/classpath/tools/gnu/classpath/tools/javah/Keywords.java.  */
static const char *const cxx_keywords[] =
static const char *const cxx_keywords[] =
{
{
  "_Complex",
  "_Complex",
  "__alignof",
  "__alignof",
  "__alignof__",
  "__alignof__",
  "__asm",
  "__asm",
  "__asm__",
  "__asm__",
  "__attribute",
  "__attribute",
  "__attribute__",
  "__attribute__",
  "__builtin_va_arg",
  "__builtin_va_arg",
  "__complex",
  "__complex",
  "__complex__",
  "__complex__",
  "__const",
  "__const",
  "__const__",
  "__const__",
  "__extension__",
  "__extension__",
  "__imag",
  "__imag",
  "__imag__",
  "__imag__",
  "__inline",
  "__inline",
  "__inline__",
  "__inline__",
  "__label__",
  "__label__",
  "__null",
  "__null",
  "__real",
  "__real",
  "__real__",
  "__real__",
  "__restrict",
  "__restrict",
  "__restrict__",
  "__restrict__",
  "__signed",
  "__signed",
  "__signed__",
  "__signed__",
  "__typeof",
  "__typeof",
  "__typeof__",
  "__typeof__",
  "__volatile",
  "__volatile",
  "__volatile__",
  "__volatile__",
  "and",
  "and",
  "and_eq",
  "and_eq",
  "asm",
  "asm",
  "auto",
  "auto",
  "bitand",
  "bitand",
  "bitor",
  "bitor",
  "bool",
  "bool",
  "break",
  "break",
  "case",
  "case",
  "catch",
  "catch",
  "char",
  "char",
  "class",
  "class",
  "compl",
  "compl",
  "const",
  "const",
  "const_cast",
  "const_cast",
  "continue",
  "continue",
  "default",
  "default",
  "delete",
  "delete",
  "do",
  "do",
  "double",
  "double",
  "dynamic_cast",
  "dynamic_cast",
  "else",
  "else",
  "enum",
  "enum",
  "explicit",
  "explicit",
  "export",
  "export",
  "extern",
  "extern",
  "false",
  "false",
  "float",
  "float",
  "for",
  "for",
  "friend",
  "friend",
  "goto",
  "goto",
  "if",
  "if",
  "inline",
  "inline",
  "int",
  "int",
  "long",
  "long",
  "mutable",
  "mutable",
  "namespace",
  "namespace",
  "new",
  "new",
  "not",
  "not",
  "not_eq",
  "not_eq",
  "operator",
  "operator",
  "or",
  "or",
  "or_eq",
  "or_eq",
  "private",
  "private",
  "protected",
  "protected",
  "public",
  "public",
  "register",
  "register",
  "reinterpret_cast",
  "reinterpret_cast",
  "return",
  "return",
  "short",
  "short",
  "signed",
  "signed",
  "sizeof",
  "sizeof",
  "static",
  "static",
  "static_cast",
  "static_cast",
  "struct",
  "struct",
  "switch",
  "switch",
  "template",
  "template",
  "this",
  "this",
  "throw",
  "throw",
  "true",
  "true",
  "try",
  "try",
  "typedef",
  "typedef",
  "typeid",
  "typeid",
  "typename",
  "typename",
  "typeof",
  "typeof",
  "union",
  "union",
  "unsigned",
  "unsigned",
  "using",
  "using",
  "virtual",
  "virtual",
  "void",
  "void",
  "volatile",
  "volatile",
  "wchar_t",
  "wchar_t",
  "while",
  "while",
  "xor",
  "xor",
  "xor_eq"
  "xor_eq"
};
};
 
 
/* Return true if NAME is a C++ keyword.  */
/* Return true if NAME is a C++ keyword.  */
int
int
cxx_keyword_p (const char *name, int length)
cxx_keyword_p (const char *name, int length)
{
{
  int last = ARRAY_SIZE (cxx_keywords);
  int last = ARRAY_SIZE (cxx_keywords);
  int first = 0;
  int first = 0;
  int mid = (last + first) / 2;
  int mid = (last + first) / 2;
  int old = -1;
  int old = -1;
 
 
  for (mid = (last + first) / 2;
  for (mid = (last + first) / 2;
       mid != old;
       mid != old;
       old = mid, mid = (last + first) / 2)
       old = mid, mid = (last + first) / 2)
    {
    {
      int kwl = strlen (cxx_keywords[mid]);
      int kwl = strlen (cxx_keywords[mid]);
      int min_length = kwl > length ? length : kwl;
      int min_length = kwl > length ? length : kwl;
      int r = utf8_cmp ((const unsigned char *) name, min_length, cxx_keywords[mid]);
      int r = utf8_cmp ((const unsigned char *) name, min_length, cxx_keywords[mid]);
 
 
      if (r == 0)
      if (r == 0)
        {
        {
          int i;
          int i;
          /* We've found a match if all the remaining characters are `$'.  */
          /* We've found a match if all the remaining characters are `$'.  */
          for (i = min_length; i < length && name[i] == '$'; ++i)
          for (i = min_length; i < length && name[i] == '$'; ++i)
            ;
            ;
          if (i == length)
          if (i == length)
            return 1;
            return 1;
          r = 1;
          r = 1;
        }
        }
 
 
      if (r < 0)
      if (r < 0)
        last = mid;
        last = mid;
      else
      else
        first = mid;
        first = mid;
    }
    }
  return 0;
  return 0;
}
}
 
 
/* If NAME happens to be a C++ keyword, add `$'.  */
/* If NAME happens to be a C++ keyword, add `$'.  */
#define MANGLE_CXX_KEYWORDS(NAME, LEN)                  \
#define MANGLE_CXX_KEYWORDS(NAME, LEN)                  \
do                                                      \
do                                                      \
  {                                                     \
  {                                                     \
    if (cxx_keyword_p ((NAME), (LEN)))                  \
    if (cxx_keyword_p ((NAME), (LEN)))                  \
      {                                                 \
      {                                                 \
        char *tmp_buf = (char *)alloca ((LEN)+1);       \
        char *tmp_buf = (char *)alloca ((LEN)+1);       \
        memcpy (tmp_buf, (NAME), (LEN));                \
        memcpy (tmp_buf, (NAME), (LEN));                \
        tmp_buf[LEN]= '$';                              \
        tmp_buf[LEN]= '$';                              \
        (NAME) = tmp_buf;                               \
        (NAME) = tmp_buf;                               \
        (LEN)++;                                        \
        (LEN)++;                                        \
      }                                                 \
      }                                                 \
  }                                                     \
  }                                                     \
while (0)
while (0)
 
 
 
 
/* If the assembler doesn't support UTF8 in symbol names, some
/* If the assembler doesn't support UTF8 in symbol names, some
   characters might need to be escaped.  */
   characters might need to be escaped.  */
 
 
#ifndef HAVE_AS_UTF8
#ifndef HAVE_AS_UTF8
 
 
/* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
/* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
   appropriately mangled (with Unicode escapes if needed) to
   appropriately mangled (with Unicode escapes if needed) to
   MANGLE_OBSTACK.  Note that `java', `lang' and `Object' are used so
   MANGLE_OBSTACK.  Note that `java', `lang' and `Object' are used so
   frequently that they could be cached.  */
   frequently that they could be cached.  */
 
 
void
void
append_gpp_mangled_name (const char *name, int len)
append_gpp_mangled_name (const char *name, int len)
{
{
  int encoded_len, needs_escapes;
  int encoded_len, needs_escapes;
  char buf[6];
  char buf[6];
 
 
  MANGLE_CXX_KEYWORDS (name, len);
  MANGLE_CXX_KEYWORDS (name, len);
 
 
  encoded_len = unicode_mangling_length (name, len);
  encoded_len = unicode_mangling_length (name, len);
  needs_escapes = encoded_len > 0;
  needs_escapes = encoded_len > 0;
 
 
  sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
  sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
  obstack_grow (mangle_obstack, buf, strlen (buf));
  obstack_grow (mangle_obstack, buf, strlen (buf));
 
 
  if (needs_escapes)
  if (needs_escapes)
    append_unicode_mangled_name (name, len);
    append_unicode_mangled_name (name, len);
  else
  else
    obstack_grow (mangle_obstack, name, len);
    obstack_grow (mangle_obstack, name, len);
}
}
 
 
/* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
/* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
   appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
   appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
   Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
   Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
   which case `__U' will be mangled `__U_'.  */
   which case `__U' will be mangled `__U_'.  */
 
 
static void
static void
append_unicode_mangled_name (const char *name, int len)
append_unicode_mangled_name (const char *name, int len)
{
{
  const unsigned char *ptr;
  const unsigned char *ptr;
  const unsigned char *limit = (const unsigned char *)name + len;
  const unsigned char *limit = (const unsigned char *)name + len;
  int uuU = 0;
  int uuU = 0;
  for (ptr = (const unsigned char *) name;  ptr < limit;  )
  for (ptr = (const unsigned char *) name;  ptr < limit;  )
    {
    {
      int ch = UTF8_GET(ptr, limit);
      int ch = UTF8_GET(ptr, limit);
 
 
      if ((ISALNUM (ch) && ch != 'U') || ch == '$')
      if ((ISALNUM (ch) && ch != 'U') || ch == '$')
        {
        {
          obstack_1grow (mangle_obstack, ch);
          obstack_1grow (mangle_obstack, ch);
          uuU = 0;
          uuU = 0;
        }
        }
      /* Everything else needs encoding */
      /* Everything else needs encoding */
      else
      else
        {
        {
          char buf [9];
          char buf [9];
          if (ch == '_' || ch == 'U')
          if (ch == '_' || ch == 'U')
            {
            {
              /* Prepare to recognize __U */
              /* Prepare to recognize __U */
              if (ch == '_' && (uuU < 3))
              if (ch == '_' && (uuU < 3))
                {
                {
                  uuU++;
                  uuU++;
                  obstack_1grow (mangle_obstack, ch);
                  obstack_1grow (mangle_obstack, ch);
                }
                }
              /* We recognize __U that we wish to encode
              /* We recognize __U that we wish to encode
                 __U_. Finish the encoding. */
                 __U_. Finish the encoding. */
              else if (ch == 'U' && (uuU == 2))
              else if (ch == 'U' && (uuU == 2))
                {
                {
                  uuU = 0;
                  uuU = 0;
                  obstack_grow (mangle_obstack, "U_", 2);
                  obstack_grow (mangle_obstack, "U_", 2);
                }
                }
              /* Otherwise, just reset uuU and emit the character we
              /* Otherwise, just reset uuU and emit the character we
                 have. */
                 have. */
              else
              else
                {
                {
                  uuU = 0;
                  uuU = 0;
                  obstack_1grow (mangle_obstack, ch);
                  obstack_1grow (mangle_obstack, ch);
                }
                }
              continue;
              continue;
            }
            }
          sprintf (buf, "__U%x_", ch);
          sprintf (buf, "__U%x_", ch);
          obstack_grow (mangle_obstack, buf, strlen (buf));
          obstack_grow (mangle_obstack, buf, strlen (buf));
          uuU = 0;
          uuU = 0;
        }
        }
    }
    }
}
}
 
 
/* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
/* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
   length of the string as mangled (a la g++) including Unicode
   length of the string as mangled (a la g++) including Unicode
   escapes.  If no escapes are needed, return 0.  */
   escapes.  If no escapes are needed, return 0.  */
 
 
static int
static int
unicode_mangling_length (const char *name, int len)
unicode_mangling_length (const char *name, int len)
{
{
  const unsigned char *ptr;
  const unsigned char *ptr;
  const unsigned char *limit = (const unsigned char *)name + len;
  const unsigned char *limit = (const unsigned char *)name + len;
  int need_escapes = 0;          /* Whether we need an escape or not */
  int need_escapes = 0;          /* Whether we need an escape or not */
  int num_chars = 0;             /* Number of characters in the mangled name */
  int num_chars = 0;             /* Number of characters in the mangled name */
  int uuU = 0;                   /* Help us to find __U. 0: '_', 1: '__' */
  int uuU = 0;                   /* Help us to find __U. 0: '_', 1: '__' */
  for (ptr = (const unsigned char *) name;  ptr < limit;  )
  for (ptr = (const unsigned char *) name;  ptr < limit;  )
    {
    {
      int ch = UTF8_GET(ptr, limit);
      int ch = UTF8_GET(ptr, limit);
 
 
      if (ch < 0)
      if (ch < 0)
        error ("internal error - invalid Utf8 name");
        error ("internal error - invalid Utf8 name");
      if ((ISALNUM (ch) && ch != 'U') || ch == '$')
      if ((ISALNUM (ch) && ch != 'U') || ch == '$')
        {
        {
          num_chars++;
          num_chars++;
          uuU = 0;
          uuU = 0;
        }
        }
      /* Everything else needs encoding */
      /* Everything else needs encoding */
      else
      else
        {
        {
          int encoding_length = 2;
          int encoding_length = 2;
 
 
          if (ch == '_' || ch == 'U')
          if (ch == '_' || ch == 'U')
            {
            {
              /* It's always at least one character. */
              /* It's always at least one character. */
              num_chars++;
              num_chars++;
 
 
              /* Prepare to recognize __U */
              /* Prepare to recognize __U */
              if (ch == '_' && (uuU < 3))
              if (ch == '_' && (uuU < 3))
                uuU++;
                uuU++;
 
 
              /* We recognize __U that we wish to encode __U_, we
              /* We recognize __U that we wish to encode __U_, we
                 count one more character. */
                 count one more character. */
              else if (ch == 'U' && (uuU == 2))
              else if (ch == 'U' && (uuU == 2))
                {
                {
                  num_chars++;
                  num_chars++;
                  need_escapes = 1;
                  need_escapes = 1;
                  uuU = 0;
                  uuU = 0;
                }
                }
              /* Otherwise, just reset uuU */
              /* Otherwise, just reset uuU */
              else
              else
                uuU = 0;
                uuU = 0;
 
 
              continue;
              continue;
            }
            }
 
 
          if (ch > 0xff)
          if (ch > 0xff)
            encoding_length++;
            encoding_length++;
          if (ch > 0xfff)
          if (ch > 0xfff)
            encoding_length++;
            encoding_length++;
 
 
          num_chars += (4 + encoding_length);
          num_chars += (4 + encoding_length);
          need_escapes = 1;
          need_escapes = 1;
          uuU = 0;
          uuU = 0;
        }
        }
    }
    }
  if (need_escapes)
  if (need_escapes)
    return num_chars;
    return num_chars;
  else
  else
    return 0;
    return 0;
}
}
 
 
#else
#else
 
 
/* The assembler supports UTF8, we don't use escapes. Mangling is
/* The assembler supports UTF8, we don't use escapes. Mangling is
   simply <N>NAME. <N> is the number of UTF8 encoded characters that
   simply <N>NAME. <N> is the number of UTF8 encoded characters that
   are found in NAME. Note that `java', `lang' and `Object' are used
   are found in NAME. Note that `java', `lang' and `Object' are used
   so frequently that they could be cached.  */
   so frequently that they could be cached.  */
 
 
void
void
append_gpp_mangled_name (const char *name, int len)
append_gpp_mangled_name (const char *name, int len)
{
{
  const unsigned char *ptr;
  const unsigned char *ptr;
  const unsigned char *limit;
  const unsigned char *limit;
  int encoded_len;
  int encoded_len;
  char buf [6];
  char buf [6];
 
 
  MANGLE_CXX_KEYWORDS (name, len);
  MANGLE_CXX_KEYWORDS (name, len);
 
 
  limit = (const unsigned char *)name + len;
  limit = (const unsigned char *)name + len;
 
 
  /* Compute the length of the string we wish to mangle. */
  /* Compute the length of the string we wish to mangle. */
  for (encoded_len =  0, ptr = (const unsigned char *) name;
  for (encoded_len =  0, ptr = (const unsigned char *) name;
       ptr < limit; encoded_len++)
       ptr < limit; encoded_len++)
    {
    {
      int ch = UTF8_GET(ptr, limit);
      int ch = UTF8_GET(ptr, limit);
 
 
      if (ch < 0)
      if (ch < 0)
        error ("internal error - invalid Utf8 name");
        error ("internal error - invalid Utf8 name");
    }
    }
 
 
  sprintf (buf, "%d", encoded_len);
  sprintf (buf, "%d", encoded_len);
  obstack_grow (mangle_obstack, buf, strlen (buf));
  obstack_grow (mangle_obstack, buf, strlen (buf));
  obstack_grow (mangle_obstack, name, len);
  obstack_grow (mangle_obstack, name, len);
}
}
 
 
#endif /* HAVE_AS_UTF8 */
#endif /* HAVE_AS_UTF8 */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.