OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / read-md.c
index 07882aa..4f1933f 100644 (file)
@@ -34,6 +34,12 @@ struct ptr_loc {
   int lineno;
 };
 
+/* A singly-linked list of filenames.  */
+struct file_name_list {
+  struct file_name_list *next;
+  const char *fname;
+};
+
 /* Obstack used for allocating MD strings.  */
 struct obstack string_obstack;
 
@@ -62,10 +68,35 @@ const char *read_md_filename;
 /* The current line number in READ_MD_FILE.  */
 int read_md_lineno;
 
+/* The name of the toplevel file that indirectly included READ_MD_FILE.  */
+const char *in_fname;
+
+/* The directory part of IN_FNAME.  NULL if IN_FNAME is a bare filename.  */
+static char *base_dir;
+
+/* The first directory to search.  */
+static struct file_name_list *first_dir_md_include;
+
+/* A pointer to the null terminator of the md include chain.  */
+static struct file_name_list **last_dir_md_include_ptr = &first_dir_md_include;
+
+/* This callback will be invoked whenever an md include directive is
+   processed.  To be used for creation of the dependency file.  */
+void (*include_callback) (const char *);
+
+/* The current maximum length of directory names in the search path
+   for include files.  (Altered as we get more of them.)  */
+static size_t max_include_len;
+
 /* A table of md_constant structures, hashed by name.  Null if no
    constant expansion should occur.  */
 static htab_t md_constants;
 
+/* A table of enum_type structures, hashed by name.  */
+static htab_t enum_types;
+
+static void handle_file (directive_handler_t);
+
 /* Given an object that starts with a char * name field, return a hash
    code for its name.  */
 
@@ -259,7 +290,10 @@ fatal_with_file_and_line (const char *msg, ...)
       if (c == EOF)
        break;
       if (c == '\r' || c == '\n')
-       break;
+       {
+         unread_char (c);
+         break;
+       }
       context[i] = c;
     }
   context[i] = '\0';
