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