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