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