]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
re PR fortran/31154 (IMPORT fails for "<imported symbol> FUNCTION (...)" kind of...
[thirdparty/gcc.git] / gcc / fortran / decl.c
CommitLineData
6de9cd9a 1/* Declaration statement matcher
636dff67
SK
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
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 21
6de9cd9a 22#include "config.h"
d22e4895 23#include "system.h"
6de9cd9a
DN
24#include "gfortran.h"
25#include "match.h"
26#include "parse.h"
6de9cd9a 27
2054fc29 28/* This flag is set if an old-style length selector is matched
6de9cd9a
DN
29 during a type-declaration statement. */
30
31static int old_char_selector;
32
46fa431d 33/* When variables acquire types and attributes from a declaration
6de9cd9a
DN
34 statement, they get them from the following static variables. The
35 first part of a declaration sets these variables and the second
36 part copies these into symbol structures. */
37
38static gfc_typespec current_ts;
39
40static symbol_attribute current_attr;
41static gfc_array_spec *current_as;
42static int colon_seen;
43
a8b3b0b6
CR
44/* The current binding label (if any). */
45static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
46/* Need to know how many identifiers are on the current data declaration
47 line in case we're given the BIND(C) attribute with a NAME= specifier. */
48static int num_idents_on_line;
49/* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
50 can supply a name if the curr_binding_label is nil and NAME= was not. */
51static int has_name_equals = 0;
52
25d8f0a2
TS
53/* Initializer of the previous enumerator. */
54
55static gfc_expr *last_initializer;
56
57/* History of all the enumerators is maintained, so that
58 kind values of all the enumerators could be updated depending
59 upon the maximum initialized value. */
60
61typedef struct enumerator_history
62{
63 gfc_symbol *sym;
64 gfc_expr *initializer;
65 struct enumerator_history *next;
66}
67enumerator_history;
68
69/* Header of enum history chain. */
70
71static enumerator_history *enum_history = NULL;
72
73/* Pointer of enum history node containing largest initializer. */
74
75static enumerator_history *max_enum = NULL;
76
6de9cd9a
DN
77/* gfc_new_block points to the symbol of a newly matched block. */
78
79gfc_symbol *gfc_new_block;
80
e2d29968
PT
81locus gfc_function_kind_locus;
82locus gfc_function_type_locus;
83
6de9cd9a 84
294fbfc8
TS
85/********************* DATA statement subroutines *********************/
86
2220652d
PT
87static bool in_match_data = false;
88
89bool
90gfc_in_match_data (void)
91{
92 return in_match_data;
93}
94
95void
96gfc_set_in_match_data (bool set_value)
97{
98 in_match_data = set_value;
99}
100
294fbfc8
TS
101/* Free a gfc_data_variable structure and everything beneath it. */
102
103static void
636dff67 104free_variable (gfc_data_variable *p)
294fbfc8
TS
105{
106 gfc_data_variable *q;
107
108 for (; p; p = q)
109 {
110 q = p->next;
111 gfc_free_expr (p->expr);
112 gfc_free_iterator (&p->iter, 0);
113 free_variable (p->list);
294fbfc8
TS
114 gfc_free (p);
115 }
116}
117
118
119/* Free a gfc_data_value structure and everything beneath it. */
120
121static void
636dff67 122free_value (gfc_data_value *p)
294fbfc8
TS
123{
124 gfc_data_value *q;
125
126 for (; p; p = q)
127 {
128 q = p->next;
129 gfc_free_expr (p->expr);
130 gfc_free (p);
131 }
132}
133
134
135/* Free a list of gfc_data structures. */
136
137void
636dff67 138gfc_free_data (gfc_data *p)
294fbfc8
TS
139{
140 gfc_data *q;
141
142 for (; p; p = q)
143 {
144 q = p->next;
294fbfc8
TS
145 free_variable (p->var);
146 free_value (p->value);
294fbfc8
TS
147 gfc_free (p);
148 }
149}
150
151
a9f6f1f2 152/* Free all data in a namespace. */
636dff67 153
a9f6f1f2 154static void
66e4ab31 155gfc_free_data_all (gfc_namespace *ns)
a9f6f1f2
JD
156{
157 gfc_data *d;
158
159 for (;ns->data;)
160 {
161 d = ns->data->next;
162 gfc_free (ns->data);
163 ns->data = d;
164 }
165}
166
167
294fbfc8
TS
168static match var_element (gfc_data_variable *);
169
170/* Match a list of variables terminated by an iterator and a right
171 parenthesis. */
172
173static match
636dff67 174var_list (gfc_data_variable *parent)
294fbfc8
TS
175{
176 gfc_data_variable *tail, var;
177 match m;
178
179 m = var_element (&var);
180 if (m == MATCH_ERROR)
181 return MATCH_ERROR;
182 if (m == MATCH_NO)
183 goto syntax;
184
185 tail = gfc_get_data_variable ();
186 *tail = var;
187
188 parent->list = tail;
189
190 for (;;)
191 {
192 if (gfc_match_char (',') != MATCH_YES)
193 goto syntax;
194
195 m = gfc_match_iterator (&parent->iter, 1);
196 if (m == MATCH_YES)
197 break;
198 if (m == MATCH_ERROR)
199 return MATCH_ERROR;
200
201 m = var_element (&var);
202 if (m == MATCH_ERROR)
203 return MATCH_ERROR;
204 if (m == MATCH_NO)
205 goto syntax;
206
207 tail->next = gfc_get_data_variable ();
208 tail = tail->next;
209
210 *tail = var;
211 }
212
213 if (gfc_match_char (')') != MATCH_YES)
214 goto syntax;
215 return MATCH_YES;
216
217syntax:
218 gfc_syntax_error (ST_DATA);
219 return MATCH_ERROR;
220}
221
222
223/* Match a single element in a data variable list, which can be a
224 variable-iterator list. */
225
226static match
636dff67 227var_element (gfc_data_variable *new)
294fbfc8
TS
228{
229 match m;
230 gfc_symbol *sym;
231
232 memset (new, 0, sizeof (gfc_data_variable));
233
234 if (gfc_match_char ('(') == MATCH_YES)
235 return var_list (new);
236
237 m = gfc_match_variable (&new->expr, 0);
238 if (m != MATCH_YES)
239 return m;
240
241 sym = new->expr->symtree->n.sym;
242
636dff67
SK
243 if (!sym->attr.function && gfc_current_ns->parent
244 && gfc_current_ns->parent == sym->ns)
294fbfc8 245 {
4075a94e 246 gfc_error ("Host associated variable '%s' may not be in the DATA "
e25a0da3 247 "statement at %C", sym->name);
294fbfc8
TS
248 return MATCH_ERROR;
249 }
250
4075a94e 251 if (gfc_current_state () != COMP_BLOCK_DATA
636dff67
SK
252 && sym->attr.in_common
253 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
254 "common block variable '%s' in DATA statement at %C",
255 sym->name) == FAILURE)
4075a94e 256 return MATCH_ERROR;
294fbfc8 257
231b2fcc 258 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
294fbfc8
TS
259 return MATCH_ERROR;
260
261 return MATCH_YES;
262}
263
264
265/* Match the top-level list of data variables. */
266
267static match
636dff67 268top_var_list (gfc_data *d)
294fbfc8
TS
269{
270 gfc_data_variable var, *tail, *new;
271 match m;
272
273 tail = NULL;
274
275 for (;;)
276 {
277 m = var_element (&var);
278 if (m == MATCH_NO)
279 goto syntax;
280 if (m == MATCH_ERROR)
281 return MATCH_ERROR;
282
283 new = gfc_get_data_variable ();
284 *new = var;
285
286 if (tail == NULL)
287 d->var = new;
288 else
289 tail->next = new;
290
291 tail = new;
292
293 if (gfc_match_char ('/') == MATCH_YES)
294 break;
295 if (gfc_match_char (',') != MATCH_YES)
296 goto syntax;
297 }
298
299 return MATCH_YES;
300
301syntax:
302 gfc_syntax_error (ST_DATA);
a9f6f1f2 303 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
304 return MATCH_ERROR;
305}
306
307
308static match
636dff67 309match_data_constant (gfc_expr **result)
294fbfc8
TS
310{
311 char name[GFC_MAX_SYMBOL_LEN + 1];
312 gfc_symbol *sym;
313 gfc_expr *expr;
314 match m;
36d3fb4c 315 locus old_loc;
294fbfc8
TS
316
317 m = gfc_match_literal_constant (&expr, 1);
318 if (m == MATCH_YES)
319 {
320 *result = expr;
321 return MATCH_YES;
322 }
323
324 if (m == MATCH_ERROR)
325 return MATCH_ERROR;
326
327 m = gfc_match_null (result);
328 if (m != MATCH_NO)
329 return m;
330
36d3fb4c
PT
331 old_loc = gfc_current_locus;
332
333 /* Should this be a structure component, try to match it
334 before matching a name. */
335 m = gfc_match_rvalue (result);
336 if (m == MATCH_ERROR)
337 return m;
338
339 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
340 {
341 if (gfc_simplify_expr (*result, 0) == FAILURE)
342 m = MATCH_ERROR;
343 return m;
344 }
345
346 gfc_current_locus = old_loc;
347
294fbfc8
TS
348 m = gfc_match_name (name);
349 if (m != MATCH_YES)
350 return m;
351
352 if (gfc_find_symbol (name, NULL, 1, &sym))
353 return MATCH_ERROR;
354
355 if (sym == NULL
356 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
357 {
358 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
359 name);
360 return MATCH_ERROR;
361 }
362 else if (sym->attr.flavor == FL_DERIVED)
363 return gfc_match_structure_constructor (sym, result);
364
365 *result = gfc_copy_expr (sym->value);
366 return MATCH_YES;
367}
368
369
370/* Match a list of values in a DATA statement. The leading '/' has
371 already been seen at this point. */
372
373static match
636dff67 374top_val_list (gfc_data *data)
294fbfc8
TS
375{
376 gfc_data_value *new, *tail;
377 gfc_expr *expr;
378 const char *msg;
379 match m;
380
381 tail = NULL;
382
383 for (;;)
384 {
385 m = match_data_constant (&expr);
386 if (m == MATCH_NO)
387 goto syntax;
388 if (m == MATCH_ERROR)
389 return MATCH_ERROR;
390
391 new = gfc_get_data_value ();
392
393 if (tail == NULL)
394 data->value = new;
395 else
396 tail->next = new;
397
398 tail = new;
399
400 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
401 {
402 tail->expr = expr;
403 tail->repeat = 1;
404 }
405 else
406 {
407 signed int tmp;
408 msg = gfc_extract_int (expr, &tmp);
409 gfc_free_expr (expr);
410 if (msg != NULL)
411 {
412 gfc_error (msg);
413 return MATCH_ERROR;
414 }
415 tail->repeat = tmp;
416
417 m = match_data_constant (&tail->expr);
418 if (m == MATCH_NO)
419 goto syntax;
420 if (m == MATCH_ERROR)
421 return MATCH_ERROR;
422 }
423
424 if (gfc_match_char ('/') == MATCH_YES)
425 break;
426 if (gfc_match_char (',') == MATCH_NO)
427 goto syntax;
428 }
429
430 return MATCH_YES;
431
432syntax:
433 gfc_syntax_error (ST_DATA);
a9f6f1f2 434 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
435 return MATCH_ERROR;
436}
437
438
439/* Matches an old style initialization. */
440
441static match
442match_old_style_init (const char *name)
443{
444 match m;
445 gfc_symtree *st;
ed0e3607 446 gfc_symbol *sym;
294fbfc8
TS
447 gfc_data *newdata;
448
449 /* Set up data structure to hold initializers. */
450 gfc_find_sym_tree (name, NULL, 0, &st);
ed0e3607
AL
451 sym = st->n.sym;
452
294fbfc8
TS
453 newdata = gfc_get_data ();
454 newdata->var = gfc_get_data_variable ();
455 newdata->var->expr = gfc_get_variable_expr (st);
8c5c0b80 456 newdata->where = gfc_current_locus;
294fbfc8 457
66e4ab31 458 /* Match initial value list. This also eats the terminal '/'. */
294fbfc8
TS
459 m = top_val_list (newdata);
460 if (m != MATCH_YES)
461 {
462 gfc_free (newdata);
463 return m;
464 }
465
466 if (gfc_pure (NULL))
467 {
468 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
469 gfc_free (newdata);
470 return MATCH_ERROR;
471 }
472
ed0e3607
AL
473 /* Mark the variable as having appeared in a data statement. */
474 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
475 {
476 gfc_free (newdata);
477 return MATCH_ERROR;
478 }
479
294fbfc8
TS
480 /* Chain in namespace list of DATA initializers. */
481 newdata->next = gfc_current_ns->data;
482 gfc_current_ns->data = newdata;
483
484 return m;
485}
486
636dff67 487
294fbfc8 488/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
13795658 489 we are matching a DATA statement and are therefore issuing an error
d51347f9 490 if we encounter something unexpected, if not, we're trying to match
69de3b83 491 an old-style initialization expression of the form INTEGER I /2/. */
294fbfc8
TS
492
493match
494gfc_match_data (void)
495{
496 gfc_data *new;
497 match m;
498
2220652d
PT
499 gfc_set_in_match_data (true);
500
294fbfc8
TS
501 for (;;)
502 {
503 new = gfc_get_data ();
504 new->where = gfc_current_locus;
505
506 m = top_var_list (new);
507 if (m != MATCH_YES)
508 goto cleanup;
509
510 m = top_val_list (new);
511 if (m != MATCH_YES)
512 goto cleanup;
513
514 new->next = gfc_current_ns->data;
515 gfc_current_ns->data = new;
516
517 if (gfc_match_eos () == MATCH_YES)
518 break;
519
520 gfc_match_char (','); /* Optional comma */
521 }
522
2220652d
PT
523 gfc_set_in_match_data (false);
524
294fbfc8
TS
525 if (gfc_pure (NULL))
526 {
527 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
528 return MATCH_ERROR;
529 }
530
531 return MATCH_YES;
532
533cleanup:
2220652d 534 gfc_set_in_match_data (false);
294fbfc8
TS
535 gfc_free_data (new);
536 return MATCH_ERROR;
537}
538
539
540/************************ Declaration statements *********************/
541
6de9cd9a
DN
542/* Match an intent specification. Since this can only happen after an
543 INTENT word, a legal intent-spec must follow. */
544
545static sym_intent
546match_intent_spec (void)
547{
548
549 if (gfc_match (" ( in out )") == MATCH_YES)
550 return INTENT_INOUT;
551 if (gfc_match (" ( in )") == MATCH_YES)
552 return INTENT_IN;
553 if (gfc_match (" ( out )") == MATCH_YES)
554 return INTENT_OUT;
555
556 gfc_error ("Bad INTENT specification at %C");
557 return INTENT_UNKNOWN;
558}
559
560
561/* Matches a character length specification, which is either a
562 specification expression or a '*'. */
563
564static match
636dff67 565char_len_param_value (gfc_expr **expr)
6de9cd9a 566{
6de9cd9a
DN
567 if (gfc_match_char ('*') == MATCH_YES)
568 {
569 *expr = NULL;
570 return MATCH_YES;
571 }
572
573 return gfc_match_expr (expr);
574}
575
576
577/* A character length is a '*' followed by a literal integer or a
578 char_len_param_value in parenthesis. */
579
580static match
636dff67 581match_char_length (gfc_expr **expr)
6de9cd9a 582{
5cf54585 583 int length;
6de9cd9a
DN
584 match m;
585
586 m = gfc_match_char ('*');
587 if (m != MATCH_YES)
588 return m;
589
5cf54585 590 m = gfc_match_small_literal_int (&length, NULL);
6de9cd9a
DN
591 if (m == MATCH_ERROR)
592 return m;
593
594 if (m == MATCH_YES)
595 {
596 *expr = gfc_int_expr (length);
597 return m;
598 }
599
600 if (gfc_match_char ('(') == MATCH_NO)
601 goto syntax;
602
603 m = char_len_param_value (expr);
604 if (m == MATCH_ERROR)
605 return m;
606 if (m == MATCH_NO)
607 goto syntax;
608
609 if (gfc_match_char (')') == MATCH_NO)
610 {
611 gfc_free_expr (*expr);
612 *expr = NULL;
613 goto syntax;
614 }
615
616 return MATCH_YES;
617
618syntax:
619 gfc_error ("Syntax error in character length specification at %C");
620 return MATCH_ERROR;
621}
622
623
9e35b386
EE
624/* Special subroutine for finding a symbol. Check if the name is found
625 in the current name space. If not, and we're compiling a function or
626 subroutine and the parent compilation unit is an interface, then check
627 to see if the name we've been given is the name of the interface
628 (located in another namespace). */
6de9cd9a
DN
629
630static int
636dff67 631find_special (const char *name, gfc_symbol **result)
6de9cd9a
DN
632{
633 gfc_state_data *s;
9e35b386 634 int i;
6de9cd9a 635
9e35b386 636 i = gfc_get_symbol (name, NULL, result);
d51347f9 637 if (i == 0)
9e35b386 638 goto end;
d51347f9 639
6de9cd9a
DN
640 if (gfc_current_state () != COMP_SUBROUTINE
641 && gfc_current_state () != COMP_FUNCTION)
9e35b386 642 goto end;
6de9cd9a
DN
643
644 s = gfc_state_stack->previous;
645 if (s == NULL)
9e35b386 646 goto end;
6de9cd9a
DN
647
648 if (s->state != COMP_INTERFACE)
9e35b386 649 goto end;
6de9cd9a 650 if (s->sym == NULL)
66e4ab31 651 goto end; /* Nameless interface. */
6de9cd9a
DN
652
653 if (strcmp (name, s->sym->name) == 0)
654 {
655 *result = s->sym;
656 return 0;
657 }
658
9e35b386
EE
659end:
660 return i;
6de9cd9a
DN
661}
662
663
664/* Special subroutine for getting a symbol node associated with a
665 procedure name, used in SUBROUTINE and FUNCTION statements. The
666 symbol is created in the parent using with symtree node in the
667 child unit pointing to the symbol. If the current namespace has no
668 parent, then the symbol is just created in the current unit. */
669
670static int
636dff67 671get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
6de9cd9a
DN
672{
673 gfc_symtree *st;
674 gfc_symbol *sym;
675 int rc;
676
1a492601
PT
677 /* Module functions have to be left in their own namespace because
678 they have potentially (almost certainly!) already been referenced.
679 In this sense, they are rather like external functions. This is
680 fixed up in resolve.c(resolve_entries), where the symbol name-
681 space is set to point to the master function, so that the fake
682 result mechanism can work. */
683 if (module_fcn_entry)
6c12686b
PT
684 {
685 /* Present if entry is declared to be a module procedure. */
686 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
aa84a9a5 687
6c12686b
PT
688 if (*result == NULL)
689 rc = gfc_get_symbol (name, NULL, result);
aa84a9a5
PT
690 else if (gfc_get_symbol (name, NULL, &sym) == 0
691 && sym
692 && sym->ts.type != BT_UNKNOWN
693 && (*result)->ts.type == BT_UNKNOWN
694 && sym->attr.flavor == FL_UNKNOWN)
695 /* Pick up the typespec for the entry, if declared in the function
696 body. Note that this symbol is FL_UNKNOWN because it will
697 only have appeared in a type declaration. The local symtree
698 is set to point to the module symbol and a unique symtree
699 to the local version. This latter ensures a correct clearing
700 of the symbols. */
701 {
702 (*result)->ts = sym->ts;
703 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
704 st->n.sym = *result;
705 st = gfc_get_unique_symtree (gfc_current_ns);
706 st->n.sym = sym;
707 }
6c12686b 708 }
68ea355b
PT
709 else
710 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
6de9cd9a 711
68ea355b 712 sym = *result;
2c693a24 713 gfc_current_ns->refs++;
6de9cd9a 714
68ea355b
PT
715 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
716 {
cda7004b
PT
717 /* Trap another encompassed procedure with the same name. All
718 these conditions are necessary to avoid picking up an entry
719 whose name clashes with that of the encompassing procedure;
720 this is handled using gsymbols to register unique,globally
721 accessible names. */
68ea355b 722 if (sym->attr.flavor != 0
636dff67
SK
723 && sym->attr.proc != 0
724 && (sym->attr.subroutine || sym->attr.function)
725 && sym->attr.if_source != IFSRC_UNKNOWN)
68ea355b
PT
726 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
727 name, &sym->declared_at);
728
fd3e70af
JD
729 /* Trap a procedure with a name the same as interface in the
730 encompassing scope. */
731 if (sym->attr.generic != 0
2305fa31
JD
732 && (sym->attr.subroutine || sym->attr.function)
733 && !sym->attr.mod_proc)
fd3e70af
JD
734 gfc_error_now ("Name '%s' at %C is already defined"
735 " as a generic interface at %L",
736 name, &sym->declared_at);
737
68ea355b
PT
738 /* Trap declarations of attributes in encompassing scope. The
739 signature for this is that ts.kind is set. Legitimate
740 references only set ts.type. */
741 if (sym->ts.kind != 0
636dff67
SK
742 && !sym->attr.implicit_type
743 && sym->attr.proc == 0
744 && gfc_current_ns->parent != NULL
745 && sym->attr.access == 0
746 && !module_fcn_entry)
747 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
748 "and must not have attributes declared at %L",
68ea355b
PT
749 name, &sym->declared_at);
750 }
751
752 if (gfc_current_ns->parent == NULL || *result == NULL)
753 return rc;
6de9cd9a 754
1a492601
PT
755 /* Module function entries will already have a symtree in
756 the current namespace but will need one at module level. */
757 if (module_fcn_entry)
6c12686b
PT
758 {
759 /* Present if entry is declared to be a module procedure. */
760 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
761 if (st == NULL)
762 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
763 }
1a492601
PT
764 else
765 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
6de9cd9a 766
6de9cd9a
DN
767 st->n.sym = sym;
768 sym->refs++;
769
66e4ab31 770 /* See if the procedure should be a module procedure. */
6de9cd9a 771
1a492601 772 if (((sym->ns->proc_name != NULL
6c12686b
PT
773 && sym->ns->proc_name->attr.flavor == FL_MODULE
774 && sym->attr.proc != PROC_MODULE)
775 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
776 && gfc_add_procedure (&sym->attr, PROC_MODULE,
777 sym->name, NULL) == FAILURE)
6de9cd9a
DN
778 rc = 2;
779
780 return rc;
781}
782
783
a8b3b0b6
CR
784/* Verify that the given symbol representing a parameter is C
785 interoperable, by checking to see if it was marked as such after
786 its declaration. If the given symbol is not interoperable, a
787 warning is reported, thus removing the need to return the status to
788 the calling function. The standard does not require the user use
789 one of the iso_c_binding named constants to declare an
790 interoperable parameter, but we can't be sure if the param is C
791 interop or not if the user doesn't. For example, integer(4) may be
792 legal Fortran, but doesn't have meaning in C. It may interop with
793 a number of the C types, which causes a problem because the
794 compiler can't know which one. This code is almost certainly not
795 portable, and the user will get what they deserve if the C type
796 across platforms isn't always interoperable with integer(4). If
797 the user had used something like integer(c_int) or integer(c_long),
798 the compiler could have automatically handled the varying sizes
799 across platforms. */
800
801try
802verify_c_interop_param (gfc_symbol *sym)
803{
804 int is_c_interop = 0;
805 try retval = SUCCESS;
806
807 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
808 Don't repeat the checks here. */
809 if (sym->attr.implicit_type)
810 return SUCCESS;
811
812 /* For subroutines or functions that are passed to a BIND(C) procedure,
813 they're interoperable if they're BIND(C) and their params are all
814 interoperable. */
815 if (sym->attr.flavor == FL_PROCEDURE)
816 {
817 if (sym->attr.is_bind_c == 0)
818 {
819 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
820 "attribute to be C interoperable", sym->name,
821 &(sym->declared_at));
822
823 return FAILURE;
824 }
825 else
826 {
827 if (sym->attr.is_c_interop == 1)
828 /* We've already checked this procedure; don't check it again. */
829 return SUCCESS;
830 else
831 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
832 sym->common_block);
833 }
834 }
835
836 /* See if we've stored a reference to a procedure that owns sym. */
837 if (sym->ns != NULL && sym->ns->proc_name != NULL)
838 {
839 if (sym->ns->proc_name->attr.is_bind_c == 1)
840 {
841 is_c_interop =
842 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
843 == SUCCESS ? 1 : 0);
844
845 if (is_c_interop != 1)
846 {
847 /* Make personalized messages to give better feedback. */
848 if (sym->ts.type == BT_DERIVED)
849 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
850 " procedure '%s' but is not C interoperable "
851 "because derived type '%s' is not C interoperable",
852 sym->name, &(sym->declared_at),
853 sym->ns->proc_name->name,
854 sym->ts.derived->name);
855 else
856 gfc_warning ("Variable '%s' at %L is a parameter to the "
857 "BIND(C) procedure '%s' but may not be C "
858 "interoperable",
859 sym->name, &(sym->declared_at),
860 sym->ns->proc_name->name);
861 }
aa5e22f0
CR
862
863 /* Character strings are only C interoperable if they have a
864 length of 1. */
865 if (sym->ts.type == BT_CHARACTER)
866 {
867 gfc_charlen *cl = sym->ts.cl;
868 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
869 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
870 {
871 gfc_error ("Character argument '%s' at %L "
872 "must be length 1 because "
873 "procedure '%s' is BIND(C)",
874 sym->name, &sym->declared_at,
875 sym->ns->proc_name->name);
876 retval = FAILURE;
877 }
878 }
879
a8b3b0b6
CR
880 /* We have to make sure that any param to a bind(c) routine does
881 not have the allocatable, pointer, or optional attributes,
882 according to J3/04-007, section 5.1. */
883 if (sym->attr.allocatable == 1)
884 {
885 gfc_error ("Variable '%s' at %L cannot have the "
886 "ALLOCATABLE attribute because procedure '%s'"
887 " is BIND(C)", sym->name, &(sym->declared_at),
888 sym->ns->proc_name->name);
889 retval = FAILURE;
890 }
891
892 if (sym->attr.pointer == 1)
893 {
894 gfc_error ("Variable '%s' at %L cannot have the "
895 "POINTER attribute because procedure '%s'"
896 " is BIND(C)", sym->name, &(sym->declared_at),
897 sym->ns->proc_name->name);
898 retval = FAILURE;
899 }
900
901 if (sym->attr.optional == 1)
902 {
903 gfc_error ("Variable '%s' at %L cannot have the "
904 "OPTIONAL attribute because procedure '%s'"
905 " is BIND(C)", sym->name, &(sym->declared_at),
906 sym->ns->proc_name->name);
907 retval = FAILURE;
908 }
909
910 /* Make sure that if it has the dimension attribute, that it is
911 either assumed size or explicit shape. */
912 if (sym->as != NULL)
913 {
914 if (sym->as->type == AS_ASSUMED_SHAPE)
915 {
916 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
917 "argument to the procedure '%s' at %L because "
918 "the procedure is BIND(C)", sym->name,
919 &(sym->declared_at), sym->ns->proc_name->name,
920 &(sym->ns->proc_name->declared_at));
921 retval = FAILURE;
922 }
923
924 if (sym->as->type == AS_DEFERRED)
925 {
926 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
927 "argument to the procedure '%s' at %L because "
928 "the procedure is BIND(C)", sym->name,
929 &(sym->declared_at), sym->ns->proc_name->name,
930 &(sym->ns->proc_name->declared_at));
931 retval = FAILURE;
932 }
933 }
934 }
935 }
936
937 return retval;
938}
939
940
941/* Function called by variable_decl() that adds a name to the symbol table. */
6de9cd9a
DN
942
943static try
636dff67
SK
944build_sym (const char *name, gfc_charlen *cl,
945 gfc_array_spec **as, locus *var_locus)
6de9cd9a
DN
946{
947 symbol_attribute attr;
948 gfc_symbol *sym;
949
9e35b386 950 if (gfc_get_symbol (name, NULL, &sym))
6de9cd9a
DN
951 return FAILURE;
952
66e4ab31 953 /* Start updating the symbol table. Add basic type attribute if present. */
6de9cd9a 954 if (current_ts.type != BT_UNKNOWN
636dff67
SK
955 && (sym->attr.implicit_type == 0
956 || !gfc_compare_types (&sym->ts, &current_ts))
6de9cd9a
DN
957 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
958 return FAILURE;
959
960 if (sym->ts.type == BT_CHARACTER)
961 sym->ts.cl = cl;
962
963 /* Add dimension attribute if present. */
964 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
965 return FAILURE;
966 *as = NULL;
967
968 /* Add attribute to symbol. The copy is so that we can reset the
969 dimension attribute. */
970 attr = current_attr;
971 attr.dimension = 0;
972
973 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
974 return FAILURE;
975
a8b3b0b6
CR
976 /* Finish any work that may need to be done for the binding label,
977 if it's a bind(c). The bind(c) attr is found before the symbol
978 is made, and before the symbol name (for data decls), so the
979 current_ts is holding the binding label, or nothing if the
980 name= attr wasn't given. Therefore, test here if we're dealing
981 with a bind(c) and make sure the binding label is set correctly. */
982 if (sym->attr.is_bind_c == 1)
983 {
984 if (sym->binding_label[0] == '\0')
985 {
ad4a2f64
TB
986 /* Set the binding label and verify that if a NAME= was specified
987 then only one identifier was in the entity-decl-list. */
988 if (set_binding_label (sym->binding_label, sym->name,
989 num_idents_on_line) == FAILURE)
a8b3b0b6
CR
990 return FAILURE;
991 }
992 }
993
994 /* See if we know we're in a common block, and if it's a bind(c)
995 common then we need to make sure we're an interoperable type. */
996 if (sym->attr.in_common == 1)
997 {
998 /* Test the common block object. */
999 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1000 && sym->ts.is_c_interop != 1)
1001 {
1002 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1003 "must be declared with a C interoperable "
1004 "kind since common block '%s' is BIND(C)",
1005 sym->name, sym->common_block->name,
1006 sym->common_block->name);
1007 gfc_clear_error ();
1008 }
1009 }
1010
9a3db5a3
PT
1011 sym->attr.implied_index = 0;
1012
6de9cd9a
DN
1013 return SUCCESS;
1014}
1015
636dff67 1016
df7cc9b5
FW
1017/* Set character constant to the given length. The constant will be padded or
1018 truncated. */
1019
1020void
636dff67 1021gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
df7cc9b5 1022{
636dff67 1023 char *s;
df7cc9b5
FW
1024 int slen;
1025
1026 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1027 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1028
1029 slen = expr->value.character.length;
1030 if (len != slen)
1031 {
150675a8 1032 s = gfc_getmem (len + 1);
df7cc9b5
FW
1033 memcpy (s, expr->value.character.string, MIN (len, slen));
1034 if (len > slen)
1035 memset (&s[slen], ' ', len - slen);
2220652d
PT
1036
1037 if (gfc_option.warn_character_truncation && slen > len)
1038 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1039 "(%d/%d)", &expr->where, slen, len);
1040
1041 /* Apply the standard by 'hand' otherwise it gets cleared for
1042 initializers. */
1043 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1044 gfc_error_now ("The CHARACTER elements of the array constructor "
1045 "at %L must have the same length (%d/%d)",
636dff67 1046 &expr->where, slen, len);
2220652d 1047
150675a8 1048 s[len] = '\0';
df7cc9b5
FW
1049 gfc_free (expr->value.character.string);
1050 expr->value.character.string = s;
1051 expr->value.character.length = len;
1052 }
1053}
6de9cd9a 1054
25d8f0a2 1055
d51347f9 1056/* Function to create and update the enumerator history
25d8f0a2 1057 using the information passed as arguments.
d51347f9
TB
1058 Pointer "max_enum" is also updated, to point to
1059 enum history node containing largest initializer.
25d8f0a2
TS
1060
1061 SYM points to the symbol node of enumerator.
66e4ab31 1062 INIT points to its enumerator value. */
25d8f0a2 1063
d51347f9 1064static void
636dff67 1065create_enum_history (gfc_symbol *sym, gfc_expr *init)
25d8f0a2
TS
1066{
1067 enumerator_history *new_enum_history;
1068 gcc_assert (sym != NULL && init != NULL);
1069
1070 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1071
1072 new_enum_history->sym = sym;
1073 new_enum_history->initializer = init;
1074 new_enum_history->next = NULL;
1075
1076 if (enum_history == NULL)
1077 {
1078 enum_history = new_enum_history;
1079 max_enum = enum_history;
1080 }
1081 else
1082 {
1083 new_enum_history->next = enum_history;
1084 enum_history = new_enum_history;
1085
d51347f9 1086 if (mpz_cmp (max_enum->initializer->value.integer,
25d8f0a2 1087 new_enum_history->initializer->value.integer) < 0)
636dff67 1088 max_enum = new_enum_history;
25d8f0a2
TS
1089 }
1090}
1091
1092
d51347f9 1093/* Function to free enum kind history. */
25d8f0a2 1094
d51347f9 1095void
636dff67 1096gfc_free_enum_history (void)
25d8f0a2 1097{
d51347f9
TB
1098 enumerator_history *current = enum_history;
1099 enumerator_history *next;
25d8f0a2
TS
1100
1101 while (current != NULL)
1102 {
1103 next = current->next;
1104 gfc_free (current);
1105 current = next;
1106 }
1107 max_enum = NULL;
1108 enum_history = NULL;
1109}
1110
1111
6de9cd9a
DN
1112/* Function called by variable_decl() that adds an initialization
1113 expression to a symbol. */
1114
1115static try
66e4ab31 1116add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
6de9cd9a
DN
1117{
1118 symbol_attribute attr;
1119 gfc_symbol *sym;
1120 gfc_expr *init;
1121
1122 init = *initp;
1123 if (find_special (name, &sym))
1124 return FAILURE;
1125
1126 attr = sym->attr;
1127
1128 /* If this symbol is confirming an implicit parameter type,
1129 then an initialization expression is not allowed. */
1130 if (attr.flavor == FL_PARAMETER
1131 && sym->value != NULL
1132 && *initp != NULL)
1133 {
1134 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1135 sym->name);
1136 return FAILURE;
1137 }
1138
c8e20bd0
TS
1139 if (attr.in_common
1140 && !attr.data
1141 && *initp != NULL)
1142 {
1143 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1144 sym->name);
1145 return FAILURE;
1146 }
1147
6de9cd9a
DN
1148 if (init == NULL)
1149 {
1150 /* An initializer is required for PARAMETER declarations. */
1151 if (attr.flavor == FL_PARAMETER)
1152 {
1153 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1154 return FAILURE;
1155 }
1156 }
1157 else
1158 {
1159 /* If a variable appears in a DATA block, it cannot have an
1de8a836 1160 initializer. */
6de9cd9a
DN
1161 if (sym->attr.data)
1162 {
636dff67
SK
1163 gfc_error ("Variable '%s' at %C with an initializer already "
1164 "appears in a DATA statement", sym->name);
6de9cd9a
DN
1165 return FAILURE;
1166 }
1167
75d17889
TS
1168 /* Check if the assignment can happen. This has to be put off
1169 until later for a derived type variable. */
6de9cd9a
DN
1170 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1171 && gfc_check_assign_symbol (sym, init) == FAILURE)
1172 return FAILURE;
1173
df7cc9b5
FW
1174 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1175 {
1176 /* Update symbol character length according initializer. */
1177 if (sym->ts.cl->length == NULL)
1178 {
a99288e5 1179 int clen;
66e4ab31
SK
1180 /* If there are multiple CHARACTER variables declared on the
1181 same line, we don't want them to share the same length. */
4213f93b
PT
1182 sym->ts.cl = gfc_get_charlen ();
1183 sym->ts.cl->next = gfc_current_ns->cl_list;
1184 gfc_current_ns->cl_list = sym->ts.cl;
96f4873b 1185
a99288e5
PT
1186 if (sym->attr.flavor == FL_PARAMETER)
1187 {
1188 if (init->expr_type == EXPR_CONSTANT)
1189 {
1190 clen = init->value.character.length;
1191 sym->ts.cl->length = gfc_int_expr (clen);
1192 }
1193 else if (init->expr_type == EXPR_ARRAY)
1194 {
1195 gfc_expr *p = init->value.constructor->expr;
1196 clen = p->value.character.length;
1197 sym->ts.cl->length = gfc_int_expr (clen);
1198 }
1199 else if (init->ts.cl && init->ts.cl->length)
1200 sym->ts.cl->length =
1201 gfc_copy_expr (sym->value->ts.cl->length);
1202 }
df7cc9b5
FW
1203 }
1204 /* Update initializer character length according symbol. */
1205 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1206 {
1207 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1208 gfc_constructor * p;
1209
1210 if (init->expr_type == EXPR_CONSTANT)
2220652d 1211 gfc_set_constant_character_len (len, init, false);
df7cc9b5
FW
1212 else if (init->expr_type == EXPR_ARRAY)
1213 {
dcdc7b6c
PT
1214 /* Build a new charlen to prevent simplification from
1215 deleting the length before it is resolved. */
1216 init->ts.cl = gfc_get_charlen ();
1217 init->ts.cl->next = gfc_current_ns->cl_list;
1218 gfc_current_ns->cl_list = sym->ts.cl;
df7cc9b5 1219 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
dcdc7b6c 1220
df7cc9b5 1221 for (p = init->value.constructor; p; p = p->next)
2220652d 1222 gfc_set_constant_character_len (len, p->expr, false);
df7cc9b5
FW
1223 }
1224 }
1225 }
1226
a8b3b0b6
CR
1227 /* Need to check if the expression we initialized this
1228 to was one of the iso_c_binding named constants. If so,
1229 and we're a parameter (constant), let it be iso_c.
1230 For example:
1231 integer(c_int), parameter :: my_int = c_int
1232 integer(my_int) :: my_int_2
1233 If we mark my_int as iso_c (since we can see it's value
1234 is equal to one of the named constants), then my_int_2
1235 will be considered C interoperable. */
1236 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1237 {
1238 sym->ts.is_iso_c |= init->ts.is_iso_c;
1239 sym->ts.is_c_interop |= init->ts.is_c_interop;
1240 /* attr bits needed for module files. */
1241 sym->attr.is_iso_c |= init->ts.is_iso_c;
1242 sym->attr.is_c_interop |= init->ts.is_c_interop;
1243 if (init->ts.is_iso_c)
1244 sym->ts.f90_type = init->ts.f90_type;
1245 }
1246
6de9cd9a
DN
1247 /* Add initializer. Make sure we keep the ranks sane. */
1248 if (sym->attr.dimension && init->rank == 0)
a9b43781
PT
1249 {
1250 mpz_t size;
1251 gfc_expr *array;
1252 gfc_constructor *c;
1253 int n;
1254 if (sym->attr.flavor == FL_PARAMETER
1255 && init->expr_type == EXPR_CONSTANT
1256 && spec_size (sym->as, &size) == SUCCESS
1257 && mpz_cmp_si (size, 0) > 0)
1258 {
1259 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1260 &init->where);
1261
1262 array->value.constructor = c = NULL;
1263 for (n = 0; n < (int)mpz_get_si (size); n++)
1264 {
1265 if (array->value.constructor == NULL)
1266 {
1267 array->value.constructor = c = gfc_get_constructor ();
1268 c->expr = init;
1269 }
1270 else
1271 {
1272 c->next = gfc_get_constructor ();
1273 c = c->next;
1274 c->expr = gfc_copy_expr (init);
1275 }
1276 }
1277
1278 array->shape = gfc_get_shape (sym->as->rank);
1279 for (n = 0; n < sym->as->rank; n++)
1280 spec_dimen_size (sym->as, n, &array->shape[n]);
1281
1282 init = array;
1283 mpz_clear (size);
1284 }
1285 init->rank = sym->as->rank;
1286 }
6de9cd9a
DN
1287
1288 sym->value = init;
ef7236d2
DF
1289 if (sym->attr.save == SAVE_NONE)
1290 sym->attr.save = SAVE_IMPLICIT;
6de9cd9a
DN
1291 *initp = NULL;
1292 }
1293
1294 return SUCCESS;
1295}
1296
1297
1298/* Function called by variable_decl() that adds a name to a structure
1299 being built. */
1300
1301static try
636dff67
SK
1302build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1303 gfc_array_spec **as)
6de9cd9a
DN
1304{
1305 gfc_component *c;
1306
1307 /* If the current symbol is of the same derived type that we're
1308 constructing, it must have the pointer attribute. */
1309 if (current_ts.type == BT_DERIVED
1310 && current_ts.derived == gfc_current_block ()
1311 && current_attr.pointer == 0)
1312 {
1313 gfc_error ("Component at %C must have the POINTER attribute");
1314 return FAILURE;
1315 }
1316
636dff67 1317 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
6de9cd9a
DN
1318 {
1319 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1320 {
1321 gfc_error ("Array component of structure at %C must have explicit "
1322 "or deferred shape");
1323 return FAILURE;
1324 }
1325 }
1326
1327 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1328 return FAILURE;
1329
1330 c->ts = current_ts;
1331 c->ts.cl = cl;
1332 gfc_set_component_attr (c, &current_attr);
1333
1334 c->initializer = *init;
1335 *init = NULL;
1336
1337 c->as = *as;
1338 if (c->as != NULL)
1339 c->dimension = 1;
1340 *as = NULL;
1341
1342 /* Check array components. */
1343 if (!c->dimension)
5046aff5
PT
1344 {
1345 if (c->allocatable)
1346 {
1347 gfc_error ("Allocatable component at %C must be an array");
1348 return FAILURE;
1349 }
1350 else
1351 return SUCCESS;
1352 }
6de9cd9a
DN
1353
1354 if (c->pointer)
1355 {
1356 if (c->as->type != AS_DEFERRED)
1357 {
5046aff5
PT
1358 gfc_error ("Pointer array component of structure at %C must have a "
1359 "deferred shape");
1360 return FAILURE;
1361 }
1362 }
1363 else if (c->allocatable)
1364 {
1365 if (c->as->type != AS_DEFERRED)
1366 {
1367 gfc_error ("Allocatable component of structure at %C must have a "
1368 "deferred shape");
6de9cd9a
DN
1369 return FAILURE;
1370 }
1371 }
1372 else
1373 {
1374 if (c->as->type != AS_EXPLICIT)
1375 {
636dff67
SK
1376 gfc_error ("Array component of structure at %C must have an "
1377 "explicit shape");
6de9cd9a
DN
1378 return FAILURE;
1379 }
1380 }
1381
1382 return SUCCESS;
1383}
1384
1385
1386/* Match a 'NULL()', and possibly take care of some side effects. */
1387
1388match
636dff67 1389gfc_match_null (gfc_expr **result)
6de9cd9a
DN
1390{
1391 gfc_symbol *sym;
1392 gfc_expr *e;
1393 match m;
1394
1395 m = gfc_match (" null ( )");
1396 if (m != MATCH_YES)
1397 return m;
1398
1399 /* The NULL symbol now has to be/become an intrinsic function. */
1400 if (gfc_get_symbol ("null", NULL, &sym))
1401 {
1402 gfc_error ("NULL() initialization at %C is ambiguous");
1403 return MATCH_ERROR;
1404 }
1405
1406 gfc_intrinsic_symbol (sym);
1407
1408 if (sym->attr.proc != PROC_INTRINSIC
231b2fcc
TS
1409 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1410 sym->name, NULL) == FAILURE
1411 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
6de9cd9a
DN
1412 return MATCH_ERROR;
1413
1414 e = gfc_get_expr ();
63645982 1415 e->where = gfc_current_locus;
6de9cd9a
DN
1416 e->expr_type = EXPR_NULL;
1417 e->ts.type = BT_UNKNOWN;
1418
1419 *result = e;
1420
1421 return MATCH_YES;
1422}
1423
1424
6de9cd9a
DN
1425/* Match a variable name with an optional initializer. When this
1426 subroutine is called, a variable is expected to be parsed next.
1427 Depending on what is happening at the moment, updates either the
1428 symbol table or the current interface. */
1429
1430static match
949d5b72 1431variable_decl (int elem)
6de9cd9a
DN
1432{
1433 char name[GFC_MAX_SYMBOL_LEN + 1];
1434 gfc_expr *initializer, *char_len;
1435 gfc_array_spec *as;
83d890b9 1436 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
6de9cd9a
DN
1437 gfc_charlen *cl;
1438 locus var_locus;
1439 match m;
1440 try t;
83d890b9 1441 gfc_symbol *sym;
25d8f0a2 1442 locus old_locus;
6de9cd9a
DN
1443
1444 initializer = NULL;
1445 as = NULL;
83d890b9 1446 cp_as = NULL;
25d8f0a2 1447 old_locus = gfc_current_locus;
6de9cd9a
DN
1448
1449 /* When we get here, we've just matched a list of attributes and
1450 maybe a type and a double colon. The next thing we expect to see
1451 is the name of the symbol. */
1452 m = gfc_match_name (name);
1453 if (m != MATCH_YES)
1454 goto cleanup;
1455
63645982 1456 var_locus = gfc_current_locus;
6de9cd9a
DN
1457
1458 /* Now we could see the optional array spec. or character length. */
1459 m = gfc_match_array_spec (&as);
83d890b9
AL
1460 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1461 cp_as = gfc_copy_array_spec (as);
1462 else if (m == MATCH_ERROR)
6de9cd9a 1463 goto cleanup;
25d8f0a2 1464
6de9cd9a
DN
1465 if (m == MATCH_NO)
1466 as = gfc_copy_array_spec (current_as);
1467
1468 char_len = NULL;
1469 cl = NULL;
1470
1471 if (current_ts.type == BT_CHARACTER)
1472 {
1473 switch (match_char_length (&char_len))
1474 {
1475 case MATCH_YES:
1476 cl = gfc_get_charlen ();
1477 cl->next = gfc_current_ns->cl_list;
1478 gfc_current_ns->cl_list = cl;
1479
1480 cl->length = char_len;
1481 break;
1482
949d5b72 1483 /* Non-constant lengths need to be copied after the first
9b21a380 1484 element. Also copy assumed lengths. */
6de9cd9a 1485 case MATCH_NO:
9b21a380
JJ
1486 if (elem > 1
1487 && (current_ts.cl->length == NULL
1488 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
949d5b72
PT
1489 {
1490 cl = gfc_get_charlen ();
1491 cl->next = gfc_current_ns->cl_list;
1492 gfc_current_ns->cl_list = cl;
1493 cl->length = gfc_copy_expr (current_ts.cl->length);
1494 }
1495 else
1496 cl = current_ts.cl;
1497
6de9cd9a
DN
1498 break;
1499
1500 case MATCH_ERROR:
1501 goto cleanup;
1502 }
1503 }
1504
83d890b9 1505 /* If this symbol has already shown up in a Cray Pointer declaration,
66e4ab31 1506 then we want to set the type & bail out. */
83d890b9
AL
1507 if (gfc_option.flag_cray_pointer)
1508 {
1509 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1510 if (sym != NULL && sym->attr.cray_pointee)
1511 {
1512 sym->ts.type = current_ts.type;
1513 sym->ts.kind = current_ts.kind;
1514 sym->ts.cl = cl;
1515 sym->ts.derived = current_ts.derived;
a8b3b0b6
CR
1516 sym->ts.is_c_interop = current_ts.is_c_interop;
1517 sym->ts.is_iso_c = current_ts.is_iso_c;
83d890b9
AL
1518 m = MATCH_YES;
1519
1520 /* Check to see if we have an array specification. */
1521 if (cp_as != NULL)
1522 {
1523 if (sym->as != NULL)
1524 {
e25a0da3 1525 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
1526 gfc_free_array_spec (cp_as);
1527 m = MATCH_ERROR;
1528 goto cleanup;
1529 }
1530 else
1531 {
1532 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1533 gfc_internal_error ("Couldn't set pointee array spec.");
d51347f9 1534
83d890b9 1535 /* Fix the array spec. */
d51347f9 1536 m = gfc_mod_pointee_as (sym->as);
83d890b9
AL
1537 if (m == MATCH_ERROR)
1538 goto cleanup;
1539 }
d51347f9 1540 }
83d890b9
AL
1541 goto cleanup;
1542 }
1543 else
1544 {
1545 gfc_free_array_spec (cp_as);
1546 }
1547 }
d51347f9
TB
1548
1549
6de9cd9a
DN
1550 /* OK, we've successfully matched the declaration. Now put the
1551 symbol in the current namespace, because it might be used in the
69de3b83 1552 optional initialization expression for this symbol, e.g. this is
6de9cd9a
DN
1553 perfectly legal:
1554
1555 integer, parameter :: i = huge(i)
1556
1557 This is only true for parameters or variables of a basic type.
1558 For components of derived types, it is not true, so we don't
1559 create a symbol for those yet. If we fail to create the symbol,
1560 bail out. */
1561 if (gfc_current_state () != COMP_DERIVED
1562 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1563 {
72af9f0b
PT
1564 m = MATCH_ERROR;
1565 goto cleanup;
1566 }
1567
6133c68a
TS
1568 /* An interface body specifies all of the procedure's
1569 characteristics and these shall be consistent with those
1570 specified in the procedure definition, except that the interface
1571 may specify a procedure that is not pure if the procedure is
1572 defined to be pure(12.3.2). */
72af9f0b 1573 if (current_ts.type == BT_DERIVED
636dff67
SK
1574 && gfc_current_ns->proc_name
1575 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
5a8af0b4
PT
1576 && current_ts.derived->ns != gfc_current_ns)
1577 {
1578 gfc_symtree *st;
1579 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1580 if (!(current_ts.derived->attr.imported
1581 && st != NULL
1582 && st->n.sym == current_ts.derived)
1583 && !gfc_current_ns->has_import_set)
1584 {
1585 gfc_error ("the type of '%s' at %C has not been declared within the "
1586 "interface", name);
1587 m = MATCH_ERROR;
1588 goto cleanup;
1589 }
6de9cd9a
DN
1590 }
1591
1592 /* In functions that have a RESULT variable defined, the function
1593 name always refers to function calls. Therefore, the name is
1594 not allowed to appear in specification statements. */
1595 if (gfc_current_state () == COMP_FUNCTION
1596 && gfc_current_block () != NULL
1597 && gfc_current_block ()->result != NULL
1598 && gfc_current_block ()->result != gfc_current_block ()
1599 && strcmp (gfc_current_block ()->name, name) == 0)
1600 {
1601 gfc_error ("Function name '%s' not allowed at %C", name);
1602 m = MATCH_ERROR;
1603 goto cleanup;
1604 }
1605
294fbfc8
TS
1606 /* We allow old-style initializations of the form
1607 integer i /2/, j(4) /3*3, 1/
1608 (if no colon has been seen). These are different from data
1609 statements in that initializers are only allowed to apply to the
1610 variable immediately preceding, i.e.
1611 integer i, j /1, 2/
1612 is not allowed. Therefore we have to do some work manually, that
75d17889 1613 could otherwise be left to the matchers for DATA statements. */
294fbfc8
TS
1614
1615 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1616 {
1617 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1618 "initialization at %C") == FAILURE)
1619 return MATCH_ERROR;
d51347f9 1620
294fbfc8
TS
1621 return match_old_style_init (name);
1622 }
1623
6de9cd9a
DN
1624 /* The double colon must be present in order to have initializers.
1625 Otherwise the statement is ambiguous with an assignment statement. */
1626 if (colon_seen)
1627 {
1628 if (gfc_match (" =>") == MATCH_YES)
1629 {
6de9cd9a
DN
1630 if (!current_attr.pointer)
1631 {
1632 gfc_error ("Initialization at %C isn't for a pointer variable");
1633 m = MATCH_ERROR;
1634 goto cleanup;
1635 }
1636
1637 m = gfc_match_null (&initializer);
1638 if (m == MATCH_NO)
1639 {
def66134 1640 gfc_error ("Pointer initialization requires a NULL() at %C");
6de9cd9a
DN
1641 m = MATCH_ERROR;
1642 }
1643
1644 if (gfc_pure (NULL))
1645 {
636dff67
SK
1646 gfc_error ("Initialization of pointer at %C is not allowed in "
1647 "a PURE procedure");
6de9cd9a
DN
1648 m = MATCH_ERROR;
1649 }
1650
1651 if (m != MATCH_YES)
1652 goto cleanup;
1653
6de9cd9a
DN
1654 }
1655 else if (gfc_match_char ('=') == MATCH_YES)
1656 {
1657 if (current_attr.pointer)
1658 {
636dff67
SK
1659 gfc_error ("Pointer initialization at %C requires '=>', "
1660 "not '='");
6de9cd9a
DN
1661 m = MATCH_ERROR;
1662 goto cleanup;
1663 }
1664
1665 m = gfc_match_init_expr (&initializer);
1666 if (m == MATCH_NO)
1667 {
1668 gfc_error ("Expected an initialization expression at %C");
1669 m = MATCH_ERROR;
1670 }
1671
1672 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1673 {
636dff67
SK
1674 gfc_error ("Initialization of variable at %C is not allowed in "
1675 "a PURE procedure");
6de9cd9a
DN
1676 m = MATCH_ERROR;
1677 }
1678
1679 if (m != MATCH_YES)
1680 goto cleanup;
1681 }
cb44ab82
VL
1682 }
1683
5046aff5
PT
1684 if (initializer != NULL && current_attr.allocatable
1685 && gfc_current_state () == COMP_DERIVED)
1686 {
636dff67
SK
1687 gfc_error ("Initialization of allocatable component at %C is not "
1688 "allowed");
5046aff5
PT
1689 m = MATCH_ERROR;
1690 goto cleanup;
1691 }
1692
54b4ba60 1693 /* Add the initializer. Note that it is fine if initializer is
6de9cd9a
DN
1694 NULL here, because we sometimes also need to check if a
1695 declaration *must* have an initialization expression. */
1696 if (gfc_current_state () != COMP_DERIVED)
1697 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1698 else
54b4ba60 1699 {
5046aff5 1700 if (current_ts.type == BT_DERIVED
636dff67 1701 && !current_attr.pointer && !initializer)
54b4ba60
PB
1702 initializer = gfc_default_initializer (&current_ts);
1703 t = build_struct (name, cl, &initializer, &as);
1704 }
6de9cd9a
DN
1705
1706 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1707
1708cleanup:
1709 /* Free stuff up and return. */
1710 gfc_free_expr (initializer);
1711 gfc_free_array_spec (as);
1712
1713 return m;
1714}
1715
1716
b2b81a3f
BM
1717/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1718 This assumes that the byte size is equal to the kind number for
1719 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
6de9cd9a
DN
1720
1721match
636dff67 1722gfc_match_old_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
1723{
1724 match m;
5cf54585 1725 int original_kind;
6de9cd9a
DN
1726
1727 if (gfc_match_char ('*') != MATCH_YES)
1728 return MATCH_NO;
1729
5cf54585 1730 m = gfc_match_small_literal_int (&ts->kind, NULL);
6de9cd9a
DN
1731 if (m != MATCH_YES)
1732 return MATCH_ERROR;
1733
e45b3c75
ES
1734 original_kind = ts->kind;
1735
6de9cd9a 1736 /* Massage the kind numbers for complex types. */
e45b3c75
ES
1737 if (ts->type == BT_COMPLEX)
1738 {
1739 if (ts->kind % 2)
636dff67
SK
1740 {
1741 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1742 gfc_basic_typename (ts->type), original_kind);
1743 return MATCH_ERROR;
1744 }
e45b3c75
ES
1745 ts->kind /= 2;
1746 }
6de9cd9a 1747
e7a2d5fb 1748 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a 1749 {
e45b3c75 1750 gfc_error ("Old-style type declaration %s*%d not supported at %C",
636dff67 1751 gfc_basic_typename (ts->type), original_kind);
6de9cd9a
DN
1752 return MATCH_ERROR;
1753 }
1754
df8652dc
SK
1755 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1756 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1757 return MATCH_ERROR;
1758
6de9cd9a
DN
1759 return MATCH_YES;
1760}
1761
1762
1763/* Match a kind specification. Since kinds are generally optional, we
1764 usually return MATCH_NO if something goes wrong. If a "kind="
1765 string is found, then we know we have an error. */
1766
1767match
e2d29968 1768gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
6de9cd9a 1769{
e2d29968 1770 locus where, loc;
6de9cd9a
DN
1771 gfc_expr *e;
1772 match m, n;
1773 const char *msg;
1774
1775 m = MATCH_NO;
e2d29968 1776 n = MATCH_YES;
6de9cd9a
DN
1777 e = NULL;
1778
e2d29968
PT
1779 where = loc = gfc_current_locus;
1780
1781 if (kind_expr_only)
1782 goto kind_expr;
6de9cd9a
DN
1783
1784 if (gfc_match_char ('(') == MATCH_NO)
1785 return MATCH_NO;
1786
1787 /* Also gobbles optional text. */
1788 if (gfc_match (" kind = ") == MATCH_YES)
1789 m = MATCH_ERROR;
1790
e2d29968
PT
1791 loc = gfc_current_locus;
1792
1793kind_expr:
6de9cd9a 1794 n = gfc_match_init_expr (&e);
e2d29968 1795
6de9cd9a 1796 if (n != MATCH_YES)
e2d29968
PT
1797 {
1798 if (gfc_current_state () == COMP_INTERFACE
1799 || gfc_current_state () == COMP_NONE
1800 || gfc_current_state () == COMP_CONTAINS)
1801 {
1802 /* Signal using kind = -1 that the expression might include
1803 use associated or imported parameters and try again after
1804 the specification expressions..... */
1805 if (gfc_match_char (')') != MATCH_YES)
1806 {
1807 gfc_error ("Missing right parenthesis at %C");
1808 m = MATCH_ERROR;
1809 goto no_match;
1810 }
1811
1812 gfc_free_expr (e);
1813 ts->kind = -1;
1814 gfc_function_kind_locus = loc;
1815 gfc_undo_symbols ();
1816 return MATCH_YES;
1817 }
1818 else
1819 {
1820 /* ....or else, the match is real. */
1821 if (n == MATCH_NO)
1822 gfc_error ("Expected initialization expression at %C");
1823 if (n != MATCH_YES)
1824 return MATCH_ERROR;
1825 }
1826 }
6de9cd9a
DN
1827
1828 if (e->rank != 0)
1829 {
1830 gfc_error ("Expected scalar initialization expression at %C");
1831 m = MATCH_ERROR;
1832 goto no_match;
1833 }
1834
1835 msg = gfc_extract_int (e, &ts->kind);
1836 if (msg != NULL)
1837 {
1838 gfc_error (msg);
1839 m = MATCH_ERROR;
1840 goto no_match;
1841 }
1842
a8b3b0b6
CR
1843 /* Before throwing away the expression, let's see if we had a
1844 C interoperable kind (and store the fact). */
1845 if (e->ts.is_c_interop == 1)
1846 {
1847 /* Mark this as c interoperable if being declared with one
1848 of the named constants from iso_c_binding. */
1849 ts->is_c_interop = e->ts.is_iso_c;
1850 ts->f90_type = e->ts.f90_type;
1851 }
1852
6de9cd9a
DN
1853 gfc_free_expr (e);
1854 e = NULL;
1855
a8b3b0b6
CR
1856 /* Ignore errors to this point, if we've gotten here. This means
1857 we ignore the m=MATCH_ERROR from above. */
e7a2d5fb 1858 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a
DN
1859 {
1860 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1861 gfc_basic_typename (ts->type));
6de9cd9a 1862 m = MATCH_ERROR;
6de9cd9a 1863 }
a8b3b0b6 1864 else if (gfc_match_char (')') != MATCH_YES)
6de9cd9a 1865 {
8998be20 1866 gfc_error ("Missing right parenthesis at %C");
e2d29968 1867 m = MATCH_ERROR;
6de9cd9a 1868 }
a8b3b0b6
CR
1869 else
1870 /* All tests passed. */
1871 m = MATCH_YES;
6de9cd9a 1872
a8b3b0b6
CR
1873 if(m == MATCH_ERROR)
1874 gfc_current_locus = where;
1875
1876 /* Return what we know from the test(s). */
1877 return m;
6de9cd9a
DN
1878
1879no_match:
1880 gfc_free_expr (e);
63645982 1881 gfc_current_locus = where;
6de9cd9a
DN
1882 return m;
1883}
1884
1885
1886/* Match the various kind/length specifications in a CHARACTER
1887 declaration. We don't return MATCH_NO. */
1888
1889static match
636dff67 1890match_char_spec (gfc_typespec *ts)
6de9cd9a 1891{
5cd09fac 1892 int kind, seen_length;
6de9cd9a
DN
1893 gfc_charlen *cl;
1894 gfc_expr *len;
1895 match m;
a8b3b0b6 1896 gfc_expr *kind_expr = NULL;
9d64df18 1897 kind = gfc_default_character_kind;
6de9cd9a
DN
1898 len = NULL;
1899 seen_length = 0;
1900
1901 /* Try the old-style specification first. */
1902 old_char_selector = 0;
1903
1904 m = match_char_length (&len);
1905 if (m != MATCH_NO)
1906 {
1907 if (m == MATCH_YES)
1908 old_char_selector = 1;
1909 seen_length = 1;
1910 goto done;
1911 }
1912
1913 m = gfc_match_char ('(');
1914 if (m != MATCH_YES)
1915 {
a8b3b0b6 1916 m = MATCH_YES; /* Character without length is a single char. */
6de9cd9a
DN
1917 goto done;
1918 }
1919
a8b3b0b6 1920 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
6de9cd9a
DN
1921 if (gfc_match (" kind =") == MATCH_YES)
1922 {
a8b3b0b6
CR
1923 m = gfc_match_small_int_expr(&kind, &kind_expr);
1924
6de9cd9a
DN
1925 if (m == MATCH_ERROR)
1926 goto done;
1927 if (m == MATCH_NO)
1928 goto syntax;
1929
1930 if (gfc_match (" , len =") == MATCH_NO)
1931 goto rparen;
1932
1933 m = char_len_param_value (&len);
1934 if (m == MATCH_NO)
1935 goto syntax;
1936 if (m == MATCH_ERROR)
1937 goto done;
1938 seen_length = 1;
1939
1940 goto rparen;
1941 }
1942
66e4ab31 1943 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
6de9cd9a
DN
1944 if (gfc_match (" len =") == MATCH_YES)
1945 {
1946 m = char_len_param_value (&len);
1947 if (m == MATCH_NO)
1948 goto syntax;
1949 if (m == MATCH_ERROR)
1950 goto done;
1951 seen_length = 1;
1952
1953 if (gfc_match_char (')') == MATCH_YES)
1954 goto done;
1955
1956 if (gfc_match (" , kind =") != MATCH_YES)
1957 goto syntax;
1958
a8b3b0b6 1959 gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a 1960
e7a2d5fb 1961 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1962 {
1963 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1964 return MATCH_YES;
1965 }
1966
1967 goto rparen;
1968 }
1969
66e4ab31 1970 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
6de9cd9a
DN
1971 m = char_len_param_value (&len);
1972 if (m == MATCH_NO)
1973 goto syntax;
1974 if (m == MATCH_ERROR)
1975 goto done;
1976 seen_length = 1;
1977
1978 m = gfc_match_char (')');
1979 if (m == MATCH_YES)
1980 goto done;
1981
1982 if (gfc_match_char (',') != MATCH_YES)
1983 goto syntax;
1984
a8b3b0b6 1985 gfc_match (" kind ="); /* Gobble optional text. */
6de9cd9a 1986
a8b3b0b6 1987 m = gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a
DN
1988 if (m == MATCH_ERROR)
1989 goto done;
1990 if (m == MATCH_NO)
1991 goto syntax;
1992
1993rparen:
1994 /* Require a right-paren at this point. */
1995 m = gfc_match_char (')');
1996 if (m == MATCH_YES)
1997 goto done;
1998
1999syntax:
2000 gfc_error ("Syntax error in CHARACTER declaration at %C");
2001 m = MATCH_ERROR;
16f8ffc8
JD
2002 gfc_free_expr (len);
2003 return m;
6de9cd9a
DN
2004
2005done:
16f8ffc8 2006 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
2007 {
2008 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
2009 m = MATCH_ERROR;
2010 }
2011
16f8ffc8
JD
2012 if (seen_length == 1 && len != NULL
2013 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
2014 {
2015 gfc_error ("Expression at %C must be of INTEGER type");
2016 m = MATCH_ERROR;
2017 }
2018
6de9cd9a
DN
2019 if (m != MATCH_YES)
2020 {
2021 gfc_free_expr (len);
a8b3b0b6 2022 gfc_free_expr (kind_expr);
6de9cd9a
DN
2023 return m;
2024 }
2025
2026 /* Do some final massaging of the length values. */
2027 cl = gfc_get_charlen ();
2028 cl->next = gfc_current_ns->cl_list;
2029 gfc_current_ns->cl_list = cl;
2030
2031 if (seen_length == 0)
2032 cl->length = gfc_int_expr (1);
2033 else
5cd09fac 2034 cl->length = len;
6de9cd9a
DN
2035
2036 ts->cl = cl;
2037 ts->kind = kind;
2038
a8b3b0b6
CR
2039 /* We have to know if it was a c interoperable kind so we can
2040 do accurate type checking of bind(c) procs, etc. */
2041 if (kind_expr != NULL)
2042 {
2043 /* Mark this as c interoperable if being declared with one
2044 of the named constants from iso_c_binding. */
2045 ts->is_c_interop = kind_expr->ts.is_iso_c;
2046 gfc_free_expr (kind_expr);
2047 }
2048 else if (len != NULL)
2049 {
2050 /* Here, we might have parsed something such as:
2051 character(c_char)
2052 In this case, the parsing code above grabs the c_char when
2053 looking for the length (line 1690, roughly). it's the last
2054 testcase for parsing the kind params of a character variable.
2055 However, it's not actually the length. this seems like it
2056 could be an error.
2057 To see if the user used a C interop kind, test the expr
2058 of the so called length, and see if it's C interoperable. */
2059 ts->is_c_interop = len->ts.is_iso_c;
2060 }
2061
6de9cd9a
DN
2062 return MATCH_YES;
2063}
2064
2065
2066/* Matches a type specification. If successful, sets the ts structure
2067 to the matched specification. This is necessary for FUNCTION and
2068 IMPLICIT statements.
2069
d51347f9 2070 If implicit_flag is nonzero, then we don't check for the optional
e5ddaa24 2071 kind specification. Not doing so is needed for matching an IMPLICIT
6de9cd9a
DN
2072 statement correctly. */
2073
e2d29968
PT
2074match
2075gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
6de9cd9a
DN
2076{
2077 char name[GFC_MAX_SYMBOL_LEN + 1];
2078 gfc_symbol *sym;
2079 match m;
0ff0dfbf 2080 int c;
e2d29968 2081 locus loc = gfc_current_locus;
6de9cd9a
DN
2082
2083 gfc_clear_ts (ts);
2084
a8b3b0b6
CR
2085 /* Clear the current binding label, in case one is given. */
2086 curr_binding_label[0] = '\0';
2087
5f700e6d
AL
2088 if (gfc_match (" byte") == MATCH_YES)
2089 {
d51347f9 2090 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
5f700e6d
AL
2091 == FAILURE)
2092 return MATCH_ERROR;
2093
2094 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2095 {
2096 gfc_error ("BYTE type used at %C "
2097 "is not available on the target machine");
2098 return MATCH_ERROR;
2099 }
d51347f9 2100
5f700e6d
AL
2101 ts->type = BT_INTEGER;
2102 ts->kind = 1;
2103 return MATCH_YES;
2104 }
2105
6de9cd9a
DN
2106 if (gfc_match (" integer") == MATCH_YES)
2107 {
2108 ts->type = BT_INTEGER;
9d64df18 2109 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
2110 goto get_kind;
2111 }
2112
2113 if (gfc_match (" character") == MATCH_YES)
2114 {
2115 ts->type = BT_CHARACTER;
e5ddaa24
TS
2116 if (implicit_flag == 0)
2117 return match_char_spec (ts);
2118 else
2119 return MATCH_YES;
6de9cd9a
DN
2120 }
2121
2122 if (gfc_match (" real") == MATCH_YES)
2123 {
2124 ts->type = BT_REAL;
9d64df18 2125 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
2126 goto get_kind;
2127 }
2128
2129 if (gfc_match (" double precision") == MATCH_YES)
2130 {
2131 ts->type = BT_REAL;
9d64df18 2132 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2133 return MATCH_YES;
2134 }
2135
2136 if (gfc_match (" complex") == MATCH_YES)
2137 {
2138 ts->type = BT_COMPLEX;
9d64df18 2139 ts->kind = gfc_default_complex_kind;
6de9cd9a
DN
2140 goto get_kind;
2141 }
2142
2143 if (gfc_match (" double complex") == MATCH_YES)
2144 {
df8652dc
SK
2145 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2146 "conform to the Fortran 95 standard") == FAILURE)
2147 return MATCH_ERROR;
2148
6de9cd9a 2149 ts->type = BT_COMPLEX;
9d64df18 2150 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2151 return MATCH_YES;
2152 }
2153
2154 if (gfc_match (" logical") == MATCH_YES)
2155 {
2156 ts->type = BT_LOGICAL;
9d64df18 2157 ts->kind = gfc_default_logical_kind;
6de9cd9a
DN
2158 goto get_kind;
2159 }
2160
2161 m = gfc_match (" type ( %n )", name);
2162 if (m != MATCH_YES)
2163 return m;
2164
e2d29968
PT
2165 if (gfc_current_state () == COMP_INTERFACE
2166 || gfc_current_state () == COMP_NONE)
2167 {
2168 gfc_function_type_locus = loc;
2169 ts->type = BT_UNKNOWN;
2170 ts->kind = -1;
2171 return MATCH_YES;
2172 }
2173
2174 /* Search for the name but allow the components to be defined later. If
2175 type = -1, this typespec has been seen in a function declaration but
2176 the type could not legally be accessed at that point. */
2177 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
6de9cd9a
DN
2178 {
2179 gfc_error ("Type name '%s' at %C is ambiguous", name);
2180 return MATCH_ERROR;
2181 }
e2d29968
PT
2182 else if (ts->kind == -1)
2183 {
2184 if (gfc_find_symbol (name, NULL, 0, &sym))
2185 {
2186 gfc_error ("Type name '%s' at %C is ambiguous", name);
2187 return MATCH_ERROR;
2188 }
2189
2190 if (sym == NULL)
2191 return MATCH_NO;
2192 }
6de9cd9a
DN
2193
2194 if (sym->attr.flavor != FL_DERIVED
231b2fcc 2195 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2196 return MATCH_ERROR;
2197
2198 ts->type = BT_DERIVED;
2199 ts->kind = 0;
2200 ts->derived = sym;
2201
2202 return MATCH_YES;
2203
2204get_kind:
2205 /* For all types except double, derived and character, look for an
2206 optional kind specifier. MATCH_NO is actually OK at this point. */
e5ddaa24 2207 if (implicit_flag == 1)
6de9cd9a
DN
2208 return MATCH_YES;
2209
0ff0dfbf
TS
2210 if (gfc_current_form == FORM_FREE)
2211 {
2212 c = gfc_peek_char();
2213 if (!gfc_is_whitespace(c) && c != '*' && c != '('
636dff67 2214 && c != ':' && c != ',')
0ff0dfbf
TS
2215 return MATCH_NO;
2216 }
2217
e2d29968 2218 m = gfc_match_kind_spec (ts, false);
6de9cd9a
DN
2219 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2220 m = gfc_match_old_kind_spec (ts);
2221
2222 if (m == MATCH_NO)
2223 m = MATCH_YES; /* No kind specifier found. */
2224
2225 return m;
2226}
2227
2228
e5ddaa24
TS
2229/* Match an IMPLICIT NONE statement. Actually, this statement is
2230 already matched in parse.c, or we would not end up here in the
2231 first place. So the only thing we need to check, is if there is
2232 trailing garbage. If not, the match is successful. */
2233
2234match
2235gfc_match_implicit_none (void)
2236{
e5ddaa24
TS
2237 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2238}
2239
2240
2241/* Match the letter range(s) of an IMPLICIT statement. */
2242
2243static match
1107b970 2244match_implicit_range (void)
e5ddaa24
TS
2245{
2246 int c, c1, c2, inner;
2247 locus cur_loc;
2248
2249 cur_loc = gfc_current_locus;
2250
2251 gfc_gobble_whitespace ();
2252 c = gfc_next_char ();
2253 if (c != '(')
2254 {
2255 gfc_error ("Missing character range in IMPLICIT at %C");
2256 goto bad;
2257 }
2258
2259 inner = 1;
2260 while (inner)
2261 {
2262 gfc_gobble_whitespace ();
2263 c1 = gfc_next_char ();
2264 if (!ISALPHA (c1))
2265 goto bad;
2266
2267 gfc_gobble_whitespace ();
2268 c = gfc_next_char ();
2269
2270 switch (c)
2271 {
2272 case ')':
66e4ab31 2273 inner = 0; /* Fall through. */
e5ddaa24
TS
2274
2275 case ',':
2276 c2 = c1;
2277 break;
2278
2279 case '-':
2280 gfc_gobble_whitespace ();
2281 c2 = gfc_next_char ();
2282 if (!ISALPHA (c2))
2283 goto bad;
2284
2285 gfc_gobble_whitespace ();
2286 c = gfc_next_char ();
2287
2288 if ((c != ',') && (c != ')'))
2289 goto bad;
2290 if (c == ')')
2291 inner = 0;
2292
2293 break;
2294
2295 default:
2296 goto bad;
2297 }
2298
2299 if (c1 > c2)
2300 {
2301 gfc_error ("Letters must be in alphabetic order in "
2302 "IMPLICIT statement at %C");
2303 goto bad;
2304 }
2305
2306 /* See if we can add the newly matched range to the pending
636dff67
SK
2307 implicits from this IMPLICIT statement. We do not check for
2308 conflicts with whatever earlier IMPLICIT statements may have
2309 set. This is done when we've successfully finished matching
2310 the current one. */
1107b970 2311 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
e5ddaa24
TS
2312 goto bad;
2313 }
2314
2315 return MATCH_YES;
2316
2317bad:
2318 gfc_syntax_error (ST_IMPLICIT);
2319
2320 gfc_current_locus = cur_loc;
2321 return MATCH_ERROR;
2322}
2323
2324
2325/* Match an IMPLICIT statement, storing the types for
2326 gfc_set_implicit() if the statement is accepted by the parser.
2327 There is a strange looking, but legal syntactic construction
2328 possible. It looks like:
2329
2330 IMPLICIT INTEGER (a-b) (c-d)
2331
2332 This is legal if "a-b" is a constant expression that happens to
2333 equal one of the legal kinds for integers. The real problem
2334 happens with an implicit specification that looks like:
2335
2336 IMPLICIT INTEGER (a-b)
2337
2338 In this case, a typespec matcher that is "greedy" (as most of the
2339 matchers are) gobbles the character range as a kindspec, leaving
2340 nothing left. We therefore have to go a bit more slowly in the
2341 matching process by inhibiting the kindspec checking during
2342 typespec matching and checking for a kind later. */
2343
2344match
2345gfc_match_implicit (void)
2346{
2347 gfc_typespec ts;
2348 locus cur_loc;
2349 int c;
2350 match m;
2351
2352 /* We don't allow empty implicit statements. */
2353 if (gfc_match_eos () == MATCH_YES)
2354 {
2355 gfc_error ("Empty IMPLICIT statement at %C");
2356 return MATCH_ERROR;
2357 }
2358
e5ddaa24
TS
2359 do
2360 {
1107b970
PB
2361 /* First cleanup. */
2362 gfc_clear_new_implicit ();
2363
e5ddaa24 2364 /* A basic type is mandatory here. */
e2d29968 2365 m = gfc_match_type_spec (&ts, 1);
e5ddaa24
TS
2366 if (m == MATCH_ERROR)
2367 goto error;
2368 if (m == MATCH_NO)
2369 goto syntax;
2370
2371 cur_loc = gfc_current_locus;
1107b970 2372 m = match_implicit_range ();
e5ddaa24
TS
2373
2374 if (m == MATCH_YES)
2375 {
1107b970 2376 /* We may have <TYPE> (<RANGE>). */
e5ddaa24
TS
2377 gfc_gobble_whitespace ();
2378 c = gfc_next_char ();
2379 if ((c == '\n') || (c == ','))
1107b970
PB
2380 {
2381 /* Check for CHARACTER with no length parameter. */
2382 if (ts.type == BT_CHARACTER && !ts.cl)
2383 {
9d64df18 2384 ts.kind = gfc_default_character_kind;
1107b970
PB
2385 ts.cl = gfc_get_charlen ();
2386 ts.cl->next = gfc_current_ns->cl_list;
2387 gfc_current_ns->cl_list = ts.cl;
2388 ts.cl->length = gfc_int_expr (1);
2389 }
2390
2391 /* Record the Successful match. */
2392 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2393 return MATCH_ERROR;
2394 continue;
2395 }
e5ddaa24
TS
2396
2397 gfc_current_locus = cur_loc;
2398 }
2399
1107b970
PB
2400 /* Discard the (incorrectly) matched range. */
2401 gfc_clear_new_implicit ();
2402
2403 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2404 if (ts.type == BT_CHARACTER)
2405 m = match_char_spec (&ts);
2406 else
e5ddaa24 2407 {
e2d29968 2408 m = gfc_match_kind_spec (&ts, false);
e5ddaa24 2409 if (m == MATCH_NO)
1107b970
PB
2410 {
2411 m = gfc_match_old_kind_spec (&ts);
2412 if (m == MATCH_ERROR)
2413 goto error;
2414 if (m == MATCH_NO)
2415 goto syntax;
2416 }
e5ddaa24 2417 }
1107b970
PB
2418 if (m == MATCH_ERROR)
2419 goto error;
e5ddaa24 2420
1107b970 2421 m = match_implicit_range ();
e5ddaa24
TS
2422 if (m == MATCH_ERROR)
2423 goto error;
2424 if (m == MATCH_NO)
2425 goto syntax;
2426
2427 gfc_gobble_whitespace ();
2428 c = gfc_next_char ();
2429 if ((c != '\n') && (c != ','))
2430 goto syntax;
2431
1107b970
PB
2432 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2433 return MATCH_ERROR;
e5ddaa24
TS
2434 }
2435 while (c == ',');
2436
1107b970 2437 return MATCH_YES;
e5ddaa24
TS
2438
2439syntax:
2440 gfc_syntax_error (ST_IMPLICIT);
2441
2442error:
2443 return MATCH_ERROR;
2444}
2445
66e4ab31 2446
8998be20
TB
2447match
2448gfc_match_import (void)
2449{
2450 char name[GFC_MAX_SYMBOL_LEN + 1];
2451 match m;
2452 gfc_symbol *sym;
2453 gfc_symtree *st;
2454
66e4ab31
SK
2455 if (gfc_current_ns->proc_name == NULL
2456 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
8998be20
TB
2457 {
2458 gfc_error ("IMPORT statement at %C only permitted in "
2459 "an INTERFACE body");
2460 return MATCH_ERROR;
2461 }
2462
636dff67 2463 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
8998be20
TB
2464 == FAILURE)
2465 return MATCH_ERROR;
2466
2467 if (gfc_match_eos () == MATCH_YES)
2468 {
2469 /* All host variables should be imported. */
2470 gfc_current_ns->has_import_set = 1;
2471 return MATCH_YES;
2472 }
2473
2474 if (gfc_match (" ::") == MATCH_YES)
2475 {
2476 if (gfc_match_eos () == MATCH_YES)
636dff67
SK
2477 {
2478 gfc_error ("Expecting list of named entities at %C");
2479 return MATCH_ERROR;
2480 }
8998be20
TB
2481 }
2482
2483 for(;;)
2484 {
2485 m = gfc_match (" %n", name);
2486 switch (m)
2487 {
2488 case MATCH_YES:
36d3fb4c 2489 if (gfc_current_ns->parent != NULL
66e4ab31 2490 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
36d3fb4c
PT
2491 {
2492 gfc_error ("Type name '%s' at %C is ambiguous", name);
2493 return MATCH_ERROR;
2494 }
2495 else if (gfc_current_ns->proc_name->ns->parent != NULL
66e4ab31
SK
2496 && gfc_find_symbol (name,
2497 gfc_current_ns->proc_name->ns->parent,
2498 1, &sym))
636dff67
SK
2499 {
2500 gfc_error ("Type name '%s' at %C is ambiguous", name);
2501 return MATCH_ERROR;
2502 }
2503
2504 if (sym == NULL)
2505 {
2506 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2507 "at %C - does not exist.", name);
2508 return MATCH_ERROR;
2509 }
2510
d51347f9 2511 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
636dff67
SK
2512 {
2513 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2514 "at %C.", name);
2515 goto next_item;
2516 }
2517
2518 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2519 st->n.sym = sym;
2520 sym->refs++;
5a8af0b4 2521 sym->attr.imported = 1;
8998be20
TB
2522
2523 goto next_item;
2524
2525 case MATCH_NO:
2526 break;
2527
2528 case MATCH_ERROR:
2529 return MATCH_ERROR;
2530 }
2531
2532 next_item:
2533 if (gfc_match_eos () == MATCH_YES)
2534 break;
2535 if (gfc_match_char (',') != MATCH_YES)
2536 goto syntax;
2537 }
2538
2539 return MATCH_YES;
2540
2541syntax:
2542 gfc_error ("Syntax error in IMPORT statement at %C");
2543 return MATCH_ERROR;
2544}
e5ddaa24 2545
66e4ab31 2546
f2449db4
RS
2547/* A minimal implementation of gfc_match without whitespace, escape
2548 characters or variable arguments. Returns true if the next
2549 characters match the TARGET template exactly. */
2550
2551static bool
2552match_string_p (const char *target)
2553{
2554 const char *p;
2555
2556 for (p = target; *p; p++)
2557 if (gfc_next_char () != *p)
2558 return false;
2559 return true;
2560}
2561
6de9cd9a
DN
2562/* Matches an attribute specification including array specs. If
2563 successful, leaves the variables current_attr and current_as
2564 holding the specification. Also sets the colon_seen variable for
2565 later use by matchers associated with initializations.
2566
2567 This subroutine is a little tricky in the sense that we don't know
2568 if we really have an attr-spec until we hit the double colon.
2569 Until that time, we can only return MATCH_NO. This forces us to
2570 check for duplicate specification at this level. */
2571
2572static match
2573match_attr_spec (void)
2574{
6de9cd9a
DN
2575 /* Modifiers that can exist in a type statement. */
2576 typedef enum
2577 { GFC_DECL_BEGIN = 0,
2578 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2579 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
ee7e677f
TB
2580 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2581 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
f2449db4 2582 DECL_IS_BIND_C, DECL_NONE,
6de9cd9a
DN
2583 GFC_DECL_END /* Sentinel */
2584 }
2585 decl_types;
2586
2587/* GFC_DECL_END is the sentinel, index starts at 0. */
2588#define NUM_DECL GFC_DECL_END
2589
6de9cd9a
DN
2590 locus start, seen_at[NUM_DECL];
2591 int seen[NUM_DECL];
2592 decl_types d;
2593 const char *attr;
2594 match m;
2595 try t;
2596
2597 gfc_clear_attr (&current_attr);
63645982 2598 start = gfc_current_locus;
6de9cd9a
DN
2599
2600 current_as = NULL;
2601 colon_seen = 0;
2602
2603 /* See if we get all of the keywords up to the final double colon. */
2604 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2605 seen[d] = 0;
2606
2607 for (;;)
2608 {
f2449db4 2609 int ch;
a8b3b0b6 2610
f2449db4
RS
2611 d = DECL_NONE;
2612 gfc_gobble_whitespace ();
2613
2614 ch = gfc_next_char ();
2615 if (ch == ':')
2616 {
2617 /* This is the successful exit condition for the loop. */
2618 if (gfc_next_char () == ':')
2619 break;
2620 }
2621 else if (ch == ',')
a8b3b0b6 2622 {
a8b3b0b6 2623 gfc_gobble_whitespace ();
f2449db4 2624 switch (gfc_peek_char ())
a8b3b0b6 2625 {
f2449db4
RS
2626 case 'a':
2627 if (match_string_p ("allocatable"))
2628 d = DECL_ALLOCATABLE;
2629 break;
2630
2631 case 'b':
a8b3b0b6 2632 /* Try and match the bind(c). */
129d15a3
JW
2633 m = gfc_match_bind_c (NULL);
2634 if (m == MATCH_YES)
a8b3b0b6 2635 d = DECL_IS_BIND_C;
129d15a3
JW
2636 else if (m == MATCH_ERROR)
2637 goto cleanup;
f2449db4
RS
2638 break;
2639
2640 case 'd':
2641 if (match_string_p ("dimension"))
2642 d = DECL_DIMENSION;
2643 break;
2644
2645 case 'e':
2646 if (match_string_p ("external"))
2647 d = DECL_EXTERNAL;
2648 break;
2649
2650 case 'i':
2651 if (match_string_p ("int"))
2652 {
2653 ch = gfc_next_char ();
2654 if (ch == 'e')
2655 {
2656 if (match_string_p ("nt"))
2657 {
2658 /* Matched "intent". */
2659 /* TODO: Call match_intent_spec from here. */
2660 if (gfc_match (" ( in out )") == MATCH_YES)
2661 d = DECL_INOUT;
2662 else if (gfc_match (" ( in )") == MATCH_YES)
2663 d = DECL_IN;
2664 else if (gfc_match (" ( out )") == MATCH_YES)
2665 d = DECL_OUT;
2666 }
2667 }
2668 else if (ch == 'r')
2669 {
2670 if (match_string_p ("insic"))
2671 {
2672 /* Matched "intrinsic". */
2673 d = DECL_INTRINSIC;
2674 }
2675 }
2676 }
2677 break;
2678
2679 case 'o':
2680 if (match_string_p ("optional"))
2681 d = DECL_OPTIONAL;
2682 break;
2683
2684 case 'p':
2685 gfc_next_char ();
2686 switch (gfc_next_char ())
2687 {
2688 case 'a':
2689 if (match_string_p ("rameter"))
2690 {
2691 /* Matched "parameter". */
2692 d = DECL_PARAMETER;
2693 }
2694 break;
2695
2696 case 'o':
2697 if (match_string_p ("inter"))
2698 {
2699 /* Matched "pointer". */
2700 d = DECL_POINTER;
2701 }
2702 break;
2703
2704 case 'r':
2705 ch = gfc_next_char ();
2706 if (ch == 'i')
2707 {
2708 if (match_string_p ("vate"))
2709 {
2710 /* Matched "private". */
2711 d = DECL_PRIVATE;
2712 }
2713 }
2714 else if (ch == 'o')
2715 {
2716 if (match_string_p ("tected"))
2717 {
2718 /* Matched "protected". */
2719 d = DECL_PROTECTED;
2720 }
2721 }
2722 break;
2723
2724 case 'u':
2725 if (match_string_p ("blic"))
2726 {
2727 /* Matched "public". */
2728 d = DECL_PUBLIC;
2729 }
2730 break;
2731 }
2732 break;
2733
2734 case 's':
2735 if (match_string_p ("save"))
2736 d = DECL_SAVE;
2737 break;
2738
2739 case 't':
2740 if (match_string_p ("target"))
2741 d = DECL_TARGET;
2742 break;
2743
2744 case 'v':
2745 gfc_next_char ();
2746 ch = gfc_next_char ();
2747 if (ch == 'a')
2748 {
2749 if (match_string_p ("lue"))
2750 {
2751 /* Matched "value". */
2752 d = DECL_VALUE;
2753 }
2754 }
2755 else if (ch == 'o')
2756 {
2757 if (match_string_p ("latile"))
2758 {
2759 /* Matched "volatile". */
2760 d = DECL_VOLATILE;
2761 }
2762 }
2763 break;
a8b3b0b6
CR
2764 }
2765 }
d468bcdb 2766
f2449db4
RS
2767 /* No double colon and no recognizable decl_type, so assume that
2768 we've been looking at something else the whole time. */
2769 if (d == DECL_NONE)
2770 {
2771 m = MATCH_NO;
2772 goto cleanup;
2773 }
d51347f9 2774
6de9cd9a 2775 seen[d]++;
63645982 2776 seen_at[d] = gfc_current_locus;
6de9cd9a
DN
2777
2778 if (d == DECL_DIMENSION)
2779 {
2780 m = gfc_match_array_spec (&current_as);
2781
2782 if (m == MATCH_NO)
2783 {
2784 gfc_error ("Missing dimension specification at %C");
2785 m = MATCH_ERROR;
2786 }
2787
2788 if (m == MATCH_ERROR)
2789 goto cleanup;
2790 }
2791 }
2792
6de9cd9a
DN
2793 /* Since we've seen a double colon, we have to be looking at an
2794 attr-spec. This means that we can now issue errors. */
2795 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2796 if (seen[d] > 1)
2797 {
2798 switch (d)
2799 {
2800 case DECL_ALLOCATABLE:
2801 attr = "ALLOCATABLE";
2802 break;
2803 case DECL_DIMENSION:
2804 attr = "DIMENSION";
2805 break;
2806 case DECL_EXTERNAL:
2807 attr = "EXTERNAL";
2808 break;
2809 case DECL_IN:
2810 attr = "INTENT (IN)";
2811 break;
2812 case DECL_OUT:
2813 attr = "INTENT (OUT)";
2814 break;
2815 case DECL_INOUT:
2816 attr = "INTENT (IN OUT)";
2817 break;
2818 case DECL_INTRINSIC:
2819 attr = "INTRINSIC";
2820 break;
2821 case DECL_OPTIONAL:
2822 attr = "OPTIONAL";
2823 break;
2824 case DECL_PARAMETER:
2825 attr = "PARAMETER";
2826 break;
2827 case DECL_POINTER:
2828 attr = "POINTER";
2829 break;
ee7e677f
TB
2830 case DECL_PROTECTED:
2831 attr = "PROTECTED";
2832 break;
6de9cd9a
DN
2833 case DECL_PRIVATE:
2834 attr = "PRIVATE";
2835 break;
2836 case DECL_PUBLIC:
2837 attr = "PUBLIC";
2838 break;
2839 case DECL_SAVE:
2840 attr = "SAVE";
2841 break;
2842 case DECL_TARGET:
2843 attr = "TARGET";
2844 break;
a8b3b0b6
CR
2845 case DECL_IS_BIND_C:
2846 attr = "IS_BIND_C";
2847 break;
2848 case DECL_VALUE:
2849 attr = "VALUE";
2850 break;
775e6c3a
TB
2851 case DECL_VOLATILE:
2852 attr = "VOLATILE";
2853 break;
6de9cd9a 2854 default:
66e4ab31 2855 attr = NULL; /* This shouldn't happen. */
6de9cd9a
DN
2856 }
2857
2858 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2859 m = MATCH_ERROR;
2860 goto cleanup;
2861 }
2862
2863 /* Now that we've dealt with duplicate attributes, add the attributes
2864 to the current attribute. */
2865 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2866 {
2867 if (seen[d] == 0)
2868 continue;
2869
2870 if (gfc_current_state () == COMP_DERIVED
2871 && d != DECL_DIMENSION && d != DECL_POINTER
f2449db4
RS
2872 && d != DECL_PRIVATE && d != DECL_PUBLIC
2873 && d != DECL_NONE)
6de9cd9a 2874 {
5046aff5
PT
2875 if (d == DECL_ALLOCATABLE)
2876 {
636dff67
SK
2877 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2878 "attribute at %C in a TYPE definition")
d51347f9 2879 == FAILURE)
5046aff5
PT
2880 {
2881 m = MATCH_ERROR;
2882 goto cleanup;
2883 }
636dff67
SK
2884 }
2885 else
5046aff5
PT
2886 {
2887 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
d51347f9 2888 &seen_at[d]);
5046aff5
PT
2889 m = MATCH_ERROR;
2890 goto cleanup;
2891 }
6de9cd9a
DN
2892 }
2893
4213f93b 2894 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
636dff67 2895 && gfc_current_state () != COMP_MODULE)
4213f93b
PT
2896 {
2897 if (d == DECL_PRIVATE)
2898 attr = "PRIVATE";
2899 else
2900 attr = "PUBLIC";
d51347f9
TB
2901 if (gfc_current_state () == COMP_DERIVED
2902 && gfc_state_stack->previous
2903 && gfc_state_stack->previous->state == COMP_MODULE)
2904 {
2905 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2906 "at %L in a TYPE definition", attr,
2907 &seen_at[d])
2908 == FAILURE)
2909 {
2910 m = MATCH_ERROR;
2911 goto cleanup;
2912 }
2913 }
2914 else
2915 {
2916 gfc_error ("%s attribute at %L is not allowed outside of the "
2917 "specification part of a module", attr, &seen_at[d]);
2918 m = MATCH_ERROR;
2919 goto cleanup;
2920 }
4213f93b
PT
2921 }
2922
6de9cd9a
DN
2923 switch (d)
2924 {
2925 case DECL_ALLOCATABLE:
2926 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2927 break;
2928
2929 case DECL_DIMENSION:
231b2fcc 2930 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2931 break;
2932
2933 case DECL_EXTERNAL:
2934 t = gfc_add_external (&current_attr, &seen_at[d]);
2935 break;
2936
2937 case DECL_IN:
2938 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2939 break;
2940
2941 case DECL_OUT:
2942 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2943 break;
2944
2945 case DECL_INOUT:
2946 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2947 break;
2948
2949 case DECL_INTRINSIC:
2950 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2951 break;
2952
2953 case DECL_OPTIONAL:
2954 t = gfc_add_optional (&current_attr, &seen_at[d]);
2955 break;
2956
2957 case DECL_PARAMETER:
231b2fcc 2958 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
6de9cd9a
DN
2959 break;
2960
2961 case DECL_POINTER:
2962 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2963 break;
2964
ee7e677f
TB
2965 case DECL_PROTECTED:
2966 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2967 {
2968 gfc_error ("PROTECTED at %C only allowed in specification "
2969 "part of a module");
2970 t = FAILURE;
2971 break;
2972 }
2973
636dff67
SK
2974 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2975 "attribute at %C")
ee7e677f
TB
2976 == FAILURE)
2977 t = FAILURE;
2978 else
2979 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2980 break;
2981
6de9cd9a 2982 case DECL_PRIVATE:
231b2fcc
TS
2983 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2984 &seen_at[d]);
6de9cd9a
DN
2985 break;
2986
2987 case DECL_PUBLIC:
231b2fcc
TS
2988 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2989 &seen_at[d]);
6de9cd9a
DN
2990 break;
2991
2992 case DECL_SAVE:
231b2fcc 2993 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2994 break;
2995
2996 case DECL_TARGET:
2997 t = gfc_add_target (&current_attr, &seen_at[d]);
2998 break;
2999
a8b3b0b6
CR
3000 case DECL_IS_BIND_C:
3001 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3002 break;
3003
06469efd 3004 case DECL_VALUE:
636dff67
SK
3005 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3006 "at %C")
06469efd
PT
3007 == FAILURE)
3008 t = FAILURE;
3009 else
3010 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3011 break;
3012
775e6c3a
TB
3013 case DECL_VOLATILE:
3014 if (gfc_notify_std (GFC_STD_F2003,
636dff67 3015 "Fortran 2003: VOLATILE attribute at %C")
775e6c3a
TB
3016 == FAILURE)
3017 t = FAILURE;
3018 else
3019 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3020 break;
3021
6de9cd9a
DN
3022 default:
3023 gfc_internal_error ("match_attr_spec(): Bad attribute");
3024 }
3025
3026 if (t == FAILURE)
3027 {
3028 m = MATCH_ERROR;
3029 goto cleanup;
3030 }
3031 }
3032
3033 colon_seen = 1;
3034 return MATCH_YES;
3035
3036cleanup:
63645982 3037 gfc_current_locus = start;
6de9cd9a
DN
3038 gfc_free_array_spec (current_as);
3039 current_as = NULL;
3040 return m;
3041}
3042
3043
a8b3b0b6
CR
3044/* Set the binding label, dest_label, either with the binding label
3045 stored in the given gfc_typespec, ts, or if none was provided, it
3046 will be the symbol name in all lower case, as required by the draft
3047 (J3/04-007, section 15.4.1). If a binding label was given and
3048 there is more than one argument (num_idents), it is an error. */
3049
3050try
3051set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3052{
ad4a2f64 3053 if (num_idents > 1 && has_name_equals)
a8b3b0b6 3054 {
ad4a2f64
TB
3055 gfc_error ("Multiple identifiers provided with "
3056 "single NAME= specifier at %C");
3057 return FAILURE;
3058 }
a8b3b0b6 3059
ad4a2f64
TB
3060 if (curr_binding_label[0] != '\0')
3061 {
a8b3b0b6
CR
3062 /* Binding label given; store in temp holder til have sym. */
3063 strncpy (dest_label, curr_binding_label,
3064 strlen (curr_binding_label) + 1);
3065 }
3066 else
3067 {
3068 /* No binding label given, and the NAME= specifier did not exist,
3069 which means there was no NAME="". */
3070 if (sym_name != NULL && has_name_equals == 0)
3071 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
3072 }
3073
3074 return SUCCESS;
3075}
3076
3077
3078/* Set the status of the given common block as being BIND(C) or not,
3079 depending on the given parameter, is_bind_c. */
3080
3081void
3082set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3083{
3084 com_block->is_bind_c = is_bind_c;
3085 return;
3086}
3087
3088
3089/* Verify that the given gfc_typespec is for a C interoperable type. */
3090
3091try
3092verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3093{
3094 try t;
3095
3096 /* Make sure the kind used is appropriate for the type.
3097 The f90_type is unknown if an integer constant was
3098 used (e.g., real(4), bind(c) :: myFloat). */
3099 if (ts->f90_type != BT_UNKNOWN)
3100 {
3101 t = gfc_validate_c_kind (ts);
3102 if (t != SUCCESS)
3103 {
3104 /* Print an error, but continue parsing line. */
3105 gfc_error_now ("C kind parameter is for type %s but "
3106 "symbol '%s' at %L is of type %s",
3107 gfc_basic_typename (ts->f90_type),
3108 name, where,
3109 gfc_basic_typename (ts->type));
3110 }
3111 }
3112
3113 /* Make sure the kind is C interoperable. This does not care about the
3114 possible error above. */
3115 if (ts->type == BT_DERIVED && ts->derived != NULL)
3116 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3117 else if (ts->is_c_interop != 1)
3118 return FAILURE;
3119
3120 return SUCCESS;
3121}
3122
3123
3124/* Verify that the variables of a given common block, which has been
3125 defined with the attribute specifier bind(c), to be of a C
3126 interoperable type. Errors will be reported here, if
3127 encountered. */
3128
3129try
3130verify_com_block_vars_c_interop (gfc_common_head *com_block)
3131{
3132 gfc_symbol *curr_sym = NULL;
3133 try retval = SUCCESS;
3134
3135 curr_sym = com_block->head;
3136
3137 /* Make sure we have at least one symbol. */
3138 if (curr_sym == NULL)
3139 return retval;
3140
3141 /* Here we know we have a symbol, so we'll execute this loop
3142 at least once. */
3143 do
3144 {
3145 /* The second to last param, 1, says this is in a common block. */
3146 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3147 curr_sym = curr_sym->common_next;
3148 } while (curr_sym != NULL);
3149
3150 return retval;
3151}
3152
3153
3154/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3155 an appropriate error message is reported. */
3156
3157try
3158verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3159 int is_in_common, gfc_common_head *com_block)
3160{
3161 try retval = SUCCESS;
d8fa96e0
CR
3162
3163 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3164 {
3165 tmp_sym = tmp_sym->result;
3166 /* Make sure it wasn't an implicitly typed result. */
3167 if (tmp_sym->attr.implicit_type)
3168 {
3169 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3170 "%L may not be C interoperable", tmp_sym->name,
3171 &tmp_sym->declared_at);
3172 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3173 /* Mark it as C interoperable to prevent duplicate warnings. */
3174 tmp_sym->ts.is_c_interop = 1;
3175 tmp_sym->attr.is_c_interop = 1;
3176 }
3177 }
a8b3b0b6
CR
3178
3179 /* Here, we know we have the bind(c) attribute, so if we have
3180 enough type info, then verify that it's a C interop kind.
3181 The info could be in the symbol already, or possibly still in
3182 the given ts (current_ts), so look in both. */
3183 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3184 {
3185 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3186 &(tmp_sym->declared_at)) != SUCCESS)
3187 {
3188 /* See if we're dealing with a sym in a common block or not. */
3189 if (is_in_common == 1)
3190 {
3191 gfc_warning ("Variable '%s' in common block '%s' at %L "
3192 "may not be a C interoperable "
3193 "kind though common block '%s' is BIND(C)",
3194 tmp_sym->name, com_block->name,
3195 &(tmp_sym->declared_at), com_block->name);
3196 }
3197 else
3198 {
3199 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3200 gfc_error ("Type declaration '%s' at %L is not C "
3201 "interoperable but it is BIND(C)",
3202 tmp_sym->name, &(tmp_sym->declared_at));
3203 else
3204 gfc_warning ("Variable '%s' at %L "
3205 "may not be a C interoperable "
3206 "kind but it is bind(c)",
3207 tmp_sym->name, &(tmp_sym->declared_at));
3208 }
3209 }
3210
3211 /* Variables declared w/in a common block can't be bind(c)
3212 since there's no way for C to see these variables, so there's
3213 semantically no reason for the attribute. */
3214 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3215 {
3216 gfc_error ("Variable '%s' in common block '%s' at "
3217 "%L cannot be declared with BIND(C) "
3218 "since it is not a global",
3219 tmp_sym->name, com_block->name,
3220 &(tmp_sym->declared_at));
3221 retval = FAILURE;
3222 }
3223
3224 /* Scalar variables that are bind(c) can not have the pointer
3225 or allocatable attributes. */
3226 if (tmp_sym->attr.is_bind_c == 1)
3227 {
3228 if (tmp_sym->attr.pointer == 1)
3229 {
3230 gfc_error ("Variable '%s' at %L cannot have both the "
3231 "POINTER and BIND(C) attributes",
3232 tmp_sym->name, &(tmp_sym->declared_at));
3233 retval = FAILURE;
3234 }
3235
3236 if (tmp_sym->attr.allocatable == 1)
3237 {
3238 gfc_error ("Variable '%s' at %L cannot have both the "
3239 "ALLOCATABLE and BIND(C) attributes",
3240 tmp_sym->name, &(tmp_sym->declared_at));
3241 retval = FAILURE;
3242 }
3243
3244 /* If it is a BIND(C) function, make sure the return value is a
3245 scalar value. The previous tests in this function made sure
3246 the type is interoperable. */
3247 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3248 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3249 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3250
3251 /* BIND(C) functions can not return a character string. */
3252 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3253 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3254 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3255 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3256 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3257 "be a character string", tmp_sym->name,
3258 &(tmp_sym->declared_at));
3259 }
3260 }
3261
3262 /* See if the symbol has been marked as private. If it has, make sure
3263 there is no binding label and warn the user if there is one. */
3264 if (tmp_sym->attr.access == ACCESS_PRIVATE
3265 && tmp_sym->binding_label[0] != '\0')
3266 /* Use gfc_warning_now because we won't say that the symbol fails
3267 just because of this. */
3268 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3269 "given the binding label '%s'", tmp_sym->name,
3270 &(tmp_sym->declared_at), tmp_sym->binding_label);
3271
3272 return retval;
3273}
3274
3275
3276/* Set the appropriate fields for a symbol that's been declared as
3277 BIND(C) (the is_bind_c flag and the binding label), and verify that
3278 the type is C interoperable. Errors are reported by the functions
3279 used to set/test these fields. */
3280
3281try
3282set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3283{
3284 try retval = SUCCESS;
3285
3286 /* TODO: Do we need to make sure the vars aren't marked private? */
3287
3288 /* Set the is_bind_c bit in symbol_attribute. */
3289 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3290
3291 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3292 num_idents) != SUCCESS)
3293 return FAILURE;
3294
3295 return retval;
3296}
3297
3298
3299/* Set the fields marking the given common block as BIND(C), including
3300 a binding label, and report any errors encountered. */
3301
3302try
3303set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3304{
3305 try retval = SUCCESS;
3306
3307 /* destLabel, common name, typespec (which may have binding label). */
3308 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3309 != SUCCESS)
3310 return FAILURE;
3311
3312 /* Set the given common block (com_block) to being bind(c) (1). */
3313 set_com_block_bind_c (com_block, 1);
3314
3315 return retval;
3316}
3317
3318
3319/* Retrieve the list of one or more identifiers that the given bind(c)
3320 attribute applies to. */
3321
3322try
3323get_bind_c_idents (void)
3324{
3325 char name[GFC_MAX_SYMBOL_LEN + 1];
3326 int num_idents = 0;
3327 gfc_symbol *tmp_sym = NULL;
3328 match found_id;
3329 gfc_common_head *com_block = NULL;
3330
3331 if (gfc_match_name (name) == MATCH_YES)
3332 {
3333 found_id = MATCH_YES;
3334 gfc_get_ha_symbol (name, &tmp_sym);
3335 }
3336 else if (match_common_name (name) == MATCH_YES)
3337 {
3338 found_id = MATCH_YES;
3339 com_block = gfc_get_common (name, 0);
3340 }
3341 else
3342 {
3343 gfc_error ("Need either entity or common block name for "
3344 "attribute specification statement at %C");
3345 return FAILURE;
3346 }
3347
3348 /* Save the current identifier and look for more. */
3349 do
3350 {
3351 /* Increment the number of identifiers found for this spec stmt. */
3352 num_idents++;
3353
3354 /* Make sure we have a sym or com block, and verify that it can
3355 be bind(c). Set the appropriate field(s) and look for more
3356 identifiers. */
3357 if (tmp_sym != NULL || com_block != NULL)
3358 {
3359 if (tmp_sym != NULL)
3360 {
3361 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3362 != SUCCESS)
3363 return FAILURE;
3364 }
3365 else
3366 {
3367 if (set_verify_bind_c_com_block(com_block, num_idents)
3368 != SUCCESS)
3369 return FAILURE;
3370 }
3371
3372 /* Look to see if we have another identifier. */
3373 tmp_sym = NULL;
3374 if (gfc_match_eos () == MATCH_YES)
3375 found_id = MATCH_NO;
3376 else if (gfc_match_char (',') != MATCH_YES)
3377 found_id = MATCH_NO;
3378 else if (gfc_match_name (name) == MATCH_YES)
3379 {
3380 found_id = MATCH_YES;
3381 gfc_get_ha_symbol (name, &tmp_sym);
3382 }
3383 else if (match_common_name (name) == MATCH_YES)
3384 {
3385 found_id = MATCH_YES;
3386 com_block = gfc_get_common (name, 0);
3387 }
3388 else
3389 {
3390 gfc_error ("Missing entity or common block name for "
3391 "attribute specification statement at %C");
3392 return FAILURE;
3393 }
3394 }
3395 else
3396 {
3397 gfc_internal_error ("Missing symbol");
3398 }
3399 } while (found_id == MATCH_YES);
3400
3401 /* if we get here we were successful */
3402 return SUCCESS;
3403}
3404
3405
3406/* Try and match a BIND(C) attribute specification statement. */
3407
3408match
3409gfc_match_bind_c_stmt (void)
3410{
3411 match found_match = MATCH_NO;
3412 gfc_typespec *ts;
3413
3414 ts = &current_ts;
3415
3416 /* This may not be necessary. */
3417 gfc_clear_ts (ts);
3418 /* Clear the temporary binding label holder. */
3419 curr_binding_label[0] = '\0';
3420
3421 /* Look for the bind(c). */
3422 found_match = gfc_match_bind_c (NULL);
3423
3424 if (found_match == MATCH_YES)
3425 {
3426 /* Look for the :: now, but it is not required. */
3427 gfc_match (" :: ");
3428
3429 /* Get the identifier(s) that needs to be updated. This may need to
3430 change to hand the flag(s) for the attr specified so all identifiers
3431 found can have all appropriate parts updated (assuming that the same
3432 spec stmt can have multiple attrs, such as both bind(c) and
3433 allocatable...). */
3434 if (get_bind_c_idents () != SUCCESS)
3435 /* Error message should have printed already. */
3436 return MATCH_ERROR;
3437 }
3438
3439 return found_match;
3440}
3441
3442
6de9cd9a
DN
3443/* Match a data declaration statement. */
3444
3445match
3446gfc_match_data_decl (void)
3447{
3448 gfc_symbol *sym;
3449 match m;
949d5b72 3450 int elem;
6de9cd9a 3451
a8b3b0b6
CR
3452 num_idents_on_line = 0;
3453
e2d29968 3454 m = gfc_match_type_spec (&current_ts, 0);
6de9cd9a
DN
3455 if (m != MATCH_YES)
3456 return m;
3457
3458 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3459 {
3460 sym = gfc_use_derived (current_ts.derived);
3461
3462 if (sym == NULL)
3463 {
3464 m = MATCH_ERROR;
3465 goto cleanup;
3466 }
3467
3468 current_ts.derived = sym;
3469 }
3470
3471 m = match_attr_spec ();
3472 if (m == MATCH_ERROR)
3473 {
3474 m = MATCH_NO;
3475 goto cleanup;
3476 }
3477
9fa6b0af
FXC
3478 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3479 && !current_ts.derived->attr.zero_comp)
6de9cd9a
DN
3480 {
3481
3482 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3483 goto ok;
3484
976e21f6 3485 gfc_find_symbol (current_ts.derived->name,
636dff67 3486 current_ts.derived->ns->parent, 1, &sym);
6de9cd9a 3487
976e21f6 3488 /* Any symbol that we find had better be a type definition
636dff67 3489 which has its components defined. */
976e21f6 3490 if (sym != NULL && sym->attr.flavor == FL_DERIVED
9fa6b0af
FXC
3491 && (current_ts.derived->components != NULL
3492 || current_ts.derived->attr.zero_comp))
6de9cd9a
DN
3493 goto ok;
3494
976e21f6
PT
3495 /* Now we have an error, which we signal, and then fix up
3496 because the knock-on is plain and simple confusing. */
3497 gfc_error_now ("Derived type at %C has not been previously defined "
636dff67 3498 "and so cannot appear in a derived type definition");
976e21f6
PT
3499 current_attr.pointer = 1;
3500 goto ok;
6de9cd9a
DN
3501 }
3502
3503ok:
3504 /* If we have an old-style character declaration, and no new-style
3505 attribute specifications, then there a comma is optional between
3506 the type specification and the variable list. */
3507 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3508 gfc_match_char (',');
3509
949d5b72
PT
3510 /* Give the types/attributes to symbols that follow. Give the element
3511 a number so that repeat character length expressions can be copied. */
3512 elem = 1;
6de9cd9a
DN
3513 for (;;)
3514 {
a8b3b0b6 3515 num_idents_on_line++;
949d5b72 3516 m = variable_decl (elem++);
6de9cd9a
DN
3517 if (m == MATCH_ERROR)
3518 goto cleanup;
3519 if (m == MATCH_NO)
3520 break;
3521
3522 if (gfc_match_eos () == MATCH_YES)
3523 goto cleanup;
3524 if (gfc_match_char (',') != MATCH_YES)
3525 break;
3526 }
3527
8f81c3c6
PT
3528 if (gfc_error_flag_test () == 0)
3529 gfc_error ("Syntax error in data declaration at %C");
6de9cd9a
DN
3530 m = MATCH_ERROR;
3531
a9f6f1f2
JD
3532 gfc_free_data_all (gfc_current_ns);
3533
6de9cd9a
DN
3534cleanup:
3535 gfc_free_array_spec (current_as);
3536 current_as = NULL;
3537 return m;
3538}
3539
3540
3541/* Match a prefix associated with a function or subroutine
3542 declaration. If the typespec pointer is nonnull, then a typespec
3543 can be matched. Note that if nothing matches, MATCH_YES is
3544 returned (the null string was matched). */
3545
3546static match
636dff67 3547match_prefix (gfc_typespec *ts)
6de9cd9a
DN
3548{
3549 int seen_type;
3550
3551 gfc_clear_attr (&current_attr);
3552 seen_type = 0;
3553
3554loop:
3555 if (!seen_type && ts != NULL
e2d29968 3556 && gfc_match_type_spec (ts, 0) == MATCH_YES
6de9cd9a
DN
3557 && gfc_match_space () == MATCH_YES)
3558 {
3559
3560 seen_type = 1;
3561 goto loop;
3562 }
3563
3564 if (gfc_match ("elemental% ") == MATCH_YES)
3565 {
3566 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3567 return MATCH_ERROR;
3568
3569 goto loop;
3570 }
3571
3572 if (gfc_match ("pure% ") == MATCH_YES)
3573 {
3574 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3575 return MATCH_ERROR;
3576
3577 goto loop;
3578 }
3579
3580 if (gfc_match ("recursive% ") == MATCH_YES)
3581 {
3582 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3583 return MATCH_ERROR;
3584
3585 goto loop;
3586 }
3587
3588 /* At this point, the next item is not a prefix. */
3589 return MATCH_YES;
3590}
3591
3592
3593/* Copy attributes matched by match_prefix() to attributes on a symbol. */
3594
3595static try
636dff67 3596copy_prefix (symbol_attribute *dest, locus *where)
6de9cd9a 3597{
6de9cd9a
DN
3598 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3599 return FAILURE;
3600
3601 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3602 return FAILURE;
3603
3604 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3605 return FAILURE;
3606
3607 return SUCCESS;
3608}
3609
3610
3611/* Match a formal argument list. */
3612
3613match
636dff67 3614gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
6de9cd9a
DN
3615{
3616 gfc_formal_arglist *head, *tail, *p, *q;
3617 char name[GFC_MAX_SYMBOL_LEN + 1];
3618 gfc_symbol *sym;
3619 match m;
3620
3621 head = tail = NULL;
3622
3623 if (gfc_match_char ('(') != MATCH_YES)
3624 {
3625 if (null_flag)
3626 goto ok;
3627 return MATCH_NO;
3628 }
3629
3630 if (gfc_match_char (')') == MATCH_YES)
3631 goto ok;
3632
3633 for (;;)
3634 {
3635 if (gfc_match_char ('*') == MATCH_YES)
3636 sym = NULL;
3637 else
3638 {
3639 m = gfc_match_name (name);
3640 if (m != MATCH_YES)
3641 goto cleanup;
3642
3643 if (gfc_get_symbol (name, NULL, &sym))
3644 goto cleanup;
3645 }
3646
3647 p = gfc_get_formal_arglist ();
3648
3649 if (head == NULL)
3650 head = tail = p;
3651 else
3652 {
3653 tail->next = p;
3654 tail = p;
3655 }
3656
3657 tail->sym = sym;
3658
3659 /* We don't add the VARIABLE flavor because the name could be a
636dff67
SK
3660 dummy procedure. We don't apply these attributes to formal
3661 arguments of statement functions. */
6de9cd9a 3662 if (sym != NULL && !st_flag
231b2fcc 3663 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
6de9cd9a
DN
3664 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3665 {
3666 m = MATCH_ERROR;
3667 goto cleanup;
3668 }
3669
3670 /* The name of a program unit can be in a different namespace,
636dff67
SK
3671 so check for it explicitly. After the statement is accepted,
3672 the name is checked for especially in gfc_get_symbol(). */
6de9cd9a
DN
3673 if (gfc_new_block != NULL && sym != NULL
3674 && strcmp (sym->name, gfc_new_block->name) == 0)
3675 {
3676 gfc_error ("Name '%s' at %C is the name of the procedure",
3677 sym->name);
3678 m = MATCH_ERROR;
3679 goto cleanup;
3680 }
3681
3682 if (gfc_match_char (')') == MATCH_YES)
3683 goto ok;
3684
3685 m = gfc_match_char (',');
3686 if (m != MATCH_YES)
3687 {
3688 gfc_error ("Unexpected junk in formal argument list at %C");
3689 goto cleanup;
3690 }
3691 }
3692
3693ok:
3694 /* Check for duplicate symbols in the formal argument list. */
3695 if (head != NULL)
3696 {
3697 for (p = head; p->next; p = p->next)
3698 {
3699 if (p->sym == NULL)
3700 continue;
3701
3702 for (q = p->next; q; q = q->next)
3703 if (p->sym == q->sym)
3704 {
636dff67
SK
3705 gfc_error ("Duplicate symbol '%s' in formal argument list "
3706 "at %C", p->sym->name);
6de9cd9a
DN
3707
3708 m = MATCH_ERROR;
3709 goto cleanup;
3710 }
3711 }
3712 }
3713
66e4ab31
SK
3714 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3715 == FAILURE)
6de9cd9a
DN
3716 {
3717 m = MATCH_ERROR;
3718 goto cleanup;
3719 }
3720
3721 return MATCH_YES;
3722
3723cleanup:
3724 gfc_free_formal_arglist (head);
3725 return m;
3726}
3727
3728
3729/* Match a RESULT specification following a function declaration or
3730 ENTRY statement. Also matches the end-of-statement. */
3731
3732static match
66e4ab31 3733match_result (gfc_symbol *function, gfc_symbol **result)
6de9cd9a
DN
3734{
3735 char name[GFC_MAX_SYMBOL_LEN + 1];
3736 gfc_symbol *r;
3737 match m;
3738
3739 if (gfc_match (" result (") != MATCH_YES)
3740 return MATCH_NO;
3741
3742 m = gfc_match_name (name);
3743 if (m != MATCH_YES)
3744 return m;
3745
a8b3b0b6
CR
3746 /* Get the right paren, and that's it because there could be the
3747 bind(c) attribute after the result clause. */
3748 if (gfc_match_char(')') != MATCH_YES)
6de9cd9a 3749 {
a8b3b0b6 3750 /* TODO: should report the missing right paren here. */
6de9cd9a
DN
3751 return MATCH_ERROR;
3752 }
3753
3754 if (strcmp (function->name, name) == 0)
3755 {
636dff67 3756 gfc_error ("RESULT variable at %C must be different than function name");
6de9cd9a
DN
3757 return MATCH_ERROR;
3758 }
3759
3760 if (gfc_get_symbol (name, NULL, &r))
3761 return MATCH_ERROR;
3762
231b2fcc
TS
3763 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3764 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
6de9cd9a
DN
3765 return MATCH_ERROR;
3766
3767 *result = r;
3768
3769 return MATCH_YES;
3770}
3771
3772
a8b3b0b6
CR
3773/* Match a function suffix, which could be a combination of a result
3774 clause and BIND(C), either one, or neither. The draft does not
3775 require them to come in a specific order. */
3776
3777match
3778gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3779{
3780 match is_bind_c; /* Found bind(c). */
3781 match is_result; /* Found result clause. */
3782 match found_match; /* Status of whether we've found a good match. */
3783 int peek_char; /* Character we're going to peek at. */
3784
3785 /* Initialize to having found nothing. */
3786 found_match = MATCH_NO;
3787 is_bind_c = MATCH_NO;
3788 is_result = MATCH_NO;
3789
3790 /* Get the next char to narrow between result and bind(c). */
3791 gfc_gobble_whitespace ();
3792 peek_char = gfc_peek_char ();
3793
3794 switch (peek_char)
3795 {
3796 case 'r':
3797 /* Look for result clause. */
3798 is_result = match_result (sym, result);
3799 if (is_result == MATCH_YES)
3800 {
3801 /* Now see if there is a bind(c) after it. */
3802 is_bind_c = gfc_match_bind_c (sym);
3803 /* We've found the result clause and possibly bind(c). */
3804 found_match = MATCH_YES;
3805 }
3806 else
3807 /* This should only be MATCH_ERROR. */
3808 found_match = is_result;
3809 break;
3810 case 'b':
3811 /* Look for bind(c) first. */
3812 is_bind_c = gfc_match_bind_c (sym);
3813 if (is_bind_c == MATCH_YES)
3814 {
3815 /* Now see if a result clause followed it. */
3816 is_result = match_result (sym, result);
3817 found_match = MATCH_YES;
3818 }
3819 else
3820 {
3821 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3822 found_match = MATCH_ERROR;
3823 }
3824 break;
3825 default:
3826 gfc_error ("Unexpected junk after function declaration at %C");
3827 found_match = MATCH_ERROR;
3828 break;
3829 }
3830
a8b3b0b6
CR
3831 if (is_bind_c == MATCH_YES)
3832 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3833 == FAILURE)
3834 return MATCH_ERROR;
3835
3836 return found_match;
3837}
3838
3839
69773742
JW
3840/* Match a PROCEDURE declaration (R1211). */
3841
3842static match
3843match_procedure_decl (void)
3844{
3845 match m;
3846 locus old_loc, entry_loc;
3847 gfc_symbol *sym, *proc_if = NULL;
3848 int num;
3849
3850 old_loc = entry_loc = gfc_current_locus;
3851
3852 gfc_clear_ts (&current_ts);
3853
3854 if (gfc_match (" (") != MATCH_YES)
3855 {
3856 gfc_current_locus = entry_loc;
3857 return MATCH_NO;
3858 }
3859
3860 /* Get the type spec. for the procedure interface. */
3861 old_loc = gfc_current_locus;
e2d29968 3862 m = gfc_match_type_spec (&current_ts, 0);
69773742
JW
3863 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3864 goto got_ts;
3865
3866 if (m == MATCH_ERROR)
3867 return m;
3868
3869 gfc_current_locus = old_loc;
3870
3871 /* Get the name of the procedure or abstract interface
3872 to inherit the interface from. */
3873 m = gfc_match_symbol (&proc_if, 1);
3874
3875 if (m == MATCH_NO)
3876 goto syntax;
3877 else if (m == MATCH_ERROR)
3878 return m;
3879
3880 /* Various interface checks. */
3881 if (proc_if)
3882 {
3883 if (proc_if->generic)
3884 {
3885 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3886 return MATCH_ERROR;
3887 }
3888 if (proc_if->attr.proc == PROC_ST_FUNCTION)
3889 {
3890 gfc_error ("Interface '%s' at %C may not be a statement function",
3891 proc_if->name);
3892 return MATCH_ERROR;
3893 }
3894 /* Handle intrinsic procedures. */
3895 if (gfc_intrinsic_name (proc_if->name, 0)
3896 || gfc_intrinsic_name (proc_if->name, 1))
3897 proc_if->attr.intrinsic = 1;
3898 if (proc_if->attr.intrinsic
3899 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3900 {
3901 gfc_error ("Intrinsic procedure '%s' not allowed "
3902 "in PROCEDURE statement at %C", proc_if->name);
3903 return MATCH_ERROR;
3904 }
3905 /* TODO: Allow intrinsics with gfc_intrinsic_actual_ok
3906 (proc_if->name, 0) after PR33162 is fixed. */
3907 if (proc_if->attr.intrinsic)
3908 {
3909 gfc_error ("Fortran 2003: Support for intrinsic procedure '%s' "
3910 "in PROCEDURE statement at %C not yet implemented "
3911 "in gfortran", proc_if->name);
3912 return MATCH_ERROR;
3913 }
3914 }
3915
3916got_ts:
3917
3918 if (gfc_match (" )") != MATCH_YES)
3919 {
3920 gfc_current_locus = entry_loc;
3921 return MATCH_NO;
3922 }
3923
3924 /* Parse attributes. */
3925 m = match_attr_spec();
3926 if (m == MATCH_ERROR)
3927 return MATCH_ERROR;
3928
3929 /* Get procedure symbols. */
3930 for(num=1;;num++)
3931 {
3932
3933 m = gfc_match_symbol (&sym, 0);
3934 if (m == MATCH_NO)
3935 goto syntax;
3936 else if (m == MATCH_ERROR)
3937 return m;
3938
3939 /* Add current_attr to the symbol attributes. */
3940 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3941 return MATCH_ERROR;
3942
3943 if (sym->attr.is_bind_c)
3944 {
3945 /* Check for C1218. */
3946 if (!proc_if || !proc_if->attr.is_bind_c)
3947 {
3948 gfc_error ("BIND(C) attribute at %C requires "
3949 "an interface with BIND(C)");
3950 return MATCH_ERROR;
3951 }
3952 /* Check for C1217. */
3953 if (has_name_equals && sym->attr.pointer)
3954 {
3955 gfc_error ("BIND(C) procedure with NAME may not have "
3956 "POINTER attribute at %C");
3957 return MATCH_ERROR;
3958 }
3959 if (has_name_equals && sym->attr.dummy)
3960 {
3961 gfc_error ("Dummy procedure at %C may not have "
3962 "BIND(C) attribute with NAME");
3963 return MATCH_ERROR;
3964 }
3965 /* Set binding label for BIND(C). */
3966 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
3967 return MATCH_ERROR;
3968 }
3969
3970 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
3971 return MATCH_ERROR;
3972 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
3973 return MATCH_ERROR;
3974
3975 /* Set interface. */
3976 if (proc_if != NULL)
3977 sym->interface = proc_if;
3978 else if (current_ts.type != BT_UNKNOWN)
3979 {
3980 sym->interface = gfc_new_symbol ("", gfc_current_ns);
3981 sym->interface->ts = current_ts;
3982 sym->interface->attr.function = 1;
3983 sym->ts = sym->interface->ts;
3984 sym->attr.function = sym->interface->attr.function;
3985 }
3986
3987 if (gfc_match_eos () == MATCH_YES)
3988 return MATCH_YES;
3989 if (gfc_match_char (',') != MATCH_YES)
3990 goto syntax;
3991 }
3992
3993syntax:
3994 gfc_error ("Syntax error in PROCEDURE statement at %C");
3995 return MATCH_ERROR;
3996}
3997
3998
3999/* Match a PROCEDURE declaration inside an interface (R1206). */
4000
4001static match
4002match_procedure_in_interface (void)
4003{
4004 match m;
4005 gfc_symbol *sym;
4006 char name[GFC_MAX_SYMBOL_LEN + 1];
4007
4008 if (current_interface.type == INTERFACE_NAMELESS
4009 || current_interface.type == INTERFACE_ABSTRACT)
4010 {
4011 gfc_error ("PROCEDURE at %C must be in a generic interface");
4012 return MATCH_ERROR;
4013 }
4014
4015 for(;;)
4016 {
4017 m = gfc_match_name (name);
4018 if (m == MATCH_NO)
4019 goto syntax;
4020 else if (m == MATCH_ERROR)
4021 return m;
4022 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4023 return MATCH_ERROR;
4024
4025 if (gfc_add_interface (sym) == FAILURE)
4026 return MATCH_ERROR;
4027
4028 sym->attr.procedure = 1;
4029
4030 if (gfc_match_eos () == MATCH_YES)
4031 break;
4032 if (gfc_match_char (',') != MATCH_YES)
4033 goto syntax;
4034 }
4035
4036 return MATCH_YES;
4037
4038syntax:
4039 gfc_error ("Syntax error in PROCEDURE statement at %C");
4040 return MATCH_ERROR;
4041}
4042
4043
4044/* General matcher for PROCEDURE declarations. */
4045
4046match
4047gfc_match_procedure (void)
4048{
4049 match m;
4050
4051 switch (gfc_current_state ())
4052 {
4053 case COMP_NONE:
4054 case COMP_PROGRAM:
4055 case COMP_MODULE:
4056 case COMP_SUBROUTINE:
4057 case COMP_FUNCTION:
4058 m = match_procedure_decl ();
4059 break;
4060 case COMP_INTERFACE:
4061 m = match_procedure_in_interface ();
4062 break;
4063 case COMP_DERIVED:
4064 gfc_error ("Fortran 2003: Procedure components at %C are "
4065 "not yet implemented in gfortran");
4066 return MATCH_ERROR;
4067 default:
4068 return MATCH_NO;
4069 }
4070
4071 if (m != MATCH_YES)
4072 return m;
4073
4074 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4075 == FAILURE)
4076 return MATCH_ERROR;
4077
4078 return m;
4079}
4080
4081
6de9cd9a
DN
4082/* Match a function declaration. */
4083
4084match
4085gfc_match_function_decl (void)
4086{
4087 char name[GFC_MAX_SYMBOL_LEN + 1];
4088 gfc_symbol *sym, *result;
4089 locus old_loc;
4090 match m;
a8b3b0b6
CR
4091 match suffix_match;
4092 match found_match; /* Status returned by match func. */
6de9cd9a
DN
4093
4094 if (gfc_current_state () != COMP_NONE
4095 && gfc_current_state () != COMP_INTERFACE
4096 && gfc_current_state () != COMP_CONTAINS)
4097 return MATCH_NO;
4098
4099 gfc_clear_ts (&current_ts);
4100
63645982 4101 old_loc = gfc_current_locus;
6de9cd9a
DN
4102
4103 m = match_prefix (&current_ts);
4104 if (m != MATCH_YES)
4105 {
63645982 4106 gfc_current_locus = old_loc;
6de9cd9a
DN
4107 return m;
4108 }
4109
4110 if (gfc_match ("function% %n", name) != MATCH_YES)
4111 {
63645982 4112 gfc_current_locus = old_loc;
6de9cd9a
DN
4113 return MATCH_NO;
4114 }
1a492601 4115 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4116 return MATCH_ERROR;
4117 gfc_new_block = sym;
4118
4119 m = gfc_match_formal_arglist (sym, 0, 0);
4120 if (m == MATCH_NO)
2b9a33ae
TS
4121 {
4122 gfc_error ("Expected formal argument list in function "
636dff67 4123 "definition at %C");
2b9a33ae
TS
4124 m = MATCH_ERROR;
4125 goto cleanup;
4126 }
6de9cd9a
DN
4127 else if (m == MATCH_ERROR)
4128 goto cleanup;
4129
4130 result = NULL;
4131
a8b3b0b6
CR
4132 /* According to the draft, the bind(c) and result clause can
4133 come in either order after the formal_arg_list (i.e., either
4134 can be first, both can exist together or by themselves or neither
4135 one). Therefore, the match_result can't match the end of the
4136 string, and check for the bind(c) or result clause in either order. */
4137 found_match = gfc_match_eos ();
4138
4139 /* Make sure that it isn't already declared as BIND(C). If it is, it
4140 must have been marked BIND(C) with a BIND(C) attribute and that is
4141 not allowed for procedures. */
4142 if (sym->attr.is_bind_c == 1)
4143 {
4144 sym->attr.is_bind_c = 0;
4145 if (sym->old_symbol != NULL)
4146 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4147 "variables or common blocks",
4148 &(sym->old_symbol->declared_at));
4149 else
4150 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4151 "variables or common blocks", &gfc_current_locus);
6de9cd9a
DN
4152 }
4153
a8b3b0b6 4154 if (found_match != MATCH_YES)
6de9cd9a 4155 {
a8b3b0b6
CR
4156 /* If we haven't found the end-of-statement, look for a suffix. */
4157 suffix_match = gfc_match_suffix (sym, &result);
4158 if (suffix_match == MATCH_YES)
4159 /* Need to get the eos now. */
4160 found_match = gfc_match_eos ();
4161 else
4162 found_match = suffix_match;
6de9cd9a
DN
4163 }
4164
a8b3b0b6
CR
4165 if(found_match != MATCH_YES)
4166 m = MATCH_ERROR;
6de9cd9a
DN
4167 else
4168 {
a8b3b0b6
CR
4169 /* Make changes to the symbol. */
4170 m = MATCH_ERROR;
4171
4172 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4173 goto cleanup;
4174
4175 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4176 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4177 goto cleanup;
6de9cd9a 4178
a8b3b0b6
CR
4179 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4180 && !sym->attr.implicit_type)
4181 {
4182 gfc_error ("Function '%s' at %C already has a type of %s", name,
4183 gfc_basic_typename (sym->ts.type));
4184 goto cleanup;
4185 }
4186
4187 if (result == NULL)
4188 {
4189 sym->ts = current_ts;
4190 sym->result = sym;
4191 }
4192 else
4193 {
4194 result->ts = current_ts;
4195 sym->result = result;
4196 }
4197
4198 return MATCH_YES;
4199 }
6de9cd9a
DN
4200
4201cleanup:
63645982 4202 gfc_current_locus = old_loc;
6de9cd9a
DN
4203 return m;
4204}
4205
636dff67
SK
4206
4207/* This is mostly a copy of parse.c(add_global_procedure) but modified to
4208 pass the name of the entry, rather than the gfc_current_block name, and
4209 to return false upon finding an existing global entry. */
68ea355b
PT
4210
4211static bool
636dff67 4212add_global_entry (const char *name, int sub)
68ea355b
PT
4213{
4214 gfc_gsymbol *s;
4215
4216 s = gfc_get_gsymbol(name);
4217
4218 if (s->defined
636dff67
SK
4219 || (s->type != GSYM_UNKNOWN
4220 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
68ea355b
PT
4221 global_used(s, NULL);
4222 else
4223 {
4224 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4225 s->where = gfc_current_locus;
4226 s->defined = 1;
4227 return true;
4228 }
4229 return false;
4230}
6de9cd9a 4231
636dff67 4232
6de9cd9a
DN
4233/* Match an ENTRY statement. */
4234
4235match
4236gfc_match_entry (void)
4237{
3d79abbd
PB
4238 gfc_symbol *proc;
4239 gfc_symbol *result;
4240 gfc_symbol *entry;
6de9cd9a
DN
4241 char name[GFC_MAX_SYMBOL_LEN + 1];
4242 gfc_compile_state state;
4243 match m;
3d79abbd 4244 gfc_entry_list *el;
c96cfa49 4245 locus old_loc;
1a492601 4246 bool module_procedure;
6de9cd9a
DN
4247
4248 m = gfc_match_name (name);
4249 if (m != MATCH_YES)
4250 return m;
4251
3d79abbd 4252 state = gfc_current_state ();
4c93c95a 4253 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 4254 {
4c93c95a
FXC
4255 switch (state)
4256 {
4257 case COMP_PROGRAM:
4258 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4259 break;
4260 case COMP_MODULE:
4261 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4262 break;
4263 case COMP_BLOCK_DATA:
636dff67
SK
4264 gfc_error ("ENTRY statement at %C cannot appear within "
4265 "a BLOCK DATA");
4c93c95a
FXC
4266 break;
4267 case COMP_INTERFACE:
636dff67
SK
4268 gfc_error ("ENTRY statement at %C cannot appear within "
4269 "an INTERFACE");
4c93c95a
FXC
4270 break;
4271 case COMP_DERIVED:
636dff67
SK
4272 gfc_error ("ENTRY statement at %C cannot appear within "
4273 "a DERIVED TYPE block");
4c93c95a
FXC
4274 break;
4275 case COMP_IF:
636dff67
SK
4276 gfc_error ("ENTRY statement at %C cannot appear within "
4277 "an IF-THEN block");
4c93c95a
FXC
4278 break;
4279 case COMP_DO:
636dff67
SK
4280 gfc_error ("ENTRY statement at %C cannot appear within "
4281 "a DO block");
4c93c95a
FXC
4282 break;
4283 case COMP_SELECT:
636dff67
SK
4284 gfc_error ("ENTRY statement at %C cannot appear within "
4285 "a SELECT block");
4c93c95a
FXC
4286 break;
4287 case COMP_FORALL:
636dff67
SK
4288 gfc_error ("ENTRY statement at %C cannot appear within "
4289 "a FORALL block");
4c93c95a
FXC
4290 break;
4291 case COMP_WHERE:
636dff67
SK
4292 gfc_error ("ENTRY statement at %C cannot appear within "
4293 "a WHERE block");
4c93c95a
FXC
4294 break;
4295 case COMP_CONTAINS:
636dff67
SK
4296 gfc_error ("ENTRY statement at %C cannot appear within "
4297 "a contained subprogram");
4c93c95a
FXC
4298 break;
4299 default:
4300 gfc_internal_error ("gfc_match_entry(): Bad state");
4301 }
3d79abbd
PB
4302 return MATCH_ERROR;
4303 }
4304
1a492601 4305 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
4306 && gfc_current_ns->parent->proc_name
4307 && gfc_current_ns->parent->proc_name->attr.flavor
4308 == FL_MODULE;
1a492601 4309
3d79abbd
PB
4310 if (gfc_current_ns->parent != NULL
4311 && gfc_current_ns->parent->proc_name
1a492601 4312 && !module_procedure)
3d79abbd
PB
4313 {
4314 gfc_error("ENTRY statement at %C cannot appear in a "
4315 "contained procedure");
4316 return MATCH_ERROR;
4317 }
4318
1a492601
PT
4319 /* Module function entries need special care in get_proc_name
4320 because previous references within the function will have
4321 created symbols attached to the current namespace. */
4322 if (get_proc_name (name, &entry,
4323 gfc_current_ns->parent != NULL
4324 && module_procedure
4325 && gfc_current_ns->proc_name->attr.function))
6de9cd9a
DN
4326 return MATCH_ERROR;
4327
3d79abbd
PB
4328 proc = gfc_current_block ();
4329
4330 if (state == COMP_SUBROUTINE)
6de9cd9a 4331 {
231b2fcc 4332 /* An entry in a subroutine. */
68ea355b
PT
4333 if (!add_global_entry (name, 1))
4334 return MATCH_ERROR;
4335
6de9cd9a
DN
4336 m = gfc_match_formal_arglist (entry, 0, 1);
4337 if (m != MATCH_YES)
4338 return MATCH_ERROR;
4339
231b2fcc
TS
4340 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4341 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a 4342 return MATCH_ERROR;
3d79abbd
PB
4343 }
4344 else
4345 {
c96cfa49 4346 /* An entry in a function.
636dff67
SK
4347 We need to take special care because writing
4348 ENTRY f()
4349 as
4350 ENTRY f
4351 is allowed, whereas
4352 ENTRY f() RESULT (r)
4353 can't be written as
4354 ENTRY f RESULT (r). */
68ea355b
PT
4355 if (!add_global_entry (name, 0))
4356 return MATCH_ERROR;
4357
c96cfa49
TS
4358 old_loc = gfc_current_locus;
4359 if (gfc_match_eos () == MATCH_YES)
4360 {
4361 gfc_current_locus = old_loc;
4362 /* Match the empty argument list, and add the interface to
4363 the symbol. */
4364 m = gfc_match_formal_arglist (entry, 0, 1);
4365 }
4366 else
4367 m = gfc_match_formal_arglist (entry, 0, 0);
4368
6de9cd9a
DN
4369 if (m != MATCH_YES)
4370 return MATCH_ERROR;
4371
6de9cd9a
DN
4372 result = NULL;
4373
4374 if (gfc_match_eos () == MATCH_YES)
4375 {
231b2fcc
TS
4376 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4377 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a
DN
4378 return MATCH_ERROR;
4379
d198b59a 4380 entry->result = entry;
6de9cd9a
DN
4381 }
4382 else
4383 {
3d79abbd 4384 m = match_result (proc, &result);
6de9cd9a
DN
4385 if (m == MATCH_NO)
4386 gfc_syntax_error (ST_ENTRY);
4387 if (m != MATCH_YES)
4388 return MATCH_ERROR;
4389
231b2fcc
TS
4390 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4391 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
636dff67
SK
4392 || gfc_add_function (&entry->attr, result->name, NULL)
4393 == FAILURE)
6de9cd9a 4394 return MATCH_ERROR;
d198b59a
JJ
4395
4396 entry->result = result;
6de9cd9a 4397 }
6de9cd9a
DN
4398 }
4399
4400 if (gfc_match_eos () != MATCH_YES)
4401 {
4402 gfc_syntax_error (ST_ENTRY);
4403 return MATCH_ERROR;
4404 }
4405
3d79abbd
PB
4406 entry->attr.recursive = proc->attr.recursive;
4407 entry->attr.elemental = proc->attr.elemental;
4408 entry->attr.pure = proc->attr.pure;
6de9cd9a 4409
3d79abbd
PB
4410 el = gfc_get_entry_list ();
4411 el->sym = entry;
4412 el->next = gfc_current_ns->entries;
4413 gfc_current_ns->entries = el;
4414 if (el->next)
4415 el->id = el->next->id + 1;
4416 else
4417 el->id = 1;
6de9cd9a 4418
3d79abbd
PB
4419 new_st.op = EXEC_ENTRY;
4420 new_st.ext.entry = el;
4421
4422 return MATCH_YES;
6de9cd9a
DN
4423}
4424
4425
4426/* Match a subroutine statement, including optional prefixes. */
4427
4428match
4429gfc_match_subroutine (void)
4430{
4431 char name[GFC_MAX_SYMBOL_LEN + 1];
4432 gfc_symbol *sym;
4433 match m;
a8b3b0b6
CR
4434 match is_bind_c;
4435 char peek_char;
6de9cd9a
DN
4436
4437 if (gfc_current_state () != COMP_NONE
4438 && gfc_current_state () != COMP_INTERFACE
4439 && gfc_current_state () != COMP_CONTAINS)
4440 return MATCH_NO;
4441
4442 m = match_prefix (NULL);
4443 if (m != MATCH_YES)
4444 return m;
4445
4446 m = gfc_match ("subroutine% %n", name);
4447 if (m != MATCH_YES)
4448 return m;
4449
1a492601 4450 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4451 return MATCH_ERROR;
4452 gfc_new_block = sym;
4453
a8b3b0b6
CR
4454 /* Check what next non-whitespace character is so we can tell if there
4455 where the required parens if we have a BIND(C). */
4456 gfc_gobble_whitespace ();
4457 peek_char = gfc_peek_char ();
4458
231b2fcc 4459 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4460 return MATCH_ERROR;
4461
4462 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4463 return MATCH_ERROR;
4464
a8b3b0b6
CR
4465 /* Make sure that it isn't already declared as BIND(C). If it is, it
4466 must have been marked BIND(C) with a BIND(C) attribute and that is
4467 not allowed for procedures. */
4468 if (sym->attr.is_bind_c == 1)
4469 {
4470 sym->attr.is_bind_c = 0;
4471 if (sym->old_symbol != NULL)
4472 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4473 "variables or common blocks",
4474 &(sym->old_symbol->declared_at));
4475 else
4476 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4477 "variables or common blocks", &gfc_current_locus);
4478 }
4479
4480 /* Here, we are just checking if it has the bind(c) attribute, and if
4481 so, then we need to make sure it's all correct. If it doesn't,
4482 we still need to continue matching the rest of the subroutine line. */
4483 is_bind_c = gfc_match_bind_c (sym);
4484 if (is_bind_c == MATCH_ERROR)
4485 {
4486 /* There was an attempt at the bind(c), but it was wrong. An
4487 error message should have been printed w/in the gfc_match_bind_c
4488 so here we'll just return the MATCH_ERROR. */
4489 return MATCH_ERROR;
4490 }
4491
4492 if (is_bind_c == MATCH_YES)
4493 {
4494 if (peek_char != '(')
4495 {
4496 gfc_error ("Missing required parentheses before BIND(C) at %C");
4497 return MATCH_ERROR;
4498 }
4499 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4500 == FAILURE)
4501 return MATCH_ERROR;
4502 }
4503
6de9cd9a
DN
4504 if (gfc_match_eos () != MATCH_YES)
4505 {
4506 gfc_syntax_error (ST_SUBROUTINE);
4507 return MATCH_ERROR;
4508 }
4509
4510 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4511 return MATCH_ERROR;
4512
4513 return MATCH_YES;
4514}
4515
4516
a8b3b0b6
CR
4517/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4518 given, and set the binding label in either the given symbol (if not
86bf520d 4519 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
4520 encounter the BIND(C) before the declaration itself. Return
4521 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4522 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4523 or MATCH_YES if the specifier was correct and the binding label and
4524 bind(c) fields were set correctly for the given symbol or the
4525 current_ts. */
4526
4527match
4528gfc_match_bind_c (gfc_symbol *sym)
4529{
4530 /* binding label, if exists */
4531 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4532 match double_quote;
4533 match single_quote;
a8b3b0b6
CR
4534
4535 /* Initialize the flag that specifies whether we encountered a NAME=
4536 specifier or not. */
4537 has_name_equals = 0;
4538
4539 /* Init the first char to nil so we can catch if we don't have
4540 the label (name attr) or the symbol name yet. */
4541 binding_label[0] = '\0';
4542
4543 /* This much we have to be able to match, in this order, if
4544 there is a bind(c) label. */
4545 if (gfc_match (" bind ( c ") != MATCH_YES)
4546 return MATCH_NO;
4547
4548 /* Now see if there is a binding label, or if we've reached the
4549 end of the bind(c) attribute without one. */
4550 if (gfc_match_char (',') == MATCH_YES)
4551 {
4552 if (gfc_match (" name = ") != MATCH_YES)
4553 {
4554 gfc_error ("Syntax error in NAME= specifier for binding label "
4555 "at %C");
4556 /* should give an error message here */
4557 return MATCH_ERROR;
4558 }
4559
4560 has_name_equals = 1;
4561
4562 /* Get the opening quote. */
4563 double_quote = MATCH_YES;
4564 single_quote = MATCH_YES;
4565 double_quote = gfc_match_char ('"');
4566 if (double_quote != MATCH_YES)
4567 single_quote = gfc_match_char ('\'');
4568 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4569 {
4570 gfc_error ("Syntax error in NAME= specifier for binding label "
4571 "at %C");
4572 return MATCH_ERROR;
4573 }
4574
4575 /* Grab the binding label, using functions that will not lower
4576 case the names automatically. */
4577 if (gfc_match_name_C (binding_label) != MATCH_YES)
4578 return MATCH_ERROR;
4579
4580 /* Get the closing quotation. */
4581 if (double_quote == MATCH_YES)
4582 {
4583 if (gfc_match_char ('"') != MATCH_YES)
4584 {
4585 gfc_error ("Missing closing quote '\"' for binding label at %C");
4586 /* User started string with '"' so looked to match it. */
4587 return MATCH_ERROR;
4588 }
4589 }
4590 else
4591 {
4592 if (gfc_match_char ('\'') != MATCH_YES)
4593 {
4594 gfc_error ("Missing closing quote '\'' for binding label at %C");
4595 /* User started string with "'" char. */
4596 return MATCH_ERROR;
4597 }
4598 }
4599 }
4600
4601 /* Get the required right paren. */
4602 if (gfc_match_char (')') != MATCH_YES)
4603 {
4604 gfc_error ("Missing closing paren for binding label at %C");
4605 return MATCH_ERROR;
4606 }
4607
4608 /* Save the binding label to the symbol. If sym is null, we're
4609 probably matching the typespec attributes of a declaration and
4610 haven't gotten the name yet, and therefore, no symbol yet. */
4611 if (binding_label[0] != '\0')
4612 {
4613 if (sym != NULL)
4614 {
4615 strncpy (sym->binding_label, binding_label,
4616 strlen (binding_label)+1);
4617 }
4618 else
4619 strncpy (curr_binding_label, binding_label,
4620 strlen (binding_label) + 1);
4621 }
4622 else
4623 {
4624 /* No binding label, but if symbol isn't null, we
4625 can set the label for it here. */
4626 /* TODO: If the name= was given and no binding label (name=""), we simply
4627 will let fortran mangle the symbol name as it usually would.
4628 However, this could still let C call it if the user looked up the
4629 symbol in the object file. Should the name set during mangling in
4630 trans-decl.c be marked with characters that are invalid for C to
4631 prevent this? */
4632 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4633 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4634 }
9e1d712c 4635
129d15a3
JW
4636 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4637 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
4638 {
4639 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4640 return MATCH_ERROR;
4641 }
4642
a8b3b0b6
CR
4643 return MATCH_YES;
4644}
4645
4646
1f2959f0 4647/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
4648
4649static int
4650contained_procedure (void)
4651{
4652 gfc_state_data *s;
4653
4654 for (s=gfc_state_stack; s; s=s->previous)
4655 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
636dff67 4656 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
ddc9ce91
TS
4657 return 1;
4658
4659 return 0;
4660}
4661
d51347f9 4662/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
4663 interoperable with the corresponding C enumeration type, making
4664 sure that -fshort-enums is honored. */
4665
4666static void
4667set_enum_kind(void)
4668{
4669 enumerator_history *current_history = NULL;
4670 int kind;
4671 int i;
4672
4673 if (max_enum == NULL || enum_history == NULL)
4674 return;
4675
4676 if (!gfc_option.fshort_enums)
d51347f9
TB
4677 return;
4678
25d8f0a2
TS
4679 i = 0;
4680 do
4681 {
4682 kind = gfc_integer_kinds[i++].kind;
4683 }
d51347f9 4684 while (kind < gfc_c_int_kind
25d8f0a2
TS
4685 && gfc_check_integer_range (max_enum->initializer->value.integer,
4686 kind) != ARITH_OK);
4687
4688 current_history = enum_history;
4689 while (current_history != NULL)
4690 {
4691 current_history->sym->ts.kind = kind;
4692 current_history = current_history->next;
4693 }
4694}
4695
636dff67 4696
6de9cd9a
DN
4697/* Match any of the various end-block statements. Returns the type of
4698 END to the caller. The END INTERFACE, END IF, END DO and END
4699 SELECT statements cannot be replaced by a single END statement. */
4700
4701match
636dff67 4702gfc_match_end (gfc_statement *st)
6de9cd9a
DN
4703{
4704 char name[GFC_MAX_SYMBOL_LEN + 1];
4705 gfc_compile_state state;
4706 locus old_loc;
4707 const char *block_name;
4708 const char *target;
ddc9ce91 4709 int eos_ok;
6de9cd9a
DN
4710 match m;
4711
63645982 4712 old_loc = gfc_current_locus;
6de9cd9a
DN
4713 if (gfc_match ("end") != MATCH_YES)
4714 return MATCH_NO;
4715
4716 state = gfc_current_state ();
636dff67
SK
4717 block_name = gfc_current_block () == NULL
4718 ? NULL : gfc_current_block ()->name;
6de9cd9a
DN
4719
4720 if (state == COMP_CONTAINS)
4721 {
4722 state = gfc_state_stack->previous->state;
636dff67
SK
4723 block_name = gfc_state_stack->previous->sym == NULL
4724 ? NULL : gfc_state_stack->previous->sym->name;
6de9cd9a
DN
4725 }
4726
4727 switch (state)
4728 {
4729 case COMP_NONE:
4730 case COMP_PROGRAM:
4731 *st = ST_END_PROGRAM;
4732 target = " program";
ddc9ce91 4733 eos_ok = 1;
6de9cd9a
DN
4734 break;
4735
4736 case COMP_SUBROUTINE:
4737 *st = ST_END_SUBROUTINE;
4738 target = " subroutine";
ddc9ce91 4739 eos_ok = !contained_procedure ();
6de9cd9a
DN
4740 break;
4741
4742 case COMP_FUNCTION:
4743 *st = ST_END_FUNCTION;
4744 target = " function";
ddc9ce91 4745 eos_ok = !contained_procedure ();
6de9cd9a
DN
4746 break;
4747
4748 case COMP_BLOCK_DATA:
4749 *st = ST_END_BLOCK_DATA;
4750 target = " block data";
ddc9ce91 4751 eos_ok = 1;
6de9cd9a
DN
4752 break;
4753
4754 case COMP_MODULE:
4755 *st = ST_END_MODULE;
4756 target = " module";
ddc9ce91 4757 eos_ok = 1;
6de9cd9a
DN
4758 break;
4759
4760 case COMP_INTERFACE:
4761 *st = ST_END_INTERFACE;
4762 target = " interface";
ddc9ce91 4763 eos_ok = 0;
6de9cd9a
DN
4764 break;
4765
4766 case COMP_DERIVED:
4767 *st = ST_END_TYPE;
4768 target = " type";
ddc9ce91 4769 eos_ok = 0;
6de9cd9a
DN
4770 break;
4771
4772 case COMP_IF:
4773 *st = ST_ENDIF;
4774 target = " if";
ddc9ce91 4775 eos_ok = 0;
6de9cd9a
DN
4776 break;
4777
4778 case COMP_DO:
4779 *st = ST_ENDDO;
4780 target = " do";
ddc9ce91 4781 eos_ok = 0;
6de9cd9a
DN
4782 break;
4783
4784 case COMP_SELECT:
4785 *st = ST_END_SELECT;
4786 target = " select";
ddc9ce91 4787 eos_ok = 0;
6de9cd9a
DN
4788 break;
4789
4790 case COMP_FORALL:
4791 *st = ST_END_FORALL;
4792 target = " forall";
ddc9ce91 4793 eos_ok = 0;
6de9cd9a
DN
4794 break;
4795
4796 case COMP_WHERE:
4797 *st = ST_END_WHERE;
4798 target = " where";
ddc9ce91 4799 eos_ok = 0;
6de9cd9a
DN
4800 break;
4801
25d8f0a2
TS
4802 case COMP_ENUM:
4803 *st = ST_END_ENUM;
4804 target = " enum";
4805 eos_ok = 0;
4806 last_initializer = NULL;
4807 set_enum_kind ();
4808 gfc_free_enum_history ();
4809 break;
4810
6de9cd9a
DN
4811 default:
4812 gfc_error ("Unexpected END statement at %C");
4813 goto cleanup;
4814 }
4815
4816 if (gfc_match_eos () == MATCH_YES)
4817 {
ddc9ce91 4818 if (!eos_ok)
6de9cd9a 4819 {
66e4ab31 4820 /* We would have required END [something]. */
59ce85b5
TS
4821 gfc_error ("%s statement expected at %L",
4822 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
4823 goto cleanup;
4824 }
4825
4826 return MATCH_YES;
4827 }
4828
4829 /* Verify that we've got the sort of end-block that we're expecting. */
4830 if (gfc_match (target) != MATCH_YES)
4831 {
4832 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4833 goto cleanup;
4834 }
4835
4836 /* If we're at the end, make sure a block name wasn't required. */
4837 if (gfc_match_eos () == MATCH_YES)
4838 {
4839
690af379
TS
4840 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4841 && *st != ST_END_FORALL && *st != ST_END_WHERE)
6de9cd9a
DN
4842 return MATCH_YES;
4843
4844 if (gfc_current_block () == NULL)
4845 return MATCH_YES;
4846
4847 gfc_error ("Expected block name of '%s' in %s statement at %C",
4848 block_name, gfc_ascii_statement (*st));
4849
4850 return MATCH_ERROR;
4851 }
4852
4853 /* END INTERFACE has a special handler for its several possible endings. */
4854 if (*st == ST_END_INTERFACE)
4855 return gfc_match_end_interface ();
4856
66e4ab31
SK
4857 /* We haven't hit the end of statement, so what is left must be an
4858 end-name. */
6de9cd9a
DN
4859 m = gfc_match_space ();
4860 if (m == MATCH_YES)
4861 m = gfc_match_name (name);
4862
4863 if (m == MATCH_NO)
4864 gfc_error ("Expected terminating name at %C");
4865 if (m != MATCH_YES)
4866 goto cleanup;
4867
4868 if (block_name == NULL)
4869 goto syntax;
4870
4871 if (strcmp (name, block_name) != 0)
4872 {
4873 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4874 gfc_ascii_statement (*st));
4875 goto cleanup;
4876 }
4877
4878 if (gfc_match_eos () == MATCH_YES)
4879 return MATCH_YES;
4880
4881syntax:
4882 gfc_syntax_error (*st);
4883
4884cleanup:
63645982 4885 gfc_current_locus = old_loc;
6de9cd9a
DN
4886 return MATCH_ERROR;
4887}
4888
4889
4890
4891/***************** Attribute declaration statements ****************/
4892
4893/* Set the attribute of a single variable. */
4894
4895static match
4896attr_decl1 (void)
4897{
4898 char name[GFC_MAX_SYMBOL_LEN + 1];
4899 gfc_array_spec *as;
4900 gfc_symbol *sym;
4901 locus var_locus;
4902 match m;
4903
4904 as = NULL;
4905
4906 m = gfc_match_name (name);
4907 if (m != MATCH_YES)
4908 goto cleanup;
4909
4910 if (find_special (name, &sym))
4911 return MATCH_ERROR;
4912
63645982 4913 var_locus = gfc_current_locus;
6de9cd9a
DN
4914
4915 /* Deal with possible array specification for certain attributes. */
4916 if (current_attr.dimension
4917 || current_attr.allocatable
4918 || current_attr.pointer
4919 || current_attr.target)
4920 {
4921 m = gfc_match_array_spec (&as);
4922 if (m == MATCH_ERROR)
4923 goto cleanup;
4924
4925 if (current_attr.dimension && m == MATCH_NO)
4926 {
636dff67
SK
4927 gfc_error ("Missing array specification at %L in DIMENSION "
4928 "statement", &var_locus);
6de9cd9a
DN
4929 m = MATCH_ERROR;
4930 goto cleanup;
4931 }
4932
4933 if ((current_attr.allocatable || current_attr.pointer)
4934 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4935 {
636dff67 4936 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
4937 m = MATCH_ERROR;
4938 goto cleanup;
4939 }
4940 }
4941
636dff67
SK
4942 /* Update symbol table. DIMENSION attribute is set
4943 in gfc_set_array_spec(). */
6de9cd9a
DN
4944 if (current_attr.dimension == 0
4945 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4946 {
4947 m = MATCH_ERROR;
4948 goto cleanup;
4949 }
4950
4951 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4952 {
4953 m = MATCH_ERROR;
4954 goto cleanup;
4955 }
d51347f9 4956
83d890b9
AL
4957 if (sym->attr.cray_pointee && sym->as != NULL)
4958 {
4959 /* Fix the array spec. */
4960 m = gfc_mod_pointee_as (sym->as);
4961 if (m == MATCH_ERROR)
4962 goto cleanup;
4963 }
6de9cd9a 4964
7114edca 4965 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
1902704e
PT
4966 {
4967 m = MATCH_ERROR;
4968 goto cleanup;
4969 }
4970
6de9cd9a
DN
4971 if ((current_attr.external || current_attr.intrinsic)
4972 && sym->attr.flavor != FL_PROCEDURE
231b2fcc 4973 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4974 {
4975 m = MATCH_ERROR;
4976 goto cleanup;
4977 }
4978
4979 return MATCH_YES;
4980
4981cleanup:
4982 gfc_free_array_spec (as);
4983 return m;
4984}
4985
4986
4987/* Generic attribute declaration subroutine. Used for attributes that
4988 just have a list of names. */
4989
4990static match
4991attr_decl (void)
4992{
4993 match m;
4994
4995 /* Gobble the optional double colon, by simply ignoring the result
4996 of gfc_match(). */
4997 gfc_match (" ::");
4998
4999 for (;;)
5000 {
5001 m = attr_decl1 ();
5002 if (m != MATCH_YES)
5003 break;
5004
5005 if (gfc_match_eos () == MATCH_YES)
5006 {
5007 m = MATCH_YES;
5008 break;
5009 }
5010
5011 if (gfc_match_char (',') != MATCH_YES)
5012 {
5013 gfc_error ("Unexpected character in variable list at %C");
5014 m = MATCH_ERROR;
5015 break;
5016 }
5017 }
5018
5019 return m;
5020}
5021
5022
83d890b9
AL
5023/* This routine matches Cray Pointer declarations of the form:
5024 pointer ( <pointer>, <pointee> )
5025 or
d51347f9
TB
5026 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5027 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
5028 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5029 be either a scalar, or an array declaration. No space is allocated for
d51347f9 5030 the pointee. For the statement
83d890b9
AL
5031 pointer (ipt, ar(10))
5032 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 5033 ar(i) => ((<type> *) ipt)(i)
b122dc6a 5034 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
5035
5036static match
5037cray_pointer_decl (void)
5038{
5039 match m;
5040 gfc_array_spec *as;
5041 gfc_symbol *cptr; /* Pointer symbol. */
5042 gfc_symbol *cpte; /* Pointee symbol. */
5043 locus var_locus;
5044 bool done = false;
5045
5046 while (!done)
5047 {
5048 if (gfc_match_char ('(') != MATCH_YES)
5049 {
5050 gfc_error ("Expected '(' at %C");
d51347f9 5051 return MATCH_ERROR;
83d890b9 5052 }
d51347f9 5053
83d890b9
AL
5054 /* Match pointer. */
5055 var_locus = gfc_current_locus;
5056 gfc_clear_attr (&current_attr);
5057 gfc_add_cray_pointer (&current_attr, &var_locus);
5058 current_ts.type = BT_INTEGER;
5059 current_ts.kind = gfc_index_integer_kind;
5060
d51347f9 5061 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
5062 if (m != MATCH_YES)
5063 {
5064 gfc_error ("Expected variable name at %C");
5065 return m;
5066 }
d51347f9 5067
83d890b9
AL
5068 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5069 return MATCH_ERROR;
5070
d51347f9 5071 gfc_set_sym_referenced (cptr);
83d890b9
AL
5072
5073 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5074 {
5075 cptr->ts.type = BT_INTEGER;
d51347f9 5076 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
5077 }
5078 else if (cptr->ts.type != BT_INTEGER)
5079 {
e25a0da3 5080 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
5081 return MATCH_ERROR;
5082 }
5083 else if (cptr->ts.kind < gfc_index_integer_kind)
5084 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 5085 " memory addresses require %d bytes",
636dff67 5086 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
5087
5088 if (gfc_match_char (',') != MATCH_YES)
5089 {
5090 gfc_error ("Expected \",\" at %C");
d51347f9 5091 return MATCH_ERROR;
83d890b9
AL
5092 }
5093
d51347f9 5094 /* Match Pointee. */
83d890b9
AL
5095 var_locus = gfc_current_locus;
5096 gfc_clear_attr (&current_attr);
5097 gfc_add_cray_pointee (&current_attr, &var_locus);
5098 current_ts.type = BT_UNKNOWN;
5099 current_ts.kind = 0;
5100
5101 m = gfc_match_symbol (&cpte, 0);
5102 if (m != MATCH_YES)
5103 {
5104 gfc_error ("Expected variable name at %C");
5105 return m;
5106 }
d51347f9 5107
83d890b9
AL
5108 /* Check for an optional array spec. */
5109 m = gfc_match_array_spec (&as);
5110 if (m == MATCH_ERROR)
5111 {
5112 gfc_free_array_spec (as);
5113 return m;
5114 }
5115 else if (m == MATCH_NO)
5116 {
5117 gfc_free_array_spec (as);
5118 as = NULL;
5119 }
5120
5121 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5122 return MATCH_ERROR;
5123
5124 gfc_set_sym_referenced (cpte);
5125
5126 if (cpte->as == NULL)
5127 {
5128 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5129 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5130 }
5131 else if (as != NULL)
5132 {
e25a0da3 5133 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
5134 gfc_free_array_spec (as);
5135 return MATCH_ERROR;
5136 }
5137
5138 as = NULL;
5139
5140 if (cpte->as != NULL)
5141 {
5142 /* Fix array spec. */
5143 m = gfc_mod_pointee_as (cpte->as);
5144 if (m == MATCH_ERROR)
5145 return m;
5146 }
5147
5148 /* Point the Pointee at the Pointer. */
b122dc6a 5149 cpte->cp_pointer = cptr;
83d890b9
AL
5150
5151 if (gfc_match_char (')') != MATCH_YES)
5152 {
5153 gfc_error ("Expected \")\" at %C");
5154 return MATCH_ERROR;
5155 }
5156 m = gfc_match_char (',');
5157 if (m != MATCH_YES)
5158 done = true; /* Stop searching for more declarations. */
5159
5160 }
5161
5162 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5163 || gfc_match_eos () != MATCH_YES)
5164 {
5165 gfc_error ("Expected \",\" or end of statement at %C");
5166 return MATCH_ERROR;
5167 }
5168 return MATCH_YES;
5169}
5170
5171
6de9cd9a
DN
5172match
5173gfc_match_external (void)
5174{
5175
5176 gfc_clear_attr (&current_attr);
1902704e 5177 current_attr.external = 1;
6de9cd9a
DN
5178
5179 return attr_decl ();
5180}
5181
5182
6de9cd9a
DN
5183match
5184gfc_match_intent (void)
5185{
5186 sym_intent intent;
5187
5188 intent = match_intent_spec ();
5189 if (intent == INTENT_UNKNOWN)
5190 return MATCH_ERROR;
5191
5192 gfc_clear_attr (&current_attr);
1902704e 5193 current_attr.intent = intent;
6de9cd9a
DN
5194
5195 return attr_decl ();
5196}
5197
5198
5199match
5200gfc_match_intrinsic (void)
5201{
5202
5203 gfc_clear_attr (&current_attr);
1902704e 5204 current_attr.intrinsic = 1;
6de9cd9a
DN
5205
5206 return attr_decl ();
5207}
5208
5209
5210match
5211gfc_match_optional (void)
5212{
5213
5214 gfc_clear_attr (&current_attr);
1902704e 5215 current_attr.optional = 1;
6de9cd9a
DN
5216
5217 return attr_decl ();
5218}
5219
5220
5221match
5222gfc_match_pointer (void)
5223{
83d890b9
AL
5224 gfc_gobble_whitespace ();
5225 if (gfc_peek_char () == '(')
5226 {
5227 if (!gfc_option.flag_cray_pointer)
5228 {
636dff67
SK
5229 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5230 "flag");
83d890b9
AL
5231 return MATCH_ERROR;
5232 }
5233 return cray_pointer_decl ();
5234 }
5235 else
5236 {
5237 gfc_clear_attr (&current_attr);
1902704e 5238 current_attr.pointer = 1;
83d890b9
AL
5239
5240 return attr_decl ();
5241 }
6de9cd9a
DN
5242}
5243
5244
5245match
5246gfc_match_allocatable (void)
5247{
6de9cd9a 5248 gfc_clear_attr (&current_attr);
1902704e 5249 current_attr.allocatable = 1;
6de9cd9a
DN
5250
5251 return attr_decl ();
5252}
5253
5254
5255match
5256gfc_match_dimension (void)
5257{
6de9cd9a 5258 gfc_clear_attr (&current_attr);
1902704e 5259 current_attr.dimension = 1;
6de9cd9a
DN
5260
5261 return attr_decl ();
5262}
5263
5264
5265match
5266gfc_match_target (void)
5267{
6de9cd9a 5268 gfc_clear_attr (&current_attr);
1902704e 5269 current_attr.target = 1;
6de9cd9a
DN
5270
5271 return attr_decl ();
5272}
5273
5274
5275/* Match the list of entities being specified in a PUBLIC or PRIVATE
5276 statement. */
5277
5278static match
5279access_attr_decl (gfc_statement st)
5280{
5281 char name[GFC_MAX_SYMBOL_LEN + 1];
5282 interface_type type;
5283 gfc_user_op *uop;
5284 gfc_symbol *sym;
5285 gfc_intrinsic_op operator;
5286 match m;
5287
5288 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5289 goto done;
5290
5291 for (;;)
5292 {
5293 m = gfc_match_generic_spec (&type, name, &operator);
5294 if (m == MATCH_NO)
5295 goto syntax;
5296 if (m == MATCH_ERROR)
5297 return MATCH_ERROR;
5298
5299 switch (type)
5300 {
5301 case INTERFACE_NAMELESS:
9e1d712c 5302 case INTERFACE_ABSTRACT:
6de9cd9a
DN
5303 goto syntax;
5304
5305 case INTERFACE_GENERIC:
5306 if (gfc_get_symbol (name, NULL, &sym))
5307 goto done;
5308
636dff67
SK
5309 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5310 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
231b2fcc 5311 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5312 return MATCH_ERROR;
5313
5314 break;
5315
5316 case INTERFACE_INTRINSIC_OP:
5317 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5318 {
5319 gfc_current_ns->operator_access[operator] =
5320 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5321 }
5322 else
5323 {
5324 gfc_error ("Access specification of the %s operator at %C has "
5325 "already been specified", gfc_op2string (operator));
5326 goto done;
5327 }
5328
5329 break;
5330
5331 case INTERFACE_USER_OP:
5332 uop = gfc_get_uop (name);
5333
5334 if (uop->access == ACCESS_UNKNOWN)
5335 {
636dff67
SK
5336 uop->access = (st == ST_PUBLIC)
5337 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
5338 }
5339 else
5340 {
636dff67
SK
5341 gfc_error ("Access specification of the .%s. operator at %C "
5342 "has already been specified", sym->name);
6de9cd9a
DN
5343 goto done;
5344 }
5345
5346 break;
5347 }
5348
5349 if (gfc_match_char (',') == MATCH_NO)
5350 break;
5351 }
5352
5353 if (gfc_match_eos () != MATCH_YES)
5354 goto syntax;
5355 return MATCH_YES;
5356
5357syntax:
5358 gfc_syntax_error (st);
5359
5360done:
5361 return MATCH_ERROR;
5362}
5363
5364
ee7e677f
TB
5365match
5366gfc_match_protected (void)
5367{
5368 gfc_symbol *sym;
5369 match m;
5370
5371 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5372 {
5373 gfc_error ("PROTECTED at %C only allowed in specification "
5374 "part of a module");
5375 return MATCH_ERROR;
5376
5377 }
5378
636dff67 5379 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
ee7e677f
TB
5380 == FAILURE)
5381 return MATCH_ERROR;
5382
5383 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5384 {
5385 return MATCH_ERROR;
5386 }
5387
5388 if (gfc_match_eos () == MATCH_YES)
5389 goto syntax;
5390
5391 for(;;)
5392 {
5393 m = gfc_match_symbol (&sym, 0);
5394 switch (m)
5395 {
5396 case MATCH_YES:
636dff67
SK
5397 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5398 == FAILURE)
ee7e677f
TB
5399 return MATCH_ERROR;
5400 goto next_item;
5401
5402 case MATCH_NO:
5403 break;
5404
5405 case MATCH_ERROR:
5406 return MATCH_ERROR;
5407 }
5408
5409 next_item:
5410 if (gfc_match_eos () == MATCH_YES)
5411 break;
5412 if (gfc_match_char (',') != MATCH_YES)
5413 goto syntax;
5414 }
5415
5416 return MATCH_YES;
5417
5418syntax:
5419 gfc_error ("Syntax error in PROTECTED statement at %C");
5420 return MATCH_ERROR;
5421}
5422
5423
86bf520d 5424/* The PRIVATE statement is a bit weird in that it can be an attribute
6de9cd9a
DN
5425 declaration, but also works as a standlone statement inside of a
5426 type declaration or a module. */
5427
5428match
636dff67 5429gfc_match_private (gfc_statement *st)
6de9cd9a
DN
5430{
5431
5432 if (gfc_match ("private") != MATCH_YES)
5433 return MATCH_NO;
5434
d51347f9
TB
5435 if (gfc_current_state () != COMP_MODULE
5436 && (gfc_current_state () != COMP_DERIVED
5437 || !gfc_state_stack->previous
5438 || gfc_state_stack->previous->state != COMP_MODULE))
5439 {
5440 gfc_error ("PRIVATE statement at %C is only allowed in the "
5441 "specification part of a module");
5442 return MATCH_ERROR;
5443 }
5444
6de9cd9a
DN
5445 if (gfc_current_state () == COMP_DERIVED)
5446 {
5447 if (gfc_match_eos () == MATCH_YES)
5448 {
5449 *st = ST_PRIVATE;
5450 return MATCH_YES;
5451 }
5452
5453 gfc_syntax_error (ST_PRIVATE);
5454 return MATCH_ERROR;
5455 }
5456
5457 if (gfc_match_eos () == MATCH_YES)
5458 {
5459 *st = ST_PRIVATE;
5460 return MATCH_YES;
5461 }
5462
5463 *st = ST_ATTR_DECL;
5464 return access_attr_decl (ST_PRIVATE);
5465}
5466
5467
5468match
636dff67 5469gfc_match_public (gfc_statement *st)
6de9cd9a
DN
5470{
5471
5472 if (gfc_match ("public") != MATCH_YES)
5473 return MATCH_NO;
5474
d51347f9
TB
5475 if (gfc_current_state () != COMP_MODULE)
5476 {
5477 gfc_error ("PUBLIC statement at %C is only allowed in the "
5478 "specification part of a module");
5479 return MATCH_ERROR;
5480 }
5481
6de9cd9a
DN
5482 if (gfc_match_eos () == MATCH_YES)
5483 {
5484 *st = ST_PUBLIC;
5485 return MATCH_YES;
5486 }
5487
5488 *st = ST_ATTR_DECL;
5489 return access_attr_decl (ST_PUBLIC);
5490}
5491
5492
5493/* Workhorse for gfc_match_parameter. */
5494
5495static match
5496do_parm (void)
5497{
5498 gfc_symbol *sym;
5499 gfc_expr *init;
5500 match m;
5501
5502 m = gfc_match_symbol (&sym, 0);
5503 if (m == MATCH_NO)
5504 gfc_error ("Expected variable name at %C in PARAMETER statement");
5505
5506 if (m != MATCH_YES)
5507 return m;
5508
5509 if (gfc_match_char ('=') == MATCH_NO)
5510 {
5511 gfc_error ("Expected = sign in PARAMETER statement at %C");
5512 return MATCH_ERROR;
5513 }
5514
5515 m = gfc_match_init_expr (&init);
5516 if (m == MATCH_NO)
5517 gfc_error ("Expected expression at %C in PARAMETER statement");
5518 if (m != MATCH_YES)
5519 return m;
5520
5521 if (sym->ts.type == BT_UNKNOWN
5522 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5523 {
5524 m = MATCH_ERROR;
5525 goto cleanup;
5526 }
5527
5528 if (gfc_check_assign_symbol (sym, init) == FAILURE
231b2fcc 5529 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5530 {
5531 m = MATCH_ERROR;
5532 goto cleanup;
5533 }
5534
7e2eba4b
DE
5535 if (sym->ts.type == BT_CHARACTER
5536 && sym->ts.cl != NULL
5537 && sym->ts.cl->length != NULL
5538 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5539 && init->expr_type == EXPR_CONSTANT
5540 && init->ts.type == BT_CHARACTER
5541 && init->ts.kind == 1)
5542 gfc_set_constant_character_len (
2220652d 5543 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
7e2eba4b 5544
6de9cd9a
DN
5545 sym->value = init;
5546 return MATCH_YES;
5547
5548cleanup:
5549 gfc_free_expr (init);
5550 return m;
5551}
5552
5553
5554/* Match a parameter statement, with the weird syntax that these have. */
5555
5556match
5557gfc_match_parameter (void)
5558{
5559 match m;
5560
5561 if (gfc_match_char ('(') == MATCH_NO)
5562 return MATCH_NO;
5563
5564 for (;;)
5565 {
5566 m = do_parm ();
5567 if (m != MATCH_YES)
5568 break;
5569
5570 if (gfc_match (" )%t") == MATCH_YES)
5571 break;
5572
5573 if (gfc_match_char (',') != MATCH_YES)
5574 {
5575 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5576 m = MATCH_ERROR;
5577 break;
5578 }
5579 }
5580
5581 return m;
5582}
5583
5584
5585/* Save statements have a special syntax. */
5586
5587match
5588gfc_match_save (void)
5589{
9056bd70
TS
5590 char n[GFC_MAX_SYMBOL_LEN+1];
5591 gfc_common_head *c;
6de9cd9a
DN
5592 gfc_symbol *sym;
5593 match m;
5594
5595 if (gfc_match_eos () == MATCH_YES)
5596 {
5597 if (gfc_current_ns->seen_save)
5598 {
636dff67
SK
5599 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5600 "follows previous SAVE statement")
09e87839
AL
5601 == FAILURE)
5602 return MATCH_ERROR;
6de9cd9a
DN
5603 }
5604
5605 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5606 return MATCH_YES;
5607 }
5608
5609 if (gfc_current_ns->save_all)
5610 {
636dff67
SK
5611 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5612 "blanket SAVE statement")
09e87839
AL
5613 == FAILURE)
5614 return MATCH_ERROR;
6de9cd9a
DN
5615 }
5616
5617 gfc_match (" ::");
5618
5619 for (;;)
5620 {
5621 m = gfc_match_symbol (&sym, 0);
5622 switch (m)
5623 {
5624 case MATCH_YES:
636dff67
SK
5625 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5626 == FAILURE)
6de9cd9a
DN
5627 return MATCH_ERROR;
5628 goto next_item;
5629
5630 case MATCH_NO:
5631 break;
5632
5633 case MATCH_ERROR:
5634 return MATCH_ERROR;
5635 }
5636
9056bd70 5637 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
5638 if (m == MATCH_ERROR)
5639 return MATCH_ERROR;
5640 if (m == MATCH_NO)
5641 goto syntax;
5642
53814b8f 5643 c = gfc_get_common (n, 0);
9056bd70
TS
5644 c->saved = 1;
5645
6de9cd9a
DN
5646 gfc_current_ns->seen_save = 1;
5647
5648 next_item:
5649 if (gfc_match_eos () == MATCH_YES)
5650 break;
5651 if (gfc_match_char (',') != MATCH_YES)
5652 goto syntax;
5653 }
5654
5655 return MATCH_YES;
5656
5657syntax:
5658 gfc_error ("Syntax error in SAVE statement at %C");
5659 return MATCH_ERROR;
5660}
5661
5662
06469efd
PT
5663match
5664gfc_match_value (void)
5665{
5666 gfc_symbol *sym;
5667 match m;
5668
636dff67 5669 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
06469efd
PT
5670 == FAILURE)
5671 return MATCH_ERROR;
5672
5673 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5674 {
5675 return MATCH_ERROR;
5676 }
5677
5678 if (gfc_match_eos () == MATCH_YES)
5679 goto syntax;
5680
5681 for(;;)
5682 {
5683 m = gfc_match_symbol (&sym, 0);
5684 switch (m)
5685 {
5686 case MATCH_YES:
636dff67
SK
5687 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5688 == FAILURE)
06469efd
PT
5689 return MATCH_ERROR;
5690 goto next_item;
5691
5692 case MATCH_NO:
5693 break;
5694
5695 case MATCH_ERROR:
5696 return MATCH_ERROR;
5697 }
5698
5699 next_item:
5700 if (gfc_match_eos () == MATCH_YES)
5701 break;
5702 if (gfc_match_char (',') != MATCH_YES)
5703 goto syntax;
5704 }
5705
5706 return MATCH_YES;
5707
5708syntax:
5709 gfc_error ("Syntax error in VALUE statement at %C");
5710 return MATCH_ERROR;
5711}
5712
66e4ab31 5713
775e6c3a
TB
5714match
5715gfc_match_volatile (void)
5716{
5717 gfc_symbol *sym;
5718 match m;
5719
636dff67 5720 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
775e6c3a
TB
5721 == FAILURE)
5722 return MATCH_ERROR;
5723
5724 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5725 {
5726 return MATCH_ERROR;
5727 }
5728
5729 if (gfc_match_eos () == MATCH_YES)
5730 goto syntax;
5731
5732 for(;;)
5733 {
9bce3c1c
TB
5734 /* VOLATILE is special because it can be added to host-associated
5735 symbols locally. */
5736 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
5737 switch (m)
5738 {
5739 case MATCH_YES:
636dff67
SK
5740 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5741 == FAILURE)
775e6c3a
TB
5742 return MATCH_ERROR;
5743 goto next_item;
5744
5745 case MATCH_NO:
5746 break;
5747
5748 case MATCH_ERROR:
5749 return MATCH_ERROR;
5750 }
5751
5752 next_item:
5753 if (gfc_match_eos () == MATCH_YES)
5754 break;
5755 if (gfc_match_char (',') != MATCH_YES)
5756 goto syntax;
5757 }
5758
5759 return MATCH_YES;
5760
5761syntax:
5762 gfc_error ("Syntax error in VOLATILE statement at %C");
5763 return MATCH_ERROR;
5764}
5765
5766
6de9cd9a
DN
5767/* Match a module procedure statement. Note that we have to modify
5768 symbols in the parent's namespace because the current one was there
49de9e73 5769 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
5770
5771match
5772gfc_match_modproc (void)
5773{
5774 char name[GFC_MAX_SYMBOL_LEN + 1];
5775 gfc_symbol *sym;
5776 match m;
060fca4a 5777 gfc_namespace *module_ns;
6de9cd9a
DN
5778
5779 if (gfc_state_stack->state != COMP_INTERFACE
5780 || gfc_state_stack->previous == NULL
129d15a3
JW
5781 || current_interface.type == INTERFACE_NAMELESS
5782 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 5783 {
636dff67
SK
5784 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5785 "interface");
6de9cd9a
DN
5786 return MATCH_ERROR;
5787 }
5788
060fca4a
PT
5789 module_ns = gfc_current_ns->parent;
5790 for (; module_ns; module_ns = module_ns->parent)
5791 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5792 break;
5793
5794 if (module_ns == NULL)
5795 return MATCH_ERROR;
5796
6de9cd9a
DN
5797 for (;;)
5798 {
5799 m = gfc_match_name (name);
5800 if (m == MATCH_NO)
5801 goto syntax;
5802 if (m != MATCH_YES)
5803 return MATCH_ERROR;
5804
060fca4a 5805 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
5806 return MATCH_ERROR;
5807
5808 if (sym->attr.proc != PROC_MODULE
231b2fcc
TS
5809 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5810 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5811 return MATCH_ERROR;
5812
5813 if (gfc_add_interface (sym) == FAILURE)
5814 return MATCH_ERROR;
5815
71f77fd7
PT
5816 sym->attr.mod_proc = 1;
5817
6de9cd9a
DN
5818 if (gfc_match_eos () == MATCH_YES)
5819 break;
5820 if (gfc_match_char (',') != MATCH_YES)
5821 goto syntax;
5822 }
5823
5824 return MATCH_YES;
5825
5826syntax:
5827 gfc_syntax_error (ST_MODULE_PROC);
5828 return MATCH_ERROR;
5829}
5830
5831
a8b3b0b6
CR
5832/* Match the optional attribute specifiers for a type declaration.
5833 Return MATCH_ERROR if an error is encountered in one of the handled
5834 attributes (public, private, bind(c)), MATCH_NO if what's found is
5835 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5836 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
5837
5838match
a8b3b0b6 5839gfc_get_type_attr_spec (symbol_attribute *attr)
6de9cd9a 5840{
a8b3b0b6 5841 /* See if the derived type is marked as private. */
6de9cd9a
DN
5842 if (gfc_match (" , private") == MATCH_YES)
5843 {
d51347f9 5844 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5845 {
d51347f9
TB
5846 gfc_error ("Derived type at %C can only be PRIVATE in the "
5847 "specification part of a module");
6de9cd9a
DN
5848 return MATCH_ERROR;
5849 }
5850
a8b3b0b6 5851 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6de9cd9a 5852 return MATCH_ERROR;
6de9cd9a 5853 }
a8b3b0b6 5854 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 5855 {
d51347f9 5856 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5857 {
d51347f9
TB
5858 gfc_error ("Derived type at %C can only be PUBLIC in the "
5859 "specification part of a module");
6de9cd9a
DN
5860 return MATCH_ERROR;
5861 }
5862
a8b3b0b6 5863 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6de9cd9a 5864 return MATCH_ERROR;
6de9cd9a 5865 }
e7303e85 5866 else if (gfc_match(" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
5867 {
5868 /* If the type is defined to be bind(c) it then needs to make
5869 sure that all fields are interoperable. This will
5870 need to be a semantic check on the finished derived type.
5871 See 15.2.3 (lines 9-12) of F2003 draft. */
5872 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5873 return MATCH_ERROR;
5874
5875 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5876 }
5877 else
5878 return MATCH_NO;
5879
5880 /* If we get here, something matched. */
5881 return MATCH_YES;
5882}
5883
5884
5885/* Match the beginning of a derived type declaration. If a type name
5886 was the result of a function, then it is possible to have a symbol
5887 already to be known as a derived type yet have no components. */
5888
5889match
5890gfc_match_derived_decl (void)
5891{
5892 char name[GFC_MAX_SYMBOL_LEN + 1];
5893 symbol_attribute attr;
5894 gfc_symbol *sym;
5895 match m;
5896 match is_type_attr_spec = MATCH_NO;
e7303e85 5897 bool seen_attr = false;
a8b3b0b6
CR
5898
5899 if (gfc_current_state () == COMP_DERIVED)
5900 return MATCH_NO;
5901
5902 gfc_clear_attr (&attr);
5903
5904 do
5905 {
5906 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5907 if (is_type_attr_spec == MATCH_ERROR)
5908 return MATCH_ERROR;
e7303e85
FXC
5909 if (is_type_attr_spec == MATCH_YES)
5910 seen_attr = true;
a8b3b0b6 5911 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 5912
e7303e85 5913 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6de9cd9a
DN
5914 {
5915 gfc_error ("Expected :: in TYPE definition at %C");
5916 return MATCH_ERROR;
5917 }
5918
5919 m = gfc_match (" %n%t", name);
5920 if (m != MATCH_YES)
5921 return m;
5922
e9c06563
TB
5923 /* Make sure the name is not the name of an intrinsic type. */
5924 if (gfc_is_intrinsic_typename (name))
6de9cd9a 5925 {
636dff67
SK
5926 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5927 "type", name);
6de9cd9a
DN
5928 return MATCH_ERROR;
5929 }
5930
5931 if (gfc_get_symbol (name, NULL, &sym))
5932 return MATCH_ERROR;
5933
5934 if (sym->ts.type != BT_UNKNOWN)
5935 {
5936 gfc_error ("Derived type name '%s' at %C already has a basic type "
5937 "of %s", sym->name, gfc_typename (&sym->ts));
5938 return MATCH_ERROR;
5939 }
5940
5941 /* The symbol may already have the derived attribute without the
5942 components. The ways this can happen is via a function
5943 definition, an INTRINSIC statement or a subtype in another
5944 derived type that is a pointer. The first part of the AND clause
f7b529fa 5945 is true if a the symbol is not the return value of a function. */
6de9cd9a 5946 if (sym->attr.flavor != FL_DERIVED
231b2fcc 5947 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5948 return MATCH_ERROR;
5949
9fa6b0af 5950 if (sym->components != NULL || sym->attr.zero_comp)
6de9cd9a 5951 {
636dff67
SK
5952 gfc_error ("Derived type definition of '%s' at %C has already been "
5953 "defined", sym->name);
6de9cd9a
DN
5954 return MATCH_ERROR;
5955 }
5956
5957 if (attr.access != ACCESS_UNKNOWN
231b2fcc 5958 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5959 return MATCH_ERROR;
5960
a8b3b0b6
CR
5961 /* See if the derived type was labeled as bind(c). */
5962 if (attr.is_bind_c != 0)
5963 sym->attr.is_bind_c = attr.is_bind_c;
5964
6de9cd9a
DN
5965 gfc_new_block = sym;
5966
5967 return MATCH_YES;
5968}
83d890b9
AL
5969
5970
5971/* Cray Pointees can be declared as:
5972 pointer (ipt, a (n,m,...,*))
5973 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5974 cheat and set a constant bound of 1 for the last dimension, if this
5975 is the case. Since there is no bounds-checking for Cray Pointees,
5976 this will be okay. */
5977
5978try
5979gfc_mod_pointee_as (gfc_array_spec *as)
5980{
5981 as->cray_pointee = true; /* This will be useful to know later. */
5982 if (as->type == AS_ASSUMED_SIZE)
5983 {
5984 as->type = AS_EXPLICIT;
5985 as->upper[as->rank - 1] = gfc_int_expr (1);
5986 as->cp_was_assumed = true;
5987 }
5988 else if (as->type == AS_ASSUMED_SHAPE)
5989 {
5990 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5991 return MATCH_ERROR;
5992 }
5993 return MATCH_YES;
5994}
25d8f0a2
TS
5995
5996
5997/* Match the enum definition statement, here we are trying to match
5998 the first line of enum definition statement.
5999 Returns MATCH_YES if match is found. */
6000
6001match
6002gfc_match_enum (void)
6003{
6004 match m;
6005
6006 m = gfc_match_eos ();
6007 if (m != MATCH_YES)
6008 return m;
6009
6133c68a 6010 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
25d8f0a2
TS
6011 == FAILURE)
6012 return MATCH_ERROR;
6013
6014 return MATCH_YES;
6015}
6016
6017
6133c68a
TS
6018/* Match a variable name with an optional initializer. When this
6019 subroutine is called, a variable is expected to be parsed next.
6020 Depending on what is happening at the moment, updates either the
6021 symbol table or the current interface. */
6022
6023static match
6024enumerator_decl (void)
6025{
6026 char name[GFC_MAX_SYMBOL_LEN + 1];
6027 gfc_expr *initializer;
6028 gfc_array_spec *as = NULL;
6029 gfc_symbol *sym;
6030 locus var_locus;
6031 match m;
6032 try t;
6033 locus old_locus;
6034
6035 initializer = NULL;
6036 old_locus = gfc_current_locus;
6037
6038 /* When we get here, we've just matched a list of attributes and
6039 maybe a type and a double colon. The next thing we expect to see
6040 is the name of the symbol. */
6041 m = gfc_match_name (name);
6042 if (m != MATCH_YES)
6043 goto cleanup;
6044
6045 var_locus = gfc_current_locus;
6046
6047 /* OK, we've successfully matched the declaration. Now put the
6048 symbol in the current namespace. If we fail to create the symbol,
6049 bail out. */
6050 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6051 {
6052 m = MATCH_ERROR;
6053 goto cleanup;
6054 }
6055
6056 /* The double colon must be present in order to have initializers.
6057 Otherwise the statement is ambiguous with an assignment statement. */
6058 if (colon_seen)
6059 {
6060 if (gfc_match_char ('=') == MATCH_YES)
6061 {
6062 m = gfc_match_init_expr (&initializer);
6063 if (m == MATCH_NO)
6064 {
6065 gfc_error ("Expected an initialization expression at %C");
6066 m = MATCH_ERROR;
6067 }
6068
6069 if (m != MATCH_YES)
6070 goto cleanup;
6071 }
6072 }
6073
6074 /* If we do not have an initializer, the initialization value of the
6075 previous enumerator (stored in last_initializer) is incremented
6076 by 1 and is used to initialize the current enumerator. */
6077 if (initializer == NULL)
6078 initializer = gfc_enum_initializer (last_initializer, old_locus);
d51347f9 6079
6133c68a
TS
6080 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6081 {
6082 gfc_error("ENUMERATOR %L not initialized with integer expression",
6083 &var_locus);
d51347f9 6084 m = MATCH_ERROR;
6133c68a
TS
6085 gfc_free_enum_history ();
6086 goto cleanup;
6087 }
6088
6089 /* Store this current initializer, for the next enumerator variable
6090 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6091 use last_initializer below. */
6092 last_initializer = initializer;
6093 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6094
6095 /* Maintain enumerator history. */
6096 gfc_find_symbol (name, NULL, 0, &sym);
6097 create_enum_history (sym, last_initializer);
6098
6099 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6100
6101cleanup:
6102 /* Free stuff up and return. */
6103 gfc_free_expr (initializer);
6104
6105 return m;
6106}
6107
6108
66e4ab31 6109/* Match the enumerator definition statement. */
25d8f0a2
TS
6110
6111match
6112gfc_match_enumerator_def (void)
6113{
6114 match m;
6133c68a 6115 try t;
d51347f9 6116
25d8f0a2 6117 gfc_clear_ts (&current_ts);
d51347f9 6118
25d8f0a2
TS
6119 m = gfc_match (" enumerator");
6120 if (m != MATCH_YES)
6121 return m;
6133c68a
TS
6122
6123 m = gfc_match (" :: ");
6124 if (m == MATCH_ERROR)
6125 return m;
6126
6127 colon_seen = (m == MATCH_YES);
d51347f9 6128
25d8f0a2
TS
6129 if (gfc_current_state () != COMP_ENUM)
6130 {
6131 gfc_error ("ENUM definition statement expected before %C");
6132 gfc_free_enum_history ();
6133 return MATCH_ERROR;
6134 }
6135
6136 (&current_ts)->type = BT_INTEGER;
6137 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 6138
6133c68a
TS
6139 gfc_clear_attr (&current_attr);
6140 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6141 if (t == FAILURE)
25d8f0a2 6142 {
6133c68a 6143 m = MATCH_ERROR;
25d8f0a2
TS
6144 goto cleanup;
6145 }
6146
25d8f0a2
TS
6147 for (;;)
6148 {
6133c68a 6149 m = enumerator_decl ();
25d8f0a2
TS
6150 if (m == MATCH_ERROR)
6151 goto cleanup;
6152 if (m == MATCH_NO)
6153 break;
6154
6155 if (gfc_match_eos () == MATCH_YES)
6156 goto cleanup;
6157 if (gfc_match_char (',') != MATCH_YES)
6158 break;
6159 }
6160
6161 if (gfc_current_state () == COMP_ENUM)
6162 {
6163 gfc_free_enum_history ();
6164 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6165 m = MATCH_ERROR;
6166 }
6167
6168cleanup:
6169 gfc_free_array_spec (current_as);
6170 current_as = NULL;
6171 return m;
6172
6173}
6174