OSDN Git Service

gcc/fortran:
[pf3gnuchains/gcc-fork.git] / gcc / fortran / scanner.c
1 /* Character scanner.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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
49 /* Structure for holding module and include file search path.  */
50 typedef struct gfc_directorylist
51 {
52   char *path;
53   struct gfc_directorylist *next;
54 }
55 gfc_directorylist;
56
57 /* List of include file search directories.  */
58 static gfc_directorylist *include_dirs;
59
60 static gfc_file *file_head, *current_file;
61
62 static int continue_flag, end_flag;
63
64 gfc_source_form gfc_current_form;
65 static gfc_linebuf *line_head, *line_tail;
66        
67 locus gfc_current_locus;
68 const char *gfc_source_file;
69       
70
71 /* Main scanner initialization.  */
72
73 void
74 gfc_scanner_init_1 (void)
75 {
76   file_head = NULL;
77   line_head = NULL;
78   line_tail = NULL;
79
80   end_flag = 0;
81 }
82
83
84 /* Main scanner destructor.  */
85
86 void
87 gfc_scanner_done_1 (void)
88 {
89   gfc_linebuf *lb;
90   gfc_file *f;
91
92   while(line_head != NULL) 
93     {
94       lb = line_head->next;
95       gfc_free(line_head);
96       line_head = lb;
97     }
98      
99   while(file_head != NULL) 
100     {
101       f = file_head->next;
102       gfc_free(file_head->filename);
103       gfc_free(file_head);
104       file_head = f;    
105     }
106
107 }
108
109
110 /* Adds path to the list pointed to by list.  */
111
112 void
113 gfc_add_include_path (const char *path)
114 {
115   gfc_directorylist *dir;
116   const char *p;
117
118   p = path;
119   while (*p == ' ' || *p == '\t')  /* someone might do 'gfortran "-I include"' */
120     if (*p++ == '\0')
121       return;
122
123   dir = include_dirs;
124   if (!dir)
125     {
126       dir = include_dirs = gfc_getmem (sizeof (gfc_directorylist));
127     }
128   else
129     {
130       while (dir->next)
131         dir = dir->next;
132
133       dir->next = gfc_getmem (sizeof (gfc_directorylist));
134       dir = dir->next;
135     }
136
137   dir->next = NULL;
138   dir->path = gfc_getmem (strlen (p) + 2);
139   strcpy (dir->path, p);
140   strcat (dir->path, "/");      /* make '/' last character */
141 }
142
143
144 /* Release resources allocated for options.  */
145
146 void
147 gfc_release_include_path (void)
148 {
149   gfc_directorylist *p;
150
151   gfc_free (gfc_option.module_dir);
152   while (include_dirs != NULL)
153     {
154       p = include_dirs;
155       include_dirs = include_dirs->next;
156       gfc_free (p->path);
157       gfc_free (p);
158     }
159 }
160
161 /* Opens file for reading, searching through the include directories
162    given if necessary.  If the include_cwd argument is true, we try
163    to open the file in the current directory first.  */
164
165 FILE *
166 gfc_open_included_file (const char *name, const bool include_cwd)
167 {
168   char *fullname;
169   gfc_directorylist *p;
170   FILE *f;
171
172   if (include_cwd)
173     {
174       f = gfc_open_file (name);
175       if (f != NULL)
176         return f;
177     }
178
179   for (p = include_dirs; p; p = p->next)
180     {
181       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
182       strcpy (fullname, p->path);
183       strcat (fullname, name);
184
185       f = gfc_open_file (fullname);
186       if (f != NULL)
187         return f;
188     }
189
190   return NULL;
191 }
192
193 /* Test to see if we're at the end of the main source file.  */
194
195 int
196 gfc_at_end (void)
197 {
198
199   return end_flag;
200 }
201
202
203 /* Test to see if we're at the end of the current file.  */
204
205 int
206 gfc_at_eof (void)
207 {
208
209   if (gfc_at_end ())
210     return 1;
211
212   if (line_head == NULL)
213     return 1;                   /* Null file */
214
215   if (gfc_current_locus.lb == NULL)
216     return 1;
217
218   return 0;
219 }
220
221
222 /* Test to see if we're at the beginning of a new line.  */
223
224 int
225 gfc_at_bol (void)
226 {
227   if (gfc_at_eof ())
228     return 1;
229
230   return (gfc_current_locus.nextc == gfc_current_locus.lb->line);
231 }
232
233
234 /* Test to see if we're at the end of a line.  */
235
236 int
237 gfc_at_eol (void)
238 {
239
240   if (gfc_at_eof ())
241     return 1;
242
243   return (*gfc_current_locus.nextc == '\0');
244 }
245
246
247 /* Advance the current line pointer to the next line.  */
248
249 void
250 gfc_advance_line (void)
251 {
252   if (gfc_at_end ())
253     return;
254
255   if (gfc_current_locus.lb == NULL) 
256     {
257       end_flag = 1;
258       return;
259     } 
260
261   gfc_current_locus.lb = gfc_current_locus.lb->next;
262
263   if (gfc_current_locus.lb != NULL)         
264     gfc_current_locus.nextc = gfc_current_locus.lb->line;
265   else 
266     {
267       gfc_current_locus.nextc = NULL;
268       end_flag = 1;
269     }       
270 }
271
272
273 /* Get the next character from the input, advancing gfc_current_file's
274    locus.  When we hit the end of the line or the end of the file, we
275    start returning a '\n' in order to complete the current statement.
276    No Fortran line conventions are implemented here.
277
278    Requiring explicit advances to the next line prevents the parse
279    pointer from being on the wrong line if the current statement ends
280    prematurely.  */
281
282 static int
283 next_char (void)
284 {
285   int c;
286   
287   if (gfc_current_locus.nextc == NULL)
288     return '\n';
289
290   c = *gfc_current_locus.nextc++;
291   if (c == '\0')
292     {
293       gfc_current_locus.nextc--; /* Remain on this line.  */
294       c = '\n';
295     }
296
297   return c;
298 }
299
300 /* Skip a comment.  When we come here the parse pointer is positioned
301    immediately after the comment character.  If we ever implement
302    compiler directives withing comments, here is where we parse the
303    directive.  */
304
305 static void
306 skip_comment_line (void)
307 {
308   char c;
309
310   do
311     {
312       c = next_char ();
313     }
314   while (c != '\n');
315
316   gfc_advance_line ();
317 }
318
319
320 /* Comment lines are null lines, lines containing only blanks or lines
321    on which the first nonblank line is a '!'.  */
322
323 static void
324 skip_free_comments (void)
325 {
326   locus start;
327   char c;
328
329   for (;;)
330     {
331       start = gfc_current_locus;
332       if (gfc_at_eof ())
333         break;
334
335       do
336         {
337           c = next_char ();
338         }
339       while (gfc_is_whitespace (c));
340
341       if (c == '\n')
342         {
343           gfc_advance_line ();
344           continue;
345         }
346
347       if (c == '!')
348         {
349           skip_comment_line ();
350           continue;
351         }
352
353       break;
354     }
355
356   gfc_current_locus = start;
357 }
358
359
360 /* Skip comment lines in fixed source mode.  We have the same rules as
361    in skip_free_comment(), except that we can have a 'c', 'C' or '*'
362    in column 1, and a '!' cannot be in column 6.  Also, we deal with
363    lines with 'd' or 'D' in column 1, if the user requested this.  */
364
365 static void
366 skip_fixed_comments (void)
367 {
368   locus start;
369   int col;
370   char c;
371
372   for (;;)
373     {
374       start = gfc_current_locus;
375       if (gfc_at_eof ())
376         break;
377
378       c = next_char ();
379       if (c == '\n')
380         {
381           gfc_advance_line ();
382           continue;
383         }
384
385       if (c == '!' || c == 'c' || c == 'C' || c == '*')
386         {
387           skip_comment_line ();
388           continue;
389         }
390
391       if (gfc_option.flag_d_lines != -1 && (c == 'd' || c == 'D'))
392         {
393           if (gfc_option.flag_d_lines == 0)
394             {
395               skip_comment_line ();
396               continue;
397             }
398           else
399             *start.nextc = c = ' ';
400         }
401
402       col = 1;
403
404       while (gfc_is_whitespace (c))
405         {
406           c = next_char ();
407           col++;
408         }
409
410       if (c == '\n')
411         {
412           gfc_advance_line ();
413           continue;
414         }
415
416       if (col != 6 && c == '!')
417         {
418           skip_comment_line ();
419           continue;
420         }
421
422       break;
423     }
424
425   gfc_current_locus = start;
426 }
427
428
429 /* Skips the current line if it is a comment.  Assumes that we are at
430    the start of the current line.  */
431
432 void
433 gfc_skip_comments (void)
434 {
435
436   if (!gfc_at_bol () || gfc_current_form == FORM_FREE)
437     skip_free_comments ();
438   else
439     skip_fixed_comments ();
440 }
441
442
443 /* Get the next character from the input, taking continuation lines
444    and end-of-line comments into account.  This implies that comment
445    lines between continued lines must be eaten here.  For higher-level
446    subroutines, this flattens continued lines into a single logical
447    line.  The in_string flag denotes whether we're inside a character
448    context or not.  */
449
450 int
451 gfc_next_char_literal (int in_string)
452 {
453   locus old_loc;
454   int i, c;
455
456   continue_flag = 0;
457
458 restart:
459   c = next_char ();
460   if (gfc_at_end ())
461     return c;
462
463   if (gfc_current_form == FORM_FREE)
464     {
465
466       if (!in_string && c == '!')
467         {
468           /* This line can't be continued */
469           do
470             {
471               c = next_char ();
472             }
473           while (c != '\n');
474
475           /* Avoid truncation warnings for comment ending lines.  */
476           gfc_current_locus.lb->truncated = 0;
477
478           goto done;
479         }
480
481       if (c != '&')
482         goto done;
483
484       /* If the next nonblank character is a ! or \n, we've got a
485          continuation line.  */
486       old_loc = gfc_current_locus;
487
488       c = next_char ();
489       while (gfc_is_whitespace (c))
490         c = next_char ();
491
492       /* Character constants to be continued cannot have commentary
493          after the '&'.  */
494
495       if (in_string && c != '\n')
496         {
497           gfc_current_locus = old_loc;
498           c = '&';
499           goto done;
500         }
501
502       if (c != '!' && c != '\n')
503         {
504           gfc_current_locus = old_loc;
505           c = '&';
506           goto done;
507         }
508
509       continue_flag = 1;
510       if (c == '!')
511         skip_comment_line ();
512       else
513         gfc_advance_line ();
514
515       /* We've got a continuation line and need to find where it continues.
516          First eat any comment lines.  */
517       gfc_skip_comments ();
518
519       /* Now that we have a non-comment line, probe ahead for the
520          first non-whitespace character.  If it is another '&', then
521          reading starts at the next character, otherwise we must back
522          up to where the whitespace started and resume from there.  */
523
524       old_loc = gfc_current_locus;
525
526       c = next_char ();
527       while (gfc_is_whitespace (c))
528         c = next_char ();
529
530       if (c != '&')
531         gfc_current_locus = old_loc;
532
533     }
534   else
535     {
536       /* Fixed form continuation.  */
537       if (!in_string && c == '!')
538         {
539           /* Skip comment at end of line.  */
540           do
541             {
542               c = next_char ();
543             }
544           while (c != '\n');
545
546           /* Avoid truncation warnings for comment ending lines.  */
547           gfc_current_locus.lb->truncated = 0;
548         }
549
550       if (c != '\n')
551         goto done;
552
553       continue_flag = 1;
554       old_loc = gfc_current_locus;
555
556       gfc_advance_line ();
557       gfc_skip_comments ();
558
559       /* See if this line is a continuation line.  */
560       for (i = 0; i < 5; i++)
561         {
562           c = next_char ();
563           if (c != ' ')
564             goto not_continuation;
565         }
566
567       c = next_char ();
568       if (c == '0' || c == ' ')
569         goto not_continuation;
570     }
571
572   /* Ready to read first character of continuation line, which might
573      be another continuation line!  */
574   goto restart;
575
576 not_continuation:
577   c = '\n';
578   gfc_current_locus = old_loc;
579
580 done:
581   continue_flag = 0;
582   return c;
583 }
584
585
586 /* Get the next character of input, folded to lowercase.  In fixed
587    form mode, we also ignore spaces.  When matcher subroutines are
588    parsing character literals, they have to call
589    gfc_next_char_literal().  */
590
591 int
592 gfc_next_char (void)
593 {
594   int c;
595
596   do
597     {
598       c = gfc_next_char_literal (0);
599     }
600   while (gfc_current_form == FORM_FIXED && gfc_is_whitespace (c));
601
602   return TOLOWER (c);
603 }
604
605
606 int
607 gfc_peek_char (void)
608 {
609   locus old_loc;
610   int c;
611
612   old_loc = gfc_current_locus;
613   c = gfc_next_char ();
614   gfc_current_locus = old_loc;
615
616   return c;
617 }
618
619
620 /* Recover from an error.  We try to get past the current statement
621    and get lined up for the next.  The next statement follows a '\n'
622    or a ';'.  We also assume that we are not within a character
623    constant, and deal with finding a '\'' or '"'.  */
624
625 void
626 gfc_error_recovery (void)
627 {
628   char c, delim;
629
630   if (gfc_at_eof ())
631     return;
632
633   for (;;)
634     {
635       c = gfc_next_char ();
636       if (c == '\n' || c == ';')
637         break;
638
639       if (c != '\'' && c != '"')
640         {
641           if (gfc_at_eof ())
642             break;
643           continue;
644         }
645       delim = c;
646
647       for (;;)
648         {
649           c = next_char ();
650
651           if (c == delim)
652             break;
653           if (c == '\n')
654             return;
655           if (c == '\\')
656             {
657               c = next_char ();
658               if (c == '\n')
659                 return;
660             }
661         }
662       if (gfc_at_eof ())
663         break;
664     }
665 }
666
667
668 /* Read ahead until the next character to be read is not whitespace.  */
669
670 void
671 gfc_gobble_whitespace (void)
672 {
673   locus old_loc;
674   int c;
675
676   do
677     {
678       old_loc = gfc_current_locus;
679       c = gfc_next_char_literal (0);
680     }
681   while (gfc_is_whitespace (c));
682
683   gfc_current_locus = old_loc;
684 }
685
686
687 /* Load a single line into pbuf.
688
689    If pbuf points to a NULL pointer, it is allocated.
690    We truncate lines that are too long, unless we're dealing with
691    preprocessor lines or if the option -ffixed-line-length-none is set,
692    in which case we reallocate the buffer to fit the entire line, if
693    need be.
694    In fixed mode, we expand a tab that occurs within the statement
695    label region to expand to spaces that leave the next character in
696    the source region.
697    load_line returns whether the line was truncated.  */
698
699 static int
700 load_line (FILE * input, char **pbuf, int *pbuflen)
701 {
702   int c, maxlen, i, preprocessor_flag, buflen = *pbuflen;
703   int trunc_flag = 0;
704   char *buffer;
705
706   /* Determine the maximum allowed line length.
707      The default for free-form is GFC_MAX_LINE, for fixed-form or for
708      unknown form it is 72. Refer to the documentation in gfc_option_t.  */
709   if (gfc_current_form == FORM_FREE)
710     {
711       if (gfc_option.free_line_length == -1)
712         maxlen = GFC_MAX_LINE;
713       else
714         maxlen = gfc_option.free_line_length;
715     }
716   else if (gfc_current_form == FORM_FIXED)
717     {
718       if (gfc_option.fixed_line_length == -1)
719         maxlen = 72;
720       else
721         maxlen = gfc_option.fixed_line_length;
722     }
723   else
724     maxlen = 72;
725
726   if (*pbuf == NULL)
727     {
728       /* Allocate the line buffer, storing its length into buflen.  */
729       if (maxlen > 0)
730         buflen = maxlen;
731       else
732         buflen = GFC_MAX_LINE;
733
734       *pbuf = gfc_getmem (buflen + 1);
735     }
736
737   i = 0;
738   buffer = *pbuf;
739
740   preprocessor_flag = 0;
741   c = fgetc (input);
742   if (c == '#')
743     /* In order to not truncate preprocessor lines, we have to
744        remember that this is one.  */
745     preprocessor_flag = 1;
746   ungetc (c, input);
747
748   for (;;)
749     {
750       c = fgetc (input);
751
752       if (c == EOF)
753         break;
754       if (c == '\n')
755         break;
756
757       if (c == '\r')
758         continue;               /* Gobble characters.  */
759       if (c == '\0')
760         continue;
761
762       if (c == '\032')
763         {
764           /* Ctrl-Z ends the file.  */
765           while (fgetc (input) != EOF);
766           break;
767         }
768
769       if (gfc_current_form == FORM_FIXED && c == '\t' && i <= 6)
770         {                       /* Tab expansion.  */
771           while (i <= 6)
772             {
773               *buffer++ = ' ';
774               i++;
775             }
776
777           continue;
778         }
779
780       *buffer++ = c;
781       i++;
782
783       if (maxlen == 0 || preprocessor_flag)
784         {
785           if (i >= buflen)
786             {
787               /* Reallocate line buffer to double size to hold the
788                  overlong line.  */
789               buflen = buflen * 2;
790               *pbuf = xrealloc (*pbuf, buflen + 1);
791               buffer = (*pbuf)+i;
792             }
793         }
794       else if (i >= maxlen)
795         {
796           /* Truncate the rest of the line.  */
797           for (;;)
798             {
799               c = fgetc (input);
800               if (c == '\n' || c == EOF)
801                 break;
802
803               trunc_flag = 1;
804             }
805
806           ungetc ('\n', input);
807         }
808     }
809
810   /* Pad lines to the selected line length in fixed form.  */
811   if (gfc_current_form == FORM_FIXED
812       && gfc_option.fixed_line_length != 0
813       && !preprocessor_flag
814       && c != EOF)
815     {
816       while (i++ < maxlen)
817         *buffer++ = ' ';
818     }
819
820   *buffer = '\0';
821   *pbuflen = buflen;
822
823   return trunc_flag;
824 }
825
826
827 /* Get a gfc_file structure, initialize it and add it to
828    the file stack.  */
829
830 static gfc_file *
831 get_file (const char *name, enum lc_reason reason ATTRIBUTE_UNUSED)
832 {
833   gfc_file *f;
834
835   f = gfc_getmem (sizeof (gfc_file));
836
837   f->filename = gfc_getmem (strlen (name) + 1);
838   strcpy (f->filename, name);
839
840   f->next = file_head;
841   file_head = f;
842
843   f->included_by = current_file;
844   if (current_file != NULL)
845     f->inclusion_line = current_file->line;
846
847 #ifdef USE_MAPPED_LOCATION
848   linemap_add (&line_table, reason, false, f->filename, 1);
849 #endif
850
851   return f;
852 }
853
854 /* Deal with a line from the C preprocessor. The
855    initial octothorp has already been seen.  */
856
857 static void
858 preprocessor_line (char *c)
859 {
860   bool flag[5];
861   int i, line;
862   char *filename;
863   gfc_file *f;
864   int escaped;
865
866   c++;
867   while (*c == ' ' || *c == '\t')
868     c++;
869
870   if (*c < '0' || *c > '9')
871     goto bad_cpp_line;
872
873   line = atoi (c);
874
875   c = strchr (c, ' ');
876   if (c == NULL)
877     {
878       /* No file name given.  Set new line number.  */
879       current_file->line = line;
880       return;
881     }
882
883   /* Skip spaces.  */
884   while (*c == ' ' || *c == '\t')
885     c++;
886
887   /* Skip quote.  */
888   if (*c != '"')
889     goto bad_cpp_line;
890   ++c;
891
892   filename = c;
893
894   /* Make filename end at quote.  */
895   escaped = false;
896   while (*c && ! (! escaped && *c == '"'))
897     {
898       if (escaped)
899         escaped = false;
900       else
901         escaped = *c == '\\';
902       ++c;
903     }
904
905   if (! *c)
906     /* Preprocessor line has no closing quote.  */
907     goto bad_cpp_line;
908
909   *c++ = '\0';
910
911
912
913   /* Get flags.  */
914
915   flag[1] = flag[2] = flag[3] = flag[4] = false;
916
917   for (;;)
918     {
919       c = strchr (c, ' ');
920       if (c == NULL)
921         break;
922
923       c++;
924       i = atoi (c);
925
926       if (1 <= i && i <= 4)
927         flag[i] = true;
928     }
929
930   /* Interpret flags.  */
931
932   if (flag[1]) /* Starting new file.  */
933     {
934       f = get_file (filename, LC_RENAME);
935       f->up = current_file;
936       current_file = f;
937     }
938
939   if (flag[2]) /* Ending current file.  */
940     {
941       if (!current_file->up
942           || strcmp (current_file->up->filename, filename) != 0)
943         {
944           gfc_warning_now ("%s:%d: file %s left but not entered",
945                            current_file->filename, current_file->line,
946                            filename);
947           return;
948         }
949       current_file = current_file->up;
950     }
951
952   /* The name of the file can be a temporary file produced by
953      cpp. Replace the name if it is different.  */
954
955   if (strcmp (current_file->filename, filename) != 0)
956     {
957       gfc_free (current_file->filename);
958       current_file->filename = gfc_getmem (strlen (filename) + 1);
959       strcpy (current_file->filename, filename);
960     }
961
962   /* Set new line number.  */
963   current_file->line = line;
964   return;
965
966  bad_cpp_line:
967   gfc_warning_now ("%s:%d: Illegal preprocessor directive",
968                    current_file->filename, current_file->line);
969   current_file->line++;
970 }
971
972
973 static try load_file (const char *, bool);
974
975 /* include_line()-- Checks a line buffer to see if it is an include
976    line.  If so, we call load_file() recursively to load the included
977    file.  We never return a syntax error because a statement like
978    "include = 5" is perfectly legal.  We return false if no include was
979    processed or true if we matched an include.  */
980
981 static bool
982 include_line (char *line)
983 {
984   char quote, *c, *begin, *stop;
985   
986   c = line;
987   while (*c == ' ' || *c == '\t')
988     c++;
989
990   if (strncasecmp (c, "include", 7))
991       return false;
992
993   c += 7;
994   while (*c == ' ' || *c == '\t')
995     c++;
996
997   /* Find filename between quotes.  */
998   
999   quote = *c++;
1000   if (quote != '"' && quote != '\'')
1001     return false;
1002
1003   begin = c;
1004
1005   while (*c != quote && *c != '\0')
1006     c++;
1007
1008   if (*c == '\0')
1009     return false;
1010
1011   stop = c++;
1012   
1013   while (*c == ' ' || *c == '\t')
1014     c++;
1015
1016   if (*c != '\0' && *c != '!')
1017     return false;
1018
1019   /* We have an include line at this point.  */
1020
1021   *stop = '\0'; /* It's ok to trash the buffer, as this line won't be
1022                    read by anything else.  */
1023
1024   load_file (begin, false);
1025   return true;
1026 }
1027
1028 /* Load a file into memory by calling load_line until the file ends.  */
1029
1030 static try
1031 load_file (const char *filename, bool initial)
1032 {
1033   char *line;
1034   gfc_linebuf *b;
1035   gfc_file *f;
1036   FILE *input;
1037   int len, line_len;
1038
1039   for (f = current_file; f; f = f->up)
1040     if (strcmp (filename, f->filename) == 0)
1041       {
1042         gfc_error_now ("File '%s' is being included recursively", filename);
1043         return FAILURE;
1044       }
1045
1046   if (initial)
1047     {
1048       input = gfc_open_file (filename);
1049       if (input == NULL)
1050         {
1051           gfc_error_now ("Can't open file '%s'", filename);
1052           return FAILURE;
1053         }
1054     }
1055   else
1056     {
1057       input = gfc_open_included_file (filename, false);
1058       if (input == NULL)
1059         {
1060           gfc_error_now ("Can't open included file '%s'", filename);
1061           return FAILURE;
1062         }
1063     }
1064
1065   /* Load the file.  */
1066
1067   f = get_file (filename, initial ? LC_RENAME : LC_ENTER);
1068   f->up = current_file;
1069   current_file = f;
1070   current_file->line = 1;
1071   line = NULL;
1072   line_len = 0;
1073
1074   for (;;)
1075     {
1076       int trunc = load_line (input, &line, &line_len);
1077
1078       len = strlen (line);
1079       if (feof (input) && len == 0)
1080         break;
1081
1082       /* There are three things this line can be: a line of Fortran
1083          source, an include line or a C preprocessor directive.  */
1084
1085       if (line[0] == '#')
1086         {
1087           preprocessor_line (line);
1088           continue;
1089         }
1090
1091       if (include_line (line))
1092         {
1093           current_file->line++;
1094           continue;
1095         }
1096
1097       /* Add line.  */
1098
1099       b = gfc_getmem (gfc_linebuf_header_size + len + 1);
1100
1101 #ifdef USE_MAPPED_LOCATION
1102       b->location
1103         = linemap_line_start (&line_table, current_file->line++, 120);
1104 #else
1105       b->linenum = current_file->line++;
1106 #endif
1107       b->file = current_file;
1108       b->truncated = trunc;
1109       strcpy (b->line, line);
1110
1111       if (line_head == NULL)
1112         line_head = b;
1113       else
1114         line_tail->next = b;
1115
1116       line_tail = b;
1117     }
1118
1119   /* Release the line buffer allocated in load_line.  */
1120   gfc_free (line);
1121
1122   fclose (input);
1123
1124   current_file = current_file->up;
1125 #ifdef USE_MAPPED_LOCATION
1126   linemap_add (&line_table, LC_LEAVE, 0, NULL, 0);
1127 #endif
1128   return SUCCESS;
1129 }
1130
1131
1132 /* Open a new file and start scanning from that file. Returns SUCCESS
1133    if everything went OK, FAILURE otherwise.  If form == FORM_UKNOWN
1134    it tries to determine the source form from the filename, defaulting
1135    to free form.  */
1136
1137 try
1138 gfc_new_file (void)
1139 {
1140   try result;
1141
1142   result = load_file (gfc_source_file, true);
1143
1144   gfc_current_locus.lb = line_head;
1145   gfc_current_locus.nextc = (line_head == NULL) ? NULL : line_head->line;
1146
1147 #if 0 /* Debugging aid.  */
1148   for (; line_head; line_head = line_head->next)
1149     gfc_status ("%s:%3d %s\n", line_head->file->filename, 
1150 #ifdef USE_MAPPED_LOCATION
1151                 LOCATION_LINE (line_head->location),
1152 #else
1153                 line_head->linenum,
1154 #endif
1155                 line_head->line);
1156
1157   exit (0);
1158 #endif
1159
1160   return result;
1161 }