@@ -298,18 +332,13 @@ read_skip_spaces (void)
       c = read_char ();
       switch (c)
        {
-       case '\n':
-         read_md_lineno++;
-         break;
-
-       case ' ': case '\t': case '\f': case '\r':
+       case ' ': case '\t': case '\f': case '\r': case '\n':
          break;
 
        case ';':
          do
            c = read_char ();
          while (c != '\n' && c != EOF);
-         read_md_lineno++;
          break;
 
        case '/':
@@ -317,14 +346,15 @@ read_skip_spaces (void)
            int prevc;
            c = read_char ();
            if (c != '*')
-             fatal_expected_char ('*', c);
+             {
+               unread_char (c);
+               fatal_with_file_and_line ("stray '/' in file");
+             }
 
            prevc = 0;
            while ((c = read_char ()) && c != EOF)
              {
-               if (c == '\n')
-                  read_md_lineno++;
-               else if (prevc == '*' && c == '/')
+               if (prevc == '*' && c == '/')
                  break;
                prevc = c;
              }
@@ -370,8 +400,6 @@ read_name (struct md_name *name)
 
   if (i == 0)
     fatal_with_file_and_line ("missing name or number");
-  if (c == '\n')
-    read_md_lineno++;
 
   name->buffer[i] = 0;
   name->string = name->buffer;
@@ -406,7 +434,6 @@ read_escape (void)
     {
       /* Backslash-newline is replaced by nothing, as in C.  */
     case '\n':
-      read_md_lineno++;
       return;
 
       /* \" \' \\ are replaced by the second character.  */
@@ -458,9 +485,7 @@ read_quoted_string (void)
   while (1)
     {
       c = read_char (); /* Read the string  */
-      if (c == '\n')
-       read_md_lineno++;
-      else if (c == '\\')
+      if (c == '\\')
        {
          read_escape ();
          continue;
@@ -491,9 +516,7 @@ read_braced_string (void)
     {
       c = read_char (); /* Read the string  */
 
-      if (c == '\n')
-       read_md_lineno++;
-      else if (c == '{')
+      if (c == '{')
        brace_depth++;
       else if (c == '}')
        brace_depth--;
@@ -555,6 +578,55 @@ read_string (int star_if_braced)
   return stringbuf;
 }
 
+/* Skip the rest of a construct that started at line LINENO and that
+   is currently nested by DEPTH levels of parentheses.  */
+
+void
+read_skip_construct (int depth, int lineno)
+{
+  struct md_name name;
+  int c;
+
+  do
+    {
+      c = read_skip_spaces ();
+      if (c == EOF)
+       {
+         error_with_line (lineno, "unterminated construct");
+         exit (1);
+       }
+      switch (c)
+       {
+       case '(':
+         depth++;
+         break;
+
+       case ')':
+         depth--;
+         break;
+
+       case ':':
+       case '[':
+       case ']':
+       case '/':
+         break;
+
+       case '\"':
+       case '{':
+         unread_char (c);
+         read_string (false);
+         break;
+
+       default:
+         unread_char (c);
+         read_name (&name);
+         break;
+       }
+    }
+  while (depth > 0);
+  unread_char (c);
+}
+
 /* Given a string, return the number of comma-separated elements in it.
    Return 0 for the null string.  */
 
@@ -602,63 +674,84 @@ scan_comma_elt (const char **pstr)
   return start;
 }
 
+/* Convert STRING to uppercase.  */
+
+void
+upcase_string (char *string)
+{
+  int i;
+
+  for (i = 0; string[i]; i++)
+    string[i] = TOUPPER (string[i]);
+}
+
+/* Add a NAME = VALUE definition to md_constants-style hash table DEFS,
+   where both NAME and VALUE are malloc()ed strings.  PARENT_ENUM is the
+   enum to which NAME belongs, or null if NAME is a stand-alone constant.  */
+
+static struct md_constant *
+add_constant (htab_t defs, char *name, char *value,
+             struct enum_type *parent_enum)
+{
+  struct md_constant *def, tmp_def;
+  void **entry_ptr;
+
+  tmp_def.name = name;
+  entry_ptr = htab_find_slot (defs, &tmp_def, INSERT);
+  if (*entry_ptr)
+    {
+      def = (struct md_constant *) *entry_ptr;
+      if (strcmp (def->value, value) != 0)
+       fatal_with_file_and_line ("redefinition of `%s', was `%s', now `%s'",
+                                 def->name, def->value, value);
+      else if (parent_enum || def->parent_enum)
+       fatal_with_file_and_line ("redefinition of `%s'", def->name);
+      free (name);
+      free (value);
+    }
+  else
+    {
+      def = XNEW (struct md_constant);
+      def->name = name;
+      def->value = value;
+      def->parent_enum = parent_enum;
+      *entry_ptr = def;
+    }
+  return def;
+}
+
 /* Process a define_constants directive, starting with the optional space
    after the "define_constants".  */
 
-void
-read_constants (void)
+static void
+handle_constants (void)
 {
   int c;
   htab_t defs;
 
-  defs = md_constants;
-  if (! defs)
-    defs = htab_create (32, leading_string_hash,
-                       leading_string_eq_p, (htab_del) 0);
-
   c = read_skip_spaces ();
   if (c != '[')
     fatal_expected_char ('[', c);
 
   /* Disable constant expansion during definition processing.  */
+  defs = md_constants;
   md_constants = 0;
   while ( (c = read_skip_spaces ()) != ']')
     {
       struct md_name name, value;
-      struct md_constant *def, tmp_def;
-      void **entry_ptr;
 
       if (c != '(')
        fatal_expected_char ('(', c);
 
       read_name (&name);
       read_name (&value);
-
-      tmp_def.name = name.string;
-      entry_ptr = htab_find_slot (defs, &tmp_def, INSERT);
-      if (*entry_ptr)
-       {
-         def = (struct md_constant *) *entry_ptr;
-         if (strcmp (def->value, value.string) != 0)
-           fatal_with_file_and_line ("redefinition of %s, was %s, now %s",
-                                     def->name, def->value, value.string);
-       }
-      else
-       {
-         def = XNEW (struct md_constant);
-         def->name = xstrdup (name.string);
-         def->value = xstrdup (value.string);
-         *entry_ptr = def;
-       }
+      add_constant (defs, xstrdup (name.string), xstrdup (value.string), 0);
 
       c = read_skip_spaces ();
       if (c != ')')
        fatal_expected_char (')', c);
     }
   md_constants = defs;
-  c = read_skip_spaces ();
-  if (c != ')')
-    fatal_expected_char (')', c);
 }
 
 /* For every constant definition, call CALLBACK with two arguments:
@@ -668,18 +761,379 @@ read_constants (void)
 void
 traverse_md_constants (htab_trav callback, void *info)
 {
-  if (md_constants)
-    htab_traverse (md_constants, callback, info);
+  htab_traverse (md_constants, callback, info);
+}
+
+/* Return a malloc()ed decimal string that represents number NUMBER.  */
+
+static char *
+decimal_string (int number)
+{
+  /* A safe overestimate.  +1 for sign, +1 for null terminator.  */
+  char buffer[sizeof (int) * CHAR_BIT + 1 + 1];
+
+  sprintf (buffer, "%d", number);
+  return xstrdup (buffer);
+}
+
+/* Process a define_enum or define_c_enum directive, starting with
+   the optional space after the "define_enum".  LINENO is the line
+   number on which the directive started and MD_P is true if the
+   directive is a define_enum rather than a define_c_enum.  */
+
+static void
+handle_enum (int lineno, bool md_p)
+{
+  char *enum_name, *value_name;
+  struct md_name name;
+  struct enum_type *def;
+  struct enum_value *ev;
+  void **slot;
+  int c;
+
+  enum_name = read_string (false);
+  slot = htab_find_slot (enum_types, &enum_name, INSERT);
+  if (*slot)
+    {
+      def = (struct enum_type *) *slot;
+      if (def->md_p != md_p)
+       error_with_line (lineno, "redefining `%s' as a different type of enum",
+                        enum_name);
+    }
+  else
+    {
+      def = XNEW (struct enum_type);
+      def->name = enum_name;
+      def->md_p = md_p;
+      def->values = 0;
+      def->tail_ptr = &def->values;
+      def->num_values = 0;
+      *slot = def;
+    }
+
+  c = read_skip_spaces ();
+  if (c != '[')
+    fatal_expected_char ('[', c);
+
+  while ((c = read_skip_spaces ()) != ']')
+    {
+      if (c == EOF)
+       {
+         error_with_line (lineno, "unterminated construct");
+         exit (1);
+       }
+      unread_char (c);
+      read_name (&name);
+
+      ev = XNEW (struct enum_value);
+      ev->next = 0;
+      if (md_p)
+       {
+         value_name = concat (def->name, "_", name.string, NULL);
+         upcase_string (value_name);
+         ev->name = xstrdup (name.string);
+       }
+      else
+       {
+         value_name = xstrdup (name.string);
+         ev->name = value_name;
+       }
+      ev->def = add_constant (md_constants, value_name,
+                             decimal_string (def->num_values), def);
+
+      *def->tail_ptr = ev;
+      def->tail_ptr = &ev->next;
+      def->num_values++;
+    }
+}
+
+/* Try to find the definition of the given enum.  Return null on failure.  */
+
+struct enum_type *
+lookup_enum_type (const char *name)
+{
+  return (struct enum_type *) htab_find (enum_types, &name);
 }
 
-/* Initialize this file's static data.  */
+/* For every enum definition, call CALLBACK with two arguments:
+   a pointer to the constant definition and INFO.  Stop when CALLBACK
+   returns zero.  */
 
 void
-init_md_reader (void)
+traverse_enum_types (htab_trav callback, void *info)
+{
+  htab_traverse (enum_types, callback, info);
+}
+
+/* Process an "include" directive, starting with the optional space
+   after the "include".  Read in the file and use HANDLE_DIRECTIVE
+   to process each unknown directive.  LINENO is the line number on
+   which the "include" occured.  */
+
+static void
+handle_include (int lineno, directive_handler_t handle_directive)
+{
+  const char *filename;
+  const char *old_filename;
+  int old_lineno;
+  char *pathname;
+  FILE *input_file, *old_file;
+
+  filename = read_string (false);
+  input_file = NULL;
+
+  /* If the specified file name is absolute, skip the include stack.  */
+  if (!IS_ABSOLUTE_PATH (filename))
+    {
+      struct file_name_list *stackp;
+
+      /* Search the directory path, trying to open the file.  */
+      for (stackp = first_dir_md_include; stackp; stackp = stackp->next)
+       {
+         static const char sep[2] = { DIR_SEPARATOR, '\0' };
+
+         pathname = concat (stackp->fname, sep, filename, NULL);
+         input_file = fopen (pathname, "r");
+         if (input_file != NULL)
+           break;
+         free (pathname);
+       }
+    }
+
+  /* If we haven't managed to open the file yet, try combining the
+     filename with BASE_DIR.  */
+  if (input_file == NULL)
+    {
+      if (base_dir)
+       pathname = concat (base_dir, filename, NULL);
+      else
+       pathname = xstrdup (filename);
+      input_file = fopen (pathname, "r");
+    }
+
+  if (input_file == NULL)
+    {
+      free (pathname);
+      error_with_line (lineno, "include file `%s' not found", filename);
+      return;
+    }
+
+  /* Save the old cursor.  Note that the LINENO argument to this
+     function is the beginning of the include statement, while
+     read_md_lineno has already been advanced.  */
+  old_file = read_md_file;
+  old_filename = read_md_filename;
+  old_lineno = read_md_lineno;
+
+  if (include_callback)
+    include_callback (pathname);
+
+  read_md_file = input_file;
+  read_md_filename = pathname;
+  handle_file (handle_directive);
+
+  /* Restore the old cursor.  */
+  read_md_file = old_file;
+  read_md_filename = old_filename;
+  read_md_lineno = old_lineno;
+
+  /* Do not free the pathname.  It is attached to the various rtx
+     queue elements.  */
+}
+
+/* Process the current file, assuming that read_md_file and
+   read_md_filename are valid.  Use HANDLE_DIRECTIVE to handle
+   unknown directives.  */
+
+static void
+handle_file (directive_handler_t handle_directive)
+{
+  struct md_name directive;
+  int c, lineno;
+
+  read_md_lineno = 1;
+  while ((c = read_skip_spaces ()) != EOF)
+    {
+      lineno = read_md_lineno;
+      if (c != '(')
+       fatal_expected_char ('(', c);
+
+      read_name (&directive);
+      if (strcmp (directive.string, "define_constants") == 0)
+       handle_constants ();
+      else if (strcmp (directive.string, "define_enum") == 0)
+       handle_enum (lineno, true);
+      else if (strcmp (directive.string, "define_c_enum") == 0)
+       handle_enum (lineno, false);
+      else if (strcmp (directive.string, "include") == 0)
+       handle_include (lineno, handle_directive);
+      else if (handle_directive)
+       handle_directive (lineno, directive.string);
+      else
+       read_skip_construct (1, lineno);
+
+      c = read_skip_spaces ();
+      if (c != ')')
+       fatal_expected_char (')', c);
+    }
+  fclose (read_md_file);
+}
+
+/* Like handle_file, but for top-level files.  Set up in_fname and
+   base_dir accordingly.  */
+
+static void
+handle_toplevel_file (directive_handler_t handle_directive)
 {
+  const char *base;
+
+  in_fname = read_md_filename;
+  base = lbasename (in_fname);
+  if (base == in_fname)
+    base_dir = NULL;
+  else
+    base_dir = xstrndup (in_fname, base - in_fname);
+
+  handle_file (handle_directive);
+}
+
+/* Parse a -I option with argument ARG.  */
+
+static void
+parse_include (const char *arg)
+{
+  struct file_name_list *dirtmp;
+
+  dirtmp = XNEW (struct file_name_list);
+  dirtmp->next = 0;
+  dirtmp->fname = arg;
+  *last_dir_md_include_ptr = dirtmp;
+  last_dir_md_include_ptr = &dirtmp->next;
+  if (strlen (dirtmp->fname) > max_include_len)
+    max_include_len = strlen (dirtmp->fname);
+}
+
+/* The main routine for reading .md files.  Try to process all the .md
+   files specified on the command line and return true if no error occured.
+
+   ARGC and ARGV are the arguments to main.
+
+   PARSE_OPT, if nonnull, is passed all unknown command-line arguments.
+   It should return true if it recognizes the argument or false if a
+   generic error should be reported.
+
+   If HANDLE_DIRECTIVE is nonnull, the parser calls it for each
+   unknown directive, otherwise it just skips such directives.
+   See the comment above the directive_handler_t definition for
+   details about the callback's interface.  */
+
+bool
+read_md_files (int argc, char **argv, bool (*parse_opt) (const char *),
+              directive_handler_t handle_directive)
+{
+  int i;
+  bool no_more_options;
+  bool already_read_stdin;
+  int num_files;
+
+  /* Initialize global data.  */
   obstack_init (&string_obstack);
   ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
   obstack_init (&ptr_loc_obstack);
   joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
   obstack_init (&joined_conditions_obstack);
+  md_constants = htab_create (31, leading_string_hash,
+                             leading_string_eq_p, (htab_del) 0);
+  enum_types = htab_create (31, leading_string_hash,
+                           leading_string_eq_p, (htab_del) 0);
+
+  /* Unlock the stdio streams.  */
+  unlock_std_streams ();
+
+  /* First we loop over all the options.  */
+  for (i = 1; i < argc; i++)
+    if (argv[i][0] == '-')
+      {
+       /* An argument consisting of exactly one dash is a request to
+          read stdin.  This will be handled in the second loop.  */
+       if (argv[i][1] == '\0')
+         continue;
+
+       /* An argument consisting of just two dashes causes option
+          parsing to cease.  */
+       if (argv[i][1] == '-' && argv[i][2] == '\0')
+         break;
+
+       if (argv[i][1] == 'I')
+         {
+           if (argv[i][2] != '\0')
+             parse_include (argv[i] + 2);
+           else if (++i < argc)
+             parse_include (argv[i]);
+           else
+             fatal ("directory name missing after -I option");
+           continue;
+         }
+
+       /* The program may have provided a callback so it can
+          accept its own options.  */
+       if (parse_opt && parse_opt (argv[i]))
+         continue;
+
+       fatal ("invalid option `%s'", argv[i]);
+      }
+
+  /* Now loop over all input files.  */
+  num_files = 0;
+  no_more_options = false;
+  already_read_stdin = false;
+  for (i = 1; i < argc; i++)
+    {
+      if (argv[i][0] == '-')
+       {
+         if (argv[i][1] == '\0')
+           {
+             /* Read stdin.  */
+             if (already_read_stdin)
+               fatal ("cannot read standard input twice");
+
+             read_md_file = stdin;
+             read_md_filename = "<stdin>";
+             handle_toplevel_file (handle_directive);
+             already_read_stdin = true;
+             continue;
+           }
+         else if (argv[i][1] == '-' && argv[i][2] == '\0')
+           {
+             /* No further arguments are to be treated as options.  */
+             no_more_options = true;
+             continue;
+           }
+         else if (!no_more_options)
+           continue;
+       }
+
+      /* If we get here we are looking at a non-option argument, i.e.
+        a file to be processed.  */
+      read_md_filename = argv[i];
+      read_md_file = fopen (read_md_filename, "r");
+      if (read_md_file == 0)
+       {
+         perror (read_md_filename);
+         return false;
+       }
+      handle_toplevel_file (handle_directive);
+      num_files++;
+    }
+
+  /* If we get to this point without having seen any files to process,
+     read the standard input now.  */
+  if (num_files == 0 && !already_read_stdin)
+    {
+      read_md_file = stdin;
+      read_md_filename = "<stdin>";
+      handle_toplevel_file (handle_directive);
+    }
+
+  return !have_error;
 }