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