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