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