]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/decl.c
* config/rs6000/rs6000.c (is_mem_ref): Ignore STACK_TIE.
[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 3655{
7389bce6 3656 bool seen_type;
6de9cd9a
DN
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;
fcd5a113 4337 unsigned int type;
68ea355b
PT
4338
4339 s = gfc_get_gsymbol(name);
7389bce6 4340 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
68ea355b
PT
4341
4342 if (s->defined
636dff67 4343 || (s->type != GSYM_UNKNOWN
7389bce6 4344 && s->type != type))
ca39e6f2 4345 gfc_global_used(s, NULL);
68ea355b
PT
4346 else
4347 {
7389bce6 4348 s->type = type;
68ea355b
PT
4349 s->where = gfc_current_locus;
4350 s->defined = 1;
4351 return true;
4352 }
4353 return false;
4354}
6de9cd9a 4355
636dff67 4356
6de9cd9a
DN
4357/* Match an ENTRY statement. */
4358
4359match
4360gfc_match_entry (void)
4361{
3d79abbd
PB
4362 gfc_symbol *proc;
4363 gfc_symbol *result;
4364 gfc_symbol *entry;
6de9cd9a
DN
4365 char name[GFC_MAX_SYMBOL_LEN + 1];
4366 gfc_compile_state state;
4367 match m;
3d79abbd 4368 gfc_entry_list *el;
c96cfa49 4369 locus old_loc;
1a492601 4370 bool module_procedure;
bc3e7a8c
TB
4371 char peek_char;
4372 match is_bind_c;
6de9cd9a
DN
4373
4374 m = gfc_match_name (name);
4375 if (m != MATCH_YES)
4376 return m;
4377
3d79abbd 4378 state = gfc_current_state ();
4c93c95a 4379 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 4380 {
4c93c95a
FXC
4381 switch (state)
4382 {
4383 case COMP_PROGRAM:
4384 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4385 break;
4386 case COMP_MODULE:
4387 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4388 break;
4389 case COMP_BLOCK_DATA:
636dff67
SK
4390 gfc_error ("ENTRY statement at %C cannot appear within "
4391 "a BLOCK DATA");
4c93c95a
FXC
4392 break;
4393 case COMP_INTERFACE:
636dff67
SK
4394 gfc_error ("ENTRY statement at %C cannot appear within "
4395 "an INTERFACE");
4c93c95a
FXC
4396 break;
4397 case COMP_DERIVED:
636dff67
SK
4398 gfc_error ("ENTRY statement at %C cannot appear within "
4399 "a DERIVED TYPE block");
4c93c95a
FXC
4400 break;
4401 case COMP_IF:
636dff67
SK
4402 gfc_error ("ENTRY statement at %C cannot appear within "
4403 "an IF-THEN block");
4c93c95a
FXC
4404 break;
4405 case COMP_DO:
636dff67
SK
4406 gfc_error ("ENTRY statement at %C cannot appear within "
4407 "a DO block");
4c93c95a
FXC
4408 break;
4409 case COMP_SELECT:
636dff67
SK
4410 gfc_error ("ENTRY statement at %C cannot appear within "
4411 "a SELECT block");
4c93c95a
FXC
4412 break;
4413 case COMP_FORALL:
636dff67
SK
4414 gfc_error ("ENTRY statement at %C cannot appear within "
4415 "a FORALL block");
4c93c95a
FXC
4416 break;
4417 case COMP_WHERE:
636dff67
SK
4418 gfc_error ("ENTRY statement at %C cannot appear within "
4419 "a WHERE block");
4c93c95a
FXC
4420 break;
4421 case COMP_CONTAINS:
636dff67
SK
4422 gfc_error ("ENTRY statement at %C cannot appear within "
4423 "a contained subprogram");
4c93c95a
FXC
4424 break;
4425 default:
4426 gfc_internal_error ("gfc_match_entry(): Bad state");
4427 }
3d79abbd
PB
4428 return MATCH_ERROR;
4429 }
4430
1a492601 4431 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
4432 && gfc_current_ns->parent->proc_name
4433 && gfc_current_ns->parent->proc_name->attr.flavor
4434 == FL_MODULE;
1a492601 4435
3d79abbd
PB
4436 if (gfc_current_ns->parent != NULL
4437 && gfc_current_ns->parent->proc_name
1a492601 4438 && !module_procedure)
3d79abbd
PB
4439 {
4440 gfc_error("ENTRY statement at %C cannot appear in a "
4441 "contained procedure");
4442 return MATCH_ERROR;
4443 }
4444
1a492601
PT
4445 /* Module function entries need special care in get_proc_name
4446 because previous references within the function will have
4447 created symbols attached to the current namespace. */
4448 if (get_proc_name (name, &entry,
4449 gfc_current_ns->parent != NULL
4450 && module_procedure
4451 && gfc_current_ns->proc_name->attr.function))
6de9cd9a
DN
4452 return MATCH_ERROR;
4453
3d79abbd
PB
4454 proc = gfc_current_block ();
4455
bc3e7a8c
TB
4456 /* Make sure that it isn't already declared as BIND(C). If it is, it
4457 must have been marked BIND(C) with a BIND(C) attribute and that is
4458 not allowed for procedures. */
4459 if (entry->attr.is_bind_c == 1)
4460 {
4461 entry->attr.is_bind_c = 0;
4462 if (entry->old_symbol != NULL)
4463 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4464 "variables or common blocks",
4465 &(entry->old_symbol->declared_at));
4466 else
4467 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4468 "variables or common blocks", &gfc_current_locus);
4469 }
4470
4471 /* Check what next non-whitespace character is so we can tell if there
4472 is the required parens if we have a BIND(C). */
4473 gfc_gobble_whitespace ();
4474 peek_char = gfc_peek_char ();
4475
3d79abbd 4476 if (state == COMP_SUBROUTINE)
6de9cd9a 4477 {
231b2fcc 4478 /* An entry in a subroutine. */
182393f4 4479 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
68ea355b
PT
4480 return MATCH_ERROR;
4481
6de9cd9a
DN
4482 m = gfc_match_formal_arglist (entry, 0, 1);
4483 if (m != MATCH_YES)
4484 return MATCH_ERROR;
4485
1eabf70a
TB
4486 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4487 never be an internal procedure. */
4488 is_bind_c = gfc_match_bind_c (entry, true);
bc3e7a8c
TB
4489 if (is_bind_c == MATCH_ERROR)
4490 return MATCH_ERROR;
4491 if (is_bind_c == MATCH_YES)
4492 {
4493 if (peek_char != '(')
4494 {
4495 gfc_error ("Missing required parentheses before BIND(C) at %C");
4496 return MATCH_ERROR;
4497 }
4498 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4499 == FAILURE)
4500 return MATCH_ERROR;
4501 }
4502
231b2fcc
TS
4503 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4504 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a 4505 return MATCH_ERROR;
3d79abbd
PB
4506 }
4507 else
4508 {
c96cfa49 4509 /* An entry in a function.
636dff67
SK
4510 We need to take special care because writing
4511 ENTRY f()
4512 as
4513 ENTRY f
4514 is allowed, whereas
4515 ENTRY f() RESULT (r)
4516 can't be written as
4517 ENTRY f RESULT (r). */
182393f4 4518 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
68ea355b
PT
4519 return MATCH_ERROR;
4520
c96cfa49
TS
4521 old_loc = gfc_current_locus;
4522 if (gfc_match_eos () == MATCH_YES)
4523 {
4524 gfc_current_locus = old_loc;
4525 /* Match the empty argument list, and add the interface to
4526 the symbol. */
4527 m = gfc_match_formal_arglist (entry, 0, 1);
4528 }
4529 else
4530 m = gfc_match_formal_arglist (entry, 0, 0);
4531
6de9cd9a
DN
4532 if (m != MATCH_YES)
4533 return MATCH_ERROR;
4534
6de9cd9a
DN
4535 result = NULL;
4536
4537 if (gfc_match_eos () == MATCH_YES)
4538 {
231b2fcc
TS
4539 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4540 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a
DN
4541 return MATCH_ERROR;
4542
d198b59a 4543 entry->result = entry;
6de9cd9a
DN
4544 }
4545 else
4546 {
bc3e7a8c 4547 m = gfc_match_suffix (entry, &result);
6de9cd9a
DN
4548 if (m == MATCH_NO)
4549 gfc_syntax_error (ST_ENTRY);
4550 if (m != MATCH_YES)
4551 return MATCH_ERROR;
4552
bc3e7a8c
TB
4553 if (result)
4554 {
4555 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4556 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4557 || gfc_add_function (&entry->attr, result->name, NULL)
4558 == FAILURE)
4559 return MATCH_ERROR;
4560 entry->result = result;
4561 }
4562 else
4563 {
4564 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4565 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4566 return MATCH_ERROR;
4567 entry->result = entry;
4568 }
6de9cd9a 4569 }
6de9cd9a
DN
4570 }
4571
4572 if (gfc_match_eos () != MATCH_YES)
4573 {
4574 gfc_syntax_error (ST_ENTRY);
4575 return MATCH_ERROR;
4576 }
4577
3d79abbd
PB
4578 entry->attr.recursive = proc->attr.recursive;
4579 entry->attr.elemental = proc->attr.elemental;
4580 entry->attr.pure = proc->attr.pure;
6de9cd9a 4581
3d79abbd
PB
4582 el = gfc_get_entry_list ();
4583 el->sym = entry;
4584 el->next = gfc_current_ns->entries;
4585 gfc_current_ns->entries = el;
4586 if (el->next)
4587 el->id = el->next->id + 1;
4588 else
4589 el->id = 1;
6de9cd9a 4590
3d79abbd
PB
4591 new_st.op = EXEC_ENTRY;
4592 new_st.ext.entry = el;
4593
4594 return MATCH_YES;
6de9cd9a
DN
4595}
4596
4597
4598/* Match a subroutine statement, including optional prefixes. */
4599
4600match
4601gfc_match_subroutine (void)
4602{
4603 char name[GFC_MAX_SYMBOL_LEN + 1];
4604 gfc_symbol *sym;
4605 match m;
a8b3b0b6
CR
4606 match is_bind_c;
4607 char peek_char;
1eabf70a 4608 bool allow_binding_name;
6de9cd9a
DN
4609
4610 if (gfc_current_state () != COMP_NONE
4611 && gfc_current_state () != COMP_INTERFACE
4612 && gfc_current_state () != COMP_CONTAINS)
4613 return MATCH_NO;
4614
4615 m = match_prefix (NULL);
4616 if (m != MATCH_YES)
4617 return m;
4618
4619 m = gfc_match ("subroutine% %n", name);
4620 if (m != MATCH_YES)
4621 return m;
4622
1a492601 4623 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4624 return MATCH_ERROR;
4625 gfc_new_block = sym;
4626
a8b3b0b6 4627 /* Check what next non-whitespace character is so we can tell if there
bc3e7a8c 4628 is the required parens if we have a BIND(C). */
a8b3b0b6
CR
4629 gfc_gobble_whitespace ();
4630 peek_char = gfc_peek_char ();
4631
231b2fcc 4632 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4633 return MATCH_ERROR;
4634
4635 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4636 return MATCH_ERROR;
4637
a8b3b0b6
CR
4638 /* Make sure that it isn't already declared as BIND(C). If it is, it
4639 must have been marked BIND(C) with a BIND(C) attribute and that is
4640 not allowed for procedures. */
4641 if (sym->attr.is_bind_c == 1)
4642 {
4643 sym->attr.is_bind_c = 0;
4644 if (sym->old_symbol != NULL)
4645 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4646 "variables or common blocks",
4647 &(sym->old_symbol->declared_at));
4648 else
4649 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4650 "variables or common blocks", &gfc_current_locus);
4651 }
1eabf70a
TB
4652
4653 /* C binding names are not allowed for internal procedures. */
4654 if (gfc_current_state () == COMP_CONTAINS
4655 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4656 allow_binding_name = false;
4657 else
4658 allow_binding_name = true;
4659
a8b3b0b6
CR
4660 /* Here, we are just checking if it has the bind(c) attribute, and if
4661 so, then we need to make sure it's all correct. If it doesn't,
4662 we still need to continue matching the rest of the subroutine line. */
1eabf70a 4663 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
a8b3b0b6
CR
4664 if (is_bind_c == MATCH_ERROR)
4665 {
4666 /* There was an attempt at the bind(c), but it was wrong. An
4667 error message should have been printed w/in the gfc_match_bind_c
4668 so here we'll just return the MATCH_ERROR. */
4669 return MATCH_ERROR;
4670 }
4671
4672 if (is_bind_c == MATCH_YES)
4673 {
1eabf70a 4674 /* The following is allowed in the Fortran 2008 draft. */
01f4fff1 4675 if (gfc_current_state () == COMP_CONTAINS
1eabf70a
TB
4676 && sym->ns->proc_name->attr.flavor != FL_MODULE
4677 && gfc_notify_std (GFC_STD_GNU, "Extension: BIND(C) attribute at "
4678 "%L may not be specified for an internal procedure",
4679 &gfc_current_locus)
4680 == FAILURE)
4681 return MATCH_ERROR;
4682
a8b3b0b6
CR
4683 if (peek_char != '(')
4684 {
4685 gfc_error ("Missing required parentheses before BIND(C) at %C");
4686 return MATCH_ERROR;
4687 }
4688 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4689 == FAILURE)
4690 return MATCH_ERROR;
4691 }
4692
6de9cd9a
DN
4693 if (gfc_match_eos () != MATCH_YES)
4694 {
4695 gfc_syntax_error (ST_SUBROUTINE);
4696 return MATCH_ERROR;
4697 }
4698
4699 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4700 return MATCH_ERROR;
4701
4702 return MATCH_YES;
4703}
4704
4705
a8b3b0b6
CR
4706/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4707 given, and set the binding label in either the given symbol (if not
86bf520d 4708 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
4709 encounter the BIND(C) before the declaration itself. Return
4710 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4711 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4712 or MATCH_YES if the specifier was correct and the binding label and
4713 bind(c) fields were set correctly for the given symbol or the
1eabf70a
TB
4714 current_ts. If allow_binding_name is false, no binding name may be
4715 given. */
a8b3b0b6
CR
4716
4717match
1eabf70a 4718gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
a8b3b0b6
CR
4719{
4720 /* binding label, if exists */
4721 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4722 match double_quote;
4723 match single_quote;
a8b3b0b6
CR
4724
4725 /* Initialize the flag that specifies whether we encountered a NAME=
4726 specifier or not. */
4727 has_name_equals = 0;
4728
4729 /* Init the first char to nil so we can catch if we don't have
4730 the label (name attr) or the symbol name yet. */
4731 binding_label[0] = '\0';
4732
4733 /* This much we have to be able to match, in this order, if
4734 there is a bind(c) label. */
4735 if (gfc_match (" bind ( c ") != MATCH_YES)
4736 return MATCH_NO;
4737
4738 /* Now see if there is a binding label, or if we've reached the
4739 end of the bind(c) attribute without one. */
4740 if (gfc_match_char (',') == MATCH_YES)
4741 {
4742 if (gfc_match (" name = ") != MATCH_YES)
4743 {
4744 gfc_error ("Syntax error in NAME= specifier for binding label "
4745 "at %C");
4746 /* should give an error message here */
4747 return MATCH_ERROR;
4748 }
4749
4750 has_name_equals = 1;
4751
4752 /* Get the opening quote. */
4753 double_quote = MATCH_YES;
4754 single_quote = MATCH_YES;
4755 double_quote = gfc_match_char ('"');
4756 if (double_quote != MATCH_YES)
4757 single_quote = gfc_match_char ('\'');
4758 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4759 {
4760 gfc_error ("Syntax error in NAME= specifier for binding label "
4761 "at %C");
4762 return MATCH_ERROR;
4763 }
4764
4765 /* Grab the binding label, using functions that will not lower
4766 case the names automatically. */
4767 if (gfc_match_name_C (binding_label) != MATCH_YES)
4768 return MATCH_ERROR;
4769
4770 /* Get the closing quotation. */
4771 if (double_quote == MATCH_YES)
4772 {
4773 if (gfc_match_char ('"') != MATCH_YES)
4774 {
4775 gfc_error ("Missing closing quote '\"' for binding label at %C");
4776 /* User started string with '"' so looked to match it. */
4777 return MATCH_ERROR;
4778 }
4779 }
4780 else
4781 {
4782 if (gfc_match_char ('\'') != MATCH_YES)
4783 {
4784 gfc_error ("Missing closing quote '\'' for binding label at %C");
4785 /* User started string with "'" char. */
4786 return MATCH_ERROR;
4787 }
4788 }
4789 }
4790
4791 /* Get the required right paren. */
4792 if (gfc_match_char (')') != MATCH_YES)
4793 {
4794 gfc_error ("Missing closing paren for binding label at %C");
4795 return MATCH_ERROR;
4796 }
4797
1eabf70a
TB
4798 if (has_name_equals && !allow_binding_name)
4799 {
4800 gfc_error ("No binding name is allowed in BIND(C) at %C");
4801 return MATCH_ERROR;
4802 }
4803
4804 if (has_name_equals && sym != NULL && sym->attr.dummy)
4805 {
4806 gfc_error ("For dummy procedure %s, no binding name is "
4807 "allowed in BIND(C) at %C", sym->name);
4808 return MATCH_ERROR;
4809 }
4810
4811
a8b3b0b6
CR
4812 /* Save the binding label to the symbol. If sym is null, we're
4813 probably matching the typespec attributes of a declaration and
4814 haven't gotten the name yet, and therefore, no symbol yet. */
4815 if (binding_label[0] != '\0')
4816 {
4817 if (sym != NULL)
4818 {
c5b5a17a 4819 strcpy (sym->binding_label, binding_label);
a8b3b0b6
CR
4820 }
4821 else
c5b5a17a 4822 strcpy (curr_binding_label, binding_label);
a8b3b0b6 4823 }
1eabf70a 4824 else if (allow_binding_name)
a8b3b0b6
CR
4825 {
4826 /* No binding label, but if symbol isn't null, we
1eabf70a
TB
4827 can set the label for it here.
4828 If name="" or allow_binding_name is false, no C binding name is
4829 created. */
a8b3b0b6
CR
4830 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4831 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4832 }
9e1d712c 4833
129d15a3
JW
4834 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4835 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
4836 {
4837 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4838 return MATCH_ERROR;
4839 }
4840
a8b3b0b6
CR
4841 return MATCH_YES;
4842}
4843
4844
1f2959f0 4845/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
4846
4847static int
4848contained_procedure (void)
4849{
4850 gfc_state_data *s;
4851
4852 for (s=gfc_state_stack; s; s=s->previous)
4853 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
636dff67 4854 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
ddc9ce91
TS
4855 return 1;
4856
4857 return 0;
4858}
4859
d51347f9 4860/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
4861 interoperable with the corresponding C enumeration type, making
4862 sure that -fshort-enums is honored. */
4863
4864static void
4865set_enum_kind(void)
4866{
4867 enumerator_history *current_history = NULL;
4868 int kind;
4869 int i;
4870
4871 if (max_enum == NULL || enum_history == NULL)
4872 return;
4873
4874 if (!gfc_option.fshort_enums)
d51347f9
TB
4875 return;
4876
25d8f0a2
TS
4877 i = 0;
4878 do
4879 {
4880 kind = gfc_integer_kinds[i++].kind;
4881 }
d51347f9 4882 while (kind < gfc_c_int_kind
25d8f0a2
TS
4883 && gfc_check_integer_range (max_enum->initializer->value.integer,
4884 kind) != ARITH_OK);
4885
4886 current_history = enum_history;
4887 while (current_history != NULL)
4888 {
4889 current_history->sym->ts.kind = kind;
4890 current_history = current_history->next;
4891 }
4892}
4893
636dff67 4894
6de9cd9a
DN
4895/* Match any of the various end-block statements. Returns the type of
4896 END to the caller. The END INTERFACE, END IF, END DO and END
4897 SELECT statements cannot be replaced by a single END statement. */
4898
4899match
636dff67 4900gfc_match_end (gfc_statement *st)
6de9cd9a
DN
4901{
4902 char name[GFC_MAX_SYMBOL_LEN + 1];
4903 gfc_compile_state state;
4904 locus old_loc;
4905 const char *block_name;
4906 const char *target;
ddc9ce91 4907 int eos_ok;
6de9cd9a
DN
4908 match m;
4909
63645982 4910 old_loc = gfc_current_locus;
6de9cd9a
DN
4911 if (gfc_match ("end") != MATCH_YES)
4912 return MATCH_NO;
4913
4914 state = gfc_current_state ();
636dff67
SK
4915 block_name = gfc_current_block () == NULL
4916 ? NULL : gfc_current_block ()->name;
6de9cd9a
DN
4917
4918 if (state == COMP_CONTAINS)
4919 {
4920 state = gfc_state_stack->previous->state;
636dff67
SK
4921 block_name = gfc_state_stack->previous->sym == NULL
4922 ? NULL : gfc_state_stack->previous->sym->name;
6de9cd9a
DN
4923 }
4924
4925 switch (state)
4926 {
4927 case COMP_NONE:
4928 case COMP_PROGRAM:
4929 *st = ST_END_PROGRAM;
4930 target = " program";
ddc9ce91 4931 eos_ok = 1;
6de9cd9a
DN
4932 break;
4933
4934 case COMP_SUBROUTINE:
4935 *st = ST_END_SUBROUTINE;
4936 target = " subroutine";
ddc9ce91 4937 eos_ok = !contained_procedure ();
6de9cd9a
DN
4938 break;
4939
4940 case COMP_FUNCTION:
4941 *st = ST_END_FUNCTION;
4942 target = " function";
ddc9ce91 4943 eos_ok = !contained_procedure ();
6de9cd9a
DN
4944 break;
4945
4946 case COMP_BLOCK_DATA:
4947 *st = ST_END_BLOCK_DATA;
4948 target = " block data";
ddc9ce91 4949 eos_ok = 1;
6de9cd9a
DN
4950 break;
4951
4952 case COMP_MODULE:
4953 *st = ST_END_MODULE;
4954 target = " module";
ddc9ce91 4955 eos_ok = 1;
6de9cd9a
DN
4956 break;
4957
4958 case COMP_INTERFACE:
4959 *st = ST_END_INTERFACE;
4960 target = " interface";
ddc9ce91 4961 eos_ok = 0;
6de9cd9a
DN
4962 break;
4963
4964 case COMP_DERIVED:
4965 *st = ST_END_TYPE;
4966 target = " type";
ddc9ce91 4967 eos_ok = 0;
6de9cd9a
DN
4968 break;
4969
4970 case COMP_IF:
4971 *st = ST_ENDIF;
4972 target = " if";
ddc9ce91 4973 eos_ok = 0;
6de9cd9a
DN
4974 break;
4975
4976 case COMP_DO:
4977 *st = ST_ENDDO;
4978 target = " do";
ddc9ce91 4979 eos_ok = 0;
6de9cd9a
DN
4980 break;
4981
4982 case COMP_SELECT:
4983 *st = ST_END_SELECT;
4984 target = " select";
ddc9ce91 4985 eos_ok = 0;
6de9cd9a
DN
4986 break;
4987
4988 case COMP_FORALL:
4989 *st = ST_END_FORALL;
4990 target = " forall";
ddc9ce91 4991 eos_ok = 0;
6de9cd9a
DN
4992 break;
4993
4994 case COMP_WHERE:
4995 *st = ST_END_WHERE;
4996 target = " where";
ddc9ce91 4997 eos_ok = 0;
6de9cd9a
DN
4998 break;
4999
25d8f0a2
TS
5000 case COMP_ENUM:
5001 *st = ST_END_ENUM;
5002 target = " enum";
5003 eos_ok = 0;
5004 last_initializer = NULL;
5005 set_enum_kind ();
5006 gfc_free_enum_history ();
5007 break;
5008
6de9cd9a
DN
5009 default:
5010 gfc_error ("Unexpected END statement at %C");
5011 goto cleanup;
5012 }
5013
5014 if (gfc_match_eos () == MATCH_YES)
5015 {
ddc9ce91 5016 if (!eos_ok)
6de9cd9a 5017 {
66e4ab31 5018 /* We would have required END [something]. */
59ce85b5
TS
5019 gfc_error ("%s statement expected at %L",
5020 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
5021 goto cleanup;
5022 }
5023
5024 return MATCH_YES;
5025 }
5026
5027 /* Verify that we've got the sort of end-block that we're expecting. */
5028 if (gfc_match (target) != MATCH_YES)
5029 {
5030 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5031 goto cleanup;
5032 }
5033
5034 /* If we're at the end, make sure a block name wasn't required. */
5035 if (gfc_match_eos () == MATCH_YES)
5036 {
5037
690af379
TS
5038 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5039 && *st != ST_END_FORALL && *st != ST_END_WHERE)
6de9cd9a
DN
5040 return MATCH_YES;
5041
5042 if (gfc_current_block () == NULL)
5043 return MATCH_YES;
5044
5045 gfc_error ("Expected block name of '%s' in %s statement at %C",
5046 block_name, gfc_ascii_statement (*st));
5047
5048 return MATCH_ERROR;
5049 }
5050
5051 /* END INTERFACE has a special handler for its several possible endings. */
5052 if (*st == ST_END_INTERFACE)
5053 return gfc_match_end_interface ();
5054
66e4ab31
SK
5055 /* We haven't hit the end of statement, so what is left must be an
5056 end-name. */
6de9cd9a
DN
5057 m = gfc_match_space ();
5058 if (m == MATCH_YES)
5059 m = gfc_match_name (name);
5060
5061 if (m == MATCH_NO)
5062 gfc_error ("Expected terminating name at %C");
5063 if (m != MATCH_YES)
5064 goto cleanup;
5065
5066 if (block_name == NULL)
5067 goto syntax;
5068
5069 if (strcmp (name, block_name) != 0)
5070 {
5071 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5072 gfc_ascii_statement (*st));
5073 goto cleanup;
5074 }
5075
5076 if (gfc_match_eos () == MATCH_YES)
5077 return MATCH_YES;
5078
5079syntax:
5080 gfc_syntax_error (*st);
5081
5082cleanup:
63645982 5083 gfc_current_locus = old_loc;
6de9cd9a
DN
5084 return MATCH_ERROR;
5085}
5086
5087
5088
5089/***************** Attribute declaration statements ****************/
5090
5091/* Set the attribute of a single variable. */
5092
5093static match
5094attr_decl1 (void)
5095{
5096 char name[GFC_MAX_SYMBOL_LEN + 1];
5097 gfc_array_spec *as;
5098 gfc_symbol *sym;
5099 locus var_locus;
5100 match m;
5101
5102 as = NULL;
5103
5104 m = gfc_match_name (name);
5105 if (m != MATCH_YES)
5106 goto cleanup;
5107
5108 if (find_special (name, &sym))
5109 return MATCH_ERROR;
5110
63645982 5111 var_locus = gfc_current_locus;
6de9cd9a
DN
5112
5113 /* Deal with possible array specification for certain attributes. */
5114 if (current_attr.dimension
5115 || current_attr.allocatable
5116 || current_attr.pointer
5117 || current_attr.target)
5118 {
5119 m = gfc_match_array_spec (&as);
5120 if (m == MATCH_ERROR)
5121 goto cleanup;
5122
5123 if (current_attr.dimension && m == MATCH_NO)
5124 {
636dff67
SK
5125 gfc_error ("Missing array specification at %L in DIMENSION "
5126 "statement", &var_locus);
6de9cd9a
DN
5127 m = MATCH_ERROR;
5128 goto cleanup;
5129 }
5130
5131 if ((current_attr.allocatable || current_attr.pointer)
5132 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5133 {
636dff67 5134 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
5135 m = MATCH_ERROR;
5136 goto cleanup;
5137 }
5138 }
5139
636dff67
SK
5140 /* Update symbol table. DIMENSION attribute is set
5141 in gfc_set_array_spec(). */
6de9cd9a
DN
5142 if (current_attr.dimension == 0
5143 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
5144 {
5145 m = MATCH_ERROR;
5146 goto cleanup;
5147 }
5148
5149 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5150 {
5151 m = MATCH_ERROR;
5152 goto cleanup;
5153 }
d51347f9 5154
83d890b9
AL
5155 if (sym->attr.cray_pointee && sym->as != NULL)
5156 {
5157 /* Fix the array spec. */
5158 m = gfc_mod_pointee_as (sym->as);
5159 if (m == MATCH_ERROR)
5160 goto cleanup;
5161 }
6de9cd9a 5162
7114edca 5163 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
1902704e
PT
5164 {
5165 m = MATCH_ERROR;
5166 goto cleanup;
5167 }
5168
6de9cd9a
DN
5169 if ((current_attr.external || current_attr.intrinsic)
5170 && sym->attr.flavor != FL_PROCEDURE
231b2fcc 5171 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5172 {
5173 m = MATCH_ERROR;
5174 goto cleanup;
5175 }
5176
5177 return MATCH_YES;
5178
5179cleanup:
5180 gfc_free_array_spec (as);
5181 return m;
5182}
5183
5184
5185/* Generic attribute declaration subroutine. Used for attributes that
5186 just have a list of names. */
5187
5188static match
5189attr_decl (void)
5190{
5191 match m;
5192
5193 /* Gobble the optional double colon, by simply ignoring the result
5194 of gfc_match(). */
5195 gfc_match (" ::");
5196
5197 for (;;)
5198 {
5199 m = attr_decl1 ();
5200 if (m != MATCH_YES)
5201 break;
5202
5203 if (gfc_match_eos () == MATCH_YES)
5204 {
5205 m = MATCH_YES;
5206 break;
5207 }
5208
5209 if (gfc_match_char (',') != MATCH_YES)
5210 {
5211 gfc_error ("Unexpected character in variable list at %C");
5212 m = MATCH_ERROR;
5213 break;
5214 }
5215 }
5216
5217 return m;
5218}
5219
5220
83d890b9
AL
5221/* This routine matches Cray Pointer declarations of the form:
5222 pointer ( <pointer>, <pointee> )
5223 or
d51347f9
TB
5224 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5225 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
5226 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5227 be either a scalar, or an array declaration. No space is allocated for
d51347f9 5228 the pointee. For the statement
83d890b9
AL
5229 pointer (ipt, ar(10))
5230 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 5231 ar(i) => ((<type> *) ipt)(i)
b122dc6a 5232 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
5233
5234static match
5235cray_pointer_decl (void)
5236{
5237 match m;
5238 gfc_array_spec *as;
5239 gfc_symbol *cptr; /* Pointer symbol. */
5240 gfc_symbol *cpte; /* Pointee symbol. */
5241 locus var_locus;
5242 bool done = false;
5243
5244 while (!done)
5245 {
5246 if (gfc_match_char ('(') != MATCH_YES)
5247 {
5248 gfc_error ("Expected '(' at %C");
d51347f9 5249 return MATCH_ERROR;
83d890b9 5250 }
d51347f9 5251
83d890b9
AL
5252 /* Match pointer. */
5253 var_locus = gfc_current_locus;
5254 gfc_clear_attr (&current_attr);
5255 gfc_add_cray_pointer (&current_attr, &var_locus);
5256 current_ts.type = BT_INTEGER;
5257 current_ts.kind = gfc_index_integer_kind;
5258
d51347f9 5259 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
5260 if (m != MATCH_YES)
5261 {
5262 gfc_error ("Expected variable name at %C");
5263 return m;
5264 }
d51347f9 5265
83d890b9
AL
5266 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5267 return MATCH_ERROR;
5268
d51347f9 5269 gfc_set_sym_referenced (cptr);
83d890b9
AL
5270
5271 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5272 {
5273 cptr->ts.type = BT_INTEGER;
d51347f9 5274 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
5275 }
5276 else if (cptr->ts.type != BT_INTEGER)
5277 {
e25a0da3 5278 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
5279 return MATCH_ERROR;
5280 }
5281 else if (cptr->ts.kind < gfc_index_integer_kind)
5282 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 5283 " memory addresses require %d bytes",
636dff67 5284 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
5285
5286 if (gfc_match_char (',') != MATCH_YES)
5287 {
5288 gfc_error ("Expected \",\" at %C");
d51347f9 5289 return MATCH_ERROR;
83d890b9
AL
5290 }
5291
d51347f9 5292 /* Match Pointee. */
83d890b9
AL
5293 var_locus = gfc_current_locus;
5294 gfc_clear_attr (&current_attr);
5295 gfc_add_cray_pointee (&current_attr, &var_locus);
5296 current_ts.type = BT_UNKNOWN;
5297 current_ts.kind = 0;
5298
5299 m = gfc_match_symbol (&cpte, 0);
5300 if (m != MATCH_YES)
5301 {
5302 gfc_error ("Expected variable name at %C");
5303 return m;
5304 }
d51347f9 5305
83d890b9
AL
5306 /* Check for an optional array spec. */
5307 m = gfc_match_array_spec (&as);
5308 if (m == MATCH_ERROR)
5309 {
5310 gfc_free_array_spec (as);
5311 return m;
5312 }
5313 else if (m == MATCH_NO)
5314 {
5315 gfc_free_array_spec (as);
5316 as = NULL;
5317 }
5318
5319 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5320 return MATCH_ERROR;
5321
5322 gfc_set_sym_referenced (cpte);
5323
5324 if (cpte->as == NULL)
5325 {
5326 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5327 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5328 }
5329 else if (as != NULL)
5330 {
e25a0da3 5331 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
5332 gfc_free_array_spec (as);
5333 return MATCH_ERROR;
5334 }
5335
5336 as = NULL;
5337
5338 if (cpte->as != NULL)
5339 {
5340 /* Fix array spec. */
5341 m = gfc_mod_pointee_as (cpte->as);
5342 if (m == MATCH_ERROR)
5343 return m;
5344 }
5345
5346 /* Point the Pointee at the Pointer. */
b122dc6a 5347 cpte->cp_pointer = cptr;
83d890b9
AL
5348
5349 if (gfc_match_char (')') != MATCH_YES)
5350 {
5351 gfc_error ("Expected \")\" at %C");
5352 return MATCH_ERROR;
5353 }
5354 m = gfc_match_char (',');
5355 if (m != MATCH_YES)
5356 done = true; /* Stop searching for more declarations. */
5357
5358 }
5359
5360 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5361 || gfc_match_eos () != MATCH_YES)
5362 {
5363 gfc_error ("Expected \",\" or end of statement at %C");
5364 return MATCH_ERROR;
5365 }
5366 return MATCH_YES;
5367}
5368
5369
6de9cd9a
DN
5370match
5371gfc_match_external (void)
5372{
5373
5374 gfc_clear_attr (&current_attr);
1902704e 5375 current_attr.external = 1;
6de9cd9a
DN
5376
5377 return attr_decl ();
5378}
5379
5380
6de9cd9a
DN
5381match
5382gfc_match_intent (void)
5383{
5384 sym_intent intent;
5385
5386 intent = match_intent_spec ();
5387 if (intent == INTENT_UNKNOWN)
5388 return MATCH_ERROR;
5389
5390 gfc_clear_attr (&current_attr);
1902704e 5391 current_attr.intent = intent;
6de9cd9a
DN
5392
5393 return attr_decl ();
5394}
5395
5396
5397match
5398gfc_match_intrinsic (void)
5399{
5400
5401 gfc_clear_attr (&current_attr);
1902704e 5402 current_attr.intrinsic = 1;
6de9cd9a
DN
5403
5404 return attr_decl ();
5405}
5406
5407
5408match
5409gfc_match_optional (void)
5410{
5411
5412 gfc_clear_attr (&current_attr);
1902704e 5413 current_attr.optional = 1;
6de9cd9a
DN
5414
5415 return attr_decl ();
5416}
5417
5418
5419match
5420gfc_match_pointer (void)
5421{
83d890b9
AL
5422 gfc_gobble_whitespace ();
5423 if (gfc_peek_char () == '(')
5424 {
5425 if (!gfc_option.flag_cray_pointer)
5426 {
636dff67
SK
5427 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5428 "flag");
83d890b9
AL
5429 return MATCH_ERROR;
5430 }
5431 return cray_pointer_decl ();
5432 }
5433 else
5434 {
5435 gfc_clear_attr (&current_attr);
1902704e 5436 current_attr.pointer = 1;
83d890b9
AL
5437
5438 return attr_decl ();
5439 }
6de9cd9a
DN
5440}
5441
5442
5443match
5444gfc_match_allocatable (void)
5445{
6de9cd9a 5446 gfc_clear_attr (&current_attr);
1902704e 5447 current_attr.allocatable = 1;
6de9cd9a
DN
5448
5449 return attr_decl ();
5450}
5451
5452
5453match
5454gfc_match_dimension (void)
5455{
6de9cd9a 5456 gfc_clear_attr (&current_attr);
1902704e 5457 current_attr.dimension = 1;
6de9cd9a
DN
5458
5459 return attr_decl ();
5460}
5461
5462
5463match
5464gfc_match_target (void)
5465{
6de9cd9a 5466 gfc_clear_attr (&current_attr);
1902704e 5467 current_attr.target = 1;
6de9cd9a
DN
5468
5469 return attr_decl ();
5470}
5471
5472
5473/* Match the list of entities being specified in a PUBLIC or PRIVATE
5474 statement. */
5475
5476static match
5477access_attr_decl (gfc_statement st)
5478{
5479 char name[GFC_MAX_SYMBOL_LEN + 1];
5480 interface_type type;
5481 gfc_user_op *uop;
5482 gfc_symbol *sym;
5483 gfc_intrinsic_op operator;
5484 match m;
5485
5486 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5487 goto done;
5488
5489 for (;;)
5490 {
5491 m = gfc_match_generic_spec (&type, name, &operator);
5492 if (m == MATCH_NO)
5493 goto syntax;
5494 if (m == MATCH_ERROR)
5495 return MATCH_ERROR;
5496
5497 switch (type)
5498 {
5499 case INTERFACE_NAMELESS:
9e1d712c 5500 case INTERFACE_ABSTRACT:
6de9cd9a
DN
5501 goto syntax;
5502
5503 case INTERFACE_GENERIC:
5504 if (gfc_get_symbol (name, NULL, &sym))
5505 goto done;
5506
636dff67
SK
5507 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5508 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
231b2fcc 5509 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5510 return MATCH_ERROR;
5511
5512 break;
5513
5514 case INTERFACE_INTRINSIC_OP:
5515 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5516 {
5517 gfc_current_ns->operator_access[operator] =
5518 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5519 }
5520 else
5521 {
5522 gfc_error ("Access specification of the %s operator at %C has "
5523 "already been specified", gfc_op2string (operator));
5524 goto done;
5525 }
5526
5527 break;
5528
5529 case INTERFACE_USER_OP:
5530 uop = gfc_get_uop (name);
5531
5532 if (uop->access == ACCESS_UNKNOWN)
5533 {
636dff67
SK
5534 uop->access = (st == ST_PUBLIC)
5535 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
5536 }
5537 else
5538 {
636dff67
SK
5539 gfc_error ("Access specification of the .%s. operator at %C "
5540 "has already been specified", sym->name);
6de9cd9a
DN
5541 goto done;
5542 }
5543
5544 break;
5545 }
5546
5547 if (gfc_match_char (',') == MATCH_NO)
5548 break;
5549 }
5550
5551 if (gfc_match_eos () != MATCH_YES)
5552 goto syntax;
5553 return MATCH_YES;
5554
5555syntax:
5556 gfc_syntax_error (st);
5557
5558done:
5559 return MATCH_ERROR;
5560}
5561
5562
ee7e677f
TB
5563match
5564gfc_match_protected (void)
5565{
5566 gfc_symbol *sym;
5567 match m;
5568
5569 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5570 {
5571 gfc_error ("PROTECTED at %C only allowed in specification "
5572 "part of a module");
5573 return MATCH_ERROR;
5574
5575 }
5576
636dff67 5577 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
ee7e677f
TB
5578 == FAILURE)
5579 return MATCH_ERROR;
5580
5581 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5582 {
5583 return MATCH_ERROR;
5584 }
5585
5586 if (gfc_match_eos () == MATCH_YES)
5587 goto syntax;
5588
5589 for(;;)
5590 {
5591 m = gfc_match_symbol (&sym, 0);
5592 switch (m)
5593 {
5594 case MATCH_YES:
636dff67
SK
5595 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5596 == FAILURE)
ee7e677f
TB
5597 return MATCH_ERROR;
5598 goto next_item;
5599
5600 case MATCH_NO:
5601 break;
5602
5603 case MATCH_ERROR:
5604 return MATCH_ERROR;
5605 }
5606
5607 next_item:
5608 if (gfc_match_eos () == MATCH_YES)
5609 break;
5610 if (gfc_match_char (',') != MATCH_YES)
5611 goto syntax;
5612 }
5613
5614 return MATCH_YES;
5615
5616syntax:
5617 gfc_error ("Syntax error in PROTECTED statement at %C");
5618 return MATCH_ERROR;
5619}
5620
5621
86bf520d 5622/* The PRIVATE statement is a bit weird in that it can be an attribute
6de9cd9a
DN
5623 declaration, but also works as a standlone statement inside of a
5624 type declaration or a module. */
5625
5626match
636dff67 5627gfc_match_private (gfc_statement *st)
6de9cd9a
DN
5628{
5629
5630 if (gfc_match ("private") != MATCH_YES)
5631 return MATCH_NO;
5632
d51347f9
TB
5633 if (gfc_current_state () != COMP_MODULE
5634 && (gfc_current_state () != COMP_DERIVED
5635 || !gfc_state_stack->previous
5636 || gfc_state_stack->previous->state != COMP_MODULE))
5637 {
5638 gfc_error ("PRIVATE statement at %C is only allowed in the "
5639 "specification part of a module");
5640 return MATCH_ERROR;
5641 }
5642
6de9cd9a
DN
5643 if (gfc_current_state () == COMP_DERIVED)
5644 {
5645 if (gfc_match_eos () == MATCH_YES)
5646 {
5647 *st = ST_PRIVATE;
5648 return MATCH_YES;
5649 }
5650
5651 gfc_syntax_error (ST_PRIVATE);
5652 return MATCH_ERROR;
5653 }
5654
5655 if (gfc_match_eos () == MATCH_YES)
5656 {
5657 *st = ST_PRIVATE;
5658 return MATCH_YES;
5659 }
5660
5661 *st = ST_ATTR_DECL;
5662 return access_attr_decl (ST_PRIVATE);
5663}
5664
5665
5666match
636dff67 5667gfc_match_public (gfc_statement *st)
6de9cd9a
DN
5668{
5669
5670 if (gfc_match ("public") != MATCH_YES)
5671 return MATCH_NO;
5672
d51347f9
TB
5673 if (gfc_current_state () != COMP_MODULE)
5674 {
5675 gfc_error ("PUBLIC statement at %C is only allowed in the "
5676 "specification part of a module");
5677 return MATCH_ERROR;
5678 }
5679
6de9cd9a
DN
5680 if (gfc_match_eos () == MATCH_YES)
5681 {
5682 *st = ST_PUBLIC;
5683 return MATCH_YES;
5684 }
5685
5686 *st = ST_ATTR_DECL;
5687 return access_attr_decl (ST_PUBLIC);
5688}
5689
5690
5691/* Workhorse for gfc_match_parameter. */
5692
5693static match
5694do_parm (void)
5695{
5696 gfc_symbol *sym;
5697 gfc_expr *init;
5698 match m;
5699
5700 m = gfc_match_symbol (&sym, 0);
5701 if (m == MATCH_NO)
5702 gfc_error ("Expected variable name at %C in PARAMETER statement");
5703
5704 if (m != MATCH_YES)
5705 return m;
5706
5707 if (gfc_match_char ('=') == MATCH_NO)
5708 {
5709 gfc_error ("Expected = sign in PARAMETER statement at %C");
5710 return MATCH_ERROR;
5711 }
5712
5713 m = gfc_match_init_expr (&init);
5714 if (m == MATCH_NO)
5715 gfc_error ("Expected expression at %C in PARAMETER statement");
5716 if (m != MATCH_YES)
5717 return m;
5718
5719 if (sym->ts.type == BT_UNKNOWN
5720 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5721 {
5722 m = MATCH_ERROR;
5723 goto cleanup;
5724 }
5725
5726 if (gfc_check_assign_symbol (sym, init) == FAILURE
231b2fcc 5727 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5728 {
5729 m = MATCH_ERROR;
5730 goto cleanup;
5731 }
5732
7e2eba4b
DE
5733 if (sym->ts.type == BT_CHARACTER
5734 && sym->ts.cl != NULL
5735 && sym->ts.cl->length != NULL
5736 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5737 && init->expr_type == EXPR_CONSTANT
5738 && init->ts.type == BT_CHARACTER
5739 && init->ts.kind == 1)
5740 gfc_set_constant_character_len (
2220652d 5741 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
7e2eba4b 5742
6de9cd9a
DN
5743 sym->value = init;
5744 return MATCH_YES;
5745
5746cleanup:
5747 gfc_free_expr (init);
5748 return m;
5749}
5750
5751
5752/* Match a parameter statement, with the weird syntax that these have. */
5753
5754match
5755gfc_match_parameter (void)
5756{
5757 match m;
5758
5759 if (gfc_match_char ('(') == MATCH_NO)
5760 return MATCH_NO;
5761
5762 for (;;)
5763 {
5764 m = do_parm ();
5765 if (m != MATCH_YES)
5766 break;
5767
5768 if (gfc_match (" )%t") == MATCH_YES)
5769 break;
5770
5771 if (gfc_match_char (',') != MATCH_YES)
5772 {
5773 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5774 m = MATCH_ERROR;
5775 break;
5776 }
5777 }
5778
5779 return m;
5780}
5781
5782
5783/* Save statements have a special syntax. */
5784
5785match
5786gfc_match_save (void)
5787{
9056bd70
TS
5788 char n[GFC_MAX_SYMBOL_LEN+1];
5789 gfc_common_head *c;
6de9cd9a
DN
5790 gfc_symbol *sym;
5791 match m;
5792
5793 if (gfc_match_eos () == MATCH_YES)
5794 {
5795 if (gfc_current_ns->seen_save)
5796 {
636dff67
SK
5797 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5798 "follows previous SAVE statement")
09e87839
AL
5799 == FAILURE)
5800 return MATCH_ERROR;
6de9cd9a
DN
5801 }
5802
5803 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5804 return MATCH_YES;
5805 }
5806
5807 if (gfc_current_ns->save_all)
5808 {
636dff67
SK
5809 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5810 "blanket SAVE statement")
09e87839
AL
5811 == FAILURE)
5812 return MATCH_ERROR;
6de9cd9a
DN
5813 }
5814
5815 gfc_match (" ::");
5816
5817 for (;;)
5818 {
5819 m = gfc_match_symbol (&sym, 0);
5820 switch (m)
5821 {
5822 case MATCH_YES:
636dff67
SK
5823 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5824 == FAILURE)
6de9cd9a
DN
5825 return MATCH_ERROR;
5826 goto next_item;
5827
5828 case MATCH_NO:
5829 break;
5830
5831 case MATCH_ERROR:
5832 return MATCH_ERROR;
5833 }
5834
9056bd70 5835 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
5836 if (m == MATCH_ERROR)
5837 return MATCH_ERROR;
5838 if (m == MATCH_NO)
5839 goto syntax;
5840
53814b8f 5841 c = gfc_get_common (n, 0);
9056bd70
TS
5842 c->saved = 1;
5843
6de9cd9a
DN
5844 gfc_current_ns->seen_save = 1;
5845
5846 next_item:
5847 if (gfc_match_eos () == MATCH_YES)
5848 break;
5849 if (gfc_match_char (',') != MATCH_YES)
5850 goto syntax;
5851 }
5852
5853 return MATCH_YES;
5854
5855syntax:
5856 gfc_error ("Syntax error in SAVE statement at %C");
5857 return MATCH_ERROR;
5858}
5859
5860
06469efd
PT
5861match
5862gfc_match_value (void)
5863{
5864 gfc_symbol *sym;
5865 match m;
5866
636dff67 5867 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
06469efd
PT
5868 == FAILURE)
5869 return MATCH_ERROR;
5870
5871 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5872 {
5873 return MATCH_ERROR;
5874 }
5875
5876 if (gfc_match_eos () == MATCH_YES)
5877 goto syntax;
5878
5879 for(;;)
5880 {
5881 m = gfc_match_symbol (&sym, 0);
5882 switch (m)
5883 {
5884 case MATCH_YES:
636dff67
SK
5885 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5886 == FAILURE)
06469efd
PT
5887 return MATCH_ERROR;
5888 goto next_item;
5889
5890 case MATCH_NO:
5891 break;
5892
5893 case MATCH_ERROR:
5894 return MATCH_ERROR;
5895 }
5896
5897 next_item:
5898 if (gfc_match_eos () == MATCH_YES)
5899 break;
5900 if (gfc_match_char (',') != MATCH_YES)
5901 goto syntax;
5902 }
5903
5904 return MATCH_YES;
5905
5906syntax:
5907 gfc_error ("Syntax error in VALUE statement at %C");
5908 return MATCH_ERROR;
5909}
5910
66e4ab31 5911
775e6c3a
TB
5912match
5913gfc_match_volatile (void)
5914{
5915 gfc_symbol *sym;
5916 match m;
5917
636dff67 5918 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
775e6c3a
TB
5919 == FAILURE)
5920 return MATCH_ERROR;
5921
5922 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5923 {
5924 return MATCH_ERROR;
5925 }
5926
5927 if (gfc_match_eos () == MATCH_YES)
5928 goto syntax;
5929
5930 for(;;)
5931 {
9bce3c1c
TB
5932 /* VOLATILE is special because it can be added to host-associated
5933 symbols locally. */
5934 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
5935 switch (m)
5936 {
5937 case MATCH_YES:
636dff67
SK
5938 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5939 == FAILURE)
775e6c3a
TB
5940 return MATCH_ERROR;
5941 goto next_item;
5942
5943 case MATCH_NO:
5944 break;
5945
5946 case MATCH_ERROR:
5947 return MATCH_ERROR;
5948 }
5949
5950 next_item:
5951 if (gfc_match_eos () == MATCH_YES)
5952 break;
5953 if (gfc_match_char (',') != MATCH_YES)
5954 goto syntax;
5955 }
5956
5957 return MATCH_YES;
5958
5959syntax:
5960 gfc_error ("Syntax error in VOLATILE statement at %C");
5961 return MATCH_ERROR;
5962}
5963
5964
6de9cd9a
DN
5965/* Match a module procedure statement. Note that we have to modify
5966 symbols in the parent's namespace because the current one was there
49de9e73 5967 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
5968
5969match
5970gfc_match_modproc (void)
5971{
5972 char name[GFC_MAX_SYMBOL_LEN + 1];
5973 gfc_symbol *sym;
5974 match m;
060fca4a 5975 gfc_namespace *module_ns;
2b77e908 5976 gfc_interface *old_interface_head, *interface;
6de9cd9a
DN
5977
5978 if (gfc_state_stack->state != COMP_INTERFACE
5979 || gfc_state_stack->previous == NULL
129d15a3
JW
5980 || current_interface.type == INTERFACE_NAMELESS
5981 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 5982 {
636dff67
SK
5983 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5984 "interface");
6de9cd9a
DN
5985 return MATCH_ERROR;
5986 }
5987
060fca4a
PT
5988 module_ns = gfc_current_ns->parent;
5989 for (; module_ns; module_ns = module_ns->parent)
5990 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5991 break;
5992
5993 if (module_ns == NULL)
5994 return MATCH_ERROR;
5995
2b77e908
FXC
5996 /* Store the current state of the interface. We will need it if we
5997 end up with a syntax error and need to recover. */
5998 old_interface_head = gfc_current_interface_head ();
5999
6de9cd9a
DN
6000 for (;;)
6001 {
2b77e908
FXC
6002 bool last = false;
6003
6de9cd9a
DN
6004 m = gfc_match_name (name);
6005 if (m == MATCH_NO)
6006 goto syntax;
6007 if (m != MATCH_YES)
6008 return MATCH_ERROR;
6009
2b77e908
FXC
6010 /* Check for syntax error before starting to add symbols to the
6011 current namespace. */
6012 if (gfc_match_eos () == MATCH_YES)
6013 last = true;
6014 if (!last && gfc_match_char (',') != MATCH_YES)
6015 goto syntax;
6016
6017 /* Now we're sure the syntax is valid, we process this item
6018 further. */
060fca4a 6019 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
6020 return MATCH_ERROR;
6021
6022 if (sym->attr.proc != PROC_MODULE
231b2fcc
TS
6023 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6024 sym->name, NULL) == FAILURE)
6de9cd9a
DN
6025 return MATCH_ERROR;
6026
6027 if (gfc_add_interface (sym) == FAILURE)
6028 return MATCH_ERROR;
6029
71f77fd7
PT
6030 sym->attr.mod_proc = 1;
6031
2b77e908 6032 if (last)
6de9cd9a 6033 break;
6de9cd9a
DN
6034 }
6035
6036 return MATCH_YES;
6037
6038syntax:
2b77e908
FXC
6039 /* Restore the previous state of the interface. */
6040 interface = gfc_current_interface_head ();
6041 gfc_set_current_interface_head (old_interface_head);
6042
6043 /* Free the new interfaces. */
6044 while (interface != old_interface_head)
6045 {
6046 gfc_interface *i = interface->next;
6047 gfc_free (interface);
6048 interface = i;
6049 }
6050
6051 /* And issue a syntax error. */
6de9cd9a
DN
6052 gfc_syntax_error (ST_MODULE_PROC);
6053 return MATCH_ERROR;
6054}
6055
6056
a8b3b0b6
CR
6057/* Match the optional attribute specifiers for a type declaration.
6058 Return MATCH_ERROR if an error is encountered in one of the handled
6059 attributes (public, private, bind(c)), MATCH_NO if what's found is
6060 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6061 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
6062
6063match
a8b3b0b6 6064gfc_get_type_attr_spec (symbol_attribute *attr)
6de9cd9a 6065{
a8b3b0b6 6066 /* See if the derived type is marked as private. */
6de9cd9a
DN
6067 if (gfc_match (" , private") == MATCH_YES)
6068 {
d51347f9 6069 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 6070 {
d51347f9
TB
6071 gfc_error ("Derived type at %C can only be PRIVATE in the "
6072 "specification part of a module");
6de9cd9a
DN
6073 return MATCH_ERROR;
6074 }
6075
a8b3b0b6 6076 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6de9cd9a 6077 return MATCH_ERROR;
6de9cd9a 6078 }
a8b3b0b6 6079 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 6080 {
d51347f9 6081 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 6082 {
d51347f9
TB
6083 gfc_error ("Derived type at %C can only be PUBLIC in the "
6084 "specification part of a module");
6de9cd9a
DN
6085 return MATCH_ERROR;
6086 }
6087
a8b3b0b6 6088 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6de9cd9a 6089 return MATCH_ERROR;
6de9cd9a 6090 }
e7303e85 6091 else if (gfc_match(" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
6092 {
6093 /* If the type is defined to be bind(c) it then needs to make
6094 sure that all fields are interoperable. This will
6095 need to be a semantic check on the finished derived type.
6096 See 15.2.3 (lines 9-12) of F2003 draft. */
6097 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6098 return MATCH_ERROR;
6099
6100 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6101 }
6102 else
6103 return MATCH_NO;
6104
6105 /* If we get here, something matched. */
6106 return MATCH_YES;
6107}
6108
6109
6110/* Match the beginning of a derived type declaration. If a type name
6111 was the result of a function, then it is possible to have a symbol
6112 already to be known as a derived type yet have no components. */
6113
6114match
6115gfc_match_derived_decl (void)
6116{
6117 char name[GFC_MAX_SYMBOL_LEN + 1];
6118 symbol_attribute attr;
6119 gfc_symbol *sym;
6120 match m;
6121 match is_type_attr_spec = MATCH_NO;
e7303e85 6122 bool seen_attr = false;
a8b3b0b6
CR
6123
6124 if (gfc_current_state () == COMP_DERIVED)
6125 return MATCH_NO;
6126
6127 gfc_clear_attr (&attr);
6128
6129 do
6130 {
6131 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6132 if (is_type_attr_spec == MATCH_ERROR)
6133 return MATCH_ERROR;
e7303e85
FXC
6134 if (is_type_attr_spec == MATCH_YES)
6135 seen_attr = true;
a8b3b0b6 6136 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 6137
e7303e85 6138 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6de9cd9a
DN
6139 {
6140 gfc_error ("Expected :: in TYPE definition at %C");
6141 return MATCH_ERROR;
6142 }
6143
6144 m = gfc_match (" %n%t", name);
6145 if (m != MATCH_YES)
6146 return m;
6147
e9c06563
TB
6148 /* Make sure the name is not the name of an intrinsic type. */
6149 if (gfc_is_intrinsic_typename (name))
6de9cd9a 6150 {
636dff67
SK
6151 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6152 "type", name);
6de9cd9a
DN
6153 return MATCH_ERROR;
6154 }
6155
6156 if (gfc_get_symbol (name, NULL, &sym))
6157 return MATCH_ERROR;
6158
6159 if (sym->ts.type != BT_UNKNOWN)
6160 {
6161 gfc_error ("Derived type name '%s' at %C already has a basic type "
6162 "of %s", sym->name, gfc_typename (&sym->ts));
6163 return MATCH_ERROR;
6164 }
6165
6166 /* The symbol may already have the derived attribute without the
6167 components. The ways this can happen is via a function
6168 definition, an INTRINSIC statement or a subtype in another
6169 derived type that is a pointer. The first part of the AND clause
f7b529fa 6170 is true if a the symbol is not the return value of a function. */
6de9cd9a 6171 if (sym->attr.flavor != FL_DERIVED
231b2fcc 6172 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
6173 return MATCH_ERROR;
6174
9fa6b0af 6175 if (sym->components != NULL || sym->attr.zero_comp)
6de9cd9a 6176 {
636dff67
SK
6177 gfc_error ("Derived type definition of '%s' at %C has already been "
6178 "defined", sym->name);
6de9cd9a
DN
6179 return MATCH_ERROR;
6180 }
6181
6182 if (attr.access != ACCESS_UNKNOWN
231b2fcc 6183 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6de9cd9a
DN
6184 return MATCH_ERROR;
6185
a8b3b0b6
CR
6186 /* See if the derived type was labeled as bind(c). */
6187 if (attr.is_bind_c != 0)
6188 sym->attr.is_bind_c = attr.is_bind_c;
6189
6de9cd9a
DN
6190 gfc_new_block = sym;
6191
6192 return MATCH_YES;
6193}
83d890b9
AL
6194
6195
6196/* Cray Pointees can be declared as:
6197 pointer (ipt, a (n,m,...,*))
6198 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6199 cheat and set a constant bound of 1 for the last dimension, if this
6200 is the case. Since there is no bounds-checking for Cray Pointees,
6201 this will be okay. */
6202
6203try
6204gfc_mod_pointee_as (gfc_array_spec *as)
6205{
6206 as->cray_pointee = true; /* This will be useful to know later. */
6207 if (as->type == AS_ASSUMED_SIZE)
6208 {
6209 as->type = AS_EXPLICIT;
6210 as->upper[as->rank - 1] = gfc_int_expr (1);
6211 as->cp_was_assumed = true;
6212 }
6213 else if (as->type == AS_ASSUMED_SHAPE)
6214 {
6215 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6216 return MATCH_ERROR;
6217 }
6218 return MATCH_YES;
6219}
25d8f0a2
TS
6220
6221
6222/* Match the enum definition statement, here we are trying to match
6223 the first line of enum definition statement.
6224 Returns MATCH_YES if match is found. */
6225
6226match
6227gfc_match_enum (void)
6228{
6229 match m;
6230
6231 m = gfc_match_eos ();
6232 if (m != MATCH_YES)
6233 return m;
6234
6133c68a 6235 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
25d8f0a2
TS
6236 == FAILURE)
6237 return MATCH_ERROR;
6238
6239 return MATCH_YES;
6240}
6241
6242
6133c68a
TS
6243/* Match a variable name with an optional initializer. When this
6244 subroutine is called, a variable is expected to be parsed next.
6245 Depending on what is happening at the moment, updates either the
6246 symbol table or the current interface. */
6247
6248static match
6249enumerator_decl (void)
6250{
6251 char name[GFC_MAX_SYMBOL_LEN + 1];
6252 gfc_expr *initializer;
6253 gfc_array_spec *as = NULL;
6254 gfc_symbol *sym;
6255 locus var_locus;
6256 match m;
6257 try t;
6258 locus old_locus;
6259
6260 initializer = NULL;
6261 old_locus = gfc_current_locus;
6262
6263 /* When we get here, we've just matched a list of attributes and
6264 maybe a type and a double colon. The next thing we expect to see
6265 is the name of the symbol. */
6266 m = gfc_match_name (name);
6267 if (m != MATCH_YES)
6268 goto cleanup;
6269
6270 var_locus = gfc_current_locus;
6271
6272 /* OK, we've successfully matched the declaration. Now put the
6273 symbol in the current namespace. If we fail to create the symbol,
6274 bail out. */
6275 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6276 {
6277 m = MATCH_ERROR;
6278 goto cleanup;
6279 }
6280
6281 /* The double colon must be present in order to have initializers.
6282 Otherwise the statement is ambiguous with an assignment statement. */
6283 if (colon_seen)
6284 {
6285 if (gfc_match_char ('=') == MATCH_YES)
6286 {
6287 m = gfc_match_init_expr (&initializer);
6288 if (m == MATCH_NO)
6289 {
6290 gfc_error ("Expected an initialization expression at %C");
6291 m = MATCH_ERROR;
6292 }
6293
6294 if (m != MATCH_YES)
6295 goto cleanup;
6296 }
6297 }
6298
6299 /* If we do not have an initializer, the initialization value of the
6300 previous enumerator (stored in last_initializer) is incremented
6301 by 1 and is used to initialize the current enumerator. */
6302 if (initializer == NULL)
6303 initializer = gfc_enum_initializer (last_initializer, old_locus);
d51347f9 6304
6133c68a
TS
6305 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6306 {
6307 gfc_error("ENUMERATOR %L not initialized with integer expression",
6308 &var_locus);
d51347f9 6309 m = MATCH_ERROR;
6133c68a
TS
6310 gfc_free_enum_history ();
6311 goto cleanup;
6312 }
6313
6314 /* Store this current initializer, for the next enumerator variable
6315 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6316 use last_initializer below. */
6317 last_initializer = initializer;
6318 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6319
6320 /* Maintain enumerator history. */
6321 gfc_find_symbol (name, NULL, 0, &sym);
6322 create_enum_history (sym, last_initializer);
6323
6324 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6325
6326cleanup:
6327 /* Free stuff up and return. */
6328 gfc_free_expr (initializer);
6329
6330 return m;
6331}
6332
6333
66e4ab31 6334/* Match the enumerator definition statement. */
25d8f0a2
TS
6335
6336match
6337gfc_match_enumerator_def (void)
6338{
6339 match m;
6133c68a 6340 try t;
d51347f9 6341
25d8f0a2 6342 gfc_clear_ts (&current_ts);
d51347f9 6343
25d8f0a2
TS
6344 m = gfc_match (" enumerator");
6345 if (m != MATCH_YES)
6346 return m;
6133c68a
TS
6347
6348 m = gfc_match (" :: ");
6349 if (m == MATCH_ERROR)
6350 return m;
6351
6352 colon_seen = (m == MATCH_YES);
d51347f9 6353
25d8f0a2
TS
6354 if (gfc_current_state () != COMP_ENUM)
6355 {
6356 gfc_error ("ENUM definition statement expected before %C");
6357 gfc_free_enum_history ();
6358 return MATCH_ERROR;
6359 }
6360
6361 (&current_ts)->type = BT_INTEGER;
6362 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 6363
6133c68a
TS
6364 gfc_clear_attr (&current_attr);
6365 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6366 if (t == FAILURE)
25d8f0a2 6367 {
6133c68a 6368 m = MATCH_ERROR;
25d8f0a2
TS
6369 goto cleanup;
6370 }
6371
25d8f0a2
TS
6372 for (;;)
6373 {
6133c68a 6374 m = enumerator_decl ();
25d8f0a2
TS
6375 if (m == MATCH_ERROR)
6376 goto cleanup;
6377 if (m == MATCH_NO)
6378 break;
6379
6380 if (gfc_match_eos () == MATCH_YES)
6381 goto cleanup;
6382 if (gfc_match_char (',') != MATCH_YES)
6383 break;
6384 }
6385
6386 if (gfc_current_state () == COMP_ENUM)
6387 {
6388 gfc_free_enum_history ();
6389 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6390 m = MATCH_ERROR;
6391 }
6392
6393cleanup:
6394 gfc_free_array_spec (current_as);
6395 current_as = NULL;
6396 return m;
6397
6398}
6399
This page took 1.901661 seconds and 5 git commands to generate.