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