]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
PR ipa/61886
[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
cc78610f 4820 /* gfc_error_now used in following and return with MATCH_YES because
4821 doing otherwise results in a cascade of extraneous errors and in
4822 some cases an ICE in symbol.c(gfc_release_symbol). */
a23f57e2 4823 if (progname->attr.module_procedure && progname->attr.host_assoc)
4b8eb6ca 4824 {
a23f57e2 4825 bool arg_count_mismatch = false;
4826
4827 if (!formal && head)
4828 arg_count_mismatch = true;
4829
4830 /* Abbreviated module procedure declaration is not meant to have any
4831 formal arguments! */
cc78610f 4832 if (!progname->abr_modproc_decl && formal && !head)
a23f57e2 4833 arg_count_mismatch = true;
4834
4b8eb6ca 4835 for (p = formal, q = head; p && q; p = p->next, q = q->next)
4836 {
4837 if ((p->next != NULL && q->next == NULL)
4838 || (p->next == NULL && q->next != NULL))
a23f57e2 4839 arg_count_mismatch = true;
4b8eb6ca 4840 else if ((p->sym == NULL && q->sym == NULL)
4841 || strcmp (p->sym->name, q->sym->name) == 0)
4842 continue;
4843 else
4844 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4845 "argument names (%s/%s) at %C",
4846 p->sym->name, q->sym->name);
4847 }
a23f57e2 4848
4849 if (arg_count_mismatch)
4850 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4851 "formal arguments at %C");
4b8eb6ca 4852 }
4853
4ee9c684 4854 return MATCH_YES;
4855
4856cleanup:
4857 gfc_free_formal_arglist (head);
4858 return m;
4859}
4860
4861
4862/* Match a RESULT specification following a function declaration or
4863 ENTRY statement. Also matches the end-of-statement. */
4864
4865static match
f6d0e37a 4866match_result (gfc_symbol *function, gfc_symbol **result)
4ee9c684 4867{
4868 char name[GFC_MAX_SYMBOL_LEN + 1];
4869 gfc_symbol *r;
4870 match m;
4871
4872 if (gfc_match (" result (") != MATCH_YES)
4873 return MATCH_NO;
4874
4875 m = gfc_match_name (name);
4876 if (m != MATCH_YES)
4877 return m;
4878
c5d33754 4879 /* Get the right paren, and that's it because there could be the
4880 bind(c) attribute after the result clause. */
60e19868 4881 if (gfc_match_char (')') != MATCH_YES)
4ee9c684 4882 {
c5d33754 4883 /* TODO: should report the missing right paren here. */
4ee9c684 4884 return MATCH_ERROR;
4885 }
4886
4887 if (strcmp (function->name, name) == 0)
4888 {
1a9745d2 4889 gfc_error ("RESULT variable at %C must be different than function name");
4ee9c684 4890 return MATCH_ERROR;
4891 }
4892
4893 if (gfc_get_symbol (name, NULL, &r))
4894 return MATCH_ERROR;
4895
60e19868 4896 if (!gfc_add_result (&r->attr, r->name, NULL))
4ee9c684 4897 return MATCH_ERROR;
4898
4899 *result = r;
4900
4901 return MATCH_YES;
4902}
4903
4904
c5d33754 4905/* Match a function suffix, which could be a combination of a result
4906 clause and BIND(C), either one, or neither. The draft does not
4907 require them to come in a specific order. */
4908
4909match
4910gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4911{
4912 match is_bind_c; /* Found bind(c). */
4913 match is_result; /* Found result clause. */
4914 match found_match; /* Status of whether we've found a good match. */
e0be6f02 4915 char peek_char; /* Character we're going to peek at. */
75ae7f6c 4916 bool allow_binding_name;
c5d33754 4917
4918 /* Initialize to having found nothing. */
4919 found_match = MATCH_NO;
8db94b3b 4920 is_bind_c = MATCH_NO;
c5d33754 4921 is_result = MATCH_NO;
4922
4923 /* Get the next char to narrow between result and bind(c). */
4924 gfc_gobble_whitespace ();
e0be6f02 4925 peek_char = gfc_peek_ascii_char ();
c5d33754 4926
75ae7f6c 4927 /* C binding names are not allowed for internal procedures. */
4928 if (gfc_current_state () == COMP_CONTAINS
4929 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4930 allow_binding_name = false;
4931 else
4932 allow_binding_name = true;
4933
c5d33754 4934 switch (peek_char)
4935 {
4936 case 'r':
4937 /* Look for result clause. */
4938 is_result = match_result (sym, result);
4939 if (is_result == MATCH_YES)
4940 {
4941 /* Now see if there is a bind(c) after it. */
75ae7f6c 4942 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 4943 /* We've found the result clause and possibly bind(c). */
4944 found_match = MATCH_YES;
4945 }
4946 else
4947 /* This should only be MATCH_ERROR. */
8db94b3b 4948 found_match = is_result;
c5d33754 4949 break;
4950 case 'b':
4951 /* Look for bind(c) first. */
75ae7f6c 4952 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 4953 if (is_bind_c == MATCH_YES)
4954 {
4955 /* Now see if a result clause followed it. */
4956 is_result = match_result (sym, result);
4957 found_match = MATCH_YES;
4958 }
4959 else
4960 {
4961 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4962 found_match = MATCH_ERROR;
4963 }
4964 break;
4965 default:
4966 gfc_error ("Unexpected junk after function declaration at %C");
4967 found_match = MATCH_ERROR;
4968 break;
4969 }
4970
c5d33754 4971 if (is_bind_c == MATCH_YES)
4518e961 4972 {
75ae7f6c 4973 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4518e961 4974 if (gfc_current_state () == COMP_CONTAINS
75ae7f6c 4975 && sym->ns->proc_name->attr.flavor != FL_MODULE
60e19868 4976 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4977 "at %L may not be specified for an internal "
4978 "procedure", &gfc_current_locus))
75ae7f6c 4979 return MATCH_ERROR;
4980
60e19868 4981 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4518e961 4982 return MATCH_ERROR;
4983 }
8db94b3b 4984
c5d33754 4985 return found_match;
4986}
4987
4988
1e057e9b 4989/* Procedure pointer return value without RESULT statement:
4990 Add "hidden" result variable named "ppr@". */
4991
60e19868 4992static bool
1e057e9b 4993add_hidden_procptr_result (gfc_symbol *sym)
4994{
4995 bool case1,case2;
4996
4997 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
60e19868 4998 return false;
1e057e9b 4999
5000 /* First usage case: PROCEDURE and EXTERNAL statements. */
5001 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
5002 && strcmp (gfc_current_block ()->name, sym->name) == 0
5003 && sym->attr.external;
5004 /* Second usage case: INTERFACE statements. */
5005 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
5006 && gfc_state_stack->previous->state == COMP_FUNCTION
5007 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
5008
5009 if (case1 || case2)
5010 {
5011 gfc_symtree *stree;
5012 if (case1)
36b0a1b0 5013 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
1e057e9b 5014 else if (case2)
180a5dc0 5015 {
5016 gfc_symtree *st2;
36b0a1b0 5017 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
180a5dc0 5018 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
5019 st2->n.sym = stree->n.sym;
5020 }
1e057e9b 5021 sym->result = stree->n.sym;
5022
5023 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
5024 sym->result->attr.pointer = sym->attr.pointer;
5025 sym->result->attr.external = sym->attr.external;
5026 sym->result->attr.referenced = sym->attr.referenced;
e40ac2fe 5027 sym->result->ts = sym->ts;
1e057e9b 5028 sym->attr.proc_pointer = 0;
5029 sym->attr.pointer = 0;
5030 sym->attr.external = 0;
5031 if (sym->result->attr.external && sym->result->attr.pointer)
5032 {
5033 sym->result->attr.pointer = 0;
5034 sym->result->attr.proc_pointer = 1;
5035 }
5036
5037 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
5038 }
5039 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5040 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
5041 && sym->result && sym->result != sym && sym->result->attr.external
5042 && sym == gfc_current_ns->proc_name
5043 && sym == sym->result->ns->proc_name
5044 && strcmp ("ppr@", sym->result->name) == 0)
5045 {
5046 sym->result->attr.proc_pointer = 1;
5047 sym->attr.pointer = 0;
60e19868 5048 return true;
1e057e9b 5049 }
5050 else
60e19868 5051 return false;
1e057e9b 5052}
5053
5054
64e93293 5055/* Match the interface for a PROCEDURE declaration,
5056 including brackets (R1212). */
af1a34ee 5057
5058static match
64e93293 5059match_procedure_interface (gfc_symbol **proc_if)
af1a34ee 5060{
5061 match m;
1eb2b542 5062 gfc_symtree *st;
af1a34ee 5063 locus old_loc, entry_loc;
1eb2b542 5064 gfc_namespace *old_ns = gfc_current_ns;
5065 char name[GFC_MAX_SYMBOL_LEN + 1];
af1a34ee 5066
1eb2b542 5067 old_loc = entry_loc = gfc_current_locus;
af1a34ee 5068 gfc_clear_ts (&current_ts);
5069
5070 if (gfc_match (" (") != MATCH_YES)
5071 {
5072 gfc_current_locus = entry_loc;
5073 return MATCH_NO;
5074 }
5075
5076 /* Get the type spec. for the procedure interface. */
5077 old_loc = gfc_current_locus;
e8152f13 5078 m = gfc_match_decl_type_spec (&current_ts, 0);
fd1277c3 5079 gfc_gobble_whitespace ();
e0be6f02 5080 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
af1a34ee 5081 goto got_ts;
5082
5083 if (m == MATCH_ERROR)
5084 return m;
5085
1eb2b542 5086 /* Procedure interface is itself a procedure. */
af1a34ee 5087 gfc_current_locus = old_loc;
1eb2b542 5088 m = gfc_match_name (name);
af1a34ee 5089
1eb2b542 5090 /* First look to see if it is already accessible in the current
5091 namespace because it is use associated or contained. */
5092 st = NULL;
5093 if (gfc_find_sym_tree (name, NULL, 0, &st))
5094 return MATCH_ERROR;
5095
5096 /* If it is still not found, then try the parent namespace, if it
5097 exists and create the symbol there if it is still not found. */
5098 if (gfc_current_ns->parent)
5099 gfc_current_ns = gfc_current_ns->parent;
5100 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
5101 return MATCH_ERROR;
5102
5103 gfc_current_ns = old_ns;
5104 *proc_if = st->n.sym;
af1a34ee 5105
64e93293 5106 if (*proc_if)
af1a34ee 5107 {
64e93293 5108 (*proc_if)->refs++;
74113644 5109 /* Resolve interface if possible. That way, attr.procedure is only set
5110 if it is declared by a later procedure-declaration-stmt, which is
87863b31 5111 invalid per F08:C1216 (cf. resolve_procedure_interface). */
64e93293 5112 while ((*proc_if)->ts.interface)
5113 *proc_if = (*proc_if)->ts.interface;
74113644 5114
87863b31 5115 if ((*proc_if)->attr.flavor == FL_UNKNOWN
5116 && (*proc_if)->ts.type == BT_UNKNOWN
60e19868 5117 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
5118 (*proc_if)->name, NULL))
87863b31 5119 return MATCH_ERROR;
af1a34ee 5120 }
5121
5122got_ts:
af1a34ee 5123 if (gfc_match (" )") != MATCH_YES)
5124 {
5125 gfc_current_locus = entry_loc;
5126 return MATCH_NO;
5127 }
5128
64e93293 5129 return MATCH_YES;
5130}
5131
5132
5133/* Match a PROCEDURE declaration (R1211). */
5134
5135static match
5136match_procedure_decl (void)
5137{
5138 match m;
5139 gfc_symbol *sym, *proc_if = NULL;
5140 int num;
5141 gfc_expr *initializer = NULL;
5142
293d72e0 5143 /* Parse interface (with brackets). */
64e93293 5144 m = match_procedure_interface (&proc_if);
5145 if (m != MATCH_YES)
5146 return m;
5147
5148 /* Parse attributes (with colons). */
af1a34ee 5149 m = match_attr_spec();
5150 if (m == MATCH_ERROR)
5151 return MATCH_ERROR;
5152
caa3ea40 5153 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
5154 {
5155 current_attr.is_bind_c = 1;
5156 has_name_equals = 0;
5157 curr_binding_label = NULL;
5158 }
5159
af1a34ee 5160 /* Get procedure symbols. */
5161 for(num=1;;num++)
5162 {
af1a34ee 5163 m = gfc_match_symbol (&sym, 0);
5164 if (m == MATCH_NO)
5165 goto syntax;
5166 else if (m == MATCH_ERROR)
5167 return m;
5168
5169 /* Add current_attr to the symbol attributes. */
60e19868 5170 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
af1a34ee 5171 return MATCH_ERROR;
5172
5173 if (sym->attr.is_bind_c)
5174 {
5175 /* Check for C1218. */
5176 if (!proc_if || !proc_if->attr.is_bind_c)
5177 {
5178 gfc_error ("BIND(C) attribute at %C requires "
5179 "an interface with BIND(C)");
5180 return MATCH_ERROR;
5181 }
5182 /* Check for C1217. */
5183 if (has_name_equals && sym->attr.pointer)
5184 {
5185 gfc_error ("BIND(C) procedure with NAME may not have "
5186 "POINTER attribute at %C");
5187 return MATCH_ERROR;
5188 }
5189 if (has_name_equals && sym->attr.dummy)
5190 {
5191 gfc_error ("Dummy procedure at %C may not have "
5192 "BIND(C) attribute with NAME");
5193 return MATCH_ERROR;
5194 }
5195 /* Set binding label for BIND(C). */
60e19868 5196 if (!set_binding_label (&sym->binding_label, sym->name, num))
af1a34ee 5197 return MATCH_ERROR;
5198 }
5199
60e19868 5200 if (!gfc_add_external (&sym->attr, NULL))
af1a34ee 5201 return MATCH_ERROR;
1e057e9b 5202
60e19868 5203 if (add_hidden_procptr_result (sym))
1e057e9b 5204 sym = sym->result;
5205
60e19868 5206 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
af1a34ee 5207 return MATCH_ERROR;
5208
5209 /* Set interface. */
5210 if (proc_if != NULL)
f7d7a083 5211 {
13a834aa 5212 if (sym->ts.type != BT_UNKNOWN)
5213 {
716da296 5214 gfc_error ("Procedure %qs at %L already has basic type of %s",
13a834aa 5215 sym->name, &gfc_current_locus,
5216 gfc_basic_typename (sym->ts.type));
5217 return MATCH_ERROR;
5218 }
2cd2bb5c 5219 sym->ts.interface = proc_if;
f7d7a083 5220 sym->attr.untyped = 1;
180a5dc0 5221 sym->attr.if_source = IFSRC_IFBODY;
f7d7a083 5222 }
af1a34ee 5223 else if (current_ts.type != BT_UNKNOWN)
5224 {
60e19868 5225 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
13a834aa 5226 return MATCH_ERROR;
2cd2bb5c 5227 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5228 sym->ts.interface->ts = current_ts;
ce80bc5b 5229 sym->ts.interface->attr.flavor = FL_PROCEDURE;
2cd2bb5c 5230 sym->ts.interface->attr.function = 1;
ce80bc5b 5231 sym->attr.function = 1;
180a5dc0 5232 sym->attr.if_source = IFSRC_UNKNOWN;
af1a34ee 5233 }
5234
cad0ddcf 5235 if (gfc_match (" =>") == MATCH_YES)
5236 {
5237 if (!current_attr.pointer)
5238 {
5239 gfc_error ("Initialization at %C isn't for a pointer variable");
5240 m = MATCH_ERROR;
5241 goto cleanup;
5242 }
5243
23d075f4 5244 m = match_pointer_init (&initializer, 1);
cad0ddcf 5245 if (m != MATCH_YES)
5246 goto cleanup;
5247
60e19868 5248 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
cad0ddcf 5249 goto cleanup;
5250
5251 }
5252
af1a34ee 5253 if (gfc_match_eos () == MATCH_YES)
5254 return MATCH_YES;
5255 if (gfc_match_char (',') != MATCH_YES)
5256 goto syntax;
5257 }
5258
5259syntax:
5260 gfc_error ("Syntax error in PROCEDURE statement at %C");
5261 return MATCH_ERROR;
cad0ddcf 5262
5263cleanup:
5264 /* Free stuff up and return. */
5265 gfc_free_expr (initializer);
5266 return m;
af1a34ee 5267}
5268
5269
64e93293 5270static match
5271match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
5272
5273
5274/* Match a procedure pointer component declaration (R445). */
5275
5276static match
5277match_ppc_decl (void)
5278{
5279 match m;
5280 gfc_symbol *proc_if = NULL;
5281 gfc_typespec ts;
5282 int num;
5283 gfc_component *c;
5284 gfc_expr *initializer = NULL;
5285 gfc_typebound_proc* tb;
5286 char name[GFC_MAX_SYMBOL_LEN + 1];
5287
5288 /* Parse interface (with brackets). */
5289 m = match_procedure_interface (&proc_if);
5290 if (m != MATCH_YES)
5291 goto syntax;
5292
5293 /* Parse attributes. */
5294 tb = XCNEW (gfc_typebound_proc);
5295 tb->where = gfc_current_locus;
5296 m = match_binding_attributes (tb, false, true);
5297 if (m == MATCH_ERROR)
5298 return m;
5299
64e93293 5300 gfc_clear_attr (&current_attr);
5301 current_attr.procedure = 1;
5302 current_attr.proc_pointer = 1;
5303 current_attr.access = tb->access;
5304 current_attr.flavor = FL_PROCEDURE;
5305
5306 /* Match the colons (required). */
5307 if (gfc_match (" ::") != MATCH_YES)
5308 {
0d2b3c9c 5309 gfc_error ("Expected %<::%> after binding-attributes at %C");
64e93293 5310 return MATCH_ERROR;
5311 }
5312
5313 /* Check for C450. */
5314 if (!tb->nopass && proc_if == NULL)
5315 {
5316 gfc_error("NOPASS or explicit interface required at %C");
5317 return MATCH_ERROR;
5318 }
5319
60e19868 5320 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
8a026279 5321 return MATCH_ERROR;
5322
64e93293 5323 /* Match PPC names. */
5324 ts = current_ts;
5325 for(num=1;;num++)
5326 {
5327 m = gfc_match_name (name);
5328 if (m == MATCH_NO)
5329 goto syntax;
5330 else if (m == MATCH_ERROR)
5331 return m;
5332
60e19868 5333 if (!gfc_add_component (gfc_current_block(), name, &c))
64e93293 5334 return MATCH_ERROR;
5335
5336 /* Add current_attr to the symbol attributes. */
60e19868 5337 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
64e93293 5338 return MATCH_ERROR;
5339
60e19868 5340 if (!gfc_add_external (&c->attr, NULL))
64e93293 5341 return MATCH_ERROR;
5342
60e19868 5343 if (!gfc_add_proc (&c->attr, name, NULL))
64e93293 5344 return MATCH_ERROR;
5345
99014f81 5346 if (num == 1)
5347 c->tb = tb;
5348 else
5349 {
5350 c->tb = XCNEW (gfc_typebound_proc);
5351 c->tb->where = gfc_current_locus;
5352 *c->tb = *tb;
5353 }
fe9b08a2 5354
64e93293 5355 /* Set interface. */
5356 if (proc_if != NULL)
5357 {
5358 c->ts.interface = proc_if;
5359 c->attr.untyped = 1;
5360 c->attr.if_source = IFSRC_IFBODY;
5361 }
5362 else if (ts.type != BT_UNKNOWN)
5363 {
5364 c->ts = ts;
5365 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
a63bcd97 5366 c->ts.interface->result = c->ts.interface;
64e93293 5367 c->ts.interface->ts = ts;
ce80bc5b 5368 c->ts.interface->attr.flavor = FL_PROCEDURE;
64e93293 5369 c->ts.interface->attr.function = 1;
ce80bc5b 5370 c->attr.function = 1;
64e93293 5371 c->attr.if_source = IFSRC_UNKNOWN;
5372 }
5373
5374 if (gfc_match (" =>") == MATCH_YES)
5375 {
23d075f4 5376 m = match_pointer_init (&initializer, 1);
64e93293 5377 if (m != MATCH_YES)
5378 {
5379 gfc_free_expr (initializer);
5380 return m;
5381 }
5382 c->initializer = initializer;
5383 }
5384
5385 if (gfc_match_eos () == MATCH_YES)
5386 return MATCH_YES;
5387 if (gfc_match_char (',') != MATCH_YES)
5388 goto syntax;
5389 }
5390
5391syntax:
5392 gfc_error ("Syntax error in procedure pointer component at %C");
5393 return MATCH_ERROR;
5394}
5395
5396
af1a34ee 5397/* Match a PROCEDURE declaration inside an interface (R1206). */
5398
5399static match
5400match_procedure_in_interface (void)
5401{
5402 match m;
5403 gfc_symbol *sym;
5404 char name[GFC_MAX_SYMBOL_LEN + 1];
2b0f5dc8 5405 locus old_locus;
af1a34ee 5406
5407 if (current_interface.type == INTERFACE_NAMELESS
5408 || current_interface.type == INTERFACE_ABSTRACT)
5409 {
5410 gfc_error ("PROCEDURE at %C must be in a generic interface");
5411 return MATCH_ERROR;
5412 }
5413
2b0f5dc8 5414 /* Check if the F2008 optional double colon appears. */
5415 gfc_gobble_whitespace ();
5416 old_locus = gfc_current_locus;
5417 if (gfc_match ("::") == MATCH_YES)
5418 {
60e19868 5419 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5420 "MODULE PROCEDURE statement at %L", &old_locus))
2b0f5dc8 5421 return MATCH_ERROR;
5422 }
5423 else
5424 gfc_current_locus = old_locus;
5425
af1a34ee 5426 for(;;)
5427 {
5428 m = gfc_match_name (name);
5429 if (m == MATCH_NO)
5430 goto syntax;
5431 else if (m == MATCH_ERROR)
5432 return m;
5433 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5434 return MATCH_ERROR;
5435
60e19868 5436 if (!gfc_add_interface (sym))
af1a34ee 5437 return MATCH_ERROR;
5438
af1a34ee 5439 if (gfc_match_eos () == MATCH_YES)
5440 break;
5441 if (gfc_match_char (',') != MATCH_YES)
5442 goto syntax;
5443 }
5444
5445 return MATCH_YES;
5446
5447syntax:
5448 gfc_error ("Syntax error in PROCEDURE statement at %C");
5449 return MATCH_ERROR;
5450}
5451
5452
5453/* General matcher for PROCEDURE declarations. */
5454
7fd88f6e 5455static match match_procedure_in_type (void);
5456
af1a34ee 5457match
5458gfc_match_procedure (void)
5459{
5460 match m;
5461
5462 switch (gfc_current_state ())
5463 {
5464 case COMP_NONE:
5465 case COMP_PROGRAM:
5466 case COMP_MODULE:
4b8eb6ca 5467 case COMP_SUBMODULE:
af1a34ee 5468 case COMP_SUBROUTINE:
5469 case COMP_FUNCTION:
0b342e60 5470 case COMP_BLOCK:
af1a34ee 5471 m = match_procedure_decl ();
5472 break;
5473 case COMP_INTERFACE:
5474 m = match_procedure_in_interface ();
5475 break;
5476 case COMP_DERIVED:
64e93293 5477 m = match_ppc_decl ();
5478 break;
7fd88f6e 5479 case COMP_DERIVED_CONTAINS:
5480 m = match_procedure_in_type ();
5481 break;
af1a34ee 5482 default:
5483 return MATCH_NO;
5484 }
5485
5486 if (m != MATCH_YES)
5487 return m;
5488
60e19868 5489 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
af1a34ee 5490 return MATCH_ERROR;
5491
5492 return m;
5493}
5494
5495
a34926ba 5496/* Warn if a matched procedure has the same name as an intrinsic; this is
5497 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5498 parser-state-stack to find out whether we're in a module. */
5499
5500static void
8290d53f 5501do_warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
a34926ba 5502{
5503 bool in_module;
5504
5505 in_module = (gfc_state_stack->previous
4b8eb6ca 5506 && (gfc_state_stack->previous->state == COMP_MODULE
5507 || gfc_state_stack->previous->state == COMP_SUBMODULE));
a34926ba 5508
5509 gfc_warn_intrinsic_shadow (sym, in_module, func);
5510}
5511
5512
4ee9c684 5513/* Match a function declaration. */
5514
5515match
5516gfc_match_function_decl (void)
5517{
5518 char name[GFC_MAX_SYMBOL_LEN + 1];
5519 gfc_symbol *sym, *result;
5520 locus old_loc;
5521 match m;
c5d33754 5522 match suffix_match;
8db94b3b 5523 match found_match; /* Status returned by match func. */
4ee9c684 5524
5525 if (gfc_current_state () != COMP_NONE
5526 && gfc_current_state () != COMP_INTERFACE
5527 && gfc_current_state () != COMP_CONTAINS)
5528 return MATCH_NO;
5529
5530 gfc_clear_ts (&current_ts);
5531
cbb9e6aa 5532 old_loc = gfc_current_locus;
4ee9c684 5533
077932f9 5534 m = gfc_match_prefix (&current_ts);
4ee9c684 5535 if (m != MATCH_YES)
5536 {
cbb9e6aa 5537 gfc_current_locus = old_loc;
4ee9c684 5538 return m;
5539 }
5540
5541 if (gfc_match ("function% %n", name) != MATCH_YES)
5542 {
cbb9e6aa 5543 gfc_current_locus = old_loc;
4ee9c684 5544 return MATCH_NO;
5545 }
4b8eb6ca 5546
d77f260f 5547 if (get_proc_name (name, &sym, false))
4ee9c684 5548 return MATCH_ERROR;
1e057e9b 5549
60e19868 5550 if (add_hidden_procptr_result (sym))
1e057e9b 5551 sym = sym->result;
5552
4b8eb6ca 5553 if (current_attr.module_procedure)
5554 sym->attr.module_procedure = 1;
5555
4ee9c684 5556 gfc_new_block = sym;
5557
5558 m = gfc_match_formal_arglist (sym, 0, 0);
5559 if (m == MATCH_NO)
9b435b6d 5560 {
5561 gfc_error ("Expected formal argument list in function "
1a9745d2 5562 "definition at %C");
9b435b6d 5563 m = MATCH_ERROR;
5564 goto cleanup;
5565 }
4ee9c684 5566 else if (m == MATCH_ERROR)
5567 goto cleanup;
5568
5569 result = NULL;
5570
c5d33754 5571 /* According to the draft, the bind(c) and result clause can
5572 come in either order after the formal_arg_list (i.e., either
5573 can be first, both can exist together or by themselves or neither
5574 one). Therefore, the match_result can't match the end of the
5575 string, and check for the bind(c) or result clause in either order. */
5576 found_match = gfc_match_eos ();
5577
5578 /* Make sure that it isn't already declared as BIND(C). If it is, it
5579 must have been marked BIND(C) with a BIND(C) attribute and that is
5580 not allowed for procedures. */
5581 if (sym->attr.is_bind_c == 1)
5582 {
5583 sym->attr.is_bind_c = 0;
5584 if (sym->old_symbol != NULL)
5585 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5586 "variables or common blocks",
5587 &(sym->old_symbol->declared_at));
5588 else
5589 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5590 "variables or common blocks", &gfc_current_locus);
4ee9c684 5591 }
5592
c5d33754 5593 if (found_match != MATCH_YES)
4ee9c684 5594 {
c5d33754 5595 /* If we haven't found the end-of-statement, look for a suffix. */
5596 suffix_match = gfc_match_suffix (sym, &result);
5597 if (suffix_match == MATCH_YES)
5598 /* Need to get the eos now. */
5599 found_match = gfc_match_eos ();
5600 else
5601 found_match = suffix_match;
4ee9c684 5602 }
5603
c5d33754 5604 if(found_match != MATCH_YES)
5605 m = MATCH_ERROR;
4ee9c684 5606 else
5607 {
c5d33754 5608 /* Make changes to the symbol. */
5609 m = MATCH_ERROR;
8db94b3b 5610
60e19868 5611 if (!gfc_add_function (&sym->attr, sym->name, NULL))
c5d33754 5612 goto cleanup;
8db94b3b 5613
60e19868 5614 if (!gfc_missing_attr (&sym->attr, NULL)
5615 || !copy_prefix (&sym->attr, &sym->declared_at))
c5d33754 5616 goto cleanup;
4ee9c684 5617
8d39570e 5618 /* Delay matching the function characteristics until after the
077932f9 5619 specification block by signalling kind=-1. */
8d39570e 5620 sym->declared_at = old_loc;
5621 if (current_ts.type != BT_UNKNOWN)
5622 current_ts.kind = -1;
5623 else
5624 current_ts.kind = 0;
077932f9 5625
c5d33754 5626 if (result == NULL)
5627 {
0477d42d 5628 if (current_ts.type != BT_UNKNOWN
60e19868 5629 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
0477d42d 5630 goto cleanup;
c5d33754 5631 sym->result = sym;
5632 }
5633 else
5634 {
0477d42d 5635 if (current_ts.type != BT_UNKNOWN
60e19868 5636 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
0477d42d 5637 goto cleanup;
c5d33754 5638 sym->result = result;
5639 }
5640
a34926ba 5641 /* Warn if this procedure has the same name as an intrinsic. */
8290d53f 5642 do_warn_intrinsic_shadow (sym, true);
a34926ba 5643
c5d33754 5644 return MATCH_YES;
5645 }
4ee9c684 5646
5647cleanup:
cbb9e6aa 5648 gfc_current_locus = old_loc;
4ee9c684 5649 return m;
5650}
5651
1a9745d2 5652
5653/* This is mostly a copy of parse.c(add_global_procedure) but modified to
5654 pass the name of the entry, rather than the gfc_current_block name, and
5655 to return false upon finding an existing global entry. */
858f9894 5656
5657static bool
8d779aef 5658add_global_entry (const char *name, const char *binding_label, bool sub,
5659 locus *where)
858f9894 5660{
5661 gfc_gsymbol *s;
8458f4ca 5662 enum gfc_symbol_type type;
858f9894 5663
5b11d932 5664 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
858f9894 5665
da5c730d 5666 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5667 name is a global identifier. */
5668 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
858f9894 5669 {
da5c730d 5670 s = gfc_get_gsymbol (name);
5671
5672 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5673 {
8d779aef 5674 gfc_global_used (s, where);
da5c730d 5675 return false;
5676 }
5677 else
5678 {
5679 s->type = type;
c8b913ab 5680 s->sym_name = name;
8d779aef 5681 s->where = *where;
da5c730d 5682 s->defined = 1;
5683 s->ns = gfc_current_ns;
5684 }
858f9894 5685 }
da5c730d 5686
5687 /* Don't add the symbol multiple times. */
5688 if (binding_label
5689 && (!gfc_notification_std (GFC_STD_F2008)
5690 || strcmp (name, binding_label) != 0))
5691 {
5692 s = gfc_get_gsymbol (binding_label);
5693
5694 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5695 {
8d779aef 5696 gfc_global_used (s, where);
da5c730d 5697 return false;
5698 }
5699 else
5700 {
5701 s->type = type;
c8b913ab 5702 s->sym_name = name;
da5c730d 5703 s->binding_label = binding_label;
8d779aef 5704 s->where = *where;
da5c730d 5705 s->defined = 1;
5706 s->ns = gfc_current_ns;
5707 }
5708 }
5709
5710 return true;
858f9894 5711}
4ee9c684 5712
1a9745d2 5713
4ee9c684 5714/* Match an ENTRY statement. */
5715
5716match
5717gfc_match_entry (void)
5718{
1b716045 5719 gfc_symbol *proc;
5720 gfc_symbol *result;
5721 gfc_symbol *entry;
4ee9c684 5722 char name[GFC_MAX_SYMBOL_LEN + 1];
5723 gfc_compile_state state;
5724 match m;
1b716045 5725 gfc_entry_list *el;
7b5e1acc 5726 locus old_loc;
d77f260f 5727 bool module_procedure;
86f0974b 5728 char peek_char;
5729 match is_bind_c;
4ee9c684 5730
5731 m = gfc_match_name (name);
5732 if (m != MATCH_YES)
5733 return m;
5734
60e19868 5735 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
a3b81b0f 5736 return MATCH_ERROR;
5737
1b716045 5738 state = gfc_current_state ();
ea37f786 5739 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
1b716045 5740 {
ea37f786 5741 switch (state)
5742 {
5743 case COMP_PROGRAM:
5744 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5745 break;
5746 case COMP_MODULE:
5747 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5748 break;
4b8eb6ca 5749 case COMP_SUBMODULE:
5750 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5751 break;
ea37f786 5752 case COMP_BLOCK_DATA:
1a9745d2 5753 gfc_error ("ENTRY statement at %C cannot appear within "
5754 "a BLOCK DATA");
ea37f786 5755 break;
5756 case COMP_INTERFACE:
1a9745d2 5757 gfc_error ("ENTRY statement at %C cannot appear within "
5758 "an INTERFACE");
ea37f786 5759 break;
5760 case COMP_DERIVED:
1a9745d2 5761 gfc_error ("ENTRY statement at %C cannot appear within "
5762 "a DERIVED TYPE block");
ea37f786 5763 break;
5764 case COMP_IF:
1a9745d2 5765 gfc_error ("ENTRY statement at %C cannot appear within "
5766 "an IF-THEN block");
ea37f786 5767 break;
5768 case COMP_DO:
55ea8666 5769 case COMP_DO_CONCURRENT:
1a9745d2 5770 gfc_error ("ENTRY statement at %C cannot appear within "
5771 "a DO block");
ea37f786 5772 break;
5773 case COMP_SELECT:
1a9745d2 5774 gfc_error ("ENTRY statement at %C cannot appear within "
5775 "a SELECT block");
ea37f786 5776 break;
5777 case COMP_FORALL:
1a9745d2 5778 gfc_error ("ENTRY statement at %C cannot appear within "
5779 "a FORALL block");
ea37f786 5780 break;
5781 case COMP_WHERE:
1a9745d2 5782 gfc_error ("ENTRY statement at %C cannot appear within "
5783 "a WHERE block");
ea37f786 5784 break;
5785 case COMP_CONTAINS:
1a9745d2 5786 gfc_error ("ENTRY statement at %C cannot appear within "
5787 "a contained subprogram");
ea37f786 5788 break;
5789 default:
3132dbae 5790 gfc_error ("Unexpected ENTRY statement at %C");
ea37f786 5791 }
1b716045 5792 return MATCH_ERROR;
5793 }
5794
c286c294 5795 if ((state == COMP_SUBROUTINE || state == COMP_FUNCTION)
5796 && gfc_state_stack->previous->state == COMP_INTERFACE)
5797 {
5798 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
5799 return MATCH_ERROR;
5800 }
5801
d77f260f 5802 module_procedure = gfc_current_ns->parent != NULL
1a9745d2 5803 && gfc_current_ns->parent->proc_name
5804 && gfc_current_ns->parent->proc_name->attr.flavor
5805 == FL_MODULE;
d77f260f 5806
1b716045 5807 if (gfc_current_ns->parent != NULL
5808 && gfc_current_ns->parent->proc_name
d77f260f 5809 && !module_procedure)
1b716045 5810 {
5811 gfc_error("ENTRY statement at %C cannot appear in a "
5812 "contained procedure");
5813 return MATCH_ERROR;
5814 }
5815
d77f260f 5816 /* Module function entries need special care in get_proc_name
5817 because previous references within the function will have
5818 created symbols attached to the current namespace. */
5819 if (get_proc_name (name, &entry,
5820 gfc_current_ns->parent != NULL
c1e4695a 5821 && module_procedure))
4ee9c684 5822 return MATCH_ERROR;
5823
1b716045 5824 proc = gfc_current_block ();
5825
86f0974b 5826 /* Make sure that it isn't already declared as BIND(C). If it is, it
5827 must have been marked BIND(C) with a BIND(C) attribute and that is
5828 not allowed for procedures. */
5829 if (entry->attr.is_bind_c == 1)
5830 {
5831 entry->attr.is_bind_c = 0;
5832 if (entry->old_symbol != NULL)
5833 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5834 "variables or common blocks",
5835 &(entry->old_symbol->declared_at));
5836 else
5837 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5838 "variables or common blocks", &gfc_current_locus);
5839 }
8db94b3b 5840
86f0974b 5841 /* Check what next non-whitespace character is so we can tell if there
5842 is the required parens if we have a BIND(C). */
8d779aef 5843 old_loc = gfc_current_locus;
86f0974b 5844 gfc_gobble_whitespace ();
e0be6f02 5845 peek_char = gfc_peek_ascii_char ();
86f0974b 5846
1b716045 5847 if (state == COMP_SUBROUTINE)
4ee9c684 5848 {
4ee9c684 5849 m = gfc_match_formal_arglist (entry, 0, 1);
5850 if (m != MATCH_YES)
5851 return MATCH_ERROR;
5852
75ae7f6c 5853 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5854 never be an internal procedure. */
5855 is_bind_c = gfc_match_bind_c (entry, true);
86f0974b 5856 if (is_bind_c == MATCH_ERROR)
5857 return MATCH_ERROR;
5858 if (is_bind_c == MATCH_YES)
5859 {
5860 if (peek_char != '(')
5861 {
5862 gfc_error ("Missing required parentheses before BIND(C) at %C");
5863 return MATCH_ERROR;
5864 }
60e19868 5865 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5866 &(entry->declared_at), 1))
86f0974b 5867 return MATCH_ERROR;
5868 }
5869
da5c730d 5870 if (!gfc_current_ns->parent
8d779aef 5871 && !add_global_entry (name, entry->binding_label, true,
5872 &old_loc))
da5c730d 5873 return MATCH_ERROR;
5874
5875 /* An entry in a subroutine. */
60e19868 5876 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5877 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
4ee9c684 5878 return MATCH_ERROR;
1b716045 5879 }
5880 else
5881 {
7b5e1acc 5882 /* An entry in a function.
1a9745d2 5883 We need to take special care because writing
5884 ENTRY f()
5885 as
5886 ENTRY f
5887 is allowed, whereas
5888 ENTRY f() RESULT (r)
5889 can't be written as
5890 ENTRY f RESULT (r). */
7b5e1acc 5891 if (gfc_match_eos () == MATCH_YES)
5892 {
5893 gfc_current_locus = old_loc;
5894 /* Match the empty argument list, and add the interface to
5895 the symbol. */
5896 m = gfc_match_formal_arglist (entry, 0, 1);
5897 }
5898 else
5899 m = gfc_match_formal_arglist (entry, 0, 0);
5900
4ee9c684 5901 if (m != MATCH_YES)
5902 return MATCH_ERROR;
5903
4ee9c684 5904 result = NULL;
5905
5906 if (gfc_match_eos () == MATCH_YES)
5907 {
60e19868 5908 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5909 || !gfc_add_function (&entry->attr, entry->name, NULL))
4ee9c684 5910 return MATCH_ERROR;
5911
c6871095 5912 entry->result = entry;
4ee9c684 5913 }
5914 else
5915 {
86f0974b 5916 m = gfc_match_suffix (entry, &result);
4ee9c684 5917 if (m == MATCH_NO)
5918 gfc_syntax_error (ST_ENTRY);
5919 if (m != MATCH_YES)
5920 return MATCH_ERROR;
5921
86f0974b 5922 if (result)
5923 {
60e19868 5924 if (!gfc_add_result (&result->attr, result->name, NULL)
5925 || !gfc_add_entry (&entry->attr, result->name, NULL)
5926 || !gfc_add_function (&entry->attr, result->name, NULL))
86f0974b 5927 return MATCH_ERROR;
5928 entry->result = result;
5929 }
5930 else
5931 {
60e19868 5932 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5933 || !gfc_add_function (&entry->attr, entry->name, NULL))
86f0974b 5934 return MATCH_ERROR;
5935 entry->result = entry;
5936 }
4ee9c684 5937 }
da5c730d 5938
5939 if (!gfc_current_ns->parent
8d779aef 5940 && !add_global_entry (name, entry->binding_label, false,
5941 &old_loc))
da5c730d 5942 return MATCH_ERROR;
4ee9c684 5943 }
5944
5945 if (gfc_match_eos () != MATCH_YES)
5946 {
5947 gfc_syntax_error (ST_ENTRY);
5948 return MATCH_ERROR;
5949 }
5950
1b716045 5951 entry->attr.recursive = proc->attr.recursive;
5952 entry->attr.elemental = proc->attr.elemental;
5953 entry->attr.pure = proc->attr.pure;
4ee9c684 5954
1b716045 5955 el = gfc_get_entry_list ();
5956 el->sym = entry;
5957 el->next = gfc_current_ns->entries;
5958 gfc_current_ns->entries = el;
5959 if (el->next)
5960 el->id = el->next->id + 1;
5961 else
5962 el->id = 1;
4ee9c684 5963
1b716045 5964 new_st.op = EXEC_ENTRY;
5965 new_st.ext.entry = el;
5966
5967 return MATCH_YES;
4ee9c684 5968}
5969
5970
5971/* Match a subroutine statement, including optional prefixes. */
5972
5973match
5974gfc_match_subroutine (void)
5975{
5976 char name[GFC_MAX_SYMBOL_LEN + 1];
5977 gfc_symbol *sym;
5978 match m;
c5d33754 5979 match is_bind_c;
5980 char peek_char;
75ae7f6c 5981 bool allow_binding_name;
4ee9c684 5982
5983 if (gfc_current_state () != COMP_NONE
5984 && gfc_current_state () != COMP_INTERFACE
5985 && gfc_current_state () != COMP_CONTAINS)
5986 return MATCH_NO;
5987
077932f9 5988 m = gfc_match_prefix (NULL);
4ee9c684 5989 if (m != MATCH_YES)
5990 return m;
5991
5992 m = gfc_match ("subroutine% %n", name);
5993 if (m != MATCH_YES)
5994 return m;
5995
d77f260f 5996 if (get_proc_name (name, &sym, false))
4ee9c684 5997 return MATCH_ERROR;
1e057e9b 5998
22c1d301 5999 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
293d72e0 6000 the symbol existed before. */
22c1d301 6001 sym->declared_at = gfc_current_locus;
6002
4b8eb6ca 6003 if (current_attr.module_procedure)
6004 sym->attr.module_procedure = 1;
6005
60e19868 6006 if (add_hidden_procptr_result (sym))
1e057e9b 6007 sym = sym->result;
6008
4ee9c684 6009 gfc_new_block = sym;
6010
c5d33754 6011 /* Check what next non-whitespace character is so we can tell if there
86f0974b 6012 is the required parens if we have a BIND(C). */
c5d33754 6013 gfc_gobble_whitespace ();
e0be6f02 6014 peek_char = gfc_peek_ascii_char ();
8db94b3b 6015
60e19868 6016 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4ee9c684 6017 return MATCH_ERROR;
6018
6019 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
6020 return MATCH_ERROR;
6021
c5d33754 6022 /* Make sure that it isn't already declared as BIND(C). If it is, it
6023 must have been marked BIND(C) with a BIND(C) attribute and that is
6024 not allowed for procedures. */
6025 if (sym->attr.is_bind_c == 1)
6026 {
6027 sym->attr.is_bind_c = 0;
6028 if (sym->old_symbol != NULL)
6029 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6030 "variables or common blocks",
6031 &(sym->old_symbol->declared_at));
6032 else
6033 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6034 "variables or common blocks", &gfc_current_locus);
6035 }
75ae7f6c 6036
6037 /* C binding names are not allowed for internal procedures. */
6038 if (gfc_current_state () == COMP_CONTAINS
6039 && sym->ns->proc_name->attr.flavor != FL_MODULE)
6040 allow_binding_name = false;
6041 else
6042 allow_binding_name = true;
6043
c5d33754 6044 /* Here, we are just checking if it has the bind(c) attribute, and if
6045 so, then we need to make sure it's all correct. If it doesn't,
6046 we still need to continue matching the rest of the subroutine line. */
75ae7f6c 6047 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
c5d33754 6048 if (is_bind_c == MATCH_ERROR)
6049 {
6050 /* There was an attempt at the bind(c), but it was wrong. An
6051 error message should have been printed w/in the gfc_match_bind_c
6052 so here we'll just return the MATCH_ERROR. */
6053 return MATCH_ERROR;
6054 }
6055
6056 if (is_bind_c == MATCH_YES)
6057 {
75ae7f6c 6058 /* The following is allowed in the Fortran 2008 draft. */
4518e961 6059 if (gfc_current_state () == COMP_CONTAINS
75ae7f6c 6060 && sym->ns->proc_name->attr.flavor != FL_MODULE
60e19868 6061 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
6062 "at %L may not be specified for an internal "
6063 "procedure", &gfc_current_locus))
75ae7f6c 6064 return MATCH_ERROR;
6065
c5d33754 6066 if (peek_char != '(')
6067 {
6068 gfc_error ("Missing required parentheses before BIND(C) at %C");
6069 return MATCH_ERROR;
6070 }
60e19868 6071 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
6072 &(sym->declared_at), 1))
c5d33754 6073 return MATCH_ERROR;
6074 }
8db94b3b 6075
4ee9c684 6076 if (gfc_match_eos () != MATCH_YES)
6077 {
6078 gfc_syntax_error (ST_SUBROUTINE);
6079 return MATCH_ERROR;
6080 }
6081
60e19868 6082 if (!copy_prefix (&sym->attr, &sym->declared_at))
4ee9c684 6083 return MATCH_ERROR;
6084
a34926ba 6085 /* Warn if it has the same name as an intrinsic. */
8290d53f 6086 do_warn_intrinsic_shadow (sym, false);
a34926ba 6087
4ee9c684 6088 return MATCH_YES;
6089}
6090
6091
7136063b 6092/* Check that the NAME identifier in a BIND attribute or statement
6093 is conform to C identifier rules. */
6094
6095match
6096check_bind_name_identifier (char **name)
6097{
6098 char *n = *name, *p;
6099
6100 /* Remove leading spaces. */
6101 while (*n == ' ')
6102 n++;
6103
6104 /* On an empty string, free memory and set name to NULL. */
6105 if (*n == '\0')
6106 {
6107 free (*name);
6108 *name = NULL;
6109 return MATCH_YES;
6110 }
6111
6112 /* Remove trailing spaces. */
6113 p = n + strlen(n) - 1;
6114 while (*p == ' ')
6115 *(p--) = '\0';
6116
6117 /* Insert the identifier into the symbol table. */
6118 p = xstrdup (n);
6119 free (*name);
6120 *name = p;
6121
6122 /* Now check that identifier is valid under C rules. */
6123 if (ISDIGIT (*p))
6124 {
6125 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6126 return MATCH_ERROR;
6127 }
6128
6129 for (; *p; p++)
6130 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
6131 {
6132 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6133 return MATCH_ERROR;
6134 }
6135
6136 return MATCH_YES;
6137}
6138
6139
c5d33754 6140/* Match a BIND(C) specifier, with the optional 'name=' specifier if
6141 given, and set the binding label in either the given symbol (if not
a0527218 6142 NULL), or in the current_ts. The symbol may be NULL because we may
c5d33754 6143 encounter the BIND(C) before the declaration itself. Return
6144 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6145 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6146 or MATCH_YES if the specifier was correct and the binding label and
6147 bind(c) fields were set correctly for the given symbol or the
75ae7f6c 6148 current_ts. If allow_binding_name is false, no binding name may be
6149 given. */
c5d33754 6150
6151match
75ae7f6c 6152gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
c5d33754 6153{
7136063b 6154 char *binding_label = NULL;
6155 gfc_expr *e = NULL;
c5d33754 6156
8db94b3b 6157 /* Initialize the flag that specifies whether we encountered a NAME=
c5d33754 6158 specifier or not. */
6159 has_name_equals = 0;
6160
c5d33754 6161 /* This much we have to be able to match, in this order, if
6162 there is a bind(c) label. */
6163 if (gfc_match (" bind ( c ") != MATCH_YES)
6164 return MATCH_NO;
6165
6166 /* Now see if there is a binding label, or if we've reached the
6167 end of the bind(c) attribute without one. */
6168 if (gfc_match_char (',') == MATCH_YES)
6169 {
6170 if (gfc_match (" name = ") != MATCH_YES)
6171 {
6172 gfc_error ("Syntax error in NAME= specifier for binding label "
6173 "at %C");
6174 /* should give an error message here */
6175 return MATCH_ERROR;
6176 }
6177
6178 has_name_equals = 1;
6179
7136063b 6180 if (gfc_match_init_expr (&e) != MATCH_YES)
6181 {
6182 gfc_free_expr (e);
6183 return MATCH_ERROR;
6184 }
8db94b3b 6185
7136063b 6186 if (!gfc_simplify_expr(e, 0))
c5d33754 6187 {
7136063b 6188 gfc_error ("NAME= specifier at %C should be a constant expression");
6189 gfc_free_expr (e);
6190 return MATCH_ERROR;
c5d33754 6191 }
7136063b 6192
6193 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
6194 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
c5d33754 6195 {
7136063b 6196 gfc_error ("NAME= specifier at %C should be a scalar of "
6197 "default character kind");
6198 gfc_free_expr(e);
6199 return MATCH_ERROR;
c5d33754 6200 }
7136063b 6201
6202 // Get a C string from the Fortran string constant
6203 binding_label = gfc_widechar_to_char (e->value.character.string,
6204 e->value.character.length);
6205 gfc_free_expr(e);
6206
6207 // Check that it is valid (old gfc_match_name_C)
6208 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
6209 return MATCH_ERROR;
6210 }
c5d33754 6211
6212 /* Get the required right paren. */
6213 if (gfc_match_char (')') != MATCH_YES)
6214 {
6215 gfc_error ("Missing closing paren for binding label at %C");
6216 return MATCH_ERROR;
6217 }
6218
75ae7f6c 6219 if (has_name_equals && !allow_binding_name)
6220 {
6221 gfc_error ("No binding name is allowed in BIND(C) at %C");
6222 return MATCH_ERROR;
6223 }
6224
6225 if (has_name_equals && sym != NULL && sym->attr.dummy)
6226 {
6227 gfc_error ("For dummy procedure %s, no binding name is "
6228 "allowed in BIND(C) at %C", sym->name);
6229 return MATCH_ERROR;
6230 }
6231
6232
c5d33754 6233 /* Save the binding label to the symbol. If sym is null, we're
6234 probably matching the typespec attributes of a declaration and
6235 haven't gotten the name yet, and therefore, no symbol yet. */
7b2060ba 6236 if (binding_label)
c5d33754 6237 {
6238 if (sym != NULL)
7b2060ba 6239 sym->binding_label = binding_label;
c5d33754 6240 else
7b2060ba 6241 curr_binding_label = binding_label;
c5d33754 6242 }
75ae7f6c 6243 else if (allow_binding_name)
c5d33754 6244 {
6245 /* No binding label, but if symbol isn't null, we
75ae7f6c 6246 can set the label for it here.
6247 If name="" or allow_binding_name is false, no C binding name is
293d72e0 6248 created. */
c5d33754 6249 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
7b2060ba 6250 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
c5d33754 6251 }
94fa7146 6252
5cf92482 6253 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
6254 && current_interface.type == INTERFACE_ABSTRACT)
94fa7146 6255 {
6256 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6257 return MATCH_ERROR;
6258 }
6259
c5d33754 6260 return MATCH_YES;
6261}
6262
6263
231e961a 6264/* Return nonzero if we're currently compiling a contained procedure. */
c0985832 6265
6266static int
6267contained_procedure (void)
6268{
1c343287 6269 gfc_state_data *s = gfc_state_stack;
c0985832 6270
1c343287 6271 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
6272 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
6273 return 1;
c0985832 6274
6275 return 0;
6276}
6277
e14bee04 6278/* Set the kind of each enumerator. The kind is selected such that it is
3b6a4b41 6279 interoperable with the corresponding C enumeration type, making
6280 sure that -fshort-enums is honored. */
6281
6282static void
6283set_enum_kind(void)
6284{
6285 enumerator_history *current_history = NULL;
6286 int kind;
6287 int i;
6288
6289 if (max_enum == NULL || enum_history == NULL)
6290 return;
6291
6f092e31 6292 if (!flag_short_enums)
e14bee04 6293 return;
6294
3b6a4b41 6295 i = 0;
6296 do
6297 {
6298 kind = gfc_integer_kinds[i++].kind;
6299 }
e14bee04 6300 while (kind < gfc_c_int_kind
3b6a4b41 6301 && gfc_check_integer_range (max_enum->initializer->value.integer,
6302 kind) != ARITH_OK);
6303
6304 current_history = enum_history;
6305 while (current_history != NULL)
6306 {
6307 current_history->sym->ts.kind = kind;
6308 current_history = current_history->next;
6309 }
6310}
6311
1a9745d2 6312
4ee9c684 6313/* Match any of the various end-block statements. Returns the type of
6a7084d7 6314 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6315 and END BLOCK statements cannot be replaced by a single END statement. */
4ee9c684 6316
6317match
1a9745d2 6318gfc_match_end (gfc_statement *st)
4ee9c684 6319{
6320 char name[GFC_MAX_SYMBOL_LEN + 1];
6321 gfc_compile_state state;
6322 locus old_loc;
6323 const char *block_name;
6324 const char *target;
c0985832 6325 int eos_ok;
4ee9c684 6326 match m;
9e5e87d9 6327 gfc_namespace *parent_ns, *ns, *prev_ns;
6328 gfc_namespace **nsp;
4b8eb6ca 6329 bool abreviated_modproc_decl;
4ee9c684 6330
cbb9e6aa 6331 old_loc = gfc_current_locus;
4ee9c684 6332 if (gfc_match ("end") != MATCH_YES)
6333 return MATCH_NO;
6334
6335 state = gfc_current_state ();
1a9745d2 6336 block_name = gfc_current_block () == NULL
6337 ? NULL : gfc_current_block ()->name;
4ee9c684 6338
d18a512a 6339 switch (state)
4ee9c684 6340 {
d18a512a 6341 case COMP_ASSOCIATE:
6342 case COMP_BLOCK:
5ebb0bc6 6343 if (!strncmp (block_name, "block@", strlen("block@")))
d18a512a 6344 block_name = NULL;
6345 break;
6346
6347 case COMP_CONTAINS:
6348 case COMP_DERIVED_CONTAINS:
4ee9c684 6349 state = gfc_state_stack->previous->state;
1a9745d2 6350 block_name = gfc_state_stack->previous->sym == NULL
6351 ? NULL : gfc_state_stack->previous->sym->name;
d18a512a 6352 break;
6353
6354 default:
6355 break;
4ee9c684 6356 }
6357
4b8eb6ca 6358 abreviated_modproc_decl
6359 = gfc_current_block ()
6360 && gfc_current_block ()->abr_modproc_decl;
6361
4ee9c684 6362 switch (state)
6363 {
6364 case COMP_NONE:
6365 case COMP_PROGRAM:
6366 *st = ST_END_PROGRAM;
6367 target = " program";
c0985832 6368 eos_ok = 1;
4ee9c684 6369 break;
6370
6371 case COMP_SUBROUTINE:
6372 *st = ST_END_SUBROUTINE;
4b8eb6ca 6373 if (!abreviated_modproc_decl)
4ee9c684 6374 target = " subroutine";
4b8eb6ca 6375 else
6376 target = " procedure";
c0985832 6377 eos_ok = !contained_procedure ();
4ee9c684 6378 break;
6379
6380 case COMP_FUNCTION:
6381 *st = ST_END_FUNCTION;
4b8eb6ca 6382 if (!abreviated_modproc_decl)
4ee9c684 6383 target = " function";
4b8eb6ca 6384 else
6385 target = " procedure";
c0985832 6386 eos_ok = !contained_procedure ();
4ee9c684 6387 break;
6388
6389 case COMP_BLOCK_DATA:
6390 *st = ST_END_BLOCK_DATA;
6391 target = " block data";
c0985832 6392 eos_ok = 1;
4ee9c684 6393 break;
6394
6395 case COMP_MODULE:
6396 *st = ST_END_MODULE;
6397 target = " module";
c0985832 6398 eos_ok = 1;
4ee9c684 6399 break;
6400
4b8eb6ca 6401 case COMP_SUBMODULE:
6402 *st = ST_END_SUBMODULE;
6403 target = " submodule";
6404 eos_ok = 1;
6405 break;
6406
4ee9c684 6407 case COMP_INTERFACE:
6408 *st = ST_END_INTERFACE;
6409 target = " interface";
c0985832 6410 eos_ok = 0;
4ee9c684 6411 break;
6412
6413 case COMP_DERIVED:
7fd88f6e 6414 case COMP_DERIVED_CONTAINS:
4ee9c684 6415 *st = ST_END_TYPE;
6416 target = " type";
c0985832 6417 eos_ok = 0;
4ee9c684 6418 break;
6419
d18a512a 6420 case COMP_ASSOCIATE:
6421 *st = ST_END_ASSOCIATE;
6422 target = " associate";
6423 eos_ok = 0;
6424 break;
6425
6a7084d7 6426 case COMP_BLOCK:
6427 *st = ST_END_BLOCK;
6428 target = " block";
6429 eos_ok = 0;
6430 break;
6431
4ee9c684 6432 case COMP_IF:
6433 *st = ST_ENDIF;
6434 target = " if";
c0985832 6435 eos_ok = 0;
4ee9c684 6436 break;
6437
6438 case COMP_DO:
55ea8666 6439 case COMP_DO_CONCURRENT:
4ee9c684 6440 *st = ST_ENDDO;
6441 target = " do";
c0985832 6442 eos_ok = 0;
4ee9c684 6443 break;
6444
c6cd3066 6445 case COMP_CRITICAL:
6446 *st = ST_END_CRITICAL;
6447 target = " critical";
6448 eos_ok = 0;
6449 break;
6450
4ee9c684 6451 case COMP_SELECT:
1de1b1a9 6452 case COMP_SELECT_TYPE:
4ee9c684 6453 *st = ST_END_SELECT;
6454 target = " select";
c0985832 6455 eos_ok = 0;
4ee9c684 6456 break;
6457
6458 case COMP_FORALL:
6459 *st = ST_END_FORALL;
6460 target = " forall";
c0985832 6461 eos_ok = 0;
4ee9c684 6462 break;
6463
6464 case COMP_WHERE:
6465 *st = ST_END_WHERE;
6466 target = " where";
c0985832 6467 eos_ok = 0;
4ee9c684 6468 break;
6469
3b6a4b41 6470 case COMP_ENUM:
6471 *st = ST_END_ENUM;
6472 target = " enum";
6473 eos_ok = 0;
6474 last_initializer = NULL;
6475 set_enum_kind ();
6476 gfc_free_enum_history ();
6477 break;
6478
4ee9c684 6479 default:
6480 gfc_error ("Unexpected END statement at %C");
6481 goto cleanup;
6482 }
6483
8d779aef 6484 old_loc = gfc_current_locus;
4ee9c684 6485 if (gfc_match_eos () == MATCH_YES)
6486 {
4b20e9cf 6487 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6488 {
60e19868 6489 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6490 "instead of %s statement at %L",
4b8eb6ca 6491 abreviated_modproc_decl ? "END PROCEDURE"
6492 : gfc_ascii_statement(*st), &old_loc))
4b20e9cf 6493 goto cleanup;
6494 }
6495 else if (!eos_ok)
4ee9c684 6496 {
f6d0e37a 6497 /* We would have required END [something]. */
d197c9ee 6498 gfc_error ("%s statement expected at %L",
6499 gfc_ascii_statement (*st), &old_loc);
4ee9c684 6500 goto cleanup;
6501 }
6502
6503 return MATCH_YES;
6504 }
6505
6506 /* Verify that we've got the sort of end-block that we're expecting. */
6507 if (gfc_match (target) != MATCH_YES)
6508 {
4b8eb6ca 6509 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6510 ? "END PROCEDURE" : gfc_ascii_statement(*st), &old_loc);
4ee9c684 6511 goto cleanup;
6512 }
6513
8d779aef 6514 old_loc = gfc_current_locus;
4ee9c684 6515 /* If we're at the end, make sure a block name wasn't required. */
6516 if (gfc_match_eos () == MATCH_YES)
6517 {
6518
0d0ce415 6519 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
c6cd3066 6520 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
d18a512a 6521 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
4ee9c684 6522 return MATCH_YES;
6523
6a7084d7 6524 if (!block_name)
4ee9c684 6525 return MATCH_YES;
6526
716da296 6527 gfc_error ("Expected block name of %qs in %s statement at %L",
8d779aef 6528 block_name, gfc_ascii_statement (*st), &old_loc);
4ee9c684 6529
6530 return MATCH_ERROR;
6531 }
6532
6533 /* END INTERFACE has a special handler for its several possible endings. */
6534 if (*st == ST_END_INTERFACE)
6535 return gfc_match_end_interface ();
6536
f6d0e37a 6537 /* We haven't hit the end of statement, so what is left must be an
6538 end-name. */
4ee9c684 6539 m = gfc_match_space ();
6540 if (m == MATCH_YES)
6541 m = gfc_match_name (name);
6542
6543 if (m == MATCH_NO)
6544 gfc_error ("Expected terminating name at %C");
6545 if (m != MATCH_YES)
6546 goto cleanup;
6547
6548 if (block_name == NULL)
6549 goto syntax;
6550
df8f279f 6551 /* We have to pick out the declared submodule name from the composite
6552 required by F2008:11.2.3 para 2, which ends in the declared name. */
6553 if (state == COMP_SUBMODULE)
6554 block_name = strchr (block_name, '.') + 1;
6555
1e057e9b 6556 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
4ee9c684 6557 {
716da296 6558 gfc_error ("Expected label %qs for %s statement at %C", block_name,
4ee9c684 6559 gfc_ascii_statement (*st));
6560 goto cleanup;
6561 }
1e057e9b 6562 /* Procedure pointer as function result. */
6563 else if (strcmp (block_name, "ppr@") == 0
6564 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6565 {
716da296 6566 gfc_error ("Expected label %qs for %s statement at %C",
1e057e9b 6567 gfc_current_block ()->ns->proc_name->name,
6568 gfc_ascii_statement (*st));
6569 goto cleanup;
6570 }
4ee9c684 6571
6572 if (gfc_match_eos () == MATCH_YES)
6573 return MATCH_YES;
6574
6575syntax:
6576 gfc_syntax_error (*st);
6577
6578cleanup:
cbb9e6aa 6579 gfc_current_locus = old_loc;
9e5e87d9 6580
6581 /* If we are missing an END BLOCK, we created a half-ready namespace.
6582 Remove it from the parent namespace's sibling list. */
6583
8097c1a6 6584 while (state == COMP_BLOCK)
9e5e87d9 6585 {
6586 parent_ns = gfc_current_ns->parent;
6587
6588 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6589
6590 prev_ns = NULL;
6591 ns = *nsp;
6592 while (ns)
6593 {
6594 if (ns == gfc_current_ns)
6595 {
6596 if (prev_ns == NULL)
6597 *nsp = NULL;
6598 else
6599 prev_ns->sibling = ns->sibling;
6600 }
6601 prev_ns = ns;
6602 ns = ns->sibling;
6603 }
6604
6605 gfc_free_namespace (gfc_current_ns);
6606 gfc_current_ns = parent_ns;
8097c1a6 6607 gfc_state_stack = gfc_state_stack->previous;
6608 state = gfc_current_state ();
9e5e87d9 6609 }
6610
4ee9c684 6611 return MATCH_ERROR;
6612}
6613
6614
6615
6616/***************** Attribute declaration statements ****************/
6617
6618/* Set the attribute of a single variable. */
6619
6620static match
6621attr_decl1 (void)
6622{
6623 char name[GFC_MAX_SYMBOL_LEN + 1];
6624 gfc_array_spec *as;
75e5ecf3 6625
6626 /* Workaround -Wmaybe-uninitialized false positive during
6627 profiledbootstrap by initializing them. */
6628 gfc_symbol *sym = NULL;
4ee9c684 6629 locus var_locus;
6630 match m;
6631
6632 as = NULL;
6633
6634 m = gfc_match_name (name);
6635 if (m != MATCH_YES)
6636 goto cleanup;
6637
36b0a1b0 6638 if (find_special (name, &sym, false))
4ee9c684 6639 return MATCH_ERROR;
6640
60e19868 6641 if (!check_function_name (name))
f3e89339 6642 {
6643 m = MATCH_ERROR;
6644 goto cleanup;
6645 }
8db94b3b 6646
cbb9e6aa 6647 var_locus = gfc_current_locus;
4ee9c684 6648
6649 /* Deal with possible array specification for certain attributes. */
6650 if (current_attr.dimension
aff518b0 6651 || current_attr.codimension
4ee9c684 6652 || current_attr.allocatable
6653 || current_attr.pointer
6654 || current_attr.target)
6655 {
aff518b0 6656 m = gfc_match_array_spec (&as, !current_attr.codimension,
6657 !current_attr.dimension
6658 && !current_attr.pointer
6659 && !current_attr.target);
4ee9c684 6660 if (m == MATCH_ERROR)
6661 goto cleanup;
6662
6663 if (current_attr.dimension && m == MATCH_NO)
6664 {
1a9745d2 6665 gfc_error ("Missing array specification at %L in DIMENSION "
6666 "statement", &var_locus);
4ee9c684 6667 m = MATCH_ERROR;
6668 goto cleanup;
6669 }
6670
13aebeb0 6671 if (current_attr.dimension && sym->value)
6672 {
6673 gfc_error ("Dimensions specified for %s at %L after its "
6674 "initialisation", sym->name, &var_locus);
6675 m = MATCH_ERROR;
6676 goto cleanup;
6677 }
6678
aff518b0 6679 if (current_attr.codimension && m == MATCH_NO)
6680 {
6681 gfc_error ("Missing array specification at %L in CODIMENSION "
6682 "statement", &var_locus);
6683 m = MATCH_ERROR;
6684 goto cleanup;
6685 }
6686
4ee9c684 6687 if ((current_attr.allocatable || current_attr.pointer)
6688 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6689 {
1a9745d2 6690 gfc_error ("Array specification must be deferred at %L", &var_locus);
4ee9c684 6691 m = MATCH_ERROR;
6692 goto cleanup;
6693 }
6694 }
6695
b3704193 6696 /* Update symbol table. DIMENSION attribute is set in
6697 gfc_set_array_spec(). For CLASS variables, this must be applied
607ae689 6698 to the first component, or '_data' field. */
a33fbb6f 6699 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
4ee9c684 6700 {
60e19868 6701 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
b3704193 6702 {
6703 m = MATCH_ERROR;
6704 goto cleanup;
6705 }
b3704193 6706 }
6707 else
6708 {
aff518b0 6709 if (current_attr.dimension == 0 && current_attr.codimension == 0
60e19868 6710 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
b3704193 6711 {
6712 m = MATCH_ERROR;
6713 goto cleanup;
6714 }
4ee9c684 6715 }
8db94b3b 6716
fa102e56 6717 if (sym->ts.type == BT_CLASS
e8393d49 6718 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
29dae2bf 6719 {
6720 m = MATCH_ERROR;
6721 goto cleanup;
6722 }
4ee9c684 6723
60e19868 6724 if (!gfc_set_array_spec (sym, as, &var_locus))
4ee9c684 6725 {
6726 m = MATCH_ERROR;
6727 goto cleanup;
6728 }
e14bee04 6729
b549d2a5 6730 if (sym->attr.cray_pointee && sym->as != NULL)
6731 {
6732 /* Fix the array spec. */
8db94b3b 6733 m = gfc_mod_pointee_as (sym->as);
b549d2a5 6734 if (m == MATCH_ERROR)
6735 goto cleanup;
6736 }
4ee9c684 6737
60e19868 6738 if (!gfc_add_attribute (&sym->attr, &var_locus))
14efb9b7 6739 {
6740 m = MATCH_ERROR;
6741 goto cleanup;
6742 }
6743
4ee9c684 6744 if ((current_attr.external || current_attr.intrinsic)
6745 && sym->attr.flavor != FL_PROCEDURE
60e19868 6746 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
4ee9c684 6747 {
6748 m = MATCH_ERROR;
6749 goto cleanup;
6750 }
6751
1e057e9b 6752 add_hidden_procptr_result (sym);
6753
4ee9c684 6754 return MATCH_YES;
6755
6756cleanup:
6757 gfc_free_array_spec (as);
6758 return m;
6759}
6760
6761
6762/* Generic attribute declaration subroutine. Used for attributes that
6763 just have a list of names. */
6764
6765static match
6766attr_decl (void)
6767{
6768 match m;
6769
6770 /* Gobble the optional double colon, by simply ignoring the result
6771 of gfc_match(). */
6772 gfc_match (" ::");
6773
6774 for (;;)
6775 {
6776 m = attr_decl1 ();
6777 if (m != MATCH_YES)
6778 break;
6779
6780 if (gfc_match_eos () == MATCH_YES)
6781 {
6782 m = MATCH_YES;
6783 break;
6784 }
6785
6786 if (gfc_match_char (',') != MATCH_YES)
6787 {
6788 gfc_error ("Unexpected character in variable list at %C");
6789 m = MATCH_ERROR;
6790 break;
6791 }
6792 }
6793
6794 return m;
6795}
6796
6797
b549d2a5 6798/* This routine matches Cray Pointer declarations of the form:
6799 pointer ( <pointer>, <pointee> )
6800 or
e14bee04 6801 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6802 The pointer, if already declared, should be an integer. Otherwise, we
b549d2a5 6803 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6804 be either a scalar, or an array declaration. No space is allocated for
e14bee04 6805 the pointee. For the statement
b549d2a5 6806 pointer (ipt, ar(10))
6807 any subsequent uses of ar will be translated (in C-notation) as
e14bee04 6808 ar(i) => ((<type> *) ipt)(i)
b7bf3f81 6809 After gimplification, pointee variable will disappear in the code. */
b549d2a5 6810
6811static match
6812cray_pointer_decl (void)
6813{
6814 match m;
aff518b0 6815 gfc_array_spec *as = NULL;
b549d2a5 6816 gfc_symbol *cptr; /* Pointer symbol. */
6817 gfc_symbol *cpte; /* Pointee symbol. */
6818 locus var_locus;
6819 bool done = false;
6820
6821 while (!done)
6822 {
6823 if (gfc_match_char ('(') != MATCH_YES)
6824 {
0d2b3c9c 6825 gfc_error ("Expected %<(%> at %C");
e14bee04 6826 return MATCH_ERROR;
b549d2a5 6827 }
e14bee04 6828
b549d2a5 6829 /* Match pointer. */
6830 var_locus = gfc_current_locus;
6831 gfc_clear_attr (&current_attr);
6832 gfc_add_cray_pointer (&current_attr, &var_locus);
6833 current_ts.type = BT_INTEGER;
6834 current_ts.kind = gfc_index_integer_kind;
6835
e14bee04 6836 m = gfc_match_symbol (&cptr, 0);
b549d2a5 6837 if (m != MATCH_YES)
6838 {
6839 gfc_error ("Expected variable name at %C");
6840 return m;
6841 }
e14bee04 6842
60e19868 6843 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
b549d2a5 6844 return MATCH_ERROR;
6845
e14bee04 6846 gfc_set_sym_referenced (cptr);
b549d2a5 6847
6848 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6849 {
6850 cptr->ts.type = BT_INTEGER;
e14bee04 6851 cptr->ts.kind = gfc_index_integer_kind;
b549d2a5 6852 }
6853 else if (cptr->ts.type != BT_INTEGER)
6854 {
7698a624 6855 gfc_error ("Cray pointer at %C must be an integer");
b549d2a5 6856 return MATCH_ERROR;
6857 }
6858 else if (cptr->ts.kind < gfc_index_integer_kind)
6f521718 6859 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
7698a624 6860 " memory addresses require %d bytes",
1a9745d2 6861 cptr->ts.kind, gfc_index_integer_kind);
b549d2a5 6862
6863 if (gfc_match_char (',') != MATCH_YES)
6864 {
6865 gfc_error ("Expected \",\" at %C");
e14bee04 6866 return MATCH_ERROR;
b549d2a5 6867 }
6868
e14bee04 6869 /* Match Pointee. */
b549d2a5 6870 var_locus = gfc_current_locus;
6871 gfc_clear_attr (&current_attr);
6872 gfc_add_cray_pointee (&current_attr, &var_locus);
6873 current_ts.type = BT_UNKNOWN;
6874 current_ts.kind = 0;
6875
6876 m = gfc_match_symbol (&cpte, 0);
6877 if (m != MATCH_YES)
6878 {
6879 gfc_error ("Expected variable name at %C");
6880 return m;
6881 }
e14bee04 6882
b549d2a5 6883 /* Check for an optional array spec. */
aff518b0 6884 m = gfc_match_array_spec (&as, true, false);
b549d2a5 6885 if (m == MATCH_ERROR)
6886 {
6887 gfc_free_array_spec (as);
6888 return m;
6889 }
6890 else if (m == MATCH_NO)
6891 {
6892 gfc_free_array_spec (as);
6893 as = NULL;
8db94b3b 6894 }
b549d2a5 6895
60e19868 6896 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
b549d2a5 6897 return MATCH_ERROR;
6898
6899 gfc_set_sym_referenced (cpte);
6900
6901 if (cpte->as == NULL)
6902 {
60e19868 6903 if (!gfc_set_array_spec (cpte, as, &var_locus))
b549d2a5 6904 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6905 }
6906 else if (as != NULL)
6907 {
7698a624 6908 gfc_error ("Duplicate array spec for Cray pointee at %C");
b549d2a5 6909 gfc_free_array_spec (as);
6910 return MATCH_ERROR;
6911 }
8db94b3b 6912
b549d2a5 6913 as = NULL;
8db94b3b 6914
b549d2a5 6915 if (cpte->as != NULL)
6916 {
6917 /* Fix array spec. */
6918 m = gfc_mod_pointee_as (cpte->as);
6919 if (m == MATCH_ERROR)
6920 return m;
8db94b3b 6921 }
6922
b549d2a5 6923 /* Point the Pointee at the Pointer. */
b7bf3f81 6924 cpte->cp_pointer = cptr;
b549d2a5 6925
6926 if (gfc_match_char (')') != MATCH_YES)
6927 {
6928 gfc_error ("Expected \")\" at %C");
8db94b3b 6929 return MATCH_ERROR;
b549d2a5 6930 }
6931 m = gfc_match_char (',');
6932 if (m != MATCH_YES)
6933 done = true; /* Stop searching for more declarations. */
6934
6935 }
8db94b3b 6936
b549d2a5 6937 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6938 || gfc_match_eos () != MATCH_YES)
6939 {
0d2b3c9c 6940 gfc_error ("Expected %<,%> or end of statement at %C");
b549d2a5 6941 return MATCH_ERROR;
6942 }
6943 return MATCH_YES;
6944}
6945
6946
4ee9c684 6947match
6948gfc_match_external (void)
6949{
6950
6951 gfc_clear_attr (&current_attr);
14efb9b7 6952 current_attr.external = 1;
4ee9c684 6953
6954 return attr_decl ();
6955}
6956
6957
4ee9c684 6958match
6959gfc_match_intent (void)
6960{
6961 sym_intent intent;
6962
6a7084d7 6963 /* This is not allowed within a BLOCK construct! */
6964 if (gfc_current_state () == COMP_BLOCK)
6965 {
6966 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6967 return MATCH_ERROR;
6968 }
6969
4ee9c684 6970 intent = match_intent_spec ();
6971 if (intent == INTENT_UNKNOWN)
6972 return MATCH_ERROR;
6973
6974 gfc_clear_attr (&current_attr);
14efb9b7 6975 current_attr.intent = intent;
4ee9c684 6976
6977 return attr_decl ();
6978}
6979
6980
6981match
6982gfc_match_intrinsic (void)
6983{
6984
6985 gfc_clear_attr (&current_attr);
14efb9b7 6986 current_attr.intrinsic = 1;
4ee9c684 6987
6988 return attr_decl ();
6989}
6990
6991
6992match
6993gfc_match_optional (void)
6994{
6a7084d7 6995 /* This is not allowed within a BLOCK construct! */
6996 if (gfc_current_state () == COMP_BLOCK)
6997 {
6998 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6999 return MATCH_ERROR;
7000 }
4ee9c684 7001
7002 gfc_clear_attr (&current_attr);
14efb9b7 7003 current_attr.optional = 1;
4ee9c684 7004
7005 return attr_decl ();
7006}
7007
7008
7009match
7010gfc_match_pointer (void)
7011{
b549d2a5 7012 gfc_gobble_whitespace ();
e0be6f02 7013 if (gfc_peek_ascii_char () == '(')
b549d2a5 7014 {
829d7a08 7015 if (!flag_cray_pointer)
b549d2a5 7016 {
1a9745d2 7017 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7018 "flag");
b549d2a5 7019 return MATCH_ERROR;
7020 }
7021 return cray_pointer_decl ();
7022 }
7023 else
7024 {
7025 gfc_clear_attr (&current_attr);
14efb9b7 7026 current_attr.pointer = 1;
8db94b3b 7027
b549d2a5 7028 return attr_decl ();
7029 }
4ee9c684 7030}
7031
7032
7033match
7034gfc_match_allocatable (void)
7035{
4ee9c684 7036 gfc_clear_attr (&current_attr);
14efb9b7 7037 current_attr.allocatable = 1;
4ee9c684 7038
7039 return attr_decl ();
7040}
7041
7042
aff518b0 7043match
7044gfc_match_codimension (void)
7045{
7046 gfc_clear_attr (&current_attr);
7047 current_attr.codimension = 1;
7048
7049 return attr_decl ();
7050}
7051
7052
b3c3927c 7053match
7054gfc_match_contiguous (void)
7055{
60e19868 7056 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
b3c3927c 7057 return MATCH_ERROR;
7058
7059 gfc_clear_attr (&current_attr);
7060 current_attr.contiguous = 1;
7061
7062 return attr_decl ();
7063}
7064
7065
4ee9c684 7066match
7067gfc_match_dimension (void)
7068{
4ee9c684 7069 gfc_clear_attr (&current_attr);
14efb9b7 7070 current_attr.dimension = 1;
4ee9c684 7071
7072 return attr_decl ();
7073}
7074
7075
7076match
7077gfc_match_target (void)
7078{
4ee9c684 7079 gfc_clear_attr (&current_attr);
14efb9b7 7080 current_attr.target = 1;
4ee9c684 7081
7082 return attr_decl ();
7083}
7084
7085
7086/* Match the list of entities being specified in a PUBLIC or PRIVATE
7087 statement. */
7088
7089static match
7090access_attr_decl (gfc_statement st)
7091{
7092 char name[GFC_MAX_SYMBOL_LEN + 1];
7093 interface_type type;
7094 gfc_user_op *uop;
c2958b6b 7095 gfc_symbol *sym, *dt_sym;
dcb1b019 7096 gfc_intrinsic_op op;
4ee9c684 7097 match m;
7098
7099 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7100 goto done;
7101
7102 for (;;)
7103 {
dcb1b019 7104 m = gfc_match_generic_spec (&type, name, &op);
4ee9c684 7105 if (m == MATCH_NO)
7106 goto syntax;
7107 if (m == MATCH_ERROR)
7108 return MATCH_ERROR;
7109
7110 switch (type)
7111 {
7112 case INTERFACE_NAMELESS:
94fa7146 7113 case INTERFACE_ABSTRACT:
4ee9c684 7114 goto syntax;
7115
7116 case INTERFACE_GENERIC:
7117 if (gfc_get_symbol (name, NULL, &sym))
7118 goto done;
7119
60e19868 7120 if (!gfc_add_access (&sym->attr,
7121 (st == ST_PUBLIC)
7122 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7123 sym->name, NULL))
4ee9c684 7124 return MATCH_ERROR;
7125
c2958b6b 7126 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
60e19868 7127 && !gfc_add_access (&dt_sym->attr,
7128 (st == ST_PUBLIC)
7129 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
7130 sym->name, NULL))
c2958b6b 7131 return MATCH_ERROR;
7132
4ee9c684 7133 break;
7134
7135 case INTERFACE_INTRINSIC_OP:
dcb1b019 7136 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
4ee9c684 7137 {
0c482c1d 7138 gfc_intrinsic_op other_op;
7139
dcb1b019 7140 gfc_current_ns->operator_access[op] =
4ee9c684 7141 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
0c482c1d 7142
7143 /* Handle the case if there is another op with the same
7144 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7145 other_op = gfc_equivalent_op (op);
7146
7147 if (other_op != INTRINSIC_NONE)
7148 gfc_current_ns->operator_access[other_op] =
7149 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
7150
4ee9c684 7151 }
7152 else
7153 {
7154 gfc_error ("Access specification of the %s operator at %C has "
dcb1b019 7155 "already been specified", gfc_op2string (op));
4ee9c684 7156 goto done;
7157 }
7158
7159 break;
7160
7161 case INTERFACE_USER_OP:
7162 uop = gfc_get_uop (name);
7163
7164 if (uop->access == ACCESS_UNKNOWN)
7165 {
1a9745d2 7166 uop->access = (st == ST_PUBLIC)
7167 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4ee9c684 7168 }
7169 else
7170 {
1a9745d2 7171 gfc_error ("Access specification of the .%s. operator at %C "
7172 "has already been specified", sym->name);
4ee9c684 7173 goto done;
7174 }
7175
7176 break;
7177 }
7178
7179 if (gfc_match_char (',') == MATCH_NO)
7180 break;
7181 }
7182
7183 if (gfc_match_eos () != MATCH_YES)
7184 goto syntax;
7185 return MATCH_YES;
7186
7187syntax:
7188 gfc_syntax_error (st);
7189
7190done:
7191 return MATCH_ERROR;
7192}
7193
7194
3ea52af3 7195match
7196gfc_match_protected (void)
7197{
7198 gfc_symbol *sym;
7199 match m;
7200
54c0257b 7201 if (!gfc_current_ns->proc_name
7202 || gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3ea52af3 7203 {
7204 gfc_error ("PROTECTED at %C only allowed in specification "
7205 "part of a module");
7206 return MATCH_ERROR;
7207
7208 }
7209
60e19868 7210 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
3ea52af3 7211 return MATCH_ERROR;
7212
7213 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7214 {
7215 return MATCH_ERROR;
7216 }
7217
7218 if (gfc_match_eos () == MATCH_YES)
7219 goto syntax;
7220
7221 for(;;)
7222 {
7223 m = gfc_match_symbol (&sym, 0);
7224 switch (m)
7225 {
7226 case MATCH_YES:
60e19868 7227 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
3ea52af3 7228 return MATCH_ERROR;
7229 goto next_item;
7230
7231 case MATCH_NO:
7232 break;
7233
7234 case MATCH_ERROR:
7235 return MATCH_ERROR;
7236 }
7237
7238 next_item:
7239 if (gfc_match_eos () == MATCH_YES)
7240 break;
7241 if (gfc_match_char (',') != MATCH_YES)
7242 goto syntax;
7243 }
7244
7245 return MATCH_YES;
7246
7247syntax:
7248 gfc_error ("Syntax error in PROTECTED statement at %C");
7249 return MATCH_ERROR;
7250}
7251
7252
a0527218 7253/* The PRIVATE statement is a bit weird in that it can be an attribute
69b1505f 7254 declaration, but also works as a standalone statement inside of a
4ee9c684 7255 type declaration or a module. */
7256
7257match
1a9745d2 7258gfc_match_private (gfc_statement *st)
4ee9c684 7259{
7260
7261 if (gfc_match ("private") != MATCH_YES)
7262 return MATCH_NO;
7263
e14bee04 7264 if (gfc_current_state () != COMP_MODULE
7fd88f6e 7265 && !(gfc_current_state () == COMP_DERIVED
7266 && gfc_state_stack->previous
7267 && gfc_state_stack->previous->state == COMP_MODULE)
7268 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7269 && gfc_state_stack->previous && gfc_state_stack->previous->previous
7270 && gfc_state_stack->previous->previous->state == COMP_MODULE))
e14bee04 7271 {
7272 gfc_error ("PRIVATE statement at %C is only allowed in the "
7273 "specification part of a module");
7274 return MATCH_ERROR;
7275 }
7276
4ee9c684 7277 if (gfc_current_state () == COMP_DERIVED)
7278 {
7279 if (gfc_match_eos () == MATCH_YES)
7280 {
7281 *st = ST_PRIVATE;
7282 return MATCH_YES;
7283 }
7284
7285 gfc_syntax_error (ST_PRIVATE);
7286 return MATCH_ERROR;
7287 }
7288
7289 if (gfc_match_eos () == MATCH_YES)
7290 {
7291 *st = ST_PRIVATE;
7292 return MATCH_YES;
7293 }
7294
7295 *st = ST_ATTR_DECL;
7296 return access_attr_decl (ST_PRIVATE);
7297}
7298
7299
7300match
1a9745d2 7301gfc_match_public (gfc_statement *st)
4ee9c684 7302{
7303
7304 if (gfc_match ("public") != MATCH_YES)
7305 return MATCH_NO;
7306
e14bee04 7307 if (gfc_current_state () != COMP_MODULE)
7308 {
7309 gfc_error ("PUBLIC statement at %C is only allowed in the "
7310 "specification part of a module");
7311 return MATCH_ERROR;
7312 }
7313
4ee9c684 7314 if (gfc_match_eos () == MATCH_YES)
7315 {
7316 *st = ST_PUBLIC;
7317 return MATCH_YES;
7318 }
7319
7320 *st = ST_ATTR_DECL;
7321 return access_attr_decl (ST_PUBLIC);
7322}
7323
7324
7325/* Workhorse for gfc_match_parameter. */
7326
7327static match
7328do_parm (void)
7329{
7330 gfc_symbol *sym;
7331 gfc_expr *init;
7332 match m;
60e19868 7333 bool t;
4ee9c684 7334
7335 m = gfc_match_symbol (&sym, 0);
7336 if (m == MATCH_NO)
7337 gfc_error ("Expected variable name at %C in PARAMETER statement");
7338
7339 if (m != MATCH_YES)
7340 return m;
7341
7342 if (gfc_match_char ('=') == MATCH_NO)
7343 {
7344 gfc_error ("Expected = sign in PARAMETER statement at %C");
7345 return MATCH_ERROR;
7346 }
7347
7348 m = gfc_match_init_expr (&init);
7349 if (m == MATCH_NO)
7350 gfc_error ("Expected expression at %C in PARAMETER statement");
7351 if (m != MATCH_YES)
7352 return m;
7353
7354 if (sym->ts.type == BT_UNKNOWN
60e19868 7355 && !gfc_set_default_type (sym, 1, NULL))
4ee9c684 7356 {
7357 m = MATCH_ERROR;
7358 goto cleanup;
7359 }
7360
60e19868 7361 if (!gfc_check_assign_symbol (sym, NULL, init)
7362 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
4ee9c684 7363 {
7364 m = MATCH_ERROR;
7365 goto cleanup;
7366 }
7367
13aebeb0 7368 if (sym->value)
7369 {
7370 gfc_error ("Initializing already initialized variable at %C");
7371 m = MATCH_ERROR;
7372 goto cleanup;
7373 }
7374
f16404e3 7375 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
60e19868 7376 return (t) ? MATCH_YES : MATCH_ERROR;
4ee9c684 7377
7378cleanup:
7379 gfc_free_expr (init);
7380 return m;
7381}
7382
7383
7384/* Match a parameter statement, with the weird syntax that these have. */
7385
7386match
7387gfc_match_parameter (void)
7388{
7389 match m;
7390
7391 if (gfc_match_char ('(') == MATCH_NO)
7392 return MATCH_NO;
7393
7394 for (;;)
7395 {
7396 m = do_parm ();
7397 if (m != MATCH_YES)
7398 break;
7399
7400 if (gfc_match (" )%t") == MATCH_YES)
7401 break;
7402
7403 if (gfc_match_char (',') != MATCH_YES)
7404 {
7405 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7406 m = MATCH_ERROR;
7407 break;
7408 }
7409 }
7410
7411 return m;
7412}
7413
7414
7415/* Save statements have a special syntax. */
7416
7417match
7418gfc_match_save (void)
7419{
82f5ee13 7420 char n[GFC_MAX_SYMBOL_LEN+1];
7421 gfc_common_head *c;
4ee9c684 7422 gfc_symbol *sym;
7423 match m;
7424
7425 if (gfc_match_eos () == MATCH_YES)
7426 {
7427 if (gfc_current_ns->seen_save)
7428 {
60e19868 7429 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7430 "follows previous SAVE statement"))
76e82f95 7431 return MATCH_ERROR;
4ee9c684 7432 }
7433
7434 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7435 return MATCH_YES;
7436 }
7437
7438 if (gfc_current_ns->save_all)
7439 {
60e19868 7440 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7441 "blanket SAVE statement"))
76e82f95 7442 return MATCH_ERROR;
4ee9c684 7443 }
7444
7445 gfc_match (" ::");
7446
7447 for (;;)
7448 {
7449 m = gfc_match_symbol (&sym, 0);
7450 switch (m)
7451 {
7452 case MATCH_YES:
60e19868 7453 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7454 &gfc_current_locus))
4ee9c684 7455 return MATCH_ERROR;
7456 goto next_item;
7457
7458 case MATCH_NO:
7459 break;
7460
7461 case MATCH_ERROR:
7462 return MATCH_ERROR;
7463 }
7464
82f5ee13 7465 m = gfc_match (" / %n /", &n);
4ee9c684 7466 if (m == MATCH_ERROR)
7467 return MATCH_ERROR;
7468 if (m == MATCH_NO)
7469 goto syntax;
7470
403ddc45 7471 c = gfc_get_common (n, 0);
82f5ee13 7472 c->saved = 1;
7473
4ee9c684 7474 gfc_current_ns->seen_save = 1;
7475
7476 next_item:
7477 if (gfc_match_eos () == MATCH_YES)
7478 break;
7479 if (gfc_match_char (',') != MATCH_YES)
7480 goto syntax;
7481 }
7482
7483 return MATCH_YES;
7484
7485syntax:
7486 gfc_error ("Syntax error in SAVE statement at %C");
7487 return MATCH_ERROR;
7488}
7489
7490
8f6339b6 7491match
7492gfc_match_value (void)
7493{
7494 gfc_symbol *sym;
7495 match m;
7496
6a7084d7 7497 /* This is not allowed within a BLOCK construct! */
7498 if (gfc_current_state () == COMP_BLOCK)
7499 {
7500 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7501 return MATCH_ERROR;
7502 }
7503
60e19868 7504 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
8f6339b6 7505 return MATCH_ERROR;
7506
7507 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7508 {
7509 return MATCH_ERROR;
7510 }
7511
7512 if (gfc_match_eos () == MATCH_YES)
7513 goto syntax;
7514
7515 for(;;)
7516 {
7517 m = gfc_match_symbol (&sym, 0);
7518 switch (m)
7519 {
7520 case MATCH_YES:
60e19868 7521 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
8f6339b6 7522 return MATCH_ERROR;
7523 goto next_item;
7524
7525 case MATCH_NO:
7526 break;
7527
7528 case MATCH_ERROR:
7529 return MATCH_ERROR;
7530 }
7531
7532 next_item:
7533 if (gfc_match_eos () == MATCH_YES)
7534 break;
7535 if (gfc_match_char (',') != MATCH_YES)
7536 goto syntax;
7537 }
7538
7539 return MATCH_YES;
7540
7541syntax:
7542 gfc_error ("Syntax error in VALUE statement at %C");
7543 return MATCH_ERROR;
7544}
7545
f6d0e37a 7546
ef814c81 7547match
7548gfc_match_volatile (void)
7549{
7550 gfc_symbol *sym;
7551 match m;
7552
60e19868 7553 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
ef814c81 7554 return MATCH_ERROR;
7555
7556 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7557 {
7558 return MATCH_ERROR;
7559 }
7560
7561 if (gfc_match_eos () == MATCH_YES)
7562 goto syntax;
7563
7564 for(;;)
7565 {
8db94b3b 7566 /* VOLATILE is special because it can be added to host-associated
293d72e0 7567 symbols locally. Except for coarrays. */
2f241857 7568 m = gfc_match_symbol (&sym, 1);
ef814c81 7569 switch (m)
7570 {
7571 case MATCH_YES:
aff518b0 7572 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7573 for variable in a BLOCK which is defined outside of the BLOCK. */
7574 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7575 {
716da296 7576 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
aff518b0 7577 "%C, which is use-/host-associated", sym->name);
7578 return MATCH_ERROR;
7579 }
60e19868 7580 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
ef814c81 7581 return MATCH_ERROR;
7582 goto next_item;
7583
7584 case MATCH_NO:
7585 break;
7586
7587 case MATCH_ERROR:
7588 return MATCH_ERROR;
7589 }
7590
7591 next_item:
7592 if (gfc_match_eos () == MATCH_YES)
7593 break;
7594 if (gfc_match_char (',') != MATCH_YES)
7595 goto syntax;
7596 }
7597
7598 return MATCH_YES;
7599
7600syntax:
7601 gfc_error ("Syntax error in VOLATILE statement at %C");
7602 return MATCH_ERROR;
7603}
7604
7605
738928be 7606match
7607gfc_match_asynchronous (void)
7608{
7609 gfc_symbol *sym;
7610 match m;
7611
60e19868 7612 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
738928be 7613 return MATCH_ERROR;
7614
7615 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7616 {
7617 return MATCH_ERROR;
7618 }
7619
7620 if (gfc_match_eos () == MATCH_YES)
7621 goto syntax;
7622
7623 for(;;)
7624 {
8db94b3b 7625 /* ASYNCHRONOUS is special because it can be added to host-associated
738928be 7626 symbols locally. */
7627 m = gfc_match_symbol (&sym, 1);
7628 switch (m)
7629 {
7630 case MATCH_YES:
60e19868 7631 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
738928be 7632 return MATCH_ERROR;
7633 goto next_item;
7634
7635 case MATCH_NO:
7636 break;
7637
7638 case MATCH_ERROR:
7639 return MATCH_ERROR;
7640 }
7641
7642 next_item:
7643 if (gfc_match_eos () == MATCH_YES)
7644 break;
7645 if (gfc_match_char (',') != MATCH_YES)
7646 goto syntax;
7647 }
7648
7649 return MATCH_YES;
7650
7651syntax:
7652 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7653 return MATCH_ERROR;
7654}
7655
7656
4b8eb6ca 7657/* Match a module procedure statement in a submodule. */
7658
7659match
7660gfc_match_submod_proc (void)
7661{
7662 char name[GFC_MAX_SYMBOL_LEN + 1];
7663 gfc_symbol *sym, *fsym;
7664 match m;
7665 gfc_formal_arglist *formal, *head, *tail;
7666
7667 if (gfc_current_state () != COMP_CONTAINS
7668 || !(gfc_state_stack->previous
7669 && gfc_state_stack->previous->state == COMP_SUBMODULE))
7670 return MATCH_NO;
7671
7672 m = gfc_match (" module% procedure% %n", name);
7673 if (m != MATCH_YES)
7674 return m;
7675
7676 if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
7677 "at %C"))
7678 return MATCH_ERROR;
7679
7680 if (get_proc_name (name, &sym, false))
7681 return MATCH_ERROR;
7682
7683 /* Make sure that the result field is appropriately filled, even though
7684 the result symbol will be replaced later on. */
fe9d2f5a 7685 if (sym->ts.interface && sym->ts.interface->attr.function)
4b8eb6ca 7686 {
7687 if (sym->ts.interface->result
7688 && sym->ts.interface->result != sym->ts.interface)
7689 sym->result= sym->ts.interface->result;
7690 else
7691 sym->result = sym;
7692 }
7693
7694 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7695 the symbol existed before. */
7696 sym->declared_at = gfc_current_locus;
7697
7698 if (!sym->attr.module_procedure)
7699 return MATCH_ERROR;
7700
7701 /* Signal match_end to expect "end procedure". */
7702 sym->abr_modproc_decl = 1;
7703
7704 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7705 sym->attr.if_source = IFSRC_DECL;
7706
7707 gfc_new_block = sym;
7708
7709 /* Make a new formal arglist with the symbols in the procedure
7710 namespace. */
7711 head = tail = NULL;
7712 for (formal = sym->formal; formal && formal->sym; formal = formal->next)
7713 {
7714 if (formal == sym->formal)
7715 head = tail = gfc_get_formal_arglist ();
7716 else
7717 {
7718 tail->next = gfc_get_formal_arglist ();
7719 tail = tail->next;
7720 }
7721
7722 if (gfc_copy_dummy_sym (&fsym, formal->sym, 0))
7723 goto cleanup;
7724
7725 tail->sym = fsym;
7726 gfc_set_sym_referenced (fsym);
7727 }
7728
7729 /* The dummy symbols get cleaned up, when the formal_namespace of the
7730 interface declaration is cleared. This allows us to add the
7731 explicit interface as is done for other type of procedure. */
7732 if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
7733 &gfc_current_locus))
7734 return MATCH_ERROR;
7735
7736 if (gfc_match_eos () != MATCH_YES)
7737 {
7738 gfc_syntax_error (ST_MODULE_PROC);
7739 return MATCH_ERROR;
7740 }
7741
7742 return MATCH_YES;
7743
7744cleanup:
7745 gfc_free_formal_arglist (head);
7746 return MATCH_ERROR;
7747}
7748
7749
4ee9c684 7750/* Match a module procedure statement. Note that we have to modify
7751 symbols in the parent's namespace because the current one was there
89d91d02 7752 to receive symbols that are in an interface's formal argument list. */
4ee9c684 7753
7754match
7755gfc_match_modproc (void)
7756{
7757 char name[GFC_MAX_SYMBOL_LEN + 1];
7758 gfc_symbol *sym;
7759 match m;
d920fb76 7760 locus old_locus;
63d42079 7761 gfc_namespace *module_ns;
94ce9f74 7762 gfc_interface *old_interface_head, *interface;
4ee9c684 7763
7764 if (gfc_state_stack->state != COMP_INTERFACE
7765 || gfc_state_stack->previous == NULL
5cf92482 7766 || current_interface.type == INTERFACE_NAMELESS
7767 || current_interface.type == INTERFACE_ABSTRACT)
4ee9c684 7768 {
1a9745d2 7769 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7770 "interface");
4ee9c684 7771 return MATCH_ERROR;
7772 }
7773
63d42079 7774 module_ns = gfc_current_ns->parent;
7775 for (; module_ns; module_ns = module_ns->parent)
15ca3865 7776 if (module_ns->proc_name->attr.flavor == FL_MODULE
7777 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7778 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7779 && !module_ns->proc_name->attr.contained))
63d42079 7780 break;
7781
7782 if (module_ns == NULL)
7783 return MATCH_ERROR;
7784
94ce9f74 7785 /* Store the current state of the interface. We will need it if we
7786 end up with a syntax error and need to recover. */
7787 old_interface_head = gfc_current_interface_head ();
7788
d920fb76 7789 /* Check if the F2008 optional double colon appears. */
7790 gfc_gobble_whitespace ();
7791 old_locus = gfc_current_locus;
7792 if (gfc_match ("::") == MATCH_YES)
7793 {
60e19868 7794 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7795 "MODULE PROCEDURE statement at %L", &old_locus))
d920fb76 7796 return MATCH_ERROR;
7797 }
7798 else
7799 gfc_current_locus = old_locus;
8db94b3b 7800
4ee9c684 7801 for (;;)
7802 {
94ce9f74 7803 bool last = false;
d920fb76 7804 old_locus = gfc_current_locus;
94ce9f74 7805
4ee9c684 7806 m = gfc_match_name (name);
7807 if (m == MATCH_NO)
7808 goto syntax;
7809 if (m != MATCH_YES)
7810 return MATCH_ERROR;
7811
94ce9f74 7812 /* Check for syntax error before starting to add symbols to the
7813 current namespace. */
7814 if (gfc_match_eos () == MATCH_YES)
7815 last = true;
d920fb76 7816
94ce9f74 7817 if (!last && gfc_match_char (',') != MATCH_YES)
7818 goto syntax;
7819
7820 /* Now we're sure the syntax is valid, we process this item
7821 further. */
63d42079 7822 if (gfc_get_symbol (name, module_ns, &sym))
4ee9c684 7823 return MATCH_ERROR;
7824
15ca3865 7825 if (sym->attr.intrinsic)
7826 {
7827 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7828 "PROCEDURE", &old_locus);
7829 return MATCH_ERROR;
7830 }
7831
4ee9c684 7832 if (sym->attr.proc != PROC_MODULE
60e19868 7833 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
4ee9c684 7834 return MATCH_ERROR;
7835
60e19868 7836 if (!gfc_add_interface (sym))
4ee9c684 7837 return MATCH_ERROR;
7838
3186f695 7839 sym->attr.mod_proc = 1;
15ca3865 7840 sym->declared_at = old_locus;
3186f695 7841
94ce9f74 7842 if (last)
4ee9c684 7843 break;
4ee9c684 7844 }
7845
7846 return MATCH_YES;
7847
7848syntax:
94ce9f74 7849 /* Restore the previous state of the interface. */
7850 interface = gfc_current_interface_head ();
7851 gfc_set_current_interface_head (old_interface_head);
7852
7853 /* Free the new interfaces. */
7854 while (interface != old_interface_head)
7855 {
7856 gfc_interface *i = interface->next;
434f0922 7857 free (interface);
94ce9f74 7858 interface = i;
7859 }
7860
7861 /* And issue a syntax error. */
4ee9c684 7862 gfc_syntax_error (ST_MODULE_PROC);
7863 return MATCH_ERROR;
7864}
7865
7866
ea94d76d 7867/* Check a derived type that is being extended. */
7a99bc9b 7868
ea94d76d 7869static gfc_symbol*
7870check_extended_derived_type (char *name)
7871{
7872 gfc_symbol *extended;
7873
7874 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7875 {
7876 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7877 return NULL;
7878 }
7879
7a99bc9b 7880 extended = gfc_find_dt_in_generic (extended);
7881
7882 /* F08:C428. */
ea94d76d 7883 if (!extended)
7884 {
716da296 7885 gfc_error ("Symbol %qs at %C has not been previously defined", name);
ea94d76d 7886 return NULL;
7887 }
7888
7889 if (extended->attr.flavor != FL_DERIVED)
7890 {
716da296 7891 gfc_error ("%qs in EXTENDS expression at %C is not a "
ea94d76d 7892 "derived type", name);
7893 return NULL;
7894 }
7895
7896 if (extended->attr.is_bind_c)
7897 {
716da296 7898 gfc_error ("%qs cannot be extended at %C because it "
ea94d76d 7899 "is BIND(C)", extended->name);
7900 return NULL;
7901 }
7902
7903 if (extended->attr.sequence)
7904 {
716da296 7905 gfc_error ("%qs cannot be extended at %C because it "
ea94d76d 7906 "is a SEQUENCE type", extended->name);
7907 return NULL;
7908 }
7909
7910 return extended;
7911}
7912
7913
c5d33754 7914/* Match the optional attribute specifiers for a type declaration.
7915 Return MATCH_ERROR if an error is encountered in one of the handled
7916 attributes (public, private, bind(c)), MATCH_NO if what's found is
7917 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7918 checking on attribute conflicts needs to be done. */
4ee9c684 7919
7920match
ea94d76d 7921gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
4ee9c684 7922{
c5d33754 7923 /* See if the derived type is marked as private. */
4ee9c684 7924 if (gfc_match (" , private") == MATCH_YES)
7925 {
e14bee04 7926 if (gfc_current_state () != COMP_MODULE)
4ee9c684 7927 {
e14bee04 7928 gfc_error ("Derived type at %C can only be PRIVATE in the "
7929 "specification part of a module");
4ee9c684 7930 return MATCH_ERROR;
7931 }
7932
60e19868 7933 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
4ee9c684 7934 return MATCH_ERROR;
4ee9c684 7935 }
c5d33754 7936 else if (gfc_match (" , public") == MATCH_YES)
4ee9c684 7937 {
e14bee04 7938 if (gfc_current_state () != COMP_MODULE)
4ee9c684 7939 {
e14bee04 7940 gfc_error ("Derived type at %C can only be PUBLIC in the "
7941 "specification part of a module");
4ee9c684 7942 return MATCH_ERROR;
7943 }
7944
60e19868 7945 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
4ee9c684 7946 return MATCH_ERROR;
4ee9c684 7947 }
ac5f2650 7948 else if (gfc_match (" , bind ( c )") == MATCH_YES)
c5d33754 7949 {
7950 /* If the type is defined to be bind(c) it then needs to make
7951 sure that all fields are interoperable. This will
7952 need to be a semantic check on the finished derived type.
7953 See 15.2.3 (lines 9-12) of F2003 draft. */
60e19868 7954 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
c5d33754 7955 return MATCH_ERROR;
7956
7957 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7958 }
ac5f2650 7959 else if (gfc_match (" , abstract") == MATCH_YES)
7960 {
60e19868 7961 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
ac5f2650 7962 return MATCH_ERROR;
7963
60e19868 7964 if (!gfc_add_abstract (attr, &gfc_current_locus))
ac5f2650 7965 return MATCH_ERROR;
7966 }
60e19868 7967 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
ea94d76d 7968 {
60e19868 7969 if (!gfc_add_extension (attr, &gfc_current_locus))
ea94d76d 7970 return MATCH_ERROR;
7971 }
c5d33754 7972 else
7973 return MATCH_NO;
7974
7975 /* If we get here, something matched. */
7976 return MATCH_YES;
7977}
7978
7979
7980/* Match the beginning of a derived type declaration. If a type name
7981 was the result of a function, then it is possible to have a symbol
7982 already to be known as a derived type yet have no components. */
7983
7984match
7985gfc_match_derived_decl (void)
7986{
7987 char name[GFC_MAX_SYMBOL_LEN + 1];
ea94d76d 7988 char parent[GFC_MAX_SYMBOL_LEN + 1];
c5d33754 7989 symbol_attribute attr;
c2958b6b 7990 gfc_symbol *sym, *gensym;
ea94d76d 7991 gfc_symbol *extended;
c5d33754 7992 match m;
7993 match is_type_attr_spec = MATCH_NO;
33e86520 7994 bool seen_attr = false;
c2958b6b 7995 gfc_interface *intr = NULL, *head;
c5d33754 7996
7997 if (gfc_current_state () == COMP_DERIVED)
7998 return MATCH_NO;
7999
ea94d76d 8000 name[0] = '\0';
8001 parent[0] = '\0';
c5d33754 8002 gfc_clear_attr (&attr);
ea94d76d 8003 extended = NULL;
c5d33754 8004
8005 do
8006 {
ea94d76d 8007 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
c5d33754 8008 if (is_type_attr_spec == MATCH_ERROR)
8009 return MATCH_ERROR;
33e86520 8010 if (is_type_attr_spec == MATCH_YES)
8011 seen_attr = true;
c5d33754 8012 } while (is_type_attr_spec == MATCH_YES);
4ee9c684 8013
e485ad6b 8014 /* Deal with derived type extensions. The extension attribute has
8015 been added to 'attr' but now the parent type must be found and
8016 checked. */
ea94d76d 8017 if (parent[0])
8018 extended = check_extended_derived_type (parent);
8019
8020 if (parent[0] && !extended)
8021 return MATCH_ERROR;
8022
33e86520 8023 if (gfc_match (" ::") != MATCH_YES && seen_attr)
4ee9c684 8024 {
8025 gfc_error ("Expected :: in TYPE definition at %C");
8026 return MATCH_ERROR;
8027 }
8028
8029 m = gfc_match (" %n%t", name);
8030 if (m != MATCH_YES)
8031 return m;
8032
a3055431 8033 /* Make sure the name is not the name of an intrinsic type. */
8034 if (gfc_is_intrinsic_typename (name))
4ee9c684 8035 {
716da296 8036 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
1a9745d2 8037 "type", name);
4ee9c684 8038 return MATCH_ERROR;
8039 }
8040
c2958b6b 8041 if (gfc_get_symbol (name, NULL, &gensym))
4ee9c684 8042 return MATCH_ERROR;
8043
c2958b6b 8044 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
4ee9c684 8045 {
716da296 8046 gfc_error ("Derived type name %qs at %C already has a basic type "
c2958b6b 8047 "of %s", gensym->name, gfc_typename (&gensym->ts));
8048 return MATCH_ERROR;
8049 }
8050
8051 if (!gensym->attr.generic
60e19868 8052 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
c2958b6b 8053 return MATCH_ERROR;
8054
8055 if (!gensym->attr.function
60e19868 8056 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
c2958b6b 8057 return MATCH_ERROR;
8058
8059 sym = gfc_find_dt_in_generic (gensym);
8060
8061 if (sym && (sym->components != NULL || sym->attr.zero_comp))
8062 {
716da296 8063 gfc_error ("Derived type definition of %qs at %C has already been "
c2958b6b 8064 "defined", sym->name);
4ee9c684 8065 return MATCH_ERROR;
8066 }
8067
c2958b6b 8068 if (!sym)
8069 {
8070 /* Use upper case to save the actual derived-type symbol. */
8071 gfc_get_symbol (gfc_get_string ("%c%s",
8072 (char) TOUPPER ((unsigned char) gensym->name[0]),
8073 &gensym->name[1]), NULL, &sym);
8074 sym->name = gfc_get_string (gensym->name);
8075 head = gensym->generic;
8076 intr = gfc_get_interface ();
8077 intr->sym = sym;
8078 intr->where = gfc_current_locus;
8079 intr->sym->declared_at = gfc_current_locus;
8080 intr->next = head;
8081 gensym->generic = intr;
8082 gensym->attr.if_source = IFSRC_DECL;
8083 }
8084
4ee9c684 8085 /* The symbol may already have the derived attribute without the
8086 components. The ways this can happen is via a function
8087 definition, an INTRINSIC statement or a subtype in another
8088 derived type that is a pointer. The first part of the AND clause
69b1505f 8089 is true if the symbol is not the return value of a function. */
4ee9c684 8090 if (sym->attr.flavor != FL_DERIVED
60e19868 8091 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
4ee9c684 8092 return MATCH_ERROR;
8093
4ee9c684 8094 if (attr.access != ACCESS_UNKNOWN
60e19868 8095 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
4ee9c684 8096 return MATCH_ERROR;
c2958b6b 8097 else if (sym->attr.access == ACCESS_UNKNOWN
8098 && gensym->attr.access != ACCESS_UNKNOWN
60e19868 8099 && !gfc_add_access (&sym->attr, gensym->attr.access,
8100 sym->name, NULL))
c2958b6b 8101 return MATCH_ERROR;
8102
8103 if (sym->attr.access != ACCESS_UNKNOWN
8104 && gensym->attr.access == ACCESS_UNKNOWN)
8105 gensym->attr.access = sym->attr.access;
4ee9c684 8106
c5d33754 8107 /* See if the derived type was labeled as bind(c). */
8108 if (attr.is_bind_c != 0)
8109 sym->attr.is_bind_c = attr.is_bind_c;
8110
223f0f57 8111 /* Construct the f2k_derived namespace if it is not yet there. */
8112 if (!sym->f2k_derived)
8113 sym->f2k_derived = gfc_get_namespace (NULL, 0);
8db94b3b 8114
ea94d76d 8115 if (extended && !sym->components)
8116 {
8117 gfc_component *p;
ea94d76d 8118
8119 /* Add the extended derived type as the first component. */
8120 gfc_add_component (sym, parent, &p);
ea94d76d 8121 extended->refs++;
8122 gfc_set_sym_referenced (extended);
8123
8124 p->ts.type = BT_DERIVED;
eeebe20b 8125 p->ts.u.derived = extended;
ea94d76d 8126 p->initializer = gfc_default_initializer (&p->ts);
8db94b3b 8127
bdfbc762 8128 /* Set extension level. */
8129 if (extended->attr.extension == 255)
8130 {
8131 /* Since the extension field is 8 bit wide, we can only have
8132 up to 255 extension levels. */
716da296 8133 gfc_error ("Maximum extension level reached with type %qs at %L",
bdfbc762 8134 extended->name, &extended->declared_at);
8135 return MATCH_ERROR;
8136 }
8137 sym->attr.extension = extended->attr.extension + 1;
ea94d76d 8138
8139 /* Provide the links between the extended type and its extension. */
8140 if (!extended->f2k_derived)
8141 extended->f2k_derived = gfc_get_namespace (NULL, 0);
ea94d76d 8142 }
8143
bdfbc762 8144 if (!sym->hash_value)
8145 /* Set the hash for the compound name for this type. */
a656e7c2 8146 sym->hash_value = gfc_hash_value (sym);
1de1b1a9 8147
ac5f2650 8148 /* Take over the ABSTRACT attribute. */
8149 sym->attr.abstract = attr.abstract;
8150
4ee9c684 8151 gfc_new_block = sym;
8152
8153 return MATCH_YES;
8154}
b549d2a5 8155
8156
8db94b3b 8157/* Cray Pointees can be declared as:
452695a8 8158 pointer (ipt, a (n,m,...,*)) */
b549d2a5 8159
8458f4ca 8160match
b549d2a5 8161gfc_mod_pointee_as (gfc_array_spec *as)
8162{
8163 as->cray_pointee = true; /* This will be useful to know later. */
8164 if (as->type == AS_ASSUMED_SIZE)
452695a8 8165 as->cp_was_assumed = true;
b549d2a5 8166 else if (as->type == AS_ASSUMED_SHAPE)
8167 {
8168 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8169 return MATCH_ERROR;
8170 }
8171 return MATCH_YES;
8172}
3b6a4b41 8173
8174
8db94b3b 8175/* Match the enum definition statement, here we are trying to match
8176 the first line of enum definition statement.
3b6a4b41 8177 Returns MATCH_YES if match is found. */
8178
8179match
8180gfc_match_enum (void)
8181{
8182 match m;
8db94b3b 8183
3b6a4b41 8184 m = gfc_match_eos ();
8185 if (m != MATCH_YES)
8186 return m;
8187
60e19868 8188 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
3b6a4b41 8189 return MATCH_ERROR;
8190
8191 return MATCH_YES;
8192}
8193
8194
fe1328da 8195/* Returns an initializer whose value is one higher than the value of the
8196 LAST_INITIALIZER argument. If the argument is NULL, the
8197 initializers value will be set to zero. The initializer's kind
8198 will be set to gfc_c_int_kind.
8199
8200 If -fshort-enums is given, the appropriate kind will be selected
8201 later after all enumerators have been parsed. A warning is issued
8202 here if an initializer exceeds gfc_c_int_kind. */
8203
8204static gfc_expr *
8205enum_initializer (gfc_expr *last_initializer, locus where)
8206{
8207 gfc_expr *result;
126387b5 8208 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
fe1328da 8209
8210 mpz_init (result->value.integer);
8211
8212 if (last_initializer != NULL)
8213 {
8214 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
8215 result->where = last_initializer->where;
8216
8217 if (gfc_check_integer_range (result->value.integer,
8218 gfc_c_int_kind) != ARITH_OK)
8219 {
8220 gfc_error ("Enumerator exceeds the C integer type at %C");
8221 return NULL;
8222 }
8223 }
8224 else
8225 {
8226 /* Control comes here, if it's the very first enumerator and no
8227 initializer has been given. It will be initialized to zero. */
8228 mpz_set_si (result->value.integer, 0);
8229 }
8230
8231 return result;
8232}
8233
8234
60fbbf9e 8235/* Match a variable name with an optional initializer. When this
8236 subroutine is called, a variable is expected to be parsed next.
8237 Depending on what is happening at the moment, updates either the
8238 symbol table or the current interface. */
8239
8240static match
8241enumerator_decl (void)
8242{
8243 char name[GFC_MAX_SYMBOL_LEN + 1];
8244 gfc_expr *initializer;
8245 gfc_array_spec *as = NULL;
8246 gfc_symbol *sym;
8247 locus var_locus;
8248 match m;
60e19868 8249 bool t;
60fbbf9e 8250 locus old_locus;
8251
8252 initializer = NULL;
8253 old_locus = gfc_current_locus;
8254
8255 /* When we get here, we've just matched a list of attributes and
8256 maybe a type and a double colon. The next thing we expect to see
8257 is the name of the symbol. */
8258 m = gfc_match_name (name);
8259 if (m != MATCH_YES)
8260 goto cleanup;
8261
8262 var_locus = gfc_current_locus;
8263
8264 /* OK, we've successfully matched the declaration. Now put the
8265 symbol in the current namespace. If we fail to create the symbol,
8266 bail out. */
60e19868 8267 if (!build_sym (name, NULL, false, &as, &var_locus))
60fbbf9e 8268 {
8269 m = MATCH_ERROR;
8270 goto cleanup;
8271 }
8272
8273 /* The double colon must be present in order to have initializers.
8274 Otherwise the statement is ambiguous with an assignment statement. */
8275 if (colon_seen)
8276 {
8277 if (gfc_match_char ('=') == MATCH_YES)
8278 {
8279 m = gfc_match_init_expr (&initializer);
8280 if (m == MATCH_NO)
8281 {
8282 gfc_error ("Expected an initialization expression at %C");
8283 m = MATCH_ERROR;
8284 }
8285
8286 if (m != MATCH_YES)
8287 goto cleanup;
8288 }
8289 }
8290
8291 /* If we do not have an initializer, the initialization value of the
8292 previous enumerator (stored in last_initializer) is incremented
8293 by 1 and is used to initialize the current enumerator. */
8294 if (initializer == NULL)
fe1328da 8295 initializer = enum_initializer (last_initializer, old_locus);
e14bee04 8296
60fbbf9e 8297 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
8298 {
a8beb4f8 8299 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8300 &var_locus);
e14bee04 8301 m = MATCH_ERROR;
60fbbf9e 8302 goto cleanup;
8303 }
8304
8305 /* Store this current initializer, for the next enumerator variable
8306 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8307 use last_initializer below. */
8308 last_initializer = initializer;
8309 t = add_init_expr_to_sym (name, &initializer, &var_locus);
8310
8311 /* Maintain enumerator history. */
8312 gfc_find_symbol (name, NULL, 0, &sym);
8313 create_enum_history (sym, last_initializer);
8314
60e19868 8315 return (t) ? MATCH_YES : MATCH_ERROR;
60fbbf9e 8316
8317cleanup:
8318 /* Free stuff up and return. */
8319 gfc_free_expr (initializer);
8320
8321 return m;
8322}
8323
8324
f6d0e37a 8325/* Match the enumerator definition statement. */
3b6a4b41 8326
8327match
8328gfc_match_enumerator_def (void)
8329{
8330 match m;
60e19868 8331 bool t;
e14bee04 8332
3b6a4b41 8333 gfc_clear_ts (&current_ts);
e14bee04 8334
3b6a4b41 8335 m = gfc_match (" enumerator");
8336 if (m != MATCH_YES)
8337 return m;
60fbbf9e 8338
8339 m = gfc_match (" :: ");
8340 if (m == MATCH_ERROR)
8341 return m;
8342
8343 colon_seen = (m == MATCH_YES);
e14bee04 8344
3b6a4b41 8345 if (gfc_current_state () != COMP_ENUM)
8346 {
8347 gfc_error ("ENUM definition statement expected before %C");
8348 gfc_free_enum_history ();
8349 return MATCH_ERROR;
8350 }
8351
8352 (&current_ts)->type = BT_INTEGER;
8353 (&current_ts)->kind = gfc_c_int_kind;
e14bee04 8354
60fbbf9e 8355 gfc_clear_attr (&current_attr);
8356 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
60e19868 8357 if (!t)
3b6a4b41 8358 {
60fbbf9e 8359 m = MATCH_ERROR;
3b6a4b41 8360 goto cleanup;
8361 }
8362
3b6a4b41 8363 for (;;)
8364 {
60fbbf9e 8365 m = enumerator_decl ();
3b6a4b41 8366 if (m == MATCH_ERROR)
a8beb4f8 8367 {
8368 gfc_free_enum_history ();
8369 goto cleanup;
8370 }
3b6a4b41 8371 if (m == MATCH_NO)
8372 break;
8373
8374 if (gfc_match_eos () == MATCH_YES)
8375 goto cleanup;
8376 if (gfc_match_char (',') != MATCH_YES)
8377 break;
8378 }
8379
8380 if (gfc_current_state () == COMP_ENUM)
8381 {
8382 gfc_free_enum_history ();
8383 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8384 m = MATCH_ERROR;
8385 }
8386
8387cleanup:
8388 gfc_free_array_spec (current_as);
8389 current_as = NULL;
8390 return m;
8391
8392}
8393
e449e4dd 8394
7fd88f6e 8395/* Match binding attributes. */
8396
8397static match
64e93293 8398match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7fd88f6e 8399{
8400 bool found_passing = false;
64e93293 8401 bool seen_ptr = false;
fe9b08a2 8402 match m = MATCH_YES;
7fd88f6e 8403
df084314 8404 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7fd88f6e 8405 this case the defaults are in there. */
8406 ba->access = ACCESS_UNKNOWN;
8407 ba->pass_arg = NULL;
8408 ba->pass_arg_num = 0;
8409 ba->nopass = 0;
8410 ba->non_overridable = 0;
61c3b81d 8411 ba->deferred = 0;
fe9b08a2 8412 ba->ppc = ppc;
7fd88f6e 8413
8414 /* If we find a comma, we believe there are binding attributes. */
fe9b08a2 8415 m = gfc_match_char (',');
8416 if (m == MATCH_NO)
8417 goto done;
7fd88f6e 8418
8419 do
8420 {
e2f06a48 8421 /* Access specifier. */
8422
8423 m = gfc_match (" public");
7fd88f6e 8424 if (m == MATCH_ERROR)
8425 goto error;
8426 if (m == MATCH_YES)
8427 {
e2f06a48 8428 if (ba->access != ACCESS_UNKNOWN)
7fd88f6e 8429 {
e2f06a48 8430 gfc_error ("Duplicate access-specifier at %C");
7fd88f6e 8431 goto error;
8432 }
8433
e2f06a48 8434 ba->access = ACCESS_PUBLIC;
7fd88f6e 8435 continue;
8436 }
8437
e2f06a48 8438 m = gfc_match (" private");
7fd88f6e 8439 if (m == MATCH_ERROR)
8440 goto error;
8441 if (m == MATCH_YES)
8442 {
e2f06a48 8443 if (ba->access != ACCESS_UNKNOWN)
7fd88f6e 8444 {
e2f06a48 8445 gfc_error ("Duplicate access-specifier at %C");
7fd88f6e 8446 goto error;
8447 }
8448
e2f06a48 8449 ba->access = ACCESS_PRIVATE;
7fd88f6e 8450 continue;
8451 }
8452
e2f06a48 8453 /* If inside GENERIC, the following is not allowed. */
8454 if (!generic)
7fd88f6e 8455 {
7fd88f6e 8456
e2f06a48 8457 /* NOPASS flag. */
8458 m = gfc_match (" nopass");
8459 if (m == MATCH_ERROR)
8460 goto error;
8461 if (m == MATCH_YES)
7fd88f6e 8462 {
e2f06a48 8463 if (found_passing)
8464 {
8465 gfc_error ("Binding attributes already specify passing,"
8466 " illegal NOPASS at %C");
8467 goto error;
8468 }
8469
8470 found_passing = true;
8471 ba->nopass = 1;
8472 continue;
7fd88f6e 8473 }
8474
e2f06a48 8475 /* PASS possibly including argument. */
8476 m = gfc_match (" pass");
8477 if (m == MATCH_ERROR)
8478 goto error;
8479 if (m == MATCH_YES)
7fd88f6e 8480 {
e2f06a48 8481 char arg[GFC_MAX_SYMBOL_LEN + 1];
8482
8483 if (found_passing)
8484 {
8485 gfc_error ("Binding attributes already specify passing,"
8486 " illegal PASS at %C");
8487 goto error;
8488 }
8489
8490 m = gfc_match (" ( %n )", arg);
8491 if (m == MATCH_ERROR)
8492 goto error;
8493 if (m == MATCH_YES)
fe9b08a2 8494 ba->pass_arg = gfc_get_string (arg);
e2f06a48 8495 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8496
8497 found_passing = true;
8498 ba->nopass = 0;
8499 continue;
7fd88f6e 8500 }
8501
64e93293 8502 if (ppc)
8503 {
8504 /* POINTER flag. */
8505 m = gfc_match (" pointer");
8506 if (m == MATCH_ERROR)
8507 goto error;
8508 if (m == MATCH_YES)
8509 {
8510 if (seen_ptr)
8511 {
8512 gfc_error ("Duplicate POINTER attribute at %C");
8513 goto error;
8514 }
8515
8516 seen_ptr = true;
64e93293 8517 continue;
8518 }
8519 }
8520 else
8521 {
8522 /* NON_OVERRIDABLE flag. */
8523 m = gfc_match (" non_overridable");
8524 if (m == MATCH_ERROR)
8525 goto error;
8526 if (m == MATCH_YES)
8527 {
8528 if (ba->non_overridable)
8529 {
8530 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8531 goto error;
8532 }
8533
8534 ba->non_overridable = 1;
8535 continue;
8536 }
8537
8538 /* DEFERRED flag. */
8539 m = gfc_match (" deferred");
8540 if (m == MATCH_ERROR)
8541 goto error;
8542 if (m == MATCH_YES)
8543 {
8544 if (ba->deferred)
8545 {
8546 gfc_error ("Duplicate DEFERRED at %C");
8547 goto error;
8548 }
8549
8550 ba->deferred = 1;
8551 continue;
8552 }
8553 }
8554
7fd88f6e 8555 }
8556
8557 /* Nothing matching found. */
e2f06a48 8558 if (generic)
8559 gfc_error ("Expected access-specifier at %C");
8560 else
8561 gfc_error ("Expected binding attribute at %C");
7fd88f6e 8562 goto error;
8563 }
8564 while (gfc_match_char (',') == MATCH_YES);
8565
61c3b81d 8566 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8567 if (ba->non_overridable && ba->deferred)
8568 {
8569 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8570 goto error;
8571 }
8572
fe9b08a2 8573 m = MATCH_YES;
8574
8575done:
e2f06a48 8576 if (ba->access == ACCESS_UNKNOWN)
8577 ba->access = gfc_typebound_default_access;
8578
64e93293 8579 if (ppc && !seen_ptr)
8580 {
8581 gfc_error ("POINTER attribute is required for procedure pointer component"
8582 " at %C");
8583 goto error;
8584 }
8585
fe9b08a2 8586 return m;
7fd88f6e 8587
8588error:
7fd88f6e 8589 return MATCH_ERROR;
8590}
8591
8592
8593/* Match a PROCEDURE specific binding inside a derived type. */
8594
8595static match
8596match_procedure_in_type (void)
8597{
8598 char name[GFC_MAX_SYMBOL_LEN + 1];
8599 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7a3aaef8 8600 char* target = NULL, *ifc = NULL;
4a12b9ba 8601 gfc_typebound_proc tb;
7fd88f6e 8602 bool seen_colons;
8603 bool seen_attrs;
8604 match m;
8605 gfc_symtree* stree;
8606 gfc_namespace* ns;
8607 gfc_symbol* block;
7a3aaef8 8608 int num;
7fd88f6e 8609
8610 /* Check current state. */
8611 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8612 block = gfc_state_stack->previous->sym;
8613 gcc_assert (block);
8614
61c3b81d 8615 /* Try to match PROCEDURE(interface). */
7fd88f6e 8616 if (gfc_match (" (") == MATCH_YES)
8617 {
61c3b81d 8618 m = gfc_match_name (target_buf);
8619 if (m == MATCH_ERROR)
8620 return m;
8621 if (m != MATCH_YES)
8622 {
0d2b3c9c 8623 gfc_error ("Interface-name expected after %<(%> at %C");
61c3b81d 8624 return MATCH_ERROR;
8625 }
8626
8627 if (gfc_match (" )") != MATCH_YES)
8628 {
0d2b3c9c 8629 gfc_error ("%<)%> expected at %C");
61c3b81d 8630 return MATCH_ERROR;
8631 }
8632
7a3aaef8 8633 ifc = target_buf;
7fd88f6e 8634 }
8635
8636 /* Construct the data structure. */
b3d1387f 8637 memset (&tb, 0, sizeof (tb));
4a12b9ba 8638 tb.where = gfc_current_locus;
7fd88f6e 8639
8640 /* Match binding attributes. */
4a12b9ba 8641 m = match_binding_attributes (&tb, false, false);
7fd88f6e 8642 if (m == MATCH_ERROR)
8643 return m;
8644 seen_attrs = (m == MATCH_YES);
8645
7a3aaef8 8646 /* Check that attribute DEFERRED is given if an interface is specified. */
4a12b9ba 8647 if (tb.deferred && !ifc)
61c3b81d 8648 {
8649 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8650 return MATCH_ERROR;
8651 }
4a12b9ba 8652 if (ifc && !tb.deferred)
61c3b81d 8653 {
8654 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8655 return MATCH_ERROR;
8656 }
8657
7fd88f6e 8658 /* Match the colons. */
8659 m = gfc_match (" ::");
8660 if (m == MATCH_ERROR)
8661 return m;
8662 seen_colons = (m == MATCH_YES);
8663 if (seen_attrs && !seen_colons)
8664 {
0d2b3c9c 8665 gfc_error ("Expected %<::%> after binding-attributes at %C");
7fd88f6e 8666 return MATCH_ERROR;
8667 }
8668
8db94b3b 8669 /* Match the binding names. */
7a3aaef8 8670 for(num=1;;num++)
7fd88f6e 8671 {
7a3aaef8 8672 m = gfc_match_name (name);
8673 if (m == MATCH_ERROR)
8674 return m;
8675 if (m == MATCH_NO)
61c3b81d 8676 {
7a3aaef8 8677 gfc_error ("Expected binding name at %C");
61c3b81d 8678 return MATCH_ERROR;
8679 }
8680
60e19868 8681 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
7a3aaef8 8682 return MATCH_ERROR;
7fd88f6e 8683
7a3aaef8 8684 /* Try to match the '=> target', if it's there. */
8685 target = ifc;
8686 m = gfc_match (" =>");
7fd88f6e 8687 if (m == MATCH_ERROR)
8688 return m;
7a3aaef8 8689 if (m == MATCH_YES)
7fd88f6e 8690 {
4a12b9ba 8691 if (tb.deferred)
7a3aaef8 8692 {
0d2b3c9c 8693 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
7a3aaef8 8694 return MATCH_ERROR;
8695 }
8696
8697 if (!seen_colons)
8698 {
0d2b3c9c 8699 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
7a3aaef8 8700 " at %C");
8701 return MATCH_ERROR;
8702 }
8703
8704 m = gfc_match_name (target_buf);
8705 if (m == MATCH_ERROR)
8706 return m;
8707 if (m == MATCH_NO)
8708 {
0d2b3c9c 8709 gfc_error ("Expected binding target after %<=>%> at %C");
7a3aaef8 8710 return MATCH_ERROR;
8711 }
8712 target = target_buf;
7fd88f6e 8713 }
7fd88f6e 8714
7a3aaef8 8715 /* If no target was found, it has the same name as the binding. */
8716 if (!target)
8717 target = name;
7fd88f6e 8718
7a3aaef8 8719 /* Get the namespace to insert the symbols into. */
8720 ns = block->f2k_derived;
8721 gcc_assert (ns);
7fd88f6e 8722
7a3aaef8 8723 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
4a12b9ba 8724 if (tb.deferred && !block->attr.abstract)
7a3aaef8 8725 {
716da296 8726 gfc_error ("Type %qs containing DEFERRED binding at %C "
7a3aaef8 8727 "is not ABSTRACT", block->name);
8728 return MATCH_ERROR;
8729 }
7fd88f6e 8730
7a3aaef8 8731 /* See if we already have a binding with this name in the symtree which
9834e1b6 8732 would be an error. If a GENERIC already targeted this binding, it may
7a3aaef8 8733 be already there but then typebound is still NULL. */
8734 stree = gfc_find_symtree (ns->tb_sym_root, name);
e204cb62 8735 if (stree && stree->n.tb)
7a3aaef8 8736 {
716da296 8737 gfc_error ("There is already a procedure with binding name %qs for "
8738 "the derived type %qs at %C", name, block->name);
7a3aaef8 8739 return MATCH_ERROR;
8740 }
61c3b81d 8741
7a3aaef8 8742 /* Insert it and set attributes. */
7fd88f6e 8743
e204cb62 8744 if (!stree)
8745 {
8746 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8747 gcc_assert (stree);
8748 }
4a12b9ba 8749 stree->n.tb = gfc_get_typebound_proc (&tb);
3323e9b1 8750
4a12b9ba 8751 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8752 false))
7a3aaef8 8753 return MATCH_ERROR;
4a12b9ba 8754 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8db94b3b 8755
7a3aaef8 8756 if (gfc_match_eos () == MATCH_YES)
8757 return MATCH_YES;
8758 if (gfc_match_char (',') != MATCH_YES)
8759 goto syntax;
3323e9b1 8760 }
7fd88f6e 8761
7a3aaef8 8762syntax:
8763 gfc_error ("Syntax error in PROCEDURE statement at %C");
8764 return MATCH_ERROR;
7fd88f6e 8765}
8766
8767
e2f06a48 8768/* Match a GENERIC procedure binding inside a derived type. */
8769
8770match
8771gfc_match_generic (void)
8772{
8773 char name[GFC_MAX_SYMBOL_LEN + 1];
a36eb9ee 8774 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
e2f06a48 8775 gfc_symbol* block;
8776 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8777 gfc_typebound_proc* tb;
e2f06a48 8778 gfc_namespace* ns;
a36eb9ee 8779 interface_type op_type;
8780 gfc_intrinsic_op op;
e2f06a48 8781 match m;
8782
8783 /* Check current state. */
8784 if (gfc_current_state () == COMP_DERIVED)
8785 {
8786 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8787 return MATCH_ERROR;
8788 }
8789 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8790 return MATCH_NO;
8791 block = gfc_state_stack->previous->sym;
8792 ns = block->f2k_derived;
8793 gcc_assert (block && ns);
8794
b3d1387f 8795 memset (&tbattr, 0, sizeof (tbattr));
8796 tbattr.where = gfc_current_locus;
8797
e2f06a48 8798 /* See if we get an access-specifier. */
64e93293 8799 m = match_binding_attributes (&tbattr, true, false);
e2f06a48 8800 if (m == MATCH_ERROR)
8801 goto error;
8802
8803 /* Now the colons, those are required. */
8804 if (gfc_match (" ::") != MATCH_YES)
8805 {
0d2b3c9c 8806 gfc_error ("Expected %<::%> at %C");
e2f06a48 8807 goto error;
8808 }
8809
a36eb9ee 8810 /* Match the binding name; depending on type (operator / generic) format
8811 it for future error messages into bind_name. */
8db94b3b 8812
a36eb9ee 8813 m = gfc_match_generic_spec (&op_type, name, &op);
e2f06a48 8814 if (m == MATCH_ERROR)
8815 return MATCH_ERROR;
8816 if (m == MATCH_NO)
8817 {
a36eb9ee 8818 gfc_error ("Expected generic name or operator descriptor at %C");
e2f06a48 8819 goto error;
8820 }
8821
a36eb9ee 8822 switch (op_type)
e2f06a48 8823 {
a36eb9ee 8824 case INTERFACE_GENERIC:
8825 snprintf (bind_name, sizeof (bind_name), "%s", name);
8826 break;
8db94b3b 8827
a36eb9ee 8828 case INTERFACE_USER_OP:
8829 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8830 break;
8db94b3b 8831
a36eb9ee 8832 case INTERFACE_INTRINSIC_OP:
8833 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8834 gfc_op2string (op));
8835 break;
8836
7e8f55c1 8837 case INTERFACE_NAMELESS:
8838 gfc_error ("Malformed GENERIC statement at %C");
8839 goto error;
8840 break;
8841
a36eb9ee 8842 default:
8843 gcc_unreachable ();
8844 }
3323e9b1 8845
a36eb9ee 8846 /* Match the required =>. */
8847 if (gfc_match (" =>") != MATCH_YES)
8848 {
0d2b3c9c 8849 gfc_error ("Expected %<=>%> at %C");
a36eb9ee 8850 goto error;
8851 }
8db94b3b 8852
a36eb9ee 8853 /* Try to find existing GENERIC binding with this name / for this operator;
8854 if there is something, check that it is another GENERIC and then extend
8855 it rather than building a new node. Otherwise, create it and put it
8856 at the right position. */
8857
8858 switch (op_type)
8859 {
8860 case INTERFACE_USER_OP:
8861 case INTERFACE_GENERIC:
8862 {
8863 const bool is_op = (op_type == INTERFACE_USER_OP);
8864 gfc_symtree* st;
8865
8866 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8867 if (st)
8868 {
8869 tb = st->n.tb;
8870 gcc_assert (tb);
8871 }
8872 else
8873 tb = NULL;
8874
8875 break;
8876 }
8877
8878 case INTERFACE_INTRINSIC_OP:
8879 tb = ns->tb_op[op];
8880 break;
8881
8882 default:
8883 gcc_unreachable ();
8884 }
8885
8886 if (tb)
8887 {
3323e9b1 8888 if (!tb->is_generic)
e2f06a48 8889 {
a36eb9ee 8890 gcc_assert (op_type == INTERFACE_GENERIC);
e2f06a48 8891 gfc_error ("There's already a non-generic procedure with binding name"
716da296 8892 " %qs for the derived type %qs at %C",
a36eb9ee 8893 bind_name, block->name);
e2f06a48 8894 goto error;
8895 }
8896
e2f06a48 8897 if (tb->access != tbattr.access)
8898 {
8899 gfc_error ("Binding at %C must have the same access as already"
716da296 8900 " defined binding %qs", bind_name);
e2f06a48 8901 goto error;
8902 }
8903 }
8904 else
8905 {
4a12b9ba 8906 tb = gfc_get_typebound_proc (NULL);
e2f06a48 8907 tb->where = gfc_current_locus;
8908 tb->access = tbattr.access;
8909 tb->is_generic = 1;
8910 tb->u.generic = NULL;
a36eb9ee 8911
8912 switch (op_type)
8913 {
8914 case INTERFACE_GENERIC:
8915 case INTERFACE_USER_OP:
8916 {
8917 const bool is_op = (op_type == INTERFACE_USER_OP);
8918 gfc_symtree* st;
8919
8920 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8921 name);
8922 gcc_assert (st);
8923 st->n.tb = tb;
8924
8925 break;
8926 }
8db94b3b 8927
a36eb9ee 8928 case INTERFACE_INTRINSIC_OP:
8929 ns->tb_op[op] = tb;
8930 break;
8931
8932 default:
8933 gcc_unreachable ();
8934 }
e2f06a48 8935 }
8936
8937 /* Now, match all following names as specific targets. */
8938 do
8939 {
8940 gfc_symtree* target_st;
8941 gfc_tbp_generic* target;
8942
8943 m = gfc_match_name (name);
8944 if (m == MATCH_ERROR)
8945 goto error;
8946 if (m == MATCH_NO)
8947 {
8948 gfc_error ("Expected specific binding name at %C");
8949 goto error;
8950 }
8951
3323e9b1 8952 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
e2f06a48 8953
8954 /* See if this is a duplicate specification. */
8955 for (target = tb->u.generic; target; target = target->next)
8956 if (target_st == target->specific_st)
8957 {
716da296 8958 gfc_error ("%qs already defined as specific binding for the"
8959 " generic %qs at %C", name, bind_name);
e2f06a48 8960 goto error;
8961 }
8962
e2f06a48 8963 target = gfc_get_tbp_generic ();
8964 target->specific_st = target_st;
8965 target->specific = NULL;
8966 target->next = tb->u.generic;
5c0f7d99 8967 target->is_operator = ((op_type == INTERFACE_USER_OP)
8968 || (op_type == INTERFACE_INTRINSIC_OP));
e2f06a48 8969 tb->u.generic = target;
8970 }
8971 while (gfc_match (" ,") == MATCH_YES);
8972
8973 /* Here should be the end. */
8974 if (gfc_match_eos () != MATCH_YES)
8975 {
8976 gfc_error ("Junk after GENERIC binding at %C");
8977 goto error;
8978 }
8979
8980 return MATCH_YES;
8981
8982error:
8983 return MATCH_ERROR;
8984}
8985
8986
223f0f57 8987/* Match a FINAL declaration inside a derived type. */
8988
8989match
8990gfc_match_final_decl (void)
8991{
8992 char name[GFC_MAX_SYMBOL_LEN + 1];
8993 gfc_symbol* sym;
8994 match m;
8995 gfc_namespace* module_ns;
8996 bool first, last;
7fd88f6e 8997 gfc_symbol* block;
223f0f57 8998
519651c1 8999 if (gfc_current_form == FORM_FREE)
9000 {
9001 char c = gfc_peek_ascii_char ();
9002 if (!gfc_is_whitespace (c) && c != ':')
9003 return MATCH_NO;
9004 }
8db94b3b 9005
7fd88f6e 9006 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
223f0f57 9007 {
519651c1 9008 if (gfc_current_form == FORM_FIXED)
9009 return MATCH_NO;
9010
223f0f57 9011 gfc_error ("FINAL declaration at %C must be inside a derived type "
7fd88f6e 9012 "CONTAINS section");
223f0f57 9013 return MATCH_ERROR;
9014 }
9015
7fd88f6e 9016 block = gfc_state_stack->previous->sym;
9017 gcc_assert (block);
223f0f57 9018
7fd88f6e 9019 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
9020 || gfc_state_stack->previous->previous->state != COMP_MODULE)
223f0f57 9021 {
9022 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9023 " specification part of a MODULE");
9024 return MATCH_ERROR;
9025 }
9026
9027 module_ns = gfc_current_ns;
9028 gcc_assert (module_ns);
9029 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
9030
9031 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9032 if (gfc_match (" ::") == MATCH_ERROR)
9033 return MATCH_ERROR;
9034
9035 /* Match the sequence of procedure names. */
9036 first = true;
9037 last = false;
9038 do
9039 {
9040 gfc_finalizer* f;
9041
9042 if (first && gfc_match_eos () == MATCH_YES)
9043 {
9044 gfc_error ("Empty FINAL at %C");
9045 return MATCH_ERROR;
9046 }
9047
9048 m = gfc_match_name (name);
9049 if (m == MATCH_NO)
9050 {
9051 gfc_error ("Expected module procedure name at %C");
9052 return MATCH_ERROR;
9053 }
9054 else if (m != MATCH_YES)
9055 return MATCH_ERROR;
9056
9057 if (gfc_match_eos () == MATCH_YES)
9058 last = true;
9059 if (!last && gfc_match_char (',') != MATCH_YES)
9060 {
0d2b3c9c 9061 gfc_error ("Expected %<,%> at %C");
223f0f57 9062 return MATCH_ERROR;
9063 }
9064
9065 if (gfc_get_symbol (name, module_ns, &sym))
9066 {
716da296 9067 gfc_error ("Unknown procedure name %qs at %C", name);
223f0f57 9068 return MATCH_ERROR;
9069 }
9070
9071 /* Mark the symbol as module procedure. */
9072 if (sym->attr.proc != PROC_MODULE
60e19868 9073 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
223f0f57 9074 return MATCH_ERROR;
9075
9076 /* Check if we already have this symbol in the list, this is an error. */
7fd88f6e 9077 for (f = block->f2k_derived->finalizers; f; f = f->next)
e449e4dd 9078 if (f->proc_sym == sym)
223f0f57 9079 {
716da296 9080 gfc_error ("%qs at %C is already defined as FINAL procedure!",
223f0f57 9081 name);
9082 return MATCH_ERROR;
9083 }
9084
9085 /* Add this symbol to the list of finalizers. */
7fd88f6e 9086 gcc_assert (block->f2k_derived);
c77c84dd 9087 sym->refs++;
48d8ad5a 9088 f = XCNEW (gfc_finalizer);
e449e4dd 9089 f->proc_sym = sym;
9090 f->proc_tree = NULL;
223f0f57 9091 f->where = gfc_current_locus;
7fd88f6e 9092 f->next = block->f2k_derived->finalizers;
9093 block->f2k_derived->finalizers = f;
223f0f57 9094
9095 first = false;
9096 }
9097 while (!last);
9098
9099 return MATCH_YES;
9100}
36b0a1b0 9101
9102
9103const ext_attr_t ext_attr_list[] = {
fa76a552 9104 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
9105 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
9106 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
9107 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
9108 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
9109 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
9110 { NULL, EXT_ATTR_LAST, NULL }
36b0a1b0 9111};
9112
9113/* Match a !GCC$ ATTRIBUTES statement of the form:
9114 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9115 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9116
9117 TODO: We should support all GCC attributes using the same syntax for
9118 the attribute list, i.e. the list in C
9119 __attributes(( attribute-list ))
9120 matches then
9121 !GCC$ ATTRIBUTES attribute-list ::
9122 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9123 saved into a TREE.
9124
9125 As there is absolutely no risk of confusion, we should never return
9126 MATCH_NO. */
9127match
9128gfc_match_gcc_attributes (void)
8db94b3b 9129{
36b0a1b0 9130 symbol_attribute attr;
9131 char name[GFC_MAX_SYMBOL_LEN + 1];
9132 unsigned id;
9133 gfc_symbol *sym;
9134 match m;
9135
9136 gfc_clear_attr (&attr);
9137 for(;;)
9138 {
9139 char ch;
9140
9141 if (gfc_match_name (name) != MATCH_YES)
9142 return MATCH_ERROR;
9143
9144 for (id = 0; id < EXT_ATTR_LAST; id++)
9145 if (strcmp (name, ext_attr_list[id].name) == 0)
9146 break;
9147
9148 if (id == EXT_ATTR_LAST)
9149 {
9150 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9151 return MATCH_ERROR;
9152 }
9153
60e19868 9154 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
36b0a1b0 9155 return MATCH_ERROR;
9156
9157 gfc_gobble_whitespace ();
9158 ch = gfc_next_ascii_char ();
9159 if (ch == ':')
9160 {
9161 /* This is the successful exit condition for the loop. */
9162 if (gfc_next_ascii_char () == ':')
9163 break;
9164 }
9165
9166 if (ch == ',')
9167 continue;
9168
9169 goto syntax;
9170 }
9171
9172 if (gfc_match_eos () == MATCH_YES)
9173 goto syntax;
9174
9175 for(;;)
9176 {
9177 m = gfc_match_name (name);
9178 if (m != MATCH_YES)
9179 return m;
9180
9181 if (find_special (name, &sym, true))
9182 return MATCH_ERROR;
8db94b3b 9183
36b0a1b0 9184 sym->attr.ext_attr |= attr.ext_attr;
9185
9186 if (gfc_match_eos () == MATCH_YES)
9187 break;
9188
9189 if (gfc_match_char (',') != MATCH_YES)
9190 goto syntax;
9191 }
9192
9193 return MATCH_YES;
9194
9195syntax:
9196 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
9197 return MATCH_ERROR;
9198}