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