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