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