+\f
+/* Input file handling. */
+
+/* Table of all input files. */
+static const char **gt_files;
+static size_t num_gt_files;
+
+/* A number of places use the name of this file for a location for
+ things that we can't rely on the source to define. Make sure we
+ can still use pointer comparison on filenames. */
+static const char this_file[] = __FILE__;
+
+/* Vector of per-language directories. */
+static const char **lang_dir_names;
+static size_t num_lang_dirs;
+
+/* An array of output files suitable for definitions. There is one
+ BASE_FILES entry for each language. */
+static outf_p *base_files;
+
+/* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
+ INPUT_FILE is used by <lang>.
+
+ This function should be written to assume that a file _is_ used
+ if the situation is unclear. If it wrongly assumes a file _is_ used,
+ a linker error will result. If it wrongly assumes a file _is not_ used,
+ some GC roots may be missed, which is a much harder-to-debug problem.
+
+ The relevant bitmap is stored immediately before the file's name in the
+ buffer set up by read_input_list. It may be unaligned, so we have to
+ read it byte-by-byte. */
+
+static lang_bitmap
+get_lang_bitmap (const char *gtfile)
+{
+
+ if (gtfile == this_file)
+ /* Things defined in this file are universal. */
+ return (((lang_bitmap)1) << num_lang_dirs) - 1;
+ else
+ {
+ lang_bitmap n = 0;
+ int i;
+ for (i = -(int) sizeof (lang_bitmap); i < 0; i++)
+ n = (n << CHAR_BIT) + (unsigned char)gtfile[i];
+ return n;
+ }
+}
+
+/* Set the bitmap returned by get_lang_bitmap. The only legitimate
+ caller of this function is read_input_list. */
+static void
+set_lang_bitmap (char *gtfile, lang_bitmap n)
+{
+ int i;
+ for (i = -1; i >= -(int) sizeof (lang_bitmap); i--)
+ {
+ gtfile[i] = n & ((1U << CHAR_BIT)-1);
+ n >>= CHAR_BIT;
+ }
+}
+
+/* Scan the input file, LIST, and determine how much space we need to
+ store strings in. Also, count the number of language directories
+ and files. The numbers returned are overestimates as they does not
+ consider repeated files. */
+static size_t
+measure_input_list (FILE *list)
+{
+ size_t n = 0;
+ int c;
+ bool atbol = true;
+ num_lang_dirs = 0;
+ num_gt_files = plugin_files ? nb_plugin_files : 0;
+ while ((c = getc (list)) != EOF)
+ {
+ n++;
+ if (atbol)
+ {
+ if (c == '[')
+ num_lang_dirs++;
+ else
+ {
+ /* Add space for a lang_bitmap before the input file name. */
+ n += sizeof (lang_bitmap);
+ num_gt_files++;
+ }
+ atbol = false;
+ }
+
+ if (c == '\n')
+ atbol = true;
+ }
+
+ rewind (list);
+ return n;
+}
+
+/* Read one input line from LIST to HEREP (which is updated). A
+ pointer to the string is returned via LINEP. If it was a language
+ subdirectory in square brackets, strip off the square brackets and
+ return true. Otherwise, leave space before the string for a
+ lang_bitmap, and return false. At EOF, returns false, does not
+ touch *HEREP, and sets *LINEP to NULL. POS is used for
+ diagnostics. */
+static bool
+read_input_line (FILE *list, char **herep, char **linep,
+ struct fileloc *pos)
+{
+ char *here = *herep;
+ char *line;
+ int c = getc (list);
+
+ /* Read over whitespace. */
+ while (c == '\n' || c == ' ')
+ c = getc (list);
+
+ if (c == EOF)
+ {
+ *linep = 0;
+ return false;
+ }
+ else if (c == '[')
+ {
+ /* No space for a lang_bitmap is necessary. Discard the '['. */
+ c = getc (list);
+ line = here;
+ while (c != ']' && c != '\n' && c != EOF)
+ {
+ *here++ = c;
+ c = getc (list);
+ }
+ *here++ = '\0';
+
+ if (c == ']')
+ {
+ c = getc (list); /* eat what should be a newline */
+ if (c != '\n' && c != EOF)
+ error_at_line (pos, "junk on line after language tag [%s]", line);
+ }
+ else
+ error_at_line (pos, "missing close bracket for language tag [%s", line);
+
+ *herep = here;
+ *linep = line;
+ return true;
+ }
+ else
+ {
+ /* Leave space for a lang_bitmap. */
+ memset (here, 0, sizeof (lang_bitmap));
+ here += sizeof (lang_bitmap);
+ line = here;
+ do
+ {
+ *here++ = c;
+ c = getc (list);
+ }
+ while (c != EOF && c != '\n');
+ *here++ = '\0';
+ *herep = here;
+ *linep = line;
+ return false;
+ }
+}
+
+/* Read the list of input files from LIST and compute all of the
+ relevant tables. There is one file per line of the list. At
+ first, all the files on the list are language-generic, but
+ eventually a line will appear which is the name of a language
+ subdirectory in square brackets, like this: [cp]. All subsequent
+ files are specific to that language, until another language
+ subdirectory tag appears. Files can appear more than once, if
+ they apply to more than one language. */
+static void
+read_input_list (const char *listname)
+{
+ FILE *list = fopen (listname, "r");
+ if (!list)
+ fatal ("cannot open %s: %s", listname, strerror (errno));
+ else
+ {
+ struct fileloc epos;
+ size_t bufsz = measure_input_list (list);
+ char *buf = XNEWVEC (char, bufsz);
+ char *here = buf;
+ char *committed = buf;
+ char *limit = buf + bufsz;
+ char *line;
+ bool is_language;
+ size_t langno = 0;
+ size_t nfiles = 0;
+ lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
+
+ epos.file = listname;
+ epos.line = 0;
+
+ lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
+ gt_files = XNEWVEC (const char *, num_gt_files);
+
+ for (;;)
+ {
+ next_line:
+ epos.line++;
+ committed = here;
+ is_language = read_input_line (list, &here, &line, &epos);
+ gcc_assert (here <= limit);
+ if (line == 0)
+ break;
+ else if (is_language)
+ {
+ size_t i;
+ gcc_assert (langno <= num_lang_dirs);
+ for (i = 0; i < langno; i++)
+ if (strcmp (lang_dir_names[i], line) == 0)
+ {
+ error_at_line (&epos, "duplicate language tag [%s]", line);
+ curlangs = 1 << i;
+ here = committed;
+ goto next_line;
+ }
+
+ curlangs = 1 << langno;
+ lang_dir_names[langno++] = line;
+ }
+ else
+ {
+ size_t i;
+ gcc_assert (nfiles <= num_gt_files);
+ for (i = 0; i < nfiles; i++)
+ if (strcmp (gt_files[i], line) == 0)
+ {
+ /* Throw away the string we just read, and add the
+ current language to the existing string's bitmap. */
+ lang_bitmap bmap = get_lang_bitmap (gt_files[i]);
+ if (bmap & curlangs)
+ error_at_line (&epos, "file %s specified more than once "
+ "for language %s", line, langno == 0
+ ? "(all)"
+ : lang_dir_names[langno - 1]);
+
+ bmap |= curlangs;
+ set_lang_bitmap (CONST_CAST(char *, gt_files[i]), bmap);
+ here = committed;
+ goto next_line;
+ }
+
+ set_lang_bitmap (line, curlangs);
+ gt_files[nfiles++] = line;
+ }
+ }
+ /* Update the global counts now that we know accurately how many
+ things there are. (We do not bother resizing the arrays down.) */
+ num_lang_dirs = langno;
+ /* Add the plugin files if provided. */
+ if (plugin_files)
+ {
+ size_t i;
+ for (i = 0; i < nb_plugin_files; i++)
+ gt_files[nfiles++] = plugin_files[i];
+ }
+ num_gt_files = nfiles;
+ }
+
+ /* Sanity check: any file that resides in a language subdirectory
+ (e.g. 'cp') ought to belong to the corresponding language.
+ ??? Still true if for instance ObjC++ is enabled and C++ isn't?
+ (Can you even do that? Should you be allowed to?) */
+ {
+ size_t f;
+ for (f = 0; f < num_gt_files; f++)
+ {
+ lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
+ const char *basename = get_file_basename (gt_files[f]);
+ const char *slashpos = strchr (basename, '/');
+
+ if (slashpos)
+ {
+ size_t l;
+ for (l = 0; l < num_lang_dirs; l++)
+ if ((size_t)(slashpos - basename) == strlen (lang_dir_names [l])
+ && memcmp (basename, lang_dir_names[l],
+ strlen (lang_dir_names[l])) == 0)
+ {
+ if (!(bitmap & (1 << l)))
+ error ("%s is in language directory '%s' but is not "
+ "tagged for that language",
+ basename, lang_dir_names[l]);
+ break;
+ }
+ }
+ }
+ }
+
+ if (ferror (list))
+ fatal ("error reading %s: %s", listname, strerror (errno));
+
+ fclose (list);
+}