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