]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
2009-08-10 Richard Guenther <rguenther@suse.de>
[thirdparty/gcc.git] / gcc / fortran / decl.c
CommitLineData
4ee9c684 1/* Declaration statement matcher
8458f4ca 2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
1a9745d2 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"
6f092e31 27#include "flags.h"
4ee9c684 28
cbbac028 29
30/* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
48d8ad5a 32#define gfc_get_data_variable() XCNEW (gfc_data_variable)
33#define gfc_get_data_value() XCNEW (gfc_data_value)
34#define gfc_get_data() XCNEW (gfc_data)
cbbac028 35
36
36ae04f2 37/* This flag is set if an old-style length selector is matched
4ee9c684 38 during a type-declaration statement. */
39
40static int old_char_selector;
41
5739e54e 42/* When variables acquire types and attributes from a declaration
4ee9c684 43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
46
47static gfc_typespec current_ts;
48
49static symbol_attribute current_attr;
50static gfc_array_spec *current_as;
51static int colon_seen;
52
c5d33754 53/* The current binding label (if any). */
54static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55/* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57static int num_idents_on_line;
58/* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60static int has_name_equals = 0;
61
3b6a4b41 62/* Initializer of the previous enumerator. */
63
64static gfc_expr *last_initializer;
65
66/* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
69
70typedef struct enumerator_history
71{
72 gfc_symbol *sym;
73 gfc_expr *initializer;
74 struct enumerator_history *next;
75}
76enumerator_history;
77
78/* Header of enum history chain. */
79
80static enumerator_history *enum_history = NULL;
81
82/* Pointer of enum history node containing largest initializer. */
83
84static enumerator_history *max_enum = NULL;
85
4ee9c684 86/* gfc_new_block points to the symbol of a newly matched block. */
87
88gfc_symbol *gfc_new_block;
89
077932f9 90bool gfc_matching_function;
67a51c8e 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
c1977dbe 235var_element (gfc_data_variable *new_var)
b4f45d02 236{
237 match m;
238 gfc_symbol *sym;
239
c1977dbe 240 memset (new_var, 0, sizeof (gfc_data_variable));
b4f45d02 241
242 if (gfc_match_char ('(') == MATCH_YES)
c1977dbe 243 return var_list (new_var);
b4f45d02 244
c1977dbe 245 m = gfc_match_variable (&new_var->expr, 0);
b4f45d02 246 if (m != MATCH_YES)
247 return m;
248
c1977dbe 249 sym = new_var->expr->symtree->n.sym;
b4f45d02 250
40de255b 251 /* Symbol should already have an associated type. */
252 if (gfc_check_symbol_typed (sym, gfc_current_ns,
253 false, gfc_current_locus) == FAILURE)
254 return MATCH_ERROR;
255
1a9745d2 256 if (!sym->attr.function && gfc_current_ns->parent
257 && gfc_current_ns->parent == sym->ns)
b4f45d02 258 {
c8df3e9c 259 gfc_error ("Host associated variable '%s' may not be in the DATA "
7698a624 260 "statement at %C", sym->name);
b4f45d02 261 return MATCH_ERROR;
262 }
263
c8df3e9c 264 if (gfc_current_state () != COMP_BLOCK_DATA
1a9745d2 265 && sym->attr.in_common
266 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
267 "common block variable '%s' in DATA statement at %C",
268 sym->name) == FAILURE)
c8df3e9c 269 return MATCH_ERROR;
b4f45d02 270
c1977dbe 271 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
b4f45d02 272 return MATCH_ERROR;
273
274 return MATCH_YES;
275}
276
277
278/* Match the top-level list of data variables. */
279
280static match
1a9745d2 281top_var_list (gfc_data *d)
b4f45d02 282{
c1977dbe 283 gfc_data_variable var, *tail, *new_var;
b4f45d02 284 match m;
285
286 tail = NULL;
287
288 for (;;)
289 {
290 m = var_element (&var);
291 if (m == MATCH_NO)
292 goto syntax;
293 if (m == MATCH_ERROR)
294 return MATCH_ERROR;
295
c1977dbe 296 new_var = gfc_get_data_variable ();
297 *new_var = var;
b4f45d02 298
299 if (tail == NULL)
c1977dbe 300 d->var = new_var;
b4f45d02 301 else
c1977dbe 302 tail->next = new_var;
b4f45d02 303
c1977dbe 304 tail = new_var;
b4f45d02 305
306 if (gfc_match_char ('/') == MATCH_YES)
307 break;
308 if (gfc_match_char (',') != MATCH_YES)
309 goto syntax;
310 }
311
312 return MATCH_YES;
313
314syntax:
315 gfc_syntax_error (ST_DATA);
af29c1f0 316 gfc_free_data_all (gfc_current_ns);
b4f45d02 317 return MATCH_ERROR;
318}
319
320
321static match
1a9745d2 322match_data_constant (gfc_expr **result)
b4f45d02 323{
324 char name[GFC_MAX_SYMBOL_LEN + 1];
325 gfc_symbol *sym;
326 gfc_expr *expr;
327 match m;
096d4ad9 328 locus old_loc;
b4f45d02 329
330 m = gfc_match_literal_constant (&expr, 1);
331 if (m == MATCH_YES)
332 {
333 *result = expr;
334 return MATCH_YES;
335 }
336
337 if (m == MATCH_ERROR)
338 return MATCH_ERROR;
339
340 m = gfc_match_null (result);
341 if (m != MATCH_NO)
342 return m;
343
096d4ad9 344 old_loc = gfc_current_locus;
345
346 /* Should this be a structure component, try to match it
347 before matching a name. */
348 m = gfc_match_rvalue (result);
349 if (m == MATCH_ERROR)
350 return m;
351
352 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
353 {
354 if (gfc_simplify_expr (*result, 0) == FAILURE)
355 m = MATCH_ERROR;
356 return m;
357 }
358
359 gfc_current_locus = old_loc;
360
b4f45d02 361 m = gfc_match_name (name);
362 if (m != MATCH_YES)
363 return m;
364
365 if (gfc_find_symbol (name, NULL, 1, &sym))
366 return MATCH_ERROR;
367
368 if (sym == NULL
369 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
370 {
371 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
372 name);
373 return MATCH_ERROR;
374 }
375 else if (sym->attr.flavor == FL_DERIVED)
ea94d76d 376 return gfc_match_structure_constructor (sym, result, false);
b4f45d02 377
f0a51891 378 /* Check to see if the value is an initialization array expression. */
379 if (sym->value->expr_type == EXPR_ARRAY)
380 {
381 gfc_current_locus = old_loc;
382
383 m = gfc_match_init_expr (result);
384 if (m == MATCH_ERROR)
385 return m;
386
387 if (m == MATCH_YES)
388 {
389 if (gfc_simplify_expr (*result, 0) == FAILURE)
390 m = MATCH_ERROR;
391
392 if ((*result)->expr_type == EXPR_CONSTANT)
393 return m;
394 else
395 {
396 gfc_error ("Invalid initializer %s in Data statement at %C", name);
397 return MATCH_ERROR;
398 }
399 }
400 }
401
b4f45d02 402 *result = gfc_copy_expr (sym->value);
403 return MATCH_YES;
404}
405
406
407/* Match a list of values in a DATA statement. The leading '/' has
408 already been seen at this point. */
409
410static match
1a9745d2 411top_val_list (gfc_data *data)
b4f45d02 412{
c1977dbe 413 gfc_data_value *new_val, *tail;
b4f45d02 414 gfc_expr *expr;
b4f45d02 415 match m;
416
417 tail = NULL;
418
419 for (;;)
420 {
421 m = match_data_constant (&expr);
422 if (m == MATCH_NO)
423 goto syntax;
424 if (m == MATCH_ERROR)
425 return MATCH_ERROR;
426
c1977dbe 427 new_val = gfc_get_data_value ();
428 mpz_init (new_val->repeat);
b4f45d02 429
430 if (tail == NULL)
c1977dbe 431 data->value = new_val;
b4f45d02 432 else
c1977dbe 433 tail->next = new_val;
b4f45d02 434
c1977dbe 435 tail = new_val;
b4f45d02 436
437 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
438 {
439 tail->expr = expr;
7d74abfd 440 mpz_set_ui (tail->repeat, 1);
b4f45d02 441 }
442 else
443 {
7d74abfd 444 if (expr->ts.type == BT_INTEGER)
445 mpz_set (tail->repeat, expr->value.integer);
b4f45d02 446 gfc_free_expr (expr);
b4f45d02 447
448 m = match_data_constant (&tail->expr);
449 if (m == MATCH_NO)
450 goto syntax;
451 if (m == MATCH_ERROR)
452 return MATCH_ERROR;
453 }
454
455 if (gfc_match_char ('/') == MATCH_YES)
456 break;
457 if (gfc_match_char (',') == MATCH_NO)
458 goto syntax;
459 }
460
461 return MATCH_YES;
462
463syntax:
464 gfc_syntax_error (ST_DATA);
af29c1f0 465 gfc_free_data_all (gfc_current_ns);
b4f45d02 466 return MATCH_ERROR;
467}
468
469
470/* Matches an old style initialization. */
471
472static match
473match_old_style_init (const char *name)
474{
475 match m;
476 gfc_symtree *st;
344b937e 477 gfc_symbol *sym;
b4f45d02 478 gfc_data *newdata;
479
480 /* Set up data structure to hold initializers. */
481 gfc_find_sym_tree (name, NULL, 0, &st);
344b937e 482 sym = st->n.sym;
483
b4f45d02 484 newdata = gfc_get_data ();
485 newdata->var = gfc_get_data_variable ();
486 newdata->var->expr = gfc_get_variable_expr (st);
5aed5db3 487 newdata->where = gfc_current_locus;
b4f45d02 488
f6d0e37a 489 /* Match initial value list. This also eats the terminal '/'. */
b4f45d02 490 m = top_val_list (newdata);
491 if (m != MATCH_YES)
492 {
493 gfc_free (newdata);
494 return m;
495 }
496
497 if (gfc_pure (NULL))
498 {
499 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
500 gfc_free (newdata);
501 return MATCH_ERROR;
502 }
503
344b937e 504 /* Mark the variable as having appeared in a data statement. */
505 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
506 {
507 gfc_free (newdata);
508 return MATCH_ERROR;
509 }
510
b4f45d02 511 /* Chain in namespace list of DATA initializers. */
512 newdata->next = gfc_current_ns->data;
513 gfc_current_ns->data = newdata;
514
515 return m;
516}
517
1a9745d2 518
b4f45d02 519/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
39fca56b 520 we are matching a DATA statement and are therefore issuing an error
e14bee04 521 if we encounter something unexpected, if not, we're trying to match
fe06c0d5 522 an old-style initialization expression of the form INTEGER I /2/. */
b4f45d02 523
524match
525gfc_match_data (void)
526{
c1977dbe 527 gfc_data *new_data;
b4f45d02 528 match m;
529
cbbac028 530 set_in_match_data (true);
1bfea7e8 531
b4f45d02 532 for (;;)
533 {
c1977dbe 534 new_data = gfc_get_data ();
535 new_data->where = gfc_current_locus;
b4f45d02 536
c1977dbe 537 m = top_var_list (new_data);
b4f45d02 538 if (m != MATCH_YES)
539 goto cleanup;
540
c1977dbe 541 m = top_val_list (new_data);
b4f45d02 542 if (m != MATCH_YES)
543 goto cleanup;
544
c1977dbe 545 new_data->next = gfc_current_ns->data;
546 gfc_current_ns->data = new_data;
b4f45d02 547
548 if (gfc_match_eos () == MATCH_YES)
549 break;
550
551 gfc_match_char (','); /* Optional comma */
552 }
553
cbbac028 554 set_in_match_data (false);
1bfea7e8 555
b4f45d02 556 if (gfc_pure (NULL))
557 {
558 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
559 return MATCH_ERROR;
560 }
561
562 return MATCH_YES;
563
564cleanup:
cbbac028 565 set_in_match_data (false);
c1977dbe 566 gfc_free_data (new_data);
b4f45d02 567 return MATCH_ERROR;
568}
569
570
571/************************ Declaration statements *********************/
572
4ee9c684 573/* Match an intent specification. Since this can only happen after an
574 INTENT word, a legal intent-spec must follow. */
575
576static sym_intent
577match_intent_spec (void)
578{
579
580 if (gfc_match (" ( in out )") == MATCH_YES)
581 return INTENT_INOUT;
582 if (gfc_match (" ( in )") == MATCH_YES)
583 return INTENT_IN;
584 if (gfc_match (" ( out )") == MATCH_YES)
585 return INTENT_OUT;
586
587 gfc_error ("Bad INTENT specification at %C");
588 return INTENT_UNKNOWN;
589}
590
591
592/* Matches a character length specification, which is either a
593 specification expression or a '*'. */
594
595static match
1a9745d2 596char_len_param_value (gfc_expr **expr)
4ee9c684 597{
294d58d3 598 match m;
599
4ee9c684 600 if (gfc_match_char ('*') == MATCH_YES)
601 {
602 *expr = NULL;
603 return MATCH_YES;
604 }
605
294d58d3 606 m = gfc_match_expr (expr);
40de255b 607
608 if (m == MATCH_YES
609 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
610 return MATCH_ERROR;
611
294d58d3 612 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
613 {
614 if ((*expr)->value.function.actual
615 && (*expr)->value.function.actual->expr->symtree)
616 {
617 gfc_expr *e;
618 e = (*expr)->value.function.actual->expr;
619 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
620 && e->expr_type == EXPR_VARIABLE)
621 {
622 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
623 goto syntax;
624 if (e->symtree->n.sym->ts.type == BT_CHARACTER
625 && e->symtree->n.sym->ts.cl
626 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
627 goto syntax;
628 }
629 }
630 }
631 return m;
632
633syntax:
634 gfc_error ("Conflict in attributes of function argument at %C");
635 return MATCH_ERROR;
4ee9c684 636}
637
638
639/* A character length is a '*' followed by a literal integer or a
640 char_len_param_value in parenthesis. */
641
642static match
1a9745d2 643match_char_length (gfc_expr **expr)
4ee9c684 644{
3bd3b616 645 int length;
4ee9c684 646 match m;
647
648 m = gfc_match_char ('*');
649 if (m != MATCH_YES)
650 return m;
651
3bd3b616 652 m = gfc_match_small_literal_int (&length, NULL);
4ee9c684 653 if (m == MATCH_ERROR)
654 return m;
655
656 if (m == MATCH_YES)
657 {
8e9b41f0 658 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
659 "Old-style character length at %C") == FAILURE)
660 return MATCH_ERROR;
4ee9c684 661 *expr = gfc_int_expr (length);
662 return m;
663 }
664
665 if (gfc_match_char ('(') == MATCH_NO)
666 goto syntax;
667
668 m = char_len_param_value (expr);
077932f9 669 if (m != MATCH_YES && gfc_matching_function)
670 {
671 gfc_undo_symbols ();
672 m = MATCH_YES;
673 }
674
4ee9c684 675 if (m == MATCH_ERROR)
676 return m;
677 if (m == MATCH_NO)
678 goto syntax;
679
680 if (gfc_match_char (')') == MATCH_NO)
681 {
682 gfc_free_expr (*expr);
683 *expr = NULL;
684 goto syntax;
685 }
686
687 return MATCH_YES;
688
689syntax:
690 gfc_error ("Syntax error in character length specification at %C");
691 return MATCH_ERROR;
692}
693
694
ee893be6 695/* Special subroutine for finding a symbol. Check if the name is found
696 in the current name space. If not, and we're compiling a function or
697 subroutine and the parent compilation unit is an interface, then check
698 to see if the name we've been given is the name of the interface
699 (located in another namespace). */
4ee9c684 700
701static int
36b0a1b0 702find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
4ee9c684 703{
704 gfc_state_data *s;
36b0a1b0 705 gfc_symtree *st;
ee893be6 706 int i;
4ee9c684 707
36b0a1b0 708 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
e14bee04 709 if (i == 0)
36b0a1b0 710 {
711 *result = st ? st->n.sym : NULL;
712 goto end;
713 }
e14bee04 714
4ee9c684 715 if (gfc_current_state () != COMP_SUBROUTINE
716 && gfc_current_state () != COMP_FUNCTION)
ee893be6 717 goto end;
4ee9c684 718
719 s = gfc_state_stack->previous;
720 if (s == NULL)
ee893be6 721 goto end;
4ee9c684 722
723 if (s->state != COMP_INTERFACE)
ee893be6 724 goto end;
4ee9c684 725 if (s->sym == NULL)
f6d0e37a 726 goto end; /* Nameless interface. */
4ee9c684 727
728 if (strcmp (name, s->sym->name) == 0)
729 {
730 *result = s->sym;
731 return 0;
732 }
733
ee893be6 734end:
735 return i;
4ee9c684 736}
737
738
739/* Special subroutine for getting a symbol node associated with a
740 procedure name, used in SUBROUTINE and FUNCTION statements. The
741 symbol is created in the parent using with symtree node in the
742 child unit pointing to the symbol. If the current namespace has no
743 parent, then the symbol is just created in the current unit. */
744
745static int
1a9745d2 746get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
4ee9c684 747{
748 gfc_symtree *st;
749 gfc_symbol *sym;
2ddb8ed9 750 int rc = 0;
4ee9c684 751
d77f260f 752 /* Module functions have to be left in their own namespace because
753 they have potentially (almost certainly!) already been referenced.
754 In this sense, they are rather like external functions. This is
755 fixed up in resolve.c(resolve_entries), where the symbol name-
756 space is set to point to the master function, so that the fake
757 result mechanism can work. */
758 if (module_fcn_entry)
861d824f 759 {
760 /* Present if entry is declared to be a module procedure. */
761 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
c6a05992 762
861d824f 763 if (*result == NULL)
764 rc = gfc_get_symbol (name, NULL, result);
a78f714d 765 else if (!gfc_get_symbol (name, NULL, &sym) && sym
c6a05992 766 && (*result)->ts.type == BT_UNKNOWN
767 && sym->attr.flavor == FL_UNKNOWN)
768 /* Pick up the typespec for the entry, if declared in the function
769 body. Note that this symbol is FL_UNKNOWN because it will
770 only have appeared in a type declaration. The local symtree
771 is set to point to the module symbol and a unique symtree
772 to the local version. This latter ensures a correct clearing
773 of the symbols. */
a78f714d 774 {
775 /* If the ENTRY proceeds its specification, we need to ensure
776 that this does not raise a "has no IMPLICIT type" error. */
777 if (sym->ts.type == BT_UNKNOWN)
2168078b 778 sym->attr.untyped = 1;
a78f714d 779
2168078b 780 (*result)->ts = sym->ts;
a78f714d 781
782 /* Put the symbol in the procedure namespace so that, should
69b1505f 783 the ENTRY precede its specification, the specification
a78f714d 784 can be applied. */
785 (*result)->ns = gfc_current_ns;
786
787 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
788 st->n.sym = *result;
789 st = gfc_get_unique_symtree (gfc_current_ns);
790 st->n.sym = sym;
791 }
861d824f 792 }
858f9894 793 else
794 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
4ee9c684 795
2ddb8ed9 796 if (rc)
797 return rc;
798
858f9894 799 sym = *result;
c717e399 800 gfc_current_ns->refs++;
4ee9c684 801
c1977dbe 802 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
858f9894 803 {
16f49153 804 /* Trap another encompassed procedure with the same name. All
805 these conditions are necessary to avoid picking up an entry
806 whose name clashes with that of the encompassing procedure;
807 this is handled using gsymbols to register unique,globally
808 accessible names. */
858f9894 809 if (sym->attr.flavor != 0
1a9745d2 810 && sym->attr.proc != 0
811 && (sym->attr.subroutine || sym->attr.function)
812 && sym->attr.if_source != IFSRC_UNKNOWN)
858f9894 813 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
814 name, &sym->declared_at);
815
5720fd2f 816 /* Trap a procedure with a name the same as interface in the
817 encompassing scope. */
818 if (sym->attr.generic != 0
db697236 819 && (sym->attr.subroutine || sym->attr.function)
820 && !sym->attr.mod_proc)
5720fd2f 821 gfc_error_now ("Name '%s' at %C is already defined"
822 " as a generic interface at %L",
823 name, &sym->declared_at);
824
858f9894 825 /* Trap declarations of attributes in encompassing scope. The
826 signature for this is that ts.kind is set. Legitimate
827 references only set ts.type. */
828 if (sym->ts.kind != 0
1a9745d2 829 && !sym->attr.implicit_type
830 && sym->attr.proc == 0
831 && gfc_current_ns->parent != NULL
832 && sym->attr.access == 0
833 && !module_fcn_entry)
834 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
835 "and must not have attributes declared at %L",
858f9894 836 name, &sym->declared_at);
837 }
838
839 if (gfc_current_ns->parent == NULL || *result == NULL)
840 return rc;
4ee9c684 841
d77f260f 842 /* Module function entries will already have a symtree in
843 the current namespace but will need one at module level. */
844 if (module_fcn_entry)
861d824f 845 {
846 /* Present if entry is declared to be a module procedure. */
847 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
848 if (st == NULL)
849 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
850 }
d77f260f 851 else
852 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
4ee9c684 853
4ee9c684 854 st->n.sym = sym;
855 sym->refs++;
856
f6d0e37a 857 /* See if the procedure should be a module procedure. */
4ee9c684 858
d77f260f 859 if (((sym->ns->proc_name != NULL
861d824f 860 && sym->ns->proc_name->attr.flavor == FL_MODULE
861 && sym->attr.proc != PROC_MODULE)
862 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
863 && gfc_add_procedure (&sym->attr, PROC_MODULE,
864 sym->name, NULL) == FAILURE)
4ee9c684 865 rc = 2;
866
867 return rc;
868}
869
870
c5d33754 871/* Verify that the given symbol representing a parameter is C
872 interoperable, by checking to see if it was marked as such after
873 its declaration. If the given symbol is not interoperable, a
874 warning is reported, thus removing the need to return the status to
875 the calling function. The standard does not require the user use
876 one of the iso_c_binding named constants to declare an
877 interoperable parameter, but we can't be sure if the param is C
878 interop or not if the user doesn't. For example, integer(4) may be
879 legal Fortran, but doesn't have meaning in C. It may interop with
880 a number of the C types, which causes a problem because the
881 compiler can't know which one. This code is almost certainly not
882 portable, and the user will get what they deserve if the C type
883 across platforms isn't always interoperable with integer(4). If
884 the user had used something like integer(c_int) or integer(c_long),
885 the compiler could have automatically handled the varying sizes
886 across platforms. */
887
f6bb8f4b 888gfc_try
c5d33754 889verify_c_interop_param (gfc_symbol *sym)
890{
891 int is_c_interop = 0;
f6bb8f4b 892 gfc_try retval = SUCCESS;
c5d33754 893
894 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
895 Don't repeat the checks here. */
896 if (sym->attr.implicit_type)
897 return SUCCESS;
898
899 /* For subroutines or functions that are passed to a BIND(C) procedure,
900 they're interoperable if they're BIND(C) and their params are all
901 interoperable. */
902 if (sym->attr.flavor == FL_PROCEDURE)
903 {
904 if (sym->attr.is_bind_c == 0)
905 {
906 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
907 "attribute to be C interoperable", sym->name,
908 &(sym->declared_at));
909
910 return FAILURE;
911 }
912 else
913 {
914 if (sym->attr.is_c_interop == 1)
915 /* We've already checked this procedure; don't check it again. */
916 return SUCCESS;
917 else
918 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
919 sym->common_block);
920 }
921 }
922
923 /* See if we've stored a reference to a procedure that owns sym. */
924 if (sym->ns != NULL && sym->ns->proc_name != NULL)
925 {
926 if (sym->ns->proc_name->attr.is_bind_c == 1)
927 {
928 is_c_interop =
209d3778 929 (verify_c_interop (&(sym->ts))
c5d33754 930 == SUCCESS ? 1 : 0);
931
932 if (is_c_interop != 1)
933 {
934 /* Make personalized messages to give better feedback. */
935 if (sym->ts.type == BT_DERIVED)
936 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
937 " procedure '%s' but is not C interoperable "
938 "because derived type '%s' is not C interoperable",
939 sym->name, &(sym->declared_at),
940 sym->ns->proc_name->name,
941 sym->ts.derived->name);
942 else
943 gfc_warning ("Variable '%s' at %L is a parameter to the "
944 "BIND(C) procedure '%s' but may not be C "
945 "interoperable",
946 sym->name, &(sym->declared_at),
947 sym->ns->proc_name->name);
948 }
e4eda3ec 949
950 /* Character strings are only C interoperable if they have a
951 length of 1. */
952 if (sym->ts.type == BT_CHARACTER)
953 {
954 gfc_charlen *cl = sym->ts.cl;
955 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
956 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
957 {
958 gfc_error ("Character argument '%s' at %L "
959 "must be length 1 because "
960 "procedure '%s' is BIND(C)",
961 sym->name, &sym->declared_at,
962 sym->ns->proc_name->name);
963 retval = FAILURE;
964 }
965 }
966
c5d33754 967 /* We have to make sure that any param to a bind(c) routine does
968 not have the allocatable, pointer, or optional attributes,
969 according to J3/04-007, section 5.1. */
970 if (sym->attr.allocatable == 1)
971 {
972 gfc_error ("Variable '%s' at %L cannot have the "
973 "ALLOCATABLE attribute because procedure '%s'"
974 " is BIND(C)", sym->name, &(sym->declared_at),
975 sym->ns->proc_name->name);
976 retval = FAILURE;
977 }
978
979 if (sym->attr.pointer == 1)
980 {
981 gfc_error ("Variable '%s' at %L cannot have the "
982 "POINTER attribute because procedure '%s'"
983 " is BIND(C)", sym->name, &(sym->declared_at),
984 sym->ns->proc_name->name);
985 retval = FAILURE;
986 }
987
988 if (sym->attr.optional == 1)
989 {
990 gfc_error ("Variable '%s' at %L cannot have the "
991 "OPTIONAL attribute because procedure '%s'"
992 " is BIND(C)", sym->name, &(sym->declared_at),
993 sym->ns->proc_name->name);
994 retval = FAILURE;
995 }
996
997 /* Make sure that if it has the dimension attribute, that it is
998 either assumed size or explicit shape. */
999 if (sym->as != NULL)
1000 {
1001 if (sym->as->type == AS_ASSUMED_SHAPE)
1002 {
1003 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1004 "argument to the procedure '%s' at %L because "
1005 "the procedure is BIND(C)", sym->name,
1006 &(sym->declared_at), sym->ns->proc_name->name,
1007 &(sym->ns->proc_name->declared_at));
1008 retval = FAILURE;
1009 }
1010
1011 if (sym->as->type == AS_DEFERRED)
1012 {
1013 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1014 "argument to the procedure '%s' at %L because "
1015 "the procedure is BIND(C)", sym->name,
1016 &(sym->declared_at), sym->ns->proc_name->name,
1017 &(sym->ns->proc_name->declared_at));
1018 retval = FAILURE;
1019 }
1020 }
1021 }
1022 }
1023
1024 return retval;
1025}
1026
1027
1028/* Function called by variable_decl() that adds a name to the symbol table. */
4ee9c684 1029
f6bb8f4b 1030static gfc_try
1a9745d2 1031build_sym (const char *name, gfc_charlen *cl,
1032 gfc_array_spec **as, locus *var_locus)
4ee9c684 1033{
1034 symbol_attribute attr;
1035 gfc_symbol *sym;
1036
ee893be6 1037 if (gfc_get_symbol (name, NULL, &sym))
4ee9c684 1038 return FAILURE;
1039
f6d0e37a 1040 /* Start updating the symbol table. Add basic type attribute if present. */
4ee9c684 1041 if (current_ts.type != BT_UNKNOWN
1a9745d2 1042 && (sym->attr.implicit_type == 0
1043 || !gfc_compare_types (&sym->ts, &current_ts))
4ee9c684 1044 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1045 return FAILURE;
1046
1047 if (sym->ts.type == BT_CHARACTER)
1048 sym->ts.cl = cl;
1049
1050 /* Add dimension attribute if present. */
1051 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1052 return FAILURE;
1053 *as = NULL;
1054
1055 /* Add attribute to symbol. The copy is so that we can reset the
1056 dimension attribute. */
1057 attr = current_attr;
1058 attr.dimension = 0;
1059
1060 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1061 return FAILURE;
1062
c5d33754 1063 /* Finish any work that may need to be done for the binding label,
1064 if it's a bind(c). The bind(c) attr is found before the symbol
1065 is made, and before the symbol name (for data decls), so the
1066 current_ts is holding the binding label, or nothing if the
1067 name= attr wasn't given. Therefore, test here if we're dealing
1068 with a bind(c) and make sure the binding label is set correctly. */
1069 if (sym->attr.is_bind_c == 1)
1070 {
1071 if (sym->binding_label[0] == '\0')
1072 {
825718f9 1073 /* Set the binding label and verify that if a NAME= was specified
1074 then only one identifier was in the entity-decl-list. */
1075 if (set_binding_label (sym->binding_label, sym->name,
1076 num_idents_on_line) == FAILURE)
c5d33754 1077 return FAILURE;
1078 }
1079 }
1080
1081 /* See if we know we're in a common block, and if it's a bind(c)
1082 common then we need to make sure we're an interoperable type. */
1083 if (sym->attr.in_common == 1)
1084 {
1085 /* Test the common block object. */
1086 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1087 && sym->ts.is_c_interop != 1)
1088 {
1089 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1090 "must be declared with a C interoperable "
1091 "kind since common block '%s' is BIND(C)",
1092 sym->name, sym->common_block->name,
1093 sym->common_block->name);
1094 gfc_clear_error ();
1095 }
1096 }
1097
2457a77e 1098 sym->attr.implied_index = 0;
1099
4ee9c684 1100 return SUCCESS;
1101}
1102
1a9745d2 1103
a270dc8e 1104/* Set character constant to the given length. The constant will be padded or
d1a39099 1105 truncated. If we're inside an array constructor without a typespec, we
1106 additionally check that all elements have the same length; check_len -1
1107 means no checking. */
a270dc8e 1108
1109void
d1a39099 1110gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
a270dc8e 1111{
c32f863c 1112 gfc_char_t *s;
a270dc8e 1113 int slen;
1114
1115 gcc_assert (expr->expr_type == EXPR_CONSTANT);
b44437b9 1116 gcc_assert (expr->ts.type == BT_CHARACTER);
a270dc8e 1117
1118 slen = expr->value.character.length;
1119 if (len != slen)
1120 {
c32f863c 1121 s = gfc_get_wide_string (len + 1);
1122 memcpy (s, expr->value.character.string,
1123 MIN (len, slen) * sizeof (gfc_char_t));
a270dc8e 1124 if (len > slen)
c32f863c 1125 gfc_wide_memset (&s[slen], ' ', len - slen);
1bfea7e8 1126
1127 if (gfc_option.warn_character_truncation && slen > len)
1128 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1129 "(%d/%d)", &expr->where, slen, len);
1130
1131 /* Apply the standard by 'hand' otherwise it gets cleared for
1132 initializers. */
d1a39099 1133 if (check_len != -1 && slen != check_len
1134 && !(gfc_option.allow_std & GFC_STD_GNU))
1bfea7e8 1135 gfc_error_now ("The CHARACTER elements of the array constructor "
1136 "at %L must have the same length (%d/%d)",
d1a39099 1137 &expr->where, slen, check_len);
1bfea7e8 1138
89f528df 1139 s[len] = '\0';
a270dc8e 1140 gfc_free (expr->value.character.string);
1141 expr->value.character.string = s;
1142 expr->value.character.length = len;
1143 }
1144}
4ee9c684 1145
3b6a4b41 1146
e14bee04 1147/* Function to create and update the enumerator history
3b6a4b41 1148 using the information passed as arguments.
e14bee04 1149 Pointer "max_enum" is also updated, to point to
1150 enum history node containing largest initializer.
3b6a4b41 1151
1152 SYM points to the symbol node of enumerator.
f6d0e37a 1153 INIT points to its enumerator value. */
3b6a4b41 1154
e14bee04 1155static void
1a9745d2 1156create_enum_history (gfc_symbol *sym, gfc_expr *init)
3b6a4b41 1157{
1158 enumerator_history *new_enum_history;
1159 gcc_assert (sym != NULL && init != NULL);
1160
48d8ad5a 1161 new_enum_history = XCNEW (enumerator_history);
3b6a4b41 1162
1163 new_enum_history->sym = sym;
1164 new_enum_history->initializer = init;
1165 new_enum_history->next = NULL;
1166
1167 if (enum_history == NULL)
1168 {
1169 enum_history = new_enum_history;
1170 max_enum = enum_history;
1171 }
1172 else
1173 {
1174 new_enum_history->next = enum_history;
1175 enum_history = new_enum_history;
1176
e14bee04 1177 if (mpz_cmp (max_enum->initializer->value.integer,
3b6a4b41 1178 new_enum_history->initializer->value.integer) < 0)
1a9745d2 1179 max_enum = new_enum_history;
3b6a4b41 1180 }
1181}
1182
1183
e14bee04 1184/* Function to free enum kind history. */
3b6a4b41 1185
e14bee04 1186void
1a9745d2 1187gfc_free_enum_history (void)
3b6a4b41 1188{
e14bee04 1189 enumerator_history *current = enum_history;
1190 enumerator_history *next;
3b6a4b41 1191
1192 while (current != NULL)
1193 {
1194 next = current->next;
1195 gfc_free (current);
1196 current = next;
1197 }
1198 max_enum = NULL;
1199 enum_history = NULL;
1200}
1201
1202
4ee9c684 1203/* Function called by variable_decl() that adds an initialization
1204 expression to a symbol. */
1205
f6bb8f4b 1206static gfc_try
f6d0e37a 1207add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
4ee9c684 1208{
1209 symbol_attribute attr;
1210 gfc_symbol *sym;
1211 gfc_expr *init;
1212
1213 init = *initp;
36b0a1b0 1214 if (find_special (name, &sym, false))
4ee9c684 1215 return FAILURE;
1216
1217 attr = sym->attr;
1218
1219 /* If this symbol is confirming an implicit parameter type,
1220 then an initialization expression is not allowed. */
1221 if (attr.flavor == FL_PARAMETER
1222 && sym->value != NULL
1223 && *initp != NULL)
1224 {
1225 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1226 sym->name);
1227 return FAILURE;
1228 }
1229
1230 if (init == NULL)
1231 {
1232 /* An initializer is required for PARAMETER declarations. */
1233 if (attr.flavor == FL_PARAMETER)
1234 {
1235 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1236 return FAILURE;
1237 }
1238 }
1239 else
1240 {
1241 /* If a variable appears in a DATA block, it cannot have an
b97f1a18 1242 initializer. */
4ee9c684 1243 if (sym->attr.data)
1244 {
1a9745d2 1245 gfc_error ("Variable '%s' at %C with an initializer already "
1246 "appears in a DATA statement", sym->name);
4ee9c684 1247 return FAILURE;
1248 }
1249
cca3db55 1250 /* Check if the assignment can happen. This has to be put off
1251 until later for a derived type variable. */
4ee9c684 1252 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1253 && gfc_check_assign_symbol (sym, init) == FAILURE)
1254 return FAILURE;
1255
da540712 1256 if (sym->ts.type == BT_CHARACTER && sym->ts.cl
1257 && init->ts.type == BT_CHARACTER)
a270dc8e 1258 {
1259 /* Update symbol character length according initializer. */
da540712 1260 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1261 return FAILURE;
1262
a270dc8e 1263 if (sym->ts.cl->length == NULL)
1264 {
03c2a028 1265 int clen;
f6d0e37a 1266 /* If there are multiple CHARACTER variables declared on the
1267 same line, we don't want them to share the same length. */
5d50997a 1268 sym->ts.cl = gfc_new_charlen (gfc_current_ns);
e9c873a4 1269
03c2a028 1270 if (sym->attr.flavor == FL_PARAMETER)
1271 {
1272 if (init->expr_type == EXPR_CONSTANT)
1273 {
1274 clen = init->value.character.length;
1275 sym->ts.cl->length = gfc_int_expr (clen);
1276 }
1277 else if (init->expr_type == EXPR_ARRAY)
1278 {
1279 gfc_expr *p = init->value.constructor->expr;
1280 clen = p->value.character.length;
1281 sym->ts.cl->length = gfc_int_expr (clen);
1282 }
1283 else if (init->ts.cl && init->ts.cl->length)
1284 sym->ts.cl->length =
1285 gfc_copy_expr (sym->value->ts.cl->length);
1286 }
a270dc8e 1287 }
1288 /* Update initializer character length according symbol. */
1289 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1290 {
1291 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1292 gfc_constructor * p;
1293
1294 if (init->expr_type == EXPR_CONSTANT)
d1a39099 1295 gfc_set_constant_character_len (len, init, -1);
a270dc8e 1296 else if (init->expr_type == EXPR_ARRAY)
1297 {
39908fd9 1298 /* Build a new charlen to prevent simplification from
1299 deleting the length before it is resolved. */
5d50997a 1300 init->ts.cl = gfc_new_charlen (gfc_current_ns);
a270dc8e 1301 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
39908fd9 1302
a270dc8e 1303 for (p = init->value.constructor; p; p = p->next)
d1a39099 1304 gfc_set_constant_character_len (len, p->expr, -1);
a270dc8e 1305 }
1306 }
1307 }
1308
c5d33754 1309 /* Need to check if the expression we initialized this
1310 to was one of the iso_c_binding named constants. If so,
1311 and we're a parameter (constant), let it be iso_c.
1312 For example:
1313 integer(c_int), parameter :: my_int = c_int
1314 integer(my_int) :: my_int_2
1315 If we mark my_int as iso_c (since we can see it's value
1316 is equal to one of the named constants), then my_int_2
1317 will be considered C interoperable. */
1318 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1319 {
1320 sym->ts.is_iso_c |= init->ts.is_iso_c;
1321 sym->ts.is_c_interop |= init->ts.is_c_interop;
1322 /* attr bits needed for module files. */
1323 sym->attr.is_iso_c |= init->ts.is_iso_c;
1324 sym->attr.is_c_interop |= init->ts.is_c_interop;
1325 if (init->ts.is_iso_c)
1326 sym->ts.f90_type = init->ts.f90_type;
1327 }
1328
4ee9c684 1329 /* Add initializer. Make sure we keep the ranks sane. */
1330 if (sym->attr.dimension && init->rank == 0)
7baa3fb4 1331 {
1332 mpz_t size;
1333 gfc_expr *array;
1334 gfc_constructor *c;
1335 int n;
1336 if (sym->attr.flavor == FL_PARAMETER
1337 && init->expr_type == EXPR_CONSTANT
1338 && spec_size (sym->as, &size) == SUCCESS
1339 && mpz_cmp_si (size, 0) > 0)
1340 {
1341 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1342 &init->where);
1343
1344 array->value.constructor = c = NULL;
1345 for (n = 0; n < (int)mpz_get_si (size); n++)
1346 {
1347 if (array->value.constructor == NULL)
1348 {
1349 array->value.constructor = c = gfc_get_constructor ();
1350 c->expr = init;
1351 }
1352 else
1353 {
1354 c->next = gfc_get_constructor ();
1355 c = c->next;
1356 c->expr = gfc_copy_expr (init);
1357 }
1358 }
1359
1360 array->shape = gfc_get_shape (sym->as->rank);
1361 for (n = 0; n < sym->as->rank; n++)
1362 spec_dimen_size (sym->as, n, &array->shape[n]);
1363
1364 init = array;
1365 mpz_clear (size);
1366 }
1367 init->rank = sym->as->rank;
1368 }
4ee9c684 1369
1370 sym->value = init;
3cd3c667 1371 if (sym->attr.save == SAVE_NONE)
1372 sym->attr.save = SAVE_IMPLICIT;
4ee9c684 1373 *initp = NULL;
1374 }
1375
1376 return SUCCESS;
1377}
1378
1379
1380/* Function called by variable_decl() that adds a name to a structure
1381 being built. */
1382
f6bb8f4b 1383static gfc_try
1a9745d2 1384build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1385 gfc_array_spec **as)
4ee9c684 1386{
1387 gfc_component *c;
1388
1389 /* If the current symbol is of the same derived type that we're
1390 constructing, it must have the pointer attribute. */
1391 if (current_ts.type == BT_DERIVED
1392 && current_ts.derived == gfc_current_block ()
1393 && current_attr.pointer == 0)
1394 {
1395 gfc_error ("Component at %C must have the POINTER attribute");
1396 return FAILURE;
1397 }
1398
1a9745d2 1399 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
4ee9c684 1400 {
1401 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1402 {
1403 gfc_error ("Array component of structure at %C must have explicit "
1404 "or deferred shape");
1405 return FAILURE;
1406 }
1407 }
1408
1409 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1410 return FAILURE;
1411
1412 c->ts = current_ts;
1413 c->ts.cl = cl;
3be2b8d5 1414 c->attr = current_attr;
4ee9c684 1415
1416 c->initializer = *init;
1417 *init = NULL;
1418
1419 c->as = *as;
1420 if (c->as != NULL)
3be2b8d5 1421 c->attr.dimension = 1;
4ee9c684 1422 *as = NULL;
1423
a2f51d5f 1424 /* Should this ever get more complicated, combine with similar section
1425 in add_init_expr_to_sym into a separate function. */
3be2b8d5 1426 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.cl
d1a39099 1427 && c->ts.cl->length && c->ts.cl->length->expr_type == EXPR_CONSTANT)
a2f51d5f 1428 {
d1a39099 1429 int len;
1430
1431 gcc_assert (c->ts.cl && c->ts.cl->length);
1432 gcc_assert (c->ts.cl->length->expr_type == EXPR_CONSTANT);
1433 gcc_assert (c->ts.cl->length->ts.type == BT_INTEGER);
1434
1435 len = mpz_get_si (c->ts.cl->length->value.integer);
a2f51d5f 1436
1437 if (c->initializer->expr_type == EXPR_CONSTANT)
d1a39099 1438 gfc_set_constant_character_len (len, c->initializer, -1);
a2f51d5f 1439 else if (mpz_cmp (c->ts.cl->length->value.integer,
1440 c->initializer->ts.cl->length->value.integer))
1441 {
d1a39099 1442 bool has_ts;
a2f51d5f 1443 gfc_constructor *ctor = c->initializer->value.constructor;
d1a39099 1444
d1a39099 1445 has_ts = (c->initializer->ts.cl
1446 && c->initializer->ts.cl->length_from_typespec);
1447
6e4faf80 1448 if (ctor)
d1a39099 1449 {
6e4faf80 1450 int first_len;
1451
1452 /* Remember the length of the first element for checking
1453 that all elements *in the constructor* have the same
1454 length. This need not be the length of the LHS! */
1455 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1456 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1457 first_len = ctor->expr->value.character.length;
1458
1459 for (; ctor; ctor = ctor->next)
d1a39099 1460 {
6e4faf80 1461 if (ctor->expr->expr_type == EXPR_CONSTANT)
1462 gfc_set_constant_character_len (len, ctor->expr,
1463 has_ts ? -1 : first_len);
d1a39099 1464 }
d1a39099 1465 }
a2f51d5f 1466 }
1467 }
1468
4ee9c684 1469 /* Check array components. */
3be2b8d5 1470 if (!c->attr.dimension)
2294b616 1471 {
3be2b8d5 1472 if (c->attr.allocatable)
2294b616 1473 {
1474 gfc_error ("Allocatable component at %C must be an array");
1475 return FAILURE;
1476 }
1477 else
1478 return SUCCESS;
1479 }
4ee9c684 1480
3be2b8d5 1481 if (c->attr.pointer)
4ee9c684 1482 {
1483 if (c->as->type != AS_DEFERRED)
1484 {
2294b616 1485 gfc_error ("Pointer array component of structure at %C must have a "
1486 "deferred shape");
1487 return FAILURE;
1488 }
1489 }
3be2b8d5 1490 else if (c->attr.allocatable)
2294b616 1491 {
1492 if (c->as->type != AS_DEFERRED)
1493 {
1494 gfc_error ("Allocatable component of structure at %C must have a "
1495 "deferred shape");
4ee9c684 1496 return FAILURE;
1497 }
1498 }
1499 else
1500 {
1501 if (c->as->type != AS_EXPLICIT)
1502 {
1a9745d2 1503 gfc_error ("Array component of structure at %C must have an "
1504 "explicit shape");
4ee9c684 1505 return FAILURE;
1506 }
1507 }
1508
1509 return SUCCESS;
1510}
1511
1512
1513/* Match a 'NULL()', and possibly take care of some side effects. */
1514
1515match
1a9745d2 1516gfc_match_null (gfc_expr **result)
4ee9c684 1517{
1518 gfc_symbol *sym;
1519 gfc_expr *e;
1520 match m;
1521
1522 m = gfc_match (" null ( )");
1523 if (m != MATCH_YES)
1524 return m;
1525
1526 /* The NULL symbol now has to be/become an intrinsic function. */
1527 if (gfc_get_symbol ("null", NULL, &sym))
1528 {
1529 gfc_error ("NULL() initialization at %C is ambiguous");
1530 return MATCH_ERROR;
1531 }
1532
1533 gfc_intrinsic_symbol (sym);
1534
1535 if (sym->attr.proc != PROC_INTRINSIC
950683ed 1536 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1537 sym->name, NULL) == FAILURE
1538 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
4ee9c684 1539 return MATCH_ERROR;
1540
1541 e = gfc_get_expr ();
cbb9e6aa 1542 e->where = gfc_current_locus;
4ee9c684 1543 e->expr_type = EXPR_NULL;
1544 e->ts.type = BT_UNKNOWN;
1545
1546 *result = e;
1547
1548 return MATCH_YES;
1549}
1550
1551
4ee9c684 1552/* Match a variable name with an optional initializer. When this
1553 subroutine is called, a variable is expected to be parsed next.
1554 Depending on what is happening at the moment, updates either the
1555 symbol table or the current interface. */
1556
1557static match
3923b69f 1558variable_decl (int elem)
4ee9c684 1559{
1560 char name[GFC_MAX_SYMBOL_LEN + 1];
1561 gfc_expr *initializer, *char_len;
1562 gfc_array_spec *as;
b549d2a5 1563 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
4ee9c684 1564 gfc_charlen *cl;
1565 locus var_locus;
1566 match m;
f6bb8f4b 1567 gfc_try t;
b549d2a5 1568 gfc_symbol *sym;
3b6a4b41 1569 locus old_locus;
4ee9c684 1570
1571 initializer = NULL;
1572 as = NULL;
b549d2a5 1573 cp_as = NULL;
3b6a4b41 1574 old_locus = gfc_current_locus;
4ee9c684 1575
1576 /* When we get here, we've just matched a list of attributes and
1577 maybe a type and a double colon. The next thing we expect to see
1578 is the name of the symbol. */
1579 m = gfc_match_name (name);
1580 if (m != MATCH_YES)
1581 goto cleanup;
1582
cbb9e6aa 1583 var_locus = gfc_current_locus;
4ee9c684 1584
1585 /* Now we could see the optional array spec. or character length. */
1586 m = gfc_match_array_spec (&as);
b549d2a5 1587 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1588 cp_as = gfc_copy_array_spec (as);
1589 else if (m == MATCH_ERROR)
4ee9c684 1590 goto cleanup;
3b6a4b41 1591
4ee9c684 1592 if (m == MATCH_NO)
1593 as = gfc_copy_array_spec (current_as);
1594
1595 char_len = NULL;
1596 cl = NULL;
1597
1598 if (current_ts.type == BT_CHARACTER)
1599 {
1600 switch (match_char_length (&char_len))
1601 {
1602 case MATCH_YES:
5d50997a 1603 cl = gfc_new_charlen (gfc_current_ns);
4ee9c684 1604
1605 cl->length = char_len;
1606 break;
1607
3923b69f 1608 /* Non-constant lengths need to be copied after the first
04b61f60 1609 element. Also copy assumed lengths. */
4ee9c684 1610 case MATCH_NO:
04b61f60 1611 if (elem > 1
1612 && (current_ts.cl->length == NULL
1613 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
3923b69f 1614 {
5d50997a 1615 cl = gfc_new_charlen (gfc_current_ns);
3923b69f 1616 cl->length = gfc_copy_expr (current_ts.cl->length);
1617 }
1618 else
1619 cl = current_ts.cl;
1620
4ee9c684 1621 break;
1622
1623 case MATCH_ERROR:
1624 goto cleanup;
1625 }
1626 }
1627
b549d2a5 1628 /* If this symbol has already shown up in a Cray Pointer declaration,
f6d0e37a 1629 then we want to set the type & bail out. */
b549d2a5 1630 if (gfc_option.flag_cray_pointer)
1631 {
1632 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1633 if (sym != NULL && sym->attr.cray_pointee)
1634 {
1635 sym->ts.type = current_ts.type;
1636 sym->ts.kind = current_ts.kind;
1637 sym->ts.cl = cl;
1638 sym->ts.derived = current_ts.derived;
c5d33754 1639 sym->ts.is_c_interop = current_ts.is_c_interop;
1640 sym->ts.is_iso_c = current_ts.is_iso_c;
b549d2a5 1641 m = MATCH_YES;
1642
1643 /* Check to see if we have an array specification. */
1644 if (cp_as != NULL)
1645 {
1646 if (sym->as != NULL)
1647 {
7698a624 1648 gfc_error ("Duplicate array spec for Cray pointee at %C");
b549d2a5 1649 gfc_free_array_spec (cp_as);
1650 m = MATCH_ERROR;
1651 goto cleanup;
1652 }
1653 else
1654 {
1655 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1656 gfc_internal_error ("Couldn't set pointee array spec.");
e14bee04 1657
b549d2a5 1658 /* Fix the array spec. */
e14bee04 1659 m = gfc_mod_pointee_as (sym->as);
b549d2a5 1660 if (m == MATCH_ERROR)
1661 goto cleanup;
1662 }
e14bee04 1663 }
b549d2a5 1664 goto cleanup;
1665 }
1666 else
1667 {
1668 gfc_free_array_spec (cp_as);
1669 }
1670 }
e14bee04 1671
1e057e9b 1672 /* Procedure pointer as function result. */
1673 if (gfc_current_state () == COMP_FUNCTION
1674 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1675 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1676 strcpy (name, "ppr@");
1677
1678 if (gfc_current_state () == COMP_FUNCTION
1679 && strcmp (name, gfc_current_block ()->name) == 0
1680 && gfc_current_block ()->result
1681 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1682 strcpy (name, "ppr@");
e14bee04 1683
4ee9c684 1684 /* OK, we've successfully matched the declaration. Now put the
1685 symbol in the current namespace, because it might be used in the
fe06c0d5 1686 optional initialization expression for this symbol, e.g. this is
4ee9c684 1687 perfectly legal:
1688
1689 integer, parameter :: i = huge(i)
1690
1691 This is only true for parameters or variables of a basic type.
1692 For components of derived types, it is not true, so we don't
1693 create a symbol for those yet. If we fail to create the symbol,
1694 bail out. */
1695 if (gfc_current_state () != COMP_DERIVED
1696 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1697 {
b8a51d79 1698 m = MATCH_ERROR;
1699 goto cleanup;
1700 }
1701
60fbbf9e 1702 /* An interface body specifies all of the procedure's
1703 characteristics and these shall be consistent with those
1704 specified in the procedure definition, except that the interface
1705 may specify a procedure that is not pure if the procedure is
1706 defined to be pure(12.3.2). */
b8a51d79 1707 if (current_ts.type == BT_DERIVED
1a9745d2 1708 && gfc_current_ns->proc_name
1709 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
c723595c 1710 && current_ts.derived->ns != gfc_current_ns)
1711 {
1712 gfc_symtree *st;
1713 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1714 if (!(current_ts.derived->attr.imported
1715 && st != NULL
1716 && st->n.sym == current_ts.derived)
1717 && !gfc_current_ns->has_import_set)
1718 {
1719 gfc_error ("the type of '%s' at %C has not been declared within the "
1720 "interface", name);
1721 m = MATCH_ERROR;
1722 goto cleanup;
1723 }
4ee9c684 1724 }
1725
1726 /* In functions that have a RESULT variable defined, the function
1727 name always refers to function calls. Therefore, the name is
1728 not allowed to appear in specification statements. */
1729 if (gfc_current_state () == COMP_FUNCTION
1730 && gfc_current_block () != NULL
1731 && gfc_current_block ()->result != NULL
1732 && gfc_current_block ()->result != gfc_current_block ()
1733 && strcmp (gfc_current_block ()->name, name) == 0)
1734 {
1735 gfc_error ("Function name '%s' not allowed at %C", name);
1736 m = MATCH_ERROR;
1737 goto cleanup;
1738 }
1739
b4f45d02 1740 /* We allow old-style initializations of the form
1741 integer i /2/, j(4) /3*3, 1/
1742 (if no colon has been seen). These are different from data
1743 statements in that initializers are only allowed to apply to the
1744 variable immediately preceding, i.e.
1745 integer i, j /1, 2/
1746 is not allowed. Therefore we have to do some work manually, that
cca3db55 1747 could otherwise be left to the matchers for DATA statements. */
b4f45d02 1748
1749 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1750 {
1751 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1752 "initialization at %C") == FAILURE)
1753 return MATCH_ERROR;
e14bee04 1754
b4f45d02 1755 return match_old_style_init (name);
1756 }
1757
4ee9c684 1758 /* The double colon must be present in order to have initializers.
1759 Otherwise the statement is ambiguous with an assignment statement. */
1760 if (colon_seen)
1761 {
1762 if (gfc_match (" =>") == MATCH_YES)
1763 {
4ee9c684 1764 if (!current_attr.pointer)
1765 {
1766 gfc_error ("Initialization at %C isn't for a pointer variable");
1767 m = MATCH_ERROR;
1768 goto cleanup;
1769 }
1770
1771 m = gfc_match_null (&initializer);
1772 if (m == MATCH_NO)
1773 {
e4789c62 1774 gfc_error ("Pointer initialization requires a NULL() at %C");
4ee9c684 1775 m = MATCH_ERROR;
1776 }
1777
1778 if (gfc_pure (NULL))
1779 {
1a9745d2 1780 gfc_error ("Initialization of pointer at %C is not allowed in "
1781 "a PURE procedure");
4ee9c684 1782 m = MATCH_ERROR;
1783 }
1784
1785 if (m != MATCH_YES)
1786 goto cleanup;
1787
4ee9c684 1788 }
1789 else if (gfc_match_char ('=') == MATCH_YES)
1790 {
1791 if (current_attr.pointer)
1792 {
1a9745d2 1793 gfc_error ("Pointer initialization at %C requires '=>', "
1794 "not '='");
4ee9c684 1795 m = MATCH_ERROR;
1796 goto cleanup;
1797 }
1798
1799 m = gfc_match_init_expr (&initializer);
1800 if (m == MATCH_NO)
1801 {
1802 gfc_error ("Expected an initialization expression at %C");
1803 m = MATCH_ERROR;
1804 }
1805
1806 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1807 {
1a9745d2 1808 gfc_error ("Initialization of variable at %C is not allowed in "
1809 "a PURE procedure");
4ee9c684 1810 m = MATCH_ERROR;
1811 }
1812
1813 if (m != MATCH_YES)
1814 goto cleanup;
1815 }
8ffad0f9 1816 }
1817
2294b616 1818 if (initializer != NULL && current_attr.allocatable
1819 && gfc_current_state () == COMP_DERIVED)
1820 {
1a9745d2 1821 gfc_error ("Initialization of allocatable component at %C is not "
1822 "allowed");
2294b616 1823 m = MATCH_ERROR;
1824 goto cleanup;
1825 }
1826
d9b3f26b 1827 /* Add the initializer. Note that it is fine if initializer is
4ee9c684 1828 NULL here, because we sometimes also need to check if a
1829 declaration *must* have an initialization expression. */
1830 if (gfc_current_state () != COMP_DERIVED)
1831 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1832 else
d9b3f26b 1833 {
2294b616 1834 if (current_ts.type == BT_DERIVED
1a9745d2 1835 && !current_attr.pointer && !initializer)
d9b3f26b 1836 initializer = gfc_default_initializer (&current_ts);
1837 t = build_struct (name, cl, &initializer, &as);
1838 }
4ee9c684 1839
1840 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1841
1842cleanup:
1843 /* Free stuff up and return. */
1844 gfc_free_expr (initializer);
1845 gfc_free_array_spec (as);
1846
1847 return m;
1848}
1849
1850
d10f89ee 1851/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1852 This assumes that the byte size is equal to the kind number for
1853 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
4ee9c684 1854
1855match
1a9745d2 1856gfc_match_old_kind_spec (gfc_typespec *ts)
4ee9c684 1857{
1858 match m;
3bd3b616 1859 int original_kind;
4ee9c684 1860
1861 if (gfc_match_char ('*') != MATCH_YES)
1862 return MATCH_NO;
1863
3bd3b616 1864 m = gfc_match_small_literal_int (&ts->kind, NULL);
4ee9c684 1865 if (m != MATCH_YES)
1866 return MATCH_ERROR;
1867
b118a35b 1868 original_kind = ts->kind;
1869
4ee9c684 1870 /* Massage the kind numbers for complex types. */
b118a35b 1871 if (ts->type == BT_COMPLEX)
1872 {
1873 if (ts->kind % 2)
1a9745d2 1874 {
1875 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1876 gfc_basic_typename (ts->type), original_kind);
1877 return MATCH_ERROR;
1878 }
b118a35b 1879 ts->kind /= 2;
1880 }
4ee9c684 1881
f2d4ef3b 1882 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 1883 {
b118a35b 1884 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1a9745d2 1885 gfc_basic_typename (ts->type), original_kind);
4ee9c684 1886 return MATCH_ERROR;
1887 }
1888
be7f01a1 1889 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1890 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1891 return MATCH_ERROR;
1892
4ee9c684 1893 return MATCH_YES;
1894}
1895
1896
1897/* Match a kind specification. Since kinds are generally optional, we
1898 usually return MATCH_NO if something goes wrong. If a "kind="
1899 string is found, then we know we have an error. */
1900
1901match
67a51c8e 1902gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
4ee9c684 1903{
67a51c8e 1904 locus where, loc;
4ee9c684 1905 gfc_expr *e;
1906 match m, n;
c632ff3d 1907 char c;
4ee9c684 1908 const char *msg;
1909
1910 m = MATCH_NO;
67a51c8e 1911 n = MATCH_YES;
4ee9c684 1912 e = NULL;
1913
67a51c8e 1914 where = loc = gfc_current_locus;
1915
1916 if (kind_expr_only)
1917 goto kind_expr;
4ee9c684 1918
1919 if (gfc_match_char ('(') == MATCH_NO)
1920 return MATCH_NO;
1921
1922 /* Also gobbles optional text. */
1923 if (gfc_match (" kind = ") == MATCH_YES)
1924 m = MATCH_ERROR;
1925
67a51c8e 1926 loc = gfc_current_locus;
1927
1928kind_expr:
4ee9c684 1929 n = gfc_match_init_expr (&e);
67a51c8e 1930
4ee9c684 1931 if (n != MATCH_YES)
67a51c8e 1932 {
077932f9 1933 if (gfc_matching_function)
67a51c8e 1934 {
077932f9 1935 /* The function kind expression might include use associated or
1936 imported parameters and try again after the specification
1937 expressions..... */
67a51c8e 1938 if (gfc_match_char (')') != MATCH_YES)
1939 {
1940 gfc_error ("Missing right parenthesis at %C");
1941 m = MATCH_ERROR;
1942 goto no_match;
1943 }
1944
1945 gfc_free_expr (e);
67a51c8e 1946 gfc_undo_symbols ();
1947 return MATCH_YES;
1948 }
1949 else
1950 {
1951 /* ....or else, the match is real. */
1952 if (n == MATCH_NO)
1953 gfc_error ("Expected initialization expression at %C");
1954 if (n != MATCH_YES)
1955 return MATCH_ERROR;
1956 }
1957 }
4ee9c684 1958
1959 if (e->rank != 0)
1960 {
1961 gfc_error ("Expected scalar initialization expression at %C");
1962 m = MATCH_ERROR;
1963 goto no_match;
1964 }
1965
1966 msg = gfc_extract_int (e, &ts->kind);
077932f9 1967
4ee9c684 1968 if (msg != NULL)
1969 {
1970 gfc_error (msg);
1971 m = MATCH_ERROR;
1972 goto no_match;
1973 }
1974
c5d33754 1975 /* Before throwing away the expression, let's see if we had a
1976 C interoperable kind (and store the fact). */
1977 if (e->ts.is_c_interop == 1)
1978 {
1979 /* Mark this as c interoperable if being declared with one
1980 of the named constants from iso_c_binding. */
1981 ts->is_c_interop = e->ts.is_iso_c;
1982 ts->f90_type = e->ts.f90_type;
1983 }
1984
4ee9c684 1985 gfc_free_expr (e);
1986 e = NULL;
1987
c5d33754 1988 /* Ignore errors to this point, if we've gotten here. This means
1989 we ignore the m=MATCH_ERROR from above. */
f2d4ef3b 1990 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
4ee9c684 1991 {
1992 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1993 gfc_basic_typename (ts->type));
c632ff3d 1994 gfc_current_locus = where;
1995 return MATCH_ERROR;
4ee9c684 1996 }
c632ff3d 1997
209d3778 1998 /* Warn if, e.g., c_int is used for a REAL variable, but not
1999 if, e.g., c_double is used for COMPLEX as the standard
2000 explicitly says that the kind type parameter for complex and real
2001 variable is the same, i.e. c_float == c_float_complex. */
2002 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2003 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2004 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
ded233a6 2005 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2006 "is %s", gfc_basic_typename (ts->f90_type), &where,
2007 gfc_basic_typename (ts->type));
209d3778 2008
c632ff3d 2009 gfc_gobble_whitespace ();
e0be6f02 2010 if ((c = gfc_next_ascii_char ()) != ')'
2011 && (ts->type != BT_CHARACTER || c != ','))
4ee9c684 2012 {
c632ff3d 2013 if (ts->type == BT_CHARACTER)
2014 gfc_error ("Missing right parenthesis or comma at %C");
2015 else
2016 gfc_error ("Missing right parenthesis at %C");
67a51c8e 2017 m = MATCH_ERROR;
4ee9c684 2018 }
c5d33754 2019 else
2020 /* All tests passed. */
2021 m = MATCH_YES;
4ee9c684 2022
c5d33754 2023 if(m == MATCH_ERROR)
2024 gfc_current_locus = where;
2025
2026 /* Return what we know from the test(s). */
2027 return m;
4ee9c684 2028
2029no_match:
2030 gfc_free_expr (e);
cbb9e6aa 2031 gfc_current_locus = where;
4ee9c684 2032 return m;
2033}
2034
2035
33399208 2036static match
2037match_char_kind (int * kind, int * is_iso_c)
2038{
2039 locus where;
2040 gfc_expr *e;
2041 match m, n;
2042 const char *msg;
2043
2044 m = MATCH_NO;
2045 e = NULL;
2046 where = gfc_current_locus;
2047
2048 n = gfc_match_init_expr (&e);
c632ff3d 2049
077932f9 2050 if (n != MATCH_YES && gfc_matching_function)
c632ff3d 2051 {
077932f9 2052 /* The expression might include use-associated or imported
2053 parameters and try again after the specification
2054 expressions. */
c632ff3d 2055 gfc_free_expr (e);
c632ff3d 2056 gfc_undo_symbols ();
2057 return MATCH_YES;
2058 }
2059
33399208 2060 if (n == MATCH_NO)
2061 gfc_error ("Expected initialization expression at %C");
2062 if (n != MATCH_YES)
2063 return MATCH_ERROR;
2064
2065 if (e->rank != 0)
2066 {
2067 gfc_error ("Expected scalar initialization expression at %C");
2068 m = MATCH_ERROR;
2069 goto no_match;
2070 }
2071
2072 msg = gfc_extract_int (e, kind);
2073 *is_iso_c = e->ts.is_iso_c;
2074 if (msg != NULL)
2075 {
2076 gfc_error (msg);
2077 m = MATCH_ERROR;
2078 goto no_match;
2079 }
2080
2081 gfc_free_expr (e);
2082
2083 /* Ignore errors to this point, if we've gotten here. This means
2084 we ignore the m=MATCH_ERROR from above. */
2085 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2086 {
2087 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2088 m = MATCH_ERROR;
2089 }
2090 else
2091 /* All tests passed. */
2092 m = MATCH_YES;
2093
2094 if (m == MATCH_ERROR)
2095 gfc_current_locus = where;
2096
2097 /* Return what we know from the test(s). */
2098 return m;
2099
2100no_match:
2101 gfc_free_expr (e);
2102 gfc_current_locus = where;
2103 return m;
2104}
2105
4ee9c684 2106/* Match the various kind/length specifications in a CHARACTER
2107 declaration. We don't return MATCH_NO. */
2108
2109static match
1a9745d2 2110match_char_spec (gfc_typespec *ts)
4ee9c684 2111{
33399208 2112 int kind, seen_length, is_iso_c;
4ee9c684 2113 gfc_charlen *cl;
2114 gfc_expr *len;
2115 match m;
33399208 2116
4ee9c684 2117 len = NULL;
2118 seen_length = 0;
33399208 2119 kind = 0;
2120 is_iso_c = 0;
4ee9c684 2121
2122 /* Try the old-style specification first. */
2123 old_char_selector = 0;
2124
2125 m = match_char_length (&len);
2126 if (m != MATCH_NO)
2127 {
2128 if (m == MATCH_YES)
2129 old_char_selector = 1;
2130 seen_length = 1;
2131 goto done;
2132 }
2133
2134 m = gfc_match_char ('(');
2135 if (m != MATCH_YES)
2136 {
c5d33754 2137 m = MATCH_YES; /* Character without length is a single char. */
4ee9c684 2138 goto done;
2139 }
2140
c5d33754 2141 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
4ee9c684 2142 if (gfc_match (" kind =") == MATCH_YES)
2143 {
33399208 2144 m = match_char_kind (&kind, &is_iso_c);
c5d33754 2145
4ee9c684 2146 if (m == MATCH_ERROR)
2147 goto done;
2148 if (m == MATCH_NO)
2149 goto syntax;
2150
2151 if (gfc_match (" , len =") == MATCH_NO)
2152 goto rparen;
2153
2154 m = char_len_param_value (&len);
2155 if (m == MATCH_NO)
2156 goto syntax;
2157 if (m == MATCH_ERROR)
2158 goto done;
2159 seen_length = 1;
2160
2161 goto rparen;
2162 }
2163
f6d0e37a 2164 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
4ee9c684 2165 if (gfc_match (" len =") == MATCH_YES)
2166 {
2167 m = char_len_param_value (&len);
2168 if (m == MATCH_NO)
2169 goto syntax;
2170 if (m == MATCH_ERROR)
2171 goto done;
2172 seen_length = 1;
2173
2174 if (gfc_match_char (')') == MATCH_YES)
2175 goto done;
2176
2177 if (gfc_match (" , kind =") != MATCH_YES)
2178 goto syntax;
2179
33399208 2180 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2181 goto done;
4ee9c684 2182
2183 goto rparen;
2184 }
2185
f6d0e37a 2186 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
4ee9c684 2187 m = char_len_param_value (&len);
2188 if (m == MATCH_NO)
2189 goto syntax;
2190 if (m == MATCH_ERROR)
2191 goto done;
2192 seen_length = 1;
2193
2194 m = gfc_match_char (')');
2195 if (m == MATCH_YES)
2196 goto done;
2197
2198 if (gfc_match_char (',') != MATCH_YES)
2199 goto syntax;
2200
c5d33754 2201 gfc_match (" kind ="); /* Gobble optional text. */
4ee9c684 2202
33399208 2203 m = match_char_kind (&kind, &is_iso_c);
4ee9c684 2204 if (m == MATCH_ERROR)
2205 goto done;
2206 if (m == MATCH_NO)
2207 goto syntax;
2208
2209rparen:
2210 /* Require a right-paren at this point. */
2211 m = gfc_match_char (')');
2212 if (m == MATCH_YES)
2213 goto done;
2214
2215syntax:
2216 gfc_error ("Syntax error in CHARACTER declaration at %C");
2217 m = MATCH_ERROR;
a3cbe8cc 2218 gfc_free_expr (len);
2219 return m;
4ee9c684 2220
2221done:
8d39570e 2222 /* Deal with character functions after USE and IMPORT statements. */
2223 if (gfc_matching_function)
077932f9 2224 {
8d39570e 2225 gfc_free_expr (len);
077932f9 2226 gfc_undo_symbols ();
2227 return MATCH_YES;
2228 }
2229
4ee9c684 2230 if (m != MATCH_YES)
2231 {
2232 gfc_free_expr (len);
2233 return m;
2234 }
2235
2236 /* Do some final massaging of the length values. */
5d50997a 2237 cl = gfc_new_charlen (gfc_current_ns);
4ee9c684 2238
2239 if (seen_length == 0)
2240 cl->length = gfc_int_expr (1);
2241 else
2fe2caa6 2242 cl->length = len;
4ee9c684 2243
2244 ts->cl = cl;
33399208 2245 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
4ee9c684 2246
c5d33754 2247 /* We have to know if it was a c interoperable kind so we can
2248 do accurate type checking of bind(c) procs, etc. */
33399208 2249 if (kind != 0)
2250 /* Mark this as c interoperable if being declared with one
2251 of the named constants from iso_c_binding. */
2252 ts->is_c_interop = is_iso_c;
c5d33754 2253 else if (len != NULL)
33399208 2254 /* Here, we might have parsed something such as: character(c_char)
2255 In this case, the parsing code above grabs the c_char when
2256 looking for the length (line 1690, roughly). it's the last
2257 testcase for parsing the kind params of a character variable.
2258 However, it's not actually the length. this seems like it
2259 could be an error.
2260 To see if the user used a C interop kind, test the expr
2261 of the so called length, and see if it's C interoperable. */
2262 ts->is_c_interop = len->ts.is_iso_c;
c5d33754 2263
4ee9c684 2264 return MATCH_YES;
2265}
2266
2267
2268/* Matches a type specification. If successful, sets the ts structure
2269 to the matched specification. This is necessary for FUNCTION and
2270 IMPLICIT statements.
2271
e14bee04 2272 If implicit_flag is nonzero, then we don't check for the optional
39351103 2273 kind specification. Not doing so is needed for matching an IMPLICIT
4ee9c684 2274 statement correctly. */
2275
67a51c8e 2276match
2277gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
4ee9c684 2278{
2279 char name[GFC_MAX_SYMBOL_LEN + 1];
2280 gfc_symbol *sym;
2281 match m;
e0be6f02 2282 char c;
077932f9 2283 bool seen_deferred_kind;
4ee9c684 2284
077932f9 2285 /* A belt and braces check that the typespec is correctly being treated
2286 as a deferred characteristic association. */
2287 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
8d39570e 2288 && (gfc_current_block ()->result->ts.kind == -1)
2289 && (ts->kind == -1);
4ee9c684 2290 gfc_clear_ts (ts);
077932f9 2291 if (seen_deferred_kind)
2292 ts->kind = -1;
4ee9c684 2293
c5d33754 2294 /* Clear the current binding label, in case one is given. */
2295 curr_binding_label[0] = '\0';
2296
25b29122 2297 if (gfc_match (" byte") == MATCH_YES)
2298 {
e14bee04 2299 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
25b29122 2300 == FAILURE)
2301 return MATCH_ERROR;
2302
2303 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2304 {
2305 gfc_error ("BYTE type used at %C "
2306 "is not available on the target machine");
2307 return MATCH_ERROR;
2308 }
e14bee04 2309
25b29122 2310 ts->type = BT_INTEGER;
2311 ts->kind = 1;
2312 return MATCH_YES;
2313 }
2314
4ee9c684 2315 if (gfc_match (" integer") == MATCH_YES)
2316 {
2317 ts->type = BT_INTEGER;
b8a891cb 2318 ts->kind = gfc_default_integer_kind;
4ee9c684 2319 goto get_kind;
2320 }
2321
2322 if (gfc_match (" character") == MATCH_YES)
2323 {
2324 ts->type = BT_CHARACTER;
39351103 2325 if (implicit_flag == 0)
2326 return match_char_spec (ts);
2327 else
2328 return MATCH_YES;
4ee9c684 2329 }
2330
2331 if (gfc_match (" real") == MATCH_YES)
2332 {
2333 ts->type = BT_REAL;
b8a891cb 2334 ts->kind = gfc_default_real_kind;
4ee9c684 2335 goto get_kind;
2336 }
2337
2338 if (gfc_match (" double precision") == MATCH_YES)
2339 {
2340 ts->type = BT_REAL;
b8a891cb 2341 ts->kind = gfc_default_double_kind;
4ee9c684 2342 return MATCH_YES;
2343 }
2344
2345 if (gfc_match (" complex") == MATCH_YES)
2346 {
2347 ts->type = BT_COMPLEX;
b8a891cb 2348 ts->kind = gfc_default_complex_kind;
4ee9c684 2349 goto get_kind;
2350 }
2351
2352 if (gfc_match (" double complex") == MATCH_YES)
2353 {
be7f01a1 2354 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2355 "conform to the Fortran 95 standard") == FAILURE)
2356 return MATCH_ERROR;
2357
4ee9c684 2358 ts->type = BT_COMPLEX;
b8a891cb 2359 ts->kind = gfc_default_double_kind;
4ee9c684 2360 return MATCH_YES;
2361 }
2362
2363 if (gfc_match (" logical") == MATCH_YES)
2364 {
2365 ts->type = BT_LOGICAL;
b8a891cb 2366 ts->kind = gfc_default_logical_kind;
4ee9c684 2367 goto get_kind;
2368 }
2369
2370 m = gfc_match (" type ( %n )", name);
2371 if (m != MATCH_YES)
8ca7f89c 2372 {
2373 m = gfc_match (" class ( %n )", name);
2374 if (m != MATCH_YES)
2375 return m;
2376 ts->is_class = 1;
2377
2378 /* TODO: Implement Polymorphism. */
2379 gfc_warning ("Polymorphic entities are not yet implemented. "
2380 "CLASS will be treated like TYPE at %C");
2381 }
4ee9c684 2382
077932f9 2383 ts->type = BT_DERIVED;
2384
2385 /* Defer association of the derived type until the end of the
2386 specification block. However, if the derived type can be
2387 found, add it to the typespec. */
2388 if (gfc_matching_function)
67a51c8e 2389 {
077932f9 2390 ts->derived = NULL;
2391 if (gfc_current_state () != COMP_INTERFACE
2392 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2393 ts->derived = sym;
67a51c8e 2394 return MATCH_YES;
2395 }
2396
2397 /* Search for the name but allow the components to be defined later. If
2398 type = -1, this typespec has been seen in a function declaration but
077932f9 2399 the type could not be accessed at that point. */
2400 sym = NULL;
67a51c8e 2401 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
4ee9c684 2402 {
2403 gfc_error ("Type name '%s' at %C is ambiguous", name);
2404 return MATCH_ERROR;
2405 }
67a51c8e 2406 else if (ts->kind == -1)
2407 {
077932f9 2408 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2409 || gfc_current_ns->has_import_set;
2410 if (gfc_find_symbol (name, NULL, iface, &sym))
67a51c8e 2411 {
2412 gfc_error ("Type name '%s' at %C is ambiguous", name);
2413 return MATCH_ERROR;
2414 }
2415
077932f9 2416 ts->kind = 0;
67a51c8e 2417 if (sym == NULL)
2418 return MATCH_NO;
2419 }
4ee9c684 2420
2421 if (sym->attr.flavor != FL_DERIVED
950683ed 2422 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4ee9c684 2423 return MATCH_ERROR;
2424
077932f9 2425 gfc_set_sym_referenced (sym);
4ee9c684 2426 ts->derived = sym;
2427
2428 return MATCH_YES;
2429
2430get_kind:
2431 /* For all types except double, derived and character, look for an
2432 optional kind specifier. MATCH_NO is actually OK at this point. */
39351103 2433 if (implicit_flag == 1)
4ee9c684 2434 return MATCH_YES;
2435
18f3698a 2436 if (gfc_current_form == FORM_FREE)
2437 {
e0be6f02 2438 c = gfc_peek_ascii_char();
18f3698a 2439 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1a9745d2 2440 && c != ':' && c != ',')
18f3698a 2441 return MATCH_NO;
2442 }
2443
67a51c8e 2444 m = gfc_match_kind_spec (ts, false);
4ee9c684 2445 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2446 m = gfc_match_old_kind_spec (ts);
2447
077932f9 2448 /* Defer association of the KIND expression of function results
2449 until after USE and IMPORT statements. */
2450 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2451 || gfc_matching_function)
2452 return MATCH_YES;
2453
4ee9c684 2454 if (m == MATCH_NO)
2455 m = MATCH_YES; /* No kind specifier found. */
2456
2457 return m;
2458}
2459
2460
39351103 2461/* Match an IMPLICIT NONE statement. Actually, this statement is
2462 already matched in parse.c, or we would not end up here in the
2463 first place. So the only thing we need to check, is if there is
2464 trailing garbage. If not, the match is successful. */
2465
2466match
2467gfc_match_implicit_none (void)
2468{
39351103 2469 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2470}
2471
2472
2473/* Match the letter range(s) of an IMPLICIT statement. */
2474
2475static match
b70528c7 2476match_implicit_range (void)
39351103 2477{
e0be6f02 2478 char c, c1, c2;
2479 int inner;
39351103 2480 locus cur_loc;
2481
2482 cur_loc = gfc_current_locus;
2483
2484 gfc_gobble_whitespace ();
e0be6f02 2485 c = gfc_next_ascii_char ();
39351103 2486 if (c != '(')
2487 {
2488 gfc_error ("Missing character range in IMPLICIT at %C");
2489 goto bad;
2490 }
2491
2492 inner = 1;
2493 while (inner)
2494 {
2495 gfc_gobble_whitespace ();
e0be6f02 2496 c1 = gfc_next_ascii_char ();
39351103 2497 if (!ISALPHA (c1))
2498 goto bad;
2499
2500 gfc_gobble_whitespace ();
e0be6f02 2501 c = gfc_next_ascii_char ();
39351103 2502
2503 switch (c)
2504 {
2505 case ')':
f6d0e37a 2506 inner = 0; /* Fall through. */
39351103 2507
2508 case ',':
2509 c2 = c1;
2510 break;
2511
2512 case '-':
2513 gfc_gobble_whitespace ();
e0be6f02 2514 c2 = gfc_next_ascii_char ();
39351103 2515 if (!ISALPHA (c2))
2516 goto bad;
2517
2518 gfc_gobble_whitespace ();
e0be6f02 2519 c = gfc_next_ascii_char ();
39351103 2520
2521 if ((c != ',') && (c != ')'))
2522 goto bad;
2523 if (c == ')')
2524 inner = 0;
2525
2526 break;
2527
2528 default:
2529 goto bad;
2530 }
2531
2532 if (c1 > c2)
2533 {
2534 gfc_error ("Letters must be in alphabetic order in "
2535 "IMPLICIT statement at %C");
2536 goto bad;
2537 }
2538
2539 /* See if we can add the newly matched range to the pending
1a9745d2 2540 implicits from this IMPLICIT statement. We do not check for
2541 conflicts with whatever earlier IMPLICIT statements may have
2542 set. This is done when we've successfully finished matching
2543 the current one. */
b70528c7 2544 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
39351103 2545 goto bad;
2546 }
2547
2548 return MATCH_YES;
2549
2550bad:
2551 gfc_syntax_error (ST_IMPLICIT);
2552
2553 gfc_current_locus = cur_loc;
2554 return MATCH_ERROR;
2555}
2556
2557
2558/* Match an IMPLICIT statement, storing the types for
2559 gfc_set_implicit() if the statement is accepted by the parser.
2560 There is a strange looking, but legal syntactic construction
2561 possible. It looks like:
2562
2563 IMPLICIT INTEGER (a-b) (c-d)
2564
2565 This is legal if "a-b" is a constant expression that happens to
2566 equal one of the legal kinds for integers. The real problem
2567 happens with an implicit specification that looks like:
2568
2569 IMPLICIT INTEGER (a-b)
2570
2571 In this case, a typespec matcher that is "greedy" (as most of the
2572 matchers are) gobbles the character range as a kindspec, leaving
2573 nothing left. We therefore have to go a bit more slowly in the
2574 matching process by inhibiting the kindspec checking during
2575 typespec matching and checking for a kind later. */
2576
2577match
2578gfc_match_implicit (void)
2579{
2580 gfc_typespec ts;
2581 locus cur_loc;
e0be6f02 2582 char c;
39351103 2583 match m;
2584
52179f31 2585 gfc_clear_ts (&ts);
2586
39351103 2587 /* We don't allow empty implicit statements. */
2588 if (gfc_match_eos () == MATCH_YES)
2589 {
2590 gfc_error ("Empty IMPLICIT statement at %C");
2591 return MATCH_ERROR;
2592 }
2593
39351103 2594 do
2595 {
b70528c7 2596 /* First cleanup. */
2597 gfc_clear_new_implicit ();
2598
39351103 2599 /* A basic type is mandatory here. */
67a51c8e 2600 m = gfc_match_type_spec (&ts, 1);
39351103 2601 if (m == MATCH_ERROR)
2602 goto error;
2603 if (m == MATCH_NO)
2604 goto syntax;
2605
2606 cur_loc = gfc_current_locus;
b70528c7 2607 m = match_implicit_range ();
39351103 2608
2609 if (m == MATCH_YES)
2610 {
b70528c7 2611 /* We may have <TYPE> (<RANGE>). */
39351103 2612 gfc_gobble_whitespace ();
e0be6f02 2613 c = gfc_next_ascii_char ();
39351103 2614 if ((c == '\n') || (c == ','))
b70528c7 2615 {
2616 /* Check for CHARACTER with no length parameter. */
2617 if (ts.type == BT_CHARACTER && !ts.cl)
2618 {
b8a891cb 2619 ts.kind = gfc_default_character_kind;
5d50997a 2620 ts.cl = gfc_new_charlen (gfc_current_ns);
b70528c7 2621 ts.cl->length = gfc_int_expr (1);
2622 }
2623
2624 /* Record the Successful match. */
2625 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2626 return MATCH_ERROR;
2627 continue;
2628 }
39351103 2629
2630 gfc_current_locus = cur_loc;
2631 }
2632
b70528c7 2633 /* Discard the (incorrectly) matched range. */
2634 gfc_clear_new_implicit ();
2635
2636 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2637 if (ts.type == BT_CHARACTER)
2638 m = match_char_spec (&ts);
2639 else
39351103 2640 {
67a51c8e 2641 m = gfc_match_kind_spec (&ts, false);
39351103 2642 if (m == MATCH_NO)
b70528c7 2643 {
2644 m = gfc_match_old_kind_spec (&ts);
2645 if (m == MATCH_ERROR)
2646 goto error;
2647 if (m == MATCH_NO)
2648 goto syntax;
2649 }
39351103 2650 }
b70528c7 2651 if (m == MATCH_ERROR)
2652 goto error;
39351103 2653
b70528c7 2654 m = match_implicit_range ();
39351103 2655 if (m == MATCH_ERROR)
2656 goto error;
2657 if (m == MATCH_NO)
2658 goto syntax;
2659
2660 gfc_gobble_whitespace ();
e0be6f02 2661 c = gfc_next_ascii_char ();
39351103 2662 if ((c != '\n') && (c != ','))
2663 goto syntax;
2664
b70528c7 2665 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2666 return MATCH_ERROR;
39351103 2667 }
2668 while (c == ',');
2669
b70528c7 2670 return MATCH_YES;
39351103 2671
2672syntax:
2673 gfc_syntax_error (ST_IMPLICIT);
2674
2675error:
2676 return MATCH_ERROR;
2677}
2678
f6d0e37a 2679
d67fc9ae 2680match
2681gfc_match_import (void)
2682{
2683 char name[GFC_MAX_SYMBOL_LEN + 1];
2684 match m;
2685 gfc_symbol *sym;
2686 gfc_symtree *st;
2687
f6d0e37a 2688 if (gfc_current_ns->proc_name == NULL
2689 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
d67fc9ae 2690 {
2691 gfc_error ("IMPORT statement at %C only permitted in "
2692 "an INTERFACE body");
2693 return MATCH_ERROR;
2694 }
2695
1a9745d2 2696 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
d67fc9ae 2697 == FAILURE)
2698 return MATCH_ERROR;
2699
2700 if (gfc_match_eos () == MATCH_YES)
2701 {
2702 /* All host variables should be imported. */
2703 gfc_current_ns->has_import_set = 1;
2704 return MATCH_YES;
2705 }
2706
2707 if (gfc_match (" ::") == MATCH_YES)
2708 {
2709 if (gfc_match_eos () == MATCH_YES)
1a9745d2 2710 {
2711 gfc_error ("Expecting list of named entities at %C");
2712 return MATCH_ERROR;
2713 }
d67fc9ae 2714 }
2715
2716 for(;;)
2717 {
2718 m = gfc_match (" %n", name);
2719 switch (m)
2720 {
2721 case MATCH_YES:
096d4ad9 2722 if (gfc_current_ns->parent != NULL
f6d0e37a 2723 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
096d4ad9 2724 {
2725 gfc_error ("Type name '%s' at %C is ambiguous", name);
2726 return MATCH_ERROR;
2727 }
2728 else if (gfc_current_ns->proc_name->ns->parent != NULL
f6d0e37a 2729 && gfc_find_symbol (name,
2730 gfc_current_ns->proc_name->ns->parent,
2731 1, &sym))
1a9745d2 2732 {
2733 gfc_error ("Type name '%s' at %C is ambiguous", name);
2734 return MATCH_ERROR;
2735 }
2736
2737 if (sym == NULL)
2738 {
2739 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2740 "at %C - does not exist.", name);
2741 return MATCH_ERROR;
2742 }
2743
e14bee04 2744 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
1a9745d2 2745 {
2746 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2747 "at %C.", name);
2748 goto next_item;
2749 }
2750
a15d1caf 2751 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
1a9745d2 2752 st->n.sym = sym;
2753 sym->refs++;
c723595c 2754 sym->attr.imported = 1;
d67fc9ae 2755
2756 goto next_item;
2757
2758 case MATCH_NO:
2759 break;
2760
2761 case MATCH_ERROR:
2762 return MATCH_ERROR;
2763 }
2764
2765 next_item:
2766 if (gfc_match_eos () == MATCH_YES)
2767 break;
2768 if (gfc_match_char (',') != MATCH_YES)
2769 goto syntax;
2770 }
2771
2772 return MATCH_YES;
2773
2774syntax:
2775 gfc_error ("Syntax error in IMPORT statement at %C");
2776 return MATCH_ERROR;
2777}
39351103 2778
f6d0e37a 2779
c72e5f7e 2780/* A minimal implementation of gfc_match without whitespace, escape
2781 characters or variable arguments. Returns true if the next
2782 characters match the TARGET template exactly. */
2783
2784static bool
2785match_string_p (const char *target)
2786{
2787 const char *p;
2788
2789 for (p = target; *p; p++)
e0be6f02 2790 if ((char) gfc_next_ascii_char () != *p)
c72e5f7e 2791 return false;
2792 return true;
2793}
2794
4ee9c684 2795/* Matches an attribute specification including array specs. If
2796 successful, leaves the variables current_attr and current_as
2797 holding the specification. Also sets the colon_seen variable for
2798 later use by matchers associated with initializations.
2799
2800 This subroutine is a little tricky in the sense that we don't know
2801 if we really have an attr-spec until we hit the double colon.
2802 Until that time, we can only return MATCH_NO. This forces us to
2803 check for duplicate specification at this level. */
2804
2805static match
2806match_attr_spec (void)
2807{
4ee9c684 2808 /* Modifiers that can exist in a type statement. */
2809 typedef enum
2810 { GFC_DECL_BEGIN = 0,
2811 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2812 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3ea52af3 2813 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2814 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
c72e5f7e 2815 DECL_IS_BIND_C, DECL_NONE,
4ee9c684 2816 GFC_DECL_END /* Sentinel */
2817 }
2818 decl_types;
2819
2820/* GFC_DECL_END is the sentinel, index starts at 0. */
2821#define NUM_DECL GFC_DECL_END
2822
4ee9c684 2823 locus start, seen_at[NUM_DECL];
2824 int seen[NUM_DECL];
9f1b7d17 2825 unsigned int d;
4ee9c684 2826 const char *attr;
2827 match m;
f6bb8f4b 2828 gfc_try t;
4ee9c684 2829
2830 gfc_clear_attr (&current_attr);
cbb9e6aa 2831 start = gfc_current_locus;
4ee9c684 2832
2833 current_as = NULL;
2834 colon_seen = 0;
2835
2836 /* See if we get all of the keywords up to the final double colon. */
2837 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2838 seen[d] = 0;
2839
2840 for (;;)
2841 {
e0be6f02 2842 char ch;
c5d33754 2843
c72e5f7e 2844 d = DECL_NONE;
2845 gfc_gobble_whitespace ();
2846
e0be6f02 2847 ch = gfc_next_ascii_char ();
c72e5f7e 2848 if (ch == ':')
2849 {
2850 /* This is the successful exit condition for the loop. */
e0be6f02 2851 if (gfc_next_ascii_char () == ':')
c72e5f7e 2852 break;
2853 }
2854 else if (ch == ',')
c5d33754 2855 {
c5d33754 2856 gfc_gobble_whitespace ();
e0be6f02 2857 switch (gfc_peek_ascii_char ())
c5d33754 2858 {
c72e5f7e 2859 case 'a':
2860 if (match_string_p ("allocatable"))
2861 d = DECL_ALLOCATABLE;
2862 break;
2863
2864 case 'b':
c5d33754 2865 /* Try and match the bind(c). */
75ae7f6c 2866 m = gfc_match_bind_c (NULL, true);
5cf92482 2867 if (m == MATCH_YES)
c5d33754 2868 d = DECL_IS_BIND_C;
5cf92482 2869 else if (m == MATCH_ERROR)
2870 goto cleanup;
c72e5f7e 2871 break;
2872
2873 case 'd':
2874 if (match_string_p ("dimension"))
2875 d = DECL_DIMENSION;
2876 break;
2877
2878 case 'e':
2879 if (match_string_p ("external"))
2880 d = DECL_EXTERNAL;
2881 break;
2882
2883 case 'i':
2884 if (match_string_p ("int"))
2885 {
e0be6f02 2886 ch = gfc_next_ascii_char ();
c72e5f7e 2887 if (ch == 'e')
2888 {
2889 if (match_string_p ("nt"))
2890 {
2891 /* Matched "intent". */
2892 /* TODO: Call match_intent_spec from here. */
2893 if (gfc_match (" ( in out )") == MATCH_YES)
2894 d = DECL_INOUT;
2895 else if (gfc_match (" ( in )") == MATCH_YES)
2896 d = DECL_IN;
2897 else if (gfc_match (" ( out )") == MATCH_YES)
2898 d = DECL_OUT;
2899 }
2900 }
2901 else if (ch == 'r')
2902 {
2903 if (match_string_p ("insic"))
2904 {
2905 /* Matched "intrinsic". */
2906 d = DECL_INTRINSIC;
2907 }
2908 }
2909 }
2910 break;
2911
2912 case 'o':
2913 if (match_string_p ("optional"))
2914 d = DECL_OPTIONAL;
2915 break;
2916
2917 case 'p':
e0be6f02 2918 gfc_next_ascii_char ();
2919 switch (gfc_next_ascii_char ())
c72e5f7e 2920 {
2921 case 'a':
2922 if (match_string_p ("rameter"))
2923 {
2924 /* Matched "parameter". */
2925 d = DECL_PARAMETER;
2926 }
2927 break;
2928
2929 case 'o':
2930 if (match_string_p ("inter"))
2931 {
2932 /* Matched "pointer". */
2933 d = DECL_POINTER;
2934 }
2935 break;
2936
2937 case 'r':
e0be6f02 2938 ch = gfc_next_ascii_char ();
c72e5f7e 2939 if (ch == 'i')
2940 {
2941 if (match_string_p ("vate"))
2942 {
2943 /* Matched "private". */
2944 d = DECL_PRIVATE;
2945 }
2946 }
2947 else if (ch == 'o')
2948 {
2949 if (match_string_p ("tected"))
2950 {
2951 /* Matched "protected". */
2952 d = DECL_PROTECTED;
2953 }
2954 }
2955 break;
2956
2957 case 'u':
2958 if (match_string_p ("blic"))
2959 {
2960 /* Matched "public". */
2961 d = DECL_PUBLIC;
2962 }
2963 break;
2964 }
2965 break;
2966
2967 case 's':
2968 if (match_string_p ("save"))
2969 d = DECL_SAVE;
2970 break;
2971
2972 case 't':
2973 if (match_string_p ("target"))
2974 d = DECL_TARGET;
2975 break;
2976
2977 case 'v':
e0be6f02 2978 gfc_next_ascii_char ();
2979 ch = gfc_next_ascii_char ();
c72e5f7e 2980 if (ch == 'a')
2981 {
2982 if (match_string_p ("lue"))
2983 {
2984 /* Matched "value". */
2985 d = DECL_VALUE;
2986 }
2987 }
2988 else if (ch == 'o')
2989 {
2990 if (match_string_p ("latile"))
2991 {
2992 /* Matched "volatile". */
2993 d = DECL_VOLATILE;
2994 }
2995 }
2996 break;
c5d33754 2997 }
2998 }
f3f9b222 2999
c72e5f7e 3000 /* No double colon and no recognizable decl_type, so assume that
3001 we've been looking at something else the whole time. */
3002 if (d == DECL_NONE)
3003 {
3004 m = MATCH_NO;
3005 goto cleanup;
3006 }
e14bee04 3007
7e221851 3008 /* Check to make sure any parens are paired up correctly. */
3009 if (gfc_match_parens () == MATCH_ERROR)
3010 {
3011 m = MATCH_ERROR;
3012 goto cleanup;
3013 }
3014
4ee9c684 3015 seen[d]++;
cbb9e6aa 3016 seen_at[d] = gfc_current_locus;
4ee9c684 3017
3018 if (d == DECL_DIMENSION)
3019 {
3020 m = gfc_match_array_spec (&current_as);
3021
3022 if (m == MATCH_NO)
3023 {
3024 gfc_error ("Missing dimension specification at %C");
3025 m = MATCH_ERROR;
3026 }
3027
3028 if (m == MATCH_ERROR)
3029 goto cleanup;
3030 }
3031 }
3032
4ee9c684 3033 /* Since we've seen a double colon, we have to be looking at an
3034 attr-spec. This means that we can now issue errors. */
3035 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3036 if (seen[d] > 1)
3037 {
3038 switch (d)
3039 {
3040 case DECL_ALLOCATABLE:
3041 attr = "ALLOCATABLE";
3042 break;
3043 case DECL_DIMENSION:
3044 attr = "DIMENSION";
3045 break;
3046 case DECL_EXTERNAL:
3047 attr = "EXTERNAL";
3048 break;
3049 case DECL_IN:
3050 attr = "INTENT (IN)";
3051 break;
3052 case DECL_OUT:
3053 attr = "INTENT (OUT)";
3054 break;
3055 case DECL_INOUT:
3056 attr = "INTENT (IN OUT)";
3057 break;
3058 case DECL_INTRINSIC:
3059 attr = "INTRINSIC";
3060 break;
3061 case DECL_OPTIONAL:
3062 attr = "OPTIONAL";
3063 break;
3064 case DECL_PARAMETER:
3065 attr = "PARAMETER";
3066 break;
3067 case DECL_POINTER:
3068 attr = "POINTER";
3069 break;
3ea52af3 3070 case DECL_PROTECTED:
3071 attr = "PROTECTED";
3072 break;
4ee9c684 3073 case DECL_PRIVATE:
3074 attr = "PRIVATE";
3075 break;
3076 case DECL_PUBLIC:
3077 attr = "PUBLIC";
3078 break;
3079 case DECL_SAVE:
3080 attr = "SAVE";
3081 break;
3082 case DECL_TARGET:
3083 attr = "TARGET";
3084 break;
c5d33754 3085 case DECL_IS_BIND_C:
3086 attr = "IS_BIND_C";
3087 break;
3088 case DECL_VALUE:
3089 attr = "VALUE";
3090 break;
ef814c81 3091 case DECL_VOLATILE:
3092 attr = "VOLATILE";
3093 break;
4ee9c684 3094 default:
f6d0e37a 3095 attr = NULL; /* This shouldn't happen. */
4ee9c684 3096 }
3097
3098 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3099 m = MATCH_ERROR;
3100 goto cleanup;
3101 }
3102
3103 /* Now that we've dealt with duplicate attributes, add the attributes
3104 to the current attribute. */
3105 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3106 {
3107 if (seen[d] == 0)
3108 continue;
3109
3110 if (gfc_current_state () == COMP_DERIVED
3111 && d != DECL_DIMENSION && d != DECL_POINTER
c72e5f7e 3112 && d != DECL_PRIVATE && d != DECL_PUBLIC
3113 && d != DECL_NONE)
4ee9c684 3114 {
2294b616 3115 if (d == DECL_ALLOCATABLE)
3116 {
1a9745d2 3117 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3118 "attribute at %C in a TYPE definition")
e14bee04 3119 == FAILURE)
2294b616 3120 {
3121 m = MATCH_ERROR;
3122 goto cleanup;
3123 }
1a9745d2 3124 }
3125 else
2294b616 3126 {
3127 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
e14bee04 3128 &seen_at[d]);
2294b616 3129 m = MATCH_ERROR;
3130 goto cleanup;
3131 }
4ee9c684 3132 }
3133
ea13b9b7 3134 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
1a9745d2 3135 && gfc_current_state () != COMP_MODULE)
ea13b9b7 3136 {
3137 if (d == DECL_PRIVATE)
3138 attr = "PRIVATE";
3139 else
3140 attr = "PUBLIC";
e14bee04 3141 if (gfc_current_state () == COMP_DERIVED
3142 && gfc_state_stack->previous
3143 && gfc_state_stack->previous->state == COMP_MODULE)
3144 {
3145 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3146 "at %L in a TYPE definition", attr,
3147 &seen_at[d])
3148 == FAILURE)
3149 {
3150 m = MATCH_ERROR;
3151 goto cleanup;
3152 }
3153 }
3154 else
3155 {
3156 gfc_error ("%s attribute at %L is not allowed outside of the "
3157 "specification part of a module", attr, &seen_at[d]);
3158 m = MATCH_ERROR;
3159 goto cleanup;
3160 }
ea13b9b7 3161 }
3162
4ee9c684 3163 switch (d)
3164 {
3165 case DECL_ALLOCATABLE:
3166 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3167 break;
3168
3169 case DECL_DIMENSION:
950683ed 3170 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
4ee9c684 3171 break;
3172
3173 case DECL_EXTERNAL:
3174 t = gfc_add_external (&current_attr, &seen_at[d]);
3175 break;
3176
3177 case DECL_IN:
3178 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3179 break;
3180
3181 case DECL_OUT:
3182 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3183 break;
3184
3185 case DECL_INOUT:
3186 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3187 break;
3188
3189 case DECL_INTRINSIC:
3190 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3191 break;
3192
3193 case DECL_OPTIONAL:
3194 t = gfc_add_optional (&current_attr, &seen_at[d]);
3195 break;
3196
3197 case DECL_PARAMETER:
950683ed 3198 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
4ee9c684 3199 break;
3200
3201 case DECL_POINTER:
3202 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3203 break;
3204
3ea52af3 3205 case DECL_PROTECTED:
3206 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3207 {
3208 gfc_error ("PROTECTED at %C only allowed in specification "
3209 "part of a module");
3210 t = FAILURE;
3211 break;
3212 }
3213
1a9745d2 3214 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3215 "attribute at %C")
3ea52af3 3216 == FAILURE)
3217 t = FAILURE;
3218 else
3219 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3220 break;
3221
4ee9c684 3222 case DECL_PRIVATE:
950683ed 3223 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3224 &seen_at[d]);
4ee9c684 3225 break;
3226
3227 case DECL_PUBLIC:
950683ed 3228 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3229 &seen_at[d]);
4ee9c684 3230 break;
3231
3232 case DECL_SAVE:
950683ed 3233 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
4ee9c684 3234 break;
3235
3236 case DECL_TARGET:
3237 t = gfc_add_target (&current_attr, &seen_at[d]);
3238 break;
3239
c5d33754 3240 case DECL_IS_BIND_C:
3241 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3242 break;
3243
8f6339b6 3244 case DECL_VALUE:
1a9745d2 3245 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3246 "at %C")
8f6339b6 3247 == FAILURE)
3248 t = FAILURE;
3249 else
3250 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3251 break;
3252
ef814c81 3253 case DECL_VOLATILE:
3254 if (gfc_notify_std (GFC_STD_F2003,
1a9745d2 3255 "Fortran 2003: VOLATILE attribute at %C")
ef814c81 3256 == FAILURE)
3257 t = FAILURE;
3258 else
3259 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3260 break;
3261
4ee9c684 3262 default:
3263 gfc_internal_error ("match_attr_spec(): Bad attribute");
3264 }
3265
3266 if (t == FAILURE)
3267 {
3268 m = MATCH_ERROR;
3269 goto cleanup;
3270 }
3271 }
3272
3273 colon_seen = 1;
3274 return MATCH_YES;
3275
3276cleanup:
cbb9e6aa 3277 gfc_current_locus = start;
4ee9c684 3278 gfc_free_array_spec (current_as);
3279 current_as = NULL;
3280 return m;
3281}
3282
3283
c5d33754 3284/* Set the binding label, dest_label, either with the binding label
3285 stored in the given gfc_typespec, ts, or if none was provided, it
3286 will be the symbol name in all lower case, as required by the draft
3287 (J3/04-007, section 15.4.1). If a binding label was given and
3288 there is more than one argument (num_idents), it is an error. */
3289
f6bb8f4b 3290gfc_try
c5d33754 3291set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3292{
825718f9 3293 if (num_idents > 1 && has_name_equals)
c5d33754 3294 {
825718f9 3295 gfc_error ("Multiple identifiers provided with "
3296 "single NAME= specifier at %C");
3297 return FAILURE;
3298 }
c5d33754 3299
825718f9 3300 if (curr_binding_label[0] != '\0')
3301 {
c5d33754 3302 /* Binding label given; store in temp holder til have sym. */
b1223b39 3303 strcpy (dest_label, curr_binding_label);
c5d33754 3304 }
3305 else
3306 {
3307 /* No binding label given, and the NAME= specifier did not exist,
3308 which means there was no NAME="". */
3309 if (sym_name != NULL && has_name_equals == 0)
b1223b39 3310 strcpy (dest_label, sym_name);
c5d33754 3311 }
3312
3313 return SUCCESS;
3314}
3315
3316
3317/* Set the status of the given common block as being BIND(C) or not,
3318 depending on the given parameter, is_bind_c. */
3319
3320void
3321set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3322{
3323 com_block->is_bind_c = is_bind_c;
3324 return;
3325}
3326
3327
3328/* Verify that the given gfc_typespec is for a C interoperable type. */
3329
f6bb8f4b 3330gfc_try
209d3778 3331verify_c_interop (gfc_typespec *ts)
c5d33754 3332{
c5d33754 3333 if (ts->type == BT_DERIVED && ts->derived != NULL)
3334 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3335 else if (ts->is_c_interop != 1)
3336 return FAILURE;
3337
3338 return SUCCESS;
3339}
3340
3341
3342/* Verify that the variables of a given common block, which has been
3343 defined with the attribute specifier bind(c), to be of a C
3344 interoperable type. Errors will be reported here, if
3345 encountered. */
3346
f6bb8f4b 3347gfc_try
c5d33754 3348verify_com_block_vars_c_interop (gfc_common_head *com_block)
3349{
3350 gfc_symbol *curr_sym = NULL;
f6bb8f4b 3351 gfc_try retval = SUCCESS;
c5d33754 3352
3353 curr_sym = com_block->head;
3354
3355 /* Make sure we have at least one symbol. */
3356 if (curr_sym == NULL)
3357 return retval;
3358
3359 /* Here we know we have a symbol, so we'll execute this loop
3360 at least once. */
3361 do
3362 {
3363 /* The second to last param, 1, says this is in a common block. */
3364 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3365 curr_sym = curr_sym->common_next;
3366 } while (curr_sym != NULL);
3367
3368 return retval;
3369}
3370
3371
3372/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3373 an appropriate error message is reported. */
3374
f6bb8f4b 3375gfc_try
c5d33754 3376verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3377 int is_in_common, gfc_common_head *com_block)
3378{
1f2ab120 3379 bool bind_c_function = false;
f6bb8f4b 3380 gfc_try retval = SUCCESS;
4f7bb9ec 3381
1f2ab120 3382 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3383 bind_c_function = true;
3384
4f7bb9ec 3385 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3386 {
3387 tmp_sym = tmp_sym->result;
3388 /* Make sure it wasn't an implicitly typed result. */
3389 if (tmp_sym->attr.implicit_type)
3390 {
3391 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3392 "%L may not be C interoperable", tmp_sym->name,
3393 &tmp_sym->declared_at);
3394 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3395 /* Mark it as C interoperable to prevent duplicate warnings. */
3396 tmp_sym->ts.is_c_interop = 1;
3397 tmp_sym->attr.is_c_interop = 1;
3398 }
3399 }
1f2ab120 3400
c5d33754 3401 /* Here, we know we have the bind(c) attribute, so if we have
3402 enough type info, then verify that it's a C interop kind.
3403 The info could be in the symbol already, or possibly still in
3404 the given ts (current_ts), so look in both. */
3405 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3406 {
209d3778 3407 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
c5d33754 3408 {
3409 /* See if we're dealing with a sym in a common block or not. */
3410 if (is_in_common == 1)
3411 {
3412 gfc_warning ("Variable '%s' in common block '%s' at %L "
3413 "may not be a C interoperable "
3414 "kind though common block '%s' is BIND(C)",
3415 tmp_sym->name, com_block->name,
3416 &(tmp_sym->declared_at), com_block->name);
3417 }
3418 else
3419 {
3420 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3421 gfc_error ("Type declaration '%s' at %L is not C "
3422 "interoperable but it is BIND(C)",
3423 tmp_sym->name, &(tmp_sym->declared_at));
3424 else
3425 gfc_warning ("Variable '%s' at %L "
3426 "may not be a C interoperable "
3427 "kind but it is bind(c)",
3428 tmp_sym->name, &(tmp_sym->declared_at));
3429 }
3430 }
3431
3432 /* Variables declared w/in a common block can't be bind(c)
3433 since there's no way for C to see these variables, so there's
3434 semantically no reason for the attribute. */
3435 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3436 {
3437 gfc_error ("Variable '%s' in common block '%s' at "
3438 "%L cannot be declared with BIND(C) "
3439 "since it is not a global",
3440 tmp_sym->name, com_block->name,
3441 &(tmp_sym->declared_at));
3442 retval = FAILURE;
3443 }
3444
3445 /* Scalar variables that are bind(c) can not have the pointer
3446 or allocatable attributes. */
3447 if (tmp_sym->attr.is_bind_c == 1)
3448 {
3449 if (tmp_sym->attr.pointer == 1)
3450 {
3451 gfc_error ("Variable '%s' at %L cannot have both the "
3452 "POINTER and BIND(C) attributes",
3453 tmp_sym->name, &(tmp_sym->declared_at));
3454 retval = FAILURE;
3455 }
3456
3457 if (tmp_sym->attr.allocatable == 1)
3458 {
3459 gfc_error ("Variable '%s' at %L cannot have both the "
3460 "ALLOCATABLE and BIND(C) attributes",
3461 tmp_sym->name, &(tmp_sym->declared_at));
3462 retval = FAILURE;
3463 }
3464
1f2ab120 3465 }
3466
3467 /* If it is a BIND(C) function, make sure the return value is a
3468 scalar value. The previous tests in this function made sure
3469 the type is interoperable. */
3470 if (bind_c_function && tmp_sym->as != NULL)
3471 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3472 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3473
3474 /* BIND(C) functions can not return a character string. */
3475 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3476 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3477 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3478 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3479 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
c5d33754 3480 "be a character string", tmp_sym->name,
3481 &(tmp_sym->declared_at));
c5d33754 3482 }
3483
3484 /* See if the symbol has been marked as private. If it has, make sure
3485 there is no binding label and warn the user if there is one. */
3486 if (tmp_sym->attr.access == ACCESS_PRIVATE
3487 && tmp_sym->binding_label[0] != '\0')
3488 /* Use gfc_warning_now because we won't say that the symbol fails
3489 just because of this. */
3490 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3491 "given the binding label '%s'", tmp_sym->name,
3492 &(tmp_sym->declared_at), tmp_sym->binding_label);
3493
3494 return retval;
3495}
3496
3497
3498/* Set the appropriate fields for a symbol that's been declared as
3499 BIND(C) (the is_bind_c flag and the binding label), and verify that
3500 the type is C interoperable. Errors are reported by the functions
3501 used to set/test these fields. */
3502
f6bb8f4b 3503gfc_try
c5d33754 3504set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3505{
f6bb8f4b 3506 gfc_try retval = SUCCESS;
c5d33754 3507
3508 /* TODO: Do we need to make sure the vars aren't marked private? */
3509
3510 /* Set the is_bind_c bit in symbol_attribute. */
3511 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3512
3513 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3514 num_idents) != SUCCESS)
3515 return FAILURE;
3516
3517 return retval;
3518}
3519
3520
3521/* Set the fields marking the given common block as BIND(C), including
3522 a binding label, and report any errors encountered. */
3523
f6bb8f4b 3524gfc_try
c5d33754 3525set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3526{
f6bb8f4b 3527 gfc_try retval = SUCCESS;
c5d33754 3528
3529 /* destLabel, common name, typespec (which may have binding label). */
3530 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3531 != SUCCESS)
3532 return FAILURE;
3533
3534 /* Set the given common block (com_block) to being bind(c) (1). */
3535 set_com_block_bind_c (com_block, 1);
3536
3537 return retval;
3538}
3539
3540
3541/* Retrieve the list of one or more identifiers that the given bind(c)
3542 attribute applies to. */
3543
f6bb8f4b 3544gfc_try
c5d33754 3545get_bind_c_idents (void)
3546{
3547 char name[GFC_MAX_SYMBOL_LEN + 1];
3548 int num_idents = 0;
3549 gfc_symbol *tmp_sym = NULL;
3550 match found_id;
3551 gfc_common_head *com_block = NULL;
3552
3553 if (gfc_match_name (name) == MATCH_YES)
3554 {
3555 found_id = MATCH_YES;
3556 gfc_get_ha_symbol (name, &tmp_sym);
3557 }
3558 else if (match_common_name (name) == MATCH_YES)
3559 {
3560 found_id = MATCH_YES;
3561 com_block = gfc_get_common (name, 0);
3562 }
3563 else
3564 {
3565 gfc_error ("Need either entity or common block name for "
3566 "attribute specification statement at %C");
3567 return FAILURE;
3568 }
3569
3570 /* Save the current identifier and look for more. */
3571 do
3572 {
3573 /* Increment the number of identifiers found for this spec stmt. */
3574 num_idents++;
3575
3576 /* Make sure we have a sym or com block, and verify that it can
3577 be bind(c). Set the appropriate field(s) and look for more
3578 identifiers. */
3579 if (tmp_sym != NULL || com_block != NULL)
3580 {
3581 if (tmp_sym != NULL)
3582 {
3583 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3584 != SUCCESS)
3585 return FAILURE;
3586 }
3587 else
3588 {
3589 if (set_verify_bind_c_com_block(com_block, num_idents)
3590 != SUCCESS)
3591 return FAILURE;
3592 }
3593
3594 /* Look to see if we have another identifier. */
3595 tmp_sym = NULL;
3596 if (gfc_match_eos () == MATCH_YES)
3597 found_id = MATCH_NO;
3598 else if (gfc_match_char (',') != MATCH_YES)
3599 found_id = MATCH_NO;
3600 else if (gfc_match_name (name) == MATCH_YES)
3601 {
3602 found_id = MATCH_YES;
3603 gfc_get_ha_symbol (name, &tmp_sym);
3604 }
3605 else if (match_common_name (name) == MATCH_YES)
3606 {
3607 found_id = MATCH_YES;
3608 com_block = gfc_get_common (name, 0);
3609 }
3610 else
3611 {
3612 gfc_error ("Missing entity or common block name for "
3613 "attribute specification statement at %C");
3614 return FAILURE;
3615 }
3616 }
3617 else
3618 {
3619 gfc_internal_error ("Missing symbol");
3620 }
3621 } while (found_id == MATCH_YES);
3622
3623 /* if we get here we were successful */
3624 return SUCCESS;
3625}
3626
3627
3628/* Try and match a BIND(C) attribute specification statement. */
3629
3630match
3631gfc_match_bind_c_stmt (void)
3632{
3633 match found_match = MATCH_NO;
3634 gfc_typespec *ts;
3635
3636 ts = &current_ts;
3637
3638 /* This may not be necessary. */
3639 gfc_clear_ts (ts);
3640 /* Clear the temporary binding label holder. */
3641 curr_binding_label[0] = '\0';
3642
3643 /* Look for the bind(c). */
75ae7f6c 3644 found_match = gfc_match_bind_c (NULL, true);
c5d33754 3645
3646 if (found_match == MATCH_YES)
3647 {
3648 /* Look for the :: now, but it is not required. */
3649 gfc_match (" :: ");
3650
3651 /* Get the identifier(s) that needs to be updated. This may need to
3652 change to hand the flag(s) for the attr specified so all identifiers
3653 found can have all appropriate parts updated (assuming that the same
3654 spec stmt can have multiple attrs, such as both bind(c) and
3655 allocatable...). */
3656 if (get_bind_c_idents () != SUCCESS)
3657 /* Error message should have printed already. */
3658 return MATCH_ERROR;
3659 }
3660
3661 return found_match;
3662}
3663
3664
4ee9c684 3665/* Match a data declaration statement. */
3666
3667match
3668gfc_match_data_decl (void)
3669{
3670 gfc_symbol *sym;
3671 match m;
3923b69f 3672 int elem;
4ee9c684 3673
c5d33754 3674 num_idents_on_line = 0;
3675
67a51c8e 3676 m = gfc_match_type_spec (&current_ts, 0);
4ee9c684 3677 if (m != MATCH_YES)
3678 return m;
3679
3680 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3681 {
3682 sym = gfc_use_derived (current_ts.derived);
3683
3684 if (sym == NULL)
3685 {
3686 m = MATCH_ERROR;
3687 goto cleanup;
3688 }
3689
3690 current_ts.derived = sym;
3691 }
3692
3693 m = match_attr_spec ();
3694 if (m == MATCH_ERROR)
3695 {
3696 m = MATCH_NO;
3697 goto cleanup;
3698 }
3699
e6b82afc 3700 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3701 && !current_ts.derived->attr.zero_comp)
4ee9c684 3702 {
3703
3704 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3705 goto ok;
3706
40cf8078 3707 gfc_find_symbol (current_ts.derived->name,
1a9745d2 3708 current_ts.derived->ns->parent, 1, &sym);
4ee9c684 3709
40cf8078 3710 /* Any symbol that we find had better be a type definition
1a9745d2 3711 which has its components defined. */
40cf8078 3712 if (sym != NULL && sym->attr.flavor == FL_DERIVED
e6b82afc 3713 && (current_ts.derived->components != NULL
3714 || current_ts.derived->attr.zero_comp))
4ee9c684 3715 goto ok;
3716
40cf8078 3717 /* Now we have an error, which we signal, and then fix up
3718 because the knock-on is plain and simple confusing. */
3719 gfc_error_now ("Derived type at %C has not been previously defined "
1a9745d2 3720 "and so cannot appear in a derived type definition");
40cf8078 3721 current_attr.pointer = 1;
3722 goto ok;
4ee9c684 3723 }
3724
3725ok:
3726 /* If we have an old-style character declaration, and no new-style
3727 attribute specifications, then there a comma is optional between
3728 the type specification and the variable list. */
3729 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3730 gfc_match_char (',');
3731
3923b69f 3732 /* Give the types/attributes to symbols that follow. Give the element
3733 a number so that repeat character length expressions can be copied. */
3734 elem = 1;
4ee9c684 3735 for (;;)
3736 {
c5d33754 3737 num_idents_on_line++;
3923b69f 3738 m = variable_decl (elem++);
4ee9c684 3739 if (m == MATCH_ERROR)
3740 goto cleanup;
3741 if (m == MATCH_NO)
3742 break;
3743
3744 if (gfc_match_eos () == MATCH_YES)
3745 goto cleanup;
3746 if (gfc_match_char (',') != MATCH_YES)
3747 break;
3748 }
3749
8f0bdb23 3750 if (gfc_error_flag_test () == 0)
3751 gfc_error ("Syntax error in data declaration at %C");
4ee9c684 3752 m = MATCH_ERROR;
3753
af29c1f0 3754 gfc_free_data_all (gfc_current_ns);
3755
4ee9c684 3756cleanup:
3757 gfc_free_array_spec (current_as);
3758 current_as = NULL;
3759 return m;
3760}
3761
3762
3763/* Match a prefix associated with a function or subroutine
3764 declaration. If the typespec pointer is nonnull, then a typespec
3765 can be matched. Note that if nothing matches, MATCH_YES is
3766 returned (the null string was matched). */
3767
077932f9 3768match
3769gfc_match_prefix (gfc_typespec *ts)
4ee9c684 3770{
5b11d932 3771 bool seen_type;
4ee9c684 3772
3773 gfc_clear_attr (&current_attr);
3774 seen_type = 0;
3775
d1645c7b 3776 gcc_assert (!gfc_matching_prefix);
3777 gfc_matching_prefix = true;
40de255b 3778
4ee9c684 3779loop:
3780 if (!seen_type && ts != NULL
67a51c8e 3781 && gfc_match_type_spec (ts, 0) == MATCH_YES
4ee9c684 3782 && gfc_match_space () == MATCH_YES)
3783 {
3784
3785 seen_type = 1;
3786 goto loop;
3787 }
3788
3789 if (gfc_match ("elemental% ") == MATCH_YES)
3790 {
3791 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
40de255b 3792 goto error;
4ee9c684 3793
3794 goto loop;
3795 }
3796
3797 if (gfc_match ("pure% ") == MATCH_YES)
3798 {
3799 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
40de255b 3800 goto error;
4ee9c684 3801
3802 goto loop;
3803 }
3804
3805 if (gfc_match ("recursive% ") == MATCH_YES)
3806 {
3807 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
40de255b 3808 goto error;
4ee9c684 3809
3810 goto loop;
3811 }
3812
3813 /* At this point, the next item is not a prefix. */
d1645c7b 3814 gcc_assert (gfc_matching_prefix);
3815 gfc_matching_prefix = false;
4ee9c684 3816 return MATCH_YES;
40de255b 3817
3818error:
d1645c7b 3819 gcc_assert (gfc_matching_prefix);
3820 gfc_matching_prefix = false;
40de255b 3821 return MATCH_ERROR;
4ee9c684 3822}
3823
3824
077932f9 3825/* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4ee9c684 3826
f6bb8f4b 3827static gfc_try
1a9745d2 3828copy_prefix (symbol_attribute *dest, locus *where)
4ee9c684 3829{
4ee9c684 3830 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3831 return FAILURE;
3832
3833 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3834 return FAILURE;
3835
3836 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3837 return FAILURE;
3838
3839 return SUCCESS;
3840}
3841
3842
3843/* Match a formal argument list. */
3844
3845match
1a9745d2 3846gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4ee9c684 3847{
3848 gfc_formal_arglist *head, *tail, *p, *q;
3849 char name[GFC_MAX_SYMBOL_LEN + 1];
3850 gfc_symbol *sym;
3851 match m;
3852
3853 head = tail = NULL;
3854
3855 if (gfc_match_char ('(') != MATCH_YES)
3856 {
3857 if (null_flag)
3858 goto ok;
3859 return MATCH_NO;
3860 }
3861
3862 if (gfc_match_char (')') == MATCH_YES)
3863 goto ok;
3864
3865 for (;;)
3866 {
3867 if (gfc_match_char ('*') == MATCH_YES)
3868 sym = NULL;
3869 else
3870 {
3871 m = gfc_match_name (name);
3872 if (m != MATCH_YES)
3873 goto cleanup;
3874
3875 if (gfc_get_symbol (name, NULL, &sym))
3876 goto cleanup;
3877 }
3878
3879 p = gfc_get_formal_arglist ();
3880
3881 if (head == NULL)
3882 head = tail = p;
3883 else
3884 {
3885 tail->next = p;
3886 tail = p;
3887 }
3888
3889 tail->sym = sym;
3890
3891 /* We don't add the VARIABLE flavor because the name could be a
1a9745d2 3892 dummy procedure. We don't apply these attributes to formal
3893 arguments of statement functions. */
4ee9c684 3894 if (sym != NULL && !st_flag
950683ed 3895 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4ee9c684 3896 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3897 {
3898 m = MATCH_ERROR;
3899 goto cleanup;
3900 }
3901
3902 /* The name of a program unit can be in a different namespace,
1a9745d2 3903 so check for it explicitly. After the statement is accepted,
3904 the name is checked for especially in gfc_get_symbol(). */
4ee9c684 3905 if (gfc_new_block != NULL && sym != NULL
3906 && strcmp (sym->name, gfc_new_block->name) == 0)
3907 {
3908 gfc_error ("Name '%s' at %C is the name of the procedure",
3909 sym->name);
3910 m = MATCH_ERROR;
3911 goto cleanup;
3912 }
3913
3914 if (gfc_match_char (')') == MATCH_YES)
3915 goto ok;
3916
3917 m = gfc_match_char (',');
3918 if (m != MATCH_YES)
3919 {
3920 gfc_error ("Unexpected junk in formal argument list at %C");
3921 goto cleanup;
3922 }
3923 }
3924
3925ok:
3926 /* Check for duplicate symbols in the formal argument list. */
3927 if (head != NULL)
3928 {
3929 for (p = head; p->next; p = p->next)
3930 {
3931 if (p->sym == NULL)
3932 continue;
3933
3934 for (q = p->next; q; q = q->next)
3935 if (p->sym == q->sym)
3936 {
1a9745d2 3937 gfc_error ("Duplicate symbol '%s' in formal argument list "
3938 "at %C", p->sym->name);
4ee9c684 3939
3940 m = MATCH_ERROR;
3941 goto cleanup;
3942 }
3943 }
3944 }
3945
f6d0e37a 3946 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3947 == FAILURE)
4ee9c684 3948 {
3949 m = MATCH_ERROR;
3950 goto cleanup;
3951 }
3952
3953 return MATCH_YES;
3954
3955cleanup:
3956 gfc_free_formal_arglist (head);
3957 return m;
3958}
3959
3960
3961/* Match a RESULT specification following a function declaration or
3962 ENTRY statement. Also matches the end-of-statement. */
3963
3964static match
f6d0e37a 3965match_result (gfc_symbol *function, gfc_symbol **result)
4ee9c684 3966{
3967 char name[GFC_MAX_SYMBOL_LEN + 1];
3968 gfc_symbol *r;
3969 match m;
3970
3971 if (gfc_match (" result (") != MATCH_YES)
3972 return MATCH_NO;
3973
3974 m = gfc_match_name (name);
3975 if (m != MATCH_YES)
3976 return m;
3977
c5d33754 3978 /* Get the right paren, and that's it because there could be the
3979 bind(c) attribute after the result clause. */
3980 if (gfc_match_char(')') != MATCH_YES)
4ee9c684 3981 {
c5d33754 3982 /* TODO: should report the missing right paren here. */
4ee9c684 3983 return MATCH_ERROR;
3984 }
3985
3986 if (strcmp (function->name, name) == 0)
3987 {
1a9745d2 3988 gfc_error ("RESULT variable at %C must be different than function name");
4ee9c684 3989 return MATCH_ERROR;
3990 }
3991
3992 if (gfc_get_symbol (name, NULL, &r))
3993 return MATCH_ERROR;
3994
f6d3042b 3995 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4ee9c684 3996 return MATCH_ERROR;
3997
3998 *result = r;
3999
4000 return MATCH_YES;
4001}
4002
4003
c5d33754 4004/* Match a function suffix, which could be a combination of a result
4005 clause and BIND(C), either one, or neither. The draft does not
4006 require them to come in a specific order. */
4007
4008match
4009gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4010{
4011 match is_bind_c; /* Found bind(c). */
4012 match is_result; /* Found result clause. */
4013 match found_match; /* Status of whether we've found a good match. */
e0be6f02 4014 char peek_char; /* Character we're going to peek at. */
75ae7f6c 4015 bool allow_binding_name;
c5d33754 4016
4017 /* Initialize to having found nothing. */
4018 found_match = MATCH_NO;
4019 is_bind_c = MATCH_NO;
4020 is_result = MATCH_NO;
4021
4022 /* Get the next char to narrow between result and bind(c). */
4023 gfc_gobble_whitespace ();
e0be6f02 4024 peek_char = gfc_peek_ascii_char ();
c5d33754 4025
75ae7f6c 4026 /* C binding names are not allowed for internal procedures. */
4027 if (gfc_current_state () == COMP_CONTAINS
4028 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4029 allow_binding_name = false;
4030 else
4031 allow_binding_name = true;
4032
c5d33754 4033 switch (peek_char)
4034 {
4035 case 'r':
4036 /* Look for result clause. */
4037 is_result = match_result (sym, result);
4038 if (is_result == MATCH_YES)
4039 {
4040 /* Now see if there is a bind(c) after it. */
75ae7f6c 4041 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 4042 /* We've found the result clause and possibly bind(c). */
4043 found_match = MATCH_YES;
4044 }
4045 else
4046 /* This should only be MATCH_ERROR. */
4047 found_match = is_result;
4048 break;
4049 case 'b':
4050 /* Look for bind(c) first. */
75ae7f6c 4051 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 4052 if (is_bind_c == MATCH_YES)
4053 {
4054 /* Now see if a result clause followed it. */
4055 is_result = match_result (sym, result);
4056 found_match = MATCH_YES;
4057 }
4058 else
4059 {
4060 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4061 found_match = MATCH_ERROR;
4062 }
4063 break;
4064 default:
4065 gfc_error ("Unexpected junk after function declaration at %C");
4066 found_match = MATCH_ERROR;
4067 break;
4068 }
4069
c5d33754 4070 if (is_bind_c == MATCH_YES)
4518e961 4071 {
75ae7f6c 4072 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4518e961 4073 if (gfc_current_state () == COMP_CONTAINS
75ae7f6c 4074 && sym->ns->proc_name->attr.flavor != FL_MODULE
2dcae6c7 4075 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4076 "at %L may not be specified for an internal "
4077 "procedure", &gfc_current_locus)
75ae7f6c 4078 == FAILURE)
4079 return MATCH_ERROR;
4080
4518e961 4081 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4082 == FAILURE)
4083 return MATCH_ERROR;
4084 }
c5d33754 4085
4086 return found_match;
4087}
4088
4089
1e057e9b 4090/* Procedure pointer return value without RESULT statement:
4091 Add "hidden" result variable named "ppr@". */
4092
4093static gfc_try
4094add_hidden_procptr_result (gfc_symbol *sym)
4095{
4096 bool case1,case2;
4097
4098 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4099 return FAILURE;
4100
4101 /* First usage case: PROCEDURE and EXTERNAL statements. */
4102 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4103 && strcmp (gfc_current_block ()->name, sym->name) == 0
4104 && sym->attr.external;
4105 /* Second usage case: INTERFACE statements. */
4106 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4107 && gfc_state_stack->previous->state == COMP_FUNCTION
4108 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4109
4110 if (case1 || case2)
4111 {
4112 gfc_symtree *stree;
4113 if (case1)
36b0a1b0 4114 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
1e057e9b 4115 else if (case2)
180a5dc0 4116 {
4117 gfc_symtree *st2;
36b0a1b0 4118 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
180a5dc0 4119 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4120 st2->n.sym = stree->n.sym;
4121 }
1e057e9b 4122 sym->result = stree->n.sym;
4123
4124 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4125 sym->result->attr.pointer = sym->attr.pointer;
4126 sym->result->attr.external = sym->attr.external;
4127 sym->result->attr.referenced = sym->attr.referenced;
e40ac2fe 4128 sym->result->ts = sym->ts;
1e057e9b 4129 sym->attr.proc_pointer = 0;
4130 sym->attr.pointer = 0;
4131 sym->attr.external = 0;
4132 if (sym->result->attr.external && sym->result->attr.pointer)
4133 {
4134 sym->result->attr.pointer = 0;
4135 sym->result->attr.proc_pointer = 1;
4136 }
4137
4138 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4139 }
4140 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4141 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4142 && sym->result && sym->result != sym && sym->result->attr.external
4143 && sym == gfc_current_ns->proc_name
4144 && sym == sym->result->ns->proc_name
4145 && strcmp ("ppr@", sym->result->name) == 0)
4146 {
4147 sym->result->attr.proc_pointer = 1;
4148 sym->attr.pointer = 0;
4149 return SUCCESS;
4150 }
4151 else
4152 return FAILURE;
4153}
4154
4155
64e93293 4156/* Match the interface for a PROCEDURE declaration,
4157 including brackets (R1212). */
af1a34ee 4158
4159static match
64e93293 4160match_procedure_interface (gfc_symbol **proc_if)
af1a34ee 4161{
4162 match m;
1eb2b542 4163 gfc_symtree *st;
af1a34ee 4164 locus old_loc, entry_loc;
1eb2b542 4165 gfc_namespace *old_ns = gfc_current_ns;
4166 char name[GFC_MAX_SYMBOL_LEN + 1];
af1a34ee 4167
1eb2b542 4168 old_loc = entry_loc = gfc_current_locus;
af1a34ee 4169 gfc_clear_ts (&current_ts);
4170
4171 if (gfc_match (" (") != MATCH_YES)
4172 {
4173 gfc_current_locus = entry_loc;
4174 return MATCH_NO;
4175 }
4176
4177 /* Get the type spec. for the procedure interface. */
4178 old_loc = gfc_current_locus;
67a51c8e 4179 m = gfc_match_type_spec (&current_ts, 0);
fd1277c3 4180 gfc_gobble_whitespace ();
e0be6f02 4181 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
af1a34ee 4182 goto got_ts;
4183
4184 if (m == MATCH_ERROR)
4185 return m;
4186
1eb2b542 4187 /* Procedure interface is itself a procedure. */
af1a34ee 4188 gfc_current_locus = old_loc;
1eb2b542 4189 m = gfc_match_name (name);
af1a34ee 4190
1eb2b542 4191 /* First look to see if it is already accessible in the current
4192 namespace because it is use associated or contained. */
4193 st = NULL;
4194 if (gfc_find_sym_tree (name, NULL, 0, &st))
4195 return MATCH_ERROR;
4196
4197 /* If it is still not found, then try the parent namespace, if it
4198 exists and create the symbol there if it is still not found. */
4199 if (gfc_current_ns->parent)
4200 gfc_current_ns = gfc_current_ns->parent;
4201 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4202 return MATCH_ERROR;
4203
4204 gfc_current_ns = old_ns;
4205 *proc_if = st->n.sym;
af1a34ee 4206
4207 /* Various interface checks. */
64e93293 4208 if (*proc_if)
af1a34ee 4209 {
64e93293 4210 (*proc_if)->refs++;
74113644 4211 /* Resolve interface if possible. That way, attr.procedure is only set
4212 if it is declared by a later procedure-declaration-stmt, which is
4213 invalid per C1212. */
64e93293 4214 while ((*proc_if)->ts.interface)
4215 *proc_if = (*proc_if)->ts.interface;
74113644 4216
64e93293 4217 if ((*proc_if)->generic)
af1a34ee 4218 {
64e93293 4219 gfc_error ("Interface '%s' at %C may not be generic",
4220 (*proc_if)->name);
af1a34ee 4221 return MATCH_ERROR;
4222 }
64e93293 4223 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
af1a34ee 4224 {
4225 gfc_error ("Interface '%s' at %C may not be a statement function",
64e93293 4226 (*proc_if)->name);
af1a34ee 4227 return MATCH_ERROR;
4228 }
4229 /* Handle intrinsic procedures. */
64e93293 4230 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4231 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4232 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4233 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4234 (*proc_if)->attr.intrinsic = 1;
4235 if ((*proc_if)->attr.intrinsic
4236 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
af1a34ee 4237 {
4238 gfc_error ("Intrinsic procedure '%s' not allowed "
64e93293 4239 "in PROCEDURE statement at %C", (*proc_if)->name);
af1a34ee 4240 return MATCH_ERROR;
4241 }
af1a34ee 4242 }
4243
4244got_ts:
af1a34ee 4245 if (gfc_match (" )") != MATCH_YES)
4246 {
4247 gfc_current_locus = entry_loc;
4248 return MATCH_NO;
4249 }
4250
64e93293 4251 return MATCH_YES;
4252}
4253
4254
4255/* Match a PROCEDURE declaration (R1211). */
4256
4257static match
4258match_procedure_decl (void)
4259{
4260 match m;
4261 gfc_symbol *sym, *proc_if = NULL;
4262 int num;
4263 gfc_expr *initializer = NULL;
4264
4265 /* Parse interface (with brackets). */
4266 m = match_procedure_interface (&proc_if);
4267 if (m != MATCH_YES)
4268 return m;
4269
4270 /* Parse attributes (with colons). */
af1a34ee 4271 m = match_attr_spec();
4272 if (m == MATCH_ERROR)
4273 return MATCH_ERROR;
4274
4275 /* Get procedure symbols. */
4276 for(num=1;;num++)
4277 {
af1a34ee 4278 m = gfc_match_symbol (&sym, 0);
4279 if (m == MATCH_NO)
4280 goto syntax;
4281 else if (m == MATCH_ERROR)
4282 return m;
4283
4284 /* Add current_attr to the symbol attributes. */
4285 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4286 return MATCH_ERROR;
4287
4288 if (sym->attr.is_bind_c)
4289 {
4290 /* Check for C1218. */
4291 if (!proc_if || !proc_if->attr.is_bind_c)
4292 {
4293 gfc_error ("BIND(C) attribute at %C requires "
4294 "an interface with BIND(C)");
4295 return MATCH_ERROR;
4296 }
4297 /* Check for C1217. */
4298 if (has_name_equals && sym->attr.pointer)
4299 {
4300 gfc_error ("BIND(C) procedure with NAME may not have "
4301 "POINTER attribute at %C");
4302 return MATCH_ERROR;
4303 }
4304 if (has_name_equals && sym->attr.dummy)
4305 {
4306 gfc_error ("Dummy procedure at %C may not have "
4307 "BIND(C) attribute with NAME");
4308 return MATCH_ERROR;
4309 }
4310 /* Set binding label for BIND(C). */
4311 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4312 return MATCH_ERROR;
4313 }
4314
cad0ddcf 4315 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
af1a34ee 4316 return MATCH_ERROR;
1e057e9b 4317
4318 if (add_hidden_procptr_result (sym) == SUCCESS)
4319 sym = sym->result;
4320
af1a34ee 4321 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4322 return MATCH_ERROR;
4323
4324 /* Set interface. */
4325 if (proc_if != NULL)
f7d7a083 4326 {
13a834aa 4327 if (sym->ts.type != BT_UNKNOWN)
4328 {
4329 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4330 sym->name, &gfc_current_locus,
4331 gfc_basic_typename (sym->ts.type));
4332 return MATCH_ERROR;
4333 }
2cd2bb5c 4334 sym->ts.interface = proc_if;
f7d7a083 4335 sym->attr.untyped = 1;
180a5dc0 4336 sym->attr.if_source = IFSRC_IFBODY;
f7d7a083 4337 }
af1a34ee 4338 else if (current_ts.type != BT_UNKNOWN)
4339 {
13a834aa 4340 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4341 return MATCH_ERROR;
2cd2bb5c 4342 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4343 sym->ts.interface->ts = current_ts;
4344 sym->ts.interface->attr.function = 1;
4345 sym->attr.function = sym->ts.interface->attr.function;
180a5dc0 4346 sym->attr.if_source = IFSRC_UNKNOWN;
af1a34ee 4347 }
4348
cad0ddcf 4349 if (gfc_match (" =>") == MATCH_YES)
4350 {
4351 if (!current_attr.pointer)
4352 {
4353 gfc_error ("Initialization at %C isn't for a pointer variable");
4354 m = MATCH_ERROR;
4355 goto cleanup;
4356 }
4357
4358 m = gfc_match_null (&initializer);
4359 if (m == MATCH_NO)
4360 {
4361 gfc_error ("Pointer initialization requires a NULL() at %C");
4362 m = MATCH_ERROR;
4363 }
4364
4365 if (gfc_pure (NULL))
4366 {
4367 gfc_error ("Initialization of pointer at %C is not allowed in "
4368 "a PURE procedure");
4369 m = MATCH_ERROR;
4370 }
4371
4372 if (m != MATCH_YES)
4373 goto cleanup;
4374
4375 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4376 != SUCCESS)
4377 goto cleanup;
4378
4379 }
4380
4381 gfc_set_sym_referenced (sym);
4382
af1a34ee 4383 if (gfc_match_eos () == MATCH_YES)
4384 return MATCH_YES;
4385 if (gfc_match_char (',') != MATCH_YES)
4386 goto syntax;
4387 }
4388
4389syntax:
4390 gfc_error ("Syntax error in PROCEDURE statement at %C");
4391 return MATCH_ERROR;
cad0ddcf 4392
4393cleanup:
4394 /* Free stuff up and return. */
4395 gfc_free_expr (initializer);
4396 return m;
af1a34ee 4397}
4398
4399
64e93293 4400static match
4401match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4402
4403
4404/* Match a procedure pointer component declaration (R445). */
4405
4406static match
4407match_ppc_decl (void)
4408{
4409 match m;
4410 gfc_symbol *proc_if = NULL;
4411 gfc_typespec ts;
4412 int num;
4413 gfc_component *c;
4414 gfc_expr *initializer = NULL;
4415 gfc_typebound_proc* tb;
4416 char name[GFC_MAX_SYMBOL_LEN + 1];
4417
4418 /* Parse interface (with brackets). */
4419 m = match_procedure_interface (&proc_if);
4420 if (m != MATCH_YES)
4421 goto syntax;
4422
4423 /* Parse attributes. */
4424 tb = XCNEW (gfc_typebound_proc);
4425 tb->where = gfc_current_locus;
4426 m = match_binding_attributes (tb, false, true);
4427 if (m == MATCH_ERROR)
4428 return m;
4429
64e93293 4430 gfc_clear_attr (&current_attr);
4431 current_attr.procedure = 1;
4432 current_attr.proc_pointer = 1;
4433 current_attr.access = tb->access;
4434 current_attr.flavor = FL_PROCEDURE;
4435
4436 /* Match the colons (required). */
4437 if (gfc_match (" ::") != MATCH_YES)
4438 {
4439 gfc_error ("Expected '::' after binding-attributes at %C");
4440 return MATCH_ERROR;
4441 }
4442
4443 /* Check for C450. */
4444 if (!tb->nopass && proc_if == NULL)
4445 {
4446 gfc_error("NOPASS or explicit interface required at %C");
4447 return MATCH_ERROR;
4448 }
4449
4450 /* Match PPC names. */
4451 ts = current_ts;
4452 for(num=1;;num++)
4453 {
4454 m = gfc_match_name (name);
4455 if (m == MATCH_NO)
4456 goto syntax;
4457 else if (m == MATCH_ERROR)
4458 return m;
4459
4460 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4461 return MATCH_ERROR;
4462
4463 /* Add current_attr to the symbol attributes. */
4464 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4465 return MATCH_ERROR;
4466
4467 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4468 return MATCH_ERROR;
4469
4470 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4471 return MATCH_ERROR;
4472
fe9b08a2 4473 c->tb = tb;
4474
64e93293 4475 /* Set interface. */
4476 if (proc_if != NULL)
4477 {
4478 c->ts.interface = proc_if;
4479 c->attr.untyped = 1;
4480 c->attr.if_source = IFSRC_IFBODY;
4481 }
4482 else if (ts.type != BT_UNKNOWN)
4483 {
4484 c->ts = ts;
4485 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4486 c->ts.interface->ts = ts;
4487 c->ts.interface->attr.function = 1;
4488 c->attr.function = c->ts.interface->attr.function;
4489 c->attr.if_source = IFSRC_UNKNOWN;
4490 }
4491
4492 if (gfc_match (" =>") == MATCH_YES)
4493 {
4494 m = gfc_match_null (&initializer);
4495 if (m == MATCH_NO)
4496 {
4497 gfc_error ("Pointer initialization requires a NULL() at %C");
4498 m = MATCH_ERROR;
4499 }
4500 if (gfc_pure (NULL))
4501 {
4502 gfc_error ("Initialization of pointer at %C is not allowed in "
4503 "a PURE procedure");
4504 m = MATCH_ERROR;
4505 }
4506 if (m != MATCH_YES)
4507 {
4508 gfc_free_expr (initializer);
4509 return m;
4510 }
4511 c->initializer = initializer;
4512 }
4513
4514 if (gfc_match_eos () == MATCH_YES)
4515 return MATCH_YES;
4516 if (gfc_match_char (',') != MATCH_YES)
4517 goto syntax;
4518 }
4519
4520syntax:
4521 gfc_error ("Syntax error in procedure pointer component at %C");
4522 return MATCH_ERROR;
4523}
4524
4525
af1a34ee 4526/* Match a PROCEDURE declaration inside an interface (R1206). */
4527
4528static match
4529match_procedure_in_interface (void)
4530{
4531 match m;
4532 gfc_symbol *sym;
4533 char name[GFC_MAX_SYMBOL_LEN + 1];
4534
4535 if (current_interface.type == INTERFACE_NAMELESS
4536 || current_interface.type == INTERFACE_ABSTRACT)
4537 {
4538 gfc_error ("PROCEDURE at %C must be in a generic interface");
4539 return MATCH_ERROR;
4540 }
4541
4542 for(;;)
4543 {
4544 m = gfc_match_name (name);
4545 if (m == MATCH_NO)
4546 goto syntax;
4547 else if (m == MATCH_ERROR)
4548 return m;
4549 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4550 return MATCH_ERROR;
4551
4552 if (gfc_add_interface (sym) == FAILURE)
4553 return MATCH_ERROR;
4554
af1a34ee 4555 if (gfc_match_eos () == MATCH_YES)
4556 break;
4557 if (gfc_match_char (',') != MATCH_YES)
4558 goto syntax;
4559 }
4560
4561 return MATCH_YES;
4562
4563syntax:
4564 gfc_error ("Syntax error in PROCEDURE statement at %C");
4565 return MATCH_ERROR;
4566}
4567
4568
4569/* General matcher for PROCEDURE declarations. */
4570
7fd88f6e 4571static match match_procedure_in_type (void);
4572
af1a34ee 4573match
4574gfc_match_procedure (void)
4575{
4576 match m;
4577
4578 switch (gfc_current_state ())
4579 {
4580 case COMP_NONE:
4581 case COMP_PROGRAM:
4582 case COMP_MODULE:
4583 case COMP_SUBROUTINE:
4584 case COMP_FUNCTION:
4585 m = match_procedure_decl ();
4586 break;
4587 case COMP_INTERFACE:
4588 m = match_procedure_in_interface ();
4589 break;
4590 case COMP_DERIVED:
64e93293 4591 m = match_ppc_decl ();
4592 break;
7fd88f6e 4593 case COMP_DERIVED_CONTAINS:
4594 m = match_procedure_in_type ();
4595 break;
af1a34ee 4596 default:
4597 return MATCH_NO;
4598 }
4599
4600 if (m != MATCH_YES)
4601 return m;
4602
4603 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4604 == FAILURE)
4605 return MATCH_ERROR;
4606
4607 return m;
4608}
4609
4610
a34926ba 4611/* Warn if a matched procedure has the same name as an intrinsic; this is
4612 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4613 parser-state-stack to find out whether we're in a module. */
4614
4615static void
4616warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4617{
4618 bool in_module;
4619
4620 in_module = (gfc_state_stack->previous
4621 && gfc_state_stack->previous->state == COMP_MODULE);
4622
4623 gfc_warn_intrinsic_shadow (sym, in_module, func);
4624}
4625
4626
4ee9c684 4627/* Match a function declaration. */
4628
4629match
4630gfc_match_function_decl (void)
4631{
4632 char name[GFC_MAX_SYMBOL_LEN + 1];
4633 gfc_symbol *sym, *result;
4634 locus old_loc;
4635 match m;
c5d33754 4636 match suffix_match;
4637 match found_match; /* Status returned by match func. */
4ee9c684 4638
4639 if (gfc_current_state () != COMP_NONE
4640 && gfc_current_state () != COMP_INTERFACE
4641 && gfc_current_state () != COMP_CONTAINS)
4642 return MATCH_NO;
4643
4644 gfc_clear_ts (&current_ts);
4645
cbb9e6aa 4646 old_loc = gfc_current_locus;
4ee9c684 4647
077932f9 4648 m = gfc_match_prefix (&current_ts);
4ee9c684 4649 if (m != MATCH_YES)
4650 {
cbb9e6aa 4651 gfc_current_locus = old_loc;
4ee9c684 4652 return m;
4653 }
4654
4655 if (gfc_match ("function% %n", name) != MATCH_YES)
4656 {
cbb9e6aa 4657 gfc_current_locus = old_loc;
4ee9c684 4658 return MATCH_NO;
4659 }
d77f260f 4660 if (get_proc_name (name, &sym, false))
4ee9c684 4661 return MATCH_ERROR;
1e057e9b 4662
4663 if (add_hidden_procptr_result (sym) == SUCCESS)
4664 sym = sym->result;
4665
4ee9c684 4666 gfc_new_block = sym;
4667
4668 m = gfc_match_formal_arglist (sym, 0, 0);
4669 if (m == MATCH_NO)
9b435b6d 4670 {
4671 gfc_error ("Expected formal argument list in function "
1a9745d2 4672 "definition at %C");
9b435b6d 4673 m = MATCH_ERROR;
4674 goto cleanup;
4675 }
4ee9c684 4676 else if (m == MATCH_ERROR)
4677 goto cleanup;
4678
4679 result = NULL;
4680
c5d33754 4681 /* According to the draft, the bind(c) and result clause can
4682 come in either order after the formal_arg_list (i.e., either
4683 can be first, both can exist together or by themselves or neither
4684 one). Therefore, the match_result can't match the end of the
4685 string, and check for the bind(c) or result clause in either order. */
4686 found_match = gfc_match_eos ();
4687
4688 /* Make sure that it isn't already declared as BIND(C). If it is, it
4689 must have been marked BIND(C) with a BIND(C) attribute and that is
4690 not allowed for procedures. */
4691 if (sym->attr.is_bind_c == 1)
4692 {
4693 sym->attr.is_bind_c = 0;
4694 if (sym->old_symbol != NULL)
4695 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4696 "variables or common blocks",
4697 &(sym->old_symbol->declared_at));
4698 else
4699 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4700 "variables or common blocks", &gfc_current_locus);
4ee9c684 4701 }
4702
c5d33754 4703 if (found_match != MATCH_YES)
4ee9c684 4704 {
c5d33754 4705 /* If we haven't found the end-of-statement, look for a suffix. */
4706 suffix_match = gfc_match_suffix (sym, &result);
4707 if (suffix_match == MATCH_YES)
4708 /* Need to get the eos now. */
4709 found_match = gfc_match_eos ();
4710 else
4711 found_match = suffix_match;
4ee9c684 4712 }
4713
c5d33754 4714 if(found_match != MATCH_YES)
4715 m = MATCH_ERROR;
4ee9c684 4716 else
4717 {
c5d33754 4718 /* Make changes to the symbol. */
4719 m = MATCH_ERROR;
4720
4721 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4722 goto cleanup;
4723
4724 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4725 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4726 goto cleanup;
4ee9c684 4727
8d39570e 4728 /* Delay matching the function characteristics until after the
077932f9 4729 specification block by signalling kind=-1. */
8d39570e 4730 sym->declared_at = old_loc;
4731 if (current_ts.type != BT_UNKNOWN)
4732 current_ts.kind = -1;
4733 else
4734 current_ts.kind = 0;
077932f9 4735
c5d33754 4736 if (result == NULL)
4737 {
0477d42d 4738 if (current_ts.type != BT_UNKNOWN
4739 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4740 goto cleanup;
c5d33754 4741 sym->result = sym;
4742 }
4743 else
4744 {
0477d42d 4745 if (current_ts.type != BT_UNKNOWN
4746 && gfc_add_type (result, &current_ts, &gfc_current_locus)
4747 == FAILURE)
4748 goto cleanup;
c5d33754 4749 sym->result = result;
4750 }
4751
a34926ba 4752 /* Warn if this procedure has the same name as an intrinsic. */
4753 warn_intrinsic_shadow (sym, true);
4754
c5d33754 4755 return MATCH_YES;
4756 }
4ee9c684 4757
4758cleanup:
cbb9e6aa 4759 gfc_current_locus = old_loc;
4ee9c684 4760 return m;
4761}
4762
1a9745d2 4763
4764/* This is mostly a copy of parse.c(add_global_procedure) but modified to
4765 pass the name of the entry, rather than the gfc_current_block name, and
4766 to return false upon finding an existing global entry. */
858f9894 4767
4768static bool
1a9745d2 4769add_global_entry (const char *name, int sub)
858f9894 4770{
4771 gfc_gsymbol *s;
8458f4ca 4772 enum gfc_symbol_type type;
858f9894 4773
4774 s = gfc_get_gsymbol(name);
5b11d932 4775 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
858f9894 4776
4777 if (s->defined
1a9745d2 4778 || (s->type != GSYM_UNKNOWN
5b11d932 4779 && s->type != type))
cbbac028 4780 gfc_global_used(s, NULL);
858f9894 4781 else
4782 {
5b11d932 4783 s->type = type;
858f9894 4784 s->where = gfc_current_locus;
4785 s->defined = 1;
83aeedb9 4786 s->ns = gfc_current_ns;
858f9894 4787 return true;
4788 }
4789 return false;
4790}
4ee9c684 4791
1a9745d2 4792
4ee9c684 4793/* Match an ENTRY statement. */
4794
4795match
4796gfc_match_entry (void)
4797{
1b716045 4798 gfc_symbol *proc;
4799 gfc_symbol *result;
4800 gfc_symbol *entry;
4ee9c684 4801 char name[GFC_MAX_SYMBOL_LEN + 1];
4802 gfc_compile_state state;
4803 match m;
1b716045 4804 gfc_entry_list *el;
7b5e1acc 4805 locus old_loc;
d77f260f 4806 bool module_procedure;
86f0974b 4807 char peek_char;
4808 match is_bind_c;
4ee9c684 4809
4810 m = gfc_match_name (name);
4811 if (m != MATCH_YES)
4812 return m;
4813
1b716045 4814 state = gfc_current_state ();
ea37f786 4815 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
1b716045 4816 {
ea37f786 4817 switch (state)
4818 {
4819 case COMP_PROGRAM:
4820 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4821 break;
4822 case COMP_MODULE:
4823 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4824 break;
4825 case COMP_BLOCK_DATA:
1a9745d2 4826 gfc_error ("ENTRY statement at %C cannot appear within "
4827 "a BLOCK DATA");
ea37f786 4828 break;
4829 case COMP_INTERFACE:
1a9745d2 4830 gfc_error ("ENTRY statement at %C cannot appear within "
4831 "an INTERFACE");
ea37f786 4832 break;
4833 case COMP_DERIVED:
1a9745d2 4834 gfc_error ("ENTRY statement at %C cannot appear within "
4835 "a DERIVED TYPE block");
ea37f786 4836 break;
4837 case COMP_IF:
1a9745d2 4838 gfc_error ("ENTRY statement at %C cannot appear within "
4839 "an IF-THEN block");
ea37f786 4840 break;
4841 case COMP_DO:
1a9745d2 4842 gfc_error ("ENTRY statement at %C cannot appear within "
4843 "a DO block");
ea37f786 4844 break;
4845 case COMP_SELECT:
1a9745d2 4846 gfc_error ("ENTRY statement at %C cannot appear within "
4847 "a SELECT block");
ea37f786 4848 break;
4849 case COMP_FORALL:
1a9745d2 4850 gfc_error ("ENTRY statement at %C cannot appear within "
4851 "a FORALL block");
ea37f786 4852 break;
4853 case COMP_WHERE:
1a9745d2 4854 gfc_error ("ENTRY statement at %C cannot appear within "
4855 "a WHERE block");
ea37f786 4856 break;
4857 case COMP_CONTAINS:
1a9745d2 4858 gfc_error ("ENTRY statement at %C cannot appear within "
4859 "a contained subprogram");
ea37f786 4860 break;
4861 default:
4862 gfc_internal_error ("gfc_match_entry(): Bad state");
4863 }
1b716045 4864 return MATCH_ERROR;
4865 }
4866
d77f260f 4867 module_procedure = gfc_current_ns->parent != NULL
1a9745d2 4868 && gfc_current_ns->parent->proc_name
4869 && gfc_current_ns->parent->proc_name->attr.flavor
4870 == FL_MODULE;
d77f260f 4871
1b716045 4872 if (gfc_current_ns->parent != NULL
4873 && gfc_current_ns->parent->proc_name
d77f260f 4874 && !module_procedure)
1b716045 4875 {
4876 gfc_error("ENTRY statement at %C cannot appear in a "
4877 "contained procedure");
4878 return MATCH_ERROR;
4879 }
4880
d77f260f 4881 /* Module function entries need special care in get_proc_name
4882 because previous references within the function will have
4883 created symbols attached to the current namespace. */
4884 if (get_proc_name (name, &entry,
4885 gfc_current_ns->parent != NULL
c1e4695a 4886 && module_procedure))
4ee9c684 4887 return MATCH_ERROR;
4888
1b716045 4889 proc = gfc_current_block ();
4890
86f0974b 4891 /* Make sure that it isn't already declared as BIND(C). If it is, it
4892 must have been marked BIND(C) with a BIND(C) attribute and that is
4893 not allowed for procedures. */
4894 if (entry->attr.is_bind_c == 1)
4895 {
4896 entry->attr.is_bind_c = 0;
4897 if (entry->old_symbol != NULL)
4898 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4899 "variables or common blocks",
4900 &(entry->old_symbol->declared_at));
4901 else
4902 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4903 "variables or common blocks", &gfc_current_locus);
4904 }
4905
4906 /* Check what next non-whitespace character is so we can tell if there
4907 is the required parens if we have a BIND(C). */
4908 gfc_gobble_whitespace ();
e0be6f02 4909 peek_char = gfc_peek_ascii_char ();
86f0974b 4910
1b716045 4911 if (state == COMP_SUBROUTINE)
4ee9c684 4912 {
950683ed 4913 /* An entry in a subroutine. */
8cafc742 4914 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
858f9894 4915 return MATCH_ERROR;
4916
4ee9c684 4917 m = gfc_match_formal_arglist (entry, 0, 1);
4918 if (m != MATCH_YES)
4919 return MATCH_ERROR;
4920
75ae7f6c 4921 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4922 never be an internal procedure. */
4923 is_bind_c = gfc_match_bind_c (entry, true);
86f0974b 4924 if (is_bind_c == MATCH_ERROR)
4925 return MATCH_ERROR;
4926 if (is_bind_c == MATCH_YES)
4927 {
4928 if (peek_char != '(')
4929 {
4930 gfc_error ("Missing required parentheses before BIND(C) at %C");
4931 return MATCH_ERROR;
4932 }
4933 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4934 == FAILURE)
4935 return MATCH_ERROR;
4936 }
4937
950683ed 4938 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4939 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 4940 return MATCH_ERROR;
1b716045 4941 }
4942 else
4943 {
7b5e1acc 4944 /* An entry in a function.
1a9745d2 4945 We need to take special care because writing
4946 ENTRY f()
4947 as
4948 ENTRY f
4949 is allowed, whereas
4950 ENTRY f() RESULT (r)
4951 can't be written as
4952 ENTRY f RESULT (r). */
8cafc742 4953 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
858f9894 4954 return MATCH_ERROR;
4955
7b5e1acc 4956 old_loc = gfc_current_locus;
4957 if (gfc_match_eos () == MATCH_YES)
4958 {
4959 gfc_current_locus = old_loc;
4960 /* Match the empty argument list, and add the interface to
4961 the symbol. */
4962 m = gfc_match_formal_arglist (entry, 0, 1);
4963 }
4964 else
4965 m = gfc_match_formal_arglist (entry, 0, 0);
4966
4ee9c684 4967 if (m != MATCH_YES)
4968 return MATCH_ERROR;
4969
4ee9c684 4970 result = NULL;
4971
4972 if (gfc_match_eos () == MATCH_YES)
4973 {
950683ed 4974 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4975 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4ee9c684 4976 return MATCH_ERROR;
4977
c6871095 4978 entry->result = entry;
4ee9c684 4979 }
4980 else
4981 {
86f0974b 4982 m = gfc_match_suffix (entry, &result);
4ee9c684 4983 if (m == MATCH_NO)
4984 gfc_syntax_error (ST_ENTRY);
4985 if (m != MATCH_YES)
4986 return MATCH_ERROR;
4987
86f0974b 4988 if (result)
4989 {
4990 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4991 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4992 || gfc_add_function (&entry->attr, result->name, NULL)
4993 == FAILURE)
4994 return MATCH_ERROR;
4995 entry->result = result;
4996 }
4997 else
4998 {
4999 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5000 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5001 return MATCH_ERROR;
5002 entry->result = entry;
5003 }
4ee9c684 5004 }
4ee9c684 5005 }
5006
5007 if (gfc_match_eos () != MATCH_YES)
5008 {
5009 gfc_syntax_error (ST_ENTRY);
5010 return MATCH_ERROR;
5011 }
5012
1b716045 5013 entry->attr.recursive = proc->attr.recursive;
5014 entry->attr.elemental = proc->attr.elemental;
5015 entry->attr.pure = proc->attr.pure;
4ee9c684 5016
1b716045 5017 el = gfc_get_entry_list ();
5018 el->sym = entry;
5019 el->next = gfc_current_ns->entries;
5020 gfc_current_ns->entries = el;
5021 if (el->next)
5022 el->id = el->next->id + 1;
5023 else
5024 el->id = 1;
4ee9c684 5025
1b716045 5026 new_st.op = EXEC_ENTRY;
5027 new_st.ext.entry = el;
5028
5029 return MATCH_YES;
4ee9c684 5030}
5031
5032
5033/* Match a subroutine statement, including optional prefixes. */
5034
5035match
5036gfc_match_subroutine (void)
5037{
5038 char name[GFC_MAX_SYMBOL_LEN + 1];
5039 gfc_symbol *sym;
5040 match m;
c5d33754 5041 match is_bind_c;
5042 char peek_char;
75ae7f6c 5043 bool allow_binding_name;
4ee9c684 5044
5045 if (gfc_current_state () != COMP_NONE
5046 && gfc_current_state () != COMP_INTERFACE
5047 && gfc_current_state () != COMP_CONTAINS)
5048 return MATCH_NO;
5049
077932f9 5050 m = gfc_match_prefix (NULL);
4ee9c684 5051 if (m != MATCH_YES)
5052 return m;
5053
5054 m = gfc_match ("subroutine% %n", name);
5055 if (m != MATCH_YES)
5056 return m;
5057
d77f260f 5058 if (get_proc_name (name, &sym, false))
4ee9c684 5059 return MATCH_ERROR;
1e057e9b 5060
5061 if (add_hidden_procptr_result (sym) == SUCCESS)
5062 sym = sym->result;
5063
4ee9c684 5064 gfc_new_block = sym;
5065
c5d33754 5066 /* Check what next non-whitespace character is so we can tell if there
86f0974b 5067 is the required parens if we have a BIND(C). */
c5d33754 5068 gfc_gobble_whitespace ();
e0be6f02 5069 peek_char = gfc_peek_ascii_char ();
c5d33754 5070
950683ed 5071 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4ee9c684 5072 return MATCH_ERROR;
5073
5074 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5075 return MATCH_ERROR;
5076
c5d33754 5077 /* Make sure that it isn't already declared as BIND(C). If it is, it
5078 must have been marked BIND(C) with a BIND(C) attribute and that is
5079 not allowed for procedures. */
5080 if (sym->attr.is_bind_c == 1)
5081 {
5082 sym->attr.is_bind_c = 0;
5083 if (sym->old_symbol != NULL)
5084 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5085 "variables or common blocks",
5086 &(sym->old_symbol->declared_at));
5087 else
5088 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5089 "variables or common blocks", &gfc_current_locus);
5090 }
75ae7f6c 5091
5092 /* C binding names are not allowed for internal procedures. */
5093 if (gfc_current_state () == COMP_CONTAINS
5094 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5095 allow_binding_name = false;
5096 else
5097 allow_binding_name = true;
5098
c5d33754 5099 /* Here, we are just checking if it has the bind(c) attribute, and if
5100 so, then we need to make sure it's all correct. If it doesn't,
5101 we still need to continue matching the rest of the subroutine line. */
75ae7f6c 5102 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 5103 if (is_bind_c == MATCH_ERROR)
5104 {
5105 /* There was an attempt at the bind(c), but it was wrong. An
5106 error message should have been printed w/in the gfc_match_bind_c
5107 so here we'll just return the MATCH_ERROR. */
5108 return MATCH_ERROR;
5109 }
5110
5111 if (is_bind_c == MATCH_YES)
5112 {
75ae7f6c 5113 /* The following is allowed in the Fortran 2008 draft. */
4518e961 5114 if (gfc_current_state () == COMP_CONTAINS
75ae7f6c 5115 && sym->ns->proc_name->attr.flavor != FL_MODULE
2dcae6c7 5116 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5117 "at %L may not be specified for an internal "
5118 "procedure", &gfc_current_locus)
75ae7f6c 5119 == FAILURE)
5120 return MATCH_ERROR;
5121
c5d33754 5122 if (peek_char != '(')
5123 {
5124 gfc_error ("Missing required parentheses before BIND(C) at %C");
5125 return MATCH_ERROR;
5126 }
5127 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5128 == FAILURE)
5129 return MATCH_ERROR;
5130 }
5131
4ee9c684 5132 if (gfc_match_eos () != MATCH_YES)
5133 {
5134 gfc_syntax_error (ST_SUBROUTINE);
5135 return MATCH_ERROR;
5136 }
5137
5138 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5139 return MATCH_ERROR;
5140
a34926ba 5141 /* Warn if it has the same name as an intrinsic. */
5142 warn_intrinsic_shadow (sym, false);
5143
4ee9c684 5144 return MATCH_YES;
5145}
5146
5147
c5d33754 5148/* Match a BIND(C) specifier, with the optional 'name=' specifier if
5149 given, and set the binding label in either the given symbol (if not
a0527218 5150 NULL), or in the current_ts. The symbol may be NULL because we may
c5d33754 5151 encounter the BIND(C) before the declaration itself. Return
5152 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5153 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5154 or MATCH_YES if the specifier was correct and the binding label and
5155 bind(c) fields were set correctly for the given symbol or the
75ae7f6c 5156 current_ts. If allow_binding_name is false, no binding name may be
5157 given. */
c5d33754 5158
5159match
75ae7f6c 5160gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
c5d33754 5161{
5162 /* binding label, if exists */
5163 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5164 match double_quote;
5165 match single_quote;
c5d33754 5166
5167 /* Initialize the flag that specifies whether we encountered a NAME=
5168 specifier or not. */
5169 has_name_equals = 0;
5170
5171 /* Init the first char to nil so we can catch if we don't have
5172 the label (name attr) or the symbol name yet. */
5173 binding_label[0] = '\0';
5174
5175 /* This much we have to be able to match, in this order, if
5176 there is a bind(c) label. */
5177 if (gfc_match (" bind ( c ") != MATCH_YES)
5178 return MATCH_NO;
5179
5180 /* Now see if there is a binding label, or if we've reached the
5181 end of the bind(c) attribute without one. */
5182 if (gfc_match_char (',') == MATCH_YES)
5183 {
5184 if (gfc_match (" name = ") != MATCH_YES)
5185 {
5186 gfc_error ("Syntax error in NAME= specifier for binding label "
5187 "at %C");
5188 /* should give an error message here */
5189 return MATCH_ERROR;
5190 }
5191
5192 has_name_equals = 1;
5193
5194 /* Get the opening quote. */
5195 double_quote = MATCH_YES;
5196 single_quote = MATCH_YES;
5197 double_quote = gfc_match_char ('"');
5198 if (double_quote != MATCH_YES)
5199 single_quote = gfc_match_char ('\'');
5200 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5201 {
5202 gfc_error ("Syntax error in NAME= specifier for binding label "
5203 "at %C");
5204 return MATCH_ERROR;
5205 }
5206
5207 /* Grab the binding label, using functions that will not lower
5208 case the names automatically. */
5209 if (gfc_match_name_C (binding_label) != MATCH_YES)
5210 return MATCH_ERROR;
5211
5212 /* Get the closing quotation. */
5213 if (double_quote == MATCH_YES)
5214 {
5215 if (gfc_match_char ('"') != MATCH_YES)
5216 {
5217 gfc_error ("Missing closing quote '\"' for binding label at %C");
5218 /* User started string with '"' so looked to match it. */
5219 return MATCH_ERROR;
5220 }
5221 }
5222 else
5223 {
5224 if (gfc_match_char ('\'') != MATCH_YES)
5225 {
5226 gfc_error ("Missing closing quote '\'' for binding label at %C");
5227 /* User started string with "'" char. */
5228 return MATCH_ERROR;
5229 }
5230 }
5231 }
5232
5233 /* Get the required right paren. */
5234 if (gfc_match_char (')') != MATCH_YES)
5235 {
5236 gfc_error ("Missing closing paren for binding label at %C");
5237 return MATCH_ERROR;
5238 }
5239
75ae7f6c 5240 if (has_name_equals && !allow_binding_name)
5241 {
5242 gfc_error ("No binding name is allowed in BIND(C) at %C");
5243 return MATCH_ERROR;
5244 }
5245
5246 if (has_name_equals && sym != NULL && sym->attr.dummy)
5247 {
5248 gfc_error ("For dummy procedure %s, no binding name is "
5249 "allowed in BIND(C) at %C", sym->name);
5250 return MATCH_ERROR;
5251 }
5252
5253
c5d33754 5254 /* Save the binding label to the symbol. If sym is null, we're
5255 probably matching the typespec attributes of a declaration and
5256 haven't gotten the name yet, and therefore, no symbol yet. */
5257 if (binding_label[0] != '\0')
5258 {
5259 if (sym != NULL)
5260 {
b1223b39 5261 strcpy (sym->binding_label, binding_label);
c5d33754 5262 }
5263 else
b1223b39 5264 strcpy (curr_binding_label, binding_label);
c5d33754 5265 }
75ae7f6c 5266 else if (allow_binding_name)
c5d33754 5267 {
5268 /* No binding label, but if symbol isn't null, we
75ae7f6c 5269 can set the label for it here.
5270 If name="" or allow_binding_name is false, no C binding name is
5271 created. */
c5d33754 5272 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5273 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5274 }
94fa7146 5275
5cf92482 5276 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5277 && current_interface.type == INTERFACE_ABSTRACT)
94fa7146 5278 {
5279 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5280 return MATCH_ERROR;
5281 }
5282
c5d33754 5283 return MATCH_YES;
5284}
5285
5286
231e961a 5287/* Return nonzero if we're currently compiling a contained procedure. */
c0985832 5288
5289static int
5290contained_procedure (void)
5291{
1c343287 5292 gfc_state_data *s = gfc_state_stack;
c0985832 5293
1c343287 5294 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5295 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5296 return 1;
c0985832 5297
5298 return 0;
5299}
5300
e14bee04 5301/* Set the kind of each enumerator. The kind is selected such that it is
3b6a4b41 5302 interoperable with the corresponding C enumeration type, making
5303 sure that -fshort-enums is honored. */
5304
5305static void
5306set_enum_kind(void)
5307{
5308 enumerator_history *current_history = NULL;
5309 int kind;
5310 int i;
5311
5312 if (max_enum == NULL || enum_history == NULL)
5313 return;
5314
6f092e31 5315 if (!flag_short_enums)
e14bee04 5316 return;
5317
3b6a4b41 5318 i = 0;
5319 do
5320 {
5321 kind = gfc_integer_kinds[i++].kind;
5322 }
e14bee04 5323 while (kind < gfc_c_int_kind
3b6a4b41 5324 && gfc_check_integer_range (max_enum->initializer->value.integer,
5325 kind) != ARITH_OK);
5326
5327 current_history = enum_history;
5328 while (current_history != NULL)
5329 {
5330 current_history->sym->ts.kind = kind;
5331 current_history = current_history->next;
5332 }
5333}
5334
1a9745d2 5335
4ee9c684 5336/* Match any of the various end-block statements. Returns the type of
5337 END to the caller. The END INTERFACE, END IF, END DO and END
5338 SELECT statements cannot be replaced by a single END statement. */
5339
5340match
1a9745d2 5341gfc_match_end (gfc_statement *st)
4ee9c684 5342{
5343 char name[GFC_MAX_SYMBOL_LEN + 1];
5344 gfc_compile_state state;
5345 locus old_loc;
5346 const char *block_name;
5347 const char *target;
c0985832 5348 int eos_ok;
4ee9c684 5349 match m;
5350
cbb9e6aa 5351 old_loc = gfc_current_locus;
4ee9c684 5352 if (gfc_match ("end") != MATCH_YES)
5353 return MATCH_NO;
5354
5355 state = gfc_current_state ();
1a9745d2 5356 block_name = gfc_current_block () == NULL
5357 ? NULL : gfc_current_block ()->name;
4ee9c684 5358
7fd88f6e 5359 if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
4ee9c684 5360 {
5361 state = gfc_state_stack->previous->state;
1a9745d2 5362 block_name = gfc_state_stack->previous->sym == NULL
5363 ? NULL : gfc_state_stack->previous->sym->name;
4ee9c684 5364 }
5365
5366 switch (state)
5367 {
5368 case COMP_NONE:
5369 case COMP_PROGRAM:
5370 *st = ST_END_PROGRAM;
5371 target = " program";
c0985832 5372 eos_ok = 1;
4ee9c684 5373 break;
5374
5375 case COMP_SUBROUTINE:
5376 *st = ST_END_SUBROUTINE;
5377 target = " subroutine";
c0985832 5378 eos_ok = !contained_procedure ();
4ee9c684 5379 break;
5380
5381 case COMP_FUNCTION:
5382 *st = ST_END_FUNCTION;
5383 target = " function";
c0985832 5384 eos_ok = !contained_procedure ();
4ee9c684 5385 break;
5386
5387 case COMP_BLOCK_DATA:
5388 *st = ST_END_BLOCK_DATA;
5389 target = " block data";
c0985832 5390 eos_ok = 1;
4ee9c684 5391 break;
5392
5393 case COMP_MODULE:
5394 *st = ST_END_MODULE;
5395 target = " module";
c0985832 5396 eos_ok = 1;
4ee9c684 5397 break;
5398
5399 case COMP_INTERFACE:
5400 *st = ST_END_INTERFACE;
5401 target = " interface";
c0985832 5402 eos_ok = 0;
4ee9c684 5403 break;
5404
5405 case COMP_DERIVED:
7fd88f6e 5406 case COMP_DERIVED_CONTAINS:
4ee9c684 5407 *st = ST_END_TYPE;
5408 target = " type";
c0985832 5409 eos_ok = 0;
4ee9c684 5410 break;
5411
5412 case COMP_IF:
5413 *st = ST_ENDIF;
5414 target = " if";
c0985832 5415 eos_ok = 0;
4ee9c684 5416 break;
5417
5418 case COMP_DO:
5419 *st = ST_ENDDO;
5420 target = " do";
c0985832 5421 eos_ok = 0;
4ee9c684 5422 break;
5423
5424 case COMP_SELECT:
5425 *st = ST_END_SELECT;
5426 target = " select";
c0985832 5427 eos_ok = 0;
4ee9c684 5428 break;
5429
5430 case COMP_FORALL:
5431 *st = ST_END_FORALL;
5432 target = " forall";
c0985832 5433 eos_ok = 0;
4ee9c684 5434 break;
5435
5436 case COMP_WHERE:
5437 *st = ST_END_WHERE;
5438 target = " where";
c0985832 5439 eos_ok = 0;
4ee9c684 5440 break;
5441
3b6a4b41 5442 case COMP_ENUM:
5443 *st = ST_END_ENUM;
5444 target = " enum";
5445 eos_ok = 0;
5446 last_initializer = NULL;
5447 set_enum_kind ();
5448 gfc_free_enum_history ();
5449 break;
5450
4ee9c684 5451 default:
5452 gfc_error ("Unexpected END statement at %C");
5453 goto cleanup;
5454 }
5455
5456 if (gfc_match_eos () == MATCH_YES)
5457 {
c0985832 5458 if (!eos_ok)
4ee9c684 5459 {
f6d0e37a 5460 /* We would have required END [something]. */
d197c9ee 5461 gfc_error ("%s statement expected at %L",
5462 gfc_ascii_statement (*st), &old_loc);
4ee9c684 5463 goto cleanup;
5464 }
5465
5466 return MATCH_YES;
5467 }
5468
5469 /* Verify that we've got the sort of end-block that we're expecting. */
5470 if (gfc_match (target) != MATCH_YES)
5471 {
5472 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5473 goto cleanup;
5474 }
5475
5476 /* If we're at the end, make sure a block name wasn't required. */
5477 if (gfc_match_eos () == MATCH_YES)
5478 {
5479
0d0ce415 5480 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5481 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4ee9c684 5482 return MATCH_YES;
5483
5484 if (gfc_current_block () == NULL)
5485 return MATCH_YES;
5486
5487 gfc_error ("Expected block name of '%s' in %s statement at %C",
5488 block_name, gfc_ascii_statement (*st));
5489
5490 return MATCH_ERROR;
5491 }
5492
5493 /* END INTERFACE has a special handler for its several possible endings. */
5494 if (*st == ST_END_INTERFACE)
5495 return gfc_match_end_interface ();
5496
f6d0e37a 5497 /* We haven't hit the end of statement, so what is left must be an
5498 end-name. */
4ee9c684 5499 m = gfc_match_space ();
5500 if (m == MATCH_YES)
5501 m = gfc_match_name (name);
5502
5503 if (m == MATCH_NO)
5504 gfc_error ("Expected terminating name at %C");
5505 if (m != MATCH_YES)
5506 goto cleanup;
5507
5508 if (block_name == NULL)
5509 goto syntax;
5510
1e057e9b 5511 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
4ee9c684 5512 {
5513 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5514 gfc_ascii_statement (*st));
5515 goto cleanup;
5516 }
1e057e9b 5517 /* Procedure pointer as function result. */
5518 else if (strcmp (block_name, "ppr@") == 0
5519 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5520 {
5521 gfc_error ("Expected label '%s' for %s statement at %C",
5522 gfc_current_block ()->ns->proc_name->name,
5523 gfc_ascii_statement (*st));
5524 goto cleanup;
5525 }
4ee9c684 5526
5527 if (gfc_match_eos () == MATCH_YES)
5528 return MATCH_YES;
5529
5530syntax:
5531 gfc_syntax_error (*st);
5532
5533cleanup:
cbb9e6aa 5534 gfc_current_locus = old_loc;
4ee9c684 5535 return MATCH_ERROR;
5536}
5537
5538
5539
5540/***************** Attribute declaration statements ****************/
5541
5542/* Set the attribute of a single variable. */
5543
5544static match
5545attr_decl1 (void)
5546{
5547 char name[GFC_MAX_SYMBOL_LEN + 1];
5548 gfc_array_spec *as;
5549 gfc_symbol *sym;
5550 locus var_locus;
5551 match m;
5552
5553 as = NULL;
5554
5555 m = gfc_match_name (name);
5556 if (m != MATCH_YES)
5557 goto cleanup;
5558
36b0a1b0 5559 if (find_special (name, &sym, false))
4ee9c684 5560 return MATCH_ERROR;
5561
cbb9e6aa 5562 var_locus = gfc_current_locus;
4ee9c684 5563
5564 /* Deal with possible array specification for certain attributes. */
5565 if (current_attr.dimension
5566 || current_attr.allocatable
5567 || current_attr.pointer
5568 || current_attr.target)
5569 {
5570 m = gfc_match_array_spec (&as);
5571 if (m == MATCH_ERROR)
5572 goto cleanup;
5573
5574 if (current_attr.dimension && m == MATCH_NO)
5575 {
1a9745d2 5576 gfc_error ("Missing array specification at %L in DIMENSION "
5577 "statement", &var_locus);
4ee9c684 5578 m = MATCH_ERROR;
5579 goto cleanup;
5580 }
5581
13aebeb0 5582 if (current_attr.dimension && sym->value)
5583 {
5584 gfc_error ("Dimensions specified for %s at %L after its "
5585 "initialisation", sym->name, &var_locus);
5586 m = MATCH_ERROR;
5587 goto cleanup;
5588 }
5589
4ee9c684 5590 if ((current_attr.allocatable || current_attr.pointer)
5591 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5592 {
1a9745d2 5593 gfc_error ("Array specification must be deferred at %L", &var_locus);
4ee9c684 5594 m = MATCH_ERROR;
5595 goto cleanup;
5596 }
5597 }
5598
1a9745d2 5599 /* Update symbol table. DIMENSION attribute is set
5600 in gfc_set_array_spec(). */
4ee9c684 5601 if (current_attr.dimension == 0
63986dfb 5602 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
4ee9c684 5603 {
5604 m = MATCH_ERROR;
5605 goto cleanup;
5606 }
5607
5608 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5609 {
5610 m = MATCH_ERROR;
5611 goto cleanup;
5612 }
e14bee04 5613
b549d2a5 5614 if (sym->attr.cray_pointee && sym->as != NULL)
5615 {
5616 /* Fix the array spec. */
5617 m = gfc_mod_pointee_as (sym->as);
5618 if (m == MATCH_ERROR)
5619 goto cleanup;
5620 }
4ee9c684 5621
25dd7350 5622 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
14efb9b7 5623 {
5624 m = MATCH_ERROR;
5625 goto cleanup;
5626 }
5627
4ee9c684 5628 if ((current_attr.external || current_attr.intrinsic)
5629 && sym->attr.flavor != FL_PROCEDURE
950683ed 5630 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4ee9c684 5631 {
5632 m = MATCH_ERROR;
5633 goto cleanup;
5634 }
5635
1e057e9b 5636 add_hidden_procptr_result (sym);
5637
4ee9c684 5638 return MATCH_YES;
5639
5640cleanup:
5641 gfc_free_array_spec (as);
5642 return m;
5643}
5644
5645
5646/* Generic attribute declaration subroutine. Used for attributes that
5647 just have a list of names. */
5648
5649static match
5650attr_decl (void)
5651{
5652 match m;
5653
5654 /* Gobble the optional double colon, by simply ignoring the result
5655 of gfc_match(). */
5656 gfc_match (" ::");
5657
5658 for (;;)
5659 {
5660 m = attr_decl1 ();
5661 if (m != MATCH_YES)
5662 break;
5663
5664 if (gfc_match_eos () == MATCH_YES)
5665 {
5666 m = MATCH_YES;
5667 break;
5668 }
5669
5670 if (gfc_match_char (',') != MATCH_YES)
5671 {
5672 gfc_error ("Unexpected character in variable list at %C");
5673 m = MATCH_ERROR;
5674 break;
5675 }
5676 }
5677
5678 return m;
5679}
5680
5681
b549d2a5 5682/* This routine matches Cray Pointer declarations of the form:
5683 pointer ( <pointer>, <pointee> )
5684 or
e14bee04 5685 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5686 The pointer, if already declared, should be an integer. Otherwise, we
b549d2a5 5687 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5688 be either a scalar, or an array declaration. No space is allocated for
e14bee04 5689 the pointee. For the statement
b549d2a5 5690 pointer (ipt, ar(10))
5691 any subsequent uses of ar will be translated (in C-notation) as
e14bee04 5692 ar(i) => ((<type> *) ipt)(i)
b7bf3f81 5693 After gimplification, pointee variable will disappear in the code. */
b549d2a5 5694
5695static match
5696cray_pointer_decl (void)
5697{
5698 match m;
5699 gfc_array_spec *as;
5700 gfc_symbol *cptr; /* Pointer symbol. */
5701 gfc_symbol *cpte; /* Pointee symbol. */
5702 locus var_locus;
5703 bool done = false;
5704
5705 while (!done)
5706 {
5707 if (gfc_match_char ('(') != MATCH_YES)
5708 {
5709 gfc_error ("Expected '(' at %C");
e14bee04 5710 return MATCH_ERROR;
b549d2a5 5711 }
e14bee04 5712
b549d2a5 5713 /* Match pointer. */
5714 var_locus = gfc_current_locus;
5715 gfc_clear_attr (&current_attr);
5716 gfc_add_cray_pointer (&current_attr, &var_locus);
5717 current_ts.type = BT_INTEGER;
5718 current_ts.kind = gfc_index_integer_kind;
5719
e14bee04 5720 m = gfc_match_symbol (&cptr, 0);
b549d2a5 5721 if (m != MATCH_YES)
5722 {
5723 gfc_error ("Expected variable name at %C");
5724 return m;
5725 }
e14bee04 5726
b549d2a5 5727 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5728 return MATCH_ERROR;
5729
e14bee04 5730 gfc_set_sym_referenced (cptr);
b549d2a5 5731
5732 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5733 {
5734 cptr->ts.type = BT_INTEGER;
e14bee04 5735 cptr->ts.kind = gfc_index_integer_kind;
b549d2a5 5736 }
5737 else if (cptr->ts.type != BT_INTEGER)
5738 {
7698a624 5739 gfc_error ("Cray pointer at %C must be an integer");
b549d2a5 5740 return MATCH_ERROR;
5741 }
5742 else if (cptr->ts.kind < gfc_index_integer_kind)
5743 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
7698a624 5744 " memory addresses require %d bytes",
1a9745d2 5745 cptr->ts.kind, gfc_index_integer_kind);
b549d2a5 5746
5747 if (gfc_match_char (',') != MATCH_YES)
5748 {
5749 gfc_error ("Expected \",\" at %C");
e14bee04 5750 return MATCH_ERROR;
b549d2a5 5751 }
5752
e14bee04 5753 /* Match Pointee. */
b549d2a5 5754 var_locus = gfc_current_locus;
5755 gfc_clear_attr (&current_attr);
5756 gfc_add_cray_pointee (&current_attr, &var_locus);
5757 current_ts.type = BT_UNKNOWN;
5758 current_ts.kind = 0;
5759
5760 m = gfc_match_symbol (&cpte, 0);
5761 if (m != MATCH_YES)
5762 {
5763 gfc_error ("Expected variable name at %C");
5764 return m;
5765 }
e14bee04 5766
b549d2a5 5767 /* Check for an optional array spec. */
5768 m = gfc_match_array_spec (&as);
5769 if (m == MATCH_ERROR)
5770 {
5771 gfc_free_array_spec (as);
5772 return m;
5773 }
5774 else if (m == MATCH_NO)
5775 {
5776 gfc_free_array_spec (as);
5777 as = NULL;
5778 }
5779
5780 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5781 return MATCH_ERROR;
5782
5783 gfc_set_sym_referenced (cpte);
5784
5785 if (cpte->as == NULL)
5786 {
5787 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5788 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5789 }
5790 else if (as != NULL)
5791 {
7698a624 5792 gfc_error ("Duplicate array spec for Cray pointee at %C");
b549d2a5 5793 gfc_free_array_spec (as);
5794 return MATCH_ERROR;
5795 }
5796
5797 as = NULL;
5798
5799 if (cpte->as != NULL)
5800 {
5801 /* Fix array spec. */
5802 m = gfc_mod_pointee_as (cpte->as);
5803 if (m == MATCH_ERROR)
5804 return m;
5805 }
5806
5807 /* Point the Pointee at the Pointer. */
b7bf3f81 5808 cpte->cp_pointer = cptr;
b549d2a5 5809
5810 if (gfc_match_char (')') != MATCH_YES)
5811 {
5812 gfc_error ("Expected \")\" at %C");
5813 return MATCH_ERROR;
5814 }
5815 m = gfc_match_char (',');
5816 if (m != MATCH_YES)
5817 done = true; /* Stop searching for more declarations. */
5818
5819 }
5820
5821 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5822 || gfc_match_eos () != MATCH_YES)
5823 {
5824 gfc_error ("Expected \",\" or end of statement at %C");
5825 return MATCH_ERROR;
5826 }
5827 return MATCH_YES;
5828}
5829
5830
4ee9c684 5831match
5832gfc_match_external (void)
5833{
5834
5835 gfc_clear_attr (&current_attr);
14efb9b7 5836 current_attr.external = 1;
4ee9c684 5837
5838 return attr_decl ();
5839}
5840
5841
4ee9c684 5842match
5843gfc_match_intent (void)
5844{
5845 sym_intent intent;
5846
5847 intent = match_intent_spec ();
5848 if (intent == INTENT_UNKNOWN)
5849 return MATCH_ERROR;
5850
5851 gfc_clear_attr (&current_attr);
14efb9b7 5852 current_attr.intent = intent;
4ee9c684 5853
5854 return attr_decl ();
5855}
5856
5857
5858match
5859gfc_match_intrinsic (void)
5860{
5861
5862 gfc_clear_attr (&current_attr);
14efb9b7 5863 current_attr.intrinsic = 1;
4ee9c684 5864
5865 return attr_decl ();
5866}
5867
5868
5869match
5870gfc_match_optional (void)
5871{
5872
5873 gfc_clear_attr (&current_attr);
14efb9b7 5874 current_attr.optional = 1;
4ee9c684 5875
5876 return attr_decl ();
5877}
5878
5879
5880match
5881gfc_match_pointer (void)
5882{
b549d2a5 5883 gfc_gobble_whitespace ();
e0be6f02 5884 if (gfc_peek_ascii_char () == '(')
b549d2a5 5885 {
5886 if (!gfc_option.flag_cray_pointer)
5887 {
1a9745d2 5888 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5889 "flag");
b549d2a5 5890 return MATCH_ERROR;
5891 }
5892 return cray_pointer_decl ();
5893 }
5894 else
5895 {
5896 gfc_clear_attr (&current_attr);
14efb9b7 5897 current_attr.pointer = 1;
b549d2a5 5898
5899 return attr_decl ();
5900 }
4ee9c684 5901}
5902
5903
5904match
5905gfc_match_allocatable (void)
5906{
4ee9c684 5907 gfc_clear_attr (&current_attr);
14efb9b7 5908 current_attr.allocatable = 1;
4ee9c684 5909
5910 return attr_decl ();
5911}
5912
5913
5914match
5915gfc_match_dimension (void)
5916{
4ee9c684 5917 gfc_clear_attr (&current_attr);
14efb9b7 5918 current_attr.dimension = 1;
4ee9c684 5919
5920 return attr_decl ();
5921}
5922
5923
5924match
5925gfc_match_target (void)
5926{
4ee9c684 5927 gfc_clear_attr (&current_attr);
14efb9b7 5928 current_attr.target = 1;
4ee9c684 5929
5930 return attr_decl ();
5931}
5932
5933
5934/* Match the list of entities being specified in a PUBLIC or PRIVATE
5935 statement. */
5936
5937static match
5938access_attr_decl (gfc_statement st)
5939{
5940 char name[GFC_MAX_SYMBOL_LEN + 1];
5941 interface_type type;
5942 gfc_user_op *uop;
5943 gfc_symbol *sym;
dcb1b019 5944 gfc_intrinsic_op op;
4ee9c684 5945 match m;
5946
5947 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5948 goto done;
5949
5950 for (;;)
5951 {
dcb1b019 5952 m = gfc_match_generic_spec (&type, name, &op);
4ee9c684 5953 if (m == MATCH_NO)
5954 goto syntax;
5955 if (m == MATCH_ERROR)
5956 return MATCH_ERROR;
5957
5958 switch (type)
5959 {
5960 case INTERFACE_NAMELESS:
94fa7146 5961 case INTERFACE_ABSTRACT:
4ee9c684 5962 goto syntax;
5963
5964 case INTERFACE_GENERIC:
5965 if (gfc_get_symbol (name, NULL, &sym))
5966 goto done;
5967
1a9745d2 5968 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5969 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
950683ed 5970 sym->name, NULL) == FAILURE)
4ee9c684 5971 return MATCH_ERROR;
5972
5973 break;
5974
5975 case INTERFACE_INTRINSIC_OP:
dcb1b019 5976 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
4ee9c684 5977 {
dcb1b019 5978 gfc_current_ns->operator_access[op] =
4ee9c684 5979 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5980 }
5981 else
5982 {
5983 gfc_error ("Access specification of the %s operator at %C has "
dcb1b019 5984 "already been specified", gfc_op2string (op));
4ee9c684 5985 goto done;
5986 }
5987
5988 break;
5989
5990 case INTERFACE_USER_OP:
5991 uop = gfc_get_uop (name);
5992
5993 if (uop->access == ACCESS_UNKNOWN)
5994 {
1a9745d2 5995 uop->access = (st == ST_PUBLIC)
5996 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4ee9c684 5997 }
5998 else
5999 {
1a9745d2 6000 gfc_error ("Access specification of the .%s. operator at %C "
6001 "has already been specified", sym->name);
4ee9c684 6002 goto done;
6003 }
6004
6005 break;
6006 }
6007
6008 if (gfc_match_char (',') == MATCH_NO)
6009 break;
6010 }
6011
6012 if (gfc_match_eos () != MATCH_YES)
6013 goto syntax;
6014 return MATCH_YES;
6015
6016syntax:
6017 gfc_syntax_error (st);
6018
6019done:
6020 return MATCH_ERROR;
6021}
6022
6023
3ea52af3 6024match
6025gfc_match_protected (void)
6026{
6027 gfc_symbol *sym;
6028 match m;
6029
6030 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6031 {
6032 gfc_error ("PROTECTED at %C only allowed in specification "
6033 "part of a module");
6034 return MATCH_ERROR;
6035
6036 }
6037
1a9745d2 6038 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
3ea52af3 6039 == FAILURE)
6040 return MATCH_ERROR;
6041
6042 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6043 {
6044 return MATCH_ERROR;
6045 }
6046
6047 if (gfc_match_eos () == MATCH_YES)
6048 goto syntax;
6049
6050 for(;;)
6051 {
6052 m = gfc_match_symbol (&sym, 0);
6053 switch (m)
6054 {
6055 case MATCH_YES:
1a9745d2 6056 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6057 == FAILURE)
3ea52af3 6058 return MATCH_ERROR;
6059 goto next_item;
6060
6061 case MATCH_NO:
6062 break;
6063
6064 case MATCH_ERROR:
6065 return MATCH_ERROR;
6066 }
6067
6068 next_item:
6069 if (gfc_match_eos () == MATCH_YES)
6070 break;
6071 if (gfc_match_char (',') != MATCH_YES)
6072 goto syntax;
6073 }
6074
6075 return MATCH_YES;
6076
6077syntax:
6078 gfc_error ("Syntax error in PROTECTED statement at %C");
6079 return MATCH_ERROR;
6080}
6081
6082
a0527218 6083/* The PRIVATE statement is a bit weird in that it can be an attribute
69b1505f 6084 declaration, but also works as a standalone statement inside of a
4ee9c684 6085 type declaration or a module. */
6086
6087match
1a9745d2 6088gfc_match_private (gfc_statement *st)
4ee9c684 6089{
6090
6091 if (gfc_match ("private") != MATCH_YES)
6092 return MATCH_NO;
6093
e14bee04 6094 if (gfc_current_state () != COMP_MODULE
7fd88f6e 6095 && !(gfc_current_state () == COMP_DERIVED
6096 && gfc_state_stack->previous
6097 && gfc_state_stack->previous->state == COMP_MODULE)
6098 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6099 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6100 && gfc_state_stack->previous->previous->state == COMP_MODULE))
e14bee04 6101 {
6102 gfc_error ("PRIVATE statement at %C is only allowed in the "
6103 "specification part of a module");
6104 return MATCH_ERROR;
6105 }
6106
4ee9c684 6107 if (gfc_current_state () == COMP_DERIVED)
6108 {
6109 if (gfc_match_eos () == MATCH_YES)
6110 {
6111 *st = ST_PRIVATE;
6112 return MATCH_YES;
6113 }
6114
6115 gfc_syntax_error (ST_PRIVATE);
6116 return MATCH_ERROR;
6117 }
6118
6119 if (gfc_match_eos () == MATCH_YES)
6120 {
6121 *st = ST_PRIVATE;
6122 return MATCH_YES;
6123 }
6124
6125 *st = ST_ATTR_DECL;
6126 return access_attr_decl (ST_PRIVATE);
6127}
6128
6129
6130match
1a9745d2 6131gfc_match_public (gfc_statement *st)
4ee9c684 6132{
6133
6134 if (gfc_match ("public") != MATCH_YES)
6135 return MATCH_NO;
6136
e14bee04 6137 if (gfc_current_state () != COMP_MODULE)
6138 {
6139 gfc_error ("PUBLIC statement at %C is only allowed in the "
6140 "specification part of a module");
6141 return MATCH_ERROR;
6142 }
6143
4ee9c684 6144 if (gfc_match_eos () == MATCH_YES)
6145 {
6146 *st = ST_PUBLIC;
6147 return MATCH_YES;
6148 }
6149
6150 *st = ST_ATTR_DECL;
6151 return access_attr_decl (ST_PUBLIC);
6152}
6153
6154
6155/* Workhorse for gfc_match_parameter. */
6156
6157static match
6158do_parm (void)
6159{
6160 gfc_symbol *sym;
6161 gfc_expr *init;
6162 match m;
6163
6164 m = gfc_match_symbol (&sym, 0);
6165 if (m == MATCH_NO)
6166 gfc_error ("Expected variable name at %C in PARAMETER statement");
6167
6168 if (m != MATCH_YES)
6169 return m;
6170
6171 if (gfc_match_char ('=') == MATCH_NO)
6172 {
6173 gfc_error ("Expected = sign in PARAMETER statement at %C");
6174 return MATCH_ERROR;
6175 }
6176
6177 m = gfc_match_init_expr (&init);
6178 if (m == MATCH_NO)
6179 gfc_error ("Expected expression at %C in PARAMETER statement");
6180 if (m != MATCH_YES)
6181 return m;
6182
6183 if (sym->ts.type == BT_UNKNOWN
6184 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6185 {
6186 m = MATCH_ERROR;
6187 goto cleanup;
6188 }
6189
6190 if (gfc_check_assign_symbol (sym, init) == FAILURE
950683ed 6191 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4ee9c684 6192 {
6193 m = MATCH_ERROR;
6194 goto cleanup;
6195 }
6196
13aebeb0 6197 if (sym->value)
6198 {
6199 gfc_error ("Initializing already initialized variable at %C");
6200 m = MATCH_ERROR;
6201 goto cleanup;
6202 }
6203
c1b6da4a 6204 if (sym->ts.type == BT_CHARACTER
6205 && sym->ts.cl != NULL
6206 && sym->ts.cl->length != NULL
6207 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
6208 && init->expr_type == EXPR_CONSTANT
f97d54ed 6209 && init->ts.type == BT_CHARACTER)
c1b6da4a 6210 gfc_set_constant_character_len (
d1a39099 6211 mpz_get_si (sym->ts.cl->length->value.integer), init, -1);
f97d54ed 6212 else if (sym->ts.type == BT_CHARACTER && sym->ts.cl != NULL
6213 && sym->ts.cl->length == NULL)
6214 {
6215 int clen;
6216 if (init->expr_type == EXPR_CONSTANT)
6217 {
6218 clen = init->value.character.length;
6219 sym->ts.cl->length = gfc_int_expr (clen);
6220 }
6221 else if (init->expr_type == EXPR_ARRAY)
6222 {
6223 gfc_expr *p = init->value.constructor->expr;
6224 clen = p->value.character.length;
6225 sym->ts.cl->length = gfc_int_expr (clen);
6226 }
6227 else if (init->ts.cl && init->ts.cl->length)
6228 sym->ts.cl->length = gfc_copy_expr (sym->value->ts.cl->length);
6229 }
c1b6da4a 6230
4ee9c684 6231 sym->value = init;
6232 return MATCH_YES;
6233
6234cleanup:
6235 gfc_free_expr (init);
6236 return m;
6237}
6238
6239
6240/* Match a parameter statement, with the weird syntax that these have. */
6241
6242match
6243gfc_match_parameter (void)
6244{
6245 match m;
6246
6247 if (gfc_match_char ('(') == MATCH_NO)
6248 return MATCH_NO;
6249
6250 for (;;)
6251 {
6252 m = do_parm ();
6253 if (m != MATCH_YES)
6254 break;
6255
6256 if (gfc_match (" )%t") == MATCH_YES)
6257 break;
6258
6259 if (gfc_match_char (',') != MATCH_YES)
6260 {
6261 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6262 m = MATCH_ERROR;
6263 break;
6264 }
6265 }
6266
6267 return m;
6268}
6269
6270
6271/* Save statements have a special syntax. */
6272
6273match
6274gfc_match_save (void)
6275{
82f5ee13 6276 char n[GFC_MAX_SYMBOL_LEN+1];
6277 gfc_common_head *c;
4ee9c684 6278 gfc_symbol *sym;
6279 match m;
6280
6281 if (gfc_match_eos () == MATCH_YES)
6282 {
6283 if (gfc_current_ns->seen_save)
6284 {
1a9745d2 6285 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6286 "follows previous SAVE statement")
76e82f95 6287 == FAILURE)
6288 return MATCH_ERROR;
4ee9c684 6289 }
6290
6291 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6292 return MATCH_YES;
6293 }
6294
6295 if (gfc_current_ns->save_all)
6296 {
1a9745d2 6297 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6298 "blanket SAVE statement")
76e82f95 6299 == FAILURE)
6300 return MATCH_ERROR;
4ee9c684 6301 }
6302
6303 gfc_match (" ::");
6304
6305 for (;;)
6306 {
6307 m = gfc_match_symbol (&sym, 0);
6308 switch (m)
6309 {
6310 case MATCH_YES:
1a9745d2 6311 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6312 == FAILURE)
4ee9c684 6313 return MATCH_ERROR;
6314 goto next_item;
6315
6316 case MATCH_NO:
6317 break;
6318
6319 case MATCH_ERROR:
6320 return MATCH_ERROR;
6321 }
6322
82f5ee13 6323 m = gfc_match (" / %n /", &n);
4ee9c684 6324 if (m == MATCH_ERROR)
6325 return MATCH_ERROR;
6326 if (m == MATCH_NO)
6327 goto syntax;
6328
403ddc45 6329 c = gfc_get_common (n, 0);
82f5ee13 6330 c->saved = 1;
6331
4ee9c684 6332 gfc_current_ns->seen_save = 1;
6333
6334 next_item:
6335 if (gfc_match_eos () == MATCH_YES)
6336 break;
6337 if (gfc_match_char (',') != MATCH_YES)
6338 goto syntax;
6339 }
6340
6341 return MATCH_YES;
6342
6343syntax:
6344 gfc_error ("Syntax error in SAVE statement at %C");
6345 return MATCH_ERROR;
6346}
6347
6348
8f6339b6 6349match
6350gfc_match_value (void)
6351{
6352 gfc_symbol *sym;
6353 match m;
6354
1a9745d2 6355 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
8f6339b6 6356 == FAILURE)
6357 return MATCH_ERROR;
6358
6359 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6360 {
6361 return MATCH_ERROR;
6362 }
6363
6364 if (gfc_match_eos () == MATCH_YES)
6365 goto syntax;
6366
6367 for(;;)
6368 {
6369 m = gfc_match_symbol (&sym, 0);
6370 switch (m)
6371 {
6372 case MATCH_YES:
1a9745d2 6373 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6374 == FAILURE)
8f6339b6 6375 return MATCH_ERROR;
6376 goto next_item;
6377
6378 case MATCH_NO:
6379 break;
6380
6381 case MATCH_ERROR:
6382 return MATCH_ERROR;
6383 }
6384
6385 next_item:
6386 if (gfc_match_eos () == MATCH_YES)
6387 break;
6388 if (gfc_match_char (',') != MATCH_YES)
6389 goto syntax;
6390 }
6391
6392 return MATCH_YES;
6393
6394syntax:
6395 gfc_error ("Syntax error in VALUE statement at %C");
6396 return MATCH_ERROR;
6397}
6398
f6d0e37a 6399
ef814c81 6400match
6401gfc_match_volatile (void)
6402{
6403 gfc_symbol *sym;
6404 match m;
6405
1a9745d2 6406 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
ef814c81 6407 == FAILURE)
6408 return MATCH_ERROR;
6409
6410 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6411 {
6412 return MATCH_ERROR;
6413 }
6414
6415 if (gfc_match_eos () == MATCH_YES)
6416 goto syntax;
6417
6418 for(;;)
6419 {
2f241857 6420 /* VOLATILE is special because it can be added to host-associated
6421 symbols locally. */
6422 m = gfc_match_symbol (&sym, 1);
ef814c81 6423 switch (m)
6424 {
6425 case MATCH_YES:
1a9745d2 6426 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6427 == FAILURE)
ef814c81 6428 return MATCH_ERROR;
6429 goto next_item;
6430
6431 case MATCH_NO:
6432 break;
6433
6434 case MATCH_ERROR:
6435 return MATCH_ERROR;
6436 }
6437
6438 next_item:
6439 if (gfc_match_eos () == MATCH_YES)
6440 break;
6441 if (gfc_match_char (',') != MATCH_YES)
6442 goto syntax;
6443 }
6444
6445 return MATCH_YES;
6446
6447syntax:
6448 gfc_error ("Syntax error in VOLATILE statement at %C");
6449 return MATCH_ERROR;
6450}
6451
6452
4ee9c684 6453/* Match a module procedure statement. Note that we have to modify
6454 symbols in the parent's namespace because the current one was there
89d91d02 6455 to receive symbols that are in an interface's formal argument list. */
4ee9c684 6456
6457match
6458gfc_match_modproc (void)
6459{
6460 char name[GFC_MAX_SYMBOL_LEN + 1];
6461 gfc_symbol *sym;
6462 match m;
63d42079 6463 gfc_namespace *module_ns;
94ce9f74 6464 gfc_interface *old_interface_head, *interface;
4ee9c684 6465
6466 if (gfc_state_stack->state != COMP_INTERFACE
6467 || gfc_state_stack->previous == NULL
5cf92482 6468 || current_interface.type == INTERFACE_NAMELESS
6469 || current_interface.type == INTERFACE_ABSTRACT)
4ee9c684 6470 {
1a9745d2 6471 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6472 "interface");
4ee9c684 6473 return MATCH_ERROR;
6474 }
6475
63d42079 6476 module_ns = gfc_current_ns->parent;
6477 for (; module_ns; module_ns = module_ns->parent)
6478 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6479 break;
6480
6481 if (module_ns == NULL)
6482 return MATCH_ERROR;
6483
94ce9f74 6484 /* Store the current state of the interface. We will need it if we
6485 end up with a syntax error and need to recover. */
6486 old_interface_head = gfc_current_interface_head ();
6487
4ee9c684 6488 for (;;)
6489 {
94ce9f74 6490 bool last = false;
6491
4ee9c684 6492 m = gfc_match_name (name);
6493 if (m == MATCH_NO)
6494 goto syntax;
6495 if (m != MATCH_YES)
6496 return MATCH_ERROR;
6497
94ce9f74 6498 /* Check for syntax error before starting to add symbols to the
6499 current namespace. */
6500 if (gfc_match_eos () == MATCH_YES)
6501 last = true;
6502 if (!last && gfc_match_char (',') != MATCH_YES)
6503 goto syntax;
6504
6505 /* Now we're sure the syntax is valid, we process this item
6506 further. */
63d42079 6507 if (gfc_get_symbol (name, module_ns, &sym))
4ee9c684 6508 return MATCH_ERROR;
6509
6510 if (sym->attr.proc != PROC_MODULE
950683ed 6511 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6512 sym->name, NULL) == FAILURE)
4ee9c684 6513 return MATCH_ERROR;
6514
6515 if (gfc_add_interface (sym) == FAILURE)
6516 return MATCH_ERROR;
6517
3186f695 6518 sym->attr.mod_proc = 1;
6519
94ce9f74 6520 if (last)
4ee9c684 6521 break;
4ee9c684 6522 }
6523
6524 return MATCH_YES;
6525
6526syntax:
94ce9f74 6527 /* Restore the previous state of the interface. */
6528 interface = gfc_current_interface_head ();
6529 gfc_set_current_interface_head (old_interface_head);
6530
6531 /* Free the new interfaces. */
6532 while (interface != old_interface_head)
6533 {
6534 gfc_interface *i = interface->next;
6535 gfc_free (interface);
6536 interface = i;
6537 }
6538
6539 /* And issue a syntax error. */
4ee9c684 6540 gfc_syntax_error (ST_MODULE_PROC);
6541 return MATCH_ERROR;
6542}
6543
6544
ea94d76d 6545/* Check a derived type that is being extended. */
6546static gfc_symbol*
6547check_extended_derived_type (char *name)
6548{
6549 gfc_symbol *extended;
6550
6551 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6552 {
6553 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6554 return NULL;
6555 }
6556
6557 if (!extended)
6558 {
6559 gfc_error ("No such symbol in TYPE definition at %C");
6560 return NULL;
6561 }
6562
6563 if (extended->attr.flavor != FL_DERIVED)
6564 {
6565 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6566 "derived type", name);
6567 return NULL;
6568 }
6569
6570 if (extended->attr.is_bind_c)
6571 {
6572 gfc_error ("'%s' cannot be extended at %C because it "
6573 "is BIND(C)", extended->name);
6574 return NULL;
6575 }
6576
6577 if (extended->attr.sequence)
6578 {
6579 gfc_error ("'%s' cannot be extended at %C because it "
6580 "is a SEQUENCE type", extended->name);
6581 return NULL;
6582 }
6583
6584 return extended;
6585}
6586
6587
c5d33754 6588/* Match the optional attribute specifiers for a type declaration.
6589 Return MATCH_ERROR if an error is encountered in one of the handled
6590 attributes (public, private, bind(c)), MATCH_NO if what's found is
6591 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6592 checking on attribute conflicts needs to be done. */
4ee9c684 6593
6594match
ea94d76d 6595gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
4ee9c684 6596{
c5d33754 6597 /* See if the derived type is marked as private. */
4ee9c684 6598 if (gfc_match (" , private") == MATCH_YES)
6599 {
e14bee04 6600 if (gfc_current_state () != COMP_MODULE)
4ee9c684 6601 {
e14bee04 6602 gfc_error ("Derived type at %C can only be PRIVATE in the "
6603 "specification part of a module");
4ee9c684 6604 return MATCH_ERROR;
6605 }
6606
c5d33754 6607 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4ee9c684 6608 return MATCH_ERROR;
4ee9c684 6609 }
c5d33754 6610 else if (gfc_match (" , public") == MATCH_YES)
4ee9c684 6611 {
e14bee04 6612 if (gfc_current_state () != COMP_MODULE)
4ee9c684 6613 {
e14bee04 6614 gfc_error ("Derived type at %C can only be PUBLIC in the "
6615 "specification part of a module");
4ee9c684 6616 return MATCH_ERROR;
6617 }
6618
c5d33754 6619 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4ee9c684 6620 return MATCH_ERROR;
4ee9c684 6621 }
ac5f2650 6622 else if (gfc_match (" , bind ( c )") == MATCH_YES)
c5d33754 6623 {
6624 /* If the type is defined to be bind(c) it then needs to make
6625 sure that all fields are interoperable. This will
6626 need to be a semantic check on the finished derived type.
6627 See 15.2.3 (lines 9-12) of F2003 draft. */
6628 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6629 return MATCH_ERROR;
6630
6631 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6632 }
ac5f2650 6633 else if (gfc_match (" , abstract") == MATCH_YES)
6634 {
6635 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6636 == FAILURE)
6637 return MATCH_ERROR;
6638
6639 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6640 return MATCH_ERROR;
6641 }
ea94d76d 6642 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6643 {
e485ad6b 6644 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
ea94d76d 6645 return MATCH_ERROR;
6646 }
c5d33754 6647 else
6648 return MATCH_NO;
6649
6650 /* If we get here, something matched. */
6651 return MATCH_YES;
6652}
6653
6654
6655/* Match the beginning of a derived type declaration. If a type name
6656 was the result of a function, then it is possible to have a symbol
6657 already to be known as a derived type yet have no components. */
6658
6659match
6660gfc_match_derived_decl (void)
6661{
6662 char name[GFC_MAX_SYMBOL_LEN + 1];
ea94d76d 6663 char parent[GFC_MAX_SYMBOL_LEN + 1];
c5d33754 6664 symbol_attribute attr;
6665 gfc_symbol *sym;
ea94d76d 6666 gfc_symbol *extended;
c5d33754 6667 match m;
6668 match is_type_attr_spec = MATCH_NO;
33e86520 6669 bool seen_attr = false;
c5d33754 6670
6671 if (gfc_current_state () == COMP_DERIVED)
6672 return MATCH_NO;
6673
ea94d76d 6674 name[0] = '\0';
6675 parent[0] = '\0';
c5d33754 6676 gfc_clear_attr (&attr);
ea94d76d 6677 extended = NULL;
c5d33754 6678
6679 do
6680 {
ea94d76d 6681 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
c5d33754 6682 if (is_type_attr_spec == MATCH_ERROR)
6683 return MATCH_ERROR;
33e86520 6684 if (is_type_attr_spec == MATCH_YES)
6685 seen_attr = true;
c5d33754 6686 } while (is_type_attr_spec == MATCH_YES);
4ee9c684 6687
e485ad6b 6688 /* Deal with derived type extensions. The extension attribute has
6689 been added to 'attr' but now the parent type must be found and
6690 checked. */
ea94d76d 6691 if (parent[0])
6692 extended = check_extended_derived_type (parent);
6693
6694 if (parent[0] && !extended)
6695 return MATCH_ERROR;
6696
33e86520 6697 if (gfc_match (" ::") != MATCH_YES && seen_attr)
4ee9c684 6698 {
6699 gfc_error ("Expected :: in TYPE definition at %C");
6700 return MATCH_ERROR;
6701 }
6702
6703 m = gfc_match (" %n%t", name);
6704 if (m != MATCH_YES)
6705 return m;
6706
a3055431 6707 /* Make sure the name is not the name of an intrinsic type. */
6708 if (gfc_is_intrinsic_typename (name))
4ee9c684 6709 {
1a9745d2 6710 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6711 "type", name);
4ee9c684 6712 return MATCH_ERROR;
6713 }
6714
6715 if (gfc_get_symbol (name, NULL, &sym))
6716 return MATCH_ERROR;
6717
6718 if (sym->ts.type != BT_UNKNOWN)
6719 {
6720 gfc_error ("Derived type name '%s' at %C already has a basic type "
6721 "of %s", sym->name, gfc_typename (&sym->ts));
6722 return MATCH_ERROR;
6723 }
6724
6725 /* The symbol may already have the derived attribute without the
6726 components. The ways this can happen is via a function
6727 definition, an INTRINSIC statement or a subtype in another
6728 derived type that is a pointer. The first part of the AND clause
69b1505f 6729 is true if the symbol is not the return value of a function. */
4ee9c684 6730 if (sym->attr.flavor != FL_DERIVED
950683ed 6731 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4ee9c684 6732 return MATCH_ERROR;
6733
e6b82afc 6734 if (sym->components != NULL || sym->attr.zero_comp)
4ee9c684 6735 {
1a9745d2 6736 gfc_error ("Derived type definition of '%s' at %C has already been "
6737 "defined", sym->name);
4ee9c684 6738 return MATCH_ERROR;
6739 }
6740
6741 if (attr.access != ACCESS_UNKNOWN
950683ed 6742 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4ee9c684 6743 return MATCH_ERROR;
6744
c5d33754 6745 /* See if the derived type was labeled as bind(c). */
6746 if (attr.is_bind_c != 0)
6747 sym->attr.is_bind_c = attr.is_bind_c;
6748
223f0f57 6749 /* Construct the f2k_derived namespace if it is not yet there. */
6750 if (!sym->f2k_derived)
6751 sym->f2k_derived = gfc_get_namespace (NULL, 0);
ea94d76d 6752
6753 if (extended && !sym->components)
6754 {
6755 gfc_component *p;
6756 gfc_symtree *st;
6757
6758 /* Add the extended derived type as the first component. */
6759 gfc_add_component (sym, parent, &p);
e485ad6b 6760 sym->attr.extension = attr.extension;
ea94d76d 6761 extended->refs++;
6762 gfc_set_sym_referenced (extended);
6763
6764 p->ts.type = BT_DERIVED;
6765 p->ts.derived = extended;
6766 p->initializer = gfc_default_initializer (&p->ts);
6767
6768 /* Provide the links between the extended type and its extension. */
6769 if (!extended->f2k_derived)
6770 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6771 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6772 st->n.sym = sym;
6773 }
6774
ac5f2650 6775 /* Take over the ABSTRACT attribute. */
6776 sym->attr.abstract = attr.abstract;
6777
4ee9c684 6778 gfc_new_block = sym;
6779
6780 return MATCH_YES;
6781}
b549d2a5 6782
6783
6784/* Cray Pointees can be declared as:
6785 pointer (ipt, a (n,m,...,*))
6786 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6787 cheat and set a constant bound of 1 for the last dimension, if this
6788 is the case. Since there is no bounds-checking for Cray Pointees,
6789 this will be okay. */
6790
8458f4ca 6791match
b549d2a5 6792gfc_mod_pointee_as (gfc_array_spec *as)
6793{
6794 as->cray_pointee = true; /* This will be useful to know later. */
6795 if (as->type == AS_ASSUMED_SIZE)
6796 {
6797 as->type = AS_EXPLICIT;
6798 as->upper[as->rank - 1] = gfc_int_expr (1);
6799 as->cp_was_assumed = true;
6800 }
6801 else if (as->type == AS_ASSUMED_SHAPE)
6802 {
6803 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6804 return MATCH_ERROR;
6805 }
6806 return MATCH_YES;
6807}
3b6a4b41 6808
6809
6810/* Match the enum definition statement, here we are trying to match
6811 the first line of enum definition statement.
6812 Returns MATCH_YES if match is found. */
6813
6814match
6815gfc_match_enum (void)
6816{
6817 match m;
6818
6819 m = gfc_match_eos ();
6820 if (m != MATCH_YES)
6821 return m;
6822
60fbbf9e 6823 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
3b6a4b41 6824 == FAILURE)
6825 return MATCH_ERROR;
6826
6827 return MATCH_YES;
6828}
6829
6830
fe1328da 6831/* Returns an initializer whose value is one higher than the value of the
6832 LAST_INITIALIZER argument. If the argument is NULL, the
6833 initializers value will be set to zero. The initializer's kind
6834 will be set to gfc_c_int_kind.
6835
6836 If -fshort-enums is given, the appropriate kind will be selected
6837 later after all enumerators have been parsed. A warning is issued
6838 here if an initializer exceeds gfc_c_int_kind. */
6839
6840static gfc_expr *
6841enum_initializer (gfc_expr *last_initializer, locus where)
6842{
6843 gfc_expr *result;
6844
6845 result = gfc_get_expr ();
6846 result->expr_type = EXPR_CONSTANT;
6847 result->ts.type = BT_INTEGER;
6848 result->ts.kind = gfc_c_int_kind;
6849 result->where = where;
6850
6851 mpz_init (result->value.integer);
6852
6853 if (last_initializer != NULL)
6854 {
6855 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
6856 result->where = last_initializer->where;
6857
6858 if (gfc_check_integer_range (result->value.integer,
6859 gfc_c_int_kind) != ARITH_OK)
6860 {
6861 gfc_error ("Enumerator exceeds the C integer type at %C");
6862 return NULL;
6863 }
6864 }
6865 else
6866 {
6867 /* Control comes here, if it's the very first enumerator and no
6868 initializer has been given. It will be initialized to zero. */
6869 mpz_set_si (result->value.integer, 0);
6870 }
6871
6872 return result;
6873}
6874
6875
60fbbf9e 6876/* Match a variable name with an optional initializer. When this
6877 subroutine is called, a variable is expected to be parsed next.
6878 Depending on what is happening at the moment, updates either the
6879 symbol table or the current interface. */
6880
6881static match
6882enumerator_decl (void)
6883{
6884 char name[GFC_MAX_SYMBOL_LEN + 1];
6885 gfc_expr *initializer;
6886 gfc_array_spec *as = NULL;
6887 gfc_symbol *sym;
6888 locus var_locus;
6889 match m;
f6bb8f4b 6890 gfc_try t;
60fbbf9e 6891 locus old_locus;
6892
6893 initializer = NULL;
6894 old_locus = gfc_current_locus;
6895
6896 /* When we get here, we've just matched a list of attributes and
6897 maybe a type and a double colon. The next thing we expect to see
6898 is the name of the symbol. */
6899 m = gfc_match_name (name);
6900 if (m != MATCH_YES)
6901 goto cleanup;
6902
6903 var_locus = gfc_current_locus;
6904
6905 /* OK, we've successfully matched the declaration. Now put the
6906 symbol in the current namespace. If we fail to create the symbol,
6907 bail out. */
6908 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6909 {
6910 m = MATCH_ERROR;
6911 goto cleanup;
6912 }
6913
6914 /* The double colon must be present in order to have initializers.
6915 Otherwise the statement is ambiguous with an assignment statement. */
6916 if (colon_seen)
6917 {
6918 if (gfc_match_char ('=') == MATCH_YES)
6919 {
6920 m = gfc_match_init_expr (&initializer);
6921 if (m == MATCH_NO)
6922 {
6923 gfc_error ("Expected an initialization expression at %C");
6924 m = MATCH_ERROR;
6925 }
6926
6927 if (m != MATCH_YES)
6928 goto cleanup;
6929 }
6930 }
6931
6932 /* If we do not have an initializer, the initialization value of the
6933 previous enumerator (stored in last_initializer) is incremented
6934 by 1 and is used to initialize the current enumerator. */
6935 if (initializer == NULL)
fe1328da 6936 initializer = enum_initializer (last_initializer, old_locus);
e14bee04 6937
60fbbf9e 6938 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6939 {
6940 gfc_error("ENUMERATOR %L not initialized with integer expression",
6941 &var_locus);
e14bee04 6942 m = MATCH_ERROR;
60fbbf9e 6943 gfc_free_enum_history ();
6944 goto cleanup;
6945 }
6946
6947 /* Store this current initializer, for the next enumerator variable
6948 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6949 use last_initializer below. */
6950 last_initializer = initializer;
6951 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6952
6953 /* Maintain enumerator history. */
6954 gfc_find_symbol (name, NULL, 0, &sym);
6955 create_enum_history (sym, last_initializer);
6956
6957 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6958
6959cleanup:
6960 /* Free stuff up and return. */
6961 gfc_free_expr (initializer);
6962
6963 return m;
6964}
6965
6966
f6d0e37a 6967/* Match the enumerator definition statement. */
3b6a4b41 6968
6969match
6970gfc_match_enumerator_def (void)
6971{
6972 match m;
f6bb8f4b 6973 gfc_try t;
e14bee04 6974
3b6a4b41 6975 gfc_clear_ts (&current_ts);
e14bee04 6976
3b6a4b41 6977 m = gfc_match (" enumerator");
6978 if (m != MATCH_YES)
6979 return m;
60fbbf9e 6980
6981 m = gfc_match (" :: ");
6982 if (m == MATCH_ERROR)
6983 return m;
6984
6985 colon_seen = (m == MATCH_YES);
e14bee04 6986
3b6a4b41 6987 if (gfc_current_state () != COMP_ENUM)
6988 {
6989 gfc_error ("ENUM definition statement expected before %C");
6990 gfc_free_enum_history ();
6991 return MATCH_ERROR;
6992 }
6993
6994 (&current_ts)->type = BT_INTEGER;
6995 (&current_ts)->kind = gfc_c_int_kind;
e14bee04 6996
60fbbf9e 6997 gfc_clear_attr (&current_attr);
6998 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6999 if (t == FAILURE)
3b6a4b41 7000 {
60fbbf9e 7001 m = MATCH_ERROR;
3b6a4b41 7002 goto cleanup;
7003 }
7004
3b6a4b41 7005 for (;;)
7006 {
60fbbf9e 7007 m = enumerator_decl ();
3b6a4b41 7008 if (m == MATCH_ERROR)
7009 goto cleanup;
7010 if (m == MATCH_NO)
7011 break;
7012
7013 if (gfc_match_eos () == MATCH_YES)
7014 goto cleanup;
7015 if (gfc_match_char (',') != MATCH_YES)
7016 break;
7017 }
7018
7019 if (gfc_current_state () == COMP_ENUM)
7020 {
7021 gfc_free_enum_history ();
7022 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7023 m = MATCH_ERROR;
7024 }
7025
7026cleanup:
7027 gfc_free_array_spec (current_as);
7028 current_as = NULL;
7029 return m;
7030
7031}
7032
e449e4dd 7033
7fd88f6e 7034/* Match binding attributes. */
7035
7036static match
64e93293 7037match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7fd88f6e 7038{
7039 bool found_passing = false;
64e93293 7040 bool seen_ptr = false;
fe9b08a2 7041 match m = MATCH_YES;
7fd88f6e 7042
7043 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7044 this case the defaults are in there. */
7045 ba->access = ACCESS_UNKNOWN;
7046 ba->pass_arg = NULL;
7047 ba->pass_arg_num = 0;
7048 ba->nopass = 0;
7049 ba->non_overridable = 0;
61c3b81d 7050 ba->deferred = 0;
fe9b08a2 7051 ba->ppc = ppc;
7fd88f6e 7052
7053 /* If we find a comma, we believe there are binding attributes. */
fe9b08a2 7054 m = gfc_match_char (',');
7055 if (m == MATCH_NO)
7056 goto done;
7fd88f6e 7057
7058 do
7059 {
e2f06a48 7060 /* Access specifier. */
7061
7062 m = gfc_match (" public");
7fd88f6e 7063 if (m == MATCH_ERROR)
7064 goto error;
7065 if (m == MATCH_YES)
7066 {
e2f06a48 7067 if (ba->access != ACCESS_UNKNOWN)
7fd88f6e 7068 {
e2f06a48 7069 gfc_error ("Duplicate access-specifier at %C");
7fd88f6e 7070 goto error;
7071 }
7072
e2f06a48 7073 ba->access = ACCESS_PUBLIC;
7fd88f6e 7074 continue;
7075 }
7076
e2f06a48 7077 m = gfc_match (" private");
7fd88f6e 7078 if (m == MATCH_ERROR)
7079 goto error;
7080 if (m == MATCH_YES)
7081 {
e2f06a48 7082 if (ba->access != ACCESS_UNKNOWN)
7fd88f6e 7083 {
e2f06a48 7084 gfc_error ("Duplicate access-specifier at %C");
7fd88f6e 7085 goto error;
7086 }
7087
e2f06a48 7088 ba->access = ACCESS_PRIVATE;
7fd88f6e 7089 continue;
7090 }
7091
e2f06a48 7092 /* If inside GENERIC, the following is not allowed. */
7093 if (!generic)
7fd88f6e 7094 {
7fd88f6e 7095
e2f06a48 7096 /* NOPASS flag. */
7097 m = gfc_match (" nopass");
7098 if (m == MATCH_ERROR)
7099 goto error;
7100 if (m == MATCH_YES)
7fd88f6e 7101 {
e2f06a48 7102 if (found_passing)
7103 {
7104 gfc_error ("Binding attributes already specify passing,"
7105 " illegal NOPASS at %C");
7106 goto error;
7107 }
7108
7109 found_passing = true;
7110 ba->nopass = 1;
7111 continue;
7fd88f6e 7112 }
7113
e2f06a48 7114 /* PASS possibly including argument. */
7115 m = gfc_match (" pass");
7116 if (m == MATCH_ERROR)
7117 goto error;
7118 if (m == MATCH_YES)
7fd88f6e 7119 {
e2f06a48 7120 char arg[GFC_MAX_SYMBOL_LEN + 1];
7121
7122 if (found_passing)
7123 {
7124 gfc_error ("Binding attributes already specify passing,"
7125 " illegal PASS at %C");
7126 goto error;
7127 }
7128
7129 m = gfc_match (" ( %n )", arg);
7130 if (m == MATCH_ERROR)
7131 goto error;
7132 if (m == MATCH_YES)
fe9b08a2 7133 ba->pass_arg = gfc_get_string (arg);
e2f06a48 7134 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7135
7136 found_passing = true;
7137 ba->nopass = 0;
7138 continue;
7fd88f6e 7139 }
7140
64e93293 7141 if (ppc)
7142 {
7143 /* POINTER flag. */
7144 m = gfc_match (" pointer");
7145 if (m == MATCH_ERROR)
7146 goto error;
7147 if (m == MATCH_YES)
7148 {
7149 if (seen_ptr)
7150 {
7151 gfc_error ("Duplicate POINTER attribute at %C");
7152 goto error;
7153 }
7154
7155 seen_ptr = true;
64e93293 7156 continue;
7157 }
7158 }
7159 else
7160 {
7161 /* NON_OVERRIDABLE flag. */
7162 m = gfc_match (" non_overridable");
7163 if (m == MATCH_ERROR)
7164 goto error;
7165 if (m == MATCH_YES)
7166 {
7167 if (ba->non_overridable)
7168 {
7169 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7170 goto error;
7171 }
7172
7173 ba->non_overridable = 1;
7174 continue;
7175 }
7176
7177 /* DEFERRED flag. */
7178 m = gfc_match (" deferred");
7179 if (m == MATCH_ERROR)
7180 goto error;
7181 if (m == MATCH_YES)
7182 {
7183 if (ba->deferred)
7184 {
7185 gfc_error ("Duplicate DEFERRED at %C");
7186 goto error;
7187 }
7188
7189 ba->deferred = 1;
7190 continue;
7191 }
7192 }
7193
7fd88f6e 7194 }
7195
7196 /* Nothing matching found. */
e2f06a48 7197 if (generic)
7198 gfc_error ("Expected access-specifier at %C");
7199 else
7200 gfc_error ("Expected binding attribute at %C");
7fd88f6e 7201 goto error;
7202 }
7203 while (gfc_match_char (',') == MATCH_YES);
7204
61c3b81d 7205 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7206 if (ba->non_overridable && ba->deferred)
7207 {
7208 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7209 goto error;
7210 }
7211
fe9b08a2 7212 m = MATCH_YES;
7213
7214done:
e2f06a48 7215 if (ba->access == ACCESS_UNKNOWN)
7216 ba->access = gfc_typebound_default_access;
7217
64e93293 7218 if (ppc && !seen_ptr)
7219 {
7220 gfc_error ("POINTER attribute is required for procedure pointer component"
7221 " at %C");
7222 goto error;
7223 }
7224
fe9b08a2 7225 return m;
7fd88f6e 7226
7227error:
7fd88f6e 7228 return MATCH_ERROR;
7229}
7230
7231
7232/* Match a PROCEDURE specific binding inside a derived type. */
7233
7234static match
7235match_procedure_in_type (void)
7236{
7237 char name[GFC_MAX_SYMBOL_LEN + 1];
7238 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
61c3b81d 7239 char* target = NULL;
7fd88f6e 7240 gfc_typebound_proc* tb;
7241 bool seen_colons;
7242 bool seen_attrs;
7243 match m;
7244 gfc_symtree* stree;
7245 gfc_namespace* ns;
7246 gfc_symbol* block;
7247
7248 /* Check current state. */
7249 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7250 block = gfc_state_stack->previous->sym;
7251 gcc_assert (block);
7252
61c3b81d 7253 /* Try to match PROCEDURE(interface). */
7fd88f6e 7254 if (gfc_match (" (") == MATCH_YES)
7255 {
61c3b81d 7256 m = gfc_match_name (target_buf);
7257 if (m == MATCH_ERROR)
7258 return m;
7259 if (m != MATCH_YES)
7260 {
7261 gfc_error ("Interface-name expected after '(' at %C");
7262 return MATCH_ERROR;
7263 }
7264
7265 if (gfc_match (" )") != MATCH_YES)
7266 {
7267 gfc_error ("')' expected at %C");
7268 return MATCH_ERROR;
7269 }
7270
7271 target = target_buf;
7fd88f6e 7272 }
7273
7274 /* Construct the data structure. */
930fe1de 7275 tb = gfc_get_typebound_proc ();
7fd88f6e 7276 tb->where = gfc_current_locus;
e2f06a48 7277 tb->is_generic = 0;
7fd88f6e 7278
7279 /* Match binding attributes. */
64e93293 7280 m = match_binding_attributes (tb, false, false);
7fd88f6e 7281 if (m == MATCH_ERROR)
7282 return m;
7283 seen_attrs = (m == MATCH_YES);
7284
61c3b81d 7285 /* Check that attribute DEFERRED is given iff an interface is specified, which
7286 means target != NULL. */
7287 if (tb->deferred && !target)
7288 {
7289 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7290 return MATCH_ERROR;
7291 }
7292 if (target && !tb->deferred)
7293 {
7294 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7295 return MATCH_ERROR;
7296 }
7297
7fd88f6e 7298 /* Match the colons. */
7299 m = gfc_match (" ::");
7300 if (m == MATCH_ERROR)
7301 return m;
7302 seen_colons = (m == MATCH_YES);
7303 if (seen_attrs && !seen_colons)
7304 {
7305 gfc_error ("Expected '::' after binding-attributes at %C");
7306 return MATCH_ERROR;
7307 }
7308
7309 /* Match the binding name. */
7310 m = gfc_match_name (name);
7311 if (m == MATCH_ERROR)
7312 return m;
7313 if (m == MATCH_NO)
7314 {
7315 gfc_error ("Expected binding name at %C");
7316 return MATCH_ERROR;
7317 }
7318
7319 /* Try to match the '=> target', if it's there. */
7fd88f6e 7320 m = gfc_match (" =>");
7321 if (m == MATCH_ERROR)
7322 return m;
7323 if (m == MATCH_YES)
7324 {
61c3b81d 7325 if (tb->deferred)
7326 {
7327 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7328 return MATCH_ERROR;
7329 }
7330
7fd88f6e 7331 if (!seen_colons)
7332 {
7333 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7334 " at %C");
7335 return MATCH_ERROR;
7336 }
7337
7338 m = gfc_match_name (target_buf);
7339 if (m == MATCH_ERROR)
7340 return m;
7341 if (m == MATCH_NO)
7342 {
7343 gfc_error ("Expected binding target after '=>' at %C");
7344 return MATCH_ERROR;
7345 }
7346 target = target_buf;
7347 }
7348
7349 /* Now we should have the end. */
7350 m = gfc_match_eos ();
7351 if (m == MATCH_ERROR)
7352 return m;
7353 if (m == MATCH_NO)
7354 {
7355 gfc_error ("Junk after PROCEDURE declaration at %C");
7356 return MATCH_ERROR;
7357 }
7358
7359 /* If no target was found, it has the same name as the binding. */
7360 if (!target)
7361 target = name;
7362
7363 /* Get the namespace to insert the symbols into. */
7364 ns = block->f2k_derived;
7365 gcc_assert (ns);
7366
61c3b81d 7367 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7368 if (tb->deferred && !block->attr.abstract)
7369 {
7370 gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7371 block->name);
7372 return MATCH_ERROR;
7373 }
7374
7fd88f6e 7375 /* See if we already have a binding with this name in the symtree which would
e2f06a48 7376 be an error. If a GENERIC already targetted this binding, it may be
7377 already there but then typebound is still NULL. */
3323e9b1 7378 stree = gfc_find_symtree (ns->tb_sym_root, name);
7379 if (stree && stree->n.tb)
7fd88f6e 7380 {
7381 gfc_error ("There's already a procedure with binding name '%s' for the"
7382 " derived type '%s' at %C", name, block->name);
7383 return MATCH_ERROR;
7384 }
7385
7386 /* Insert it and set attributes. */
3323e9b1 7387
7388 if (!stree)
7389 {
7390 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7391 gcc_assert (stree);
7392 }
7393 stree->n.tb = tb;
7394
36b0a1b0 7395 if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific, false))
7fd88f6e 7396 return MATCH_ERROR;
e2f06a48 7397 gfc_set_sym_referenced (tb->u.specific->n.sym);
7fd88f6e 7398
7399 return MATCH_YES;
7400}
7401
7402
e2f06a48 7403/* Match a GENERIC procedure binding inside a derived type. */
7404
7405match
7406gfc_match_generic (void)
7407{
7408 char name[GFC_MAX_SYMBOL_LEN + 1];
7409 gfc_symbol* block;
7410 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7411 gfc_typebound_proc* tb;
7412 gfc_symtree* st;
7413 gfc_namespace* ns;
7414 match m;
7415
7416 /* Check current state. */
7417 if (gfc_current_state () == COMP_DERIVED)
7418 {
7419 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7420 return MATCH_ERROR;
7421 }
7422 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7423 return MATCH_NO;
7424 block = gfc_state_stack->previous->sym;
7425 ns = block->f2k_derived;
7426 gcc_assert (block && ns);
7427
7428 /* See if we get an access-specifier. */
64e93293 7429 m = match_binding_attributes (&tbattr, true, false);
e2f06a48 7430 if (m == MATCH_ERROR)
7431 goto error;
7432
7433 /* Now the colons, those are required. */
7434 if (gfc_match (" ::") != MATCH_YES)
7435 {
7436 gfc_error ("Expected '::' at %C");
7437 goto error;
7438 }
7439
7440 /* The binding name and =>. */
7441 m = gfc_match (" %n =>", name);
7442 if (m == MATCH_ERROR)
7443 return MATCH_ERROR;
7444 if (m == MATCH_NO)
7445 {
7446 gfc_error ("Expected generic name at %C");
7447 goto error;
7448 }
7449
7450 /* If there's already something with this name, check that it is another
7451 GENERIC and then extend that rather than build a new node. */
3323e9b1 7452 st = gfc_find_symtree (ns->tb_sym_root, name);
e2f06a48 7453 if (st)
7454 {
3323e9b1 7455 gcc_assert (st->n.tb);
7456 tb = st->n.tb;
7457
7458 if (!tb->is_generic)
e2f06a48 7459 {
7460 gfc_error ("There's already a non-generic procedure with binding name"
7461 " '%s' for the derived type '%s' at %C",
7462 name, block->name);
7463 goto error;
7464 }
7465
e2f06a48 7466 if (tb->access != tbattr.access)
7467 {
7468 gfc_error ("Binding at %C must have the same access as already"
7469 " defined binding '%s'", name);
7470 goto error;
7471 }
7472 }
7473 else
7474 {
3323e9b1 7475 st = gfc_new_symtree (&ns->tb_sym_root, name);
7476 gcc_assert (st);
e2f06a48 7477
3323e9b1 7478 st->n.tb = tb = gfc_get_typebound_proc ();
e2f06a48 7479 tb->where = gfc_current_locus;
7480 tb->access = tbattr.access;
7481 tb->is_generic = 1;
7482 tb->u.generic = NULL;
7483 }
7484
7485 /* Now, match all following names as specific targets. */
7486 do
7487 {
7488 gfc_symtree* target_st;
7489 gfc_tbp_generic* target;
7490
7491 m = gfc_match_name (name);
7492 if (m == MATCH_ERROR)
7493 goto error;
7494 if (m == MATCH_NO)
7495 {
7496 gfc_error ("Expected specific binding name at %C");
7497 goto error;
7498 }
7499
3323e9b1 7500 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
e2f06a48 7501
7502 /* See if this is a duplicate specification. */
7503 for (target = tb->u.generic; target; target = target->next)
7504 if (target_st == target->specific_st)
7505 {
7506 gfc_error ("'%s' already defined as specific binding for the"
3323e9b1 7507 " generic '%s' at %C", name, st->name);
e2f06a48 7508 goto error;
7509 }
7510
e2f06a48 7511 target = gfc_get_tbp_generic ();
7512 target->specific_st = target_st;
7513 target->specific = NULL;
7514 target->next = tb->u.generic;
7515 tb->u.generic = target;
7516 }
7517 while (gfc_match (" ,") == MATCH_YES);
7518
7519 /* Here should be the end. */
7520 if (gfc_match_eos () != MATCH_YES)
7521 {
7522 gfc_error ("Junk after GENERIC binding at %C");
7523 goto error;
7524 }
7525
7526 return MATCH_YES;
7527
7528error:
7529 return MATCH_ERROR;
7530}
7531
7532
223f0f57 7533/* Match a FINAL declaration inside a derived type. */
7534
7535match
7536gfc_match_final_decl (void)
7537{
7538 char name[GFC_MAX_SYMBOL_LEN + 1];
7539 gfc_symbol* sym;
7540 match m;
7541 gfc_namespace* module_ns;
7542 bool first, last;
7fd88f6e 7543 gfc_symbol* block;
223f0f57 7544
7fd88f6e 7545 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
223f0f57 7546 {
7547 gfc_error ("FINAL declaration at %C must be inside a derived type "
7fd88f6e 7548 "CONTAINS section");
223f0f57 7549 return MATCH_ERROR;
7550 }
7551
7fd88f6e 7552 block = gfc_state_stack->previous->sym;
7553 gcc_assert (block);
223f0f57 7554
7fd88f6e 7555 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7556 || gfc_state_stack->previous->previous->state != COMP_MODULE)
223f0f57 7557 {
7558 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7559 " specification part of a MODULE");
7560 return MATCH_ERROR;
7561 }
7562
7563 module_ns = gfc_current_ns;
7564 gcc_assert (module_ns);
7565 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7566
7567 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7568 if (gfc_match (" ::") == MATCH_ERROR)
7569 return MATCH_ERROR;
7570
7571 /* Match the sequence of procedure names. */
7572 first = true;
7573 last = false;
7574 do
7575 {
7576 gfc_finalizer* f;
7577
7578 if (first && gfc_match_eos () == MATCH_YES)
7579 {
7580 gfc_error ("Empty FINAL at %C");
7581 return MATCH_ERROR;
7582 }
7583
7584 m = gfc_match_name (name);
7585 if (m == MATCH_NO)
7586 {
7587 gfc_error ("Expected module procedure name at %C");
7588 return MATCH_ERROR;
7589 }
7590 else if (m != MATCH_YES)
7591 return MATCH_ERROR;
7592
7593 if (gfc_match_eos () == MATCH_YES)
7594 last = true;
7595 if (!last && gfc_match_char (',') != MATCH_YES)
7596 {
7597 gfc_error ("Expected ',' at %C");
7598 return MATCH_ERROR;
7599 }
7600
7601 if (gfc_get_symbol (name, module_ns, &sym))
7602 {
7603 gfc_error ("Unknown procedure name \"%s\" at %C", name);
7604 return MATCH_ERROR;
7605 }
7606
7607 /* Mark the symbol as module procedure. */
7608 if (sym->attr.proc != PROC_MODULE
7609 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7610 sym->name, NULL) == FAILURE)
7611 return MATCH_ERROR;
7612
7613 /* Check if we already have this symbol in the list, this is an error. */
7fd88f6e 7614 for (f = block->f2k_derived->finalizers; f; f = f->next)
e449e4dd 7615 if (f->proc_sym == sym)
223f0f57 7616 {
7617 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7618 name);
7619 return MATCH_ERROR;
7620 }
7621
7622 /* Add this symbol to the list of finalizers. */
7fd88f6e 7623 gcc_assert (block->f2k_derived);
223f0f57 7624 ++sym->refs;
48d8ad5a 7625 f = XCNEW (gfc_finalizer);
e449e4dd 7626 f->proc_sym = sym;
7627 f->proc_tree = NULL;
223f0f57 7628 f->where = gfc_current_locus;
7fd88f6e 7629 f->next = block->f2k_derived->finalizers;
7630 block->f2k_derived->finalizers = f;
223f0f57 7631
7632 first = false;
7633 }
7634 while (!last);
7635
7636 return MATCH_YES;
7637}
36b0a1b0 7638
7639
7640const ext_attr_t ext_attr_list[] = {
7641 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
7642 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
7643 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
7644 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
7645 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
7646 { NULL, EXT_ATTR_LAST, NULL }
7647};
7648
7649/* Match a !GCC$ ATTRIBUTES statement of the form:
7650 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
7651 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
7652
7653 TODO: We should support all GCC attributes using the same syntax for
7654 the attribute list, i.e. the list in C
7655 __attributes(( attribute-list ))
7656 matches then
7657 !GCC$ ATTRIBUTES attribute-list ::
7658 Cf. c-parser.c's c_parser_attributes; the data can then directly be
7659 saved into a TREE.
7660
7661 As there is absolutely no risk of confusion, we should never return
7662 MATCH_NO. */
7663match
7664gfc_match_gcc_attributes (void)
7665{
7666 symbol_attribute attr;
7667 char name[GFC_MAX_SYMBOL_LEN + 1];
7668 unsigned id;
7669 gfc_symbol *sym;
7670 match m;
7671
7672 gfc_clear_attr (&attr);
7673 for(;;)
7674 {
7675 char ch;
7676
7677 if (gfc_match_name (name) != MATCH_YES)
7678 return MATCH_ERROR;
7679
7680 for (id = 0; id < EXT_ATTR_LAST; id++)
7681 if (strcmp (name, ext_attr_list[id].name) == 0)
7682 break;
7683
7684 if (id == EXT_ATTR_LAST)
7685 {
7686 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
7687 return MATCH_ERROR;
7688 }
7689
de0c4488 7690 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
36b0a1b0 7691 == FAILURE)
7692 return MATCH_ERROR;
7693
7694 gfc_gobble_whitespace ();
7695 ch = gfc_next_ascii_char ();
7696 if (ch == ':')
7697 {
7698 /* This is the successful exit condition for the loop. */
7699 if (gfc_next_ascii_char () == ':')
7700 break;
7701 }
7702
7703 if (ch == ',')
7704 continue;
7705
7706 goto syntax;
7707 }
7708
7709 if (gfc_match_eos () == MATCH_YES)
7710 goto syntax;
7711
7712 for(;;)
7713 {
7714 m = gfc_match_name (name);
7715 if (m != MATCH_YES)
7716 return m;
7717
7718 if (find_special (name, &sym, true))
7719 return MATCH_ERROR;
7720
7721 sym->attr.ext_attr |= attr.ext_attr;
7722
7723 if (gfc_match_eos () == MATCH_YES)
7724 break;
7725
7726 if (gfc_match_char (',') != MATCH_YES)
7727 goto syntax;
7728 }
7729
7730 return MATCH_YES;
7731
7732syntax:
7733 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
7734 return MATCH_ERROR;
7735}