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