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