OSDN Git Service

* simplify.c: Update copyright to 2007.
[pf3gnuchains/gcc-fork.git] / gcc / fortran / scanner.c
1 /* Character scanner.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* Set of subroutines to (ultimately) return the next character to the
24    various matching subroutines.  This file's job is to read files and
25    build up lines that are parsed by the parser.  This means that we
26    handle continuation lines and "include" lines.
27
28    The first thing the scanner does is to load an entire file into
29    memory.  We load the entire file into memory for a couple reasons.
30    The first is that we want to be able to deal with nonseekable input
31    (pipes, stdin) and there is a lot of backing up involved during
32    parsing.
33
34    The second is that we want to be able to print the locus of errors,
35    and an error on line 999999 could conflict with something on line
36    one.  Given nonseekable input, we've got to store the whole thing.
37
38    One thing that helps are the column truncation limits that give us
39    an upper bound on the size of individual lines.  We don't store the
40    truncated stuff.
41
42    From the scanner's viewpoint, the higher level subroutines ask for
43    new characters and do a lot of jumping backwards.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "gfortran.h"
48 #include "toplev.h"
49
50 /* Structure for holding module and include file search path.  */
51 typedef struct gfc_directorylist
52 {
53   char *path;
54   bool use_for_modules;
55   struct gfc_directorylist *next;
56 }
57 gfc_directorylist;
58
59 /* List of include file search directories.  */
60 static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
61
62 static gfc_file *file_head, *current_file;
63
64 static int continue_flag, end_flag, openmp_flag;
65 static int continue_count, continue_line;
66 static locus openmp_locus;
67
68 gfc_source_form gfc_current_form;
69 static gfc_linebuf *line_head, *line_tail;
70        
71 locus gfc_current_locus;
72 const char *gfc_source_file;
73 static FILE *gfc_src_file;
74 static char *gfc_src_preprocessor_lines[2];
75
76 extern int pedantic;
77
78 /* Main scanner initialization.  */
79
80 void
81 gfc_scanner_init_1 (void)
82 {
83   file_head = NULL;
84   line_head = NULL;
85   line_tail = NULL;
86
87   continue_count = 0;
88   continue_line = 0;
89
90   end_flag = 0;
91 }
92
93
94 /* Main scanner destructor.  */
95
96 void
97 gfc_scanner_done_1 (void)
98 {
99   gfc_linebuf *lb;
100   gfc_file *f;
101
102   while(line_head != NULL) 
103     {
104       lb = line_head->next;
105       gfc_free(line_head);
106       line_head = lb;
107     }
108      
109   while(file_head != NULL) 
110     {
111       f = file_head->next;
112       gfc_free(file_head->filename);
113       gfc_free(file_head);
114       file_head = f;    
115     }
116
117 }
118
119
120 /* Adds path to the list pointed to by list.  */
121
122 static void
123 add_path_to_list (gfc_directorylist **list, const char *path,
124                   bool use_for_modules)
125 {
126   gfc_directorylist *dir;
127   const char *p;
128
129   p = path;
130   while (*p == ' ' || *p == '\t')  /* someone might do "-I include" */
131     if (*p++ == '\0')
132       return;
133
134   dir = *list;
135   if (!dir)
136     dir = *list = gfc_getmem (sizeof (gfc_directorylist));
137   else
138     {
139       while (dir->next)
140         dir = dir->next;
141
142       dir->next = gfc_getmem (sizeof (gfc_directorylist));
143       dir = dir->next;
144     }
145
146   dir->next = NULL;
147   dir->use_for_modules = use_for_modules;
148   dir->path = gfc_getmem (strlen (p) + 2);
149   strcpy (dir->path, p);
150   strcat (dir->path, "/");      /* make '/' last character */
151 }
152
153
154 void
155 gfc_add_include_path (const char *path, bool use_for_modules)
156 {
157   add_path_to_list (&include_dirs, path, use_for_modules);
158 }
159
160
161 void
162 gfc_add_intrinsic_modules_path (const char *path)
163 {
164   add_path_to_list (&intrinsic_modules_dirs, path, true);
165 }
166
167
168 /* Release resources allocated for options.  */
169
170 void
171 gfc_release_include_path (void)
172 {
173   gfc_directorylist *p;
174
175   while (include_dirs != NULL)
176     {
177       p = include_dirs;
178       include_dirs = include_dirs->next;
179       gfc_free (p->path);
180       gfc_free (p);
181     }
182
183   while (intrinsic_modules_dirs != NULL)
184     {
185       p = intrinsic_modules_dirs;
186       intrinsic_modules_dirs = intrinsic_modules_dirs->next;
187       gfc_free (p->path);
188       gfc_free (p);
189     }
190
191   gfc_free (gfc_option.module_dir);
192 }
193
194
195 static FILE *
196 open_included_file (const char *name, gfc_directorylist *list, bool module)
197 {
198   char *fullname;
199   gfc_directorylist *p;
200   FILE *f;
201
202   for (p = list; p; p = p->next)
203     {
204       if (module && !p->use_for_modules)
205         continue;
206
207       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
208       strcpy (fullname, p->path);
209       strcat (fullname, name);
210
211       f = gfc_open_file (fullname);
212       if (f != NULL)
213         return f;
214     }
215
216   return NULL;
217 }
218
219
220 /* Opens file for reading, searching through the include directories
221    given if necessary.  If the include_cwd argument is true, we try
222    to open the file in the current directory first.  */
223
224 FILE *
225 gfc_open_included_file (const char *name, bool include_cwd, bool module)
226 {
227   FILE *f;
228
229   if (IS_ABSOLUTE_PATH (name))
230     return gfc_open_file (name);
231
232   if (include_cwd)
233     {
234       f = gfc_open_file (name);
235       if (f != NULL)
236         return f;
237     }
238
239   return open_included_file (name, include_dirs, module);
240 }
241
242 FILE *
243 gfc_open_intrinsic_module (const char *name)
244 {
245   if (IS_ABSOLUTE_PATH (name))
246     return gfc_open_file (name);
247
248   return open_included_file (name, intrinsic_modules_dirs, true);
249 }
250
251 /* Test to see if we're at the end of the main source file.  */
252
253 int
254 gfc_at_end (void)
255 {
256
257   return end_flag;
258 }
259
260
261 /* Test to see if we're at the end of the current file.  */
262
263 int
264 gfc_at_eof (void)
265 {
266
267   if (gfc_at_end ())
268     return 1;
269
270   if (line_head == NULL)
271     return 1;                   /* Null file */
272
273   if (gfc_current_locus.lb == NULL)
274     return 1;
275
276   return 0;
277 }
278
279
280 /* Test to see if we're at the beginning of a new line.  */
281
282 int
283 gfc_at_bol (void)
284 {
285   if (gfc_at_eof ())
286     return 1;
287
288   return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
289 }
290
291
292 /* Test to see if we're at the end of a line.  */
293
294 int
295 gfc_at_eol (void)
296 {
297
298   if (gfc_at_eof ())
299     return 1;
300
301   return (*gfc_current_locus.nextc == '\0');
302 }
303
304
305 /* Advance the current line pointer to the next line.  */
306
307 void
308 gfc_advance_line (void)
309 {
310   if (gfc_at_end ())
311     return;
312
313   if (gfc_current_locus.lb == NULL) 
314     {
315       end_flag = 1;
316       return;
317     } 
318
319   gfc_current_locus.lb = gfc_current_locus.lb->next;
320
321   if (gfc_current_locus.lb != NULL)         
322     gfc_current_locus.nextc = gfc_current_locus.lb->line;
323   else 
324     {
325       gfc_current_locus.nextc = NULL;
326       end_flag = 1;
327     }       
328 }
329
330
331 /* Get the next character from the input, advancing gfc_current_file's
332    locus.  When we hit the end of the line or the end of the file, we
333    start returning a '\n' in order to complete the current statement.
334    No Fortran line conventions are implemented here.
335
336    Requiring explicit advances to the next line prevents the parse
337    pointer from being on the wrong line if the current statement ends
338    prematurely.  */
339
340 static int
341 next_char (void)
342 {
343   int c;
344   
345   if (gfc_current_locus.nextc == NULL)
346     return '\n';
347
348   c = *gfc_current_locus.nextc++;
349   if (c == '\0')
350     {
351       gfc_current_locus.nextc--; /* Remain on this line.  */
352       c = '\n';
353     }
354
355   return c;
356 }
357
358 /* Skip a comment.  When we come here the parse pointer is positioned
359    immediately after the comment character.  If we ever implement
360    compiler directives withing comments, here is where we parse the
361    directive.  */
362
363 static void
364 skip_comment_line (void)
365 {
366   char c;
367
368   do
369     {
370       c = next_char ();
371     }
372   while (c != '\n');
373
374   gfc_advance_line ();
375 }
376
377
378 /* Comment lines are null lines, lines containing only blanks or lines
379    on which the first nonblank line is a '!'.
380    Return true if !$ openmp conditional compilation sentinel was
381    seen.  */
382
383 static bool
384 skip_free_comments (void)
385 {
386   locus start;
387   char c;
388   int at_bol;
389
390   for (;;)
391     {
392       at_bol = gfc_at_bol ();
393       start = gfc_current_locus;
394       if (gfc_at_eof ())
395         break;
396
397       do
398         c = next_char ();
399       while (gfc_is_whitespace (c));
400
401       if (c == '\n')
402         {
403           gfc_advance_line ();
404           continue;
405         }
406
407       if (c == '!')
408         {
409           /* If -fopenmp, we need to handle here 2 things:
410              1) don't treat !$omp as comments, but directives
411              2) handle OpenMP conditional compilation, where
412                 !$ should be treated as 2 spaces (for initial lines
413                 only if followed by space).  */
414           if (gfc_option.flag_openmp && at_bol)
415             {
416               locus old_loc = gfc_current_locus;
417               if (next_char () == '$')
418                 {
419                   c = next_char ();
420                   if (c == 'o' || c == 'O')
421                     {
422                       if (((c = next_char ()) == 'm' || c == 'M')
423                           && ((c = next_char ()) == 'p' || c == 'P')
424                           && ((c = next_char ()) == ' ' || continue_flag))
425                         {
426                           while (gfc_is_whitespace (c))
427                             c = next_char ();
428                           if (c != '\n' && c != '!')
429                             {
430                               openmp_flag = 1;
431                               openmp_locus = old_loc;
432                               gfc_current_locus = start;
433                               return false;
434                             }
435                         }
436                       gfc_current_locus = old_loc;
437                       next_char ();
438                       c = next_char ();
439                     }
440                   if (continue_flag || c == ' ')
441                     {
442                       gfc_current_locus = old_loc;
443                       next_char ();
444                       openmp_flag = 0;
445                       return true;
446                     }
447                 }
448               gfc_current_locus = old_loc;
449             }
450           skip_comment_line ();
451           continue;
452         }
453
454       break;
455     }
456
457   if (openmp_flag && at_bol)
458     openmp_flag = 0;
459   gfc_current_locus = start;
460   return false;
461 }
462
463
464 /* Skip comment lines in fixed source mode.  We have the same rules as
465    in skip_free_comment(), except that we can have a 'c', 'C' or '*'
466    in column 1, and a '!' cannot be in column 6.  Also, we deal with
467    lines with 'd' or 'D' in column 1, if the user requested this.  */
468
469 static void
470 skip_fixed_comments (void)
471 {
472   locus start;
473   int col;
474   char c;
475
476   if (! gfc_at_bol ())
477     {
478       start = gfc_current_locus;
479       if (! gfc_at_eof ())
480         {
481           do
482             c = next_char ();
483           while (gfc_is_whitespace (c));
484
485           if (c == '\n')
486             gfc_advance_line ();
487           else if (c == '!')
488             skip_comment_line ();
489         }
490
491       if (! gfc_at_bol ())
492         {
493           gfc_current_locus = start;
494           return;
495         }
496     }
497
498   for (;;)
499     {
500       start = gfc_current_locus;
501       if (gfc_at_eof ())
502         break;
503
504       c = next_char ();
505       if (c == '\n')
506         {
507           gfc_advance_line ();
508           continue;
509         }
510
511       if (c == '!' || c == 'c' || c == 'C' || c == '*')
512         {
513           /* If -fopenmp, we need to handle here 2 things:
514              1) don't treat !$omp|c$omp|*$omp as comments, but directives
515              2) handle OpenMP conditional compilation, where
516                 !$|c$|*$ should be treated as 2 spaces if the characters
517                 in columns 3 to 6 are valid fixed form label columns
518                 characters.  */
519           if (gfc_option.flag_openmp)
520             {
521               if (next_char () == '$')
522                 {
523                   c = next_char ();
524                   if (c == 'o' || c == 'O')
525                     {
526                       if (((c = next_char ()) == 'm' || c == 'M')
527                           && ((c = next_char ()) == 'p' || c == 'P'))
528                         {
529                           c = next_char ();
530                           if (c != '\n'
531                               && ((openmp_flag && continue_flag)
532                                   || c == ' ' || c == '0'))
533                             {
534                               c = next_char ();
535                               while (gfc_is_whitespace (c))
536                                 c = next_char ();
537                               if (c != '\n' && c != '!')
538                                 {
539                                   /* Canonicalize to *$omp.  */
540                                   *start.nextc = '*';
541                                   openmp_flag = 1;
542                                   gfc_current_locus = start;
543                                   return;
544                                 }
545                             }
546                         }
547                     }
548                   else
549                     {
550                       int digit_seen = 0;
551
552                       for (col = 3; col < 6; col++, c = next_char ())
553                         if (c == ' ')
554                           continue;
555                         else if (c < '0' || c > '9')
556                           break;
557                         else
558                           digit_seen = 1;
559
560                       if (col == 6 && c != '\n'
561                           && ((continue_flag && !digit_seen)
562                               || c == ' ' || c == '0'))
563                         {
564                           gfc_current_locus = start;
565                           start.nextc[0] = ' ';
566                           start.nextc[1] = ' ';
567                           continue;
568                         }
569                     }
570                 }
571               gfc_current_locus = start;
572             }
573           skip_comment_line ();
574           continue;
575         }
576
577       if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
578         {
579           if (gfc_option.flag_d_lines == 0)
580             {
581               skip_comment_line ();
582               continue;
583             }
584           else
585             *start.nextc = c = ' ';
586         }
587
588       col = 1;
589
590       while (gfc_is_whitespace (c))
591         {
592           c = next_char ();
593           col++;
594         }
595
596       if (c == '\n')
597         {
598           gfc_advance_line ();
599           continue;
600         }
601
602       if (col != 6 && c == '!')
603         {
604           skip_comment_line ();
605           continue;
606         }
607
608       break;
609     }
610
611   openmp_flag = 0;
612   gfc_current_locus = start;
613 }
614
615
616 /* Skips the current line if it is a comment.  */
617
618 void
619 gfc_skip_comments (void)
620 {
621   if (gfc_current_form == FORM_FREE)
622     skip_free_comments ();
623   else
624     skip_fixed_comments ();
625 }
626
627
628 /* Get the next character from the input, taking continuation lines
629    and end-of-line comments into account.  This implies that comment
630    lines between continued lines must be eaten here.  For higher-level
631    subroutines, this flattens continued lines into a single logical
632    line.  The in_string flag denotes whether we're inside a character
633    context or not.  */
634
635 int
636 gfc_next_char_literal (int in_string)
637 {
638   locus old_loc;
639   int i, c, prev_openmp_flag;
640
641   continue_flag = 0;
642
643 restart:
644   c = next_char ();
645   if (gfc_at_end ())
646     {
647       continue_count = 0;
648       return c;
649     }
650
651   if (gfc_current_form == FORM_FREE)
652     {
653       bool openmp_cond_flag;
654
655       if (!in_string && c == '!')
656         {
657           if (openmp_flag
658               && memcmp (&gfc_current_locus, &openmp_locus,
659                  sizeof (gfc_current_locus)) == 0)
660             goto done;
661
662           /* This line can't be continued */
663           do
664             {
665               c = next_char ();
666             }
667           while (c != '\n');
668
669           /* Avoid truncation warnings for comment ending lines.  */
670           gfc_current_locus.lb->truncated = 0;
671
672           goto done;
673         }
674
675       if (c != '&')
676         goto done;
677
678       /* If the next nonblank character is a ! or \n, we've got a
679          continuation line.  */
680       old_loc = gfc_current_locus;
681
682       c = next_char ();
683       while (gfc_is_whitespace (c))
684         c = next_char ();
685
686       /* Character constants to be continued cannot have commentary
687          after the '&'.  */
688
689       if (in_string && c != '\n')
690         {
691           gfc_current_locus = old_loc;
692           c = '&';
693           goto done;
694         }
695
696       if (c != '!' && c != '\n')
697         {
698           gfc_current_locus = old_loc;
699           c = '&';
700           goto done;
701         }
702
703       prev_openmp_flag = openmp_flag;
704       continue_flag = 1;
705       if (c == '!')
706         skip_comment_line ();
707       else
708         gfc_advance_line ();
709
710       /* We've got a continuation line.  If we are on the very next line after
711          the last continuation, increment the continuation line count and
712          check whether the limit has been exceeded.  */
713       if (gfc_current_locus.lb->linenum == continue_line + 1)
714         {
715           if (++continue_count == gfc_option.max_continue_free)
716             {
717               if (gfc_notification_std (GFC_STD_GNU)
718                   || pedantic)
719                 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
720                               gfc_option.max_continue_free);
721             }
722         }
723       continue_line = gfc_current_locus.lb->linenum;
724
725       /* Now find where it continues. First eat any comment lines.  */
726       openmp_cond_flag = skip_free_comments ();
727
728       if (prev_openmp_flag != openmp_flag)
729         {
730           gfc_current_locus = old_loc;
731           openmp_flag = prev_openmp_flag;
732           c = '&';
733           goto done;
734         }
735
736       /* Now that we have a non-comment line, probe ahead for the
737          first non-whitespace character.  If it is another '&', then
738          reading starts at the next character, otherwise we must back
739          up to where the whitespace started and resume from there.  */
740
741       old_loc = gfc_current_locus;
742
743       c = next_char ();
744       while (gfc_is_whitespace (c))
745         c = next_char ();
746
747       if (openmp_flag)
748         {
749           for (i = 0; i < 5; i++, c = next_char ())
750             {
751               gcc_assert (TOLOWER (c) == "!$omp"[i]);
752               if (i == 4)
753                 old_loc = gfc_current_locus;
754             }
755           while (gfc_is_whitespace (c))
756             c = next_char ();
757         }
758
759       if (c != '&')
760         {
761           if (in_string)
762             {
763               if (gfc_option.warn_ampersand)
764                 gfc_warning_now ("Missing '&' in continued character constant at %C");
765               gfc_current_locus.nextc--;
766             }
767           /* Both !$omp and !$ -fopenmp continuation lines have & on the
768              continuation line only optionally.  */
769           else if (openmp_flag || openmp_cond_flag)
770             gfc_current_locus.nextc--;
771           else
772             {
773               c = ' ';
774               gfc_current_locus = old_loc;
775               goto done;
776             }
777         }
778     }
779   else
780     {
781       /* Fixed form continuation.  */
782       if (!in_string && c == '!')
783         {
784           /* Skip comment at end of line.  */
785           do
786             {
787               c = next_char ();
788             }
789           while (c != '\n');
790
791           /* Avoid truncation warnings for comment ending lines.  */
792           gfc_current_locus.lb->truncated = 0;
793         }
794
795       if (c != '\n')
796         goto done;
797
798       prev_openmp_flag = openmp_flag;
799       continue_flag = 1;
800       old_loc = gfc_current_locus;
801
802       gfc_advance_line ();
803       skip_fixed_comments ();
804
805       /* See if this line is a continuation line.  */
806       if (openmp_flag != prev_openmp_flag)
807         {
808           openmp_flag = prev_openmp_flag;
809           goto not_continuation;
810         }
811
812       if (!openmp_flag)
813         for (i = 0; i < 5; i++)
814           {
815             c = next_char ();
816             if (c != ' ')
817               goto not_continuation;
818           }
819       else
820         for (i = 0; i < 5; i++)
821           {
822             c = next_char ();
823             if (TOLOWER (c) != "*$omp"[i])
824               goto not_continuation;
825           }
826
827       c = next_char ();
828       if (c == '0' || c == ' ' || c == '\n')
829         goto not_continuation;
830
831       /* We've got a continuation line.  If we are on the very next line after
832          the last continuation, increment the continuation line count and
833          check whether the limit has been exceeded.  */
834       if (gfc_current_locus.lb->linenum == continue_line + 1)
835         {
836           if (++continue_count == gfc_option.max_continue_fixed)
837             {
838               if (gfc_notification_std (GFC_STD_GNU)
839                   || pedantic)
840                 gfc_warning ("Limit of %d continuations exceeded in statement at %C",
841                               gfc_option.max_continue_fixed);
842             }
843         }
844
845       if (continue_line < gfc_current_locus.lb->linenum)
846         continue_line = gfc_current_locus.lb->linenum;
847     }
848
849   /* Ready to read first character of continuation line, which might
850      be another continuation line!  */
851   goto restart;
852
853 not_continuation:
854   c = '\n';
855   gfc_current_locus = old_loc;
856
857 done:
858   if (c == '\n')
859     continue_count = 0;
860   continue_flag = 0;
861   return c;
862 }
863
864
865 /* Get the next character of input, folded to lowercase.  In fixed
866    form mode, we also ignore spaces.  When matcher subroutines are
867    parsing character literals, they have to call
868    gfc_next_char_literal().  */
869
870 int
871 gfc_next_char (void)
872 {
873   int c;
874
875   do
876     {
877       c = gfc_next_char_literal (0);
878     }
879   while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
880
881   return TOLOWER (c);
882 }
883
884
885 int
886 gfc_peek_char (void)
887 {
888   locus old_loc;
889   int c;
890
891   old_loc = gfc_current_locus;
892   c = gfc_next_char ();
893   gfc_current_locus = old_loc;
894
895   return c;
896 }
897
898
899 /* Recover from an error.  We try to get past the current statement
900    and get lined up for the next.  The next statement follows a '\n'
901    or a ';'.  We also assume that we are not within a character
902    constant, and deal with finding a '\'' or '"'.  */
903
904 void
905 gfc_error_recovery (void)
906 {
907   char c, delim;
908
909   if (gfc_at_eof ())
910     return;
911
912   for (;;)
913     {
914       c = gfc_next_char ();
915       if (c == '\n' || c == ';')
916         break;
917
918       if (c != '\'' && c != '"')
919         {
920           if (gfc_at_eof ())
921             break;
922           continue;
923         }
924       delim = c;
925
926       for (;;)
927         {
928           c = next_char ();
929
930           if (c == delim)
931             break;
932           if (c == '\n')
933             return;
934           if (c == '\\')
935             {
936               c = next_char ();
937               if (c == '\n')
938                 return;
939             }
940         }
941       if (gfc_at_eof ())
942         break;
943     }
944 }
945
946
947 /* Read ahead until the next character to be read is not whitespace.  */
948
949 void
950 gfc_gobble_whitespace (void)
951 {
952   static int linenum = 0;
953   locus old_loc;
954   int c;
955
956   do
957     {
958       old_loc = gfc_current_locus;
959       c = gfc_next_char_literal (0);
960       /* Issue a warning for nonconforming tabs.  We keep track of the line
961          number because the Fortran matchers will often back up and the same
962          line will be scanned multiple times.  */
963       if (!gfc_option.warn_tabs && c == '\t')
964         {
965 #ifdef USE_MAPPED_LOCATION
966           int cur_linenum = LOCATION_LINE (gfc_current_locus.lb->location);
967 #else
968           int cur_linenum = gfc_current_locus.lb->linenum;
969 #endif
970           if (cur_linenum != linenum)
971             {
972               linenum = cur_linenum;
973               gfc_warning_now ("Nonconforming tab character at %C");
974             }
975         }
976     }
977   while (gfc_is_whitespace (c));
978
979   gfc_current_locus = old_loc;
980 }
981
982
983 /* Load a single line into pbuf.
984
985    If pbuf points to a NULL pointer, it is allocated.
986    We truncate lines that are too long, unless we're dealing with
987    preprocessor lines or if the option -ffixed-line-length-none is set,
988    in which case we reallocate the buffer to fit the entire line, if
989    need be.
990    In fixed mode, we expand a tab that occurs within the statement
991    label region to expand to spaces that leave the next character in
992    the source region.
993    load_line returns whether the line was truncated.
994
995    NOTE: The error machinery isn't available at this point, so we can't
996          easily report line and column numbers consistent with other 
997          parts of gfortran.  */
998
999 static int
1000 load_line (FILE * input, char **pbuf, int *pbuflen)
1001 {
1002   static int linenum = 0, current_line = 1;
1003   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
1004   int trunc_flag = 0, seen_comment = 0;
1005   int seen_printable = 0, seen_ampersand = 0;
1006   char *buffer;
1007
1008   /* Determine the maximum allowed line length.  */
1009   if (gfc_current_form == FORM_FREE)
1010     maxlen = gfc_option.free_line_length;
1011   else if (gfc_current_form == FORM_FIXED)
1012     maxlen = gfc_option.fixed_line_length;
1013   else
1014     maxlen = 72;
1015
1016   if (*pbuf == NULL)
1017     {
1018       /* Allocate the line buffer, storing its length into buflen.
1019          Note that if maxlen==0, indicating that arbitrary-length lines
1020          are allowed, the buffer will be reallocated if this length is
1021          insufficient; since 132 characters is the length of a standard
1022          free-form line, we use that as a starting guess.  */
1023       if (maxlen > 0)
1024         buflen = maxlen;
1025       else
1026         buflen = 132;
1027
1028       *pbuf = gfc_getmem (buflen + 1);
1029     }
1030
1031   i = 0;
1032   buffer = *pbuf;
1033
1034   preprocessor_flag = 0;
1035   c = fgetc (input);
1036   if (c == '#')
1037     /* In order to not truncate preprocessor lines, we have to
1038        remember that this is one.  */
1039     preprocessor_flag = 1;
1040   ungetc (c, input);
1041
1042   for (;;)
1043     {
1044       c = fgetc (input);
1045
1046       if (c == EOF)
1047         break;
1048       if (c == '\n')
1049         {
1050           /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3.  */
1051           if (gfc_current_form == FORM_FREE 
1052                 && !seen_printable && seen_ampersand)
1053             {
1054               if (pedantic)
1055                 gfc_error_now
1056                   ("'&' not allowed by itself in line %d", current_line);
1057               else
1058                 gfc_warning_now
1059                   ("'&' not allowed by itself in line %d", current_line);
1060             }
1061           break;
1062         }
1063
1064       if (c == '\r')
1065         continue;               /* Gobble characters.  */
1066       if (c == '\0')
1067         continue;
1068
1069       if (c == '\032')
1070         {
1071           /* Ctrl-Z ends the file.  */
1072           while (fgetc (input) != EOF);
1073           break;
1074         }
1075
1076       /* Check for illegal use of ampersand. See F95 Standard 3.3.1.3.  */
1077       if (c == '&')
1078         seen_ampersand = 1;
1079
1080       if ((c != ' ' && c != '&' && c != '!') || (c == '!' && !seen_ampersand))
1081         seen_printable = 1;
1082       
1083       if (gfc_current_form == FORM_FREE 
1084             && c == '!' && !seen_printable && seen_ampersand)
1085         {
1086           if (pedantic)
1087             gfc_error_now (
1088               "'&' not allowed by itself with comment in line %d", current_line);
1089           else
1090             gfc_warning_now (
1091               "'&' not allowed by itself with comment in line %d", current_line);
1092           seen_printable = 1;
1093         }
1094
1095       /* Is this a fixed-form comment?  */
1096       if (gfc_current_form == FORM_FIXED && i == 0
1097           && (c == '*' || c == 'c' || c == 'd'))
1098         seen_comment = 1;
1099
1100       if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
1101         {
1102           if (!gfc_option.warn_tabs && seen_comment == 0
1103               && current_line != linenum)
1104             {
1105               linenum = current_line;
1106               gfc_warning_now (
1107                 "Nonconforming tab character in column 1 of line %d", linenum);
1108             }
1109
1110           while (i <= 6)
1111             {
1112               *buffer++ = ' ';
1113               i++;
1114             }
1115
1116           continue;
1117         }
1118
1119       *buffer++ = c;
1120       i++;
1121
1122       if (maxlen == 0 || preprocessor_flag)
1123         {
1124           if (i >= buflen)
1125             {
1126               /* Reallocate line buffer to double size to hold the
1127                 overlong line.  */
1128               buflen = buflen * 2;
1129               *pbuf = xrealloc (*pbuf, buflen + 1);
1130               buffer = (*pbuf)+i;
1131             }
1132         }
1133       else if (i >= maxlen)
1134         {
1135           /* Truncate the rest of the line.  */
1136           for (;;)
1137             {
1138               c = fgetc (input);
1139               if (c == '\n' || c == EOF)
1140                 break;
1141
1142               trunc_flag = 1;
1143             }
1144
1145           ungetc ('\n', input);
1146         }
1147     }
1148
1149   /* Pad lines to the selected line length in fixed form.  */
1150   if (gfc_current_form == FORM_FIXED
1151       && gfc_option.fixed_line_length != 0
1152       && !preprocessor_flag
1153       && c != EOF)
1154     {
1155       while (i++ < maxlen)
1156         *buffer++ = ' ';
1157     }
1158
1159   *buffer = '\0';
1160   *pbuflen = buflen;
1161   current_line++;
1162
1163   return trunc_flag;
1164 }
1165
1166
1167 /* Get a gfc_file structure, initialize it and add it to
1168    the file stack.  */
1169
1170 static gfc_file *
1171 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
1172 {
1173   gfc_file *f;
1174
1175   f = gfc_getmem (sizeof (gfc_file));
1176
1177   f->filename = gfc_getmem (strlen (name) + 1);
1178   strcpy (f->filename, name);
1179
1180   f->next = file_head;
1181   file_head = f;
1182
1183   f->included_by = current_file;
1184   if (current_file != NULL)
1185     f->inclusion_line = current_file->line;
1186
1187 #ifdef USE_MAPPED_LOCATION
1188   linemap_add (&line_table, reason, false, f->filename, 1);
1189 #endif
1190
1191   return f;
1192 }
1193
1194 /* Deal with a line from the C preprocessor. The
1195    initial octothorp has already been seen.  */
1196
1197 static void
1198 preprocessor_line (char *c)
1199 {
1200   bool flag[5];
1201   int i, line;
1202   char *filename;
1203   gfc_file *f;
1204   int escaped, unescape;
1205
1206   c++;
1207   while (*c == ' ' || *c == '\t')
1208     c++;
1209
1210   if (*c < '0' || *c > '9')
1211     goto bad_cpp_line;
1212
1213   line = atoi (c);
1214
1215   c = strchr (c, ' ');
1216   if (c == NULL)
1217     {
1218       /* No file name given.  Set new line number.  */
1219       current_file->line = line;
1220       return;
1221     }
1222
1223   /* Skip spaces.  */
1224   while (*c == ' ' || *c == '\t')
1225     c++;
1226
1227   /* Skip quote.  */
1228   if (*c != '"')
1229     goto bad_cpp_line;
1230   ++c;
1231
1232   filename = c;
1233
1234   /* Make filename end at quote.  */
1235   unescape = 0;
1236   escaped = false;
1237   while (*c && ! (! escaped && *c == '"'))
1238     {
1239       if (escaped)
1240         escaped = false;
1241       else if (*c == '\\')
1242         {
1243           escaped = true;
1244           unescape++;
1245         }
1246       ++c;
1247     }
1248
1249   if (! *c)
1250     /* Preprocessor line has no closing quote.  */
1251     goto bad_cpp_line;
1252
1253   *c++ = '\0';
1254
1255   /* Undo effects of cpp_quote_string.  */
1256   if (unescape)
1257     {
1258       char *s = filename;
1259       char *d = gfc_getmem (c - filename - unescape);
1260
1261       filename = d;
1262       while (*s)
1263         {
1264           if (*s == '\\')
1265             *d++ = *++s;
1266           else
1267             *d++ = *s;
1268           s++;
1269         }
1270       *d = '\0';
1271     }
1272
1273   /* Get flags.  */
1274
1275   flag[1] = flag[2] = flag[3] = flag[4] = false;
1276
1277   for (;;)
1278     {
1279       c = strchr (c, ' ');
1280       if (c == NULL)
1281         break;
1282
1283       c++;
1284       i = atoi (c);
1285
1286       if (1 <= i && i <= 4)
1287         flag[i] = true;
1288     }
1289
1290   /* Interpret flags.  */
1291
1292   if (flag[1]) /* Starting new file.  */
1293     {
1294       f = get_file (filename, LC_RENAME);
1295       f->up = current_file;
1296       current_file = f;
1297     }
1298
1299   if (flag[2]) /* Ending current file.  */
1300     {
1301       if (!current_file->up
1302           || strcmp (current_file->up->filename, filename) != 0)
1303         {
1304           gfc_warning_now ("%s:%d: file %s left but not entered",
1305                            current_file->filename, current_file->line,
1306                            filename);
1307           if (unescape)
1308             gfc_free (filename);
1309           return;
1310         }
1311       current_file = current_file->up;
1312     }
1313
1314   /* The name of the file can be a temporary file produced by
1315      cpp. Replace the name if it is different.  */
1316
1317   if (strcmp (current_file->filename, filename) != 0)
1318     {
1319       gfc_free (current_file->filename);
1320       current_file->filename = gfc_getmem (strlen (filename) + 1);
1321       strcpy (current_file->filename, filename);
1322     }
1323
1324   /* Set new line number.  */
1325   current_file->line = line;
1326   if (unescape)
1327     gfc_free (filename);
1328   return;
1329
1330  bad_cpp_line:
1331   gfc_warning_now ("%s:%d: Illegal preprocessor directive",
1332                    current_file->filename, current_file->line);
1333   current_file->line++;
1334 }
1335
1336
1337 static try load_file (const char *, bool);
1338
1339 /* include_line()-- Checks a line buffer to see if it is an include
1340    line.  If so, we call load_file() recursively to load the included
1341    file.  We never return a syntax error because a statement like
1342    "include = 5" is perfectly legal.  We return false if no include was
1343    processed or true if we matched an include.  */
1344
1345 static bool
1346 include_line (char *line)
1347 {
1348   char quote, *c, *begin, *stop;
1349
1350   c = line;
1351
1352   if (gfc_option.flag_openmp)
1353     {
1354       if (gfc_current_form == FORM_FREE)
1355         {
1356           while (*c == ' ' || *c == '\t')
1357             c++;
1358           if (*c == '!' && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1359             c += 3;
1360         }
1361       else
1362         {
1363           if ((*c == '!' || *c == 'c' || *c == 'C' || *c == '*')
1364               && c[1] == '$' && (c[2] == ' ' || c[2] == '\t'))
1365             c += 3;
1366         }
1367     }
1368
1369   while (*c == ' ' || *c == '\t')
1370     c++;
1371
1372   if (strncasecmp (c, "include", 7))
1373       return false;
1374
1375   c += 7;
1376   while (*c == ' ' || *c == '\t')
1377     c++;
1378
1379   /* Find filename between quotes.  */
1380   
1381   quote = *c++;
1382   if (quote != '"' && quote != '\'')
1383     return false;
1384
1385   begin = c;
1386
1387   while (*c != quote && *c != '\0')
1388     c++;
1389
1390   if (*c == '\0')
1391     return false;
1392
1393   stop = c++;
1394   
1395   while (*c == ' ' || *c == '\t')
1396     c++;
1397
1398   if (*c != '\0' && *c != '!')
1399     return false;
1400
1401   /* We have an include line at this point.  */
1402
1403   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1404                    read by anything else.  */
1405
1406   load_file (begin, false);
1407   return true;
1408 }
1409
1410 /* Load a file into memory by calling load_line until the file ends.  */
1411
1412 static try
1413 load_file (const char *filename, bool initial)
1414 {
1415   char *line;
1416   gfc_linebuf *b;
1417   gfc_file *f;
1418   FILE *input;
1419   int len, line_len;
1420
1421   for (f = current_file; f; f = f->up)
1422     if (strcmp (filename, f->filename) == 0)
1423       {
1424         gfc_error_now ("File '%s' is being included recursively", filename);
1425         return FAILURE;
1426       }
1427
1428   if (initial)
1429     {
1430       if (gfc_src_file)
1431         {
1432           input = gfc_src_file;
1433           gfc_src_file = NULL;
1434         }
1435       else
1436         input = gfc_open_file (filename);
1437       if (input == NULL)
1438         {
1439           gfc_error_now ("Can't open file '%s'", filename);
1440           return FAILURE;
1441         }
1442     }
1443   else
1444     {
1445       input = gfc_open_included_file (filename, false, false);
1446       if (input == NULL)
1447         {
1448           gfc_error_now ("Can't open included file '%s'", filename);
1449           return FAILURE;
1450         }
1451     }
1452
1453   /* Load the file.  */
1454
1455   f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1456   f->up = current_file;
1457   current_file = f;
1458   current_file->line = 1;
1459   line = NULL;
1460   line_len = 0;
1461
1462   if (initial && gfc_src_preprocessor_lines[0])
1463     {
1464       preprocessor_line (gfc_src_preprocessor_lines[0]);
1465       gfc_free (gfc_src_preprocessor_lines[0]);
1466       gfc_src_preprocessor_lines[0] = NULL;
1467       if (gfc_src_preprocessor_lines[1])
1468         {
1469           preprocessor_line (gfc_src_preprocessor_lines[1]);
1470           gfc_free (gfc_src_preprocessor_lines[1]);
1471           gfc_src_preprocessor_lines[1] = NULL;
1472         }
1473     }
1474
1475   for (;;)
1476     {
1477       int trunc = load_line (input, &line, &line_len);
1478
1479       len = strlen (line);
1480       if (feof (input) && len == 0)
1481         break;
1482
1483       /* There are three things this line can be: a line of Fortran
1484          source, an include line or a C preprocessor directive.  */
1485
1486       if (line[0] == '#')
1487         {
1488           preprocessor_line (line);
1489           continue;
1490         }
1491
1492       if (include_line (line))
1493         {
1494           current_file->line++;
1495           continue;
1496         }
1497
1498       /* Add line.  */
1499
1500       b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1501
1502 #ifdef USE_MAPPED_LOCATION
1503       b->location
1504         = linemap_line_start (&line_table, current_file->line++, 120);
1505 #else
1506       b->linenum = current_file->line++;
1507 #endif
1508       b->file = current_file;
1509       b->truncated = trunc;
1510       strcpy (b->line, line);
1511
1512       if (line_head == NULL)
1513         line_head = b;
1514       else
1515         line_tail->next = b;
1516
1517       line_tail = b;
1518     }
1519
1520   /* Release the line buffer allocated in load_line.  */
1521   gfc_free (line);
1522
1523   fclose (input);
1524
1525   current_file = current_file->up;
1526 #ifdef USE_MAPPED_LOCATION
1527   linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1528 #endif
1529   return SUCCESS;
1530 }
1531
1532
1533 /* Open a new file and start scanning from that file. Returns SUCCESS
1534    if everything went OK, FAILURE otherwise.  If form == FORM_UKNOWN
1535    it tries to determine the source form from the filename, defaulting
1536    to free form.  */
1537
1538 try
1539 gfc_new_file (void)
1540 {
1541   try result;
1542
1543   result = load_file (gfc_source_file, true);
1544
1545   gfc_current_locus.lb = line_head;
1546   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1547
1548 #if 0 /* Debugging aid.  */
1549   for (; line_head; line_head = line_head->next)
1550     gfc_status ("%s:%3d %s\n", line_head->file->filename, 
1551 #ifdef USE_MAPPED_LOCATION
1552                 LOCATION_LINE (line_head->location),
1553 #else
1554                 line_head->linenum,
1555 #endif
1556                 line_head->line);
1557
1558   exit (0);
1559 #endif
1560
1561   return result;
1562 }
1563
1564 static char *
1565 unescape_filename (const char *ptr)
1566 {
1567   const char *p = ptr, *s;
1568   char *d, *ret;
1569   int escaped, unescape = 0;
1570
1571   /* Make filename end at quote.  */
1572   escaped = false;
1573   while (*p && ! (! escaped && *p == '"'))
1574     {
1575       if (escaped)
1576         escaped = false;
1577       else if (*p == '\\')
1578         {
1579           escaped = true;
1580           unescape++;
1581         }
1582       ++p;
1583     }
1584
1585   if (! *p || p[1])
1586     return NULL;
1587
1588   /* Undo effects of cpp_quote_string.  */
1589   s = ptr;
1590   d = gfc_getmem (p + 1 - ptr - unescape);
1591   ret = d;
1592
1593   while (s != p)
1594     {
1595       if (*s == '\\')
1596         *d++ = *++s;
1597       else
1598         *d++ = *s;
1599       s++;
1600     }
1601   *d = '\0';
1602   return ret;
1603 }
1604
1605 /* For preprocessed files, if the first tokens are of the form # NUM.
1606    handle the directives so we know the original file name.  */
1607
1608 const char *
1609 gfc_read_orig_filename (const char *filename, const char **canon_source_file)
1610 {
1611   int c, len;
1612   char *dirname;
1613
1614   gfc_src_file = gfc_open_file (filename);
1615   if (gfc_src_file == NULL)
1616     return NULL;
1617
1618   c = fgetc (gfc_src_file);
1619   ungetc (c, gfc_src_file);
1620
1621   if (c != '#')
1622     return NULL;
1623
1624   len = 0;
1625   load_line (gfc_src_file, &gfc_src_preprocessor_lines[0], &len);
1626
1627   if (strncmp (gfc_src_preprocessor_lines[0], "# 1 \"", 5) != 0)
1628     return NULL;
1629
1630   filename = unescape_filename (gfc_src_preprocessor_lines[0] + 5);
1631   if (filename == NULL)
1632     return NULL;
1633
1634   c = fgetc (gfc_src_file);
1635   ungetc (c, gfc_src_file);
1636
1637   if (c != '#')
1638     return filename;
1639
1640   len = 0;
1641   load_line (gfc_src_file, &gfc_src_preprocessor_lines[1], &len);
1642
1643   if (strncmp (gfc_src_preprocessor_lines[1], "# 1 \"", 5) != 0)
1644     return filename;
1645
1646   dirname = unescape_filename (gfc_src_preprocessor_lines[1] + 5);
1647   if (dirname == NULL)
1648     return filename;
1649
1650   len = strlen (dirname);
1651   if (len < 3 || dirname[len - 1] != '/' || dirname[len - 2] != '/')
1652     {
1653       gfc_free (dirname);
1654       return filename;
1655     }
1656   dirname[len - 2] = '\0';
1657   set_src_pwd (dirname);
1658
1659   if (! IS_ABSOLUTE_PATH (filename))
1660     {
1661       char *p = gfc_getmem (len + strlen (filename));
1662
1663       memcpy (p, dirname, len - 2);
1664       p[len - 2] = '/';
1665       strcpy (p + len - 1, filename);
1666       *canon_source_file = p;
1667     }
1668
1669   gfc_free (dirname);
1670   return filename;
1671 }