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