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