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