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