]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
2007-11-17 Richard Guenther <rguenther@suse.de>
[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 {
74113644 3949 /* Resolve interface if possible. That way, attr.procedure is only set
3950 if it is declared by a later procedure-declaration-stmt, which is
3951 invalid per C1212. */
3952 while (proc_if->interface)
3953 proc_if = proc_if->interface;
3954
af1a34ee 3955 if (proc_if->generic)
3956 {
3957 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3958 return MATCH_ERROR;
3959 }
3960 if (proc_if->attr.proc == PROC_ST_FUNCTION)
3961 {
3962 gfc_error ("Interface '%s' at %C may not be a statement function",
3963 proc_if->name);
3964 return MATCH_ERROR;
3965 }
3966 /* Handle intrinsic procedures. */
3967 if (gfc_intrinsic_name (proc_if->name, 0)
3968 || gfc_intrinsic_name (proc_if->name, 1))
3969 proc_if->attr.intrinsic = 1;
3970 if (proc_if->attr.intrinsic
3971 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3972 {
3973 gfc_error ("Intrinsic procedure '%s' not allowed "
3974 "in PROCEDURE statement at %C", proc_if->name);
3975 return MATCH_ERROR;
3976 }
af1a34ee 3977 }
3978
3979got_ts:
af1a34ee 3980 if (gfc_match (" )") != MATCH_YES)
3981 {
3982 gfc_current_locus = entry_loc;
3983 return MATCH_NO;
3984 }
3985
3986 /* Parse attributes. */
3987 m = match_attr_spec();
3988 if (m == MATCH_ERROR)
3989 return MATCH_ERROR;
3990
3991 /* Get procedure symbols. */
3992 for(num=1;;num++)
3993 {
af1a34ee 3994 m = gfc_match_symbol (&sym, 0);
3995 if (m == MATCH_NO)
3996 goto syntax;
3997 else if (m == MATCH_ERROR)
3998 return m;
3999
4000 /* Add current_attr to the symbol attributes. */
4001 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4002 return MATCH_ERROR;
4003
4004 if (sym->attr.is_bind_c)
4005 {
4006 /* Check for C1218. */
4007 if (!proc_if || !proc_if->attr.is_bind_c)
4008 {
4009 gfc_error ("BIND(C) attribute at %C requires "
4010 "an interface with BIND(C)");
4011 return MATCH_ERROR;
4012 }
4013 /* Check for C1217. */
4014 if (has_name_equals && sym->attr.pointer)
4015 {
4016 gfc_error ("BIND(C) procedure with NAME may not have "
4017 "POINTER attribute at %C");
4018 return MATCH_ERROR;
4019 }
4020 if (has_name_equals && sym->attr.dummy)
4021 {
4022 gfc_error ("Dummy procedure at %C may not have "
4023 "BIND(C) attribute with NAME");
4024 return MATCH_ERROR;
4025 }
4026 /* Set binding label for BIND(C). */
4027 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4028 return MATCH_ERROR;
4029 }
4030
4031 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4032 return MATCH_ERROR;
4033 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4034 return MATCH_ERROR;
4035
4036 /* Set interface. */
4037 if (proc_if != NULL)
f7d7a083 4038 {
4039 sym->interface = proc_if;
4040 sym->attr.untyped = 1;
4041 }
af1a34ee 4042 else if (current_ts.type != BT_UNKNOWN)
4043 {
4044 sym->interface = gfc_new_symbol ("", gfc_current_ns);
4045 sym->interface->ts = current_ts;
4046 sym->interface->attr.function = 1;
4047 sym->ts = sym->interface->ts;
4048 sym->attr.function = sym->interface->attr.function;
4049 }
4050
4051 if (gfc_match_eos () == MATCH_YES)
4052 return MATCH_YES;
4053 if (gfc_match_char (',') != MATCH_YES)
4054 goto syntax;
4055 }
4056
4057syntax:
4058 gfc_error ("Syntax error in PROCEDURE statement at %C");
4059 return MATCH_ERROR;
4060}
4061
4062
4063/* Match a PROCEDURE declaration inside an interface (R1206). */
4064
4065static match
4066match_procedure_in_interface (void)
4067{
4068 match m;
4069 gfc_symbol *sym;
4070 char name[GFC_MAX_SYMBOL_LEN + 1];
4071
4072 if (current_interface.type == INTERFACE_NAMELESS
4073 || current_interface.type == INTERFACE_ABSTRACT)
4074 {
4075 gfc_error ("PROCEDURE at %C must be in a generic interface");
4076 return MATCH_ERROR;
4077 }
4078
4079 for(;;)
4080 {
4081 m = gfc_match_name (name);
4082 if (m == MATCH_NO)
4083 goto syntax;
4084 else if (m == MATCH_ERROR)
4085 return m;
4086 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4087 return MATCH_ERROR;
4088
4089 if (gfc_add_interface (sym) == FAILURE)
4090 return MATCH_ERROR;
4091
af1a34ee 4092 if (gfc_match_eos () == MATCH_YES)
4093 break;
4094 if (gfc_match_char (',') != MATCH_YES)
4095 goto syntax;
4096 }
4097
4098 return MATCH_YES;
4099
4100syntax:
4101 gfc_error ("Syntax error in PROCEDURE statement at %C");
4102 return MATCH_ERROR;
4103}
4104
4105
4106/* General matcher for PROCEDURE declarations. */
4107
4108match
4109gfc_match_procedure (void)
4110{
4111 match m;
4112
4113 switch (gfc_current_state ())
4114 {
4115 case COMP_NONE:
4116 case COMP_PROGRAM:
4117 case COMP_MODULE:
4118 case COMP_SUBROUTINE:
4119 case COMP_FUNCTION:
4120 m = match_procedure_decl ();
4121 break;
4122 case COMP_INTERFACE:
4123 m = match_procedure_in_interface ();
4124 break;
4125 case COMP_DERIVED:
4126 gfc_error ("Fortran 2003: Procedure components at %C are "
4127 "not yet implemented in gfortran");
4128 return MATCH_ERROR;
4129 default:
4130 return MATCH_NO;
4131 }
4132
4133 if (m != MATCH_YES)
4134 return m;
4135
4136 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4137 == FAILURE)
4138 return MATCH_ERROR;
4139
4140 return m;
4141}
4142
4143
4ee9c684 4144/* Match a function declaration. */
4145
4146match
4147gfc_match_function_decl (void)
4148{
4149 char name[GFC_MAX_SYMBOL_LEN + 1];
4150 gfc_symbol *sym, *result;
4151 locus old_loc;
4152 match m;
c5d33754 4153 match suffix_match;
4154 match found_match; /* Status returned by match func. */
4ee9c684 4155
4156 if (gfc_current_state () != COMP_NONE
4157 && gfc_current_state () != COMP_INTERFACE
4158 && gfc_current_state () != COMP_CONTAINS)
4159 return MATCH_NO;
4160
4161 gfc_clear_ts (&current_ts);
4162
cbb9e6aa 4163 old_loc = gfc_current_locus;
4ee9c684 4164
4165 m = match_prefix (&current_ts);
4166 if (m != MATCH_YES)
4167 {
cbb9e6aa 4168 gfc_current_locus = old_loc;
4ee9c684 4169 return m;
4170 }
4171
4172 if (gfc_match ("function% %n", name) != MATCH_YES)
4173 {
cbb9e6aa 4174 gfc_current_locus = old_loc;
4ee9c684 4175 return MATCH_NO;
4176 }
d77f260f 4177 if (get_proc_name (name, &sym, false))
4ee9c684 4178 return MATCH_ERROR;
4179 gfc_new_block = sym;
4180
4181 m = gfc_match_formal_arglist (sym, 0, 0);
4182 if (m == MATCH_NO)
9b435b6d 4183 {
4184 gfc_error ("Expected formal argument list in function "
1a9745d2 4185 "definition at %C");
9b435b6d 4186 m = MATCH_ERROR;
4187 goto cleanup;
4188 }
4ee9c684 4189 else if (m == MATCH_ERROR)
4190 goto cleanup;
4191
4192 result = NULL;
4193
c5d33754 4194 /* According to the draft, the bind(c) and result clause can
4195 come in either order after the formal_arg_list (i.e., either
4196 can be first, both can exist together or by themselves or neither
4197 one). Therefore, the match_result can't match the end of the
4198 string, and check for the bind(c) or result clause in either order. */
4199 found_match = gfc_match_eos ();
4200
4201 /* Make sure that it isn't already declared as BIND(C). If it is, it
4202 must have been marked BIND(C) with a BIND(C) attribute and that is
4203 not allowed for procedures. */
4204 if (sym->attr.is_bind_c == 1)
4205 {
4206 sym->attr.is_bind_c = 0;
4207 if (sym->old_symbol != NULL)
4208 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4209 "variables or common blocks",
4210 &(sym->old_symbol->declared_at));
4211 else
4212 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4213 "variables or common blocks", &gfc_current_locus);
4ee9c684 4214 }
4215
c5d33754 4216 if (found_match != MATCH_YES)
4ee9c684 4217 {
c5d33754 4218 /* If we haven't found the end-of-statement, look for a suffix. */
4219 suffix_match = gfc_match_suffix (sym, &result);
4220 if (suffix_match == MATCH_YES)
4221 /* Need to get the eos now. */
4222 found_match = gfc_match_eos ();
4223 else
4224 found_match = suffix_match;
4ee9c684 4225 }
4226
c5d33754 4227 if(found_match != MATCH_YES)
4228 m = MATCH_ERROR;
4ee9c684 4229 else
4230 {
c5d33754 4231 /* Make changes to the symbol. */
4232 m = MATCH_ERROR;
4233
4234 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4235 goto cleanup;
4236
4237 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4238 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4239 goto cleanup;
4ee9c684 4240
c5d33754 4241 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4242 && !sym->attr.implicit_type)
4243 {
4244 gfc_error ("Function '%s' at %C already has a type of %s", name,
4245 gfc_basic_typename (sym->ts.type));
4246 goto cleanup;
4247 }
4248
4249 if (result == NULL)
4250 {
4251 sym->ts = current_ts;
4252 sym->result = sym;
4253 }
4254 else
4255 {
4256 result->ts = current_ts;
4257 sym->result = result;
4258 }
4259
4260 return MATCH_YES;
4261 }
4ee9c684 4262
4263cleanup:
cbb9e6aa 4264 gfc_current_locus = old_loc;
4ee9c684 4265 return m;
4266}
4267
1a9745d2 4268
4269/* This is mostly a copy of parse.c(add_global_procedure) but modified to
4270 pass the name of the entry, rather than the gfc_current_block name, and
4271 to return false upon finding an existing global entry. */
858f9894 4272
4273static bool
1a9745d2 4274add_global_entry (const char *name, int sub)
858f9894 4275{
4276 gfc_gsymbol *s;
4277
4278 s = gfc_get_gsymbol(name);
4279
4280 if (s->defined
1a9745d2 4281 || (s->type != GSYM_UNKNOWN
4282 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
cbbac028 4283 gfc_global_used(s, NULL);
858f9894 4284 else
4285 {
4286 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4287 s->where = gfc_current_locus;
4288 s->defined = 1;
4289 return true;
4290 }
4291 return false;
4292}
4ee9c684 4293
1a9745d2 4294
4ee9c684 4295/* Match an ENTRY statement. */
4296
4297match
4298gfc_match_entry (void)
4299{
1b716045 4300 gfc_symbol *proc;
4301 gfc_symbol *result;
4302 gfc_symbol *entry;
4ee9c684 4303 char name[GFC_MAX_SYMBOL_LEN + 1];
4304 gfc_compile_state state;
4305 match m;
1b716045 4306 gfc_entry_list *el;
7b5e1acc 4307 locus old_loc;
d77f260f 4308 bool module_procedure;
4ee9c684 4309
4310 m = gfc_match_name (name);
4311 if (m != MATCH_YES)
4312 return m;
4313
1b716045 4314 state = gfc_current_state ();
ea37f786 4315 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
1b716045 4316 {
ea37f786 4317 switch (state)
4318 {
4319 case COMP_PROGRAM:
4320 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4321 break;
4322 case COMP_MODULE:
4323 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4324 break;
4325 case COMP_BLOCK_DATA:
1a9745d2 4326 gfc_error ("ENTRY statement at %C cannot appear within "
4327 "a BLOCK DATA");
ea37f786 4328 break;
4329 case COMP_INTERFACE:
1a9745d2 4330 gfc_error ("ENTRY statement at %C cannot appear within "
4331 "an INTERFACE");
ea37f786 4332 break;
4333 case COMP_DERIVED:
1a9745d2 4334 gfc_error ("ENTRY statement at %C cannot appear within "
4335 "a DERIVED TYPE block");
ea37f786 4336 break;
4337 case COMP_IF:
1a9745d2 4338 gfc_error ("ENTRY statement at %C cannot appear within "
4339 "an IF-THEN block");
ea37f786 4340 break;
4341 case COMP_DO:
1a9745d2 4342 gfc_error ("ENTRY statement at %C cannot appear within "
4343 "a DO block");
ea37f786 4344 break;
4345 case COMP_SELECT:
1a9745d2 4346 gfc_error ("ENTRY statement at %C cannot appear within "
4347 "a SELECT block");
ea37f786 4348 break;
4349 case COMP_FORALL:
1a9745d2 4350 gfc_error ("ENTRY statement at %C cannot appear within "
4351 "a FORALL block");
ea37f786 4352 break;
4353 case COMP_WHERE:
1a9745d2 4354 gfc_error ("ENTRY statement at %C cannot appear within "
4355 "a WHERE block");
ea37f786 4356 break;
4357 case COMP_CONTAINS:
1a9745d2 4358 gfc_error ("ENTRY statement at %C cannot appear within "
4359 "a contained subprogram");
ea37f786 4360 break;
4361 default:
4362 gfc_internal_error ("gfc_match_entry(): Bad state");
4363 }
1b716045 4364 return MATCH_ERROR;
4365 }
4366
d77f260f 4367 module_procedure = gfc_current_ns->parent != NULL
1a9745d2 4368 && gfc_current_ns->parent->proc_name
4369 && gfc_current_ns->parent->proc_name->attr.flavor
4370 == FL_MODULE;
d77f260f 4371
1b716045 4372 if (gfc_current_ns->parent != NULL
4373 && gfc_current_ns->parent->proc_name
d77f260f 4374 && !module_procedure)
1b716045 4375 {
4376 gfc_error("ENTRY statement at %C cannot appear in a "
4377 "contained procedure");
4378 return MATCH_ERROR;
4379 }
4380
d77f260f 4381 /* Module function entries need special care in get_proc_name
4382 because previous references within the function will have
4383 created symbols attached to the current namespace. */
4384 if (get_proc_name (name, &entry,
4385 gfc_current_ns->parent != NULL
4386 && module_procedure
4387 && gfc_current_ns->proc_name->attr.function))
4ee9c684 4388 return MATCH_ERROR;
4389
1b716045 4390 proc = gfc_current_block ();
4391
4392 if (state == COMP_SUBROUTINE)
4ee9c684 4393 {
950683ed 4394 /* An entry in a subroutine. */
8cafc742 4395 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
858f9894 4396 return MATCH_ERROR;
4397
4ee9c684 4398 m = gfc_match_formal_arglist (entry, 0, 1);
4399 if (m != MATCH_YES)
4400 return MATCH_ERROR;
4401
950683ed 4402 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4403 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 4404 return MATCH_ERROR;
1b716045 4405 }
4406 else
4407 {
7b5e1acc 4408 /* An entry in a function.
1a9745d2 4409 We need to take special care because writing
4410 ENTRY f()
4411 as
4412 ENTRY f
4413 is allowed, whereas
4414 ENTRY f() RESULT (r)
4415 can't be written as
4416 ENTRY f RESULT (r). */
8cafc742 4417 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
858f9894 4418 return MATCH_ERROR;
4419
7b5e1acc 4420 old_loc = gfc_current_locus;
4421 if (gfc_match_eos () == MATCH_YES)
4422 {
4423 gfc_current_locus = old_loc;
4424 /* Match the empty argument list, and add the interface to
4425 the symbol. */
4426 m = gfc_match_formal_arglist (entry, 0, 1);
4427 }
4428 else
4429 m = gfc_match_formal_arglist (entry, 0, 0);
4430
4ee9c684 4431 if (m != MATCH_YES)
4432 return MATCH_ERROR;
4433
4ee9c684 4434 result = NULL;
4435
4436 if (gfc_match_eos () == MATCH_YES)
4437 {
950683ed 4438 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4439 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 4440 return MATCH_ERROR;
4441
c6871095 4442 entry->result = entry;
4ee9c684 4443 }
4444 else
4445 {
1b716045 4446 m = match_result (proc, &result);
4ee9c684 4447 if (m == MATCH_NO)
4448 gfc_syntax_error (ST_ENTRY);
4449 if (m != MATCH_YES)
4450 return MATCH_ERROR;
4451
950683ed 4452 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4453 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
1a9745d2 4454 || gfc_add_function (&entry->attr, result->name, NULL)
4455 == FAILURE)
4ee9c684 4456 return MATCH_ERROR;
c6871095 4457
4458 entry->result = result;
4ee9c684 4459 }
4ee9c684 4460 }
4461
4462 if (gfc_match_eos () != MATCH_YES)
4463 {
4464 gfc_syntax_error (ST_ENTRY);
4465 return MATCH_ERROR;
4466 }
4467
1b716045 4468 entry->attr.recursive = proc->attr.recursive;
4469 entry->attr.elemental = proc->attr.elemental;
4470 entry->attr.pure = proc->attr.pure;
4ee9c684 4471
1b716045 4472 el = gfc_get_entry_list ();
4473 el->sym = entry;
4474 el->next = gfc_current_ns->entries;
4475 gfc_current_ns->entries = el;
4476 if (el->next)
4477 el->id = el->next->id + 1;
4478 else
4479 el->id = 1;
4ee9c684 4480
1b716045 4481 new_st.op = EXEC_ENTRY;
4482 new_st.ext.entry = el;
4483
4484 return MATCH_YES;
4ee9c684 4485}
4486
4487
4488/* Match a subroutine statement, including optional prefixes. */
4489
4490match
4491gfc_match_subroutine (void)
4492{
4493 char name[GFC_MAX_SYMBOL_LEN + 1];
4494 gfc_symbol *sym;
4495 match m;
c5d33754 4496 match is_bind_c;
4497 char peek_char;
4ee9c684 4498
4499 if (gfc_current_state () != COMP_NONE
4500 && gfc_current_state () != COMP_INTERFACE
4501 && gfc_current_state () != COMP_CONTAINS)
4502 return MATCH_NO;
4503
4504 m = match_prefix (NULL);
4505 if (m != MATCH_YES)
4506 return m;
4507
4508 m = gfc_match ("subroutine% %n", name);
4509 if (m != MATCH_YES)
4510 return m;
4511
d77f260f 4512 if (get_proc_name (name, &sym, false))
4ee9c684 4513 return MATCH_ERROR;
4514 gfc_new_block = sym;
4515
c5d33754 4516 /* Check what next non-whitespace character is so we can tell if there
4517 where the required parens if we have a BIND(C). */
4518 gfc_gobble_whitespace ();
4519 peek_char = gfc_peek_char ();
4520
950683ed 4521 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4ee9c684 4522 return MATCH_ERROR;
4523
4524 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4525 return MATCH_ERROR;
4526
c5d33754 4527 /* Make sure that it isn't already declared as BIND(C). If it is, it
4528 must have been marked BIND(C) with a BIND(C) attribute and that is
4529 not allowed for procedures. */
4530 if (sym->attr.is_bind_c == 1)
4531 {
4532 sym->attr.is_bind_c = 0;
4533 if (sym->old_symbol != NULL)
4534 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4535 "variables or common blocks",
4536 &(sym->old_symbol->declared_at));
4537 else
4538 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4539 "variables or common blocks", &gfc_current_locus);
4540 }
4541
4542 /* Here, we are just checking if it has the bind(c) attribute, and if
4543 so, then we need to make sure it's all correct. If it doesn't,
4544 we still need to continue matching the rest of the subroutine line. */
4545 is_bind_c = gfc_match_bind_c (sym);
4546 if (is_bind_c == MATCH_ERROR)
4547 {
4548 /* There was an attempt at the bind(c), but it was wrong. An
4549 error message should have been printed w/in the gfc_match_bind_c
4550 so here we'll just return the MATCH_ERROR. */
4551 return MATCH_ERROR;
4552 }
4553
4554 if (is_bind_c == MATCH_YES)
4555 {
4556 if (peek_char != '(')
4557 {
4558 gfc_error ("Missing required parentheses before BIND(C) at %C");
4559 return MATCH_ERROR;
4560 }
4561 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4562 == FAILURE)
4563 return MATCH_ERROR;
4564 }
4565
4ee9c684 4566 if (gfc_match_eos () != MATCH_YES)
4567 {
4568 gfc_syntax_error (ST_SUBROUTINE);
4569 return MATCH_ERROR;
4570 }
4571
4572 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4573 return MATCH_ERROR;
4574
4575 return MATCH_YES;
4576}
4577
4578
c5d33754 4579/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4580 given, and set the binding label in either the given symbol (if not
a0527218 4581 NULL), or in the current_ts. The symbol may be NULL because we may
c5d33754 4582 encounter the BIND(C) before the declaration itself. Return
4583 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4584 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4585 or MATCH_YES if the specifier was correct and the binding label and
4586 bind(c) fields were set correctly for the given symbol or the
4587 current_ts. */
4588
4589match
4590gfc_match_bind_c (gfc_symbol *sym)
4591{
4592 /* binding label, if exists */
4593 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4594 match double_quote;
4595 match single_quote;
c5d33754 4596
4597 /* Initialize the flag that specifies whether we encountered a NAME=
4598 specifier or not. */
4599 has_name_equals = 0;
4600
4601 /* Init the first char to nil so we can catch if we don't have
4602 the label (name attr) or the symbol name yet. */
4603 binding_label[0] = '\0';
4604
4605 /* This much we have to be able to match, in this order, if
4606 there is a bind(c) label. */
4607 if (gfc_match (" bind ( c ") != MATCH_YES)
4608 return MATCH_NO;
4609
4610 /* Now see if there is a binding label, or if we've reached the
4611 end of the bind(c) attribute without one. */
4612 if (gfc_match_char (',') == MATCH_YES)
4613 {
4614 if (gfc_match (" name = ") != MATCH_YES)
4615 {
4616 gfc_error ("Syntax error in NAME= specifier for binding label "
4617 "at %C");
4618 /* should give an error message here */
4619 return MATCH_ERROR;
4620 }
4621
4622 has_name_equals = 1;
4623
4624 /* Get the opening quote. */
4625 double_quote = MATCH_YES;
4626 single_quote = MATCH_YES;
4627 double_quote = gfc_match_char ('"');
4628 if (double_quote != MATCH_YES)
4629 single_quote = gfc_match_char ('\'');
4630 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4631 {
4632 gfc_error ("Syntax error in NAME= specifier for binding label "
4633 "at %C");
4634 return MATCH_ERROR;
4635 }
4636
4637 /* Grab the binding label, using functions that will not lower
4638 case the names automatically. */
4639 if (gfc_match_name_C (binding_label) != MATCH_YES)
4640 return MATCH_ERROR;
4641
4642 /* Get the closing quotation. */
4643 if (double_quote == MATCH_YES)
4644 {
4645 if (gfc_match_char ('"') != MATCH_YES)
4646 {
4647 gfc_error ("Missing closing quote '\"' for binding label at %C");
4648 /* User started string with '"' so looked to match it. */
4649 return MATCH_ERROR;
4650 }
4651 }
4652 else
4653 {
4654 if (gfc_match_char ('\'') != MATCH_YES)
4655 {
4656 gfc_error ("Missing closing quote '\'' for binding label at %C");
4657 /* User started string with "'" char. */
4658 return MATCH_ERROR;
4659 }
4660 }
4661 }
4662
4663 /* Get the required right paren. */
4664 if (gfc_match_char (')') != MATCH_YES)
4665 {
4666 gfc_error ("Missing closing paren for binding label at %C");
4667 return MATCH_ERROR;
4668 }
4669
4670 /* Save the binding label to the symbol. If sym is null, we're
4671 probably matching the typespec attributes of a declaration and
4672 haven't gotten the name yet, and therefore, no symbol yet. */
4673 if (binding_label[0] != '\0')
4674 {
4675 if (sym != NULL)
4676 {
4677 strncpy (sym->binding_label, binding_label,
4678 strlen (binding_label)+1);
4679 }
4680 else
4681 strncpy (curr_binding_label, binding_label,
4682 strlen (binding_label) + 1);
4683 }
4684 else
4685 {
4686 /* No binding label, but if symbol isn't null, we
4687 can set the label for it here. */
4688 /* TODO: If the name= was given and no binding label (name=""), we simply
4689 will let fortran mangle the symbol name as it usually would.
4690 However, this could still let C call it if the user looked up the
4691 symbol in the object file. Should the name set during mangling in
4692 trans-decl.c be marked with characters that are invalid for C to
4693 prevent this? */
4694 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4695 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4696 }
94fa7146 4697
5cf92482 4698 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4699 && current_interface.type == INTERFACE_ABSTRACT)
94fa7146 4700 {
4701 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4702 return MATCH_ERROR;
4703 }
4704
c5d33754 4705 return MATCH_YES;
4706}
4707
4708
231e961a 4709/* Return nonzero if we're currently compiling a contained procedure. */
c0985832 4710
4711static int
4712contained_procedure (void)
4713{
4714 gfc_state_data *s;
4715
4716 for (s=gfc_state_stack; s; s=s->previous)
4717 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
1a9745d2 4718 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
c0985832 4719 return 1;
4720
4721 return 0;
4722}
4723
e14bee04 4724/* Set the kind of each enumerator. The kind is selected such that it is
3b6a4b41 4725 interoperable with the corresponding C enumeration type, making
4726 sure that -fshort-enums is honored. */
4727
4728static void
4729set_enum_kind(void)
4730{
4731 enumerator_history *current_history = NULL;
4732 int kind;
4733 int i;
4734
4735 if (max_enum == NULL || enum_history == NULL)
4736 return;
4737
4738 if (!gfc_option.fshort_enums)
e14bee04 4739 return;
4740
3b6a4b41 4741 i = 0;
4742 do
4743 {
4744 kind = gfc_integer_kinds[i++].kind;
4745 }
e14bee04 4746 while (kind < gfc_c_int_kind
3b6a4b41 4747 && gfc_check_integer_range (max_enum->initializer->value.integer,
4748 kind) != ARITH_OK);
4749
4750 current_history = enum_history;
4751 while (current_history != NULL)
4752 {
4753 current_history->sym->ts.kind = kind;
4754 current_history = current_history->next;
4755 }
4756}
4757
1a9745d2 4758
4ee9c684 4759/* Match any of the various end-block statements. Returns the type of
4760 END to the caller. The END INTERFACE, END IF, END DO and END
4761 SELECT statements cannot be replaced by a single END statement. */
4762
4763match
1a9745d2 4764gfc_match_end (gfc_statement *st)
4ee9c684 4765{
4766 char name[GFC_MAX_SYMBOL_LEN + 1];
4767 gfc_compile_state state;
4768 locus old_loc;
4769 const char *block_name;
4770 const char *target;
c0985832 4771 int eos_ok;
4ee9c684 4772 match m;
4773
cbb9e6aa 4774 old_loc = gfc_current_locus;
4ee9c684 4775 if (gfc_match ("end") != MATCH_YES)
4776 return MATCH_NO;
4777
4778 state = gfc_current_state ();
1a9745d2 4779 block_name = gfc_current_block () == NULL
4780 ? NULL : gfc_current_block ()->name;
4ee9c684 4781
4782 if (state == COMP_CONTAINS)
4783 {
4784 state = gfc_state_stack->previous->state;
1a9745d2 4785 block_name = gfc_state_stack->previous->sym == NULL
4786 ? NULL : gfc_state_stack->previous->sym->name;
4ee9c684 4787 }
4788
4789 switch (state)
4790 {
4791 case COMP_NONE:
4792 case COMP_PROGRAM:
4793 *st = ST_END_PROGRAM;
4794 target = " program";
c0985832 4795 eos_ok = 1;
4ee9c684 4796 break;
4797
4798 case COMP_SUBROUTINE:
4799 *st = ST_END_SUBROUTINE;
4800 target = " subroutine";
c0985832 4801 eos_ok = !contained_procedure ();
4ee9c684 4802 break;
4803
4804 case COMP_FUNCTION:
4805 *st = ST_END_FUNCTION;
4806 target = " function";
c0985832 4807 eos_ok = !contained_procedure ();
4ee9c684 4808 break;
4809
4810 case COMP_BLOCK_DATA:
4811 *st = ST_END_BLOCK_DATA;
4812 target = " block data";
c0985832 4813 eos_ok = 1;
4ee9c684 4814 break;
4815
4816 case COMP_MODULE:
4817 *st = ST_END_MODULE;
4818 target = " module";
c0985832 4819 eos_ok = 1;
4ee9c684 4820 break;
4821
4822 case COMP_INTERFACE:
4823 *st = ST_END_INTERFACE;
4824 target = " interface";
c0985832 4825 eos_ok = 0;
4ee9c684 4826 break;
4827
4828 case COMP_DERIVED:
4829 *st = ST_END_TYPE;
4830 target = " type";
c0985832 4831 eos_ok = 0;
4ee9c684 4832 break;
4833
4834 case COMP_IF:
4835 *st = ST_ENDIF;
4836 target = " if";
c0985832 4837 eos_ok = 0;
4ee9c684 4838 break;
4839
4840 case COMP_DO:
4841 *st = ST_ENDDO;
4842 target = " do";
c0985832 4843 eos_ok = 0;
4ee9c684 4844 break;
4845
4846 case COMP_SELECT:
4847 *st = ST_END_SELECT;
4848 target = " select";
c0985832 4849 eos_ok = 0;
4ee9c684 4850 break;
4851
4852 case COMP_FORALL:
4853 *st = ST_END_FORALL;
4854 target = " forall";
c0985832 4855 eos_ok = 0;
4ee9c684 4856 break;
4857
4858 case COMP_WHERE:
4859 *st = ST_END_WHERE;
4860 target = " where";
c0985832 4861 eos_ok = 0;
4ee9c684 4862 break;
4863
3b6a4b41 4864 case COMP_ENUM:
4865 *st = ST_END_ENUM;
4866 target = " enum";
4867 eos_ok = 0;
4868 last_initializer = NULL;
4869 set_enum_kind ();
4870 gfc_free_enum_history ();
4871 break;
4872
4ee9c684 4873 default:
4874 gfc_error ("Unexpected END statement at %C");
4875 goto cleanup;
4876 }
4877
4878 if (gfc_match_eos () == MATCH_YES)
4879 {
c0985832 4880 if (!eos_ok)
4ee9c684 4881 {
f6d0e37a 4882 /* We would have required END [something]. */
d197c9ee 4883 gfc_error ("%s statement expected at %L",
4884 gfc_ascii_statement (*st), &old_loc);
4ee9c684 4885 goto cleanup;
4886 }
4887
4888 return MATCH_YES;
4889 }
4890
4891 /* Verify that we've got the sort of end-block that we're expecting. */
4892 if (gfc_match (target) != MATCH_YES)
4893 {
4894 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4895 goto cleanup;
4896 }
4897
4898 /* If we're at the end, make sure a block name wasn't required. */
4899 if (gfc_match_eos () == MATCH_YES)
4900 {
4901
0d0ce415 4902 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4903 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4ee9c684 4904 return MATCH_YES;
4905
4906 if (gfc_current_block () == NULL)
4907 return MATCH_YES;
4908
4909 gfc_error ("Expected block name of '%s' in %s statement at %C",
4910 block_name, gfc_ascii_statement (*st));
4911
4912 return MATCH_ERROR;
4913 }
4914
4915 /* END INTERFACE has a special handler for its several possible endings. */
4916 if (*st == ST_END_INTERFACE)
4917 return gfc_match_end_interface ();
4918
f6d0e37a 4919 /* We haven't hit the end of statement, so what is left must be an
4920 end-name. */
4ee9c684 4921 m = gfc_match_space ();
4922 if (m == MATCH_YES)
4923 m = gfc_match_name (name);
4924
4925 if (m == MATCH_NO)
4926 gfc_error ("Expected terminating name at %C");
4927 if (m != MATCH_YES)
4928 goto cleanup;
4929
4930 if (block_name == NULL)
4931 goto syntax;
4932
4933 if (strcmp (name, block_name) != 0)
4934 {
4935 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4936 gfc_ascii_statement (*st));
4937 goto cleanup;
4938 }
4939
4940 if (gfc_match_eos () == MATCH_YES)
4941 return MATCH_YES;
4942
4943syntax:
4944 gfc_syntax_error (*st);
4945
4946cleanup:
cbb9e6aa 4947 gfc_current_locus = old_loc;
4ee9c684 4948 return MATCH_ERROR;
4949}
4950
4951
4952
4953/***************** Attribute declaration statements ****************/
4954
4955/* Set the attribute of a single variable. */
4956
4957static match
4958attr_decl1 (void)
4959{
4960 char name[GFC_MAX_SYMBOL_LEN + 1];
4961 gfc_array_spec *as;
4962 gfc_symbol *sym;
4963 locus var_locus;
4964 match m;
4965
4966 as = NULL;
4967
4968 m = gfc_match_name (name);
4969 if (m != MATCH_YES)
4970 goto cleanup;
4971
4972 if (find_special (name, &sym))
4973 return MATCH_ERROR;
4974
cbb9e6aa 4975 var_locus = gfc_current_locus;
4ee9c684 4976
4977 /* Deal with possible array specification for certain attributes. */
4978 if (current_attr.dimension
4979 || current_attr.allocatable
4980 || current_attr.pointer
4981 || current_attr.target)
4982 {
4983 m = gfc_match_array_spec (&as);
4984 if (m == MATCH_ERROR)
4985 goto cleanup;
4986
4987 if (current_attr.dimension && m == MATCH_NO)
4988 {
1a9745d2 4989 gfc_error ("Missing array specification at %L in DIMENSION "
4990 "statement", &var_locus);
4ee9c684 4991 m = MATCH_ERROR;
4992 goto cleanup;
4993 }
4994
4995 if ((current_attr.allocatable || current_attr.pointer)
4996 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4997 {
1a9745d2 4998 gfc_error ("Array specification must be deferred at %L", &var_locus);
4ee9c684 4999 m = MATCH_ERROR;
5000 goto cleanup;
5001 }
5002 }
5003
1a9745d2 5004 /* Update symbol table. DIMENSION attribute is set
5005 in gfc_set_array_spec(). */
4ee9c684 5006 if (current_attr.dimension == 0
5007 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
5008 {
5009 m = MATCH_ERROR;
5010 goto cleanup;
5011 }
5012
5013 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5014 {
5015 m = MATCH_ERROR;
5016 goto cleanup;
5017 }
e14bee04 5018
b549d2a5 5019 if (sym->attr.cray_pointee && sym->as != NULL)
5020 {
5021 /* Fix the array spec. */
5022 m = gfc_mod_pointee_as (sym->as);
5023 if (m == MATCH_ERROR)
5024 goto cleanup;
5025 }
4ee9c684 5026
25dd7350 5027 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
14efb9b7 5028 {
5029 m = MATCH_ERROR;
5030 goto cleanup;
5031 }
5032
4ee9c684 5033 if ((current_attr.external || current_attr.intrinsic)
5034 && sym->attr.flavor != FL_PROCEDURE
950683ed 5035 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4ee9c684 5036 {
5037 m = MATCH_ERROR;
5038 goto cleanup;
5039 }
5040
5041 return MATCH_YES;
5042
5043cleanup:
5044 gfc_free_array_spec (as);
5045 return m;
5046}
5047
5048
5049/* Generic attribute declaration subroutine. Used for attributes that
5050 just have a list of names. */
5051
5052static match
5053attr_decl (void)
5054{
5055 match m;
5056
5057 /* Gobble the optional double colon, by simply ignoring the result
5058 of gfc_match(). */
5059 gfc_match (" ::");
5060
5061 for (;;)
5062 {
5063 m = attr_decl1 ();
5064 if (m != MATCH_YES)
5065 break;
5066
5067 if (gfc_match_eos () == MATCH_YES)
5068 {
5069 m = MATCH_YES;
5070 break;
5071 }
5072
5073 if (gfc_match_char (',') != MATCH_YES)
5074 {
5075 gfc_error ("Unexpected character in variable list at %C");
5076 m = MATCH_ERROR;
5077 break;
5078 }
5079 }
5080
5081 return m;
5082}
5083
5084
b549d2a5 5085/* This routine matches Cray Pointer declarations of the form:
5086 pointer ( <pointer>, <pointee> )
5087 or
e14bee04 5088 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5089 The pointer, if already declared, should be an integer. Otherwise, we
b549d2a5 5090 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5091 be either a scalar, or an array declaration. No space is allocated for
e14bee04 5092 the pointee. For the statement
b549d2a5 5093 pointer (ipt, ar(10))
5094 any subsequent uses of ar will be translated (in C-notation) as
e14bee04 5095 ar(i) => ((<type> *) ipt)(i)
b7bf3f81 5096 After gimplification, pointee variable will disappear in the code. */
b549d2a5 5097
5098static match
5099cray_pointer_decl (void)
5100{
5101 match m;
5102 gfc_array_spec *as;
5103 gfc_symbol *cptr; /* Pointer symbol. */
5104 gfc_symbol *cpte; /* Pointee symbol. */
5105 locus var_locus;
5106 bool done = false;
5107
5108 while (!done)
5109 {
5110 if (gfc_match_char ('(') != MATCH_YES)
5111 {
5112 gfc_error ("Expected '(' at %C");
e14bee04 5113 return MATCH_ERROR;
b549d2a5 5114 }
e14bee04 5115
b549d2a5 5116 /* Match pointer. */
5117 var_locus = gfc_current_locus;
5118 gfc_clear_attr (&current_attr);
5119 gfc_add_cray_pointer (&current_attr, &var_locus);
5120 current_ts.type = BT_INTEGER;
5121 current_ts.kind = gfc_index_integer_kind;
5122
e14bee04 5123 m = gfc_match_symbol (&cptr, 0);
b549d2a5 5124 if (m != MATCH_YES)
5125 {
5126 gfc_error ("Expected variable name at %C");
5127 return m;
5128 }
e14bee04 5129
b549d2a5 5130 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5131 return MATCH_ERROR;
5132
e14bee04 5133 gfc_set_sym_referenced (cptr);
b549d2a5 5134
5135 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5136 {
5137 cptr->ts.type = BT_INTEGER;
e14bee04 5138 cptr->ts.kind = gfc_index_integer_kind;
b549d2a5 5139 }
5140 else if (cptr->ts.type != BT_INTEGER)
5141 {
7698a624 5142 gfc_error ("Cray pointer at %C must be an integer");
b549d2a5 5143 return MATCH_ERROR;
5144 }
5145 else if (cptr->ts.kind < gfc_index_integer_kind)
5146 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
7698a624 5147 " memory addresses require %d bytes",
1a9745d2 5148 cptr->ts.kind, gfc_index_integer_kind);
b549d2a5 5149
5150 if (gfc_match_char (',') != MATCH_YES)
5151 {
5152 gfc_error ("Expected \",\" at %C");
e14bee04 5153 return MATCH_ERROR;
b549d2a5 5154 }
5155
e14bee04 5156 /* Match Pointee. */
b549d2a5 5157 var_locus = gfc_current_locus;
5158 gfc_clear_attr (&current_attr);
5159 gfc_add_cray_pointee (&current_attr, &var_locus);
5160 current_ts.type = BT_UNKNOWN;
5161 current_ts.kind = 0;
5162
5163 m = gfc_match_symbol (&cpte, 0);
5164 if (m != MATCH_YES)
5165 {
5166 gfc_error ("Expected variable name at %C");
5167 return m;
5168 }
e14bee04 5169
b549d2a5 5170 /* Check for an optional array spec. */
5171 m = gfc_match_array_spec (&as);
5172 if (m == MATCH_ERROR)
5173 {
5174 gfc_free_array_spec (as);
5175 return m;
5176 }
5177 else if (m == MATCH_NO)
5178 {
5179 gfc_free_array_spec (as);
5180 as = NULL;
5181 }
5182
5183 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5184 return MATCH_ERROR;
5185
5186 gfc_set_sym_referenced (cpte);
5187
5188 if (cpte->as == NULL)
5189 {
5190 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5191 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5192 }
5193 else if (as != NULL)
5194 {
7698a624 5195 gfc_error ("Duplicate array spec for Cray pointee at %C");
b549d2a5 5196 gfc_free_array_spec (as);
5197 return MATCH_ERROR;
5198 }
5199
5200 as = NULL;
5201
5202 if (cpte->as != NULL)
5203 {
5204 /* Fix array spec. */
5205 m = gfc_mod_pointee_as (cpte->as);
5206 if (m == MATCH_ERROR)
5207 return m;
5208 }
5209
5210 /* Point the Pointee at the Pointer. */
b7bf3f81 5211 cpte->cp_pointer = cptr;
b549d2a5 5212
5213 if (gfc_match_char (')') != MATCH_YES)
5214 {
5215 gfc_error ("Expected \")\" at %C");
5216 return MATCH_ERROR;
5217 }
5218 m = gfc_match_char (',');
5219 if (m != MATCH_YES)
5220 done = true; /* Stop searching for more declarations. */
5221
5222 }
5223
5224 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5225 || gfc_match_eos () != MATCH_YES)
5226 {
5227 gfc_error ("Expected \",\" or end of statement at %C");
5228 return MATCH_ERROR;
5229 }
5230 return MATCH_YES;
5231}
5232
5233
4ee9c684 5234match
5235gfc_match_external (void)
5236{
5237
5238 gfc_clear_attr (&current_attr);
14efb9b7 5239 current_attr.external = 1;
4ee9c684 5240
5241 return attr_decl ();
5242}
5243
5244
4ee9c684 5245match
5246gfc_match_intent (void)
5247{
5248 sym_intent intent;
5249
5250 intent = match_intent_spec ();
5251 if (intent == INTENT_UNKNOWN)
5252 return MATCH_ERROR;
5253
5254 gfc_clear_attr (&current_attr);
14efb9b7 5255 current_attr.intent = intent;
4ee9c684 5256
5257 return attr_decl ();
5258}
5259
5260
5261match
5262gfc_match_intrinsic (void)
5263{
5264
5265 gfc_clear_attr (&current_attr);
14efb9b7 5266 current_attr.intrinsic = 1;
4ee9c684 5267
5268 return attr_decl ();
5269}
5270
5271
5272match
5273gfc_match_optional (void)
5274{
5275
5276 gfc_clear_attr (&current_attr);
14efb9b7 5277 current_attr.optional = 1;
4ee9c684 5278
5279 return attr_decl ();
5280}
5281
5282
5283match
5284gfc_match_pointer (void)
5285{
b549d2a5 5286 gfc_gobble_whitespace ();
5287 if (gfc_peek_char () == '(')
5288 {
5289 if (!gfc_option.flag_cray_pointer)
5290 {
1a9745d2 5291 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5292 "flag");
b549d2a5 5293 return MATCH_ERROR;
5294 }
5295 return cray_pointer_decl ();
5296 }
5297 else
5298 {
5299 gfc_clear_attr (&current_attr);
14efb9b7 5300 current_attr.pointer = 1;
b549d2a5 5301
5302 return attr_decl ();
5303 }
4ee9c684 5304}
5305
5306
5307match
5308gfc_match_allocatable (void)
5309{
4ee9c684 5310 gfc_clear_attr (&current_attr);
14efb9b7 5311 current_attr.allocatable = 1;
4ee9c684 5312
5313 return attr_decl ();
5314}
5315
5316
5317match
5318gfc_match_dimension (void)
5319{
4ee9c684 5320 gfc_clear_attr (&current_attr);
14efb9b7 5321 current_attr.dimension = 1;
4ee9c684 5322
5323 return attr_decl ();
5324}
5325
5326
5327match
5328gfc_match_target (void)
5329{
4ee9c684 5330 gfc_clear_attr (&current_attr);
14efb9b7 5331 current_attr.target = 1;
4ee9c684 5332
5333 return attr_decl ();
5334}
5335
5336
5337/* Match the list of entities being specified in a PUBLIC or PRIVATE
5338 statement. */
5339
5340static match
5341access_attr_decl (gfc_statement st)
5342{
5343 char name[GFC_MAX_SYMBOL_LEN + 1];
5344 interface_type type;
5345 gfc_user_op *uop;
5346 gfc_symbol *sym;
5347 gfc_intrinsic_op operator;
5348 match m;
5349
5350 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5351 goto done;
5352
5353 for (;;)
5354 {
5355 m = gfc_match_generic_spec (&type, name, &operator);
5356 if (m == MATCH_NO)
5357 goto syntax;
5358 if (m == MATCH_ERROR)
5359 return MATCH_ERROR;
5360
5361 switch (type)
5362 {
5363 case INTERFACE_NAMELESS:
94fa7146 5364 case INTERFACE_ABSTRACT:
4ee9c684 5365 goto syntax;
5366
5367 case INTERFACE_GENERIC:
5368 if (gfc_get_symbol (name, NULL, &sym))
5369 goto done;
5370
1a9745d2 5371 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5372 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
950683ed 5373 sym->name, NULL) == FAILURE)
4ee9c684 5374 return MATCH_ERROR;
5375
5376 break;
5377
5378 case INTERFACE_INTRINSIC_OP:
5379 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5380 {
5381 gfc_current_ns->operator_access[operator] =
5382 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5383 }
5384 else
5385 {
5386 gfc_error ("Access specification of the %s operator at %C has "
5387 "already been specified", gfc_op2string (operator));
5388 goto done;
5389 }
5390
5391 break;
5392
5393 case INTERFACE_USER_OP:
5394 uop = gfc_get_uop (name);
5395
5396 if (uop->access == ACCESS_UNKNOWN)
5397 {
1a9745d2 5398 uop->access = (st == ST_PUBLIC)
5399 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4ee9c684 5400 }
5401 else
5402 {
1a9745d2 5403 gfc_error ("Access specification of the .%s. operator at %C "
5404 "has already been specified", sym->name);
4ee9c684 5405 goto done;
5406 }
5407
5408 break;
5409 }
5410
5411 if (gfc_match_char (',') == MATCH_NO)
5412 break;
5413 }
5414
5415 if (gfc_match_eos () != MATCH_YES)
5416 goto syntax;
5417 return MATCH_YES;
5418
5419syntax:
5420 gfc_syntax_error (st);
5421
5422done:
5423 return MATCH_ERROR;
5424}
5425
5426
3ea52af3 5427match
5428gfc_match_protected (void)
5429{
5430 gfc_symbol *sym;
5431 match m;
5432
5433 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5434 {
5435 gfc_error ("PROTECTED at %C only allowed in specification "
5436 "part of a module");
5437 return MATCH_ERROR;
5438
5439 }
5440
1a9745d2 5441 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
3ea52af3 5442 == FAILURE)
5443 return MATCH_ERROR;
5444
5445 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5446 {
5447 return MATCH_ERROR;
5448 }
5449
5450 if (gfc_match_eos () == MATCH_YES)
5451 goto syntax;
5452
5453 for(;;)
5454 {
5455 m = gfc_match_symbol (&sym, 0);
5456 switch (m)
5457 {
5458 case MATCH_YES:
1a9745d2 5459 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5460 == FAILURE)
3ea52af3 5461 return MATCH_ERROR;
5462 goto next_item;
5463
5464 case MATCH_NO:
5465 break;
5466
5467 case MATCH_ERROR:
5468 return MATCH_ERROR;
5469 }
5470
5471 next_item:
5472 if (gfc_match_eos () == MATCH_YES)
5473 break;
5474 if (gfc_match_char (',') != MATCH_YES)
5475 goto syntax;
5476 }
5477
5478 return MATCH_YES;
5479
5480syntax:
5481 gfc_error ("Syntax error in PROTECTED statement at %C");
5482 return MATCH_ERROR;
5483}
5484
5485
a0527218 5486/* The PRIVATE statement is a bit weird in that it can be an attribute
4ee9c684 5487 declaration, but also works as a standlone statement inside of a
5488 type declaration or a module. */
5489
5490match
1a9745d2 5491gfc_match_private (gfc_statement *st)
4ee9c684 5492{
5493
5494 if (gfc_match ("private") != MATCH_YES)
5495 return MATCH_NO;
5496
e14bee04 5497 if (gfc_current_state () != COMP_MODULE
5498 && (gfc_current_state () != COMP_DERIVED
5499 || !gfc_state_stack->previous
5500 || gfc_state_stack->previous->state != COMP_MODULE))
5501 {
5502 gfc_error ("PRIVATE statement at %C is only allowed in the "
5503 "specification part of a module");
5504 return MATCH_ERROR;
5505 }
5506
4ee9c684 5507 if (gfc_current_state () == COMP_DERIVED)
5508 {
5509 if (gfc_match_eos () == MATCH_YES)
5510 {
5511 *st = ST_PRIVATE;
5512 return MATCH_YES;
5513 }
5514
5515 gfc_syntax_error (ST_PRIVATE);
5516 return MATCH_ERROR;
5517 }
5518
5519 if (gfc_match_eos () == MATCH_YES)
5520 {
5521 *st = ST_PRIVATE;
5522 return MATCH_YES;
5523 }
5524
5525 *st = ST_ATTR_DECL;
5526 return access_attr_decl (ST_PRIVATE);
5527}
5528
5529
5530match
1a9745d2 5531gfc_match_public (gfc_statement *st)
4ee9c684 5532{
5533
5534 if (gfc_match ("public") != MATCH_YES)
5535 return MATCH_NO;
5536
e14bee04 5537 if (gfc_current_state () != COMP_MODULE)
5538 {
5539 gfc_error ("PUBLIC statement at %C is only allowed in the "
5540 "specification part of a module");
5541 return MATCH_ERROR;
5542 }
5543
4ee9c684 5544 if (gfc_match_eos () == MATCH_YES)
5545 {
5546 *st = ST_PUBLIC;
5547 return MATCH_YES;
5548 }
5549
5550 *st = ST_ATTR_DECL;
5551 return access_attr_decl (ST_PUBLIC);
5552}
5553
5554
5555/* Workhorse for gfc_match_parameter. */
5556
5557static match
5558do_parm (void)
5559{
5560 gfc_symbol *sym;
5561 gfc_expr *init;
5562 match m;
5563
5564 m = gfc_match_symbol (&sym, 0);
5565 if (m == MATCH_NO)
5566 gfc_error ("Expected variable name at %C in PARAMETER statement");
5567
5568 if (m != MATCH_YES)
5569 return m;
5570
5571 if (gfc_match_char ('=') == MATCH_NO)
5572 {
5573 gfc_error ("Expected = sign in PARAMETER statement at %C");
5574 return MATCH_ERROR;
5575 }
5576
5577 m = gfc_match_init_expr (&init);
5578 if (m == MATCH_NO)
5579 gfc_error ("Expected expression at %C in PARAMETER statement");
5580 if (m != MATCH_YES)
5581 return m;
5582
5583 if (sym->ts.type == BT_UNKNOWN
5584 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5585 {
5586 m = MATCH_ERROR;
5587 goto cleanup;
5588 }
5589
5590 if (gfc_check_assign_symbol (sym, init) == FAILURE
950683ed 5591 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4ee9c684 5592 {
5593 m = MATCH_ERROR;
5594 goto cleanup;
5595 }
5596
c1b6da4a 5597 if (sym->ts.type == BT_CHARACTER
5598 && sym->ts.cl != NULL
5599 && sym->ts.cl->length != NULL
5600 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5601 && init->expr_type == EXPR_CONSTANT
5602 && init->ts.type == BT_CHARACTER
5603 && init->ts.kind == 1)
5604 gfc_set_constant_character_len (
1bfea7e8 5605 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
c1b6da4a 5606
4ee9c684 5607 sym->value = init;
5608 return MATCH_YES;
5609
5610cleanup:
5611 gfc_free_expr (init);
5612 return m;
5613}
5614
5615
5616/* Match a parameter statement, with the weird syntax that these have. */
5617
5618match
5619gfc_match_parameter (void)
5620{
5621 match m;
5622
5623 if (gfc_match_char ('(') == MATCH_NO)
5624 return MATCH_NO;
5625
5626 for (;;)
5627 {
5628 m = do_parm ();
5629 if (m != MATCH_YES)
5630 break;
5631
5632 if (gfc_match (" )%t") == MATCH_YES)
5633 break;
5634
5635 if (gfc_match_char (',') != MATCH_YES)
5636 {
5637 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5638 m = MATCH_ERROR;
5639 break;
5640 }
5641 }
5642
5643 return m;
5644}
5645
5646
5647/* Save statements have a special syntax. */
5648
5649match
5650gfc_match_save (void)
5651{
82f5ee13 5652 char n[GFC_MAX_SYMBOL_LEN+1];
5653 gfc_common_head *c;
4ee9c684 5654 gfc_symbol *sym;
5655 match m;
5656
5657 if (gfc_match_eos () == MATCH_YES)
5658 {
5659 if (gfc_current_ns->seen_save)
5660 {
1a9745d2 5661 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5662 "follows previous SAVE statement")
76e82f95 5663 == FAILURE)
5664 return MATCH_ERROR;
4ee9c684 5665 }
5666
5667 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5668 return MATCH_YES;
5669 }
5670
5671 if (gfc_current_ns->save_all)
5672 {
1a9745d2 5673 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5674 "blanket SAVE statement")
76e82f95 5675 == FAILURE)
5676 return MATCH_ERROR;
4ee9c684 5677 }
5678
5679 gfc_match (" ::");
5680
5681 for (;;)
5682 {
5683 m = gfc_match_symbol (&sym, 0);
5684 switch (m)
5685 {
5686 case MATCH_YES:
1a9745d2 5687 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5688 == FAILURE)
4ee9c684 5689 return MATCH_ERROR;
5690 goto next_item;
5691
5692 case MATCH_NO:
5693 break;
5694
5695 case MATCH_ERROR:
5696 return MATCH_ERROR;
5697 }
5698
82f5ee13 5699 m = gfc_match (" / %n /", &n);
4ee9c684 5700 if (m == MATCH_ERROR)
5701 return MATCH_ERROR;
5702 if (m == MATCH_NO)
5703 goto syntax;
5704
403ddc45 5705 c = gfc_get_common (n, 0);
82f5ee13 5706 c->saved = 1;
5707
4ee9c684 5708 gfc_current_ns->seen_save = 1;
5709
5710 next_item:
5711 if (gfc_match_eos () == MATCH_YES)
5712 break;
5713 if (gfc_match_char (',') != MATCH_YES)
5714 goto syntax;
5715 }
5716
5717 return MATCH_YES;
5718
5719syntax:
5720 gfc_error ("Syntax error in SAVE statement at %C");
5721 return MATCH_ERROR;
5722}
5723
5724
8f6339b6 5725match
5726gfc_match_value (void)
5727{
5728 gfc_symbol *sym;
5729 match m;
5730
1a9745d2 5731 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
8f6339b6 5732 == FAILURE)
5733 return MATCH_ERROR;
5734
5735 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5736 {
5737 return MATCH_ERROR;
5738 }
5739
5740 if (gfc_match_eos () == MATCH_YES)
5741 goto syntax;
5742
5743 for(;;)
5744 {
5745 m = gfc_match_symbol (&sym, 0);
5746 switch (m)
5747 {
5748 case MATCH_YES:
1a9745d2 5749 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5750 == FAILURE)
8f6339b6 5751 return MATCH_ERROR;
5752 goto next_item;
5753
5754 case MATCH_NO:
5755 break;
5756
5757 case MATCH_ERROR:
5758 return MATCH_ERROR;
5759 }
5760
5761 next_item:
5762 if (gfc_match_eos () == MATCH_YES)
5763 break;
5764 if (gfc_match_char (',') != MATCH_YES)
5765 goto syntax;
5766 }
5767
5768 return MATCH_YES;
5769
5770syntax:
5771 gfc_error ("Syntax error in VALUE statement at %C");
5772 return MATCH_ERROR;
5773}
5774
f6d0e37a 5775
ef814c81 5776match
5777gfc_match_volatile (void)
5778{
5779 gfc_symbol *sym;
5780 match m;
5781
1a9745d2 5782 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
ef814c81 5783 == FAILURE)
5784 return MATCH_ERROR;
5785
5786 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5787 {
5788 return MATCH_ERROR;
5789 }
5790
5791 if (gfc_match_eos () == MATCH_YES)
5792 goto syntax;
5793
5794 for(;;)
5795 {
2f241857 5796 /* VOLATILE is special because it can be added to host-associated
5797 symbols locally. */
5798 m = gfc_match_symbol (&sym, 1);
ef814c81 5799 switch (m)
5800 {
5801 case MATCH_YES:
1a9745d2 5802 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5803 == FAILURE)
ef814c81 5804 return MATCH_ERROR;
5805 goto next_item;
5806
5807 case MATCH_NO:
5808 break;
5809
5810 case MATCH_ERROR:
5811 return MATCH_ERROR;
5812 }
5813
5814 next_item:
5815 if (gfc_match_eos () == MATCH_YES)
5816 break;
5817 if (gfc_match_char (',') != MATCH_YES)
5818 goto syntax;
5819 }
5820
5821 return MATCH_YES;
5822
5823syntax:
5824 gfc_error ("Syntax error in VOLATILE statement at %C");
5825 return MATCH_ERROR;
5826}
5827
5828
4ee9c684 5829/* Match a module procedure statement. Note that we have to modify
5830 symbols in the parent's namespace because the current one was there
89d91d02 5831 to receive symbols that are in an interface's formal argument list. */
4ee9c684 5832
5833match
5834gfc_match_modproc (void)
5835{
5836 char name[GFC_MAX_SYMBOL_LEN + 1];
5837 gfc_symbol *sym;
5838 match m;
63d42079 5839 gfc_namespace *module_ns;
4ee9c684 5840
5841 if (gfc_state_stack->state != COMP_INTERFACE
5842 || gfc_state_stack->previous == NULL
5cf92482 5843 || current_interface.type == INTERFACE_NAMELESS
5844 || current_interface.type == INTERFACE_ABSTRACT)
4ee9c684 5845 {
1a9745d2 5846 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5847 "interface");
4ee9c684 5848 return MATCH_ERROR;
5849 }
5850
63d42079 5851 module_ns = gfc_current_ns->parent;
5852 for (; module_ns; module_ns = module_ns->parent)
5853 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5854 break;
5855
5856 if (module_ns == NULL)
5857 return MATCH_ERROR;
5858
4ee9c684 5859 for (;;)
5860 {
5861 m = gfc_match_name (name);
5862 if (m == MATCH_NO)
5863 goto syntax;
5864 if (m != MATCH_YES)
5865 return MATCH_ERROR;
5866
63d42079 5867 if (gfc_get_symbol (name, module_ns, &sym))
4ee9c684 5868 return MATCH_ERROR;
5869
5870 if (sym->attr.proc != PROC_MODULE
950683ed 5871 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5872 sym->name, NULL) == FAILURE)
4ee9c684 5873 return MATCH_ERROR;
5874
5875 if (gfc_add_interface (sym) == FAILURE)
5876 return MATCH_ERROR;
5877
3186f695 5878 sym->attr.mod_proc = 1;
5879
4ee9c684 5880 if (gfc_match_eos () == MATCH_YES)
5881 break;
5882 if (gfc_match_char (',') != MATCH_YES)
5883 goto syntax;
5884 }
5885
5886 return MATCH_YES;
5887
5888syntax:
5889 gfc_syntax_error (ST_MODULE_PROC);
5890 return MATCH_ERROR;
5891}
5892
5893
c5d33754 5894/* Match the optional attribute specifiers for a type declaration.
5895 Return MATCH_ERROR if an error is encountered in one of the handled
5896 attributes (public, private, bind(c)), MATCH_NO if what's found is
5897 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5898 checking on attribute conflicts needs to be done. */
4ee9c684 5899
5900match
c5d33754 5901gfc_get_type_attr_spec (symbol_attribute *attr)
4ee9c684 5902{
c5d33754 5903 /* See if the derived type is marked as private. */
4ee9c684 5904 if (gfc_match (" , private") == MATCH_YES)
5905 {
e14bee04 5906 if (gfc_current_state () != COMP_MODULE)
4ee9c684 5907 {
e14bee04 5908 gfc_error ("Derived type at %C can only be PRIVATE in the "
5909 "specification part of a module");
4ee9c684 5910 return MATCH_ERROR;
5911 }
5912
c5d33754 5913 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4ee9c684 5914 return MATCH_ERROR;
4ee9c684 5915 }
c5d33754 5916 else if (gfc_match (" , public") == MATCH_YES)
4ee9c684 5917 {
e14bee04 5918 if (gfc_current_state () != COMP_MODULE)
4ee9c684 5919 {
e14bee04 5920 gfc_error ("Derived type at %C can only be PUBLIC in the "
5921 "specification part of a module");
4ee9c684 5922 return MATCH_ERROR;
5923 }
5924
c5d33754 5925 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4ee9c684 5926 return MATCH_ERROR;
4ee9c684 5927 }
33e86520 5928 else if (gfc_match(" , bind ( c )") == MATCH_YES)
c5d33754 5929 {
5930 /* If the type is defined to be bind(c) it then needs to make
5931 sure that all fields are interoperable. This will
5932 need to be a semantic check on the finished derived type.
5933 See 15.2.3 (lines 9-12) of F2003 draft. */
5934 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5935 return MATCH_ERROR;
5936
5937 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5938 }
5939 else
5940 return MATCH_NO;
5941
5942 /* If we get here, something matched. */
5943 return MATCH_YES;
5944}
5945
5946
5947/* Match the beginning of a derived type declaration. If a type name
5948 was the result of a function, then it is possible to have a symbol
5949 already to be known as a derived type yet have no components. */
5950
5951match
5952gfc_match_derived_decl (void)
5953{
5954 char name[GFC_MAX_SYMBOL_LEN + 1];
5955 symbol_attribute attr;
5956 gfc_symbol *sym;
5957 match m;
5958 match is_type_attr_spec = MATCH_NO;
33e86520 5959 bool seen_attr = false;
c5d33754 5960
5961 if (gfc_current_state () == COMP_DERIVED)
5962 return MATCH_NO;
5963
5964 gfc_clear_attr (&attr);
5965
5966 do
5967 {
5968 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5969 if (is_type_attr_spec == MATCH_ERROR)
5970 return MATCH_ERROR;
33e86520 5971 if (is_type_attr_spec == MATCH_YES)
5972 seen_attr = true;
c5d33754 5973 } while (is_type_attr_spec == MATCH_YES);
4ee9c684 5974
33e86520 5975 if (gfc_match (" ::") != MATCH_YES && seen_attr)
4ee9c684 5976 {
5977 gfc_error ("Expected :: in TYPE definition at %C");
5978 return MATCH_ERROR;
5979 }
5980
5981 m = gfc_match (" %n%t", name);
5982 if (m != MATCH_YES)
5983 return m;
5984
a3055431 5985 /* Make sure the name is not the name of an intrinsic type. */
5986 if (gfc_is_intrinsic_typename (name))
4ee9c684 5987 {
1a9745d2 5988 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5989 "type", name);
4ee9c684 5990 return MATCH_ERROR;
5991 }
5992
5993 if (gfc_get_symbol (name, NULL, &sym))
5994 return MATCH_ERROR;
5995
5996 if (sym->ts.type != BT_UNKNOWN)
5997 {
5998 gfc_error ("Derived type name '%s' at %C already has a basic type "
5999 "of %s", sym->name, gfc_typename (&sym->ts));
6000 return MATCH_ERROR;
6001 }
6002
6003 /* The symbol may already have the derived attribute without the
6004 components. The ways this can happen is via a function
6005 definition, an INTRINSIC statement or a subtype in another
6006 derived type that is a pointer. The first part of the AND clause
b14e2757 6007 is true if a the symbol is not the return value of a function. */
4ee9c684 6008 if (sym->attr.flavor != FL_DERIVED
950683ed 6009 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4ee9c684 6010 return MATCH_ERROR;
6011
e6b82afc 6012 if (sym->components != NULL || sym->attr.zero_comp)
4ee9c684 6013 {
1a9745d2 6014 gfc_error ("Derived type definition of '%s' at %C has already been "
6015 "defined", sym->name);
4ee9c684 6016 return MATCH_ERROR;
6017 }
6018
6019 if (attr.access != ACCESS_UNKNOWN
950683ed 6020 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4ee9c684 6021 return MATCH_ERROR;
6022
c5d33754 6023 /* See if the derived type was labeled as bind(c). */
6024 if (attr.is_bind_c != 0)
6025 sym->attr.is_bind_c = attr.is_bind_c;
6026
4ee9c684 6027 gfc_new_block = sym;
6028
6029 return MATCH_YES;
6030}
b549d2a5 6031
6032
6033/* Cray Pointees can be declared as:
6034 pointer (ipt, a (n,m,...,*))
6035 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6036 cheat and set a constant bound of 1 for the last dimension, if this
6037 is the case. Since there is no bounds-checking for Cray Pointees,
6038 this will be okay. */
6039
6040try
6041gfc_mod_pointee_as (gfc_array_spec *as)
6042{
6043 as->cray_pointee = true; /* This will be useful to know later. */
6044 if (as->type == AS_ASSUMED_SIZE)
6045 {
6046 as->type = AS_EXPLICIT;
6047 as->upper[as->rank - 1] = gfc_int_expr (1);
6048 as->cp_was_assumed = true;
6049 }
6050 else if (as->type == AS_ASSUMED_SHAPE)
6051 {
6052 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6053 return MATCH_ERROR;
6054 }
6055 return MATCH_YES;
6056}
3b6a4b41 6057
6058
6059/* Match the enum definition statement, here we are trying to match
6060 the first line of enum definition statement.
6061 Returns MATCH_YES if match is found. */
6062
6063match
6064gfc_match_enum (void)
6065{
6066 match m;
6067
6068 m = gfc_match_eos ();
6069 if (m != MATCH_YES)
6070 return m;
6071
60fbbf9e 6072 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
3b6a4b41 6073 == FAILURE)
6074 return MATCH_ERROR;
6075
6076 return MATCH_YES;
6077}
6078
6079
60fbbf9e 6080/* Match a variable name with an optional initializer. When this
6081 subroutine is called, a variable is expected to be parsed next.
6082 Depending on what is happening at the moment, updates either the
6083 symbol table or the current interface. */
6084
6085static match
6086enumerator_decl (void)
6087{
6088 char name[GFC_MAX_SYMBOL_LEN + 1];
6089 gfc_expr *initializer;
6090 gfc_array_spec *as = NULL;
6091 gfc_symbol *sym;
6092 locus var_locus;
6093 match m;
6094 try t;
6095 locus old_locus;
6096
6097 initializer = NULL;
6098 old_locus = gfc_current_locus;
6099
6100 /* When we get here, we've just matched a list of attributes and
6101 maybe a type and a double colon. The next thing we expect to see
6102 is the name of the symbol. */
6103 m = gfc_match_name (name);
6104 if (m != MATCH_YES)
6105 goto cleanup;
6106
6107 var_locus = gfc_current_locus;
6108
6109 /* OK, we've successfully matched the declaration. Now put the
6110 symbol in the current namespace. If we fail to create the symbol,
6111 bail out. */
6112 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6113 {
6114 m = MATCH_ERROR;
6115 goto cleanup;
6116 }
6117
6118 /* The double colon must be present in order to have initializers.
6119 Otherwise the statement is ambiguous with an assignment statement. */
6120 if (colon_seen)
6121 {
6122 if (gfc_match_char ('=') == MATCH_YES)
6123 {
6124 m = gfc_match_init_expr (&initializer);
6125 if (m == MATCH_NO)
6126 {
6127 gfc_error ("Expected an initialization expression at %C");
6128 m = MATCH_ERROR;
6129 }
6130
6131 if (m != MATCH_YES)
6132 goto cleanup;
6133 }
6134 }
6135
6136 /* If we do not have an initializer, the initialization value of the
6137 previous enumerator (stored in last_initializer) is incremented
6138 by 1 and is used to initialize the current enumerator. */
6139 if (initializer == NULL)
6140 initializer = gfc_enum_initializer (last_initializer, old_locus);
e14bee04 6141
60fbbf9e 6142 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6143 {
6144 gfc_error("ENUMERATOR %L not initialized with integer expression",
6145 &var_locus);
e14bee04 6146 m = MATCH_ERROR;
60fbbf9e 6147 gfc_free_enum_history ();
6148 goto cleanup;
6149 }
6150
6151 /* Store this current initializer, for the next enumerator variable
6152 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6153 use last_initializer below. */
6154 last_initializer = initializer;
6155 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6156
6157 /* Maintain enumerator history. */
6158 gfc_find_symbol (name, NULL, 0, &sym);
6159 create_enum_history (sym, last_initializer);
6160
6161 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6162
6163cleanup:
6164 /* Free stuff up and return. */
6165 gfc_free_expr (initializer);
6166
6167 return m;
6168}
6169
6170
f6d0e37a 6171/* Match the enumerator definition statement. */
3b6a4b41 6172
6173match
6174gfc_match_enumerator_def (void)
6175{
6176 match m;
60fbbf9e 6177 try t;
e14bee04 6178
3b6a4b41 6179 gfc_clear_ts (&current_ts);
e14bee04 6180
3b6a4b41 6181 m = gfc_match (" enumerator");
6182 if (m != MATCH_YES)
6183 return m;
60fbbf9e 6184
6185 m = gfc_match (" :: ");
6186 if (m == MATCH_ERROR)
6187 return m;
6188
6189 colon_seen = (m == MATCH_YES);
e14bee04 6190
3b6a4b41 6191 if (gfc_current_state () != COMP_ENUM)
6192 {
6193 gfc_error ("ENUM definition statement expected before %C");
6194 gfc_free_enum_history ();
6195 return MATCH_ERROR;
6196 }
6197
6198 (&current_ts)->type = BT_INTEGER;
6199 (&current_ts)->kind = gfc_c_int_kind;
e14bee04 6200
60fbbf9e 6201 gfc_clear_attr (&current_attr);
6202 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6203 if (t == FAILURE)
3b6a4b41 6204 {
60fbbf9e 6205 m = MATCH_ERROR;
3b6a4b41 6206 goto cleanup;
6207 }
6208
3b6a4b41 6209 for (;;)
6210 {
60fbbf9e 6211 m = enumerator_decl ();
3b6a4b41 6212 if (m == MATCH_ERROR)
6213 goto cleanup;
6214 if (m == MATCH_NO)
6215 break;
6216
6217 if (gfc_match_eos () == MATCH_YES)
6218 goto cleanup;
6219 if (gfc_match_char (',') != MATCH_YES)
6220 break;
6221 }
6222
6223 if (gfc_current_state () == COMP_ENUM)
6224 {
6225 gfc_free_enum_history ();
6226 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6227 m = MATCH_ERROR;
6228 }
6229
6230cleanup:
6231 gfc_free_array_spec (current_as);
6232 current_as = NULL;
6233 return m;
6234
6235}
6236