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