OSDN Git Service

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