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