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