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