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