OSDN Git Service

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