]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/decl.c
re PR c++/31941 ("confused by earlier errors" message without earlier error message)
[thirdparty/gcc.git] / gcc / fortran / decl.c
CommitLineData
6de9cd9a 1/* Declaration statement matcher
636dff67
SK
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
6de9cd9a
DN
4 Contributed by Andy Vaught
5
9fc4d79b 6This file is part of GCC.
6de9cd9a 7
9fc4d79b
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
d234d788 10Software Foundation; either version 3, or (at your option) any later
9fc4d79b 11version.
6de9cd9a 12
9fc4d79b
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a 21
6de9cd9a 22#include "config.h"
d22e4895 23#include "system.h"
6de9cd9a
DN
24#include "gfortran.h"
25#include "match.h"
26#include "parse.h"
6de9cd9a 27
2054fc29 28/* This flag is set if an old-style length selector is matched
6de9cd9a
DN
29 during a type-declaration statement. */
30
31static int old_char_selector;
32
46fa431d 33/* When variables acquire types and attributes from a declaration
6de9cd9a
DN
34 statement, they get them from the following static variables. The
35 first part of a declaration sets these variables and the second
36 part copies these into symbol structures. */
37
38static gfc_typespec current_ts;
39
40static symbol_attribute current_attr;
41static gfc_array_spec *current_as;
42static int colon_seen;
43
a8b3b0b6
CR
44/* The current binding label (if any). */
45static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
46/* Need to know how many identifiers are on the current data declaration
47 line in case we're given the BIND(C) attribute with a NAME= specifier. */
48static int num_idents_on_line;
49/* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
50 can supply a name if the curr_binding_label is nil and NAME= was not. */
51static int has_name_equals = 0;
52
25d8f0a2
TS
53/* Initializer of the previous enumerator. */
54
55static gfc_expr *last_initializer;
56
57/* History of all the enumerators is maintained, so that
58 kind values of all the enumerators could be updated depending
59 upon the maximum initialized value. */
60
61typedef struct enumerator_history
62{
63 gfc_symbol *sym;
64 gfc_expr *initializer;
65 struct enumerator_history *next;
66}
67enumerator_history;
68
69/* Header of enum history chain. */
70
71static enumerator_history *enum_history = NULL;
72
73/* Pointer of enum history node containing largest initializer. */
74
75static enumerator_history *max_enum = NULL;
76
6de9cd9a
DN
77/* gfc_new_block points to the symbol of a newly matched block. */
78
79gfc_symbol *gfc_new_block;
80
81
294fbfc8
TS
82/********************* DATA statement subroutines *********************/
83
2220652d
PT
84static bool in_match_data = false;
85
86bool
87gfc_in_match_data (void)
88{
89 return in_match_data;
90}
91
92void
93gfc_set_in_match_data (bool set_value)
94{
95 in_match_data = set_value;
96}
97
294fbfc8
TS
98/* Free a gfc_data_variable structure and everything beneath it. */
99
100static void
636dff67 101free_variable (gfc_data_variable *p)
294fbfc8
TS
102{
103 gfc_data_variable *q;
104
105 for (; p; p = q)
106 {
107 q = p->next;
108 gfc_free_expr (p->expr);
109 gfc_free_iterator (&p->iter, 0);
110 free_variable (p->list);
294fbfc8
TS
111 gfc_free (p);
112 }
113}
114
115
116/* Free a gfc_data_value structure and everything beneath it. */
117
118static void
636dff67 119free_value (gfc_data_value *p)
294fbfc8
TS
120{
121 gfc_data_value *q;
122
123 for (; p; p = q)
124 {
125 q = p->next;
126 gfc_free_expr (p->expr);
127 gfc_free (p);
128 }
129}
130
131
132/* Free a list of gfc_data structures. */
133
134void
636dff67 135gfc_free_data (gfc_data *p)
294fbfc8
TS
136{
137 gfc_data *q;
138
139 for (; p; p = q)
140 {
141 q = p->next;
294fbfc8
TS
142 free_variable (p->var);
143 free_value (p->value);
294fbfc8
TS
144 gfc_free (p);
145 }
146}
147
148
a9f6f1f2 149/* Free all data in a namespace. */
636dff67 150
a9f6f1f2 151static void
66e4ab31 152gfc_free_data_all (gfc_namespace *ns)
a9f6f1f2
JD
153{
154 gfc_data *d;
155
156 for (;ns->data;)
157 {
158 d = ns->data->next;
159 gfc_free (ns->data);
160 ns->data = d;
161 }
162}
163
164
294fbfc8
TS
165static match var_element (gfc_data_variable *);
166
167/* Match a list of variables terminated by an iterator and a right
168 parenthesis. */
169
170static match
636dff67 171var_list (gfc_data_variable *parent)
294fbfc8
TS
172{
173 gfc_data_variable *tail, var;
174 match m;
175
176 m = var_element (&var);
177 if (m == MATCH_ERROR)
178 return MATCH_ERROR;
179 if (m == MATCH_NO)
180 goto syntax;
181
182 tail = gfc_get_data_variable ();
183 *tail = var;
184
185 parent->list = tail;
186
187 for (;;)
188 {
189 if (gfc_match_char (',') != MATCH_YES)
190 goto syntax;
191
192 m = gfc_match_iterator (&parent->iter, 1);
193 if (m == MATCH_YES)
194 break;
195 if (m == MATCH_ERROR)
196 return MATCH_ERROR;
197
198 m = var_element (&var);
199 if (m == MATCH_ERROR)
200 return MATCH_ERROR;
201 if (m == MATCH_NO)
202 goto syntax;
203
204 tail->next = gfc_get_data_variable ();
205 tail = tail->next;
206
207 *tail = var;
208 }
209
210 if (gfc_match_char (')') != MATCH_YES)
211 goto syntax;
212 return MATCH_YES;
213
214syntax:
215 gfc_syntax_error (ST_DATA);
216 return MATCH_ERROR;
217}
218
219
220/* Match a single element in a data variable list, which can be a
221 variable-iterator list. */
222
223static match
636dff67 224var_element (gfc_data_variable *new)
294fbfc8
TS
225{
226 match m;
227 gfc_symbol *sym;
228
229 memset (new, 0, sizeof (gfc_data_variable));
230
231 if (gfc_match_char ('(') == MATCH_YES)
232 return var_list (new);
233
234 m = gfc_match_variable (&new->expr, 0);
235 if (m != MATCH_YES)
236 return m;
237
238 sym = new->expr->symtree->n.sym;
239
636dff67
SK
240 if (!sym->attr.function && gfc_current_ns->parent
241 && gfc_current_ns->parent == sym->ns)
294fbfc8 242 {
4075a94e 243 gfc_error ("Host associated variable '%s' may not be in the DATA "
e25a0da3 244 "statement at %C", sym->name);
294fbfc8
TS
245 return MATCH_ERROR;
246 }
247
4075a94e 248 if (gfc_current_state () != COMP_BLOCK_DATA
636dff67
SK
249 && sym->attr.in_common
250 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
251 "common block variable '%s' in DATA statement at %C",
252 sym->name) == FAILURE)
4075a94e 253 return MATCH_ERROR;
294fbfc8 254
231b2fcc 255 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
294fbfc8
TS
256 return MATCH_ERROR;
257
258 return MATCH_YES;
259}
260
261
262/* Match the top-level list of data variables. */
263
264static match
636dff67 265top_var_list (gfc_data *d)
294fbfc8
TS
266{
267 gfc_data_variable var, *tail, *new;
268 match m;
269
270 tail = NULL;
271
272 for (;;)
273 {
274 m = var_element (&var);
275 if (m == MATCH_NO)
276 goto syntax;
277 if (m == MATCH_ERROR)
278 return MATCH_ERROR;
279
280 new = gfc_get_data_variable ();
281 *new = var;
282
283 if (tail == NULL)
284 d->var = new;
285 else
286 tail->next = new;
287
288 tail = new;
289
290 if (gfc_match_char ('/') == MATCH_YES)
291 break;
292 if (gfc_match_char (',') != MATCH_YES)
293 goto syntax;
294 }
295
296 return MATCH_YES;
297
298syntax:
299 gfc_syntax_error (ST_DATA);
a9f6f1f2 300 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
301 return MATCH_ERROR;
302}
303
304
305static match
636dff67 306match_data_constant (gfc_expr **result)
294fbfc8
TS
307{
308 char name[GFC_MAX_SYMBOL_LEN + 1];
309 gfc_symbol *sym;
310 gfc_expr *expr;
311 match m;
36d3fb4c 312 locus old_loc;
294fbfc8
TS
313
314 m = gfc_match_literal_constant (&expr, 1);
315 if (m == MATCH_YES)
316 {
317 *result = expr;
318 return MATCH_YES;
319 }
320
321 if (m == MATCH_ERROR)
322 return MATCH_ERROR;
323
324 m = gfc_match_null (result);
325 if (m != MATCH_NO)
326 return m;
327
36d3fb4c
PT
328 old_loc = gfc_current_locus;
329
330 /* Should this be a structure component, try to match it
331 before matching a name. */
332 m = gfc_match_rvalue (result);
333 if (m == MATCH_ERROR)
334 return m;
335
336 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
337 {
338 if (gfc_simplify_expr (*result, 0) == FAILURE)
339 m = MATCH_ERROR;
340 return m;
341 }
342
343 gfc_current_locus = old_loc;
344
294fbfc8
TS
345 m = gfc_match_name (name);
346 if (m != MATCH_YES)
347 return m;
348
349 if (gfc_find_symbol (name, NULL, 1, &sym))
350 return MATCH_ERROR;
351
352 if (sym == NULL
353 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
354 {
355 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
356 name);
357 return MATCH_ERROR;
358 }
359 else if (sym->attr.flavor == FL_DERIVED)
360 return gfc_match_structure_constructor (sym, result);
361
362 *result = gfc_copy_expr (sym->value);
363 return MATCH_YES;
364}
365
366
367/* Match a list of values in a DATA statement. The leading '/' has
368 already been seen at this point. */
369
370static match
636dff67 371top_val_list (gfc_data *data)
294fbfc8
TS
372{
373 gfc_data_value *new, *tail;
374 gfc_expr *expr;
375 const char *msg;
376 match m;
377
378 tail = NULL;
379
380 for (;;)
381 {
382 m = match_data_constant (&expr);
383 if (m == MATCH_NO)
384 goto syntax;
385 if (m == MATCH_ERROR)
386 return MATCH_ERROR;
387
388 new = gfc_get_data_value ();
389
390 if (tail == NULL)
391 data->value = new;
392 else
393 tail->next = new;
394
395 tail = new;
396
397 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
398 {
399 tail->expr = expr;
400 tail->repeat = 1;
401 }
402 else
403 {
404 signed int tmp;
405 msg = gfc_extract_int (expr, &tmp);
406 gfc_free_expr (expr);
407 if (msg != NULL)
408 {
409 gfc_error (msg);
410 return MATCH_ERROR;
411 }
412 tail->repeat = tmp;
413
414 m = match_data_constant (&tail->expr);
415 if (m == MATCH_NO)
416 goto syntax;
417 if (m == MATCH_ERROR)
418 return MATCH_ERROR;
419 }
420
421 if (gfc_match_char ('/') == MATCH_YES)
422 break;
423 if (gfc_match_char (',') == MATCH_NO)
424 goto syntax;
425 }
426
427 return MATCH_YES;
428
429syntax:
430 gfc_syntax_error (ST_DATA);
a9f6f1f2 431 gfc_free_data_all (gfc_current_ns);
294fbfc8
TS
432 return MATCH_ERROR;
433}
434
435
436/* Matches an old style initialization. */
437
438static match
439match_old_style_init (const char *name)
440{
441 match m;
442 gfc_symtree *st;
ed0e3607 443 gfc_symbol *sym;
294fbfc8
TS
444 gfc_data *newdata;
445
446 /* Set up data structure to hold initializers. */
447 gfc_find_sym_tree (name, NULL, 0, &st);
ed0e3607
AL
448 sym = st->n.sym;
449
294fbfc8
TS
450 newdata = gfc_get_data ();
451 newdata->var = gfc_get_data_variable ();
452 newdata->var->expr = gfc_get_variable_expr (st);
8c5c0b80 453 newdata->where = gfc_current_locus;
294fbfc8 454
66e4ab31 455 /* Match initial value list. This also eats the terminal '/'. */
294fbfc8
TS
456 m = top_val_list (newdata);
457 if (m != MATCH_YES)
458 {
459 gfc_free (newdata);
460 return m;
461 }
462
463 if (gfc_pure (NULL))
464 {
465 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
466 gfc_free (newdata);
467 return MATCH_ERROR;
468 }
469
ed0e3607
AL
470 /* Mark the variable as having appeared in a data statement. */
471 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
472 {
473 gfc_free (newdata);
474 return MATCH_ERROR;
475 }
476
294fbfc8
TS
477 /* Chain in namespace list of DATA initializers. */
478 newdata->next = gfc_current_ns->data;
479 gfc_current_ns->data = newdata;
480
481 return m;
482}
483
636dff67 484
294fbfc8 485/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
13795658 486 we are matching a DATA statement and are therefore issuing an error
d51347f9 487 if we encounter something unexpected, if not, we're trying to match
69de3b83 488 an old-style initialization expression of the form INTEGER I /2/. */
294fbfc8
TS
489
490match
491gfc_match_data (void)
492{
493 gfc_data *new;
494 match m;
495
2220652d
PT
496 gfc_set_in_match_data (true);
497
294fbfc8
TS
498 for (;;)
499 {
500 new = gfc_get_data ();
501 new->where = gfc_current_locus;
502
503 m = top_var_list (new);
504 if (m != MATCH_YES)
505 goto cleanup;
506
507 m = top_val_list (new);
508 if (m != MATCH_YES)
509 goto cleanup;
510
511 new->next = gfc_current_ns->data;
512 gfc_current_ns->data = new;
513
514 if (gfc_match_eos () == MATCH_YES)
515 break;
516
517 gfc_match_char (','); /* Optional comma */
518 }
519
2220652d
PT
520 gfc_set_in_match_data (false);
521
294fbfc8
TS
522 if (gfc_pure (NULL))
523 {
524 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
525 return MATCH_ERROR;
526 }
527
528 return MATCH_YES;
529
530cleanup:
2220652d 531 gfc_set_in_match_data (false);
294fbfc8
TS
532 gfc_free_data (new);
533 return MATCH_ERROR;
534}
535
536
537/************************ Declaration statements *********************/
538
6de9cd9a
DN
539/* Match an intent specification. Since this can only happen after an
540 INTENT word, a legal intent-spec must follow. */
541
542static sym_intent
543match_intent_spec (void)
544{
545
546 if (gfc_match (" ( in out )") == MATCH_YES)
547 return INTENT_INOUT;
548 if (gfc_match (" ( in )") == MATCH_YES)
549 return INTENT_IN;
550 if (gfc_match (" ( out )") == MATCH_YES)
551 return INTENT_OUT;
552
553 gfc_error ("Bad INTENT specification at %C");
554 return INTENT_UNKNOWN;
555}
556
557
558/* Matches a character length specification, which is either a
559 specification expression or a '*'. */
560
561static match
636dff67 562char_len_param_value (gfc_expr **expr)
6de9cd9a 563{
6de9cd9a
DN
564 if (gfc_match_char ('*') == MATCH_YES)
565 {
566 *expr = NULL;
567 return MATCH_YES;
568 }
569
570 return gfc_match_expr (expr);
571}
572
573
574/* A character length is a '*' followed by a literal integer or a
575 char_len_param_value in parenthesis. */
576
577static match
636dff67 578match_char_length (gfc_expr **expr)
6de9cd9a 579{
5cf54585 580 int length;
6de9cd9a
DN
581 match m;
582
583 m = gfc_match_char ('*');
584 if (m != MATCH_YES)
585 return m;
586
5cf54585 587 m = gfc_match_small_literal_int (&length, NULL);
6de9cd9a
DN
588 if (m == MATCH_ERROR)
589 return m;
590
591 if (m == MATCH_YES)
592 {
593 *expr = gfc_int_expr (length);
594 return m;
595 }
596
597 if (gfc_match_char ('(') == MATCH_NO)
598 goto syntax;
599
600 m = char_len_param_value (expr);
601 if (m == MATCH_ERROR)
602 return m;
603 if (m == MATCH_NO)
604 goto syntax;
605
606 if (gfc_match_char (')') == MATCH_NO)
607 {
608 gfc_free_expr (*expr);
609 *expr = NULL;
610 goto syntax;
611 }
612
613 return MATCH_YES;
614
615syntax:
616 gfc_error ("Syntax error in character length specification at %C");
617 return MATCH_ERROR;
618}
619
620
9e35b386
EE
621/* Special subroutine for finding a symbol. Check if the name is found
622 in the current name space. If not, and we're compiling a function or
623 subroutine and the parent compilation unit is an interface, then check
624 to see if the name we've been given is the name of the interface
625 (located in another namespace). */
6de9cd9a
DN
626
627static int
636dff67 628find_special (const char *name, gfc_symbol **result)
6de9cd9a
DN
629{
630 gfc_state_data *s;
9e35b386 631 int i;
6de9cd9a 632
9e35b386 633 i = gfc_get_symbol (name, NULL, result);
d51347f9 634 if (i == 0)
9e35b386 635 goto end;
d51347f9 636
6de9cd9a
DN
637 if (gfc_current_state () != COMP_SUBROUTINE
638 && gfc_current_state () != COMP_FUNCTION)
9e35b386 639 goto end;
6de9cd9a
DN
640
641 s = gfc_state_stack->previous;
642 if (s == NULL)
9e35b386 643 goto end;
6de9cd9a
DN
644
645 if (s->state != COMP_INTERFACE)
9e35b386 646 goto end;
6de9cd9a 647 if (s->sym == NULL)
66e4ab31 648 goto end; /* Nameless interface. */
6de9cd9a
DN
649
650 if (strcmp (name, s->sym->name) == 0)
651 {
652 *result = s->sym;
653 return 0;
654 }
655
9e35b386
EE
656end:
657 return i;
6de9cd9a
DN
658}
659
660
661/* Special subroutine for getting a symbol node associated with a
662 procedure name, used in SUBROUTINE and FUNCTION statements. The
663 symbol is created in the parent using with symtree node in the
664 child unit pointing to the symbol. If the current namespace has no
665 parent, then the symbol is just created in the current unit. */
666
667static int
636dff67 668get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
6de9cd9a
DN
669{
670 gfc_symtree *st;
671 gfc_symbol *sym;
672 int rc;
673
1a492601
PT
674 /* Module functions have to be left in their own namespace because
675 they have potentially (almost certainly!) already been referenced.
676 In this sense, they are rather like external functions. This is
677 fixed up in resolve.c(resolve_entries), where the symbol name-
678 space is set to point to the master function, so that the fake
679 result mechanism can work. */
680 if (module_fcn_entry)
6c12686b
PT
681 {
682 /* Present if entry is declared to be a module procedure. */
683 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
aa84a9a5 684
6c12686b
PT
685 if (*result == NULL)
686 rc = gfc_get_symbol (name, NULL, result);
aa84a9a5
PT
687 else if (gfc_get_symbol (name, NULL, &sym) == 0
688 && sym
689 && sym->ts.type != BT_UNKNOWN
690 && (*result)->ts.type == BT_UNKNOWN
691 && sym->attr.flavor == FL_UNKNOWN)
692 /* Pick up the typespec for the entry, if declared in the function
693 body. Note that this symbol is FL_UNKNOWN because it will
694 only have appeared in a type declaration. The local symtree
695 is set to point to the module symbol and a unique symtree
696 to the local version. This latter ensures a correct clearing
697 of the symbols. */
698 {
699 (*result)->ts = sym->ts;
700 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
701 st->n.sym = *result;
702 st = gfc_get_unique_symtree (gfc_current_ns);
703 st->n.sym = sym;
704 }
6c12686b 705 }
68ea355b
PT
706 else
707 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
6de9cd9a 708
68ea355b 709 sym = *result;
2c693a24 710 gfc_current_ns->refs++;
6de9cd9a 711
68ea355b
PT
712 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
713 {
cda7004b
PT
714 /* Trap another encompassed procedure with the same name. All
715 these conditions are necessary to avoid picking up an entry
716 whose name clashes with that of the encompassing procedure;
717 this is handled using gsymbols to register unique,globally
718 accessible names. */
68ea355b 719 if (sym->attr.flavor != 0
636dff67
SK
720 && sym->attr.proc != 0
721 && (sym->attr.subroutine || sym->attr.function)
722 && sym->attr.if_source != IFSRC_UNKNOWN)
68ea355b
PT
723 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
724 name, &sym->declared_at);
725
fd3e70af
JD
726 /* Trap a procedure with a name the same as interface in the
727 encompassing scope. */
728 if (sym->attr.generic != 0
2305fa31
JD
729 && (sym->attr.subroutine || sym->attr.function)
730 && !sym->attr.mod_proc)
fd3e70af
JD
731 gfc_error_now ("Name '%s' at %C is already defined"
732 " as a generic interface at %L",
733 name, &sym->declared_at);
734
68ea355b
PT
735 /* Trap declarations of attributes in encompassing scope. The
736 signature for this is that ts.kind is set. Legitimate
737 references only set ts.type. */
738 if (sym->ts.kind != 0
636dff67
SK
739 && !sym->attr.implicit_type
740 && sym->attr.proc == 0
741 && gfc_current_ns->parent != NULL
742 && sym->attr.access == 0
743 && !module_fcn_entry)
744 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
745 "and must not have attributes declared at %L",
68ea355b
PT
746 name, &sym->declared_at);
747 }
748
749 if (gfc_current_ns->parent == NULL || *result == NULL)
750 return rc;
6de9cd9a 751
1a492601
PT
752 /* Module function entries will already have a symtree in
753 the current namespace but will need one at module level. */
754 if (module_fcn_entry)
6c12686b
PT
755 {
756 /* Present if entry is declared to be a module procedure. */
757 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
758 if (st == NULL)
759 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
760 }
1a492601
PT
761 else
762 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
6de9cd9a 763
6de9cd9a
DN
764 st->n.sym = sym;
765 sym->refs++;
766
66e4ab31 767 /* See if the procedure should be a module procedure. */
6de9cd9a 768
1a492601 769 if (((sym->ns->proc_name != NULL
6c12686b
PT
770 && sym->ns->proc_name->attr.flavor == FL_MODULE
771 && sym->attr.proc != PROC_MODULE)
772 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
773 && gfc_add_procedure (&sym->attr, PROC_MODULE,
774 sym->name, NULL) == FAILURE)
6de9cd9a
DN
775 rc = 2;
776
777 return rc;
778}
779
780
a8b3b0b6
CR
781/* Verify that the given symbol representing a parameter is C
782 interoperable, by checking to see if it was marked as such after
783 its declaration. If the given symbol is not interoperable, a
784 warning is reported, thus removing the need to return the status to
785 the calling function. The standard does not require the user use
786 one of the iso_c_binding named constants to declare an
787 interoperable parameter, but we can't be sure if the param is C
788 interop or not if the user doesn't. For example, integer(4) may be
789 legal Fortran, but doesn't have meaning in C. It may interop with
790 a number of the C types, which causes a problem because the
791 compiler can't know which one. This code is almost certainly not
792 portable, and the user will get what they deserve if the C type
793 across platforms isn't always interoperable with integer(4). If
794 the user had used something like integer(c_int) or integer(c_long),
795 the compiler could have automatically handled the varying sizes
796 across platforms. */
797
798try
799verify_c_interop_param (gfc_symbol *sym)
800{
801 int is_c_interop = 0;
802 try retval = SUCCESS;
803
804 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
805 Don't repeat the checks here. */
806 if (sym->attr.implicit_type)
807 return SUCCESS;
808
809 /* For subroutines or functions that are passed to a BIND(C) procedure,
810 they're interoperable if they're BIND(C) and their params are all
811 interoperable. */
812 if (sym->attr.flavor == FL_PROCEDURE)
813 {
814 if (sym->attr.is_bind_c == 0)
815 {
816 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
817 "attribute to be C interoperable", sym->name,
818 &(sym->declared_at));
819
820 return FAILURE;
821 }
822 else
823 {
824 if (sym->attr.is_c_interop == 1)
825 /* We've already checked this procedure; don't check it again. */
826 return SUCCESS;
827 else
828 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
829 sym->common_block);
830 }
831 }
832
833 /* See if we've stored a reference to a procedure that owns sym. */
834 if (sym->ns != NULL && sym->ns->proc_name != NULL)
835 {
836 if (sym->ns->proc_name->attr.is_bind_c == 1)
837 {
838 is_c_interop =
839 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
840 == SUCCESS ? 1 : 0);
841
842 if (is_c_interop != 1)
843 {
844 /* Make personalized messages to give better feedback. */
845 if (sym->ts.type == BT_DERIVED)
846 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
847 " procedure '%s' but is not C interoperable "
848 "because derived type '%s' is not C interoperable",
849 sym->name, &(sym->declared_at),
850 sym->ns->proc_name->name,
851 sym->ts.derived->name);
852 else
853 gfc_warning ("Variable '%s' at %L is a parameter to the "
854 "BIND(C) procedure '%s' but may not be C "
855 "interoperable",
856 sym->name, &(sym->declared_at),
857 sym->ns->proc_name->name);
858 }
aa5e22f0
CR
859
860 /* Character strings are only C interoperable if they have a
861 length of 1. */
862 if (sym->ts.type == BT_CHARACTER)
863 {
864 gfc_charlen *cl = sym->ts.cl;
865 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
866 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
867 {
868 gfc_error ("Character argument '%s' at %L "
869 "must be length 1 because "
870 "procedure '%s' is BIND(C)",
871 sym->name, &sym->declared_at,
872 sym->ns->proc_name->name);
873 retval = FAILURE;
874 }
875 }
876
a8b3b0b6
CR
877 /* We have to make sure that any param to a bind(c) routine does
878 not have the allocatable, pointer, or optional attributes,
879 according to J3/04-007, section 5.1. */
880 if (sym->attr.allocatable == 1)
881 {
882 gfc_error ("Variable '%s' at %L cannot have the "
883 "ALLOCATABLE attribute because procedure '%s'"
884 " is BIND(C)", sym->name, &(sym->declared_at),
885 sym->ns->proc_name->name);
886 retval = FAILURE;
887 }
888
889 if (sym->attr.pointer == 1)
890 {
891 gfc_error ("Variable '%s' at %L cannot have the "
892 "POINTER attribute because procedure '%s'"
893 " is BIND(C)", sym->name, &(sym->declared_at),
894 sym->ns->proc_name->name);
895 retval = FAILURE;
896 }
897
898 if (sym->attr.optional == 1)
899 {
900 gfc_error ("Variable '%s' at %L cannot have the "
901 "OPTIONAL attribute because procedure '%s'"
902 " is BIND(C)", sym->name, &(sym->declared_at),
903 sym->ns->proc_name->name);
904 retval = FAILURE;
905 }
906
907 /* Make sure that if it has the dimension attribute, that it is
908 either assumed size or explicit shape. */
909 if (sym->as != NULL)
910 {
911 if (sym->as->type == AS_ASSUMED_SHAPE)
912 {
913 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
914 "argument to the procedure '%s' at %L because "
915 "the procedure is BIND(C)", sym->name,
916 &(sym->declared_at), sym->ns->proc_name->name,
917 &(sym->ns->proc_name->declared_at));
918 retval = FAILURE;
919 }
920
921 if (sym->as->type == AS_DEFERRED)
922 {
923 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
924 "argument to the procedure '%s' at %L because "
925 "the procedure is BIND(C)", sym->name,
926 &(sym->declared_at), sym->ns->proc_name->name,
927 &(sym->ns->proc_name->declared_at));
928 retval = FAILURE;
929 }
930 }
931 }
932 }
933
934 return retval;
935}
936
937
938/* Function called by variable_decl() that adds a name to the symbol table. */
6de9cd9a
DN
939
940static try
636dff67
SK
941build_sym (const char *name, gfc_charlen *cl,
942 gfc_array_spec **as, locus *var_locus)
6de9cd9a
DN
943{
944 symbol_attribute attr;
945 gfc_symbol *sym;
946
9e35b386 947 if (gfc_get_symbol (name, NULL, &sym))
6de9cd9a
DN
948 return FAILURE;
949
66e4ab31 950 /* Start updating the symbol table. Add basic type attribute if present. */
6de9cd9a 951 if (current_ts.type != BT_UNKNOWN
636dff67
SK
952 && (sym->attr.implicit_type == 0
953 || !gfc_compare_types (&sym->ts, &current_ts))
6de9cd9a
DN
954 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
955 return FAILURE;
956
957 if (sym->ts.type == BT_CHARACTER)
958 sym->ts.cl = cl;
959
960 /* Add dimension attribute if present. */
961 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
962 return FAILURE;
963 *as = NULL;
964
965 /* Add attribute to symbol. The copy is so that we can reset the
966 dimension attribute. */
967 attr = current_attr;
968 attr.dimension = 0;
969
970 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
971 return FAILURE;
972
a8b3b0b6
CR
973 /* Finish any work that may need to be done for the binding label,
974 if it's a bind(c). The bind(c) attr is found before the symbol
975 is made, and before the symbol name (for data decls), so the
976 current_ts is holding the binding label, or nothing if the
977 name= attr wasn't given. Therefore, test here if we're dealing
978 with a bind(c) and make sure the binding label is set correctly. */
979 if (sym->attr.is_bind_c == 1)
980 {
981 if (sym->binding_label[0] == '\0')
982 {
983 /* Here, we're not checking the numIdents (the last param).
984 This could be an error we're letting slip through! */
985 if (set_binding_label (sym->binding_label, sym->name, 1) == FAILURE)
986 return FAILURE;
987 }
988 }
989
990 /* See if we know we're in a common block, and if it's a bind(c)
991 common then we need to make sure we're an interoperable type. */
992 if (sym->attr.in_common == 1)
993 {
994 /* Test the common block object. */
995 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
996 && sym->ts.is_c_interop != 1)
997 {
998 gfc_error_now ("Variable '%s' in common block '%s' at %C "
999 "must be declared with a C interoperable "
1000 "kind since common block '%s' is BIND(C)",
1001 sym->name, sym->common_block->name,
1002 sym->common_block->name);
1003 gfc_clear_error ();
1004 }
1005 }
1006
9a3db5a3
PT
1007 sym->attr.implied_index = 0;
1008
6de9cd9a
DN
1009 return SUCCESS;
1010}
1011
636dff67 1012
df7cc9b5
FW
1013/* Set character constant to the given length. The constant will be padded or
1014 truncated. */
1015
1016void
636dff67 1017gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
df7cc9b5 1018{
636dff67 1019 char *s;
df7cc9b5
FW
1020 int slen;
1021
1022 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1023 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1024
1025 slen = expr->value.character.length;
1026 if (len != slen)
1027 {
150675a8 1028 s = gfc_getmem (len + 1);
df7cc9b5
FW
1029 memcpy (s, expr->value.character.string, MIN (len, slen));
1030 if (len > slen)
1031 memset (&s[slen], ' ', len - slen);
2220652d
PT
1032
1033 if (gfc_option.warn_character_truncation && slen > len)
1034 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1035 "(%d/%d)", &expr->where, slen, len);
1036
1037 /* Apply the standard by 'hand' otherwise it gets cleared for
1038 initializers. */
1039 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1040 gfc_error_now ("The CHARACTER elements of the array constructor "
1041 "at %L must have the same length (%d/%d)",
636dff67 1042 &expr->where, slen, len);
2220652d 1043
150675a8 1044 s[len] = '\0';
df7cc9b5
FW
1045 gfc_free (expr->value.character.string);
1046 expr->value.character.string = s;
1047 expr->value.character.length = len;
1048 }
1049}
6de9cd9a 1050
25d8f0a2 1051
d51347f9 1052/* Function to create and update the enumerator history
25d8f0a2 1053 using the information passed as arguments.
d51347f9
TB
1054 Pointer "max_enum" is also updated, to point to
1055 enum history node containing largest initializer.
25d8f0a2
TS
1056
1057 SYM points to the symbol node of enumerator.
66e4ab31 1058 INIT points to its enumerator value. */
25d8f0a2 1059
d51347f9 1060static void
636dff67 1061create_enum_history (gfc_symbol *sym, gfc_expr *init)
25d8f0a2
TS
1062{
1063 enumerator_history *new_enum_history;
1064 gcc_assert (sym != NULL && init != NULL);
1065
1066 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1067
1068 new_enum_history->sym = sym;
1069 new_enum_history->initializer = init;
1070 new_enum_history->next = NULL;
1071
1072 if (enum_history == NULL)
1073 {
1074 enum_history = new_enum_history;
1075 max_enum = enum_history;
1076 }
1077 else
1078 {
1079 new_enum_history->next = enum_history;
1080 enum_history = new_enum_history;
1081
d51347f9 1082 if (mpz_cmp (max_enum->initializer->value.integer,
25d8f0a2 1083 new_enum_history->initializer->value.integer) < 0)
636dff67 1084 max_enum = new_enum_history;
25d8f0a2
TS
1085 }
1086}
1087
1088
d51347f9 1089/* Function to free enum kind history. */
25d8f0a2 1090
d51347f9 1091void
636dff67 1092gfc_free_enum_history (void)
25d8f0a2 1093{
d51347f9
TB
1094 enumerator_history *current = enum_history;
1095 enumerator_history *next;
25d8f0a2
TS
1096
1097 while (current != NULL)
1098 {
1099 next = current->next;
1100 gfc_free (current);
1101 current = next;
1102 }
1103 max_enum = NULL;
1104 enum_history = NULL;
1105}
1106
1107
6de9cd9a
DN
1108/* Function called by variable_decl() that adds an initialization
1109 expression to a symbol. */
1110
1111static try
66e4ab31 1112add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
6de9cd9a
DN
1113{
1114 symbol_attribute attr;
1115 gfc_symbol *sym;
1116 gfc_expr *init;
1117
1118 init = *initp;
1119 if (find_special (name, &sym))
1120 return FAILURE;
1121
1122 attr = sym->attr;
1123
1124 /* If this symbol is confirming an implicit parameter type,
1125 then an initialization expression is not allowed. */
1126 if (attr.flavor == FL_PARAMETER
1127 && sym->value != NULL
1128 && *initp != NULL)
1129 {
1130 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1131 sym->name);
1132 return FAILURE;
1133 }
1134
c8e20bd0
TS
1135 if (attr.in_common
1136 && !attr.data
1137 && *initp != NULL)
1138 {
1139 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1140 sym->name);
1141 return FAILURE;
1142 }
1143
6de9cd9a
DN
1144 if (init == NULL)
1145 {
1146 /* An initializer is required for PARAMETER declarations. */
1147 if (attr.flavor == FL_PARAMETER)
1148 {
1149 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1150 return FAILURE;
1151 }
1152 }
1153 else
1154 {
1155 /* If a variable appears in a DATA block, it cannot have an
1de8a836 1156 initializer. */
6de9cd9a
DN
1157 if (sym->attr.data)
1158 {
636dff67
SK
1159 gfc_error ("Variable '%s' at %C with an initializer already "
1160 "appears in a DATA statement", sym->name);
6de9cd9a
DN
1161 return FAILURE;
1162 }
1163
75d17889
TS
1164 /* Check if the assignment can happen. This has to be put off
1165 until later for a derived type variable. */
6de9cd9a
DN
1166 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1167 && gfc_check_assign_symbol (sym, init) == FAILURE)
1168 return FAILURE;
1169
df7cc9b5
FW
1170 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1171 {
1172 /* Update symbol character length according initializer. */
1173 if (sym->ts.cl->length == NULL)
1174 {
66e4ab31
SK
1175 /* If there are multiple CHARACTER variables declared on the
1176 same line, we don't want them to share the same length. */
4213f93b
PT
1177 sym->ts.cl = gfc_get_charlen ();
1178 sym->ts.cl->next = gfc_current_ns->cl_list;
1179 gfc_current_ns->cl_list = sym->ts.cl;
96f4873b
PT
1180
1181 if (sym->attr.flavor == FL_PARAMETER
636dff67 1182 && init->expr_type == EXPR_ARRAY)
96f4873b 1183 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
df7cc9b5
FW
1184 }
1185 /* Update initializer character length according symbol. */
1186 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1187 {
1188 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1189 gfc_constructor * p;
1190
1191 if (init->expr_type == EXPR_CONSTANT)
2220652d 1192 gfc_set_constant_character_len (len, init, false);
df7cc9b5
FW
1193 else if (init->expr_type == EXPR_ARRAY)
1194 {
dcdc7b6c
PT
1195 /* Build a new charlen to prevent simplification from
1196 deleting the length before it is resolved. */
1197 init->ts.cl = gfc_get_charlen ();
1198 init->ts.cl->next = gfc_current_ns->cl_list;
1199 gfc_current_ns->cl_list = sym->ts.cl;
df7cc9b5 1200 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
dcdc7b6c 1201
df7cc9b5 1202 for (p = init->value.constructor; p; p = p->next)
2220652d 1203 gfc_set_constant_character_len (len, p->expr, false);
df7cc9b5
FW
1204 }
1205 }
1206 }
1207
a8b3b0b6
CR
1208 /* Need to check if the expression we initialized this
1209 to was one of the iso_c_binding named constants. If so,
1210 and we're a parameter (constant), let it be iso_c.
1211 For example:
1212 integer(c_int), parameter :: my_int = c_int
1213 integer(my_int) :: my_int_2
1214 If we mark my_int as iso_c (since we can see it's value
1215 is equal to one of the named constants), then my_int_2
1216 will be considered C interoperable. */
1217 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1218 {
1219 sym->ts.is_iso_c |= init->ts.is_iso_c;
1220 sym->ts.is_c_interop |= init->ts.is_c_interop;
1221 /* attr bits needed for module files. */
1222 sym->attr.is_iso_c |= init->ts.is_iso_c;
1223 sym->attr.is_c_interop |= init->ts.is_c_interop;
1224 if (init->ts.is_iso_c)
1225 sym->ts.f90_type = init->ts.f90_type;
1226 }
1227
6de9cd9a
DN
1228 /* Add initializer. Make sure we keep the ranks sane. */
1229 if (sym->attr.dimension && init->rank == 0)
a9b43781
PT
1230 {
1231 mpz_t size;
1232 gfc_expr *array;
1233 gfc_constructor *c;
1234 int n;
1235 if (sym->attr.flavor == FL_PARAMETER
1236 && init->expr_type == EXPR_CONSTANT
1237 && spec_size (sym->as, &size) == SUCCESS
1238 && mpz_cmp_si (size, 0) > 0)
1239 {
1240 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1241 &init->where);
1242
1243 array->value.constructor = c = NULL;
1244 for (n = 0; n < (int)mpz_get_si (size); n++)
1245 {
1246 if (array->value.constructor == NULL)
1247 {
1248 array->value.constructor = c = gfc_get_constructor ();
1249 c->expr = init;
1250 }
1251 else
1252 {
1253 c->next = gfc_get_constructor ();
1254 c = c->next;
1255 c->expr = gfc_copy_expr (init);
1256 }
1257 }
1258
1259 array->shape = gfc_get_shape (sym->as->rank);
1260 for (n = 0; n < sym->as->rank; n++)
1261 spec_dimen_size (sym->as, n, &array->shape[n]);
1262
1263 init = array;
1264 mpz_clear (size);
1265 }
1266 init->rank = sym->as->rank;
1267 }
6de9cd9a
DN
1268
1269 sym->value = init;
ef7236d2
DF
1270 if (sym->attr.save == SAVE_NONE)
1271 sym->attr.save = SAVE_IMPLICIT;
6de9cd9a
DN
1272 *initp = NULL;
1273 }
1274
1275 return SUCCESS;
1276}
1277
1278
1279/* Function called by variable_decl() that adds a name to a structure
1280 being built. */
1281
1282static try
636dff67
SK
1283build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1284 gfc_array_spec **as)
6de9cd9a
DN
1285{
1286 gfc_component *c;
1287
1288 /* If the current symbol is of the same derived type that we're
1289 constructing, it must have the pointer attribute. */
1290 if (current_ts.type == BT_DERIVED
1291 && current_ts.derived == gfc_current_block ()
1292 && current_attr.pointer == 0)
1293 {
1294 gfc_error ("Component at %C must have the POINTER attribute");
1295 return FAILURE;
1296 }
1297
636dff67 1298 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
6de9cd9a
DN
1299 {
1300 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1301 {
1302 gfc_error ("Array component of structure at %C must have explicit "
1303 "or deferred shape");
1304 return FAILURE;
1305 }
1306 }
1307
1308 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1309 return FAILURE;
1310
1311 c->ts = current_ts;
1312 c->ts.cl = cl;
1313 gfc_set_component_attr (c, &current_attr);
1314
1315 c->initializer = *init;
1316 *init = NULL;
1317
1318 c->as = *as;
1319 if (c->as != NULL)
1320 c->dimension = 1;
1321 *as = NULL;
1322
1323 /* Check array components. */
1324 if (!c->dimension)
5046aff5
PT
1325 {
1326 if (c->allocatable)
1327 {
1328 gfc_error ("Allocatable component at %C must be an array");
1329 return FAILURE;
1330 }
1331 else
1332 return SUCCESS;
1333 }
6de9cd9a
DN
1334
1335 if (c->pointer)
1336 {
1337 if (c->as->type != AS_DEFERRED)
1338 {
5046aff5
PT
1339 gfc_error ("Pointer array component of structure at %C must have a "
1340 "deferred shape");
1341 return FAILURE;
1342 }
1343 }
1344 else if (c->allocatable)
1345 {
1346 if (c->as->type != AS_DEFERRED)
1347 {
1348 gfc_error ("Allocatable component of structure at %C must have a "
1349 "deferred shape");
6de9cd9a
DN
1350 return FAILURE;
1351 }
1352 }
1353 else
1354 {
1355 if (c->as->type != AS_EXPLICIT)
1356 {
636dff67
SK
1357 gfc_error ("Array component of structure at %C must have an "
1358 "explicit shape");
6de9cd9a
DN
1359 return FAILURE;
1360 }
1361 }
1362
1363 return SUCCESS;
1364}
1365
1366
1367/* Match a 'NULL()', and possibly take care of some side effects. */
1368
1369match
636dff67 1370gfc_match_null (gfc_expr **result)
6de9cd9a
DN
1371{
1372 gfc_symbol *sym;
1373 gfc_expr *e;
1374 match m;
1375
1376 m = gfc_match (" null ( )");
1377 if (m != MATCH_YES)
1378 return m;
1379
1380 /* The NULL symbol now has to be/become an intrinsic function. */
1381 if (gfc_get_symbol ("null", NULL, &sym))
1382 {
1383 gfc_error ("NULL() initialization at %C is ambiguous");
1384 return MATCH_ERROR;
1385 }
1386
1387 gfc_intrinsic_symbol (sym);
1388
1389 if (sym->attr.proc != PROC_INTRINSIC
231b2fcc
TS
1390 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1391 sym->name, NULL) == FAILURE
1392 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
6de9cd9a
DN
1393 return MATCH_ERROR;
1394
1395 e = gfc_get_expr ();
63645982 1396 e->where = gfc_current_locus;
6de9cd9a
DN
1397 e->expr_type = EXPR_NULL;
1398 e->ts.type = BT_UNKNOWN;
1399
1400 *result = e;
1401
1402 return MATCH_YES;
1403}
1404
1405
6de9cd9a
DN
1406/* Match a variable name with an optional initializer. When this
1407 subroutine is called, a variable is expected to be parsed next.
1408 Depending on what is happening at the moment, updates either the
1409 symbol table or the current interface. */
1410
1411static match
949d5b72 1412variable_decl (int elem)
6de9cd9a
DN
1413{
1414 char name[GFC_MAX_SYMBOL_LEN + 1];
1415 gfc_expr *initializer, *char_len;
1416 gfc_array_spec *as;
83d890b9 1417 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
6de9cd9a
DN
1418 gfc_charlen *cl;
1419 locus var_locus;
1420 match m;
1421 try t;
83d890b9 1422 gfc_symbol *sym;
25d8f0a2 1423 locus old_locus;
6de9cd9a
DN
1424
1425 initializer = NULL;
1426 as = NULL;
83d890b9 1427 cp_as = NULL;
25d8f0a2 1428 old_locus = gfc_current_locus;
6de9cd9a
DN
1429
1430 /* When we get here, we've just matched a list of attributes and
1431 maybe a type and a double colon. The next thing we expect to see
1432 is the name of the symbol. */
1433 m = gfc_match_name (name);
1434 if (m != MATCH_YES)
1435 goto cleanup;
1436
63645982 1437 var_locus = gfc_current_locus;
6de9cd9a
DN
1438
1439 /* Now we could see the optional array spec. or character length. */
1440 m = gfc_match_array_spec (&as);
83d890b9
AL
1441 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1442 cp_as = gfc_copy_array_spec (as);
1443 else if (m == MATCH_ERROR)
6de9cd9a 1444 goto cleanup;
25d8f0a2 1445
6de9cd9a
DN
1446 if (m == MATCH_NO)
1447 as = gfc_copy_array_spec (current_as);
1448
1449 char_len = NULL;
1450 cl = NULL;
1451
1452 if (current_ts.type == BT_CHARACTER)
1453 {
1454 switch (match_char_length (&char_len))
1455 {
1456 case MATCH_YES:
1457 cl = gfc_get_charlen ();
1458 cl->next = gfc_current_ns->cl_list;
1459 gfc_current_ns->cl_list = cl;
1460
1461 cl->length = char_len;
1462 break;
1463
949d5b72
PT
1464 /* Non-constant lengths need to be copied after the first
1465 element. */
6de9cd9a 1466 case MATCH_NO:
949d5b72 1467 if (elem > 1 && current_ts.cl->length
636dff67 1468 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
949d5b72
PT
1469 {
1470 cl = gfc_get_charlen ();
1471 cl->next = gfc_current_ns->cl_list;
1472 gfc_current_ns->cl_list = cl;
1473 cl->length = gfc_copy_expr (current_ts.cl->length);
1474 }
1475 else
1476 cl = current_ts.cl;
1477
6de9cd9a
DN
1478 break;
1479
1480 case MATCH_ERROR:
1481 goto cleanup;
1482 }
1483 }
1484
83d890b9 1485 /* If this symbol has already shown up in a Cray Pointer declaration,
66e4ab31 1486 then we want to set the type & bail out. */
83d890b9
AL
1487 if (gfc_option.flag_cray_pointer)
1488 {
1489 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1490 if (sym != NULL && sym->attr.cray_pointee)
1491 {
1492 sym->ts.type = current_ts.type;
1493 sym->ts.kind = current_ts.kind;
1494 sym->ts.cl = cl;
1495 sym->ts.derived = current_ts.derived;
a8b3b0b6
CR
1496 sym->ts.is_c_interop = current_ts.is_c_interop;
1497 sym->ts.is_iso_c = current_ts.is_iso_c;
83d890b9
AL
1498 m = MATCH_YES;
1499
1500 /* Check to see if we have an array specification. */
1501 if (cp_as != NULL)
1502 {
1503 if (sym->as != NULL)
1504 {
e25a0da3 1505 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
1506 gfc_free_array_spec (cp_as);
1507 m = MATCH_ERROR;
1508 goto cleanup;
1509 }
1510 else
1511 {
1512 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1513 gfc_internal_error ("Couldn't set pointee array spec.");
d51347f9 1514
83d890b9 1515 /* Fix the array spec. */
d51347f9 1516 m = gfc_mod_pointee_as (sym->as);
83d890b9
AL
1517 if (m == MATCH_ERROR)
1518 goto cleanup;
1519 }
d51347f9 1520 }
83d890b9
AL
1521 goto cleanup;
1522 }
1523 else
1524 {
1525 gfc_free_array_spec (cp_as);
1526 }
1527 }
d51347f9
TB
1528
1529
6de9cd9a
DN
1530 /* OK, we've successfully matched the declaration. Now put the
1531 symbol in the current namespace, because it might be used in the
69de3b83 1532 optional initialization expression for this symbol, e.g. this is
6de9cd9a
DN
1533 perfectly legal:
1534
1535 integer, parameter :: i = huge(i)
1536
1537 This is only true for parameters or variables of a basic type.
1538 For components of derived types, it is not true, so we don't
1539 create a symbol for those yet. If we fail to create the symbol,
1540 bail out. */
1541 if (gfc_current_state () != COMP_DERIVED
1542 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1543 {
72af9f0b
PT
1544 m = MATCH_ERROR;
1545 goto cleanup;
1546 }
1547
6133c68a
TS
1548 /* An interface body specifies all of the procedure's
1549 characteristics and these shall be consistent with those
1550 specified in the procedure definition, except that the interface
1551 may specify a procedure that is not pure if the procedure is
1552 defined to be pure(12.3.2). */
72af9f0b 1553 if (current_ts.type == BT_DERIVED
636dff67
SK
1554 && gfc_current_ns->proc_name
1555 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
5a8af0b4
PT
1556 && current_ts.derived->ns != gfc_current_ns)
1557 {
1558 gfc_symtree *st;
1559 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1560 if (!(current_ts.derived->attr.imported
1561 && st != NULL
1562 && st->n.sym == current_ts.derived)
1563 && !gfc_current_ns->has_import_set)
1564 {
1565 gfc_error ("the type of '%s' at %C has not been declared within the "
1566 "interface", name);
1567 m = MATCH_ERROR;
1568 goto cleanup;
1569 }
6de9cd9a
DN
1570 }
1571
1572 /* In functions that have a RESULT variable defined, the function
1573 name always refers to function calls. Therefore, the name is
1574 not allowed to appear in specification statements. */
1575 if (gfc_current_state () == COMP_FUNCTION
1576 && gfc_current_block () != NULL
1577 && gfc_current_block ()->result != NULL
1578 && gfc_current_block ()->result != gfc_current_block ()
1579 && strcmp (gfc_current_block ()->name, name) == 0)
1580 {
1581 gfc_error ("Function name '%s' not allowed at %C", name);
1582 m = MATCH_ERROR;
1583 goto cleanup;
1584 }
1585
294fbfc8
TS
1586 /* We allow old-style initializations of the form
1587 integer i /2/, j(4) /3*3, 1/
1588 (if no colon has been seen). These are different from data
1589 statements in that initializers are only allowed to apply to the
1590 variable immediately preceding, i.e.
1591 integer i, j /1, 2/
1592 is not allowed. Therefore we have to do some work manually, that
75d17889 1593 could otherwise be left to the matchers for DATA statements. */
294fbfc8
TS
1594
1595 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1596 {
1597 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1598 "initialization at %C") == FAILURE)
1599 return MATCH_ERROR;
d51347f9 1600
294fbfc8
TS
1601 return match_old_style_init (name);
1602 }
1603
6de9cd9a
DN
1604 /* The double colon must be present in order to have initializers.
1605 Otherwise the statement is ambiguous with an assignment statement. */
1606 if (colon_seen)
1607 {
1608 if (gfc_match (" =>") == MATCH_YES)
1609 {
6de9cd9a
DN
1610 if (!current_attr.pointer)
1611 {
1612 gfc_error ("Initialization at %C isn't for a pointer variable");
1613 m = MATCH_ERROR;
1614 goto cleanup;
1615 }
1616
1617 m = gfc_match_null (&initializer);
1618 if (m == MATCH_NO)
1619 {
def66134 1620 gfc_error ("Pointer initialization requires a NULL() at %C");
6de9cd9a
DN
1621 m = MATCH_ERROR;
1622 }
1623
1624 if (gfc_pure (NULL))
1625 {
636dff67
SK
1626 gfc_error ("Initialization of pointer at %C is not allowed in "
1627 "a PURE procedure");
6de9cd9a
DN
1628 m = MATCH_ERROR;
1629 }
1630
1631 if (m != MATCH_YES)
1632 goto cleanup;
1633
6de9cd9a
DN
1634 }
1635 else if (gfc_match_char ('=') == MATCH_YES)
1636 {
1637 if (current_attr.pointer)
1638 {
636dff67
SK
1639 gfc_error ("Pointer initialization at %C requires '=>', "
1640 "not '='");
6de9cd9a
DN
1641 m = MATCH_ERROR;
1642 goto cleanup;
1643 }
1644
1645 m = gfc_match_init_expr (&initializer);
1646 if (m == MATCH_NO)
1647 {
1648 gfc_error ("Expected an initialization expression at %C");
1649 m = MATCH_ERROR;
1650 }
1651
1652 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1653 {
636dff67
SK
1654 gfc_error ("Initialization of variable at %C is not allowed in "
1655 "a PURE procedure");
6de9cd9a
DN
1656 m = MATCH_ERROR;
1657 }
1658
1659 if (m != MATCH_YES)
1660 goto cleanup;
1661 }
cb44ab82
VL
1662 }
1663
5046aff5
PT
1664 if (initializer != NULL && current_attr.allocatable
1665 && gfc_current_state () == COMP_DERIVED)
1666 {
636dff67
SK
1667 gfc_error ("Initialization of allocatable component at %C is not "
1668 "allowed");
5046aff5
PT
1669 m = MATCH_ERROR;
1670 goto cleanup;
1671 }
1672
54b4ba60 1673 /* Add the initializer. Note that it is fine if initializer is
6de9cd9a
DN
1674 NULL here, because we sometimes also need to check if a
1675 declaration *must* have an initialization expression. */
1676 if (gfc_current_state () != COMP_DERIVED)
1677 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1678 else
54b4ba60 1679 {
5046aff5 1680 if (current_ts.type == BT_DERIVED
636dff67 1681 && !current_attr.pointer && !initializer)
54b4ba60
PB
1682 initializer = gfc_default_initializer (&current_ts);
1683 t = build_struct (name, cl, &initializer, &as);
1684 }
6de9cd9a
DN
1685
1686 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1687
1688cleanup:
1689 /* Free stuff up and return. */
1690 gfc_free_expr (initializer);
1691 gfc_free_array_spec (as);
1692
1693 return m;
1694}
1695
1696
b2b81a3f
BM
1697/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1698 This assumes that the byte size is equal to the kind number for
1699 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
6de9cd9a
DN
1700
1701match
636dff67 1702gfc_match_old_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
1703{
1704 match m;
5cf54585 1705 int original_kind;
6de9cd9a
DN
1706
1707 if (gfc_match_char ('*') != MATCH_YES)
1708 return MATCH_NO;
1709
5cf54585 1710 m = gfc_match_small_literal_int (&ts->kind, NULL);
6de9cd9a
DN
1711 if (m != MATCH_YES)
1712 return MATCH_ERROR;
1713
e45b3c75
ES
1714 original_kind = ts->kind;
1715
6de9cd9a 1716 /* Massage the kind numbers for complex types. */
e45b3c75
ES
1717 if (ts->type == BT_COMPLEX)
1718 {
1719 if (ts->kind % 2)
636dff67
SK
1720 {
1721 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1722 gfc_basic_typename (ts->type), original_kind);
1723 return MATCH_ERROR;
1724 }
e45b3c75
ES
1725 ts->kind /= 2;
1726 }
6de9cd9a 1727
e7a2d5fb 1728 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a 1729 {
e45b3c75 1730 gfc_error ("Old-style type declaration %s*%d not supported at %C",
636dff67 1731 gfc_basic_typename (ts->type), original_kind);
6de9cd9a
DN
1732 return MATCH_ERROR;
1733 }
1734
df8652dc
SK
1735 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1736 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1737 return MATCH_ERROR;
1738
6de9cd9a
DN
1739 return MATCH_YES;
1740}
1741
1742
1743/* Match a kind specification. Since kinds are generally optional, we
1744 usually return MATCH_NO if something goes wrong. If a "kind="
1745 string is found, then we know we have an error. */
1746
1747match
636dff67 1748gfc_match_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
1749{
1750 locus where;
1751 gfc_expr *e;
1752 match m, n;
1753 const char *msg;
1754
1755 m = MATCH_NO;
1756 e = NULL;
1757
63645982 1758 where = gfc_current_locus;
6de9cd9a
DN
1759
1760 if (gfc_match_char ('(') == MATCH_NO)
1761 return MATCH_NO;
1762
1763 /* Also gobbles optional text. */
1764 if (gfc_match (" kind = ") == MATCH_YES)
1765 m = MATCH_ERROR;
1766
1767 n = gfc_match_init_expr (&e);
1768 if (n == MATCH_NO)
1769 gfc_error ("Expected initialization expression at %C");
1770 if (n != MATCH_YES)
1771 return MATCH_ERROR;
1772
1773 if (e->rank != 0)
1774 {
1775 gfc_error ("Expected scalar initialization expression at %C");
1776 m = MATCH_ERROR;
1777 goto no_match;
1778 }
1779
1780 msg = gfc_extract_int (e, &ts->kind);
1781 if (msg != NULL)
1782 {
1783 gfc_error (msg);
1784 m = MATCH_ERROR;
1785 goto no_match;
1786 }
1787
a8b3b0b6
CR
1788 /* Before throwing away the expression, let's see if we had a
1789 C interoperable kind (and store the fact). */
1790 if (e->ts.is_c_interop == 1)
1791 {
1792 /* Mark this as c interoperable if being declared with one
1793 of the named constants from iso_c_binding. */
1794 ts->is_c_interop = e->ts.is_iso_c;
1795 ts->f90_type = e->ts.f90_type;
1796 }
1797
6de9cd9a
DN
1798 gfc_free_expr (e);
1799 e = NULL;
1800
a8b3b0b6
CR
1801 /* Ignore errors to this point, if we've gotten here. This means
1802 we ignore the m=MATCH_ERROR from above. */
e7a2d5fb 1803 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a
DN
1804 {
1805 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1806 gfc_basic_typename (ts->type));
6de9cd9a 1807 m = MATCH_ERROR;
6de9cd9a 1808 }
a8b3b0b6 1809 else if (gfc_match_char (')') != MATCH_YES)
6de9cd9a 1810 {
8998be20 1811 gfc_error ("Missing right parenthesis at %C");
a8b3b0b6 1812 m = MATCH_ERROR;
6de9cd9a 1813 }
a8b3b0b6
CR
1814 else
1815 /* All tests passed. */
1816 m = MATCH_YES;
6de9cd9a 1817
a8b3b0b6
CR
1818 if(m == MATCH_ERROR)
1819 gfc_current_locus = where;
1820
1821 /* Return what we know from the test(s). */
1822 return m;
6de9cd9a
DN
1823
1824no_match:
1825 gfc_free_expr (e);
63645982 1826 gfc_current_locus = where;
6de9cd9a
DN
1827 return m;
1828}
1829
1830
1831/* Match the various kind/length specifications in a CHARACTER
1832 declaration. We don't return MATCH_NO. */
1833
1834static match
636dff67 1835match_char_spec (gfc_typespec *ts)
6de9cd9a 1836{
5cd09fac 1837 int kind, seen_length;
6de9cd9a
DN
1838 gfc_charlen *cl;
1839 gfc_expr *len;
1840 match m;
a8b3b0b6 1841 gfc_expr *kind_expr = NULL;
9d64df18 1842 kind = gfc_default_character_kind;
6de9cd9a
DN
1843 len = NULL;
1844 seen_length = 0;
1845
1846 /* Try the old-style specification first. */
1847 old_char_selector = 0;
1848
1849 m = match_char_length (&len);
1850 if (m != MATCH_NO)
1851 {
1852 if (m == MATCH_YES)
1853 old_char_selector = 1;
1854 seen_length = 1;
1855 goto done;
1856 }
1857
1858 m = gfc_match_char ('(');
1859 if (m != MATCH_YES)
1860 {
a8b3b0b6 1861 m = MATCH_YES; /* Character without length is a single char. */
6de9cd9a
DN
1862 goto done;
1863 }
1864
a8b3b0b6 1865 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
6de9cd9a
DN
1866 if (gfc_match (" kind =") == MATCH_YES)
1867 {
a8b3b0b6
CR
1868 m = gfc_match_small_int_expr(&kind, &kind_expr);
1869
6de9cd9a
DN
1870 if (m == MATCH_ERROR)
1871 goto done;
1872 if (m == MATCH_NO)
1873 goto syntax;
1874
1875 if (gfc_match (" , len =") == MATCH_NO)
1876 goto rparen;
1877
1878 m = char_len_param_value (&len);
1879 if (m == MATCH_NO)
1880 goto syntax;
1881 if (m == MATCH_ERROR)
1882 goto done;
1883 seen_length = 1;
1884
1885 goto rparen;
1886 }
1887
66e4ab31 1888 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
6de9cd9a
DN
1889 if (gfc_match (" len =") == MATCH_YES)
1890 {
1891 m = char_len_param_value (&len);
1892 if (m == MATCH_NO)
1893 goto syntax;
1894 if (m == MATCH_ERROR)
1895 goto done;
1896 seen_length = 1;
1897
1898 if (gfc_match_char (')') == MATCH_YES)
1899 goto done;
1900
1901 if (gfc_match (" , kind =") != MATCH_YES)
1902 goto syntax;
1903
a8b3b0b6 1904 gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a 1905
e7a2d5fb 1906 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1907 {
1908 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1909 return MATCH_YES;
1910 }
1911
1912 goto rparen;
1913 }
1914
66e4ab31 1915 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
6de9cd9a
DN
1916 m = char_len_param_value (&len);
1917 if (m == MATCH_NO)
1918 goto syntax;
1919 if (m == MATCH_ERROR)
1920 goto done;
1921 seen_length = 1;
1922
1923 m = gfc_match_char (')');
1924 if (m == MATCH_YES)
1925 goto done;
1926
1927 if (gfc_match_char (',') != MATCH_YES)
1928 goto syntax;
1929
a8b3b0b6 1930 gfc_match (" kind ="); /* Gobble optional text. */
6de9cd9a 1931
a8b3b0b6 1932 m = gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a
DN
1933 if (m == MATCH_ERROR)
1934 goto done;
1935 if (m == MATCH_NO)
1936 goto syntax;
1937
1938rparen:
1939 /* Require a right-paren at this point. */
1940 m = gfc_match_char (')');
1941 if (m == MATCH_YES)
1942 goto done;
1943
1944syntax:
1945 gfc_error ("Syntax error in CHARACTER declaration at %C");
1946 m = MATCH_ERROR;
16f8ffc8
JD
1947 gfc_free_expr (len);
1948 return m;
6de9cd9a
DN
1949
1950done:
16f8ffc8 1951 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1952 {
1953 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1954 m = MATCH_ERROR;
1955 }
1956
16f8ffc8
JD
1957 if (seen_length == 1 && len != NULL
1958 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1959 {
1960 gfc_error ("Expression at %C must be of INTEGER type");
1961 m = MATCH_ERROR;
1962 }
1963
6de9cd9a
DN
1964 if (m != MATCH_YES)
1965 {
1966 gfc_free_expr (len);
a8b3b0b6 1967 gfc_free_expr (kind_expr);
6de9cd9a
DN
1968 return m;
1969 }
1970
1971 /* Do some final massaging of the length values. */
1972 cl = gfc_get_charlen ();
1973 cl->next = gfc_current_ns->cl_list;
1974 gfc_current_ns->cl_list = cl;
1975
1976 if (seen_length == 0)
1977 cl->length = gfc_int_expr (1);
1978 else
5cd09fac 1979 cl->length = len;
6de9cd9a
DN
1980
1981 ts->cl = cl;
1982 ts->kind = kind;
1983
a8b3b0b6
CR
1984 /* We have to know if it was a c interoperable kind so we can
1985 do accurate type checking of bind(c) procs, etc. */
1986 if (kind_expr != NULL)
1987 {
1988 /* Mark this as c interoperable if being declared with one
1989 of the named constants from iso_c_binding. */
1990 ts->is_c_interop = kind_expr->ts.is_iso_c;
1991 gfc_free_expr (kind_expr);
1992 }
1993 else if (len != NULL)
1994 {
1995 /* Here, we might have parsed something such as:
1996 character(c_char)
1997 In this case, the parsing code above grabs the c_char when
1998 looking for the length (line 1690, roughly). it's the last
1999 testcase for parsing the kind params of a character variable.
2000 However, it's not actually the length. this seems like it
2001 could be an error.
2002 To see if the user used a C interop kind, test the expr
2003 of the so called length, and see if it's C interoperable. */
2004 ts->is_c_interop = len->ts.is_iso_c;
2005 }
2006
6de9cd9a
DN
2007 return MATCH_YES;
2008}
2009
2010
2011/* Matches a type specification. If successful, sets the ts structure
2012 to the matched specification. This is necessary for FUNCTION and
2013 IMPLICIT statements.
2014
d51347f9 2015 If implicit_flag is nonzero, then we don't check for the optional
e5ddaa24 2016 kind specification. Not doing so is needed for matching an IMPLICIT
6de9cd9a
DN
2017 statement correctly. */
2018
e5ddaa24 2019static match
636dff67 2020match_type_spec (gfc_typespec *ts, int implicit_flag)
6de9cd9a
DN
2021{
2022 char name[GFC_MAX_SYMBOL_LEN + 1];
2023 gfc_symbol *sym;
2024 match m;
0ff0dfbf 2025 int c;
6de9cd9a
DN
2026
2027 gfc_clear_ts (ts);
2028
a8b3b0b6
CR
2029 /* Clear the current binding label, in case one is given. */
2030 curr_binding_label[0] = '\0';
2031
5f700e6d
AL
2032 if (gfc_match (" byte") == MATCH_YES)
2033 {
d51347f9 2034 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
5f700e6d
AL
2035 == FAILURE)
2036 return MATCH_ERROR;
2037
2038 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2039 {
2040 gfc_error ("BYTE type used at %C "
2041 "is not available on the target machine");
2042 return MATCH_ERROR;
2043 }
d51347f9 2044
5f700e6d
AL
2045 ts->type = BT_INTEGER;
2046 ts->kind = 1;
2047 return MATCH_YES;
2048 }
2049
6de9cd9a
DN
2050 if (gfc_match (" integer") == MATCH_YES)
2051 {
2052 ts->type = BT_INTEGER;
9d64df18 2053 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
2054 goto get_kind;
2055 }
2056
2057 if (gfc_match (" character") == MATCH_YES)
2058 {
2059 ts->type = BT_CHARACTER;
e5ddaa24
TS
2060 if (implicit_flag == 0)
2061 return match_char_spec (ts);
2062 else
2063 return MATCH_YES;
6de9cd9a
DN
2064 }
2065
2066 if (gfc_match (" real") == MATCH_YES)
2067 {
2068 ts->type = BT_REAL;
9d64df18 2069 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
2070 goto get_kind;
2071 }
2072
2073 if (gfc_match (" double precision") == MATCH_YES)
2074 {
2075 ts->type = BT_REAL;
9d64df18 2076 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2077 return MATCH_YES;
2078 }
2079
2080 if (gfc_match (" complex") == MATCH_YES)
2081 {
2082 ts->type = BT_COMPLEX;
9d64df18 2083 ts->kind = gfc_default_complex_kind;
6de9cd9a
DN
2084 goto get_kind;
2085 }
2086
2087 if (gfc_match (" double complex") == MATCH_YES)
2088 {
df8652dc
SK
2089 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2090 "conform to the Fortran 95 standard") == FAILURE)
2091 return MATCH_ERROR;
2092
6de9cd9a 2093 ts->type = BT_COMPLEX;
9d64df18 2094 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2095 return MATCH_YES;
2096 }
2097
2098 if (gfc_match (" logical") == MATCH_YES)
2099 {
2100 ts->type = BT_LOGICAL;
9d64df18 2101 ts->kind = gfc_default_logical_kind;
6de9cd9a
DN
2102 goto get_kind;
2103 }
2104
2105 m = gfc_match (" type ( %n )", name);
2106 if (m != MATCH_YES)
2107 return m;
2108
2109 /* Search for the name but allow the components to be defined later. */
2110 if (gfc_get_ha_symbol (name, &sym))
2111 {
2112 gfc_error ("Type name '%s' at %C is ambiguous", name);
2113 return MATCH_ERROR;
2114 }
2115
2116 if (sym->attr.flavor != FL_DERIVED
231b2fcc 2117 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2118 return MATCH_ERROR;
2119
2120 ts->type = BT_DERIVED;
2121 ts->kind = 0;
2122 ts->derived = sym;
2123
2124 return MATCH_YES;
2125
2126get_kind:
2127 /* For all types except double, derived and character, look for an
2128 optional kind specifier. MATCH_NO is actually OK at this point. */
e5ddaa24 2129 if (implicit_flag == 1)
6de9cd9a
DN
2130 return MATCH_YES;
2131
0ff0dfbf
TS
2132 if (gfc_current_form == FORM_FREE)
2133 {
2134 c = gfc_peek_char();
2135 if (!gfc_is_whitespace(c) && c != '*' && c != '('
636dff67 2136 && c != ':' && c != ',')
0ff0dfbf
TS
2137 return MATCH_NO;
2138 }
2139
6de9cd9a
DN
2140 m = gfc_match_kind_spec (ts);
2141 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2142 m = gfc_match_old_kind_spec (ts);
2143
2144 if (m == MATCH_NO)
2145 m = MATCH_YES; /* No kind specifier found. */
2146
2147 return m;
2148}
2149
2150
e5ddaa24
TS
2151/* Match an IMPLICIT NONE statement. Actually, this statement is
2152 already matched in parse.c, or we would not end up here in the
2153 first place. So the only thing we need to check, is if there is
2154 trailing garbage. If not, the match is successful. */
2155
2156match
2157gfc_match_implicit_none (void)
2158{
e5ddaa24
TS
2159 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2160}
2161
2162
2163/* Match the letter range(s) of an IMPLICIT statement. */
2164
2165static match
1107b970 2166match_implicit_range (void)
e5ddaa24
TS
2167{
2168 int c, c1, c2, inner;
2169 locus cur_loc;
2170
2171 cur_loc = gfc_current_locus;
2172
2173 gfc_gobble_whitespace ();
2174 c = gfc_next_char ();
2175 if (c != '(')
2176 {
2177 gfc_error ("Missing character range in IMPLICIT at %C");
2178 goto bad;
2179 }
2180
2181 inner = 1;
2182 while (inner)
2183 {
2184 gfc_gobble_whitespace ();
2185 c1 = gfc_next_char ();
2186 if (!ISALPHA (c1))
2187 goto bad;
2188
2189 gfc_gobble_whitespace ();
2190 c = gfc_next_char ();
2191
2192 switch (c)
2193 {
2194 case ')':
66e4ab31 2195 inner = 0; /* Fall through. */
e5ddaa24
TS
2196
2197 case ',':
2198 c2 = c1;
2199 break;
2200
2201 case '-':
2202 gfc_gobble_whitespace ();
2203 c2 = gfc_next_char ();
2204 if (!ISALPHA (c2))
2205 goto bad;
2206
2207 gfc_gobble_whitespace ();
2208 c = gfc_next_char ();
2209
2210 if ((c != ',') && (c != ')'))
2211 goto bad;
2212 if (c == ')')
2213 inner = 0;
2214
2215 break;
2216
2217 default:
2218 goto bad;
2219 }
2220
2221 if (c1 > c2)
2222 {
2223 gfc_error ("Letters must be in alphabetic order in "
2224 "IMPLICIT statement at %C");
2225 goto bad;
2226 }
2227
2228 /* See if we can add the newly matched range to the pending
636dff67
SK
2229 implicits from this IMPLICIT statement. We do not check for
2230 conflicts with whatever earlier IMPLICIT statements may have
2231 set. This is done when we've successfully finished matching
2232 the current one. */
1107b970 2233 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
e5ddaa24
TS
2234 goto bad;
2235 }
2236
2237 return MATCH_YES;
2238
2239bad:
2240 gfc_syntax_error (ST_IMPLICIT);
2241
2242 gfc_current_locus = cur_loc;
2243 return MATCH_ERROR;
2244}
2245
2246
2247/* Match an IMPLICIT statement, storing the types for
2248 gfc_set_implicit() if the statement is accepted by the parser.
2249 There is a strange looking, but legal syntactic construction
2250 possible. It looks like:
2251
2252 IMPLICIT INTEGER (a-b) (c-d)
2253
2254 This is legal if "a-b" is a constant expression that happens to
2255 equal one of the legal kinds for integers. The real problem
2256 happens with an implicit specification that looks like:
2257
2258 IMPLICIT INTEGER (a-b)
2259
2260 In this case, a typespec matcher that is "greedy" (as most of the
2261 matchers are) gobbles the character range as a kindspec, leaving
2262 nothing left. We therefore have to go a bit more slowly in the
2263 matching process by inhibiting the kindspec checking during
2264 typespec matching and checking for a kind later. */
2265
2266match
2267gfc_match_implicit (void)
2268{
2269 gfc_typespec ts;
2270 locus cur_loc;
2271 int c;
2272 match m;
2273
2274 /* We don't allow empty implicit statements. */
2275 if (gfc_match_eos () == MATCH_YES)
2276 {
2277 gfc_error ("Empty IMPLICIT statement at %C");
2278 return MATCH_ERROR;
2279 }
2280
e5ddaa24
TS
2281 do
2282 {
1107b970
PB
2283 /* First cleanup. */
2284 gfc_clear_new_implicit ();
2285
e5ddaa24
TS
2286 /* A basic type is mandatory here. */
2287 m = match_type_spec (&ts, 1);
2288 if (m == MATCH_ERROR)
2289 goto error;
2290 if (m == MATCH_NO)
2291 goto syntax;
2292
2293 cur_loc = gfc_current_locus;
1107b970 2294 m = match_implicit_range ();
e5ddaa24
TS
2295
2296 if (m == MATCH_YES)
2297 {
1107b970 2298 /* We may have <TYPE> (<RANGE>). */
e5ddaa24
TS
2299 gfc_gobble_whitespace ();
2300 c = gfc_next_char ();
2301 if ((c == '\n') || (c == ','))
1107b970
PB
2302 {
2303 /* Check for CHARACTER with no length parameter. */
2304 if (ts.type == BT_CHARACTER && !ts.cl)
2305 {
9d64df18 2306 ts.kind = gfc_default_character_kind;
1107b970
PB
2307 ts.cl = gfc_get_charlen ();
2308 ts.cl->next = gfc_current_ns->cl_list;
2309 gfc_current_ns->cl_list = ts.cl;
2310 ts.cl->length = gfc_int_expr (1);
2311 }
2312
2313 /* Record the Successful match. */
2314 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2315 return MATCH_ERROR;
2316 continue;
2317 }
e5ddaa24
TS
2318
2319 gfc_current_locus = cur_loc;
2320 }
2321
1107b970
PB
2322 /* Discard the (incorrectly) matched range. */
2323 gfc_clear_new_implicit ();
2324
2325 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2326 if (ts.type == BT_CHARACTER)
2327 m = match_char_spec (&ts);
2328 else
e5ddaa24 2329 {
1107b970 2330 m = gfc_match_kind_spec (&ts);
e5ddaa24 2331 if (m == MATCH_NO)
1107b970
PB
2332 {
2333 m = gfc_match_old_kind_spec (&ts);
2334 if (m == MATCH_ERROR)
2335 goto error;
2336 if (m == MATCH_NO)
2337 goto syntax;
2338 }
e5ddaa24 2339 }
1107b970
PB
2340 if (m == MATCH_ERROR)
2341 goto error;
e5ddaa24 2342
1107b970 2343 m = match_implicit_range ();
e5ddaa24
TS
2344 if (m == MATCH_ERROR)
2345 goto error;
2346 if (m == MATCH_NO)
2347 goto syntax;
2348
2349 gfc_gobble_whitespace ();
2350 c = gfc_next_char ();
2351 if ((c != '\n') && (c != ','))
2352 goto syntax;
2353
1107b970
PB
2354 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2355 return MATCH_ERROR;
e5ddaa24
TS
2356 }
2357 while (c == ',');
2358
1107b970 2359 return MATCH_YES;
e5ddaa24
TS
2360
2361syntax:
2362 gfc_syntax_error (ST_IMPLICIT);
2363
2364error:
2365 return MATCH_ERROR;
2366}
2367
66e4ab31 2368
8998be20
TB
2369match
2370gfc_match_import (void)
2371{
2372 char name[GFC_MAX_SYMBOL_LEN + 1];
2373 match m;
2374 gfc_symbol *sym;
2375 gfc_symtree *st;
2376
66e4ab31
SK
2377 if (gfc_current_ns->proc_name == NULL
2378 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
8998be20
TB
2379 {
2380 gfc_error ("IMPORT statement at %C only permitted in "
2381 "an INTERFACE body");
2382 return MATCH_ERROR;
2383 }
2384
636dff67 2385 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
8998be20
TB
2386 == FAILURE)
2387 return MATCH_ERROR;
2388
2389 if (gfc_match_eos () == MATCH_YES)
2390 {
2391 /* All host variables should be imported. */
2392 gfc_current_ns->has_import_set = 1;
2393 return MATCH_YES;
2394 }
2395
2396 if (gfc_match (" ::") == MATCH_YES)
2397 {
2398 if (gfc_match_eos () == MATCH_YES)
636dff67
SK
2399 {
2400 gfc_error ("Expecting list of named entities at %C");
2401 return MATCH_ERROR;
2402 }
8998be20
TB
2403 }
2404
2405 for(;;)
2406 {
2407 m = gfc_match (" %n", name);
2408 switch (m)
2409 {
2410 case MATCH_YES:
36d3fb4c 2411 if (gfc_current_ns->parent != NULL
66e4ab31 2412 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
36d3fb4c
PT
2413 {
2414 gfc_error ("Type name '%s' at %C is ambiguous", name);
2415 return MATCH_ERROR;
2416 }
2417 else if (gfc_current_ns->proc_name->ns->parent != NULL
66e4ab31
SK
2418 && gfc_find_symbol (name,
2419 gfc_current_ns->proc_name->ns->parent,
2420 1, &sym))
636dff67
SK
2421 {
2422 gfc_error ("Type name '%s' at %C is ambiguous", name);
2423 return MATCH_ERROR;
2424 }
2425
2426 if (sym == NULL)
2427 {
2428 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2429 "at %C - does not exist.", name);
2430 return MATCH_ERROR;
2431 }
2432
d51347f9 2433 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
636dff67
SK
2434 {
2435 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2436 "at %C.", name);
2437 goto next_item;
2438 }
2439
2440 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2441 st->n.sym = sym;
2442 sym->refs++;
5a8af0b4 2443 sym->attr.imported = 1;
8998be20
TB
2444
2445 goto next_item;
2446
2447 case MATCH_NO:
2448 break;
2449
2450 case MATCH_ERROR:
2451 return MATCH_ERROR;
2452 }
2453
2454 next_item:
2455 if (gfc_match_eos () == MATCH_YES)
2456 break;
2457 if (gfc_match_char (',') != MATCH_YES)
2458 goto syntax;
2459 }
2460
2461 return MATCH_YES;
2462
2463syntax:
2464 gfc_error ("Syntax error in IMPORT statement at %C");
2465 return MATCH_ERROR;
2466}
e5ddaa24 2467
66e4ab31 2468
6de9cd9a
DN
2469/* Matches an attribute specification including array specs. If
2470 successful, leaves the variables current_attr and current_as
2471 holding the specification. Also sets the colon_seen variable for
2472 later use by matchers associated with initializations.
2473
2474 This subroutine is a little tricky in the sense that we don't know
2475 if we really have an attr-spec until we hit the double colon.
2476 Until that time, we can only return MATCH_NO. This forces us to
2477 check for duplicate specification at this level. */
2478
2479static match
2480match_attr_spec (void)
2481{
6de9cd9a
DN
2482 /* Modifiers that can exist in a type statement. */
2483 typedef enum
2484 { GFC_DECL_BEGIN = 0,
2485 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2486 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
ee7e677f
TB
2487 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2488 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
a8b3b0b6 2489 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
6de9cd9a
DN
2490 GFC_DECL_END /* Sentinel */
2491 }
2492 decl_types;
2493
2494/* GFC_DECL_END is the sentinel, index starts at 0. */
2495#define NUM_DECL GFC_DECL_END
2496
2497 static mstring decls[] = {
2498 minit (", allocatable", DECL_ALLOCATABLE),
2499 minit (", dimension", DECL_DIMENSION),
2500 minit (", external", DECL_EXTERNAL),
2501 minit (", intent ( in )", DECL_IN),
2502 minit (", intent ( out )", DECL_OUT),
2503 minit (", intent ( in out )", DECL_INOUT),
2504 minit (", intrinsic", DECL_INTRINSIC),
2505 minit (", optional", DECL_OPTIONAL),
2506 minit (", parameter", DECL_PARAMETER),
2507 minit (", pointer", DECL_POINTER),
ee7e677f 2508 minit (", protected", DECL_PROTECTED),
6de9cd9a
DN
2509 minit (", private", DECL_PRIVATE),
2510 minit (", public", DECL_PUBLIC),
2511 minit (", save", DECL_SAVE),
2512 minit (", target", DECL_TARGET),
06469efd 2513 minit (", value", DECL_VALUE),
775e6c3a 2514 minit (", volatile", DECL_VOLATILE),
6de9cd9a
DN
2515 minit ("::", DECL_COLON),
2516 minit (NULL, DECL_NONE)
2517 };
2518
2519 locus start, seen_at[NUM_DECL];
2520 int seen[NUM_DECL];
2521 decl_types d;
2522 const char *attr;
2523 match m;
2524 try t;
a8b3b0b6 2525 char peek_char;
6de9cd9a
DN
2526
2527 gfc_clear_attr (&current_attr);
63645982 2528 start = gfc_current_locus;
6de9cd9a
DN
2529
2530 current_as = NULL;
2531 colon_seen = 0;
2532
2533 /* See if we get all of the keywords up to the final double colon. */
2534 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2535 seen[d] = 0;
2536
2537 for (;;)
2538 {
2539 d = (decl_types) gfc_match_strings (decls);
a8b3b0b6
CR
2540
2541 if (d == DECL_NONE)
2542 {
2543 /* See if we can find the bind(c) since all else failed.
2544 We need to skip over any whitespace and stop on the ','. */
2545 gfc_gobble_whitespace ();
2546 peek_char = gfc_peek_char ();
2547 if (peek_char == ',')
2548 {
2549 /* Chomp the comma. */
2550 peek_char = gfc_next_char ();
2551 /* Try and match the bind(c). */
129d15a3
JW
2552 m = gfc_match_bind_c (NULL);
2553 if (m == MATCH_YES)
a8b3b0b6 2554 d = DECL_IS_BIND_C;
129d15a3
JW
2555 else if (m == MATCH_ERROR)
2556 goto cleanup;
a8b3b0b6
CR
2557 }
2558 }
d468bcdb 2559
6de9cd9a
DN
2560 if (d == DECL_NONE || d == DECL_COLON)
2561 break;
d51347f9 2562
6de9cd9a 2563 seen[d]++;
63645982 2564 seen_at[d] = gfc_current_locus;
6de9cd9a
DN
2565
2566 if (d == DECL_DIMENSION)
2567 {
2568 m = gfc_match_array_spec (&current_as);
2569
2570 if (m == MATCH_NO)
2571 {
2572 gfc_error ("Missing dimension specification at %C");
2573 m = MATCH_ERROR;
2574 }
2575
2576 if (m == MATCH_ERROR)
2577 goto cleanup;
2578 }
2579 }
2580
2581 /* No double colon, so assume that we've been looking at something
2582 else the whole time. */
2583 if (d == DECL_NONE)
2584 {
2585 m = MATCH_NO;
2586 goto cleanup;
2587 }
2588
2589 /* Since we've seen a double colon, we have to be looking at an
2590 attr-spec. This means that we can now issue errors. */
2591 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2592 if (seen[d] > 1)
2593 {
2594 switch (d)
2595 {
2596 case DECL_ALLOCATABLE:
2597 attr = "ALLOCATABLE";
2598 break;
2599 case DECL_DIMENSION:
2600 attr = "DIMENSION";
2601 break;
2602 case DECL_EXTERNAL:
2603 attr = "EXTERNAL";
2604 break;
2605 case DECL_IN:
2606 attr = "INTENT (IN)";
2607 break;
2608 case DECL_OUT:
2609 attr = "INTENT (OUT)";
2610 break;
2611 case DECL_INOUT:
2612 attr = "INTENT (IN OUT)";
2613 break;
2614 case DECL_INTRINSIC:
2615 attr = "INTRINSIC";
2616 break;
2617 case DECL_OPTIONAL:
2618 attr = "OPTIONAL";
2619 break;
2620 case DECL_PARAMETER:
2621 attr = "PARAMETER";
2622 break;
2623 case DECL_POINTER:
2624 attr = "POINTER";
2625 break;
ee7e677f
TB
2626 case DECL_PROTECTED:
2627 attr = "PROTECTED";
2628 break;
6de9cd9a
DN
2629 case DECL_PRIVATE:
2630 attr = "PRIVATE";
2631 break;
2632 case DECL_PUBLIC:
2633 attr = "PUBLIC";
2634 break;
2635 case DECL_SAVE:
2636 attr = "SAVE";
2637 break;
2638 case DECL_TARGET:
2639 attr = "TARGET";
2640 break;
a8b3b0b6
CR
2641 case DECL_IS_BIND_C:
2642 attr = "IS_BIND_C";
2643 break;
2644 case DECL_VALUE:
2645 attr = "VALUE";
2646 break;
775e6c3a
TB
2647 case DECL_VOLATILE:
2648 attr = "VOLATILE";
2649 break;
6de9cd9a 2650 default:
66e4ab31 2651 attr = NULL; /* This shouldn't happen. */
6de9cd9a
DN
2652 }
2653
2654 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2655 m = MATCH_ERROR;
2656 goto cleanup;
2657 }
2658
2659 /* Now that we've dealt with duplicate attributes, add the attributes
2660 to the current attribute. */
2661 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2662 {
2663 if (seen[d] == 0)
2664 continue;
2665
2666 if (gfc_current_state () == COMP_DERIVED
2667 && d != DECL_DIMENSION && d != DECL_POINTER
d51347f9
TB
2668 && d != DECL_COLON && d != DECL_PRIVATE
2669 && d != DECL_PUBLIC && d != DECL_NONE)
6de9cd9a 2670 {
5046aff5
PT
2671 if (d == DECL_ALLOCATABLE)
2672 {
636dff67
SK
2673 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2674 "attribute at %C in a TYPE definition")
d51347f9 2675 == FAILURE)
5046aff5
PT
2676 {
2677 m = MATCH_ERROR;
2678 goto cleanup;
2679 }
636dff67
SK
2680 }
2681 else
5046aff5
PT
2682 {
2683 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
d51347f9 2684 &seen_at[d]);
5046aff5
PT
2685 m = MATCH_ERROR;
2686 goto cleanup;
2687 }
6de9cd9a
DN
2688 }
2689
4213f93b 2690 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
636dff67 2691 && gfc_current_state () != COMP_MODULE)
4213f93b
PT
2692 {
2693 if (d == DECL_PRIVATE)
2694 attr = "PRIVATE";
2695 else
2696 attr = "PUBLIC";
d51347f9
TB
2697 if (gfc_current_state () == COMP_DERIVED
2698 && gfc_state_stack->previous
2699 && gfc_state_stack->previous->state == COMP_MODULE)
2700 {
2701 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2702 "at %L in a TYPE definition", attr,
2703 &seen_at[d])
2704 == FAILURE)
2705 {
2706 m = MATCH_ERROR;
2707 goto cleanup;
2708 }
2709 }
2710 else
2711 {
2712 gfc_error ("%s attribute at %L is not allowed outside of the "
2713 "specification part of a module", attr, &seen_at[d]);
2714 m = MATCH_ERROR;
2715 goto cleanup;
2716 }
4213f93b
PT
2717 }
2718
6de9cd9a
DN
2719 switch (d)
2720 {
2721 case DECL_ALLOCATABLE:
2722 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2723 break;
2724
2725 case DECL_DIMENSION:
231b2fcc 2726 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2727 break;
2728
2729 case DECL_EXTERNAL:
2730 t = gfc_add_external (&current_attr, &seen_at[d]);
2731 break;
2732
2733 case DECL_IN:
2734 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2735 break;
2736
2737 case DECL_OUT:
2738 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2739 break;
2740
2741 case DECL_INOUT:
2742 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2743 break;
2744
2745 case DECL_INTRINSIC:
2746 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2747 break;
2748
2749 case DECL_OPTIONAL:
2750 t = gfc_add_optional (&current_attr, &seen_at[d]);
2751 break;
2752
2753 case DECL_PARAMETER:
231b2fcc 2754 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
6de9cd9a
DN
2755 break;
2756
2757 case DECL_POINTER:
2758 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2759 break;
2760
ee7e677f
TB
2761 case DECL_PROTECTED:
2762 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2763 {
2764 gfc_error ("PROTECTED at %C only allowed in specification "
2765 "part of a module");
2766 t = FAILURE;
2767 break;
2768 }
2769
636dff67
SK
2770 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2771 "attribute at %C")
ee7e677f
TB
2772 == FAILURE)
2773 t = FAILURE;
2774 else
2775 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2776 break;
2777
6de9cd9a 2778 case DECL_PRIVATE:
231b2fcc
TS
2779 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2780 &seen_at[d]);
6de9cd9a
DN
2781 break;
2782
2783 case DECL_PUBLIC:
231b2fcc
TS
2784 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2785 &seen_at[d]);
6de9cd9a
DN
2786 break;
2787
2788 case DECL_SAVE:
231b2fcc 2789 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2790 break;
2791
2792 case DECL_TARGET:
2793 t = gfc_add_target (&current_attr, &seen_at[d]);
2794 break;
2795
a8b3b0b6
CR
2796 case DECL_IS_BIND_C:
2797 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
2798 break;
2799
06469efd 2800 case DECL_VALUE:
636dff67
SK
2801 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2802 "at %C")
06469efd
PT
2803 == FAILURE)
2804 t = FAILURE;
2805 else
2806 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
2807 break;
2808
775e6c3a
TB
2809 case DECL_VOLATILE:
2810 if (gfc_notify_std (GFC_STD_F2003,
636dff67 2811 "Fortran 2003: VOLATILE attribute at %C")
775e6c3a
TB
2812 == FAILURE)
2813 t = FAILURE;
2814 else
2815 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
2816 break;
2817
6de9cd9a
DN
2818 default:
2819 gfc_internal_error ("match_attr_spec(): Bad attribute");
2820 }
2821
2822 if (t == FAILURE)
2823 {
2824 m = MATCH_ERROR;
2825 goto cleanup;
2826 }
2827 }
2828
2829 colon_seen = 1;
2830 return MATCH_YES;
2831
2832cleanup:
63645982 2833 gfc_current_locus = start;
6de9cd9a
DN
2834 gfc_free_array_spec (current_as);
2835 current_as = NULL;
2836 return m;
2837}
2838
2839
a8b3b0b6
CR
2840/* Set the binding label, dest_label, either with the binding label
2841 stored in the given gfc_typespec, ts, or if none was provided, it
2842 will be the symbol name in all lower case, as required by the draft
2843 (J3/04-007, section 15.4.1). If a binding label was given and
2844 there is more than one argument (num_idents), it is an error. */
2845
2846try
2847set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2848{
2849 if (curr_binding_label[0] != '\0')
2850 {
2851 if (num_idents > 1 || num_idents_on_line > 1)
2852 {
2853 gfc_error ("Multiple identifiers provided with "
2854 "single NAME= specifier at %C");
2855 return FAILURE;
2856 }
2857
2858 /* Binding label given; store in temp holder til have sym. */
2859 strncpy (dest_label, curr_binding_label,
2860 strlen (curr_binding_label) + 1);
2861 }
2862 else
2863 {
2864 /* No binding label given, and the NAME= specifier did not exist,
2865 which means there was no NAME="". */
2866 if (sym_name != NULL && has_name_equals == 0)
2867 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2868 }
2869
2870 return SUCCESS;
2871}
2872
2873
2874/* Set the status of the given common block as being BIND(C) or not,
2875 depending on the given parameter, is_bind_c. */
2876
2877void
2878set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2879{
2880 com_block->is_bind_c = is_bind_c;
2881 return;
2882}
2883
2884
2885/* Verify that the given gfc_typespec is for a C interoperable type. */
2886
2887try
2888verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2889{
2890 try t;
2891
2892 /* Make sure the kind used is appropriate for the type.
2893 The f90_type is unknown if an integer constant was
2894 used (e.g., real(4), bind(c) :: myFloat). */
2895 if (ts->f90_type != BT_UNKNOWN)
2896 {
2897 t = gfc_validate_c_kind (ts);
2898 if (t != SUCCESS)
2899 {
2900 /* Print an error, but continue parsing line. */
2901 gfc_error_now ("C kind parameter is for type %s but "
2902 "symbol '%s' at %L is of type %s",
2903 gfc_basic_typename (ts->f90_type),
2904 name, where,
2905 gfc_basic_typename (ts->type));
2906 }
2907 }
2908
2909 /* Make sure the kind is C interoperable. This does not care about the
2910 possible error above. */
2911 if (ts->type == BT_DERIVED && ts->derived != NULL)
2912 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2913 else if (ts->is_c_interop != 1)
2914 return FAILURE;
2915
2916 return SUCCESS;
2917}
2918
2919
2920/* Verify that the variables of a given common block, which has been
2921 defined with the attribute specifier bind(c), to be of a C
2922 interoperable type. Errors will be reported here, if
2923 encountered. */
2924
2925try
2926verify_com_block_vars_c_interop (gfc_common_head *com_block)
2927{
2928 gfc_symbol *curr_sym = NULL;
2929 try retval = SUCCESS;
2930
2931 curr_sym = com_block->head;
2932
2933 /* Make sure we have at least one symbol. */
2934 if (curr_sym == NULL)
2935 return retval;
2936
2937 /* Here we know we have a symbol, so we'll execute this loop
2938 at least once. */
2939 do
2940 {
2941 /* The second to last param, 1, says this is in a common block. */
2942 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2943 curr_sym = curr_sym->common_next;
2944 } while (curr_sym != NULL);
2945
2946 return retval;
2947}
2948
2949
2950/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2951 an appropriate error message is reported. */
2952
2953try
2954verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2955 int is_in_common, gfc_common_head *com_block)
2956{
2957 try retval = SUCCESS;
d8fa96e0
CR
2958
2959 if (tmp_sym->attr.function && tmp_sym->result != NULL)
2960 {
2961 tmp_sym = tmp_sym->result;
2962 /* Make sure it wasn't an implicitly typed result. */
2963 if (tmp_sym->attr.implicit_type)
2964 {
2965 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
2966 "%L may not be C interoperable", tmp_sym->name,
2967 &tmp_sym->declared_at);
2968 tmp_sym->ts.f90_type = tmp_sym->ts.type;
2969 /* Mark it as C interoperable to prevent duplicate warnings. */
2970 tmp_sym->ts.is_c_interop = 1;
2971 tmp_sym->attr.is_c_interop = 1;
2972 }
2973 }
a8b3b0b6
CR
2974
2975 /* Here, we know we have the bind(c) attribute, so if we have
2976 enough type info, then verify that it's a C interop kind.
2977 The info could be in the symbol already, or possibly still in
2978 the given ts (current_ts), so look in both. */
2979 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2980 {
2981 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2982 &(tmp_sym->declared_at)) != SUCCESS)
2983 {
2984 /* See if we're dealing with a sym in a common block or not. */
2985 if (is_in_common == 1)
2986 {
2987 gfc_warning ("Variable '%s' in common block '%s' at %L "
2988 "may not be a C interoperable "
2989 "kind though common block '%s' is BIND(C)",
2990 tmp_sym->name, com_block->name,
2991 &(tmp_sym->declared_at), com_block->name);
2992 }
2993 else
2994 {
2995 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2996 gfc_error ("Type declaration '%s' at %L is not C "
2997 "interoperable but it is BIND(C)",
2998 tmp_sym->name, &(tmp_sym->declared_at));
2999 else
3000 gfc_warning ("Variable '%s' at %L "
3001 "may not be a C interoperable "
3002 "kind but it is bind(c)",
3003 tmp_sym->name, &(tmp_sym->declared_at));
3004 }
3005 }
3006
3007 /* Variables declared w/in a common block can't be bind(c)
3008 since there's no way for C to see these variables, so there's
3009 semantically no reason for the attribute. */
3010 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3011 {
3012 gfc_error ("Variable '%s' in common block '%s' at "
3013 "%L cannot be declared with BIND(C) "
3014 "since it is not a global",
3015 tmp_sym->name, com_block->name,
3016 &(tmp_sym->declared_at));
3017 retval = FAILURE;
3018 }
3019
3020 /* Scalar variables that are bind(c) can not have the pointer
3021 or allocatable attributes. */
3022 if (tmp_sym->attr.is_bind_c == 1)
3023 {
3024 if (tmp_sym->attr.pointer == 1)
3025 {
3026 gfc_error ("Variable '%s' at %L cannot have both the "
3027 "POINTER and BIND(C) attributes",
3028 tmp_sym->name, &(tmp_sym->declared_at));
3029 retval = FAILURE;
3030 }
3031
3032 if (tmp_sym->attr.allocatable == 1)
3033 {
3034 gfc_error ("Variable '%s' at %L cannot have both the "
3035 "ALLOCATABLE and BIND(C) attributes",
3036 tmp_sym->name, &(tmp_sym->declared_at));
3037 retval = FAILURE;
3038 }
3039
3040 /* If it is a BIND(C) function, make sure the return value is a
3041 scalar value. The previous tests in this function made sure
3042 the type is interoperable. */
3043 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3044 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3045 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3046
3047 /* BIND(C) functions can not return a character string. */
3048 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3049 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3050 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3051 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3052 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3053 "be a character string", tmp_sym->name,
3054 &(tmp_sym->declared_at));
3055 }
3056 }
3057
3058 /* See if the symbol has been marked as private. If it has, make sure
3059 there is no binding label and warn the user if there is one. */
3060 if (tmp_sym->attr.access == ACCESS_PRIVATE
3061 && tmp_sym->binding_label[0] != '\0')
3062 /* Use gfc_warning_now because we won't say that the symbol fails
3063 just because of this. */
3064 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3065 "given the binding label '%s'", tmp_sym->name,
3066 &(tmp_sym->declared_at), tmp_sym->binding_label);
3067
3068 return retval;
3069}
3070
3071
3072/* Set the appropriate fields for a symbol that's been declared as
3073 BIND(C) (the is_bind_c flag and the binding label), and verify that
3074 the type is C interoperable. Errors are reported by the functions
3075 used to set/test these fields. */
3076
3077try
3078set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3079{
3080 try retval = SUCCESS;
3081
3082 /* TODO: Do we need to make sure the vars aren't marked private? */
3083
3084 /* Set the is_bind_c bit in symbol_attribute. */
3085 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3086
3087 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3088 num_idents) != SUCCESS)
3089 return FAILURE;
3090
3091 return retval;
3092}
3093
3094
3095/* Set the fields marking the given common block as BIND(C), including
3096 a binding label, and report any errors encountered. */
3097
3098try
3099set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3100{
3101 try retval = SUCCESS;
3102
3103 /* destLabel, common name, typespec (which may have binding label). */
3104 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3105 != SUCCESS)
3106 return FAILURE;
3107
3108 /* Set the given common block (com_block) to being bind(c) (1). */
3109 set_com_block_bind_c (com_block, 1);
3110
3111 return retval;
3112}
3113
3114
3115/* Retrieve the list of one or more identifiers that the given bind(c)
3116 attribute applies to. */
3117
3118try
3119get_bind_c_idents (void)
3120{
3121 char name[GFC_MAX_SYMBOL_LEN + 1];
3122 int num_idents = 0;
3123 gfc_symbol *tmp_sym = NULL;
3124 match found_id;
3125 gfc_common_head *com_block = NULL;
3126
3127 if (gfc_match_name (name) == MATCH_YES)
3128 {
3129 found_id = MATCH_YES;
3130 gfc_get_ha_symbol (name, &tmp_sym);
3131 }
3132 else if (match_common_name (name) == MATCH_YES)
3133 {
3134 found_id = MATCH_YES;
3135 com_block = gfc_get_common (name, 0);
3136 }
3137 else
3138 {
3139 gfc_error ("Need either entity or common block name for "
3140 "attribute specification statement at %C");
3141 return FAILURE;
3142 }
3143
3144 /* Save the current identifier and look for more. */
3145 do
3146 {
3147 /* Increment the number of identifiers found for this spec stmt. */
3148 num_idents++;
3149
3150 /* Make sure we have a sym or com block, and verify that it can
3151 be bind(c). Set the appropriate field(s) and look for more
3152 identifiers. */
3153 if (tmp_sym != NULL || com_block != NULL)
3154 {
3155 if (tmp_sym != NULL)
3156 {
3157 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3158 != SUCCESS)
3159 return FAILURE;
3160 }
3161 else
3162 {
3163 if (set_verify_bind_c_com_block(com_block, num_idents)
3164 != SUCCESS)
3165 return FAILURE;
3166 }
3167
3168 /* Look to see if we have another identifier. */
3169 tmp_sym = NULL;
3170 if (gfc_match_eos () == MATCH_YES)
3171 found_id = MATCH_NO;
3172 else if (gfc_match_char (',') != MATCH_YES)
3173 found_id = MATCH_NO;
3174 else if (gfc_match_name (name) == MATCH_YES)
3175 {
3176 found_id = MATCH_YES;
3177 gfc_get_ha_symbol (name, &tmp_sym);
3178 }
3179 else if (match_common_name (name) == MATCH_YES)
3180 {
3181 found_id = MATCH_YES;
3182 com_block = gfc_get_common (name, 0);
3183 }
3184 else
3185 {
3186 gfc_error ("Missing entity or common block name for "
3187 "attribute specification statement at %C");
3188 return FAILURE;
3189 }
3190 }
3191 else
3192 {
3193 gfc_internal_error ("Missing symbol");
3194 }
3195 } while (found_id == MATCH_YES);
3196
3197 /* if we get here we were successful */
3198 return SUCCESS;
3199}
3200
3201
3202/* Try and match a BIND(C) attribute specification statement. */
3203
3204match
3205gfc_match_bind_c_stmt (void)
3206{
3207 match found_match = MATCH_NO;
3208 gfc_typespec *ts;
3209
3210 ts = &current_ts;
3211
3212 /* This may not be necessary. */
3213 gfc_clear_ts (ts);
3214 /* Clear the temporary binding label holder. */
3215 curr_binding_label[0] = '\0';
3216
3217 /* Look for the bind(c). */
3218 found_match = gfc_match_bind_c (NULL);
3219
3220 if (found_match == MATCH_YES)
3221 {
3222 /* Look for the :: now, but it is not required. */
3223 gfc_match (" :: ");
3224
3225 /* Get the identifier(s) that needs to be updated. This may need to
3226 change to hand the flag(s) for the attr specified so all identifiers
3227 found can have all appropriate parts updated (assuming that the same
3228 spec stmt can have multiple attrs, such as both bind(c) and
3229 allocatable...). */
3230 if (get_bind_c_idents () != SUCCESS)
3231 /* Error message should have printed already. */
3232 return MATCH_ERROR;
3233 }
3234
3235 return found_match;
3236}
3237
3238
6de9cd9a
DN
3239/* Match a data declaration statement. */
3240
3241match
3242gfc_match_data_decl (void)
3243{
3244 gfc_symbol *sym;
3245 match m;
949d5b72 3246 int elem;
6de9cd9a 3247
a8b3b0b6
CR
3248 num_idents_on_line = 0;
3249
e5ddaa24 3250 m = match_type_spec (&current_ts, 0);
6de9cd9a
DN
3251 if (m != MATCH_YES)
3252 return m;
3253
3254 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3255 {
3256 sym = gfc_use_derived (current_ts.derived);
3257
3258 if (sym == NULL)
3259 {
3260 m = MATCH_ERROR;
3261 goto cleanup;
3262 }
3263
3264 current_ts.derived = sym;
3265 }
3266
3267 m = match_attr_spec ();
3268 if (m == MATCH_ERROR)
3269 {
3270 m = MATCH_NO;
3271 goto cleanup;
3272 }
3273
3274 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3275 {
3276
3277 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3278 goto ok;
3279
976e21f6 3280 gfc_find_symbol (current_ts.derived->name,
636dff67 3281 current_ts.derived->ns->parent, 1, &sym);
6de9cd9a 3282
976e21f6 3283 /* Any symbol that we find had better be a type definition
636dff67 3284 which has its components defined. */
976e21f6 3285 if (sym != NULL && sym->attr.flavor == FL_DERIVED
636dff67 3286 && current_ts.derived->components != NULL)
6de9cd9a
DN
3287 goto ok;
3288
976e21f6
PT
3289 /* Now we have an error, which we signal, and then fix up
3290 because the knock-on is plain and simple confusing. */
3291 gfc_error_now ("Derived type at %C has not been previously defined "
636dff67 3292 "and so cannot appear in a derived type definition");
976e21f6
PT
3293 current_attr.pointer = 1;
3294 goto ok;
6de9cd9a
DN
3295 }
3296
3297ok:
3298 /* If we have an old-style character declaration, and no new-style
3299 attribute specifications, then there a comma is optional between
3300 the type specification and the variable list. */
3301 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3302 gfc_match_char (',');
3303
949d5b72
PT
3304 /* Give the types/attributes to symbols that follow. Give the element
3305 a number so that repeat character length expressions can be copied. */
3306 elem = 1;
6de9cd9a
DN
3307 for (;;)
3308 {
a8b3b0b6 3309 num_idents_on_line++;
949d5b72 3310 m = variable_decl (elem++);
6de9cd9a
DN
3311 if (m == MATCH_ERROR)
3312 goto cleanup;
3313 if (m == MATCH_NO)
3314 break;
3315
3316 if (gfc_match_eos () == MATCH_YES)
3317 goto cleanup;
3318 if (gfc_match_char (',') != MATCH_YES)
3319 break;
3320 }
3321
8f81c3c6
PT
3322 if (gfc_error_flag_test () == 0)
3323 gfc_error ("Syntax error in data declaration at %C");
6de9cd9a
DN
3324 m = MATCH_ERROR;
3325
a9f6f1f2
JD
3326 gfc_free_data_all (gfc_current_ns);
3327
6de9cd9a
DN
3328cleanup:
3329 gfc_free_array_spec (current_as);
3330 current_as = NULL;
3331 return m;
3332}
3333
3334
3335/* Match a prefix associated with a function or subroutine
3336 declaration. If the typespec pointer is nonnull, then a typespec
3337 can be matched. Note that if nothing matches, MATCH_YES is
3338 returned (the null string was matched). */
3339
3340static match
636dff67 3341match_prefix (gfc_typespec *ts)
6de9cd9a
DN
3342{
3343 int seen_type;
3344
3345 gfc_clear_attr (&current_attr);
3346 seen_type = 0;
3347
3348loop:
3349 if (!seen_type && ts != NULL
e5ddaa24 3350 && match_type_spec (ts, 0) == MATCH_YES
6de9cd9a
DN
3351 && gfc_match_space () == MATCH_YES)
3352 {
3353
3354 seen_type = 1;
3355 goto loop;
3356 }
3357
3358 if (gfc_match ("elemental% ") == MATCH_YES)
3359 {
3360 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3361 return MATCH_ERROR;
3362
3363 goto loop;
3364 }
3365
3366 if (gfc_match ("pure% ") == MATCH_YES)
3367 {
3368 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3369 return MATCH_ERROR;
3370
3371 goto loop;
3372 }
3373
3374 if (gfc_match ("recursive% ") == MATCH_YES)
3375 {
3376 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3377 return MATCH_ERROR;
3378
3379 goto loop;
3380 }
3381
3382 /* At this point, the next item is not a prefix. */
3383 return MATCH_YES;
3384}
3385
3386
3387/* Copy attributes matched by match_prefix() to attributes on a symbol. */
3388
3389static try
636dff67 3390copy_prefix (symbol_attribute *dest, locus *where)
6de9cd9a 3391{
6de9cd9a
DN
3392 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3393 return FAILURE;
3394
3395 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3396 return FAILURE;
3397
3398 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3399 return FAILURE;
3400
3401 return SUCCESS;
3402}
3403
3404
3405/* Match a formal argument list. */
3406
3407match
636dff67 3408gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
6de9cd9a
DN
3409{
3410 gfc_formal_arglist *head, *tail, *p, *q;
3411 char name[GFC_MAX_SYMBOL_LEN + 1];
3412 gfc_symbol *sym;
3413 match m;
3414
3415 head = tail = NULL;
3416
3417 if (gfc_match_char ('(') != MATCH_YES)
3418 {
3419 if (null_flag)
3420 goto ok;
3421 return MATCH_NO;
3422 }
3423
3424 if (gfc_match_char (')') == MATCH_YES)
3425 goto ok;
3426
3427 for (;;)
3428 {
3429 if (gfc_match_char ('*') == MATCH_YES)
3430 sym = NULL;
3431 else
3432 {
3433 m = gfc_match_name (name);
3434 if (m != MATCH_YES)
3435 goto cleanup;
3436
3437 if (gfc_get_symbol (name, NULL, &sym))
3438 goto cleanup;
3439 }
3440
3441 p = gfc_get_formal_arglist ();
3442
3443 if (head == NULL)
3444 head = tail = p;
3445 else
3446 {
3447 tail->next = p;
3448 tail = p;
3449 }
3450
3451 tail->sym = sym;
3452
3453 /* We don't add the VARIABLE flavor because the name could be a
636dff67
SK
3454 dummy procedure. We don't apply these attributes to formal
3455 arguments of statement functions. */
6de9cd9a 3456 if (sym != NULL && !st_flag
231b2fcc 3457 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
6de9cd9a
DN
3458 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3459 {
3460 m = MATCH_ERROR;
3461 goto cleanup;
3462 }
3463
3464 /* The name of a program unit can be in a different namespace,
636dff67
SK
3465 so check for it explicitly. After the statement is accepted,
3466 the name is checked for especially in gfc_get_symbol(). */
6de9cd9a
DN
3467 if (gfc_new_block != NULL && sym != NULL
3468 && strcmp (sym->name, gfc_new_block->name) == 0)
3469 {
3470 gfc_error ("Name '%s' at %C is the name of the procedure",
3471 sym->name);
3472 m = MATCH_ERROR;
3473 goto cleanup;
3474 }
3475
3476 if (gfc_match_char (')') == MATCH_YES)
3477 goto ok;
3478
3479 m = gfc_match_char (',');
3480 if (m != MATCH_YES)
3481 {
3482 gfc_error ("Unexpected junk in formal argument list at %C");
3483 goto cleanup;
3484 }
3485 }
3486
3487ok:
3488 /* Check for duplicate symbols in the formal argument list. */
3489 if (head != NULL)
3490 {
3491 for (p = head; p->next; p = p->next)
3492 {
3493 if (p->sym == NULL)
3494 continue;
3495
3496 for (q = p->next; q; q = q->next)
3497 if (p->sym == q->sym)
3498 {
636dff67
SK
3499 gfc_error ("Duplicate symbol '%s' in formal argument list "
3500 "at %C", p->sym->name);
6de9cd9a
DN
3501
3502 m = MATCH_ERROR;
3503 goto cleanup;
3504 }
3505 }
3506 }
3507
66e4ab31
SK
3508 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3509 == FAILURE)
6de9cd9a
DN
3510 {
3511 m = MATCH_ERROR;
3512 goto cleanup;
3513 }
3514
3515 return MATCH_YES;
3516
3517cleanup:
3518 gfc_free_formal_arglist (head);
3519 return m;
3520}
3521
3522
3523/* Match a RESULT specification following a function declaration or
3524 ENTRY statement. Also matches the end-of-statement. */
3525
3526static match
66e4ab31 3527match_result (gfc_symbol *function, gfc_symbol **result)
6de9cd9a
DN
3528{
3529 char name[GFC_MAX_SYMBOL_LEN + 1];
3530 gfc_symbol *r;
3531 match m;
3532
3533 if (gfc_match (" result (") != MATCH_YES)
3534 return MATCH_NO;
3535
3536 m = gfc_match_name (name);
3537 if (m != MATCH_YES)
3538 return m;
3539
a8b3b0b6
CR
3540 /* Get the right paren, and that's it because there could be the
3541 bind(c) attribute after the result clause. */
3542 if (gfc_match_char(')') != MATCH_YES)
6de9cd9a 3543 {
a8b3b0b6 3544 /* TODO: should report the missing right paren here. */
6de9cd9a
DN
3545 return MATCH_ERROR;
3546 }
3547
3548 if (strcmp (function->name, name) == 0)
3549 {
636dff67 3550 gfc_error ("RESULT variable at %C must be different than function name");
6de9cd9a
DN
3551 return MATCH_ERROR;
3552 }
3553
3554 if (gfc_get_symbol (name, NULL, &r))
3555 return MATCH_ERROR;
3556
231b2fcc
TS
3557 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3558 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
6de9cd9a
DN
3559 return MATCH_ERROR;
3560
3561 *result = r;
3562
3563 return MATCH_YES;
3564}
3565
3566
a8b3b0b6
CR
3567/* Match a function suffix, which could be a combination of a result
3568 clause and BIND(C), either one, or neither. The draft does not
3569 require them to come in a specific order. */
3570
3571match
3572gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3573{
3574 match is_bind_c; /* Found bind(c). */
3575 match is_result; /* Found result clause. */
3576 match found_match; /* Status of whether we've found a good match. */
3577 int peek_char; /* Character we're going to peek at. */
3578
3579 /* Initialize to having found nothing. */
3580 found_match = MATCH_NO;
3581 is_bind_c = MATCH_NO;
3582 is_result = MATCH_NO;
3583
3584 /* Get the next char to narrow between result and bind(c). */
3585 gfc_gobble_whitespace ();
3586 peek_char = gfc_peek_char ();
3587
3588 switch (peek_char)
3589 {
3590 case 'r':
3591 /* Look for result clause. */
3592 is_result = match_result (sym, result);
3593 if (is_result == MATCH_YES)
3594 {
3595 /* Now see if there is a bind(c) after it. */
3596 is_bind_c = gfc_match_bind_c (sym);
3597 /* We've found the result clause and possibly bind(c). */
3598 found_match = MATCH_YES;
3599 }
3600 else
3601 /* This should only be MATCH_ERROR. */
3602 found_match = is_result;
3603 break;
3604 case 'b':
3605 /* Look for bind(c) first. */
3606 is_bind_c = gfc_match_bind_c (sym);
3607 if (is_bind_c == MATCH_YES)
3608 {
3609 /* Now see if a result clause followed it. */
3610 is_result = match_result (sym, result);
3611 found_match = MATCH_YES;
3612 }
3613 else
3614 {
3615 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3616 found_match = MATCH_ERROR;
3617 }
3618 break;
3619 default:
3620 gfc_error ("Unexpected junk after function declaration at %C");
3621 found_match = MATCH_ERROR;
3622 break;
3623 }
3624
a8b3b0b6
CR
3625 if (is_bind_c == MATCH_YES)
3626 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3627 == FAILURE)
3628 return MATCH_ERROR;
3629
3630 return found_match;
3631}
3632
3633
6de9cd9a
DN
3634/* Match a function declaration. */
3635
3636match
3637gfc_match_function_decl (void)
3638{
3639 char name[GFC_MAX_SYMBOL_LEN + 1];
3640 gfc_symbol *sym, *result;
3641 locus old_loc;
3642 match m;
a8b3b0b6
CR
3643 match suffix_match;
3644 match found_match; /* Status returned by match func. */
6de9cd9a
DN
3645
3646 if (gfc_current_state () != COMP_NONE
3647 && gfc_current_state () != COMP_INTERFACE
3648 && gfc_current_state () != COMP_CONTAINS)
3649 return MATCH_NO;
3650
3651 gfc_clear_ts (&current_ts);
3652
63645982 3653 old_loc = gfc_current_locus;
6de9cd9a
DN
3654
3655 m = match_prefix (&current_ts);
3656 if (m != MATCH_YES)
3657 {
63645982 3658 gfc_current_locus = old_loc;
6de9cd9a
DN
3659 return m;
3660 }
3661
3662 if (gfc_match ("function% %n", name) != MATCH_YES)
3663 {
63645982 3664 gfc_current_locus = old_loc;
6de9cd9a
DN
3665 return MATCH_NO;
3666 }
1a492601 3667 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
3668 return MATCH_ERROR;
3669 gfc_new_block = sym;
3670
3671 m = gfc_match_formal_arglist (sym, 0, 0);
3672 if (m == MATCH_NO)
2b9a33ae
TS
3673 {
3674 gfc_error ("Expected formal argument list in function "
636dff67 3675 "definition at %C");
2b9a33ae
TS
3676 m = MATCH_ERROR;
3677 goto cleanup;
3678 }
6de9cd9a
DN
3679 else if (m == MATCH_ERROR)
3680 goto cleanup;
3681
3682 result = NULL;
3683
a8b3b0b6
CR
3684 /* According to the draft, the bind(c) and result clause can
3685 come in either order after the formal_arg_list (i.e., either
3686 can be first, both can exist together or by themselves or neither
3687 one). Therefore, the match_result can't match the end of the
3688 string, and check for the bind(c) or result clause in either order. */
3689 found_match = gfc_match_eos ();
3690
3691 /* Make sure that it isn't already declared as BIND(C). If it is, it
3692 must have been marked BIND(C) with a BIND(C) attribute and that is
3693 not allowed for procedures. */
3694 if (sym->attr.is_bind_c == 1)
3695 {
3696 sym->attr.is_bind_c = 0;
3697 if (sym->old_symbol != NULL)
3698 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3699 "variables or common blocks",
3700 &(sym->old_symbol->declared_at));
3701 else
3702 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3703 "variables or common blocks", &gfc_current_locus);
6de9cd9a
DN
3704 }
3705
a8b3b0b6 3706 if (found_match != MATCH_YES)
6de9cd9a 3707 {
a8b3b0b6
CR
3708 /* If we haven't found the end-of-statement, look for a suffix. */
3709 suffix_match = gfc_match_suffix (sym, &result);
3710 if (suffix_match == MATCH_YES)
3711 /* Need to get the eos now. */
3712 found_match = gfc_match_eos ();
3713 else
3714 found_match = suffix_match;
6de9cd9a
DN
3715 }
3716
a8b3b0b6
CR
3717 if(found_match != MATCH_YES)
3718 m = MATCH_ERROR;
6de9cd9a
DN
3719 else
3720 {
a8b3b0b6
CR
3721 /* Make changes to the symbol. */
3722 m = MATCH_ERROR;
3723
3724 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3725 goto cleanup;
3726
3727 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3728 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3729 goto cleanup;
6de9cd9a 3730
a8b3b0b6
CR
3731 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3732 && !sym->attr.implicit_type)
3733 {
3734 gfc_error ("Function '%s' at %C already has a type of %s", name,
3735 gfc_basic_typename (sym->ts.type));
3736 goto cleanup;
3737 }
3738
3739 if (result == NULL)
3740 {
3741 sym->ts = current_ts;
3742 sym->result = sym;
3743 }
3744 else
3745 {
3746 result->ts = current_ts;
3747 sym->result = result;
3748 }
3749
3750 return MATCH_YES;
3751 }
6de9cd9a
DN
3752
3753cleanup:
63645982 3754 gfc_current_locus = old_loc;
6de9cd9a
DN
3755 return m;
3756}
3757
636dff67
SK
3758
3759/* This is mostly a copy of parse.c(add_global_procedure) but modified to
3760 pass the name of the entry, rather than the gfc_current_block name, and
3761 to return false upon finding an existing global entry. */
68ea355b
PT
3762
3763static bool
636dff67 3764add_global_entry (const char *name, int sub)
68ea355b
PT
3765{
3766 gfc_gsymbol *s;
3767
3768 s = gfc_get_gsymbol(name);
3769
3770 if (s->defined
636dff67
SK
3771 || (s->type != GSYM_UNKNOWN
3772 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
68ea355b
PT
3773 global_used(s, NULL);
3774 else
3775 {
3776 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3777 s->where = gfc_current_locus;
3778 s->defined = 1;
3779 return true;
3780 }
3781 return false;
3782}
6de9cd9a 3783
636dff67 3784
6de9cd9a
DN
3785/* Match an ENTRY statement. */
3786
3787match
3788gfc_match_entry (void)
3789{
3d79abbd
PB
3790 gfc_symbol *proc;
3791 gfc_symbol *result;
3792 gfc_symbol *entry;
6de9cd9a
DN
3793 char name[GFC_MAX_SYMBOL_LEN + 1];
3794 gfc_compile_state state;
3795 match m;
3d79abbd 3796 gfc_entry_list *el;
c96cfa49 3797 locus old_loc;
1a492601 3798 bool module_procedure;
6de9cd9a
DN
3799
3800 m = gfc_match_name (name);
3801 if (m != MATCH_YES)
3802 return m;
3803
3d79abbd 3804 state = gfc_current_state ();
4c93c95a 3805 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 3806 {
4c93c95a
FXC
3807 switch (state)
3808 {
3809 case COMP_PROGRAM:
3810 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3811 break;
3812 case COMP_MODULE:
3813 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3814 break;
3815 case COMP_BLOCK_DATA:
636dff67
SK
3816 gfc_error ("ENTRY statement at %C cannot appear within "
3817 "a BLOCK DATA");
4c93c95a
FXC
3818 break;
3819 case COMP_INTERFACE:
636dff67
SK
3820 gfc_error ("ENTRY statement at %C cannot appear within "
3821 "an INTERFACE");
4c93c95a
FXC
3822 break;
3823 case COMP_DERIVED:
636dff67
SK
3824 gfc_error ("ENTRY statement at %C cannot appear within "
3825 "a DERIVED TYPE block");
4c93c95a
FXC
3826 break;
3827 case COMP_IF:
636dff67
SK
3828 gfc_error ("ENTRY statement at %C cannot appear within "
3829 "an IF-THEN block");
4c93c95a
FXC
3830 break;
3831 case COMP_DO:
636dff67
SK
3832 gfc_error ("ENTRY statement at %C cannot appear within "
3833 "a DO block");
4c93c95a
FXC
3834 break;
3835 case COMP_SELECT:
636dff67
SK
3836 gfc_error ("ENTRY statement at %C cannot appear within "
3837 "a SELECT block");
4c93c95a
FXC
3838 break;
3839 case COMP_FORALL:
636dff67
SK
3840 gfc_error ("ENTRY statement at %C cannot appear within "
3841 "a FORALL block");
4c93c95a
FXC
3842 break;
3843 case COMP_WHERE:
636dff67
SK
3844 gfc_error ("ENTRY statement at %C cannot appear within "
3845 "a WHERE block");
4c93c95a
FXC
3846 break;
3847 case COMP_CONTAINS:
636dff67
SK
3848 gfc_error ("ENTRY statement at %C cannot appear within "
3849 "a contained subprogram");
4c93c95a
FXC
3850 break;
3851 default:
3852 gfc_internal_error ("gfc_match_entry(): Bad state");
3853 }
3d79abbd
PB
3854 return MATCH_ERROR;
3855 }
3856
1a492601 3857 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
3858 && gfc_current_ns->parent->proc_name
3859 && gfc_current_ns->parent->proc_name->attr.flavor
3860 == FL_MODULE;
1a492601 3861
3d79abbd
PB
3862 if (gfc_current_ns->parent != NULL
3863 && gfc_current_ns->parent->proc_name
1a492601 3864 && !module_procedure)
3d79abbd
PB
3865 {
3866 gfc_error("ENTRY statement at %C cannot appear in a "
3867 "contained procedure");
3868 return MATCH_ERROR;
3869 }
3870
1a492601
PT
3871 /* Module function entries need special care in get_proc_name
3872 because previous references within the function will have
3873 created symbols attached to the current namespace. */
3874 if (get_proc_name (name, &entry,
3875 gfc_current_ns->parent != NULL
3876 && module_procedure
3877 && gfc_current_ns->proc_name->attr.function))
6de9cd9a
DN
3878 return MATCH_ERROR;
3879
3d79abbd
PB
3880 proc = gfc_current_block ();
3881
3882 if (state == COMP_SUBROUTINE)
6de9cd9a 3883 {
231b2fcc 3884 /* An entry in a subroutine. */
68ea355b
PT
3885 if (!add_global_entry (name, 1))
3886 return MATCH_ERROR;
3887
6de9cd9a
DN
3888 m = gfc_match_formal_arglist (entry, 0, 1);
3889 if (m != MATCH_YES)
3890 return MATCH_ERROR;
3891
231b2fcc
TS
3892 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3893 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a 3894 return MATCH_ERROR;
3d79abbd
PB
3895 }
3896 else
3897 {
c96cfa49 3898 /* An entry in a function.
636dff67
SK
3899 We need to take special care because writing
3900 ENTRY f()
3901 as
3902 ENTRY f
3903 is allowed, whereas
3904 ENTRY f() RESULT (r)
3905 can't be written as
3906 ENTRY f RESULT (r). */
68ea355b
PT
3907 if (!add_global_entry (name, 0))
3908 return MATCH_ERROR;
3909
c96cfa49
TS
3910 old_loc = gfc_current_locus;
3911 if (gfc_match_eos () == MATCH_YES)
3912 {
3913 gfc_current_locus = old_loc;
3914 /* Match the empty argument list, and add the interface to
3915 the symbol. */
3916 m = gfc_match_formal_arglist (entry, 0, 1);
3917 }
3918 else
3919 m = gfc_match_formal_arglist (entry, 0, 0);
3920
6de9cd9a
DN
3921 if (m != MATCH_YES)
3922 return MATCH_ERROR;
3923
6de9cd9a
DN
3924 result = NULL;
3925
3926 if (gfc_match_eos () == MATCH_YES)
3927 {
231b2fcc
TS
3928 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3929 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a
DN
3930 return MATCH_ERROR;
3931
d198b59a 3932 entry->result = entry;
6de9cd9a
DN
3933 }
3934 else
3935 {
3d79abbd 3936 m = match_result (proc, &result);
6de9cd9a
DN
3937 if (m == MATCH_NO)
3938 gfc_syntax_error (ST_ENTRY);
3939 if (m != MATCH_YES)
3940 return MATCH_ERROR;
3941
231b2fcc
TS
3942 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3943 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
636dff67
SK
3944 || gfc_add_function (&entry->attr, result->name, NULL)
3945 == FAILURE)
6de9cd9a 3946 return MATCH_ERROR;
d198b59a
JJ
3947
3948 entry->result = result;
6de9cd9a 3949 }
6de9cd9a
DN
3950 }
3951
3952 if (gfc_match_eos () != MATCH_YES)
3953 {
3954 gfc_syntax_error (ST_ENTRY);
3955 return MATCH_ERROR;
3956 }
3957
3d79abbd
PB
3958 entry->attr.recursive = proc->attr.recursive;
3959 entry->attr.elemental = proc->attr.elemental;
3960 entry->attr.pure = proc->attr.pure;
6de9cd9a 3961
3d79abbd
PB
3962 el = gfc_get_entry_list ();
3963 el->sym = entry;
3964 el->next = gfc_current_ns->entries;
3965 gfc_current_ns->entries = el;
3966 if (el->next)
3967 el->id = el->next->id + 1;
3968 else
3969 el->id = 1;
6de9cd9a 3970
3d79abbd
PB
3971 new_st.op = EXEC_ENTRY;
3972 new_st.ext.entry = el;
3973
3974 return MATCH_YES;
6de9cd9a
DN
3975}
3976
3977
3978/* Match a subroutine statement, including optional prefixes. */
3979
3980match
3981gfc_match_subroutine (void)
3982{
3983 char name[GFC_MAX_SYMBOL_LEN + 1];
3984 gfc_symbol *sym;
3985 match m;
a8b3b0b6
CR
3986 match is_bind_c;
3987 char peek_char;
6de9cd9a
DN
3988
3989 if (gfc_current_state () != COMP_NONE
3990 && gfc_current_state () != COMP_INTERFACE
3991 && gfc_current_state () != COMP_CONTAINS)
3992 return MATCH_NO;
3993
3994 m = match_prefix (NULL);
3995 if (m != MATCH_YES)
3996 return m;
3997
3998 m = gfc_match ("subroutine% %n", name);
3999 if (m != MATCH_YES)
4000 return m;
4001
1a492601 4002 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
4003 return MATCH_ERROR;
4004 gfc_new_block = sym;
4005
a8b3b0b6
CR
4006 /* Check what next non-whitespace character is so we can tell if there
4007 where the required parens if we have a BIND(C). */
4008 gfc_gobble_whitespace ();
4009 peek_char = gfc_peek_char ();
4010
231b2fcc 4011 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4012 return MATCH_ERROR;
4013
4014 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4015 return MATCH_ERROR;
4016
a8b3b0b6
CR
4017 /* Make sure that it isn't already declared as BIND(C). If it is, it
4018 must have been marked BIND(C) with a BIND(C) attribute and that is
4019 not allowed for procedures. */
4020 if (sym->attr.is_bind_c == 1)
4021 {
4022 sym->attr.is_bind_c = 0;
4023 if (sym->old_symbol != NULL)
4024 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4025 "variables or common blocks",
4026 &(sym->old_symbol->declared_at));
4027 else
4028 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4029 "variables or common blocks", &gfc_current_locus);
4030 }
4031
4032 /* Here, we are just checking if it has the bind(c) attribute, and if
4033 so, then we need to make sure it's all correct. If it doesn't,
4034 we still need to continue matching the rest of the subroutine line. */
4035 is_bind_c = gfc_match_bind_c (sym);
4036 if (is_bind_c == MATCH_ERROR)
4037 {
4038 /* There was an attempt at the bind(c), but it was wrong. An
4039 error message should have been printed w/in the gfc_match_bind_c
4040 so here we'll just return the MATCH_ERROR. */
4041 return MATCH_ERROR;
4042 }
4043
4044 if (is_bind_c == MATCH_YES)
4045 {
4046 if (peek_char != '(')
4047 {
4048 gfc_error ("Missing required parentheses before BIND(C) at %C");
4049 return MATCH_ERROR;
4050 }
4051 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4052 == FAILURE)
4053 return MATCH_ERROR;
4054 }
4055
6de9cd9a
DN
4056 if (gfc_match_eos () != MATCH_YES)
4057 {
4058 gfc_syntax_error (ST_SUBROUTINE);
4059 return MATCH_ERROR;
4060 }
4061
4062 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4063 return MATCH_ERROR;
4064
4065 return MATCH_YES;
4066}
4067
4068
a8b3b0b6
CR
4069/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4070 given, and set the binding label in either the given symbol (if not
86bf520d 4071 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
4072 encounter the BIND(C) before the declaration itself. Return
4073 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4074 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4075 or MATCH_YES if the specifier was correct and the binding label and
4076 bind(c) fields were set correctly for the given symbol or the
4077 current_ts. */
4078
4079match
4080gfc_match_bind_c (gfc_symbol *sym)
4081{
4082 /* binding label, if exists */
4083 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4084 match double_quote;
4085 match single_quote;
4086 int has_name_equals = 0;
4087
4088 /* Initialize the flag that specifies whether we encountered a NAME=
4089 specifier or not. */
4090 has_name_equals = 0;
4091
4092 /* Init the first char to nil so we can catch if we don't have
4093 the label (name attr) or the symbol name yet. */
4094 binding_label[0] = '\0';
4095
4096 /* This much we have to be able to match, in this order, if
4097 there is a bind(c) label. */
4098 if (gfc_match (" bind ( c ") != MATCH_YES)
4099 return MATCH_NO;
4100
4101 /* Now see if there is a binding label, or if we've reached the
4102 end of the bind(c) attribute without one. */
4103 if (gfc_match_char (',') == MATCH_YES)
4104 {
4105 if (gfc_match (" name = ") != MATCH_YES)
4106 {
4107 gfc_error ("Syntax error in NAME= specifier for binding label "
4108 "at %C");
4109 /* should give an error message here */
4110 return MATCH_ERROR;
4111 }
4112
4113 has_name_equals = 1;
4114
4115 /* Get the opening quote. */
4116 double_quote = MATCH_YES;
4117 single_quote = MATCH_YES;
4118 double_quote = gfc_match_char ('"');
4119 if (double_quote != MATCH_YES)
4120 single_quote = gfc_match_char ('\'');
4121 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4122 {
4123 gfc_error ("Syntax error in NAME= specifier for binding label "
4124 "at %C");
4125 return MATCH_ERROR;
4126 }
4127
4128 /* Grab the binding label, using functions that will not lower
4129 case the names automatically. */
4130 if (gfc_match_name_C (binding_label) != MATCH_YES)
4131 return MATCH_ERROR;
4132
4133 /* Get the closing quotation. */
4134 if (double_quote == MATCH_YES)
4135 {
4136 if (gfc_match_char ('"') != MATCH_YES)
4137 {
4138 gfc_error ("Missing closing quote '\"' for binding label at %C");
4139 /* User started string with '"' so looked to match it. */
4140 return MATCH_ERROR;
4141 }
4142 }
4143 else
4144 {
4145 if (gfc_match_char ('\'') != MATCH_YES)
4146 {
4147 gfc_error ("Missing closing quote '\'' for binding label at %C");
4148 /* User started string with "'" char. */
4149 return MATCH_ERROR;
4150 }
4151 }
4152 }
4153
4154 /* Get the required right paren. */
4155 if (gfc_match_char (')') != MATCH_YES)
4156 {
4157 gfc_error ("Missing closing paren for binding label at %C");
4158 return MATCH_ERROR;
4159 }
4160
4161 /* Save the binding label to the symbol. If sym is null, we're
4162 probably matching the typespec attributes of a declaration and
4163 haven't gotten the name yet, and therefore, no symbol yet. */
4164 if (binding_label[0] != '\0')
4165 {
4166 if (sym != NULL)
4167 {
4168 strncpy (sym->binding_label, binding_label,
4169 strlen (binding_label)+1);
4170 }
4171 else
4172 strncpy (curr_binding_label, binding_label,
4173 strlen (binding_label) + 1);
4174 }
4175 else
4176 {
4177 /* No binding label, but if symbol isn't null, we
4178 can set the label for it here. */
4179 /* TODO: If the name= was given and no binding label (name=""), we simply
4180 will let fortran mangle the symbol name as it usually would.
4181 However, this could still let C call it if the user looked up the
4182 symbol in the object file. Should the name set during mangling in
4183 trans-decl.c be marked with characters that are invalid for C to
4184 prevent this? */
4185 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4186 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4187 }
9e1d712c 4188
129d15a3
JW
4189 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4190 && current_interface.type == INTERFACE_ABSTRACT)
9e1d712c
TB
4191 {
4192 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4193 return MATCH_ERROR;
4194 }
4195
a8b3b0b6
CR
4196 return MATCH_YES;
4197}
4198
4199
1f2959f0 4200/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
4201
4202static int
4203contained_procedure (void)
4204{
4205 gfc_state_data *s;
4206
4207 for (s=gfc_state_stack; s; s=s->previous)
4208 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
636dff67 4209 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
ddc9ce91
TS
4210 return 1;
4211
4212 return 0;
4213}
4214
d51347f9 4215/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
4216 interoperable with the corresponding C enumeration type, making
4217 sure that -fshort-enums is honored. */
4218
4219static void
4220set_enum_kind(void)
4221{
4222 enumerator_history *current_history = NULL;
4223 int kind;
4224 int i;
4225
4226 if (max_enum == NULL || enum_history == NULL)
4227 return;
4228
4229 if (!gfc_option.fshort_enums)
d51347f9
TB
4230 return;
4231
25d8f0a2
TS
4232 i = 0;
4233 do
4234 {
4235 kind = gfc_integer_kinds[i++].kind;
4236 }
d51347f9 4237 while (kind < gfc_c_int_kind
25d8f0a2
TS
4238 && gfc_check_integer_range (max_enum->initializer->value.integer,
4239 kind) != ARITH_OK);
4240
4241 current_history = enum_history;
4242 while (current_history != NULL)
4243 {
4244 current_history->sym->ts.kind = kind;
4245 current_history = current_history->next;
4246 }
4247}
4248
636dff67 4249
6de9cd9a
DN
4250/* Match any of the various end-block statements. Returns the type of
4251 END to the caller. The END INTERFACE, END IF, END DO and END
4252 SELECT statements cannot be replaced by a single END statement. */
4253
4254match
636dff67 4255gfc_match_end (gfc_statement *st)
6de9cd9a
DN
4256{
4257 char name[GFC_MAX_SYMBOL_LEN + 1];
4258 gfc_compile_state state;
4259 locus old_loc;
4260 const char *block_name;
4261 const char *target;
ddc9ce91 4262 int eos_ok;
6de9cd9a
DN
4263 match m;
4264
63645982 4265 old_loc = gfc_current_locus;
6de9cd9a
DN
4266 if (gfc_match ("end") != MATCH_YES)
4267 return MATCH_NO;
4268
4269 state = gfc_current_state ();
636dff67
SK
4270 block_name = gfc_current_block () == NULL
4271 ? NULL : gfc_current_block ()->name;
6de9cd9a
DN
4272
4273 if (state == COMP_CONTAINS)
4274 {
4275 state = gfc_state_stack->previous->state;
636dff67
SK
4276 block_name = gfc_state_stack->previous->sym == NULL
4277 ? NULL : gfc_state_stack->previous->sym->name;
6de9cd9a
DN
4278 }
4279
4280 switch (state)
4281 {
4282 case COMP_NONE:
4283 case COMP_PROGRAM:
4284 *st = ST_END_PROGRAM;
4285 target = " program";
ddc9ce91 4286 eos_ok = 1;
6de9cd9a
DN
4287 break;
4288
4289 case COMP_SUBROUTINE:
4290 *st = ST_END_SUBROUTINE;
4291 target = " subroutine";
ddc9ce91 4292 eos_ok = !contained_procedure ();
6de9cd9a
DN
4293 break;
4294
4295 case COMP_FUNCTION:
4296 *st = ST_END_FUNCTION;
4297 target = " function";
ddc9ce91 4298 eos_ok = !contained_procedure ();
6de9cd9a
DN
4299 break;
4300
4301 case COMP_BLOCK_DATA:
4302 *st = ST_END_BLOCK_DATA;
4303 target = " block data";
ddc9ce91 4304 eos_ok = 1;
6de9cd9a
DN
4305 break;
4306
4307 case COMP_MODULE:
4308 *st = ST_END_MODULE;
4309 target = " module";
ddc9ce91 4310 eos_ok = 1;
6de9cd9a
DN
4311 break;
4312
4313 case COMP_INTERFACE:
4314 *st = ST_END_INTERFACE;
4315 target = " interface";
ddc9ce91 4316 eos_ok = 0;
6de9cd9a
DN
4317 break;
4318
4319 case COMP_DERIVED:
4320 *st = ST_END_TYPE;
4321 target = " type";
ddc9ce91 4322 eos_ok = 0;
6de9cd9a
DN
4323 break;
4324
4325 case COMP_IF:
4326 *st = ST_ENDIF;
4327 target = " if";
ddc9ce91 4328 eos_ok = 0;
6de9cd9a
DN
4329 break;
4330
4331 case COMP_DO:
4332 *st = ST_ENDDO;
4333 target = " do";
ddc9ce91 4334 eos_ok = 0;
6de9cd9a
DN
4335 break;
4336
4337 case COMP_SELECT:
4338 *st = ST_END_SELECT;
4339 target = " select";
ddc9ce91 4340 eos_ok = 0;
6de9cd9a
DN
4341 break;
4342
4343 case COMP_FORALL:
4344 *st = ST_END_FORALL;
4345 target = " forall";
ddc9ce91 4346 eos_ok = 0;
6de9cd9a
DN
4347 break;
4348
4349 case COMP_WHERE:
4350 *st = ST_END_WHERE;
4351 target = " where";
ddc9ce91 4352 eos_ok = 0;
6de9cd9a
DN
4353 break;
4354
25d8f0a2
TS
4355 case COMP_ENUM:
4356 *st = ST_END_ENUM;
4357 target = " enum";
4358 eos_ok = 0;
4359 last_initializer = NULL;
4360 set_enum_kind ();
4361 gfc_free_enum_history ();
4362 break;
4363
6de9cd9a
DN
4364 default:
4365 gfc_error ("Unexpected END statement at %C");
4366 goto cleanup;
4367 }
4368
4369 if (gfc_match_eos () == MATCH_YES)
4370 {
ddc9ce91 4371 if (!eos_ok)
6de9cd9a 4372 {
66e4ab31 4373 /* We would have required END [something]. */
59ce85b5
TS
4374 gfc_error ("%s statement expected at %L",
4375 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
4376 goto cleanup;
4377 }
4378
4379 return MATCH_YES;
4380 }
4381
4382 /* Verify that we've got the sort of end-block that we're expecting. */
4383 if (gfc_match (target) != MATCH_YES)
4384 {
4385 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4386 goto cleanup;
4387 }
4388
4389 /* If we're at the end, make sure a block name wasn't required. */
4390 if (gfc_match_eos () == MATCH_YES)
4391 {
4392
690af379
TS
4393 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4394 && *st != ST_END_FORALL && *st != ST_END_WHERE)
6de9cd9a
DN
4395 return MATCH_YES;
4396
4397 if (gfc_current_block () == NULL)
4398 return MATCH_YES;
4399
4400 gfc_error ("Expected block name of '%s' in %s statement at %C",
4401 block_name, gfc_ascii_statement (*st));
4402
4403 return MATCH_ERROR;
4404 }
4405
4406 /* END INTERFACE has a special handler for its several possible endings. */
4407 if (*st == ST_END_INTERFACE)
4408 return gfc_match_end_interface ();
4409
66e4ab31
SK
4410 /* We haven't hit the end of statement, so what is left must be an
4411 end-name. */
6de9cd9a
DN
4412 m = gfc_match_space ();
4413 if (m == MATCH_YES)
4414 m = gfc_match_name (name);
4415
4416 if (m == MATCH_NO)
4417 gfc_error ("Expected terminating name at %C");
4418 if (m != MATCH_YES)
4419 goto cleanup;
4420
4421 if (block_name == NULL)
4422 goto syntax;
4423
4424 if (strcmp (name, block_name) != 0)
4425 {
4426 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4427 gfc_ascii_statement (*st));
4428 goto cleanup;
4429 }
4430
4431 if (gfc_match_eos () == MATCH_YES)
4432 return MATCH_YES;
4433
4434syntax:
4435 gfc_syntax_error (*st);
4436
4437cleanup:
63645982 4438 gfc_current_locus = old_loc;
6de9cd9a
DN
4439 return MATCH_ERROR;
4440}
4441
4442
4443
4444/***************** Attribute declaration statements ****************/
4445
4446/* Set the attribute of a single variable. */
4447
4448static match
4449attr_decl1 (void)
4450{
4451 char name[GFC_MAX_SYMBOL_LEN + 1];
4452 gfc_array_spec *as;
4453 gfc_symbol *sym;
4454 locus var_locus;
4455 match m;
4456
4457 as = NULL;
4458
4459 m = gfc_match_name (name);
4460 if (m != MATCH_YES)
4461 goto cleanup;
4462
4463 if (find_special (name, &sym))
4464 return MATCH_ERROR;
4465
63645982 4466 var_locus = gfc_current_locus;
6de9cd9a
DN
4467
4468 /* Deal with possible array specification for certain attributes. */
4469 if (current_attr.dimension
4470 || current_attr.allocatable
4471 || current_attr.pointer
4472 || current_attr.target)
4473 {
4474 m = gfc_match_array_spec (&as);
4475 if (m == MATCH_ERROR)
4476 goto cleanup;
4477
4478 if (current_attr.dimension && m == MATCH_NO)
4479 {
636dff67
SK
4480 gfc_error ("Missing array specification at %L in DIMENSION "
4481 "statement", &var_locus);
6de9cd9a
DN
4482 m = MATCH_ERROR;
4483 goto cleanup;
4484 }
4485
4486 if ((current_attr.allocatable || current_attr.pointer)
4487 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4488 {
636dff67 4489 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
4490 m = MATCH_ERROR;
4491 goto cleanup;
4492 }
4493 }
4494
636dff67
SK
4495 /* Update symbol table. DIMENSION attribute is set
4496 in gfc_set_array_spec(). */
6de9cd9a
DN
4497 if (current_attr.dimension == 0
4498 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4499 {
4500 m = MATCH_ERROR;
4501 goto cleanup;
4502 }
4503
4504 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4505 {
4506 m = MATCH_ERROR;
4507 goto cleanup;
4508 }
d51347f9 4509
83d890b9
AL
4510 if (sym->attr.cray_pointee && sym->as != NULL)
4511 {
4512 /* Fix the array spec. */
4513 m = gfc_mod_pointee_as (sym->as);
4514 if (m == MATCH_ERROR)
4515 goto cleanup;
4516 }
6de9cd9a 4517
7114edca 4518 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
1902704e
PT
4519 {
4520 m = MATCH_ERROR;
4521 goto cleanup;
4522 }
4523
6de9cd9a
DN
4524 if ((current_attr.external || current_attr.intrinsic)
4525 && sym->attr.flavor != FL_PROCEDURE
231b2fcc 4526 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4527 {
4528 m = MATCH_ERROR;
4529 goto cleanup;
4530 }
4531
4532 return MATCH_YES;
4533
4534cleanup:
4535 gfc_free_array_spec (as);
4536 return m;
4537}
4538
4539
4540/* Generic attribute declaration subroutine. Used for attributes that
4541 just have a list of names. */
4542
4543static match
4544attr_decl (void)
4545{
4546 match m;
4547
4548 /* Gobble the optional double colon, by simply ignoring the result
4549 of gfc_match(). */
4550 gfc_match (" ::");
4551
4552 for (;;)
4553 {
4554 m = attr_decl1 ();
4555 if (m != MATCH_YES)
4556 break;
4557
4558 if (gfc_match_eos () == MATCH_YES)
4559 {
4560 m = MATCH_YES;
4561 break;
4562 }
4563
4564 if (gfc_match_char (',') != MATCH_YES)
4565 {
4566 gfc_error ("Unexpected character in variable list at %C");
4567 m = MATCH_ERROR;
4568 break;
4569 }
4570 }
4571
4572 return m;
4573}
4574
4575
83d890b9
AL
4576/* This routine matches Cray Pointer declarations of the form:
4577 pointer ( <pointer>, <pointee> )
4578 or
d51347f9
TB
4579 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4580 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
4581 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4582 be either a scalar, or an array declaration. No space is allocated for
d51347f9 4583 the pointee. For the statement
83d890b9
AL
4584 pointer (ipt, ar(10))
4585 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 4586 ar(i) => ((<type> *) ipt)(i)
b122dc6a 4587 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
4588
4589static match
4590cray_pointer_decl (void)
4591{
4592 match m;
4593 gfc_array_spec *as;
4594 gfc_symbol *cptr; /* Pointer symbol. */
4595 gfc_symbol *cpte; /* Pointee symbol. */
4596 locus var_locus;
4597 bool done = false;
4598
4599 while (!done)
4600 {
4601 if (gfc_match_char ('(') != MATCH_YES)
4602 {
4603 gfc_error ("Expected '(' at %C");
d51347f9 4604 return MATCH_ERROR;
83d890b9 4605 }
d51347f9 4606
83d890b9
AL
4607 /* Match pointer. */
4608 var_locus = gfc_current_locus;
4609 gfc_clear_attr (&current_attr);
4610 gfc_add_cray_pointer (&current_attr, &var_locus);
4611 current_ts.type = BT_INTEGER;
4612 current_ts.kind = gfc_index_integer_kind;
4613
d51347f9 4614 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
4615 if (m != MATCH_YES)
4616 {
4617 gfc_error ("Expected variable name at %C");
4618 return m;
4619 }
d51347f9 4620
83d890b9
AL
4621 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
4622 return MATCH_ERROR;
4623
d51347f9 4624 gfc_set_sym_referenced (cptr);
83d890b9
AL
4625
4626 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
4627 {
4628 cptr->ts.type = BT_INTEGER;
d51347f9 4629 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
4630 }
4631 else if (cptr->ts.type != BT_INTEGER)
4632 {
e25a0da3 4633 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
4634 return MATCH_ERROR;
4635 }
4636 else if (cptr->ts.kind < gfc_index_integer_kind)
4637 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 4638 " memory addresses require %d bytes",
636dff67 4639 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
4640
4641 if (gfc_match_char (',') != MATCH_YES)
4642 {
4643 gfc_error ("Expected \",\" at %C");
d51347f9 4644 return MATCH_ERROR;
83d890b9
AL
4645 }
4646
d51347f9 4647 /* Match Pointee. */
83d890b9
AL
4648 var_locus = gfc_current_locus;
4649 gfc_clear_attr (&current_attr);
4650 gfc_add_cray_pointee (&current_attr, &var_locus);
4651 current_ts.type = BT_UNKNOWN;
4652 current_ts.kind = 0;
4653
4654 m = gfc_match_symbol (&cpte, 0);
4655 if (m != MATCH_YES)
4656 {
4657 gfc_error ("Expected variable name at %C");
4658 return m;
4659 }
d51347f9 4660
83d890b9
AL
4661 /* Check for an optional array spec. */
4662 m = gfc_match_array_spec (&as);
4663 if (m == MATCH_ERROR)
4664 {
4665 gfc_free_array_spec (as);
4666 return m;
4667 }
4668 else if (m == MATCH_NO)
4669 {
4670 gfc_free_array_spec (as);
4671 as = NULL;
4672 }
4673
4674 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
4675 return MATCH_ERROR;
4676
4677 gfc_set_sym_referenced (cpte);
4678
4679 if (cpte->as == NULL)
4680 {
4681 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
4682 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4683 }
4684 else if (as != NULL)
4685 {
e25a0da3 4686 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
4687 gfc_free_array_spec (as);
4688 return MATCH_ERROR;
4689 }
4690
4691 as = NULL;
4692
4693 if (cpte->as != NULL)
4694 {
4695 /* Fix array spec. */
4696 m = gfc_mod_pointee_as (cpte->as);
4697 if (m == MATCH_ERROR)
4698 return m;
4699 }
4700
4701 /* Point the Pointee at the Pointer. */
b122dc6a 4702 cpte->cp_pointer = cptr;
83d890b9
AL
4703
4704 if (gfc_match_char (')') != MATCH_YES)
4705 {
4706 gfc_error ("Expected \")\" at %C");
4707 return MATCH_ERROR;
4708 }
4709 m = gfc_match_char (',');
4710 if (m != MATCH_YES)
4711 done = true; /* Stop searching for more declarations. */
4712
4713 }
4714
4715 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
4716 || gfc_match_eos () != MATCH_YES)
4717 {
4718 gfc_error ("Expected \",\" or end of statement at %C");
4719 return MATCH_ERROR;
4720 }
4721 return MATCH_YES;
4722}
4723
4724
6de9cd9a
DN
4725match
4726gfc_match_external (void)
4727{
4728
4729 gfc_clear_attr (&current_attr);
1902704e 4730 current_attr.external = 1;
6de9cd9a
DN
4731
4732 return attr_decl ();
4733}
4734
4735
6de9cd9a
DN
4736match
4737gfc_match_intent (void)
4738{
4739 sym_intent intent;
4740
4741 intent = match_intent_spec ();
4742 if (intent == INTENT_UNKNOWN)
4743 return MATCH_ERROR;
4744
4745 gfc_clear_attr (&current_attr);
1902704e 4746 current_attr.intent = intent;
6de9cd9a
DN
4747
4748 return attr_decl ();
4749}
4750
4751
4752match
4753gfc_match_intrinsic (void)
4754{
4755
4756 gfc_clear_attr (&current_attr);
1902704e 4757 current_attr.intrinsic = 1;
6de9cd9a
DN
4758
4759 return attr_decl ();
4760}
4761
4762
4763match
4764gfc_match_optional (void)
4765{
4766
4767 gfc_clear_attr (&current_attr);
1902704e 4768 current_attr.optional = 1;
6de9cd9a
DN
4769
4770 return attr_decl ();
4771}
4772
4773
4774match
4775gfc_match_pointer (void)
4776{
83d890b9
AL
4777 gfc_gobble_whitespace ();
4778 if (gfc_peek_char () == '(')
4779 {
4780 if (!gfc_option.flag_cray_pointer)
4781 {
636dff67
SK
4782 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4783 "flag");
83d890b9
AL
4784 return MATCH_ERROR;
4785 }
4786 return cray_pointer_decl ();
4787 }
4788 else
4789 {
4790 gfc_clear_attr (&current_attr);
1902704e 4791 current_attr.pointer = 1;
83d890b9
AL
4792
4793 return attr_decl ();
4794 }
6de9cd9a
DN
4795}
4796
4797
4798match
4799gfc_match_allocatable (void)
4800{
6de9cd9a 4801 gfc_clear_attr (&current_attr);
1902704e 4802 current_attr.allocatable = 1;
6de9cd9a
DN
4803
4804 return attr_decl ();
4805}
4806
4807
4808match
4809gfc_match_dimension (void)
4810{
6de9cd9a 4811 gfc_clear_attr (&current_attr);
1902704e 4812 current_attr.dimension = 1;
6de9cd9a
DN
4813
4814 return attr_decl ();
4815}
4816
4817
4818match
4819gfc_match_target (void)
4820{
6de9cd9a 4821 gfc_clear_attr (&current_attr);
1902704e 4822 current_attr.target = 1;
6de9cd9a
DN
4823
4824 return attr_decl ();
4825}
4826
4827
4828/* Match the list of entities being specified in a PUBLIC or PRIVATE
4829 statement. */
4830
4831static match
4832access_attr_decl (gfc_statement st)
4833{
4834 char name[GFC_MAX_SYMBOL_LEN + 1];
4835 interface_type type;
4836 gfc_user_op *uop;
4837 gfc_symbol *sym;
4838 gfc_intrinsic_op operator;
4839 match m;
4840
4841 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4842 goto done;
4843
4844 for (;;)
4845 {
4846 m = gfc_match_generic_spec (&type, name, &operator);
4847 if (m == MATCH_NO)
4848 goto syntax;
4849 if (m == MATCH_ERROR)
4850 return MATCH_ERROR;
4851
4852 switch (type)
4853 {
4854 case INTERFACE_NAMELESS:
9e1d712c 4855 case INTERFACE_ABSTRACT:
6de9cd9a
DN
4856 goto syntax;
4857
4858 case INTERFACE_GENERIC:
4859 if (gfc_get_symbol (name, NULL, &sym))
4860 goto done;
4861
636dff67
SK
4862 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
4863 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
231b2fcc 4864 sym->name, NULL) == FAILURE)
6de9cd9a
DN
4865 return MATCH_ERROR;
4866
4867 break;
4868
4869 case INTERFACE_INTRINSIC_OP:
4870 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
4871 {
4872 gfc_current_ns->operator_access[operator] =
4873 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4874 }
4875 else
4876 {
4877 gfc_error ("Access specification of the %s operator at %C has "
4878 "already been specified", gfc_op2string (operator));
4879 goto done;
4880 }
4881
4882 break;
4883
4884 case INTERFACE_USER_OP:
4885 uop = gfc_get_uop (name);
4886
4887 if (uop->access == ACCESS_UNKNOWN)
4888 {
636dff67
SK
4889 uop->access = (st == ST_PUBLIC)
4890 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
4891 }
4892 else
4893 {
636dff67
SK
4894 gfc_error ("Access specification of the .%s. operator at %C "
4895 "has already been specified", sym->name);
6de9cd9a
DN
4896 goto done;
4897 }
4898
4899 break;
4900 }
4901
4902 if (gfc_match_char (',') == MATCH_NO)
4903 break;
4904 }
4905
4906 if (gfc_match_eos () != MATCH_YES)
4907 goto syntax;
4908 return MATCH_YES;
4909
4910syntax:
4911 gfc_syntax_error (st);
4912
4913done:
4914 return MATCH_ERROR;
4915}
4916
4917
ee7e677f
TB
4918match
4919gfc_match_protected (void)
4920{
4921 gfc_symbol *sym;
4922 match m;
4923
4924 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4925 {
4926 gfc_error ("PROTECTED at %C only allowed in specification "
4927 "part of a module");
4928 return MATCH_ERROR;
4929
4930 }
4931
636dff67 4932 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
ee7e677f
TB
4933 == FAILURE)
4934 return MATCH_ERROR;
4935
4936 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4937 {
4938 return MATCH_ERROR;
4939 }
4940
4941 if (gfc_match_eos () == MATCH_YES)
4942 goto syntax;
4943
4944 for(;;)
4945 {
4946 m = gfc_match_symbol (&sym, 0);
4947 switch (m)
4948 {
4949 case MATCH_YES:
636dff67
SK
4950 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
4951 == FAILURE)
ee7e677f
TB
4952 return MATCH_ERROR;
4953 goto next_item;
4954
4955 case MATCH_NO:
4956 break;
4957
4958 case MATCH_ERROR:
4959 return MATCH_ERROR;
4960 }
4961
4962 next_item:
4963 if (gfc_match_eos () == MATCH_YES)
4964 break;
4965 if (gfc_match_char (',') != MATCH_YES)
4966 goto syntax;
4967 }
4968
4969 return MATCH_YES;
4970
4971syntax:
4972 gfc_error ("Syntax error in PROTECTED statement at %C");
4973 return MATCH_ERROR;
4974}
4975
4976
86bf520d 4977/* The PRIVATE statement is a bit weird in that it can be an attribute
6de9cd9a
DN
4978 declaration, but also works as a standlone statement inside of a
4979 type declaration or a module. */
4980
4981match
636dff67 4982gfc_match_private (gfc_statement *st)
6de9cd9a
DN
4983{
4984
4985 if (gfc_match ("private") != MATCH_YES)
4986 return MATCH_NO;
4987
d51347f9
TB
4988 if (gfc_current_state () != COMP_MODULE
4989 && (gfc_current_state () != COMP_DERIVED
4990 || !gfc_state_stack->previous
4991 || gfc_state_stack->previous->state != COMP_MODULE))
4992 {
4993 gfc_error ("PRIVATE statement at %C is only allowed in the "
4994 "specification part of a module");
4995 return MATCH_ERROR;
4996 }
4997
6de9cd9a
DN
4998 if (gfc_current_state () == COMP_DERIVED)
4999 {
5000 if (gfc_match_eos () == MATCH_YES)
5001 {
5002 *st = ST_PRIVATE;
5003 return MATCH_YES;
5004 }
5005
5006 gfc_syntax_error (ST_PRIVATE);
5007 return MATCH_ERROR;
5008 }
5009
5010 if (gfc_match_eos () == MATCH_YES)
5011 {
5012 *st = ST_PRIVATE;
5013 return MATCH_YES;
5014 }
5015
5016 *st = ST_ATTR_DECL;
5017 return access_attr_decl (ST_PRIVATE);
5018}
5019
5020
5021match
636dff67 5022gfc_match_public (gfc_statement *st)
6de9cd9a
DN
5023{
5024
5025 if (gfc_match ("public") != MATCH_YES)
5026 return MATCH_NO;
5027
d51347f9
TB
5028 if (gfc_current_state () != COMP_MODULE)
5029 {
5030 gfc_error ("PUBLIC statement at %C is only allowed in the "
5031 "specification part of a module");
5032 return MATCH_ERROR;
5033 }
5034
6de9cd9a
DN
5035 if (gfc_match_eos () == MATCH_YES)
5036 {
5037 *st = ST_PUBLIC;
5038 return MATCH_YES;
5039 }
5040
5041 *st = ST_ATTR_DECL;
5042 return access_attr_decl (ST_PUBLIC);
5043}
5044
5045
5046/* Workhorse for gfc_match_parameter. */
5047
5048static match
5049do_parm (void)
5050{
5051 gfc_symbol *sym;
5052 gfc_expr *init;
5053 match m;
5054
5055 m = gfc_match_symbol (&sym, 0);
5056 if (m == MATCH_NO)
5057 gfc_error ("Expected variable name at %C in PARAMETER statement");
5058
5059 if (m != MATCH_YES)
5060 return m;
5061
5062 if (gfc_match_char ('=') == MATCH_NO)
5063 {
5064 gfc_error ("Expected = sign in PARAMETER statement at %C");
5065 return MATCH_ERROR;
5066 }
5067
5068 m = gfc_match_init_expr (&init);
5069 if (m == MATCH_NO)
5070 gfc_error ("Expected expression at %C in PARAMETER statement");
5071 if (m != MATCH_YES)
5072 return m;
5073
5074 if (sym->ts.type == BT_UNKNOWN
5075 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5076 {
5077 m = MATCH_ERROR;
5078 goto cleanup;
5079 }
5080
5081 if (gfc_check_assign_symbol (sym, init) == FAILURE
231b2fcc 5082 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5083 {
5084 m = MATCH_ERROR;
5085 goto cleanup;
5086 }
5087
7e2eba4b
DE
5088 if (sym->ts.type == BT_CHARACTER
5089 && sym->ts.cl != NULL
5090 && sym->ts.cl->length != NULL
5091 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5092 && init->expr_type == EXPR_CONSTANT
5093 && init->ts.type == BT_CHARACTER
5094 && init->ts.kind == 1)
5095 gfc_set_constant_character_len (
2220652d 5096 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
7e2eba4b 5097
6de9cd9a
DN
5098 sym->value = init;
5099 return MATCH_YES;
5100
5101cleanup:
5102 gfc_free_expr (init);
5103 return m;
5104}
5105
5106
5107/* Match a parameter statement, with the weird syntax that these have. */
5108
5109match
5110gfc_match_parameter (void)
5111{
5112 match m;
5113
5114 if (gfc_match_char ('(') == MATCH_NO)
5115 return MATCH_NO;
5116
5117 for (;;)
5118 {
5119 m = do_parm ();
5120 if (m != MATCH_YES)
5121 break;
5122
5123 if (gfc_match (" )%t") == MATCH_YES)
5124 break;
5125
5126 if (gfc_match_char (',') != MATCH_YES)
5127 {
5128 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5129 m = MATCH_ERROR;
5130 break;
5131 }
5132 }
5133
5134 return m;
5135}
5136
5137
5138/* Save statements have a special syntax. */
5139
5140match
5141gfc_match_save (void)
5142{
9056bd70
TS
5143 char n[GFC_MAX_SYMBOL_LEN+1];
5144 gfc_common_head *c;
6de9cd9a
DN
5145 gfc_symbol *sym;
5146 match m;
5147
5148 if (gfc_match_eos () == MATCH_YES)
5149 {
5150 if (gfc_current_ns->seen_save)
5151 {
636dff67
SK
5152 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5153 "follows previous SAVE statement")
09e87839
AL
5154 == FAILURE)
5155 return MATCH_ERROR;
6de9cd9a
DN
5156 }
5157
5158 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5159 return MATCH_YES;
5160 }
5161
5162 if (gfc_current_ns->save_all)
5163 {
636dff67
SK
5164 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5165 "blanket SAVE statement")
09e87839
AL
5166 == FAILURE)
5167 return MATCH_ERROR;
6de9cd9a
DN
5168 }
5169
5170 gfc_match (" ::");
5171
5172 for (;;)
5173 {
5174 m = gfc_match_symbol (&sym, 0);
5175 switch (m)
5176 {
5177 case MATCH_YES:
636dff67
SK
5178 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5179 == FAILURE)
6de9cd9a
DN
5180 return MATCH_ERROR;
5181 goto next_item;
5182
5183 case MATCH_NO:
5184 break;
5185
5186 case MATCH_ERROR:
5187 return MATCH_ERROR;
5188 }
5189
9056bd70 5190 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
5191 if (m == MATCH_ERROR)
5192 return MATCH_ERROR;
5193 if (m == MATCH_NO)
5194 goto syntax;
5195
53814b8f 5196 c = gfc_get_common (n, 0);
9056bd70
TS
5197 c->saved = 1;
5198
6de9cd9a
DN
5199 gfc_current_ns->seen_save = 1;
5200
5201 next_item:
5202 if (gfc_match_eos () == MATCH_YES)
5203 break;
5204 if (gfc_match_char (',') != MATCH_YES)
5205 goto syntax;
5206 }
5207
5208 return MATCH_YES;
5209
5210syntax:
5211 gfc_error ("Syntax error in SAVE statement at %C");
5212 return MATCH_ERROR;
5213}
5214
5215
06469efd
PT
5216match
5217gfc_match_value (void)
5218{
5219 gfc_symbol *sym;
5220 match m;
5221
636dff67 5222 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
06469efd
PT
5223 == FAILURE)
5224 return MATCH_ERROR;
5225
5226 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5227 {
5228 return MATCH_ERROR;
5229 }
5230
5231 if (gfc_match_eos () == MATCH_YES)
5232 goto syntax;
5233
5234 for(;;)
5235 {
5236 m = gfc_match_symbol (&sym, 0);
5237 switch (m)
5238 {
5239 case MATCH_YES:
636dff67
SK
5240 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5241 == FAILURE)
06469efd
PT
5242 return MATCH_ERROR;
5243 goto next_item;
5244
5245 case MATCH_NO:
5246 break;
5247
5248 case MATCH_ERROR:
5249 return MATCH_ERROR;
5250 }
5251
5252 next_item:
5253 if (gfc_match_eos () == MATCH_YES)
5254 break;
5255 if (gfc_match_char (',') != MATCH_YES)
5256 goto syntax;
5257 }
5258
5259 return MATCH_YES;
5260
5261syntax:
5262 gfc_error ("Syntax error in VALUE statement at %C");
5263 return MATCH_ERROR;
5264}
5265
66e4ab31 5266
775e6c3a
TB
5267match
5268gfc_match_volatile (void)
5269{
5270 gfc_symbol *sym;
5271 match m;
5272
636dff67 5273 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
775e6c3a
TB
5274 == FAILURE)
5275 return MATCH_ERROR;
5276
5277 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5278 {
5279 return MATCH_ERROR;
5280 }
5281
5282 if (gfc_match_eos () == MATCH_YES)
5283 goto syntax;
5284
5285 for(;;)
5286 {
9bce3c1c
TB
5287 /* VOLATILE is special because it can be added to host-associated
5288 symbols locally. */
5289 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
5290 switch (m)
5291 {
5292 case MATCH_YES:
636dff67
SK
5293 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5294 == FAILURE)
775e6c3a
TB
5295 return MATCH_ERROR;
5296 goto next_item;
5297
5298 case MATCH_NO:
5299 break;
5300
5301 case MATCH_ERROR:
5302 return MATCH_ERROR;
5303 }
5304
5305 next_item:
5306 if (gfc_match_eos () == MATCH_YES)
5307 break;
5308 if (gfc_match_char (',') != MATCH_YES)
5309 goto syntax;
5310 }
5311
5312 return MATCH_YES;
5313
5314syntax:
5315 gfc_error ("Syntax error in VOLATILE statement at %C");
5316 return MATCH_ERROR;
5317}
5318
5319
6de9cd9a
DN
5320/* Match a module procedure statement. Note that we have to modify
5321 symbols in the parent's namespace because the current one was there
49de9e73 5322 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
5323
5324match
5325gfc_match_modproc (void)
5326{
5327 char name[GFC_MAX_SYMBOL_LEN + 1];
5328 gfc_symbol *sym;
5329 match m;
060fca4a 5330 gfc_namespace *module_ns;
6de9cd9a
DN
5331
5332 if (gfc_state_stack->state != COMP_INTERFACE
5333 || gfc_state_stack->previous == NULL
129d15a3
JW
5334 || current_interface.type == INTERFACE_NAMELESS
5335 || current_interface.type == INTERFACE_ABSTRACT)
6de9cd9a 5336 {
636dff67
SK
5337 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5338 "interface");
6de9cd9a
DN
5339 return MATCH_ERROR;
5340 }
5341
060fca4a
PT
5342 module_ns = gfc_current_ns->parent;
5343 for (; module_ns; module_ns = module_ns->parent)
5344 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5345 break;
5346
5347 if (module_ns == NULL)
5348 return MATCH_ERROR;
5349
6de9cd9a
DN
5350 for (;;)
5351 {
5352 m = gfc_match_name (name);
5353 if (m == MATCH_NO)
5354 goto syntax;
5355 if (m != MATCH_YES)
5356 return MATCH_ERROR;
5357
060fca4a 5358 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
5359 return MATCH_ERROR;
5360
5361 if (sym->attr.proc != PROC_MODULE
231b2fcc
TS
5362 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5363 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5364 return MATCH_ERROR;
5365
5366 if (gfc_add_interface (sym) == FAILURE)
5367 return MATCH_ERROR;
5368
71f77fd7
PT
5369 sym->attr.mod_proc = 1;
5370
6de9cd9a
DN
5371 if (gfc_match_eos () == MATCH_YES)
5372 break;
5373 if (gfc_match_char (',') != MATCH_YES)
5374 goto syntax;
5375 }
5376
5377 return MATCH_YES;
5378
5379syntax:
5380 gfc_syntax_error (ST_MODULE_PROC);
5381 return MATCH_ERROR;
5382}
5383
5384
a8b3b0b6
CR
5385/* Match the optional attribute specifiers for a type declaration.
5386 Return MATCH_ERROR if an error is encountered in one of the handled
5387 attributes (public, private, bind(c)), MATCH_NO if what's found is
5388 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5389 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
5390
5391match
a8b3b0b6 5392gfc_get_type_attr_spec (symbol_attribute *attr)
6de9cd9a 5393{
a8b3b0b6 5394 /* See if the derived type is marked as private. */
6de9cd9a
DN
5395 if (gfc_match (" , private") == MATCH_YES)
5396 {
d51347f9 5397 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5398 {
d51347f9
TB
5399 gfc_error ("Derived type at %C can only be PRIVATE in the "
5400 "specification part of a module");
6de9cd9a
DN
5401 return MATCH_ERROR;
5402 }
5403
a8b3b0b6 5404 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6de9cd9a 5405 return MATCH_ERROR;
6de9cd9a 5406 }
a8b3b0b6 5407 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 5408 {
d51347f9 5409 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5410 {
d51347f9
TB
5411 gfc_error ("Derived type at %C can only be PUBLIC in the "
5412 "specification part of a module");
6de9cd9a
DN
5413 return MATCH_ERROR;
5414 }
5415
a8b3b0b6 5416 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6de9cd9a 5417 return MATCH_ERROR;
6de9cd9a 5418 }
e7303e85 5419 else if (gfc_match(" , bind ( c )") == MATCH_YES)
a8b3b0b6
CR
5420 {
5421 /* If the type is defined to be bind(c) it then needs to make
5422 sure that all fields are interoperable. This will
5423 need to be a semantic check on the finished derived type.
5424 See 15.2.3 (lines 9-12) of F2003 draft. */
5425 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5426 return MATCH_ERROR;
5427
5428 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5429 }
5430 else
5431 return MATCH_NO;
5432
5433 /* If we get here, something matched. */
5434 return MATCH_YES;
5435}
5436
5437
5438/* Match the beginning of a derived type declaration. If a type name
5439 was the result of a function, then it is possible to have a symbol
5440 already to be known as a derived type yet have no components. */
5441
5442match
5443gfc_match_derived_decl (void)
5444{
5445 char name[GFC_MAX_SYMBOL_LEN + 1];
5446 symbol_attribute attr;
5447 gfc_symbol *sym;
5448 match m;
5449 match is_type_attr_spec = MATCH_NO;
e7303e85 5450 bool seen_attr = false;
a8b3b0b6
CR
5451
5452 if (gfc_current_state () == COMP_DERIVED)
5453 return MATCH_NO;
5454
5455 gfc_clear_attr (&attr);
5456
5457 do
5458 {
5459 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5460 if (is_type_attr_spec == MATCH_ERROR)
5461 return MATCH_ERROR;
e7303e85
FXC
5462 if (is_type_attr_spec == MATCH_YES)
5463 seen_attr = true;
a8b3b0b6 5464 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a 5465
e7303e85 5466 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6de9cd9a
DN
5467 {
5468 gfc_error ("Expected :: in TYPE definition at %C");
5469 return MATCH_ERROR;
5470 }
5471
5472 m = gfc_match (" %n%t", name);
5473 if (m != MATCH_YES)
5474 return m;
5475
e9c06563
TB
5476 /* Make sure the name is not the name of an intrinsic type. */
5477 if (gfc_is_intrinsic_typename (name))
6de9cd9a 5478 {
636dff67
SK
5479 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5480 "type", name);
6de9cd9a
DN
5481 return MATCH_ERROR;
5482 }
5483
5484 if (gfc_get_symbol (name, NULL, &sym))
5485 return MATCH_ERROR;
5486
5487 if (sym->ts.type != BT_UNKNOWN)
5488 {
5489 gfc_error ("Derived type name '%s' at %C already has a basic type "
5490 "of %s", sym->name, gfc_typename (&sym->ts));
5491 return MATCH_ERROR;
5492 }
5493
5494 /* The symbol may already have the derived attribute without the
5495 components. The ways this can happen is via a function
5496 definition, an INTRINSIC statement or a subtype in another
5497 derived type that is a pointer. The first part of the AND clause
f7b529fa 5498 is true if a the symbol is not the return value of a function. */
6de9cd9a 5499 if (sym->attr.flavor != FL_DERIVED
231b2fcc 5500 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5501 return MATCH_ERROR;
5502
5503 if (sym->components != NULL)
5504 {
636dff67
SK
5505 gfc_error ("Derived type definition of '%s' at %C has already been "
5506 "defined", sym->name);
6de9cd9a
DN
5507 return MATCH_ERROR;
5508 }
5509
5510 if (attr.access != ACCESS_UNKNOWN
231b2fcc 5511 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5512 return MATCH_ERROR;
5513
a8b3b0b6
CR
5514 /* See if the derived type was labeled as bind(c). */
5515 if (attr.is_bind_c != 0)
5516 sym->attr.is_bind_c = attr.is_bind_c;
5517
6de9cd9a
DN
5518 gfc_new_block = sym;
5519
5520 return MATCH_YES;
5521}
83d890b9
AL
5522
5523
5524/* Cray Pointees can be declared as:
5525 pointer (ipt, a (n,m,...,*))
5526 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5527 cheat and set a constant bound of 1 for the last dimension, if this
5528 is the case. Since there is no bounds-checking for Cray Pointees,
5529 this will be okay. */
5530
5531try
5532gfc_mod_pointee_as (gfc_array_spec *as)
5533{
5534 as->cray_pointee = true; /* This will be useful to know later. */
5535 if (as->type == AS_ASSUMED_SIZE)
5536 {
5537 as->type = AS_EXPLICIT;
5538 as->upper[as->rank - 1] = gfc_int_expr (1);
5539 as->cp_was_assumed = true;
5540 }
5541 else if (as->type == AS_ASSUMED_SHAPE)
5542 {
5543 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5544 return MATCH_ERROR;
5545 }
5546 return MATCH_YES;
5547}
25d8f0a2
TS
5548
5549
5550/* Match the enum definition statement, here we are trying to match
5551 the first line of enum definition statement.
5552 Returns MATCH_YES if match is found. */
5553
5554match
5555gfc_match_enum (void)
5556{
5557 match m;
5558
5559 m = gfc_match_eos ();
5560 if (m != MATCH_YES)
5561 return m;
5562
6133c68a 5563 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
25d8f0a2
TS
5564 == FAILURE)
5565 return MATCH_ERROR;
5566
5567 return MATCH_YES;
5568}
5569
5570
6133c68a
TS
5571/* Match a variable name with an optional initializer. When this
5572 subroutine is called, a variable is expected to be parsed next.
5573 Depending on what is happening at the moment, updates either the
5574 symbol table or the current interface. */
5575
5576static match
5577enumerator_decl (void)
5578{
5579 char name[GFC_MAX_SYMBOL_LEN + 1];
5580 gfc_expr *initializer;
5581 gfc_array_spec *as = NULL;
5582 gfc_symbol *sym;
5583 locus var_locus;
5584 match m;
5585 try t;
5586 locus old_locus;
5587
5588 initializer = NULL;
5589 old_locus = gfc_current_locus;
5590
5591 /* When we get here, we've just matched a list of attributes and
5592 maybe a type and a double colon. The next thing we expect to see
5593 is the name of the symbol. */
5594 m = gfc_match_name (name);
5595 if (m != MATCH_YES)
5596 goto cleanup;
5597
5598 var_locus = gfc_current_locus;
5599
5600 /* OK, we've successfully matched the declaration. Now put the
5601 symbol in the current namespace. If we fail to create the symbol,
5602 bail out. */
5603 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5604 {
5605 m = MATCH_ERROR;
5606 goto cleanup;
5607 }
5608
5609 /* The double colon must be present in order to have initializers.
5610 Otherwise the statement is ambiguous with an assignment statement. */
5611 if (colon_seen)
5612 {
5613 if (gfc_match_char ('=') == MATCH_YES)
5614 {
5615 m = gfc_match_init_expr (&initializer);
5616 if (m == MATCH_NO)
5617 {
5618 gfc_error ("Expected an initialization expression at %C");
5619 m = MATCH_ERROR;
5620 }
5621
5622 if (m != MATCH_YES)
5623 goto cleanup;
5624 }
5625 }
5626
5627 /* If we do not have an initializer, the initialization value of the
5628 previous enumerator (stored in last_initializer) is incremented
5629 by 1 and is used to initialize the current enumerator. */
5630 if (initializer == NULL)
5631 initializer = gfc_enum_initializer (last_initializer, old_locus);
d51347f9 5632
6133c68a
TS
5633 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
5634 {
5635 gfc_error("ENUMERATOR %L not initialized with integer expression",
5636 &var_locus);
d51347f9 5637 m = MATCH_ERROR;
6133c68a
TS
5638 gfc_free_enum_history ();
5639 goto cleanup;
5640 }
5641
5642 /* Store this current initializer, for the next enumerator variable
5643 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5644 use last_initializer below. */
5645 last_initializer = initializer;
5646 t = add_init_expr_to_sym (name, &initializer, &var_locus);
5647
5648 /* Maintain enumerator history. */
5649 gfc_find_symbol (name, NULL, 0, &sym);
5650 create_enum_history (sym, last_initializer);
5651
5652 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
5653
5654cleanup:
5655 /* Free stuff up and return. */
5656 gfc_free_expr (initializer);
5657
5658 return m;
5659}
5660
5661
66e4ab31 5662/* Match the enumerator definition statement. */
25d8f0a2
TS
5663
5664match
5665gfc_match_enumerator_def (void)
5666{
5667 match m;
6133c68a 5668 try t;
d51347f9 5669
25d8f0a2 5670 gfc_clear_ts (&current_ts);
d51347f9 5671
25d8f0a2
TS
5672 m = gfc_match (" enumerator");
5673 if (m != MATCH_YES)
5674 return m;
6133c68a
TS
5675
5676 m = gfc_match (" :: ");
5677 if (m == MATCH_ERROR)
5678 return m;
5679
5680 colon_seen = (m == MATCH_YES);
d51347f9 5681
25d8f0a2
TS
5682 if (gfc_current_state () != COMP_ENUM)
5683 {
5684 gfc_error ("ENUM definition statement expected before %C");
5685 gfc_free_enum_history ();
5686 return MATCH_ERROR;
5687 }
5688
5689 (&current_ts)->type = BT_INTEGER;
5690 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 5691
6133c68a
TS
5692 gfc_clear_attr (&current_attr);
5693 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
5694 if (t == FAILURE)
25d8f0a2 5695 {
6133c68a 5696 m = MATCH_ERROR;
25d8f0a2
TS
5697 goto cleanup;
5698 }
5699
25d8f0a2
TS
5700 for (;;)
5701 {
6133c68a 5702 m = enumerator_decl ();
25d8f0a2
TS
5703 if (m == MATCH_ERROR)
5704 goto cleanup;
5705 if (m == MATCH_NO)
5706 break;
5707
5708 if (gfc_match_eos () == MATCH_YES)
5709 goto cleanup;
5710 if (gfc_match_char (',') != MATCH_YES)
5711 break;
5712 }
5713
5714 if (gfc_current_state () == COMP_ENUM)
5715 {
5716 gfc_free_enum_history ();
5717 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5718 m = MATCH_ERROR;
5719 }
5720
5721cleanup:
5722 gfc_free_array_spec (current_as);
5723 current_as = NULL;
5724 return m;
5725
5726}
5727