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