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