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