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