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