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