Define arm_arch_core_flags in a single file
[official-gcc.git] / gcc / read-md.c
blob6d9a1bd01b0ca1d37d48334d036aa9f3e76a82f1
1 /* MD reader for GCC.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "errors.h"
24 #include "statistics.h"
25 #include "vec.h"
26 #include "read-md.h"
28 /* Associates PTR (which can be a string, etc.) with the file location
29 specified by FILENAME and LINENO. */
30 struct ptr_loc {
31 const void *ptr;
32 const char *filename;
33 int lineno;
36 /* This callback will be invoked whenever an md include directive is
37 processed. To be used for creation of the dependency file. */
38 void (*include_callback) (const char *);
40 /* Global singleton. */
42 md_reader *md_reader_ptr;
44 /* Given an object that starts with a char * name field, return a hash
45 code for its name. */
47 hashval_t
48 leading_string_hash (const void *def)
50 return htab_hash_string (*(const char *const *) def);
53 /* Given two objects that start with char * name fields, return true if
54 they have the same name. */
56 int
57 leading_string_eq_p (const void *def1, const void *def2)
59 return strcmp (*(const char *const *) def1,
60 *(const char *const *) def2) == 0;
63 /* Return a hash value for the pointer pointed to by DEF. */
65 static hashval_t
66 leading_ptr_hash (const void *def)
68 return htab_hash_pointer (*(const void *const *) def);
71 /* Return true if DEF1 and DEF2 are pointers to the same pointer. */
73 static int
74 leading_ptr_eq_p (const void *def1, const void *def2)
76 return *(const void *const *) def1 == *(const void *const *) def2;
79 /* Associate PTR with the file position given by FILENAME and LINENO. */
81 void
82 md_reader::set_md_ptr_loc (const void *ptr, const char *filename, int lineno)
84 struct ptr_loc *loc;
86 loc = (struct ptr_loc *) obstack_alloc (&m_ptr_loc_obstack,
87 sizeof (struct ptr_loc));
88 loc->ptr = ptr;
89 loc->filename = filename;
90 loc->lineno = lineno;
91 *htab_find_slot (m_ptr_locs, loc, INSERT) = loc;
94 /* Return the position associated with pointer PTR. Return null if no
95 position was set. */
97 const struct ptr_loc *
98 md_reader::get_md_ptr_loc (const void *ptr)
100 return (const struct ptr_loc *) htab_find (m_ptr_locs, &ptr);
103 /* Associate NEW_PTR with the same file position as OLD_PTR. */
105 void
106 md_reader::copy_md_ptr_loc (const void *new_ptr, const void *old_ptr)
108 const struct ptr_loc *loc = get_md_ptr_loc (old_ptr);
109 if (loc != 0)
110 set_md_ptr_loc (new_ptr, loc->filename, loc->lineno);
113 /* If PTR is associated with a known file position, print a #line
114 directive for it to OUTF. */
116 void
117 md_reader::fprint_md_ptr_loc (FILE *outf, const void *ptr)
119 const struct ptr_loc *loc = get_md_ptr_loc (ptr);
120 if (loc != 0)
121 fprintf (outf, "#line %d \"%s\"\n", loc->lineno, loc->filename);
124 /* Special fprint_md_ptr_loc for writing to STDOUT. */
125 void
126 md_reader::print_md_ptr_loc (const void *ptr)
128 fprint_md_ptr_loc (stdout, ptr);
131 /* Return a condition that satisfies both COND1 and COND2. Either string
132 may be null or empty. */
134 const char *
135 md_reader::join_c_conditions (const char *cond1, const char *cond2)
137 char *result;
138 const void **entry;
140 if (cond1 == 0 || cond1[0] == 0)
141 return cond2;
143 if (cond2 == 0 || cond2[0] == 0)
144 return cond1;
146 if (strcmp (cond1, cond2) == 0)
147 return cond1;
149 result = concat ("(", cond1, ") && (", cond2, ")", NULL);
150 obstack_ptr_grow (&m_joined_conditions_obstack, result);
151 obstack_ptr_grow (&m_joined_conditions_obstack, cond1);
152 obstack_ptr_grow (&m_joined_conditions_obstack, cond2);
153 entry = XOBFINISH (&m_joined_conditions_obstack, const void **);
154 *htab_find_slot (m_joined_conditions, entry, INSERT) = entry;
155 return result;
158 /* Print condition COND to OUTF, wrapped in brackets. If COND was created
159 by join_c_conditions, recursively invoke this function for the original
160 conditions and join the result with "&&". Otherwise print a #line
161 directive for COND if its original file position is known. */
163 void
164 md_reader::fprint_c_condition (FILE *outf, const char *cond)
166 const char **halves = (const char **) htab_find (m_joined_conditions, &cond);
167 if (halves != 0)
169 fprintf (outf, "(");
170 fprint_c_condition (outf, halves[1]);
171 fprintf (outf, " && ");
172 fprint_c_condition (outf, halves[2]);
173 fprintf (outf, ")");
175 else
177 fputc ('\n', outf);
178 fprint_md_ptr_loc (outf, cond);
179 fprintf (outf, "(%s)", cond);
183 /* Special fprint_c_condition for writing to STDOUT. */
185 void
186 md_reader::print_c_condition (const char *cond)
188 fprint_c_condition (stdout, cond);
191 /* A vfprintf-like function for reporting an error against line LINENO
192 of the current MD file. */
194 static void ATTRIBUTE_PRINTF(2,0)
195 message_at_1 (file_location loc, const char *msg, va_list ap)
197 fprintf (stderr, "%s:%d:%d: ", loc.filename, loc.lineno, loc.colno);
198 vfprintf (stderr, msg, ap);
199 fputc ('\n', stderr);
202 /* A printf-like function for reporting a message against location LOC. */
204 void
205 message_at (file_location loc, const char *msg, ...)
207 va_list ap;
209 va_start (ap, msg);
210 message_at_1 (loc, msg, ap);
211 va_end (ap);
214 /* Like message_at, but treat the condition as an error. */
216 void
217 error_at (file_location loc, const char *msg, ...)
219 va_list ap;
221 va_start (ap, msg);
222 message_at_1 (loc, msg, ap);
223 va_end (ap);
224 have_error = 1;
227 /* Like message_at, but treat the condition as a fatal error. */
229 void
230 fatal_at (file_location loc, const char *msg, ...)
232 va_list ap;
234 va_start (ap, msg);
235 message_at_1 (loc, msg, ap);
236 va_end (ap);
237 exit (1);
240 /* A printf-like function for reporting an error against the current
241 position in the MD file. */
243 void
244 fatal_with_file_and_line (const char *msg, ...)
246 char context[64];
247 size_t i;
248 int c;
249 va_list ap;
251 va_start (ap, msg);
253 fprintf (stderr, "%s:%d:%d: error: ", md_reader_ptr->get_filename (),
254 md_reader_ptr->get_lineno (),
255 md_reader_ptr->get_colno ());
256 vfprintf (stderr, msg, ap);
257 putc ('\n', stderr);
259 /* Gather some following context. */
260 for (i = 0; i < sizeof (context)-1; ++i)
262 c = read_char ();
263 if (c == EOF)
264 break;
265 if (c == '\r' || c == '\n')
267 unread_char (c);
268 break;
270 context[i] = c;
272 context[i] = '\0';
274 fprintf (stderr, "%s:%d:%d: note: following context is `%s'\n",
275 md_reader_ptr->get_filename (),
276 md_reader_ptr->get_lineno (),
277 md_reader_ptr->get_colno (), context);
279 va_end (ap);
280 exit (1);
283 /* Report that we found character ACTUAL when we expected to find
284 character EXPECTED. */
286 void
287 fatal_expected_char (int expected, int actual)
289 if (actual == EOF)
290 fatal_with_file_and_line ("expected character `%c', found EOF",
291 expected);
292 else
293 fatal_with_file_and_line ("expected character `%c', found `%c'",
294 expected, actual);
297 /* Read chars from the MD file until a non-whitespace char and return that.
298 Comments, both Lisp style and C style, are treated as whitespace. */
301 read_skip_spaces (void)
303 int c;
305 while (1)
307 c = read_char ();
308 switch (c)
310 case ' ': case '\t': case '\f': case '\r': case '\n':
311 break;
313 case ';':
315 c = read_char ();
316 while (c != '\n' && c != EOF);
317 break;
319 case '/':
321 int prevc;
322 c = read_char ();
323 if (c != '*')
325 unread_char (c);
326 fatal_with_file_and_line ("stray '/' in file");
329 prevc = 0;
330 while ((c = read_char ()) && c != EOF)
332 if (prevc == '*' && c == '/')
333 break;
334 prevc = c;
337 break;
339 default:
340 return c;
345 /* Consume the next character, issuing a fatal error if it is not
346 EXPECTED. */
348 void
349 md_reader::require_char (char expected)
351 int ch = read_char ();
352 if (ch != expected)
353 fatal_expected_char (expected, ch);
356 /* Consume any whitespace, then consume the next non-whitespace
357 character, issuing a fatal error if it is not EXPECTED. */
359 void
360 md_reader::require_char_ws (char expected)
362 int ch = read_skip_spaces ();
363 if (ch != expected)
364 fatal_expected_char (expected, ch);
367 /* Consume any whitespace, then consume the next word (as per read_name),
368 issuing a fatal error if it is not EXPECTED. */
370 void
371 md_reader::require_word_ws (const char *expected)
373 struct md_name name;
374 read_name (&name);
375 if (strcmp (name.string, expected))
376 fatal_with_file_and_line ("missing '%s'", expected);
379 /* Read the next character from the file. */
382 md_reader::read_char (void)
384 int ch;
386 ch = getc (m_read_md_file);
387 if (ch == '\n')
389 m_read_md_lineno++;
390 m_last_line_colno = m_read_md_colno;
391 m_read_md_colno = 0;
393 else
394 m_read_md_colno++;
396 return ch;
399 /* Put back CH, which was the last character read from the file. */
401 void
402 md_reader::unread_char (int ch)
404 if (ch == '\n')
406 m_read_md_lineno--;
407 m_read_md_colno = m_last_line_colno;
409 else
410 m_read_md_colno--;
411 ungetc (ch, m_read_md_file);
414 /* Peek at the next character from the file without consuming it. */
417 md_reader::peek_char (void)
419 int ch = read_char ();
420 unread_char (ch);
421 return ch;
424 /* Read an rtx code name into NAME. It is terminated by any of the
425 punctuation chars of rtx printed syntax. */
427 void
428 md_reader::read_name (struct md_name *name)
430 int c;
431 size_t i;
432 int angle_bracket_depth;
434 c = read_skip_spaces ();
436 i = 0;
437 angle_bracket_depth = 0;
438 while (1)
440 if (c == '<')
441 angle_bracket_depth++;
443 if ((c == '>') && (angle_bracket_depth > 0))
444 angle_bracket_depth--;
446 if (c == ' ' || c == '\n' || c == '\t' || c == '\f' || c == '\r'
447 || c == EOF)
448 break;
449 if (angle_bracket_depth == 0)
451 if (c == ':' || c == ')' || c == ']'
452 || c == '"' || c == '/' || c == '(' || c == '[')
454 unread_char (c);
455 break;
459 if (i == sizeof (name->buffer) - 1)
460 fatal_with_file_and_line ("name too long");
461 name->buffer[i++] = c;
463 c = read_char ();
466 if (i == 0)
467 fatal_with_file_and_line ("missing name or number");
469 name->buffer[i] = 0;
470 name->string = name->buffer;
472 if (m_md_constants)
474 /* Do constant expansion. */
475 struct md_constant *def;
479 struct md_constant tmp_def;
481 tmp_def.name = name->string;
482 def = (struct md_constant *) htab_find (m_md_constants, &tmp_def);
483 if (def)
484 name->string = def->value;
486 while (def);
490 /* Subroutine of the string readers. Handles backslash escapes.
491 Caller has read the backslash, but not placed it into the obstack. */
493 void
494 md_reader::read_escape ()
496 int c = read_char ();
498 switch (c)
500 /* Backslash-newline is replaced by nothing, as in C. */
501 case '\n':
502 return;
504 /* \" \' \\ are replaced by the second character. */
505 case '\\':
506 case '"':
507 case '\'':
508 break;
510 /* Standard C string escapes:
511 \a \b \f \n \r \t \v
512 \[0-7] \x
513 all are passed through to the output string unmolested.
514 In normal use these wind up in a string constant processed
515 by the C compiler, which will translate them appropriately.
516 We do not bother checking that \[0-7] are followed by up to
517 two octal digits, or that \x is followed by N hex digits.
518 \? \u \U are left out because they are not in traditional C. */
519 case 'a': case 'b': case 'f': case 'n': case 'r': case 't': case 'v':
520 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
521 case '7': case 'x':
522 obstack_1grow (&m_string_obstack, '\\');
523 break;
525 /* \; makes stuff for a C string constant containing
526 newline and tab. */
527 case ';':
528 obstack_grow (&m_string_obstack, "\\n\\t", 4);
529 return;
531 /* pass anything else through, but issue a warning. */
532 default:
533 fprintf (stderr, "%s:%d: warning: unrecognized escape \\%c\n",
534 get_filename (), get_lineno (),
536 obstack_1grow (&m_string_obstack, '\\');
537 break;
540 obstack_1grow (&m_string_obstack, c);
543 /* Read a double-quoted string onto the obstack. Caller has scanned
544 the leading quote. */
546 char *
547 md_reader::read_quoted_string ()
549 int c;
551 while (1)
553 c = read_char (); /* Read the string */
554 if (c == '\\')
556 read_escape ();
557 continue;
559 else if (c == '"' || c == EOF)
560 break;
562 obstack_1grow (&m_string_obstack, c);
565 obstack_1grow (&m_string_obstack, 0);
566 return XOBFINISH (&m_string_obstack, char *);
569 /* Read a braced string (a la Tcl) onto the string obstack. Caller
570 has scanned the leading brace. Note that unlike quoted strings,
571 the outermost braces _are_ included in the string constant. */
573 char *
574 md_reader::read_braced_string ()
576 int c;
577 int brace_depth = 1; /* caller-processed */
578 unsigned long starting_read_md_lineno = get_lineno ();
580 obstack_1grow (&m_string_obstack, '{');
581 while (brace_depth)
583 c = read_char (); /* Read the string */
585 if (c == '{')
586 brace_depth++;
587 else if (c == '}')
588 brace_depth--;
589 else if (c == '\\')
591 read_escape ();
592 continue;
594 else if (c == EOF)
595 fatal_with_file_and_line
596 ("missing closing } for opening brace on line %lu",
597 starting_read_md_lineno);
599 obstack_1grow (&m_string_obstack, c);
602 obstack_1grow (&m_string_obstack, 0);
603 return XOBFINISH (&m_string_obstack, char *);
606 /* Read some kind of string constant. This is the high-level routine
607 used by read_rtx. It handles surrounding parentheses, leading star,
608 and dispatch to the appropriate string constant reader. */
610 char *
611 md_reader::read_string (int star_if_braced)
613 char *stringbuf;
614 int saw_paren = 0;
615 int c, old_lineno;
617 c = read_skip_spaces ();
618 if (c == '(')
620 saw_paren = 1;
621 c = read_skip_spaces ();
624 old_lineno = get_lineno ();
625 if (c == '"')
626 stringbuf = read_quoted_string ();
627 else if (c == '{')
629 if (star_if_braced)
630 obstack_1grow (&m_string_obstack, '*');
631 stringbuf = read_braced_string ();
633 else
634 fatal_with_file_and_line ("expected `\"' or `{', found `%c'", c);
636 if (saw_paren)
637 require_char_ws (')');
639 set_md_ptr_loc (stringbuf, get_filename (), old_lineno);
640 return stringbuf;
643 /* Skip the rest of a construct that started at line LINENO and that
644 is currently nested by DEPTH levels of parentheses. */
646 void
647 md_reader::read_skip_construct (int depth, file_location loc)
649 struct md_name name;
650 int c;
654 c = read_skip_spaces ();
655 if (c == EOF)
657 error_at (loc, "unterminated construct");
658 exit (1);
660 switch (c)
662 case '(':
663 depth++;
664 break;
666 case ')':
667 depth--;
668 break;
670 case ':':
671 case '[':
672 case ']':
673 case '/':
674 break;
676 case '\"':
677 case '{':
678 unread_char (c);
679 read_string (false);
680 break;
682 default:
683 unread_char (c);
684 read_name (&name);
685 break;
688 while (depth > 0);
689 unread_char (c);
692 /* Given a string, return the number of comma-separated elements in it.
693 Return 0 for the null string. */
696 n_comma_elts (const char *s)
698 int n;
700 if (*s == '\0')
701 return 0;
703 for (n = 1; *s; s++)
704 if (*s == ',')
705 n++;
707 return n;
710 /* Given a pointer to a (char *), return a pointer to the beginning of the
711 next comma-separated element in the string. Advance the pointer given
712 to the end of that element. Return NULL if at end of string. Caller
713 is responsible for copying the string if necessary. White space between
714 a comma and an element is ignored. */
716 const char *
717 scan_comma_elt (const char **pstr)
719 const char *start;
720 const char *p = *pstr;
722 if (*p == ',')
723 p++;
724 while (ISSPACE (*p))
725 p++;
727 if (*p == '\0')
728 return NULL;
730 start = p;
732 while (*p != ',' && *p != '\0')
733 p++;
735 *pstr = p;
736 return start;
739 /* Convert STRING to uppercase. */
741 void
742 upcase_string (char *string)
744 int i;
746 for (i = 0; string[i]; i++)
747 string[i] = TOUPPER (string[i]);
750 /* Add a NAME = VALUE definition to md_constants-style hash table DEFS,
751 where both NAME and VALUE are malloc()ed strings. PARENT_ENUM is the
752 enum to which NAME belongs, or null if NAME is a stand-alone constant. */
754 static struct md_constant *
755 add_constant (htab_t defs, char *name, char *value,
756 struct enum_type *parent_enum)
758 struct md_constant *def, tmp_def;
759 void **entry_ptr;
761 tmp_def.name = name;
762 entry_ptr = htab_find_slot (defs, &tmp_def, INSERT);
763 if (*entry_ptr)
765 def = (struct md_constant *) *entry_ptr;
766 if (strcmp (def->value, value) != 0)
767 fatal_with_file_and_line ("redefinition of `%s', was `%s', now `%s'",
768 def->name, def->value, value);
769 else if (parent_enum || def->parent_enum)
770 fatal_with_file_and_line ("redefinition of `%s'", def->name);
771 free (name);
772 free (value);
774 else
776 def = XNEW (struct md_constant);
777 def->name = name;
778 def->value = value;
779 def->parent_enum = parent_enum;
780 *entry_ptr = def;
782 return def;
785 /* Process a define_constants directive, starting with the optional space
786 after the "define_constants". */
788 void
789 md_reader::handle_constants ()
791 int c;
792 htab_t defs;
794 require_char_ws ('[');
796 /* Disable constant expansion during definition processing. */
797 defs = m_md_constants;
798 m_md_constants = 0;
799 while ( (c = read_skip_spaces ()) != ']')
801 struct md_name name, value;
803 if (c != '(')
804 fatal_expected_char ('(', c);
806 read_name (&name);
807 read_name (&value);
808 add_constant (defs, xstrdup (name.string), xstrdup (value.string), 0);
810 require_char_ws (')');
812 m_md_constants = defs;
815 /* For every constant definition, call CALLBACK with two arguments:
816 a pointer a pointer to the constant definition and INFO.
817 Stop when CALLBACK returns zero. */
819 void
820 md_reader::traverse_md_constants (htab_trav callback, void *info)
822 htab_traverse (get_md_constants (), callback, info);
825 /* Return a malloc()ed decimal string that represents number NUMBER. */
827 static char *
828 md_decimal_string (int number)
830 /* A safe overestimate. +1 for sign, +1 for null terminator. */
831 char buffer[sizeof (int) * CHAR_BIT + 1 + 1];
833 sprintf (buffer, "%d", number);
834 return xstrdup (buffer);
837 /* Process a define_enum or define_c_enum directive, starting with
838 the optional space after the "define_enum". LINENO is the line
839 number on which the directive started and MD_P is true if the
840 directive is a define_enum rather than a define_c_enum. */
842 void
843 md_reader::handle_enum (file_location loc, bool md_p)
845 char *enum_name, *value_name;
846 struct md_name name;
847 struct enum_type *def;
848 struct enum_value *ev;
849 void **slot;
850 int c;
852 enum_name = read_string (false);
853 slot = htab_find_slot (m_enum_types, &enum_name, INSERT);
854 if (*slot)
856 def = (struct enum_type *) *slot;
857 if (def->md_p != md_p)
858 error_at (loc, "redefining `%s' as a different type of enum",
859 enum_name);
861 else
863 def = XNEW (struct enum_type);
864 def->name = enum_name;
865 def->md_p = md_p;
866 def->values = 0;
867 def->tail_ptr = &def->values;
868 def->num_values = 0;
869 *slot = def;
872 require_char_ws ('[');
874 while ((c = read_skip_spaces ()) != ']')
876 if (c == EOF)
878 error_at (loc, "unterminated construct");
879 exit (1);
881 unread_char (c);
882 read_name (&name);
884 ev = XNEW (struct enum_value);
885 ev->next = 0;
886 if (md_p)
888 value_name = concat (def->name, "_", name.string, NULL);
889 upcase_string (value_name);
890 ev->name = xstrdup (name.string);
892 else
894 value_name = xstrdup (name.string);
895 ev->name = value_name;
897 ev->def = add_constant (get_md_constants (), value_name,
898 md_decimal_string (def->num_values), def);
900 *def->tail_ptr = ev;
901 def->tail_ptr = &ev->next;
902 def->num_values++;
906 /* Try to find the definition of the given enum. Return null on failure. */
908 struct enum_type *
909 md_reader::lookup_enum_type (const char *name)
911 return (struct enum_type *) htab_find (m_enum_types, &name);
914 /* For every enum definition, call CALLBACK with two arguments:
915 a pointer to the constant definition and INFO. Stop when CALLBACK
916 returns zero. */
918 void
919 md_reader::traverse_enum_types (htab_trav callback, void *info)
921 htab_traverse (m_enum_types, callback, info);
925 /* Constructor for md_reader. */
927 md_reader::md_reader ()
928 : m_toplevel_fname (NULL),
929 m_base_dir (NULL),
930 m_read_md_file (NULL),
931 m_read_md_filename (NULL),
932 m_read_md_lineno (0),
933 m_read_md_colno (0),
934 m_first_dir_md_include (NULL),
935 m_last_dir_md_include_ptr (&m_first_dir_md_include)
937 /* Set the global singleton pointer. */
938 md_reader_ptr = this;
940 obstack_init (&m_string_obstack);
942 m_ptr_locs = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
943 obstack_init (&m_ptr_loc_obstack);
945 m_joined_conditions = htab_create (161, leading_ptr_hash, leading_ptr_eq_p, 0);
946 obstack_init (&m_joined_conditions_obstack);
948 m_md_constants = htab_create (31, leading_string_hash,
949 leading_string_eq_p, (htab_del) 0);
951 m_enum_types = htab_create (31, leading_string_hash,
952 leading_string_eq_p, (htab_del) 0);
954 /* Unlock the stdio streams. */
955 unlock_std_streams ();
958 /* md_reader's destructor. */
960 md_reader::~md_reader ()
962 free (m_base_dir);
964 htab_delete (m_enum_types);
966 htab_delete (m_md_constants);
968 obstack_free (&m_joined_conditions_obstack, NULL);
969 htab_delete (m_joined_conditions);
971 obstack_free (&m_ptr_loc_obstack, NULL);
972 htab_delete (m_ptr_locs);
974 obstack_free (&m_string_obstack, NULL);
976 /* Clear the global singleton pointer. */
977 md_reader_ptr = NULL;
980 /* Process an "include" directive, starting with the optional space
981 after the "include". Read in the file and use HANDLE_DIRECTIVE
982 to process each unknown directive. LINENO is the line number on
983 which the "include" occurred. */
985 void
986 md_reader::handle_include (file_location loc)
988 const char *filename;
989 const char *old_filename;
990 int old_lineno, old_colno;
991 char *pathname;
992 FILE *input_file, *old_file;
994 filename = read_string (false);
995 input_file = NULL;
997 /* If the specified file name is absolute, skip the include stack. */
998 if (!IS_ABSOLUTE_PATH (filename))
1000 struct file_name_list *stackp;
1002 /* Search the directory path, trying to open the file. */
1003 for (stackp = m_first_dir_md_include; stackp; stackp = stackp->next)
1005 static const char sep[2] = { DIR_SEPARATOR, '\0' };
1007 pathname = concat (stackp->fname, sep, filename, NULL);
1008 input_file = fopen (pathname, "r");
1009 if (input_file != NULL)
1010 break;
1011 free (pathname);
1015 /* If we haven't managed to open the file yet, try combining the
1016 filename with BASE_DIR. */
1017 if (input_file == NULL)
1019 if (m_base_dir)
1020 pathname = concat (m_base_dir, filename, NULL);
1021 else
1022 pathname = xstrdup (filename);
1023 input_file = fopen (pathname, "r");
1026 if (input_file == NULL)
1028 free (pathname);
1029 error_at (loc, "include file `%s' not found", filename);
1030 return;
1033 /* Save the old cursor. Note that the LINENO argument to this
1034 function is the beginning of the include statement, while
1035 read_md_lineno has already been advanced. */
1036 old_file = m_read_md_file;
1037 old_filename = m_read_md_filename;
1038 old_lineno = m_read_md_lineno;
1039 old_colno = m_read_md_colno;
1041 if (include_callback)
1042 include_callback (pathname);
1044 m_read_md_file = input_file;
1045 m_read_md_filename = pathname;
1047 handle_file ();
1049 /* Restore the old cursor. */
1050 m_read_md_file = old_file;
1051 m_read_md_filename = old_filename;
1052 m_read_md_lineno = old_lineno;
1053 m_read_md_colno = old_colno;
1055 /* Do not free the pathname. It is attached to the various rtx
1056 queue elements. */
1059 /* Process the current file, assuming that read_md_file and
1060 read_md_filename are valid. Use HANDLE_DIRECTIVE to handle
1061 unknown directives. */
1063 void
1064 md_reader::handle_file ()
1066 struct md_name directive;
1067 int c;
1069 m_read_md_lineno = 1;
1070 m_read_md_colno = 0;
1071 while ((c = read_skip_spaces ()) != EOF)
1073 file_location loc = get_current_location ();
1074 if (c != '(')
1075 fatal_expected_char ('(', c);
1077 read_name (&directive);
1078 if (strcmp (directive.string, "define_constants") == 0)
1079 handle_constants ();
1080 else if (strcmp (directive.string, "define_enum") == 0)
1081 handle_enum (loc, true);
1082 else if (strcmp (directive.string, "define_c_enum") == 0)
1083 handle_enum (loc, false);
1084 else if (strcmp (directive.string, "include") == 0)
1085 handle_include (loc);
1086 else
1087 handle_unknown_directive (loc, directive.string);
1089 require_char_ws (')');
1091 fclose (m_read_md_file);
1094 /* Like handle_file, but for top-level files. Set up m_toplevel_fname
1095 and m_base_dir accordingly. */
1097 void
1098 md_reader::handle_toplevel_file ()
1100 const char *base;
1102 m_toplevel_fname = m_read_md_filename;
1103 base = lbasename (m_toplevel_fname);
1104 if (base == m_toplevel_fname)
1105 m_base_dir = NULL;
1106 else
1107 m_base_dir = xstrndup (m_toplevel_fname, base - m_toplevel_fname);
1109 handle_file ();
1112 file_location
1113 md_reader::get_current_location () const
1115 return file_location (m_read_md_filename, m_read_md_lineno, m_read_md_colno);
1118 /* Parse a -I option with argument ARG. */
1120 void
1121 md_reader::add_include_path (const char *arg)
1123 struct file_name_list *dirtmp;
1125 dirtmp = XNEW (struct file_name_list);
1126 dirtmp->next = 0;
1127 dirtmp->fname = arg;
1128 *m_last_dir_md_include_ptr = dirtmp;
1129 m_last_dir_md_include_ptr = &dirtmp->next;
1132 /* The main routine for reading .md files. Try to process all the .md
1133 files specified on the command line and return true if no error occurred.
1135 ARGC and ARGV are the arguments to main.
1137 PARSE_OPT, if nonnull, is passed all unknown command-line arguments.
1138 It should return true if it recognizes the argument or false if a
1139 generic error should be reported. */
1141 bool
1142 md_reader::read_md_files (int argc, const char **argv,
1143 bool (*parse_opt) (const char *))
1145 int i;
1146 bool no_more_options;
1147 bool already_read_stdin;
1148 int num_files;
1150 /* First we loop over all the options. */
1151 for (i = 1; i < argc; i++)
1152 if (argv[i][0] == '-')
1154 /* An argument consisting of exactly one dash is a request to
1155 read stdin. This will be handled in the second loop. */
1156 if (argv[i][1] == '\0')
1157 continue;
1159 /* An argument consisting of just two dashes causes option
1160 parsing to cease. */
1161 if (argv[i][1] == '-' && argv[i][2] == '\0')
1162 break;
1164 if (argv[i][1] == 'I')
1166 if (argv[i][2] != '\0')
1167 add_include_path (argv[i] + 2);
1168 else if (++i < argc)
1169 add_include_path (argv[i]);
1170 else
1171 fatal ("directory name missing after -I option");
1172 continue;
1175 /* The program may have provided a callback so it can
1176 accept its own options. */
1177 if (parse_opt && parse_opt (argv[i]))
1178 continue;
1180 fatal ("invalid option `%s'", argv[i]);
1183 /* Now loop over all input files. */
1184 num_files = 0;
1185 no_more_options = false;
1186 already_read_stdin = false;
1187 for (i = 1; i < argc; i++)
1189 if (argv[i][0] == '-')
1191 if (argv[i][1] == '\0')
1193 /* Read stdin. */
1194 if (already_read_stdin)
1195 fatal ("cannot read standard input twice");
1197 m_read_md_file = stdin;
1198 m_read_md_filename = "<stdin>";
1199 handle_toplevel_file ();
1200 already_read_stdin = true;
1201 continue;
1203 else if (argv[i][1] == '-' && argv[i][2] == '\0')
1205 /* No further arguments are to be treated as options. */
1206 no_more_options = true;
1207 continue;
1209 else if (!no_more_options)
1210 continue;
1213 /* If we get here we are looking at a non-option argument, i.e.
1214 a file to be processed. */
1215 m_read_md_filename = argv[i];
1216 m_read_md_file = fopen (m_read_md_filename, "r");
1217 if (m_read_md_file == 0)
1219 perror (m_read_md_filename);
1220 return false;
1222 handle_toplevel_file ();
1223 num_files++;
1226 /* If we get to this point without having seen any files to process,
1227 read the standard input now. */
1228 if (num_files == 0 && !already_read_stdin)
1230 m_read_md_file = stdin;
1231 m_read_md_filename = "<stdin>";
1232 handle_toplevel_file ();
1235 return !have_error;
1238 /* class noop_reader : public md_reader */
1240 /* A dummy implementation which skips unknown directives. */
1241 void
1242 noop_reader::handle_unknown_directive (file_location loc, const char *)
1244 read_skip_construct (1, loc);