OSDN Git Service

2009-08-17 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / scanner.c
1 /* Character scanner.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* Set of subroutines to (ultimately) return the next character to the
23    various matching subroutines.  This file's job is to read files and
24    build up lines that are parsed by the parser.  This means that we
25    handle continuation lines and "include" lines.
26
27    The first thing the scanner does is to load an entire file into
28    memory.  We load the entire file into memory for a couple reasons.
29    The first is that we want to be able to deal with nonseekable input
30    (pipes, stdin) and there is a lot of backing up involved during
31    parsing.
32
33    The second is that we want to be able to print the locus of errors,
34    and an error on line 999999 could conflict with something on line
35    one.  Given nonseekable input, we've got to store the whole thing.
36
37    One thing that helps are the column truncation limits that give us
38    an upper bound on the size of individual lines.  We don't store the
39    truncated stuff.
40
41    From the scanner's viewpoint, the higher level subroutines ask for
42    new characters and do a lot of jumping backwards.  */
43
44 #include "config.h"
45 #include "system.h"
46 #include "gfortran.h"
47 #include "toplev.h"
48 #include "debug.h"
49 #include "flags.h"
50 #include "cpp.h"
51
52 /* Structure for holding module and include file search path.  */
53 typedef struct gfc_directorylist
54 {
55   char *path;
56   bool use_for_modules;
57   struct gfc_directorylist *next;
58 }
59 gfc_directorylist;
60
61 /* List of include file search directories.  */
62 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
63
64 static gfc_file *file_head, *current_file;
65
66 static int continue_flag, end_flag, openmp_flag, gcc_attribute_flag;
67 static int continue_count, continue_line;
68 static locus openmp_locus;
69 static locus gcc_attribute_locus;
70
71 gfc_source_form gfc_current_form;
72 static gfc_linebuf *line_head, *line_tail;
73        
74 locus gfc_current_locus;
75 const char *gfc_source_file;
76 static FILE *gfc_src_file;
77 static gfc_char_t *gfc_src_preprocessor_lines[2];
78
79 extern int pedantic;
80
81 static struct gfc_file_change
82 {
83   const char *filename;
84   gfc_linebuf *lb;
85   int line;
86 } *file_changes;
87 size_t file_changes_cur, file_changes_count;
88 size_t file_changes_allocated;
89
90
91 /* Functions dealing with our wide characters (gfc_char_t) and
92    sequences of such characters.  */
93
94 int
95 gfc_wide_fits_in_byte (gfc_char_t c)
96 {
97   return (c <= UCHAR_MAX);
98 }
99
100 static inline int
101 wide_is_ascii (gfc_char_t c)
102 {
103   return (gfc_wide_fits_in_byte (c) && ((unsigned char) c & ~0x7f) == 0);
104 }
105
106 int
107 gfc_wide_is_printable (gfc_char_t c)
108 {
109   return (gfc_wide_fits_in_byte (c) && ISPRINT ((unsigned char) c));
110 }
111
112 gfc_char_t
113 gfc_wide_tolower (gfc_char_t c)
114 {
115   return (wide_is_ascii (c) ? (gfc_char_t) TOLOWER((unsigned char) c) : c);
116 }
117
118 gfc_char_t
119 gfc_wide_toupper (gfc_char_t c)
120 {
121   return (wide_is_ascii (c) ? (gfc_char_t) TOUPPER((unsigned char) c) : c);
122 }
123
124 int
125 gfc_wide_is_digit (gfc_char_t c)
126 {
127   return (c >= '0' && c <= '9');
128 }
129
130 static inline int
131 wide_atoi (gfc_char_t *c)
132 {
133 #define MAX_DIGITS 20
134   char buf[MAX_DIGITS+1];
135   int i = 0;
136
137   while (gfc_wide_is_digit(*c) && i < MAX_DIGITS)
138     buf[i++] = *c++;
139   buf[i] = '\0';
140   return atoi (buf);
141 }
142
143 size_t
144 gfc_wide_strlen (const gfc_char_t *str)
145 {
146   size_t i;
147
148   for (i = 0; str[i]; i++)
149     ;
150
151   return i;
152 }
153
154 gfc_char_t *
155 gfc_wide_memset (gfc_char_t *b, gfc_char_t c, size_t len)
156 {
157   size_t i;
158
159   for (i = 0; i < len; i++)
160     b[i] = c;
161
162   return b;
163 }
164
165 static gfc_char_t *
166 wide_strcpy (gfc_char_t *dest, const gfc_char_t *src)
167 {
168   gfc_char_t *d;
169
170   for (d = dest; (*d = *src) != '\0'; ++src, ++d)
171     ;
172
173   return dest;
174 }
175
176 static gfc_char_t *
177 wide_strchr (const gfc_char_t *s, gfc_char_t c)
178 {
179   do {
180     if (*s == c)
181       {
182         return CONST_CAST(gfc_char_t *, s);
183       }
184   } while (*s++);
185   return 0;
186 }
187
188 char *
189 gfc_widechar_to_char (const gfc_char_t *s, int length)
190 {
191   size_t len, i;
192   char *res;
193
194   if (s == NULL)
195     return NULL;
196
197   /* Passing a negative length is used to indicate that length should be
198      calculated using gfc_wide_strlen().  */
199   len = (length >= 0 ? (size_t) length : gfc_wide_strlen (s));
200   res = XNEWVEC (char, len + 1);
201
202   for (i = 0; i < len; i++)
203     {
204       gcc_assert (gfc_wide_fits_in_byte (s[i]));
205       res[i] = (unsigned char) s[i];
206     }
207
208   res[len] = '\0';
209   return res;
210 }
211
212 gfc_char_t *
213 gfc_char_to_widechar (const char *s)
214 {
215   size_t len, i;
216   gfc_char_t *res;
217
218   if (s == NULL)
219     return NULL;
220
221   len = strlen (s);
222   res = gfc_get_wide_string (len + 1);
223
224   for (i = 0; i < len; i++)
225     res[i] = (unsigned char) s[i];
226
227   res[len] = '\0';
228   return res;
229 }
230
231 static int
232 wide_strncmp (const gfc_char_t *s1, const char *s2, size_t n)
233 {
234   gfc_char_t c1, c2;
235
236   while (n-- > 0)
237     {
238       c1 = *s1++;
239       c2 = *s2++;
240       if (c1 != c2)
241         return (c1 > c2 ? 1 : -1);
242       if (c1 == '\0')
243         return 0;
244     }
245   return 0;
246 }
247
248 int
249 gfc_wide_strncasecmp (const gfc_char_t *s1, const char *s2, size_t n)
250 {
251   gfc_char_t c1, c2;
252
253   while (n-- > 0)
254     {
255       c1 = gfc_wide_tolower (*s1++);
256       c2 = TOLOWER (*s2++);
257       if (c1 != c2)
258         return (c1 > c2 ? 1 : -1);
259       if (c1 == '\0')
260         return 0;
261     }
262   return 0;
263 }
264
265
266 /* Main scanner initialization.  */
267
268 void
269 gfc_scanner_init_1 (void)
270 {
271   file_head = NULL;
272   line_head = NULL;
273   line_tail = NULL;
274
275   continue_count = 0;
276   continue_line = 0;
277
278   end_flag = 0;
279 }
280
281
282 /* Main scanner destructor.  */
283
284 void
285 gfc_scanner_done_1 (void)
286 {
287   gfc_linebuf *lb;
288   gfc_file *f;
289
290   while(line_head != NULL) 
291     {
292       lb = line_head->next;
293       gfc_free(line_head);
294       line_head = lb;
295     }
296      
297   while(file_head != NULL) 
298     {
299       f = file_head->next;
300       gfc_free(file_head->filename);
301       gfc_free(file_head);
302       file_head = f;    
303     }
304 }
305
306
307 /* Adds path to the list pointed to by list.  */
308
309 static void
310 add_path_to_list (gfc_directorylist **list, const char *path,
311                   bool use_for_modules, bool head)
312 {
313   gfc_directorylist *dir;
314   const char *p;
315
316   p = path;
317   while (*p == ' ' || *p == '\t')  /* someone might do "-I include" */
318     if (*p++ == '\0')
319       return;
320
321   if (head || *list == NULL)
322     {
323       dir = XCNEW (gfc_directorylist);
324       if (!head)
325         *list = dir;
326     }
327   else
328     {
329       dir = *list;
330       while (dir->next)
331         dir = dir->next;
332
333       dir->next = XCNEW (gfc_directorylist);
334       dir = dir->next;
335     }
336
337   dir->next = head ? *list : NULL;
338   if (head)
339     *list = dir;
340   dir->use_for_modules = use_for_modules;
341   dir->path = XCNEWVEC (char, strlen (p) + 2);
342   strcpy (dir->path, p);
343   strcat (dir->path, "/");      /* make '/' last character */
344 }
345
346
347 void
348 gfc_add_include_path (const char *path, bool use_for_modules, bool file_dir)
349 {
350   add_path_to_list (&include_dirs, path, use_for_modules, file_dir);
351
352   /* For '#include "..."' these directories are automatically searched.  */
353   if (!file_dir)
354     gfc_cpp_add_include_path (xstrdup(path), true);
355 }
356
357
358 void
359 gfc_add_intrinsic_modules_path (const char *path)
360 {
361   add_path_to_list (&intrinsic_modules_dirs, path, true, false);
362 }
363
364
365 /* Release resources allocated for options.  */
366
367 void
368 gfc_release_include_path (void)
369 {
370   gfc_directorylist *p;
371
372   while (include_dirs != NULL)
373     {
374       p = include_dirs;
375       include_dirs = include_dirs->next;
376       gfc_free (p->path);
377       gfc_free (p);
378     }
379
380   while (intrinsic_modules_dirs != NULL)
381     {
382       p = intrinsic_modules_dirs;
383       intrinsic_modules_dirs = intrinsic_modules_dirs->next;
384       gfc_free (p->path);
385       gfc_free (p);
386     }
387
388   gfc_free (gfc_option.module_dir);
389 }
390
391
392 static FILE *
393 open_included_file (const char *name, gfc_directorylist *list, bool module)
394 {
395   char *fullname;
396   gfc_directorylist *p;
397   FILE *f;
398
399   for (p = list; p; p = p->next)
400     {
401       if (module && !p->use_for_modules)
402         continue;
403
404       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
405       strcpy (fullname, p->path);
406       strcat (fullname, name);
407
408       f = gfc_open_file (fullname);
409       if (f != NULL)
410         return f;
411     }
412
413   return NULL;
414 }
415
416
417 /* Opens file for reading, searching through the include directories
418    given if necessary.  If the include_cwd argument is true, we try
419    to open the file in the current directory first.  */
420
421 FILE *
422 gfc_open_included_file (const char *name, bool include_cwd, bool module)
423 {
424   FILE *f;
425
426   if (IS_ABSOLUTE_PATH (name))
427     return gfc_open_file (name);
428
429   if (include_cwd)
430     {
431       f = gfc_open_file (name);
432       if (f != NULL)
433         return f;
434     }
435
436   return open_included_file (name, include_dirs, module);
437 }
438
439 FILE *
440 gfc_open_intrinsic_module (const char *name)
441 {
442   if (IS_ABSOLUTE_PATH (name))
443     return gfc_open_file (name);
444
445   return open_included_file (name, intrinsic_modules_dirs, true);
446 }
447
448
449 /* Test to see if we're at the end of the main source file.  */
450
451 int
452 gfc_at_end (void)
453 {
454   return end_flag;
455 }
456
457
458 /* Test to see if we're at the end of the current file.  */
459
460 int
461 gfc_at_eof (void)
462 {
463   if (gfc_at_end ())
464     return 1;
465
466   if (line_head == NULL)
467     return 1;                   /* Null file */
468
469   if (gfc_current_locus.lb == NULL)
470     return 1;
471
472   return 0;
473 }
474
475
476 /* Test to see if we're at the beginning of a new line.  */
477
478 int
479 gfc_at_bol (void)
480 {
481   if (gfc_at_eof ())
482     return 1;
483
484   return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
485 }
486
487
488 /* Test to see if we're at the end of a line.  */
489
490 int
491 gfc_at_eol (void)
492 {
493   if (gfc_at_eof ())
494     return 1;
495
496   return (*gfc_current_locus.nextc == '\0');
497 }
498
499 static void
500 add_file_change (const char *filename, int line)
501 {
502   if (file_changes_count == file_changes_allocated)
503     {
504       if (file_changes_allocated)
505         file_changes_allocated *= 2;
506       else
507         file_changes_allocated = 16;
508       file_changes = XRESIZEVEC (struct gfc_file_change, file_changes,
509                                  file_changes_allocated);
510     }
511   file_changes[file_changes_count].filename = filename;
512   file_changes[file_changes_count].lb = NULL;
513   file_changes[file_changes_count++].line = line;
514 }
515
516 static void
517 report_file_change (gfc_linebuf *lb)
518 {
519   size_t c = file_changes_cur;
520   while (c < file_changes_count
521          && file_changes[c].lb == lb)
522     {
523       if (file_changes[c].filename)
524         (*debug_hooks->start_source_file) (file_changes[c].line,
525                                            file_changes[c].filename);
526       else
527         (*debug_hooks->end_source_file) (file_changes[c].line);
528       ++c;
529     }
530   file_changes_cur = c;
531 }
532
533 void
534 gfc_start_source_files (void)
535 {
536   /* If the debugger wants the name of the main source file,
537      we give it.  */
538   if (debug_hooks->start_end_main_source_file)
539     (*debug_hooks->start_source_file) (0, gfc_source_file);
540
541   file_changes_cur = 0;
542   report_file_change (gfc_current_locus.lb);
543 }
544
545 void
546 gfc_end_source_files (void)
547 {
548   report_file_change (NULL);
549
550   if (debug_hooks->start_end_main_source_file)
551     (*debug_hooks->end_source_file) (0);
552 }
553
554 /* Advance the current line pointer to the next line.  */
555
556 void
557 gfc_advance_line (void)
558 {
559   if (gfc_at_end ())
560     return;
561
562   if (gfc_current_locus.lb == NULL) 
563     {
564       end_flag = 1;
565       return;
566     } 
567
568   if (gfc_current_locus.lb->next
569       && !gfc_current_locus.lb->next->dbg_emitted)
570     {
571       report_file_change (gfc_current_locus.lb->next);
572       gfc_current_locus.lb->next->dbg_emitted = true;
573     }
574
575   gfc_current_locus.lb = gfc_current_locus.lb->next;
576
577   if (gfc_current_locus.lb != NULL)      
578     gfc_current_locus.nextc = gfc_current_locus.lb->line;
579   else 
580     {
581       gfc_current_locus.nextc = NULL;
582       end_flag = 1;
583     }       
584 }
585
586
587 /* Get the next character from the input, advancing gfc_current_file's
588    locus.  When we hit the end of the line or the end of the file, we
589    start returning a '\n' in order to complete the current statement.
590    No Fortran line conventions are implemented here.
591
592    Requiring explicit advances to the next line prevents the parse
593    pointer from being on the wrong line if the current statement ends
594    prematurely.  */
595
596 static gfc_char_t
597 next_char (void)
598 {
599   gfc_char_t c;
600   
601   if (gfc_current_locus.nextc == NULL)
602     return '\n';
603
604   c = *gfc_current_locus.nextc++;
605   if (c == '\0')
606     {
607       gfc_current_locus.nextc--; /* Remain on this line.  */
608       c = '\n';
609     }
610
611   return c;
612 }
613
614
615 /* Skip a comment.  When we come here the parse pointer is positioned
616    immediately after the comment character.  If we ever implement
617    compiler directives withing comments, here is where we parse the
618    directive.  */
619
620 static void
621 skip_comment_line (void)
622 {
623   gfc_char_t c;
624
625   do
626     {
627       c = next_char ();
628     }
629   while (c != '\n');
630
631   gfc_advance_line ();
632 }
633
634
635 int
636 gfc_define_undef_line (void)
637 {
638   char *tmp;
639
640   /* All lines beginning with '#' are either #define or #undef.  */
641   if (debug_info_level != DINFO_LEVEL_VERBOSE || gfc_peek_ascii_char () != '#')
642     return 0;
643
644   if (wide_strncmp (gfc_current_locus.nextc, "#define ", 8) == 0)
645     {
646       tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[8], -1);
647       (*debug_hooks->define) (gfc_linebuf_linenum (gfc_current_locus.lb),
648                               tmp);
649       gfc_free (tmp);
650     }
651
652   if (wide_strncmp (gfc_current_locus.nextc, "#undef ", 7) == 0)
653     {
654       tmp = gfc_widechar_to_char (&gfc_current_locus.nextc[7], -1);
655       (*debug_hooks->undef) (gfc_linebuf_linenum (gfc_current_locus.lb),
656                              tmp);
657       gfc_free (tmp);
658     }
659
660   /* Skip the rest of the line.  */
661   skip_comment_line ();
662
663   return 1;
664 }
665
666
667 /* Return true if GCC$ was matched.  */
668 static bool
669 skip_gcc_attribute (locus start)
670 {
671   bool r = false;
672   char c;
673   locus old_loc = gfc_current_locus;
674
675   if ((c = next_char ()) == 'g' || c == 'G')
676     if ((c = next_char ()) == 'c' || c == 'C')
677       if ((c = next_char ()) == 'c' || c == 'C')
678         if ((c = next_char ()) == '$')
679           r = true;
680
681   if (r == false)
682     gfc_current_locus = old_loc;
683   else
684    {
685       gcc_attribute_flag = 1;
686       gcc_attribute_locus = old_loc;
687       gfc_current_locus = start;
688    }
689
690   return r;
691 }
692
693
694
695 /* Comment lines are null lines, lines containing only blanks or lines
696    on which the first nonblank line is a '!'.
697    Return true if !$ openmp conditional compilation sentinel was
698    seen.  */
699
700 static bool
701 skip_free_comments (void)
702 {
703   locus start;
704   gfc_char_t c;
705   int at_bol;
706
707   for (;;)
708     {
709       at_bol = gfc_at_bol ();
710       start = gfc_current_locus;
711       if (gfc_at_eof ())
712         break;
713
714       do
715         c = next_char ();
716       while (gfc_is_whitespace (c));
717
718       if (c == '\n')
719         {
720           gfc_advance_line ();
721           continue;
722         }
723
724       if (c == '!')
725         {
726           /* Keep the !GCC$ line.  */
727                   if (at_bol && skip_gcc_attribute (start))
728             return false;
729
730           /* If -fopenmp, we need to handle here 2 things:
731              1) don't treat !$omp as comments, but directives
732              2) handle OpenMP conditional compilation, where
733                 !$ should be treated as 2 spaces (for initial lines
734                 only if followed by space).  */
735           if (gfc_option.flag_openmp && at_bol)
736             {
737               locus old_loc = gfc_current_locus;
738               if (next_char () == '$')
739                 {
740                   c = next_char ();
741                   if (c == 'o' || c == 'O')
742                     {
743                       if (((c = next_char ()) == 'm' || c == 'M')
744                           && ((c = next_char ()) == 'p' || c == 'P'))
745                         {
746                           if ((c = next_char ()) == ' ' || c == '\t'
747                               || continue_flag)
748                             {
749                               while (gfc_is_whitespace (c))
750                                 c = next_char ();
751                               if (c != '\n' && c != '!')
752                                 {
753                                   openmp_flag = 1;
754                                   openmp_locus = old_loc;
755                                   gfc_current_locus = start;
756                                   return false;
757                                 }
758                             }
759                           else
760                             gfc_warning_now ("!$OMP at %C starts a commented "
761                                              "line as it neither is followed "
762                                              "by a space nor is a "
763                                              "continuation line");
764                         }
765                       gfc_current_locus = old_loc;
766                       next_char ();
767                       c = next_char ();
768                     }
769                   if (continue_flag || c == ' ' || c == '\t')
770                     {
771                       gfc_current_locus = old_loc;
772                       next_char ();
773                       openmp_flag = 0;
774                       return true;
775                     }
776                 }
777               gfc_current_locus = old_loc;
778             }
779           skip_comment_line ();
780           continue;
781         }
782
783       break;
784     }
785
786   if (openmp_flag && at_bol)
787     openmp_flag = 0;
788
789   gcc_attribute_flag = 0;
790   gfc_current_locus = start;
791   return false;
792 }
793
794
795 /* Skip comment lines in fixed source mode.  We have the same rules as
796    in skip_free_comment(), except that we can have a 'c', 'C' or '*'
797    in column 1, and a '!' cannot be in column 6.  Also, we deal with
798    lines with 'd' or 'D' in column 1, if the user requested this.  */
799
800 static void
801 skip_fixed_comments (void)
802 {
803   locus start;
804   int col;
805   gfc_char_t c;
806
807   if (! gfc_at_bol ())
808     {
809       start = gfc_current_locus;
810       if (! gfc_at_eof ())
811         {
812           do
813             c = next_char ();
814           while (gfc_is_whitespace (c));
815
816           if (c == '\n')
817             gfc_advance_line ();
818           else if (c == '!')
819             skip_comment_line ();
820         }
821
822       if (! gfc_at_bol ())
823         {
824           gfc_current_locus = start;
825           return;
826         }
827     }
828
829   for (;;)
830     {
831       start = gfc_current_locus;
832       if (gfc_at_eof ())
833         break;
834
835       c = next_char ();
836       if (c == '\n')
837         {
838           gfc_advance_line ();
839           continue;
840         }
841
842       if (c == '!' || c == 'c' || c == 'C' || c == '*')
843         {
844           if (skip_gcc_attribute (start))
845             {
846               /* Canonicalize to *$omp.  */
847               *start.nextc = '*';
848               return;
849             }
850
851           /* If -fopenmp, we need to handle here 2 things:
852              1) don't treat !$omp|c$omp|*$omp as comments, but directives
853              2) handle OpenMP conditional compilation, where
854                 !$|c$|*$ should be treated as 2 spaces if the characters
855                 in columns 3 to 6 are valid fixed form label columns
856                 characters.  */
857           if (gfc_current_locus.lb != NULL
858               && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
859             continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
860
861           if (gfc_option.flag_openmp)
862             {
863               if (next_char () == '$')
864                 {
865                   c = next_char ();
866                   if (c == 'o' || c == 'O')
867                     {
868                       if (((c = next_char ()) == 'm' || c == 'M')
869                           && ((c = next_char ()) == 'p' || c == 'P'))
870                         {
871                           c = next_char ();
872                           if (c != '\n'
873                               && ((openmp_flag && continue_flag)
874                                   || c == ' ' || c == '\t' || c == '0'))
875                             {
876                               do
877                                 c = next_char ();
878                               while (gfc_is_whitespace (c));
879                               if (c != '\n' && c != '!')
880                                 {
881                                   /* Canonicalize to *$omp.  */
882                                   *start.nextc = '*';
883                                   openmp_flag = 1;
884                                   gfc_current_locus = start;
885                                   return;
886                                 }
887                             }
888                         }
889                     }
890                   else
891                     {
892                       int digit_seen = 0;
893
894                       for (col = 3; col < 6; col++, c = next_char ())
895                         if (c == ' ')
896                           continue;
897                         else if (c == '\t')
898                           {
899                             col = 6;
900                             break;
901                           }
902                         else if (c < '0' || c > '9')
903                           break;
904                         else
905                           digit_seen = 1;
906
907                       if (col == 6 && c != '\n'
908                           && ((continue_flag && !digit_seen)
909                               || c == ' ' || c == '\t' || c == '0'))
910                         {
911                           gfc_current_locus = start;
912                           start.nextc[0] = ' ';
913                           start.nextc[1] = ' ';
914                           continue;
915                         }
916                     }
917                 }
918               gfc_current_locus = start;
919             }
920           skip_comment_line ();
921           continue;
922         }
923
924       if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
925         {
926           if (gfc_option.flag_d_lines == 0)
927             {
928               skip_comment_line ();
929               continue;
930             }
931           else
932             *start.nextc = c = ' ';
933         }
934
935       col = 1;
936
937       while (gfc_is_whitespace (c))
938         {
939           c = next_char ();
940           col++;
941         }
942
943       if (c == '\n')
944         {
945           gfc_advance_line ();
946           continue;
947         }
948
949       if (col != 6 && c == '!')
950         {
951           if (gfc_current_locus.lb != NULL
952               && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
953             continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
954           skip_comment_line ();
955           continue;
956         }
957
958       break;
959     }
960
961   openmp_flag = 0;
962   gcc_attribute_flag = 0;
963   gfc_current_locus = start;
964 }
965
966
967 /* Skips the current line if it is a comment.  */
968
969 void
970 gfc_skip_comments (void)
971 {
972   if (gfc_current_form == FORM_FREE)
973     skip_free_comments ();
974   else
975     skip_fixed_comments ();
976 }
977
978
979 /* Get the next character from the input, taking continuation lines
980    and end-of-line comments into account.  This implies that comment
981    lines between continued lines must be eaten here.  For higher-level
982    subroutines, this flattens continued lines into a single logical
983    line.  The in_string flag denotes whether we're inside a character
984    context or not.  */
985
986 gfc_char_t
987 gfc_next_char_literal (int in_string)
988 {
989   locus old_loc;
990   int i, prev_openmp_flag;
991   gfc_char_t c;
992
993   continue_flag = 0;
994
995 restart:
996   c = next_char ();
997   if (gfc_at_end ())
998     {
999       continue_count = 0;
1000       return c;
1001     }
1002
1003   if (gfc_current_form == FORM_FREE)
1004     {
1005       bool openmp_cond_flag;
1006
1007       if (!in_string && c == '!')
1008         {
1009           if (gcc_attribute_flag
1010               && memcmp (&gfc_current_locus, &gcc_attribute_locus,
1011                  sizeof (gfc_current_locus)) == 0)
1012             goto done;
1013
1014           if (openmp_flag
1015               && memcmp (&gfc_current_locus, &openmp_locus,
1016                  sizeof (gfc_current_locus)) == 0)
1017             goto done;
1018
1019           /* This line can't be continued */
1020           do
1021             {
1022               c = next_char ();
1023             }
1024           while (c != '\n');
1025
1026           /* Avoid truncation warnings for comment ending lines.  */
1027           gfc_current_locus.lb->truncated = 0;
1028
1029           goto done;
1030         }
1031
1032       if (c != '&')
1033         goto done;
1034
1035       /* If the next nonblank character is a ! or \n, we've got a
1036          continuation line.  */
1037       old_loc = gfc_current_locus;
1038
1039       c = next_char ();
1040       while (gfc_is_whitespace (c))
1041         c = next_char ();
1042
1043       /* Character constants to be continued cannot have commentary
1044          after the '&'.  */
1045
1046       if (in_string && c != '\n')
1047         {
1048           gfc_current_locus = old_loc;
1049           c = '&';
1050           goto done;
1051         }
1052
1053       if (c != '!' && c != '\n')
1054         {
1055           gfc_current_locus = old_loc;
1056           c = '&';
1057           goto done;
1058         }
1059
1060       prev_openmp_flag = openmp_flag;
1061       continue_flag = 1;
1062       if (c == '!')
1063         skip_comment_line ();
1064       else
1065         gfc_advance_line ();
1066       
1067       if (gfc_at_eof())
1068         goto not_continuation;
1069
1070       /* We've got a continuation line.  If we are on the very next line after
1071          the last continuation, increment the continuation line count and
1072          check whether the limit has been exceeded.  */
1073       if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1074         {
1075           if (++continue_count == gfc_option.max_continue_free)
1076             {
1077               if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1078                 gfc_warning ("Limit of %d continuations exceeded in "
1079                              "statement at %C", gfc_option.max_continue_free);
1080             }
1081         }
1082
1083       /* Now find where it continues. First eat any comment lines.  */
1084       openmp_cond_flag = skip_free_comments ();
1085
1086       if (gfc_current_locus.lb != NULL
1087           && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1088         continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1089
1090       if (prev_openmp_flag != openmp_flag)
1091         {
1092           gfc_current_locus = old_loc;
1093           openmp_flag = prev_openmp_flag;
1094           c = '&';
1095           goto done;
1096         }
1097
1098       /* Now that we have a non-comment line, probe ahead for the
1099          first non-whitespace character.  If it is another '&', then
1100          reading starts at the next character, otherwise we must back
1101          up to where the whitespace started and resume from there.  */
1102
1103       old_loc = gfc_current_locus;
1104
1105       c = next_char ();
1106       while (gfc_is_whitespace (c))
1107         c = next_char ();
1108
1109       if (openmp_flag)
1110         {
1111           for (i = 0; i < 5; i++, c = next_char ())
1112             {
1113               gcc_assert (gfc_wide_tolower (c) == (unsigned char) "!$omp"[i]);
1114               if (i == 4)
1115                 old_loc = gfc_current_locus;
1116             }
1117           while (gfc_is_whitespace (c))
1118             c = next_char ();
1119         }
1120
1121       if (c != '&')
1122         {
1123           if (in_string)
1124             {
1125               if (gfc_option.warn_ampersand)
1126                 gfc_warning_now ("Missing '&' in continued character "
1127                                  "constant at %C");
1128               gfc_current_locus.nextc--;
1129             }
1130           /* Both !$omp and !$ -fopenmp continuation lines have & on the
1131              continuation line only optionally.  */
1132           else if (openmp_flag || openmp_cond_flag)
1133             gfc_current_locus.nextc--;
1134           else
1135             {
1136               c = ' ';
1137               gfc_current_locus = old_loc;
1138               goto done;
1139             }
1140         }
1141     }
1142   else /* Fixed form.  */
1143     {
1144       /* Fixed form continuation.  */
1145       if (!in_string && c == '!')
1146         {
1147           /* Skip comment at end of line.  */
1148           do
1149             {
1150               c = next_char ();
1151             }
1152           while (c != '\n');
1153
1154           /* Avoid truncation warnings for comment ending lines.  */
1155           gfc_current_locus.lb->truncated = 0;
1156         }
1157
1158       if (c != '\n')
1159         goto done;
1160
1161       prev_openmp_flag = openmp_flag;
1162       continue_flag = 1;
1163       old_loc = gfc_current_locus;
1164
1165       gfc_advance_line ();
1166       skip_fixed_comments ();
1167
1168       /* See if this line is a continuation line.  */
1169       if (openmp_flag != prev_openmp_flag)
1170         {
1171           openmp_flag = prev_openmp_flag;
1172           goto not_continuation;
1173         }
1174
1175       if (!openmp_flag)
1176         for (i = 0; i < 5; i++)
1177           {
1178             c = next_char ();
1179             if (c != ' ')
1180               goto not_continuation;
1181           }
1182       else
1183         for (i = 0; i < 5; i++)
1184           {
1185             c = next_char ();
1186             if (gfc_wide_tolower (c) != (unsigned char) "*$omp"[i])
1187               goto not_continuation;
1188           }
1189
1190       c = next_char ();
1191       if (c == '0' || c == ' ' || c == '\n')
1192         goto not_continuation;
1193
1194       /* We've got a continuation line.  If we are on the very next line after
1195          the last continuation, increment the continuation line count and
1196          check whether the limit has been exceeded.  */
1197       if (gfc_linebuf_linenum (gfc_current_locus.lb) == continue_line + 1)
1198         {
1199           if (++continue_count == gfc_option.max_continue_fixed)
1200             {
1201               if (gfc_notification_std (GFC_STD_GNU) || pedantic)
1202                 gfc_warning ("Limit of %d continuations exceeded in "
1203                              "statement at %C",
1204                              gfc_option.max_continue_fixed);
1205             }
1206         }
1207
1208       if (gfc_current_locus.lb != NULL
1209           && continue_line < gfc_linebuf_linenum (gfc_current_locus.lb))
1210         continue_line = gfc_linebuf_linenum (gfc_current_locus.lb);
1211     }
1212
1213   /* Ready to read first character of continuation line, which might
1214      be another continuation line!  */
1215   goto restart;
1216
1217 not_continuation:
1218   c = '\n';
1219   gfc_current_locus = old_loc;
1220
1221 done:
1222   if (c == '\n')
1223     continue_count = 0;
1224   continue_flag = 0;
1225   return c;
1226 }
1227
1228
1229 /* Get the next character of input, folded to lowercase.  In fixed
1230    form mode, we also ignore spaces.  When matcher subroutines are
1231    parsing character literals, they have to call
1232    gfc_next_char_literal().  */
1233
1234 gfc_char_t
1235 gfc_next_char (void)
1236 {
1237   gfc_char_t c;
1238
1239   do
1240     {
1241       c = gfc_next_char_literal (0);
1242     }
1243   while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
1244
1245   return gfc_wide_tolower (c);
1246 }
1247
1248 char
1249 gfc_next_ascii_char (void)
1250 {
1251   gfc_char_t c = gfc_next_char ();
1252
1253   return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1254                                     : (unsigned char) UCHAR_MAX);
1255 }
1256
1257
1258 gfc_char_t
1259 gfc_peek_char (void)
1260 {
1261   locus old_loc;
1262   gfc_char_t c;
1263
1264   old_loc = gfc_current_locus;
1265   c = gfc_next_char ();
1266   gfc_current_locus = old_loc;
1267
1268   return c;
1269 }
1270
1271
1272 char
1273 gfc_peek_ascii_char (void)
1274 {
1275   gfc_char_t c = gfc_peek_char ();
1276
1277   return (gfc_wide_fits_in_byte (c) ? (unsigned char) c
1278                                     : (unsigned char) UCHAR_MAX);
1279 }
1280
1281
1282 /* Recover from an error.  We try to get past the current statement
1283    and get lined up for the next.  The next statement follows a '\n'
1284    or a ';'.  We also assume that we are not within a character
1285    constant, and deal with finding a '\'' or '"'.  */
1286
1287 void
1288 gfc_error_recovery (void)
1289 {
1290   gfc_char_t c, delim;
1291
1292   if (gfc_at_eof ())
1293     return;
1294
1295   for (;;)
1296     {
1297       c = gfc_next_char ();
1298       if (c == '\n' || c == ';')
1299         break;
1300
1301       if (c != '\'' && c != '"')
1302         {
1303           if (gfc_at_eof ())
1304             break;
1305           continue;
1306         }
1307       delim = c;
1308
1309       for (;;)
1310         {
1311           c = next_char ();
1312
1313           if (c == delim)
1314             break;
1315           if (c == '\n')
1316             return;
1317           if (c == '\\')
1318             {
1319               c = next_char ();
1320               if (c == '\n')
1321                 return;
1322             }
1323         }
1324       if (gfc_at_eof ())
1325         break;
1326     }
1327 }
1328
1329
1330 /* Read ahead until the next character to be read is not whitespace.  */
1331
1332 void
1333 gfc_gobble_whitespace (void)
1334 {
1335   static int linenum = 0;
1336   locus old_loc;
1337   gfc_char_t c;
1338
1339   do
1340     {
1341       old_loc = gfc_current_locus;
1342       c = gfc_next_char_literal (0);
1343       /* Issue a warning for nonconforming tabs.  We keep track of the line
1344          number because the Fortran matchers will often back up and the same
1345          line will be scanned multiple times.  */
1346       if (!gfc_option.warn_tabs && c == '\t')
1347         {
1348           int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
1349           if (cur_linenum != linenum)
1350             {
1351               linenum = cur_linenum;
1352               gfc_warning_now ("Nonconforming tab character at %C");
1353             }
1354         }
1355     }
1356   while (gfc_is_whitespace (c));
1357
1358   gfc_current_locus = old_loc;
1359 }
1360
1361
1362 /* Load a single line into pbuf.
1363
1364    If pbuf points to a NULL pointer, it is allocated.
1365    We truncate lines that are too long, unless we're dealing with
1366    preprocessor lines or if the option -ffixed-line-length-none is set,
1367    in which case we reallocate the buffer to fit the entire line, if
1368    need be.
1369    In fixed mode, we expand a tab that occurs within the statement
1370    label region to expand to spaces that leave the next character in
1371    the source region.
1372
1373    If first_char is not NULL, it's a pointer to a single char value holding
1374    the first character of the line, which has already been read by the
1375    caller.  This avoids the use of ungetc().
1376
1377    load_line returns whether the line was truncated.
1378
1379    NOTE: The error machinery isn't available at this point, so we can't
1380          easily report line and column numbers consistent with other 
1381          parts of gfortran.  */
1382
1383 static int
1384 load_line (FILE *input, gfc_char_t **pbuf, int *pbuflen, const int *first_char)
1385 {
1386   static int linenum = 0, current_line = 1;
1387   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1388   int trunc_flag = 0, seen_comment = 0;
1389   int seen_printable = 0, seen_ampersand = 0;
1390   gfc_char_t *buffer;
1391   bool found_tab = false;
1392
1393   /* Determine the maximum allowed line length.  */
1394   if (gfc_current_form == FORM_FREE)
1395     maxlen = gfc_option.free_line_length;
1396   else if (gfc_current_form == FORM_FIXED)
1397     maxlen = gfc_option.fixed_line_length;
1398   else
1399     maxlen = 72;
1400
1401   if (*pbuf == NULL)
1402     {
1403       /* Allocate the line buffer, storing its length into buflen.
1404          Note that if maxlen==0, indicating that arbitrary-length lines
1405          are allowed, the buffer will be reallocated if this length is
1406          insufficient; since 132 characters is the length of a standard
1407          free-form line, we use that as a starting guess.  */
1408       if (maxlen > 0)
1409         buflen = maxlen;
1410       else
1411         buflen = 132;
1412
1413       *pbuf = gfc_get_wide_string (buflen + 1);
1414     }
1415
1416   i = 0;
1417   buffer = *pbuf;
1418
1419   if (first_char)
1420     c = *first_char;
1421   else
1422     c = getc (input);
1423
1424   /* In order to not truncate preprocessor lines, we have to
1425      remember that this is one.  */
1426   preprocessor_flag = (c == '#' ? 1 : 0);
1427
1428   for (;;)
1429     {
1430       if (c == EOF)
1431         break;
1432
1433       if (c == '\n')
1434         {
1435           /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3.  */
1436           if (gfc_current_form == FORM_FREE 
1437               && !seen_printable && seen_ampersand)
1438             {
1439               if (pedantic)
1440                 gfc_error_now ("'&' not allowed by itself in line %d",
1441                                current_line);
1442               else
1443                 gfc_warning_now ("'&' not allowed by itself in line %d",
1444                                  current_line);
1445             }
1446           break;
1447         }
1448
1449       if (c == '\r' || c == '\0')
1450         goto next_char;                 /* Gobble characters.  */
1451
1452       if (c == '&')
1453         {
1454           if (seen_ampersand)
1455             {
1456               seen_ampersand = 0;
1457               seen_printable = 1;
1458             }
1459           else
1460             seen_ampersand = 1;
1461         }
1462
1463       if ((c != '&' && c != '!' && c != ' ') || (c == '!' && !seen_ampersand))
1464         seen_printable = 1;
1465
1466       /* Is this a fixed-form comment?  */
1467       if (gfc_current_form == FORM_FIXED && i == 0
1468           && (c == '*' || c == 'c' || c == 'd'))
1469         seen_comment = 1;
1470
1471       /* Vendor extension: "<tab>1" marks a continuation line.  */
1472       if (found_tab)
1473         {
1474           found_tab = false;
1475           if (c >= '1' && c <= '9')
1476             {
1477               *(buffer-1) = c;
1478               goto next_char;
1479             }
1480         }
1481
1482       if (gfc_current_form == FORM_FIXED && c == '\t' && i < 6)
1483         {
1484           found_tab = true;
1485
1486           if (!gfc_option.warn_tabs && seen_comment == 0
1487               && current_line != linenum)
1488             {
1489               linenum = current_line;
1490               gfc_warning_now ("Nonconforming tab character in column %d "
1491                                "of line %d", i+1, linenum);
1492             }
1493
1494           while (i < 6)
1495             {
1496               *buffer++ = ' ';
1497               i++;
1498             }
1499
1500           goto next_char;
1501         }
1502
1503       *buffer++ = c;
1504       i++;
1505
1506       if (maxlen == 0 || preprocessor_flag)
1507         {
1508           if (i >= buflen)
1509             {
1510               /* Reallocate line buffer to double size to hold the
1511                 overlong line.  */
1512               buflen = buflen * 2;
1513               *pbuf = XRESIZEVEC (gfc_char_t, *pbuf, (buflen + 1));
1514               buffer = (*pbuf) + i;
1515             }
1516         }
1517       else if (i >= maxlen)
1518         {
1519           /* Truncate the rest of the line.  */
1520           for (;;)
1521             {
1522               c = getc (input);
1523               if (c == '\r')
1524                 continue;
1525
1526               if (c == '\n' || c == EOF)
1527                 break;
1528
1529               trunc_flag = 1;
1530             }
1531
1532           c = '\n';
1533           continue;
1534         }
1535
1536 next_char:
1537       c = getc (input);
1538     }
1539
1540   /* Pad lines to the selected line length in fixed form.  */
1541   if (gfc_current_form == FORM_FIXED
1542       && gfc_option.fixed_line_length != 0
1543       && !preprocessor_flag
1544       && c != EOF)
1545     {
1546       while (i++ < maxlen)
1547         *buffer++ = ' ';
1548     }
1549
1550   *buffer = '\0';
1551   *pbuflen = buflen;
1552   current_line++;
1553
1554   return trunc_flag;
1555 }
1556
1557
1558 /* Get a gfc_file structure, initialize it and add it to
1559    the file stack.  */
1560
1561 static gfc_file *
1562 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1563 {
1564   gfc_file *f;
1565
1566   f = XCNEW (gfc_file);
1567
1568   f->filename = xstrdup (name);
1569
1570   f->next = file_head;
1571   file_head = f;
1572
1573   f->up = current_file;
1574   if (current_file != NULL)
1575     f->inclusion_line = current_file->line;
1576
1577   linemap_add (line_table, reason, false, f->filename, 1);
1578
1579   return f;
1580 }
1581
1582
1583 /* Deal with a line from the C preprocessor. The
1584    initial octothorp has already been seen.  */
1585
1586 static void
1587 preprocessor_line (gfc_char_t *c)
1588 {
1589   bool flag[5];
1590   int i, line;
1591   gfc_char_t *wide_filename;
1592   gfc_file *f;
1593   int escaped, unescape;
1594   char *filename;
1595
1596   c++;
1597   while (*c == ' ' || *c == '\t')
1598     c++;
1599
1600   if (*c < '0' || *c > '9')
1601     goto bad_cpp_line;
1602
1603   line = wide_atoi (c);
1604
1605   c = wide_strchr (c, ' ');
1606   if (c == NULL)
1607     {
1608       /* No file name given.  Set new line number.  */
1609       current_file->line = line;
1610       return;
1611     }
1612
1613   /* Skip spaces.  */
1614   while (*c == ' ' || *c == '\t')
1615     c++;
1616
1617   /* Skip quote.  */
1618   if (*c != '"')
1619     goto bad_cpp_line;
1620   ++c;
1621
1622   wide_filename = c;
1623
1624   /* Make filename end at quote.  */
1625   unescape = 0;
1626   escaped = false;
1627   while (*c && ! (!escaped && *c == '"'))
1628     {
1629       if (escaped)
1630         escaped = false;
1631       else if (*c == '\\')
1632         {
1633           escaped = true;
1634           unescape++;
1635         }
1636       ++c;
1637     }
1638
1639   if (! *c)
1640     /* Preprocessor line has no closing quote.  */
1641     goto bad_cpp_line;
1642
1643   *c++ = '\0';
1644
1645   /* Undo effects of cpp_quote_string.  */
1646   if (unescape)
1647     {
1648       gfc_char_t *s = wide_filename;
1649       gfc_char_t *d = gfc_get_wide_string (c - wide_filename - unescape);
1650
1651       wide_filename = d;
1652       while (*s)
1653         {
1654           if (*s == '\\')
1655             *d++ = *++s;
1656           else
1657             *d++ = *s;
1658           s++;
1659         }
1660       *d = '\0';
1661     }
1662
1663   /* Get flags.  */
1664
1665   flag[1] = flag[2] = flag[3] = flag[4] = false;
1666
1667   for (;;)
1668     {
1669       c = wide_strchr (c, ' ');
1670       if (c == NULL)
1671         break;
1672
1673       c++;
1674       i = wide_atoi (c);
1675
1676       if (1 <= i && i <= 4)
1677         flag[i] = true;
1678     }
1679
1680   /* Convert the filename in wide characters into a filename in narrow
1681      characters.  */
1682   filename = gfc_widechar_to_char (wide_filename, -1);
1683
1684   /* Interpret flags.  */
1685
1686   if (flag[1]) /* Starting new file.  */
1687     {
1688       f = get_file (filename, LC_RENAME);
1689       add_file_change (f->filename, f->inclusion_line);
1690       current_file = f;
1691     }
1692
1693   if (flag[2]) /* Ending current file.  */
1694     {
1695       if (!current_file->up
1696           || strcmp (current_file->up->filename, filename) != 0)
1697         {
1698           gfc_warning_now ("%s:%d: file %s left but not entered",
1699                            current_file->filename, current_file->line,
1700                            filename);
1701           if (unescape)
1702             gfc_free (wide_filename);
1703           gfc_free (filename);
1704           return;
1705         }
1706
1707       add_file_change (NULL, line);
1708       current_file = current_file->up;
1709       linemap_add (line_table, LC_RENAME, false, current_file->filename,
1710                    current_file->line);
1711     }
1712
1713   /* The name of the file can be a temporary file produced by
1714      cpp. Replace the name if it is different.  */
1715
1716   if (strcmp (current_file->filename, filename) != 0)
1717     {
1718        /* FIXME: we leak the old filename because a pointer to it may be stored
1719           in the linemap.  Alternative could be using GC or updating linemap to
1720           point to the new name, but there is no API for that currently. */
1721       current_file->filename = xstrdup (filename);
1722     }
1723
1724   /* Set new line number.  */
1725   current_file->line = line;
1726   if (unescape)
1727     gfc_free (wide_filename);
1728   gfc_free (filename);
1729   return;
1730
1731  bad_cpp_line:
1732   gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1733                    current_file->filename, current_file->line);
1734   current_file->line++;
1735 }
1736
1737
1738 static gfc_try load_file (const char *, const char *, bool);
1739
1740 /* include_line()-- Checks a line buffer to see if it is an include
1741    line.  If so, we call load_file() recursively to load the included
1742    file.  We never return a syntax error because a statement like
1743    "include = 5" is perfectly legal.  We return false if no include was
1744    processed or true if we matched an include.  */
1745
1746 static bool
1747 include_line (gfc_char_t *line)
1748 {
1749   gfc_char_t quote, *c, *begin, *stop;
1750   char *filename;
1751
1752   c = line;
1753
1754   if (gfc_option.flag_openmp)
1755     {
1756       if (gfc_current_form == FORM_FREE)
1757         {
1758           while (*c == ' ' || *c == '\t')
1759             c++;
1760           if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1761             c += 3;
1762         }
1763       else
1764         {
1765           if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1766               && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1767             c += 3;
1768         }
1769     }
1770
1771   while (*c == ' ' || *c == '\t')
1772     c++;
1773
1774   if (gfc_wide_strncasecmp (c, "include", 7))
1775     return false;
1776
1777   c += 7;
1778   while (*c == ' ' || *c == '\t')
1779     c++;
1780
1781   /* Find filename between quotes.  */
1782   
1783   quote = *c++;
1784   if (quote != '"' && quote != '\'')
1785     return false;
1786
1787   begin = c;
1788
1789   while (*c != quote && *c != '\0')
1790     c++;
1791
1792   if (*c == '\0')
1793     return false;
1794
1795   stop = c++;
1796   
1797   while (*c == ' ' || *c == '\t')
1798     c++;
1799
1800   if (*c != '\0' && *c != '!')
1801     return false;
1802
1803   /* We have an include line at this point.  */
1804
1805   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1806                    read by anything else.  */
1807
1808   filename = gfc_widechar_to_char (begin, -1);
1809   load_file (filename, NULL, false);
1810   gfc_free (filename);
1811   return true;
1812 }
1813
1814
1815 /* Load a file into memory by calling load_line until the file ends.  */
1816
1817 static gfc_try
1818 load_file (const char *realfilename, const char *displayedname, bool initial)
1819 {
1820   gfc_char_t *line;
1821   gfc_linebuf *b;
1822   gfc_file *f;
1823   FILE *input;
1824   int len, line_len;
1825   bool first_line;
1826   const char *filename;
1827
1828   filename = displayedname ? displayedname : realfilename;
1829
1830   for (f = current_file; f; f = f->up)
1831     if (strcmp (filename, f->filename) == 0)
1832       {
1833         fprintf (stderr, "%s:%d: Error: File '%s' is being included "
1834                  "recursively\n", current_file->filename, current_file->line,
1835                  filename);
1836         return FAILURE;
1837       }
1838
1839   if (initial)
1840     {
1841       if (gfc_src_file)
1842         {
1843           input = gfc_src_file;
1844           gfc_src_file = NULL;
1845         }
1846       else
1847         input = gfc_open_file (realfilename);
1848       if (input == NULL)
1849         {
1850           gfc_error_now ("Can't open file '%s'", filename);
1851           return FAILURE;
1852         }
1853     }
1854   else
1855     {
1856       input = gfc_open_included_file (realfilename, false, false);
1857       if (input == NULL)
1858         {
1859           fprintf (stderr, "%s:%d: Error: Can't open included file '%s'\n",
1860                    current_file->filename, current_file->line, filename);
1861           return FAILURE;
1862         }
1863     }
1864
1865   /* Load the file.  */
1866
1867   f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1868   if (!initial)
1869     add_file_change (f->filename, f->inclusion_line);
1870   current_file = f;
1871   current_file->line = 1;
1872   line = NULL;
1873   line_len = 0;
1874   first_line = true;
1875
1876   if (initial && gfc_src_preprocessor_lines[0])
1877     {
1878       preprocessor_line (gfc_src_preprocessor_lines[0]);
1879       gfc_free (gfc_src_preprocessor_lines[0]);
1880       gfc_src_preprocessor_lines[0] = NULL;
1881       if (gfc_src_preprocessor_lines[1])
1882         {
1883           preprocessor_line (gfc_src_preprocessor_lines[1]);
1884           gfc_free (gfc_src_preprocessor_lines[1]);
1885           gfc_src_preprocessor_lines[1] = NULL;
1886         }
1887     }
1888
1889   for (;;)
1890     {
1891       int trunc = load_line (input, &line, &line_len, NULL);
1892
1893       len = gfc_wide_strlen (line);
1894       if (feof (input) && len == 0)
1895         break;
1896
1897       /* If this is the first line of the file, it can contain a byte
1898          order mark (BOM), which we will ignore:
1899            FF FE is UTF-16 little endian,
1900            FE FF is UTF-16 big endian,
1901            EF BB BF is UTF-8.  */
1902       if (first_line
1903           && ((line_len >= 2 && line[0] == (unsigned char) '\xFF'
1904                              && line[1] == (unsigned char) '\xFE')
1905               || (line_len >= 2 && line[0] == (unsigned char) '\xFE'
1906                                 && line[1] == (unsigned char) '\xFF')
1907               || (line_len >= 3 && line[0] == (unsigned char) '\xEF'
1908                                 && line[1] == (unsigned char) '\xBB'
1909                                 && line[2] == (unsigned char) '\xBF')))
1910         {
1911           int n = line[1] == (unsigned char) '\xBB' ? 3 : 2;
1912           gfc_char_t *new_char = gfc_get_wide_string (line_len);
1913
1914           wide_strcpy (new_char, &line[n]);
1915           gfc_free (line);
1916           line = new_char;
1917           len -= n;
1918         }
1919
1920       /* There are three things this line can be: a line of Fortran
1921          source, an include line or a C preprocessor directive.  */
1922
1923       if (line[0] == '#')
1924         {
1925           /* When -g3 is specified, it's possible that we emit #define
1926              and #undef lines, which we need to pass to the middle-end
1927              so that it can emit correct debug info.  */
1928           if (debug_info_level == DINFO_LEVEL_VERBOSE
1929               && (wide_strncmp (line, "#define ", 8) == 0
1930                   || wide_strncmp (line, "#undef ", 7) == 0))
1931             ;
1932           else
1933             {
1934               preprocessor_line (line);
1935               continue;
1936             }
1937         }
1938
1939       /* Preprocessed files have preprocessor lines added before the byte
1940          order mark, so first_line is not about the first line of the file
1941          but the first line that's not a preprocessor line.  */
1942       first_line = false;
1943
1944       if (include_line (line))
1945         {
1946           current_file->line++;
1947           continue;
1948         }
1949
1950       /* Add line.  */
1951
1952       b = (gfc_linebuf *) gfc_getmem (gfc_linebuf_header_size
1953                                       + (len + 1) * sizeof (gfc_char_t));
1954
1955       b->location
1956         = linemap_line_start (line_table, current_file->line++, 120);
1957       b->file = current_file;
1958       b->truncated = trunc;
1959       wide_strcpy (b->line, line);
1960
1961       if (line_head == NULL)
1962         line_head = b;
1963       else
1964         line_tail->next = b;
1965
1966       line_tail = b;
1967
1968       while (file_changes_cur < file_changes_count)
1969         file_changes[file_changes_cur++].lb = b;
1970     }
1971
1972   /* Release the line buffer allocated in load_line.  */
1973   gfc_free (line);
1974
1975   fclose (input);
1976
1977   if (!initial)
1978     add_file_change (NULL, current_file->inclusion_line + 1);
1979   current_file = current_file->up;
1980   linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
1981   return SUCCESS;
1982 }
1983
1984
1985 /* Open a new file and start scanning from that file. Returns SUCCESS
1986    if everything went OK, FAILURE otherwise.  If form == FORM_UNKNOWN
1987    it tries to determine the source form from the filename, defaulting
1988    to free form.  */
1989
1990 gfc_try
1991 gfc_new_file (void)
1992 {
1993   gfc_try result;
1994
1995   if (gfc_cpp_enabled ())
1996     {
1997       result = gfc_cpp_preprocess (gfc_source_file);
1998       if (!gfc_cpp_preprocess_only ())
1999         result = load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
2000     }
2001   else
2002     result = load_file (gfc_source_file, NULL, true);
2003
2004   gfc_current_locus.lb = line_head;
2005   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
2006
2007 #if 0 /* Debugging aid.  */
2008   for (; line_head; line_head = line_head->next)
2009     printf ("%s:%3d %s\n", LOCATION_FILE (line_head->location),
2010             LOCATION_LINE (line_head->location), line_head->line);
2011
2012   exit (0);
2013 #endif
2014
2015   return result;
2016 }
2017
2018 static char *
2019 unescape_filename (const char *ptr)
2020 {
2021   const char *p = ptr, *s;
2022   char *d, *ret;
2023   int escaped, unescape = 0;
2024
2025   /* Make filename end at quote.  */
2026   escaped = false;
2027   while (*p && ! (! escaped && *p == '"'))
2028     {
2029       if (escaped)
2030         escaped = false;
2031       else if (*p == '\\')
2032         {
2033           escaped = true;
2034           unescape++;
2035         }
2036       ++p;
2037     }
2038
2039   if (!*p || p[1])
2040     return NULL;
2041
2042   /* Undo effects of cpp_quote_string.  */
2043   s = ptr;
2044   d = XCNEWVEC (char, p + 1 - ptr - unescape);
2045   ret = d;
2046
2047   while (s != p)
2048     {
2049       if (*s == '\\')
2050         *d++ = *++s;
2051       else
2052         *d++ = *s;
2053       s++;
2054     }
2055   *d = '\0';
2056   return ret;
2057 }
2058
2059 /* For preprocessed files, if the first tokens are of the form # NUM.
2060    handle the directives so we know the original file name.  */
2061
2062 const char *
2063 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
2064 {
2065   int c, len;
2066   char *dirname, *tmp;
2067
2068   gfc_src_file = gfc_open_file (filename);
2069   if (gfc_src_file == NULL)
2070     return NULL;
2071
2072   c = getc (gfc_src_file);
2073
2074   if (c != '#')
2075     return NULL;
2076
2077   len = 0;
2078   load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len, &c);
2079
2080   if (wide_strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
2081     return NULL;
2082
2083   tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[0][5], -1);
2084   filename = unescape_filename (tmp);
2085   gfc_free (tmp);
2086   if (filename == NULL)
2087     return NULL;
2088
2089   c = getc (gfc_src_file);
2090
2091   if (c != '#')
2092     return filename;
2093
2094   len = 0;
2095   load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len, &c);
2096
2097   if (wide_strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
2098     return filename;
2099
2100   tmp = gfc_widechar_to_char (&gfc_src_preprocessor_lines[1][5], -1);
2101   dirname = unescape_filename (tmp);
2102   gfc_free (tmp);
2103   if (dirname == NULL)
2104     return filename;
2105
2106   len = strlen (dirname);
2107   if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
2108     {
2109       gfc_free (dirname);
2110       return filename;
2111     }
2112   dirname[len - 2] = '\0';
2113   set_src_pwd (dirname);
2114
2115   if (! IS_ABSOLUTE_PATH (filename))
2116     {
2117       char *p = XCNEWVEC (char, len + strlen (filename));
2118
2119       memcpy (p, dirname, len - 2);
2120       p[len - 2] = '/';
2121       strcpy (p + len - 1, filename);
2122       *canon_source_file = p;
2123     }
2124
2125   gfc_free (dirname);
2126   return filename;
2127 }