]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/decl.c
re PR c/30427 (~ vector float is accepted)
[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
1556 && current_ts.derived->ns != gfc_current_ns
1557 && !gfc_current_ns->has_import_set)
72af9f0b
PT
1558 {
1559 gfc_error ("the type of '%s' at %C has not been declared within the "
1560 "interface", name);
6de9cd9a
DN
1561 m = MATCH_ERROR;
1562 goto cleanup;
1563 }
1564
1565 /* In functions that have a RESULT variable defined, the function
1566 name always refers to function calls. Therefore, the name is
1567 not allowed to appear in specification statements. */
1568 if (gfc_current_state () == COMP_FUNCTION
1569 && gfc_current_block () != NULL
1570 && gfc_current_block ()->result != NULL
1571 && gfc_current_block ()->result != gfc_current_block ()
1572 && strcmp (gfc_current_block ()->name, name) == 0)
1573 {
1574 gfc_error ("Function name '%s' not allowed at %C", name);
1575 m = MATCH_ERROR;
1576 goto cleanup;
1577 }
1578
294fbfc8
TS
1579 /* We allow old-style initializations of the form
1580 integer i /2/, j(4) /3*3, 1/
1581 (if no colon has been seen). These are different from data
1582 statements in that initializers are only allowed to apply to the
1583 variable immediately preceding, i.e.
1584 integer i, j /1, 2/
1585 is not allowed. Therefore we have to do some work manually, that
75d17889 1586 could otherwise be left to the matchers for DATA statements. */
294fbfc8
TS
1587
1588 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1589 {
1590 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1591 "initialization at %C") == FAILURE)
1592 return MATCH_ERROR;
d51347f9 1593
294fbfc8
TS
1594 return match_old_style_init (name);
1595 }
1596
6de9cd9a
DN
1597 /* The double colon must be present in order to have initializers.
1598 Otherwise the statement is ambiguous with an assignment statement. */
1599 if (colon_seen)
1600 {
1601 if (gfc_match (" =>") == MATCH_YES)
1602 {
6de9cd9a
DN
1603 if (!current_attr.pointer)
1604 {
1605 gfc_error ("Initialization at %C isn't for a pointer variable");
1606 m = MATCH_ERROR;
1607 goto cleanup;
1608 }
1609
1610 m = gfc_match_null (&initializer);
1611 if (m == MATCH_NO)
1612 {
def66134 1613 gfc_error ("Pointer initialization requires a NULL() at %C");
6de9cd9a
DN
1614 m = MATCH_ERROR;
1615 }
1616
1617 if (gfc_pure (NULL))
1618 {
636dff67
SK
1619 gfc_error ("Initialization of pointer at %C is not allowed in "
1620 "a PURE procedure");
6de9cd9a
DN
1621 m = MATCH_ERROR;
1622 }
1623
1624 if (m != MATCH_YES)
1625 goto cleanup;
1626
6de9cd9a
DN
1627 }
1628 else if (gfc_match_char ('=') == MATCH_YES)
1629 {
1630 if (current_attr.pointer)
1631 {
636dff67
SK
1632 gfc_error ("Pointer initialization at %C requires '=>', "
1633 "not '='");
6de9cd9a
DN
1634 m = MATCH_ERROR;
1635 goto cleanup;
1636 }
1637
1638 m = gfc_match_init_expr (&initializer);
1639 if (m == MATCH_NO)
1640 {
1641 gfc_error ("Expected an initialization expression at %C");
1642 m = MATCH_ERROR;
1643 }
1644
1645 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1646 {
636dff67
SK
1647 gfc_error ("Initialization of variable at %C is not allowed in "
1648 "a PURE procedure");
6de9cd9a
DN
1649 m = MATCH_ERROR;
1650 }
1651
1652 if (m != MATCH_YES)
1653 goto cleanup;
1654 }
cb44ab82
VL
1655 }
1656
5046aff5
PT
1657 if (initializer != NULL && current_attr.allocatable
1658 && gfc_current_state () == COMP_DERIVED)
1659 {
636dff67
SK
1660 gfc_error ("Initialization of allocatable component at %C is not "
1661 "allowed");
5046aff5
PT
1662 m = MATCH_ERROR;
1663 goto cleanup;
1664 }
1665
54b4ba60 1666 /* Add the initializer. Note that it is fine if initializer is
6de9cd9a
DN
1667 NULL here, because we sometimes also need to check if a
1668 declaration *must* have an initialization expression. */
1669 if (gfc_current_state () != COMP_DERIVED)
1670 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1671 else
54b4ba60 1672 {
5046aff5 1673 if (current_ts.type == BT_DERIVED
636dff67 1674 && !current_attr.pointer && !initializer)
54b4ba60
PB
1675 initializer = gfc_default_initializer (&current_ts);
1676 t = build_struct (name, cl, &initializer, &as);
1677 }
6de9cd9a
DN
1678
1679 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1680
1681cleanup:
1682 /* Free stuff up and return. */
1683 gfc_free_expr (initializer);
1684 gfc_free_array_spec (as);
1685
1686 return m;
1687}
1688
1689
b2b81a3f
BM
1690/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1691 This assumes that the byte size is equal to the kind number for
1692 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
6de9cd9a
DN
1693
1694match
636dff67 1695gfc_match_old_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
1696{
1697 match m;
5cf54585 1698 int original_kind;
6de9cd9a
DN
1699
1700 if (gfc_match_char ('*') != MATCH_YES)
1701 return MATCH_NO;
1702
5cf54585 1703 m = gfc_match_small_literal_int (&ts->kind, NULL);
6de9cd9a
DN
1704 if (m != MATCH_YES)
1705 return MATCH_ERROR;
1706
e45b3c75
ES
1707 original_kind = ts->kind;
1708
6de9cd9a 1709 /* Massage the kind numbers for complex types. */
e45b3c75
ES
1710 if (ts->type == BT_COMPLEX)
1711 {
1712 if (ts->kind % 2)
636dff67
SK
1713 {
1714 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1715 gfc_basic_typename (ts->type), original_kind);
1716 return MATCH_ERROR;
1717 }
e45b3c75
ES
1718 ts->kind /= 2;
1719 }
6de9cd9a 1720
e7a2d5fb 1721 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a 1722 {
e45b3c75 1723 gfc_error ("Old-style type declaration %s*%d not supported at %C",
636dff67 1724 gfc_basic_typename (ts->type), original_kind);
6de9cd9a
DN
1725 return MATCH_ERROR;
1726 }
1727
df8652dc
SK
1728 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1729 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1730 return MATCH_ERROR;
1731
6de9cd9a
DN
1732 return MATCH_YES;
1733}
1734
1735
1736/* Match a kind specification. Since kinds are generally optional, we
1737 usually return MATCH_NO if something goes wrong. If a "kind="
1738 string is found, then we know we have an error. */
1739
1740match
636dff67 1741gfc_match_kind_spec (gfc_typespec *ts)
6de9cd9a
DN
1742{
1743 locus where;
1744 gfc_expr *e;
1745 match m, n;
1746 const char *msg;
1747
1748 m = MATCH_NO;
1749 e = NULL;
1750
63645982 1751 where = gfc_current_locus;
6de9cd9a
DN
1752
1753 if (gfc_match_char ('(') == MATCH_NO)
1754 return MATCH_NO;
1755
1756 /* Also gobbles optional text. */
1757 if (gfc_match (" kind = ") == MATCH_YES)
1758 m = MATCH_ERROR;
1759
1760 n = gfc_match_init_expr (&e);
1761 if (n == MATCH_NO)
1762 gfc_error ("Expected initialization expression at %C");
1763 if (n != MATCH_YES)
1764 return MATCH_ERROR;
1765
1766 if (e->rank != 0)
1767 {
1768 gfc_error ("Expected scalar initialization expression at %C");
1769 m = MATCH_ERROR;
1770 goto no_match;
1771 }
1772
1773 msg = gfc_extract_int (e, &ts->kind);
1774 if (msg != NULL)
1775 {
1776 gfc_error (msg);
1777 m = MATCH_ERROR;
1778 goto no_match;
1779 }
1780
a8b3b0b6
CR
1781 /* Before throwing away the expression, let's see if we had a
1782 C interoperable kind (and store the fact). */
1783 if (e->ts.is_c_interop == 1)
1784 {
1785 /* Mark this as c interoperable if being declared with one
1786 of the named constants from iso_c_binding. */
1787 ts->is_c_interop = e->ts.is_iso_c;
1788 ts->f90_type = e->ts.f90_type;
1789 }
1790
6de9cd9a
DN
1791 gfc_free_expr (e);
1792 e = NULL;
1793
a8b3b0b6
CR
1794 /* Ignore errors to this point, if we've gotten here. This means
1795 we ignore the m=MATCH_ERROR from above. */
e7a2d5fb 1796 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
6de9cd9a
DN
1797 {
1798 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1799 gfc_basic_typename (ts->type));
6de9cd9a 1800 m = MATCH_ERROR;
6de9cd9a 1801 }
a8b3b0b6 1802 else if (gfc_match_char (')') != MATCH_YES)
6de9cd9a 1803 {
8998be20 1804 gfc_error ("Missing right parenthesis at %C");
a8b3b0b6 1805 m = MATCH_ERROR;
6de9cd9a 1806 }
a8b3b0b6
CR
1807 else
1808 /* All tests passed. */
1809 m = MATCH_YES;
6de9cd9a 1810
a8b3b0b6
CR
1811 if(m == MATCH_ERROR)
1812 gfc_current_locus = where;
1813
1814 /* Return what we know from the test(s). */
1815 return m;
6de9cd9a
DN
1816
1817no_match:
1818 gfc_free_expr (e);
63645982 1819 gfc_current_locus = where;
6de9cd9a
DN
1820 return m;
1821}
1822
1823
1824/* Match the various kind/length specifications in a CHARACTER
1825 declaration. We don't return MATCH_NO. */
1826
1827static match
636dff67 1828match_char_spec (gfc_typespec *ts)
6de9cd9a 1829{
5cd09fac 1830 int kind, seen_length;
6de9cd9a
DN
1831 gfc_charlen *cl;
1832 gfc_expr *len;
1833 match m;
a8b3b0b6 1834 gfc_expr *kind_expr = NULL;
9d64df18 1835 kind = gfc_default_character_kind;
6de9cd9a
DN
1836 len = NULL;
1837 seen_length = 0;
1838
1839 /* Try the old-style specification first. */
1840 old_char_selector = 0;
1841
1842 m = match_char_length (&len);
1843 if (m != MATCH_NO)
1844 {
1845 if (m == MATCH_YES)
1846 old_char_selector = 1;
1847 seen_length = 1;
1848 goto done;
1849 }
1850
1851 m = gfc_match_char ('(');
1852 if (m != MATCH_YES)
1853 {
a8b3b0b6 1854 m = MATCH_YES; /* Character without length is a single char. */
6de9cd9a
DN
1855 goto done;
1856 }
1857
a8b3b0b6 1858 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
6de9cd9a
DN
1859 if (gfc_match (" kind =") == MATCH_YES)
1860 {
a8b3b0b6
CR
1861 m = gfc_match_small_int_expr(&kind, &kind_expr);
1862
6de9cd9a
DN
1863 if (m == MATCH_ERROR)
1864 goto done;
1865 if (m == MATCH_NO)
1866 goto syntax;
1867
1868 if (gfc_match (" , len =") == MATCH_NO)
1869 goto rparen;
1870
1871 m = char_len_param_value (&len);
1872 if (m == MATCH_NO)
1873 goto syntax;
1874 if (m == MATCH_ERROR)
1875 goto done;
1876 seen_length = 1;
1877
1878 goto rparen;
1879 }
1880
66e4ab31 1881 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
6de9cd9a
DN
1882 if (gfc_match (" len =") == MATCH_YES)
1883 {
1884 m = char_len_param_value (&len);
1885 if (m == MATCH_NO)
1886 goto syntax;
1887 if (m == MATCH_ERROR)
1888 goto done;
1889 seen_length = 1;
1890
1891 if (gfc_match_char (')') == MATCH_YES)
1892 goto done;
1893
1894 if (gfc_match (" , kind =") != MATCH_YES)
1895 goto syntax;
1896
a8b3b0b6 1897 gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a 1898
e7a2d5fb 1899 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1900 {
1901 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1902 return MATCH_YES;
1903 }
1904
1905 goto rparen;
1906 }
1907
66e4ab31 1908 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
6de9cd9a
DN
1909 m = char_len_param_value (&len);
1910 if (m == MATCH_NO)
1911 goto syntax;
1912 if (m == MATCH_ERROR)
1913 goto done;
1914 seen_length = 1;
1915
1916 m = gfc_match_char (')');
1917 if (m == MATCH_YES)
1918 goto done;
1919
1920 if (gfc_match_char (',') != MATCH_YES)
1921 goto syntax;
1922
a8b3b0b6 1923 gfc_match (" kind ="); /* Gobble optional text. */
6de9cd9a 1924
a8b3b0b6 1925 m = gfc_match_small_int_expr(&kind, &kind_expr);
6de9cd9a
DN
1926 if (m == MATCH_ERROR)
1927 goto done;
1928 if (m == MATCH_NO)
1929 goto syntax;
1930
1931rparen:
1932 /* Require a right-paren at this point. */
1933 m = gfc_match_char (')');
1934 if (m == MATCH_YES)
1935 goto done;
1936
1937syntax:
1938 gfc_error ("Syntax error in CHARACTER declaration at %C");
1939 m = MATCH_ERROR;
16f8ffc8
JD
1940 gfc_free_expr (len);
1941 return m;
6de9cd9a
DN
1942
1943done:
16f8ffc8 1944 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
6de9cd9a
DN
1945 {
1946 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1947 m = MATCH_ERROR;
1948 }
1949
16f8ffc8
JD
1950 if (seen_length == 1 && len != NULL
1951 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1952 {
1953 gfc_error ("Expression at %C must be of INTEGER type");
1954 m = MATCH_ERROR;
1955 }
1956
6de9cd9a
DN
1957 if (m != MATCH_YES)
1958 {
1959 gfc_free_expr (len);
a8b3b0b6 1960 gfc_free_expr (kind_expr);
6de9cd9a
DN
1961 return m;
1962 }
1963
1964 /* Do some final massaging of the length values. */
1965 cl = gfc_get_charlen ();
1966 cl->next = gfc_current_ns->cl_list;
1967 gfc_current_ns->cl_list = cl;
1968
1969 if (seen_length == 0)
1970 cl->length = gfc_int_expr (1);
1971 else
5cd09fac 1972 cl->length = len;
6de9cd9a
DN
1973
1974 ts->cl = cl;
1975 ts->kind = kind;
1976
a8b3b0b6
CR
1977 /* We have to know if it was a c interoperable kind so we can
1978 do accurate type checking of bind(c) procs, etc. */
1979 if (kind_expr != NULL)
1980 {
1981 /* Mark this as c interoperable if being declared with one
1982 of the named constants from iso_c_binding. */
1983 ts->is_c_interop = kind_expr->ts.is_iso_c;
1984 gfc_free_expr (kind_expr);
1985 }
1986 else if (len != NULL)
1987 {
1988 /* Here, we might have parsed something such as:
1989 character(c_char)
1990 In this case, the parsing code above grabs the c_char when
1991 looking for the length (line 1690, roughly). it's the last
1992 testcase for parsing the kind params of a character variable.
1993 However, it's not actually the length. this seems like it
1994 could be an error.
1995 To see if the user used a C interop kind, test the expr
1996 of the so called length, and see if it's C interoperable. */
1997 ts->is_c_interop = len->ts.is_iso_c;
1998 }
1999
6de9cd9a
DN
2000 return MATCH_YES;
2001}
2002
2003
2004/* Matches a type specification. If successful, sets the ts structure
2005 to the matched specification. This is necessary for FUNCTION and
2006 IMPLICIT statements.
2007
d51347f9 2008 If implicit_flag is nonzero, then we don't check for the optional
e5ddaa24 2009 kind specification. Not doing so is needed for matching an IMPLICIT
6de9cd9a
DN
2010 statement correctly. */
2011
e5ddaa24 2012static match
636dff67 2013match_type_spec (gfc_typespec *ts, int implicit_flag)
6de9cd9a
DN
2014{
2015 char name[GFC_MAX_SYMBOL_LEN + 1];
2016 gfc_symbol *sym;
2017 match m;
0ff0dfbf 2018 int c;
6de9cd9a
DN
2019
2020 gfc_clear_ts (ts);
2021
a8b3b0b6
CR
2022 /* Clear the current binding label, in case one is given. */
2023 curr_binding_label[0] = '\0';
2024
5f700e6d
AL
2025 if (gfc_match (" byte") == MATCH_YES)
2026 {
d51347f9 2027 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
5f700e6d
AL
2028 == FAILURE)
2029 return MATCH_ERROR;
2030
2031 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2032 {
2033 gfc_error ("BYTE type used at %C "
2034 "is not available on the target machine");
2035 return MATCH_ERROR;
2036 }
d51347f9 2037
5f700e6d
AL
2038 ts->type = BT_INTEGER;
2039 ts->kind = 1;
2040 return MATCH_YES;
2041 }
2042
6de9cd9a
DN
2043 if (gfc_match (" integer") == MATCH_YES)
2044 {
2045 ts->type = BT_INTEGER;
9d64df18 2046 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
2047 goto get_kind;
2048 }
2049
2050 if (gfc_match (" character") == MATCH_YES)
2051 {
2052 ts->type = BT_CHARACTER;
e5ddaa24
TS
2053 if (implicit_flag == 0)
2054 return match_char_spec (ts);
2055 else
2056 return MATCH_YES;
6de9cd9a
DN
2057 }
2058
2059 if (gfc_match (" real") == MATCH_YES)
2060 {
2061 ts->type = BT_REAL;
9d64df18 2062 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
2063 goto get_kind;
2064 }
2065
2066 if (gfc_match (" double precision") == MATCH_YES)
2067 {
2068 ts->type = BT_REAL;
9d64df18 2069 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2070 return MATCH_YES;
2071 }
2072
2073 if (gfc_match (" complex") == MATCH_YES)
2074 {
2075 ts->type = BT_COMPLEX;
9d64df18 2076 ts->kind = gfc_default_complex_kind;
6de9cd9a
DN
2077 goto get_kind;
2078 }
2079
2080 if (gfc_match (" double complex") == MATCH_YES)
2081 {
df8652dc
SK
2082 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2083 "conform to the Fortran 95 standard") == FAILURE)
2084 return MATCH_ERROR;
2085
6de9cd9a 2086 ts->type = BT_COMPLEX;
9d64df18 2087 ts->kind = gfc_default_double_kind;
6de9cd9a
DN
2088 return MATCH_YES;
2089 }
2090
2091 if (gfc_match (" logical") == MATCH_YES)
2092 {
2093 ts->type = BT_LOGICAL;
9d64df18 2094 ts->kind = gfc_default_logical_kind;
6de9cd9a
DN
2095 goto get_kind;
2096 }
2097
2098 m = gfc_match (" type ( %n )", name);
2099 if (m != MATCH_YES)
2100 return m;
2101
2102 /* Search for the name but allow the components to be defined later. */
2103 if (gfc_get_ha_symbol (name, &sym))
2104 {
2105 gfc_error ("Type name '%s' at %C is ambiguous", name);
2106 return MATCH_ERROR;
2107 }
2108
2109 if (sym->attr.flavor != FL_DERIVED
231b2fcc 2110 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
2111 return MATCH_ERROR;
2112
2113 ts->type = BT_DERIVED;
2114 ts->kind = 0;
2115 ts->derived = sym;
2116
2117 return MATCH_YES;
2118
2119get_kind:
2120 /* For all types except double, derived and character, look for an
2121 optional kind specifier. MATCH_NO is actually OK at this point. */
e5ddaa24 2122 if (implicit_flag == 1)
6de9cd9a
DN
2123 return MATCH_YES;
2124
0ff0dfbf
TS
2125 if (gfc_current_form == FORM_FREE)
2126 {
2127 c = gfc_peek_char();
2128 if (!gfc_is_whitespace(c) && c != '*' && c != '('
636dff67 2129 && c != ':' && c != ',')
0ff0dfbf
TS
2130 return MATCH_NO;
2131 }
2132
6de9cd9a
DN
2133 m = gfc_match_kind_spec (ts);
2134 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2135 m = gfc_match_old_kind_spec (ts);
2136
2137 if (m == MATCH_NO)
2138 m = MATCH_YES; /* No kind specifier found. */
2139
2140 return m;
2141}
2142
2143
e5ddaa24
TS
2144/* Match an IMPLICIT NONE statement. Actually, this statement is
2145 already matched in parse.c, or we would not end up here in the
2146 first place. So the only thing we need to check, is if there is
2147 trailing garbage. If not, the match is successful. */
2148
2149match
2150gfc_match_implicit_none (void)
2151{
e5ddaa24
TS
2152 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2153}
2154
2155
2156/* Match the letter range(s) of an IMPLICIT statement. */
2157
2158static match
1107b970 2159match_implicit_range (void)
e5ddaa24
TS
2160{
2161 int c, c1, c2, inner;
2162 locus cur_loc;
2163
2164 cur_loc = gfc_current_locus;
2165
2166 gfc_gobble_whitespace ();
2167 c = gfc_next_char ();
2168 if (c != '(')
2169 {
2170 gfc_error ("Missing character range in IMPLICIT at %C");
2171 goto bad;
2172 }
2173
2174 inner = 1;
2175 while (inner)
2176 {
2177 gfc_gobble_whitespace ();
2178 c1 = gfc_next_char ();
2179 if (!ISALPHA (c1))
2180 goto bad;
2181
2182 gfc_gobble_whitespace ();
2183 c = gfc_next_char ();
2184
2185 switch (c)
2186 {
2187 case ')':
66e4ab31 2188 inner = 0; /* Fall through. */
e5ddaa24
TS
2189
2190 case ',':
2191 c2 = c1;
2192 break;
2193
2194 case '-':
2195 gfc_gobble_whitespace ();
2196 c2 = gfc_next_char ();
2197 if (!ISALPHA (c2))
2198 goto bad;
2199
2200 gfc_gobble_whitespace ();
2201 c = gfc_next_char ();
2202
2203 if ((c != ',') && (c != ')'))
2204 goto bad;
2205 if (c == ')')
2206 inner = 0;
2207
2208 break;
2209
2210 default:
2211 goto bad;
2212 }
2213
2214 if (c1 > c2)
2215 {
2216 gfc_error ("Letters must be in alphabetic order in "
2217 "IMPLICIT statement at %C");
2218 goto bad;
2219 }
2220
2221 /* See if we can add the newly matched range to the pending
636dff67
SK
2222 implicits from this IMPLICIT statement. We do not check for
2223 conflicts with whatever earlier IMPLICIT statements may have
2224 set. This is done when we've successfully finished matching
2225 the current one. */
1107b970 2226 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
e5ddaa24
TS
2227 goto bad;
2228 }
2229
2230 return MATCH_YES;
2231
2232bad:
2233 gfc_syntax_error (ST_IMPLICIT);
2234
2235 gfc_current_locus = cur_loc;
2236 return MATCH_ERROR;
2237}
2238
2239
2240/* Match an IMPLICIT statement, storing the types for
2241 gfc_set_implicit() if the statement is accepted by the parser.
2242 There is a strange looking, but legal syntactic construction
2243 possible. It looks like:
2244
2245 IMPLICIT INTEGER (a-b) (c-d)
2246
2247 This is legal if "a-b" is a constant expression that happens to
2248 equal one of the legal kinds for integers. The real problem
2249 happens with an implicit specification that looks like:
2250
2251 IMPLICIT INTEGER (a-b)
2252
2253 In this case, a typespec matcher that is "greedy" (as most of the
2254 matchers are) gobbles the character range as a kindspec, leaving
2255 nothing left. We therefore have to go a bit more slowly in the
2256 matching process by inhibiting the kindspec checking during
2257 typespec matching and checking for a kind later. */
2258
2259match
2260gfc_match_implicit (void)
2261{
2262 gfc_typespec ts;
2263 locus cur_loc;
2264 int c;
2265 match m;
2266
2267 /* We don't allow empty implicit statements. */
2268 if (gfc_match_eos () == MATCH_YES)
2269 {
2270 gfc_error ("Empty IMPLICIT statement at %C");
2271 return MATCH_ERROR;
2272 }
2273
e5ddaa24
TS
2274 do
2275 {
1107b970
PB
2276 /* First cleanup. */
2277 gfc_clear_new_implicit ();
2278
e5ddaa24
TS
2279 /* A basic type is mandatory here. */
2280 m = match_type_spec (&ts, 1);
2281 if (m == MATCH_ERROR)
2282 goto error;
2283 if (m == MATCH_NO)
2284 goto syntax;
2285
2286 cur_loc = gfc_current_locus;
1107b970 2287 m = match_implicit_range ();
e5ddaa24
TS
2288
2289 if (m == MATCH_YES)
2290 {
1107b970 2291 /* We may have <TYPE> (<RANGE>). */
e5ddaa24
TS
2292 gfc_gobble_whitespace ();
2293 c = gfc_next_char ();
2294 if ((c == '\n') || (c == ','))
1107b970
PB
2295 {
2296 /* Check for CHARACTER with no length parameter. */
2297 if (ts.type == BT_CHARACTER && !ts.cl)
2298 {
9d64df18 2299 ts.kind = gfc_default_character_kind;
1107b970
PB
2300 ts.cl = gfc_get_charlen ();
2301 ts.cl->next = gfc_current_ns->cl_list;
2302 gfc_current_ns->cl_list = ts.cl;
2303 ts.cl->length = gfc_int_expr (1);
2304 }
2305
2306 /* Record the Successful match. */
2307 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2308 return MATCH_ERROR;
2309 continue;
2310 }
e5ddaa24
TS
2311
2312 gfc_current_locus = cur_loc;
2313 }
2314
1107b970
PB
2315 /* Discard the (incorrectly) matched range. */
2316 gfc_clear_new_implicit ();
2317
2318 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2319 if (ts.type == BT_CHARACTER)
2320 m = match_char_spec (&ts);
2321 else
e5ddaa24 2322 {
1107b970 2323 m = gfc_match_kind_spec (&ts);
e5ddaa24 2324 if (m == MATCH_NO)
1107b970
PB
2325 {
2326 m = gfc_match_old_kind_spec (&ts);
2327 if (m == MATCH_ERROR)
2328 goto error;
2329 if (m == MATCH_NO)
2330 goto syntax;
2331 }
e5ddaa24 2332 }
1107b970
PB
2333 if (m == MATCH_ERROR)
2334 goto error;
e5ddaa24 2335
1107b970 2336 m = match_implicit_range ();
e5ddaa24
TS
2337 if (m == MATCH_ERROR)
2338 goto error;
2339 if (m == MATCH_NO)
2340 goto syntax;
2341
2342 gfc_gobble_whitespace ();
2343 c = gfc_next_char ();
2344 if ((c != '\n') && (c != ','))
2345 goto syntax;
2346
1107b970
PB
2347 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2348 return MATCH_ERROR;
e5ddaa24
TS
2349 }
2350 while (c == ',');
2351
1107b970 2352 return MATCH_YES;
e5ddaa24
TS
2353
2354syntax:
2355 gfc_syntax_error (ST_IMPLICIT);
2356
2357error:
2358 return MATCH_ERROR;
2359}
2360
66e4ab31 2361
8998be20
TB
2362match
2363gfc_match_import (void)
2364{
2365 char name[GFC_MAX_SYMBOL_LEN + 1];
2366 match m;
2367 gfc_symbol *sym;
2368 gfc_symtree *st;
2369
66e4ab31
SK
2370 if (gfc_current_ns->proc_name == NULL
2371 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
8998be20
TB
2372 {
2373 gfc_error ("IMPORT statement at %C only permitted in "
2374 "an INTERFACE body");
2375 return MATCH_ERROR;
2376 }
2377
636dff67 2378 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
8998be20
TB
2379 == FAILURE)
2380 return MATCH_ERROR;
2381
2382 if (gfc_match_eos () == MATCH_YES)
2383 {
2384 /* All host variables should be imported. */
2385 gfc_current_ns->has_import_set = 1;
2386 return MATCH_YES;
2387 }
2388
2389 if (gfc_match (" ::") == MATCH_YES)
2390 {
2391 if (gfc_match_eos () == MATCH_YES)
636dff67
SK
2392 {
2393 gfc_error ("Expecting list of named entities at %C");
2394 return MATCH_ERROR;
2395 }
8998be20
TB
2396 }
2397
2398 for(;;)
2399 {
2400 m = gfc_match (" %n", name);
2401 switch (m)
2402 {
2403 case MATCH_YES:
36d3fb4c 2404 if (gfc_current_ns->parent != NULL
66e4ab31 2405 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
36d3fb4c
PT
2406 {
2407 gfc_error ("Type name '%s' at %C is ambiguous", name);
2408 return MATCH_ERROR;
2409 }
2410 else if (gfc_current_ns->proc_name->ns->parent != NULL
66e4ab31
SK
2411 && gfc_find_symbol (name,
2412 gfc_current_ns->proc_name->ns->parent,
2413 1, &sym))
636dff67
SK
2414 {
2415 gfc_error ("Type name '%s' at %C is ambiguous", name);
2416 return MATCH_ERROR;
2417 }
2418
2419 if (sym == NULL)
2420 {
2421 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2422 "at %C - does not exist.", name);
2423 return MATCH_ERROR;
2424 }
2425
d51347f9 2426 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
636dff67
SK
2427 {
2428 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2429 "at %C.", name);
2430 goto next_item;
2431 }
2432
2433 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2434 st->n.sym = sym;
2435 sym->refs++;
2436 sym->ns = gfc_current_ns;
8998be20
TB
2437
2438 goto next_item;
2439
2440 case MATCH_NO:
2441 break;
2442
2443 case MATCH_ERROR:
2444 return MATCH_ERROR;
2445 }
2446
2447 next_item:
2448 if (gfc_match_eos () == MATCH_YES)
2449 break;
2450 if (gfc_match_char (',') != MATCH_YES)
2451 goto syntax;
2452 }
2453
2454 return MATCH_YES;
2455
2456syntax:
2457 gfc_error ("Syntax error in IMPORT statement at %C");
2458 return MATCH_ERROR;
2459}
e5ddaa24 2460
66e4ab31 2461
6de9cd9a
DN
2462/* Matches an attribute specification including array specs. If
2463 successful, leaves the variables current_attr and current_as
2464 holding the specification. Also sets the colon_seen variable for
2465 later use by matchers associated with initializations.
2466
2467 This subroutine is a little tricky in the sense that we don't know
2468 if we really have an attr-spec until we hit the double colon.
2469 Until that time, we can only return MATCH_NO. This forces us to
2470 check for duplicate specification at this level. */
2471
2472static match
2473match_attr_spec (void)
2474{
6de9cd9a
DN
2475 /* Modifiers that can exist in a type statement. */
2476 typedef enum
2477 { GFC_DECL_BEGIN = 0,
2478 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2479 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
ee7e677f
TB
2480 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2481 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
a8b3b0b6 2482 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
6de9cd9a
DN
2483 GFC_DECL_END /* Sentinel */
2484 }
2485 decl_types;
2486
2487/* GFC_DECL_END is the sentinel, index starts at 0. */
2488#define NUM_DECL GFC_DECL_END
2489
2490 static mstring decls[] = {
2491 minit (", allocatable", DECL_ALLOCATABLE),
2492 minit (", dimension", DECL_DIMENSION),
2493 minit (", external", DECL_EXTERNAL),
2494 minit (", intent ( in )", DECL_IN),
2495 minit (", intent ( out )", DECL_OUT),
2496 minit (", intent ( in out )", DECL_INOUT),
2497 minit (", intrinsic", DECL_INTRINSIC),
2498 minit (", optional", DECL_OPTIONAL),
2499 minit (", parameter", DECL_PARAMETER),
2500 minit (", pointer", DECL_POINTER),
ee7e677f 2501 minit (", protected", DECL_PROTECTED),
6de9cd9a
DN
2502 minit (", private", DECL_PRIVATE),
2503 minit (", public", DECL_PUBLIC),
2504 minit (", save", DECL_SAVE),
2505 minit (", target", DECL_TARGET),
06469efd 2506 minit (", value", DECL_VALUE),
775e6c3a 2507 minit (", volatile", DECL_VOLATILE),
6de9cd9a
DN
2508 minit ("::", DECL_COLON),
2509 minit (NULL, DECL_NONE)
2510 };
2511
2512 locus start, seen_at[NUM_DECL];
2513 int seen[NUM_DECL];
2514 decl_types d;
2515 const char *attr;
2516 match m;
2517 try t;
a8b3b0b6 2518 char peek_char;
6de9cd9a
DN
2519
2520 gfc_clear_attr (&current_attr);
63645982 2521 start = gfc_current_locus;
6de9cd9a
DN
2522
2523 current_as = NULL;
2524 colon_seen = 0;
2525
2526 /* See if we get all of the keywords up to the final double colon. */
2527 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2528 seen[d] = 0;
2529
2530 for (;;)
2531 {
2532 d = (decl_types) gfc_match_strings (decls);
a8b3b0b6
CR
2533
2534 if (d == DECL_NONE)
2535 {
2536 /* See if we can find the bind(c) since all else failed.
2537 We need to skip over any whitespace and stop on the ','. */
2538 gfc_gobble_whitespace ();
2539 peek_char = gfc_peek_char ();
2540 if (peek_char == ',')
2541 {
2542 /* Chomp the comma. */
2543 peek_char = gfc_next_char ();
2544 /* Try and match the bind(c). */
d468bcdb 2545 if (gfc_match_bind_c (NULL) == MATCH_YES)
a8b3b0b6 2546 d = DECL_IS_BIND_C;
a8b3b0b6
CR
2547 }
2548 }
d468bcdb 2549
6de9cd9a
DN
2550 if (d == DECL_NONE || d == DECL_COLON)
2551 break;
d51347f9 2552
6de9cd9a 2553 seen[d]++;
63645982 2554 seen_at[d] = gfc_current_locus;
6de9cd9a
DN
2555
2556 if (d == DECL_DIMENSION)
2557 {
2558 m = gfc_match_array_spec (&current_as);
2559
2560 if (m == MATCH_NO)
2561 {
2562 gfc_error ("Missing dimension specification at %C");
2563 m = MATCH_ERROR;
2564 }
2565
2566 if (m == MATCH_ERROR)
2567 goto cleanup;
2568 }
2569 }
2570
2571 /* No double colon, so assume that we've been looking at something
2572 else the whole time. */
2573 if (d == DECL_NONE)
2574 {
2575 m = MATCH_NO;
2576 goto cleanup;
2577 }
2578
2579 /* Since we've seen a double colon, we have to be looking at an
2580 attr-spec. This means that we can now issue errors. */
2581 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2582 if (seen[d] > 1)
2583 {
2584 switch (d)
2585 {
2586 case DECL_ALLOCATABLE:
2587 attr = "ALLOCATABLE";
2588 break;
2589 case DECL_DIMENSION:
2590 attr = "DIMENSION";
2591 break;
2592 case DECL_EXTERNAL:
2593 attr = "EXTERNAL";
2594 break;
2595 case DECL_IN:
2596 attr = "INTENT (IN)";
2597 break;
2598 case DECL_OUT:
2599 attr = "INTENT (OUT)";
2600 break;
2601 case DECL_INOUT:
2602 attr = "INTENT (IN OUT)";
2603 break;
2604 case DECL_INTRINSIC:
2605 attr = "INTRINSIC";
2606 break;
2607 case DECL_OPTIONAL:
2608 attr = "OPTIONAL";
2609 break;
2610 case DECL_PARAMETER:
2611 attr = "PARAMETER";
2612 break;
2613 case DECL_POINTER:
2614 attr = "POINTER";
2615 break;
ee7e677f
TB
2616 case DECL_PROTECTED:
2617 attr = "PROTECTED";
2618 break;
6de9cd9a
DN
2619 case DECL_PRIVATE:
2620 attr = "PRIVATE";
2621 break;
2622 case DECL_PUBLIC:
2623 attr = "PUBLIC";
2624 break;
2625 case DECL_SAVE:
2626 attr = "SAVE";
2627 break;
2628 case DECL_TARGET:
2629 attr = "TARGET";
2630 break;
a8b3b0b6
CR
2631 case DECL_IS_BIND_C:
2632 attr = "IS_BIND_C";
2633 break;
2634 case DECL_VALUE:
2635 attr = "VALUE";
2636 break;
775e6c3a
TB
2637 case DECL_VOLATILE:
2638 attr = "VOLATILE";
2639 break;
6de9cd9a 2640 default:
66e4ab31 2641 attr = NULL; /* This shouldn't happen. */
6de9cd9a
DN
2642 }
2643
2644 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2645 m = MATCH_ERROR;
2646 goto cleanup;
2647 }
2648
2649 /* Now that we've dealt with duplicate attributes, add the attributes
2650 to the current attribute. */
2651 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2652 {
2653 if (seen[d] == 0)
2654 continue;
2655
2656 if (gfc_current_state () == COMP_DERIVED
2657 && d != DECL_DIMENSION && d != DECL_POINTER
d51347f9
TB
2658 && d != DECL_COLON && d != DECL_PRIVATE
2659 && d != DECL_PUBLIC && d != DECL_NONE)
6de9cd9a 2660 {
5046aff5
PT
2661 if (d == DECL_ALLOCATABLE)
2662 {
636dff67
SK
2663 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2664 "attribute at %C in a TYPE definition")
d51347f9 2665 == FAILURE)
5046aff5
PT
2666 {
2667 m = MATCH_ERROR;
2668 goto cleanup;
2669 }
636dff67
SK
2670 }
2671 else
5046aff5
PT
2672 {
2673 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
d51347f9 2674 &seen_at[d]);
5046aff5
PT
2675 m = MATCH_ERROR;
2676 goto cleanup;
2677 }
6de9cd9a
DN
2678 }
2679
4213f93b 2680 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
636dff67 2681 && gfc_current_state () != COMP_MODULE)
4213f93b
PT
2682 {
2683 if (d == DECL_PRIVATE)
2684 attr = "PRIVATE";
2685 else
2686 attr = "PUBLIC";
d51347f9
TB
2687 if (gfc_current_state () == COMP_DERIVED
2688 && gfc_state_stack->previous
2689 && gfc_state_stack->previous->state == COMP_MODULE)
2690 {
2691 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2692 "at %L in a TYPE definition", attr,
2693 &seen_at[d])
2694 == FAILURE)
2695 {
2696 m = MATCH_ERROR;
2697 goto cleanup;
2698 }
2699 }
2700 else
2701 {
2702 gfc_error ("%s attribute at %L is not allowed outside of the "
2703 "specification part of a module", attr, &seen_at[d]);
2704 m = MATCH_ERROR;
2705 goto cleanup;
2706 }
4213f93b
PT
2707 }
2708
6de9cd9a
DN
2709 switch (d)
2710 {
2711 case DECL_ALLOCATABLE:
2712 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2713 break;
2714
2715 case DECL_DIMENSION:
231b2fcc 2716 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2717 break;
2718
2719 case DECL_EXTERNAL:
2720 t = gfc_add_external (&current_attr, &seen_at[d]);
2721 break;
2722
2723 case DECL_IN:
2724 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2725 break;
2726
2727 case DECL_OUT:
2728 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2729 break;
2730
2731 case DECL_INOUT:
2732 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2733 break;
2734
2735 case DECL_INTRINSIC:
2736 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2737 break;
2738
2739 case DECL_OPTIONAL:
2740 t = gfc_add_optional (&current_attr, &seen_at[d]);
2741 break;
2742
2743 case DECL_PARAMETER:
231b2fcc 2744 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
6de9cd9a
DN
2745 break;
2746
2747 case DECL_POINTER:
2748 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2749 break;
2750
ee7e677f
TB
2751 case DECL_PROTECTED:
2752 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2753 {
2754 gfc_error ("PROTECTED at %C only allowed in specification "
2755 "part of a module");
2756 t = FAILURE;
2757 break;
2758 }
2759
636dff67
SK
2760 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2761 "attribute at %C")
ee7e677f
TB
2762 == FAILURE)
2763 t = FAILURE;
2764 else
2765 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2766 break;
2767
6de9cd9a 2768 case DECL_PRIVATE:
231b2fcc
TS
2769 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2770 &seen_at[d]);
6de9cd9a
DN
2771 break;
2772
2773 case DECL_PUBLIC:
231b2fcc
TS
2774 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2775 &seen_at[d]);
6de9cd9a
DN
2776 break;
2777
2778 case DECL_SAVE:
231b2fcc 2779 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
6de9cd9a
DN
2780 break;
2781
2782 case DECL_TARGET:
2783 t = gfc_add_target (&current_attr, &seen_at[d]);
2784 break;
2785
a8b3b0b6
CR
2786 case DECL_IS_BIND_C:
2787 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
2788 break;
2789
06469efd 2790 case DECL_VALUE:
636dff67
SK
2791 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2792 "at %C")
06469efd
PT
2793 == FAILURE)
2794 t = FAILURE;
2795 else
2796 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
2797 break;
2798
775e6c3a
TB
2799 case DECL_VOLATILE:
2800 if (gfc_notify_std (GFC_STD_F2003,
636dff67 2801 "Fortran 2003: VOLATILE attribute at %C")
775e6c3a
TB
2802 == FAILURE)
2803 t = FAILURE;
2804 else
2805 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
2806 break;
2807
6de9cd9a
DN
2808 default:
2809 gfc_internal_error ("match_attr_spec(): Bad attribute");
2810 }
2811
2812 if (t == FAILURE)
2813 {
2814 m = MATCH_ERROR;
2815 goto cleanup;
2816 }
2817 }
2818
2819 colon_seen = 1;
2820 return MATCH_YES;
2821
2822cleanup:
63645982 2823 gfc_current_locus = start;
6de9cd9a
DN
2824 gfc_free_array_spec (current_as);
2825 current_as = NULL;
2826 return m;
2827}
2828
2829
a8b3b0b6
CR
2830/* Set the binding label, dest_label, either with the binding label
2831 stored in the given gfc_typespec, ts, or if none was provided, it
2832 will be the symbol name in all lower case, as required by the draft
2833 (J3/04-007, section 15.4.1). If a binding label was given and
2834 there is more than one argument (num_idents), it is an error. */
2835
2836try
2837set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2838{
2839 if (curr_binding_label[0] != '\0')
2840 {
2841 if (num_idents > 1 || num_idents_on_line > 1)
2842 {
2843 gfc_error ("Multiple identifiers provided with "
2844 "single NAME= specifier at %C");
2845 return FAILURE;
2846 }
2847
2848 /* Binding label given; store in temp holder til have sym. */
2849 strncpy (dest_label, curr_binding_label,
2850 strlen (curr_binding_label) + 1);
2851 }
2852 else
2853 {
2854 /* No binding label given, and the NAME= specifier did not exist,
2855 which means there was no NAME="". */
2856 if (sym_name != NULL && has_name_equals == 0)
2857 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2858 }
2859
2860 return SUCCESS;
2861}
2862
2863
2864/* Set the status of the given common block as being BIND(C) or not,
2865 depending on the given parameter, is_bind_c. */
2866
2867void
2868set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2869{
2870 com_block->is_bind_c = is_bind_c;
2871 return;
2872}
2873
2874
2875/* Verify that the given gfc_typespec is for a C interoperable type. */
2876
2877try
2878verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2879{
2880 try t;
2881
2882 /* Make sure the kind used is appropriate for the type.
2883 The f90_type is unknown if an integer constant was
2884 used (e.g., real(4), bind(c) :: myFloat). */
2885 if (ts->f90_type != BT_UNKNOWN)
2886 {
2887 t = gfc_validate_c_kind (ts);
2888 if (t != SUCCESS)
2889 {
2890 /* Print an error, but continue parsing line. */
2891 gfc_error_now ("C kind parameter is for type %s but "
2892 "symbol '%s' at %L is of type %s",
2893 gfc_basic_typename (ts->f90_type),
2894 name, where,
2895 gfc_basic_typename (ts->type));
2896 }
2897 }
2898
2899 /* Make sure the kind is C interoperable. This does not care about the
2900 possible error above. */
2901 if (ts->type == BT_DERIVED && ts->derived != NULL)
2902 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2903 else if (ts->is_c_interop != 1)
2904 return FAILURE;
2905
2906 return SUCCESS;
2907}
2908
2909
2910/* Verify that the variables of a given common block, which has been
2911 defined with the attribute specifier bind(c), to be of a C
2912 interoperable type. Errors will be reported here, if
2913 encountered. */
2914
2915try
2916verify_com_block_vars_c_interop (gfc_common_head *com_block)
2917{
2918 gfc_symbol *curr_sym = NULL;
2919 try retval = SUCCESS;
2920
2921 curr_sym = com_block->head;
2922
2923 /* Make sure we have at least one symbol. */
2924 if (curr_sym == NULL)
2925 return retval;
2926
2927 /* Here we know we have a symbol, so we'll execute this loop
2928 at least once. */
2929 do
2930 {
2931 /* The second to last param, 1, says this is in a common block. */
2932 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2933 curr_sym = curr_sym->common_next;
2934 } while (curr_sym != NULL);
2935
2936 return retval;
2937}
2938
2939
2940/* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2941 an appropriate error message is reported. */
2942
2943try
2944verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2945 int is_in_common, gfc_common_head *com_block)
2946{
2947 try retval = SUCCESS;
d8fa96e0
CR
2948
2949 if (tmp_sym->attr.function && tmp_sym->result != NULL)
2950 {
2951 tmp_sym = tmp_sym->result;
2952 /* Make sure it wasn't an implicitly typed result. */
2953 if (tmp_sym->attr.implicit_type)
2954 {
2955 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
2956 "%L may not be C interoperable", tmp_sym->name,
2957 &tmp_sym->declared_at);
2958 tmp_sym->ts.f90_type = tmp_sym->ts.type;
2959 /* Mark it as C interoperable to prevent duplicate warnings. */
2960 tmp_sym->ts.is_c_interop = 1;
2961 tmp_sym->attr.is_c_interop = 1;
2962 }
2963 }
a8b3b0b6
CR
2964
2965 /* Here, we know we have the bind(c) attribute, so if we have
2966 enough type info, then verify that it's a C interop kind.
2967 The info could be in the symbol already, or possibly still in
2968 the given ts (current_ts), so look in both. */
2969 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2970 {
2971 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2972 &(tmp_sym->declared_at)) != SUCCESS)
2973 {
2974 /* See if we're dealing with a sym in a common block or not. */
2975 if (is_in_common == 1)
2976 {
2977 gfc_warning ("Variable '%s' in common block '%s' at %L "
2978 "may not be a C interoperable "
2979 "kind though common block '%s' is BIND(C)",
2980 tmp_sym->name, com_block->name,
2981 &(tmp_sym->declared_at), com_block->name);
2982 }
2983 else
2984 {
2985 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2986 gfc_error ("Type declaration '%s' at %L is not C "
2987 "interoperable but it is BIND(C)",
2988 tmp_sym->name, &(tmp_sym->declared_at));
2989 else
2990 gfc_warning ("Variable '%s' at %L "
2991 "may not be a C interoperable "
2992 "kind but it is bind(c)",
2993 tmp_sym->name, &(tmp_sym->declared_at));
2994 }
2995 }
2996
2997 /* Variables declared w/in a common block can't be bind(c)
2998 since there's no way for C to see these variables, so there's
2999 semantically no reason for the attribute. */
3000 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3001 {
3002 gfc_error ("Variable '%s' in common block '%s' at "
3003 "%L cannot be declared with BIND(C) "
3004 "since it is not a global",
3005 tmp_sym->name, com_block->name,
3006 &(tmp_sym->declared_at));
3007 retval = FAILURE;
3008 }
3009
3010 /* Scalar variables that are bind(c) can not have the pointer
3011 or allocatable attributes. */
3012 if (tmp_sym->attr.is_bind_c == 1)
3013 {
3014 if (tmp_sym->attr.pointer == 1)
3015 {
3016 gfc_error ("Variable '%s' at %L cannot have both the "
3017 "POINTER and BIND(C) attributes",
3018 tmp_sym->name, &(tmp_sym->declared_at));
3019 retval = FAILURE;
3020 }
3021
3022 if (tmp_sym->attr.allocatable == 1)
3023 {
3024 gfc_error ("Variable '%s' at %L cannot have both the "
3025 "ALLOCATABLE and BIND(C) attributes",
3026 tmp_sym->name, &(tmp_sym->declared_at));
3027 retval = FAILURE;
3028 }
3029
3030 /* If it is a BIND(C) function, make sure the return value is a
3031 scalar value. The previous tests in this function made sure
3032 the type is interoperable. */
3033 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3034 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3035 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3036
3037 /* BIND(C) functions can not return a character string. */
3038 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3039 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3040 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3041 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3042 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3043 "be a character string", tmp_sym->name,
3044 &(tmp_sym->declared_at));
3045 }
3046 }
3047
3048 /* See if the symbol has been marked as private. If it has, make sure
3049 there is no binding label and warn the user if there is one. */
3050 if (tmp_sym->attr.access == ACCESS_PRIVATE
3051 && tmp_sym->binding_label[0] != '\0')
3052 /* Use gfc_warning_now because we won't say that the symbol fails
3053 just because of this. */
3054 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3055 "given the binding label '%s'", tmp_sym->name,
3056 &(tmp_sym->declared_at), tmp_sym->binding_label);
3057
3058 return retval;
3059}
3060
3061
3062/* Set the appropriate fields for a symbol that's been declared as
3063 BIND(C) (the is_bind_c flag and the binding label), and verify that
3064 the type is C interoperable. Errors are reported by the functions
3065 used to set/test these fields. */
3066
3067try
3068set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3069{
3070 try retval = SUCCESS;
3071
3072 /* TODO: Do we need to make sure the vars aren't marked private? */
3073
3074 /* Set the is_bind_c bit in symbol_attribute. */
3075 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3076
3077 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3078 num_idents) != SUCCESS)
3079 return FAILURE;
3080
3081 return retval;
3082}
3083
3084
3085/* Set the fields marking the given common block as BIND(C), including
3086 a binding label, and report any errors encountered. */
3087
3088try
3089set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3090{
3091 try retval = SUCCESS;
3092
3093 /* destLabel, common name, typespec (which may have binding label). */
3094 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3095 != SUCCESS)
3096 return FAILURE;
3097
3098 /* Set the given common block (com_block) to being bind(c) (1). */
3099 set_com_block_bind_c (com_block, 1);
3100
3101 return retval;
3102}
3103
3104
3105/* Retrieve the list of one or more identifiers that the given bind(c)
3106 attribute applies to. */
3107
3108try
3109get_bind_c_idents (void)
3110{
3111 char name[GFC_MAX_SYMBOL_LEN + 1];
3112 int num_idents = 0;
3113 gfc_symbol *tmp_sym = NULL;
3114 match found_id;
3115 gfc_common_head *com_block = NULL;
3116
3117 if (gfc_match_name (name) == MATCH_YES)
3118 {
3119 found_id = MATCH_YES;
3120 gfc_get_ha_symbol (name, &tmp_sym);
3121 }
3122 else if (match_common_name (name) == MATCH_YES)
3123 {
3124 found_id = MATCH_YES;
3125 com_block = gfc_get_common (name, 0);
3126 }
3127 else
3128 {
3129 gfc_error ("Need either entity or common block name for "
3130 "attribute specification statement at %C");
3131 return FAILURE;
3132 }
3133
3134 /* Save the current identifier and look for more. */
3135 do
3136 {
3137 /* Increment the number of identifiers found for this spec stmt. */
3138 num_idents++;
3139
3140 /* Make sure we have a sym or com block, and verify that it can
3141 be bind(c). Set the appropriate field(s) and look for more
3142 identifiers. */
3143 if (tmp_sym != NULL || com_block != NULL)
3144 {
3145 if (tmp_sym != NULL)
3146 {
3147 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3148 != SUCCESS)
3149 return FAILURE;
3150 }
3151 else
3152 {
3153 if (set_verify_bind_c_com_block(com_block, num_idents)
3154 != SUCCESS)
3155 return FAILURE;
3156 }
3157
3158 /* Look to see if we have another identifier. */
3159 tmp_sym = NULL;
3160 if (gfc_match_eos () == MATCH_YES)
3161 found_id = MATCH_NO;
3162 else if (gfc_match_char (',') != MATCH_YES)
3163 found_id = MATCH_NO;
3164 else if (gfc_match_name (name) == MATCH_YES)
3165 {
3166 found_id = MATCH_YES;
3167 gfc_get_ha_symbol (name, &tmp_sym);
3168 }
3169 else if (match_common_name (name) == MATCH_YES)
3170 {
3171 found_id = MATCH_YES;
3172 com_block = gfc_get_common (name, 0);
3173 }
3174 else
3175 {
3176 gfc_error ("Missing entity or common block name for "
3177 "attribute specification statement at %C");
3178 return FAILURE;
3179 }
3180 }
3181 else
3182 {
3183 gfc_internal_error ("Missing symbol");
3184 }
3185 } while (found_id == MATCH_YES);
3186
3187 /* if we get here we were successful */
3188 return SUCCESS;
3189}
3190
3191
3192/* Try and match a BIND(C) attribute specification statement. */
3193
3194match
3195gfc_match_bind_c_stmt (void)
3196{
3197 match found_match = MATCH_NO;
3198 gfc_typespec *ts;
3199
3200 ts = &current_ts;
3201
3202 /* This may not be necessary. */
3203 gfc_clear_ts (ts);
3204 /* Clear the temporary binding label holder. */
3205 curr_binding_label[0] = '\0';
3206
3207 /* Look for the bind(c). */
3208 found_match = gfc_match_bind_c (NULL);
3209
3210 if (found_match == MATCH_YES)
3211 {
3212 /* Look for the :: now, but it is not required. */
3213 gfc_match (" :: ");
3214
3215 /* Get the identifier(s) that needs to be updated. This may need to
3216 change to hand the flag(s) for the attr specified so all identifiers
3217 found can have all appropriate parts updated (assuming that the same
3218 spec stmt can have multiple attrs, such as both bind(c) and
3219 allocatable...). */
3220 if (get_bind_c_idents () != SUCCESS)
3221 /* Error message should have printed already. */
3222 return MATCH_ERROR;
3223 }
3224
3225 return found_match;
3226}
3227
3228
6de9cd9a
DN
3229/* Match a data declaration statement. */
3230
3231match
3232gfc_match_data_decl (void)
3233{
3234 gfc_symbol *sym;
3235 match m;
949d5b72 3236 int elem;
6de9cd9a 3237
a8b3b0b6
CR
3238 num_idents_on_line = 0;
3239
e5ddaa24 3240 m = match_type_spec (&current_ts, 0);
6de9cd9a
DN
3241 if (m != MATCH_YES)
3242 return m;
3243
3244 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3245 {
3246 sym = gfc_use_derived (current_ts.derived);
3247
3248 if (sym == NULL)
3249 {
3250 m = MATCH_ERROR;
3251 goto cleanup;
3252 }
3253
3254 current_ts.derived = sym;
3255 }
3256
3257 m = match_attr_spec ();
3258 if (m == MATCH_ERROR)
3259 {
3260 m = MATCH_NO;
3261 goto cleanup;
3262 }
3263
3264 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3265 {
3266
3267 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3268 goto ok;
3269
976e21f6 3270 gfc_find_symbol (current_ts.derived->name,
636dff67 3271 current_ts.derived->ns->parent, 1, &sym);
6de9cd9a 3272
976e21f6 3273 /* Any symbol that we find had better be a type definition
636dff67 3274 which has its components defined. */
976e21f6 3275 if (sym != NULL && sym->attr.flavor == FL_DERIVED
636dff67 3276 && current_ts.derived->components != NULL)
6de9cd9a
DN
3277 goto ok;
3278
976e21f6
PT
3279 /* Now we have an error, which we signal, and then fix up
3280 because the knock-on is plain and simple confusing. */
3281 gfc_error_now ("Derived type at %C has not been previously defined "
636dff67 3282 "and so cannot appear in a derived type definition");
976e21f6
PT
3283 current_attr.pointer = 1;
3284 goto ok;
6de9cd9a
DN
3285 }
3286
3287ok:
3288 /* If we have an old-style character declaration, and no new-style
3289 attribute specifications, then there a comma is optional between
3290 the type specification and the variable list. */
3291 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3292 gfc_match_char (',');
3293
949d5b72
PT
3294 /* Give the types/attributes to symbols that follow. Give the element
3295 a number so that repeat character length expressions can be copied. */
3296 elem = 1;
6de9cd9a
DN
3297 for (;;)
3298 {
a8b3b0b6 3299 num_idents_on_line++;
949d5b72 3300 m = variable_decl (elem++);
6de9cd9a
DN
3301 if (m == MATCH_ERROR)
3302 goto cleanup;
3303 if (m == MATCH_NO)
3304 break;
3305
3306 if (gfc_match_eos () == MATCH_YES)
3307 goto cleanup;
3308 if (gfc_match_char (',') != MATCH_YES)
3309 break;
3310 }
3311
8f81c3c6
PT
3312 if (gfc_error_flag_test () == 0)
3313 gfc_error ("Syntax error in data declaration at %C");
6de9cd9a
DN
3314 m = MATCH_ERROR;
3315
a9f6f1f2
JD
3316 gfc_free_data_all (gfc_current_ns);
3317
6de9cd9a
DN
3318cleanup:
3319 gfc_free_array_spec (current_as);
3320 current_as = NULL;
3321 return m;
3322}
3323
3324
3325/* Match a prefix associated with a function or subroutine
3326 declaration. If the typespec pointer is nonnull, then a typespec
3327 can be matched. Note that if nothing matches, MATCH_YES is
3328 returned (the null string was matched). */
3329
3330static match
636dff67 3331match_prefix (gfc_typespec *ts)
6de9cd9a
DN
3332{
3333 int seen_type;
3334
3335 gfc_clear_attr (&current_attr);
3336 seen_type = 0;
3337
3338loop:
3339 if (!seen_type && ts != NULL
e5ddaa24 3340 && match_type_spec (ts, 0) == MATCH_YES
6de9cd9a
DN
3341 && gfc_match_space () == MATCH_YES)
3342 {
3343
3344 seen_type = 1;
3345 goto loop;
3346 }
3347
3348 if (gfc_match ("elemental% ") == MATCH_YES)
3349 {
3350 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3351 return MATCH_ERROR;
3352
3353 goto loop;
3354 }
3355
3356 if (gfc_match ("pure% ") == MATCH_YES)
3357 {
3358 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3359 return MATCH_ERROR;
3360
3361 goto loop;
3362 }
3363
3364 if (gfc_match ("recursive% ") == MATCH_YES)
3365 {
3366 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3367 return MATCH_ERROR;
3368
3369 goto loop;
3370 }
3371
3372 /* At this point, the next item is not a prefix. */
3373 return MATCH_YES;
3374}
3375
3376
3377/* Copy attributes matched by match_prefix() to attributes on a symbol. */
3378
3379static try
636dff67 3380copy_prefix (symbol_attribute *dest, locus *where)
6de9cd9a 3381{
6de9cd9a
DN
3382 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3383 return FAILURE;
3384
3385 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3386 return FAILURE;
3387
3388 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3389 return FAILURE;
3390
3391 return SUCCESS;
3392}
3393
3394
3395/* Match a formal argument list. */
3396
3397match
636dff67 3398gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
6de9cd9a
DN
3399{
3400 gfc_formal_arglist *head, *tail, *p, *q;
3401 char name[GFC_MAX_SYMBOL_LEN + 1];
3402 gfc_symbol *sym;
3403 match m;
3404
3405 head = tail = NULL;
3406
3407 if (gfc_match_char ('(') != MATCH_YES)
3408 {
3409 if (null_flag)
3410 goto ok;
3411 return MATCH_NO;
3412 }
3413
3414 if (gfc_match_char (')') == MATCH_YES)
3415 goto ok;
3416
3417 for (;;)
3418 {
3419 if (gfc_match_char ('*') == MATCH_YES)
3420 sym = NULL;
3421 else
3422 {
3423 m = gfc_match_name (name);
3424 if (m != MATCH_YES)
3425 goto cleanup;
3426
3427 if (gfc_get_symbol (name, NULL, &sym))
3428 goto cleanup;
3429 }
3430
3431 p = gfc_get_formal_arglist ();
3432
3433 if (head == NULL)
3434 head = tail = p;
3435 else
3436 {
3437 tail->next = p;
3438 tail = p;
3439 }
3440
3441 tail->sym = sym;
3442
3443 /* We don't add the VARIABLE flavor because the name could be a
636dff67
SK
3444 dummy procedure. We don't apply these attributes to formal
3445 arguments of statement functions. */
6de9cd9a 3446 if (sym != NULL && !st_flag
231b2fcc 3447 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
6de9cd9a
DN
3448 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3449 {
3450 m = MATCH_ERROR;
3451 goto cleanup;
3452 }
3453
3454 /* The name of a program unit can be in a different namespace,
636dff67
SK
3455 so check for it explicitly. After the statement is accepted,
3456 the name is checked for especially in gfc_get_symbol(). */
6de9cd9a
DN
3457 if (gfc_new_block != NULL && sym != NULL
3458 && strcmp (sym->name, gfc_new_block->name) == 0)
3459 {
3460 gfc_error ("Name '%s' at %C is the name of the procedure",
3461 sym->name);
3462 m = MATCH_ERROR;
3463 goto cleanup;
3464 }
3465
3466 if (gfc_match_char (')') == MATCH_YES)
3467 goto ok;
3468
3469 m = gfc_match_char (',');
3470 if (m != MATCH_YES)
3471 {
3472 gfc_error ("Unexpected junk in formal argument list at %C");
3473 goto cleanup;
3474 }
3475 }
3476
3477ok:
3478 /* Check for duplicate symbols in the formal argument list. */
3479 if (head != NULL)
3480 {
3481 for (p = head; p->next; p = p->next)
3482 {
3483 if (p->sym == NULL)
3484 continue;
3485
3486 for (q = p->next; q; q = q->next)
3487 if (p->sym == q->sym)
3488 {
636dff67
SK
3489 gfc_error ("Duplicate symbol '%s' in formal argument list "
3490 "at %C", p->sym->name);
6de9cd9a
DN
3491
3492 m = MATCH_ERROR;
3493 goto cleanup;
3494 }
3495 }
3496 }
3497
66e4ab31
SK
3498 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3499 == FAILURE)
6de9cd9a
DN
3500 {
3501 m = MATCH_ERROR;
3502 goto cleanup;
3503 }
3504
3505 return MATCH_YES;
3506
3507cleanup:
3508 gfc_free_formal_arglist (head);
3509 return m;
3510}
3511
3512
3513/* Match a RESULT specification following a function declaration or
3514 ENTRY statement. Also matches the end-of-statement. */
3515
3516static match
66e4ab31 3517match_result (gfc_symbol *function, gfc_symbol **result)
6de9cd9a
DN
3518{
3519 char name[GFC_MAX_SYMBOL_LEN + 1];
3520 gfc_symbol *r;
3521 match m;
3522
3523 if (gfc_match (" result (") != MATCH_YES)
3524 return MATCH_NO;
3525
3526 m = gfc_match_name (name);
3527 if (m != MATCH_YES)
3528 return m;
3529
a8b3b0b6
CR
3530 /* Get the right paren, and that's it because there could be the
3531 bind(c) attribute after the result clause. */
3532 if (gfc_match_char(')') != MATCH_YES)
6de9cd9a 3533 {
a8b3b0b6 3534 /* TODO: should report the missing right paren here. */
6de9cd9a
DN
3535 return MATCH_ERROR;
3536 }
3537
3538 if (strcmp (function->name, name) == 0)
3539 {
636dff67 3540 gfc_error ("RESULT variable at %C must be different than function name");
6de9cd9a
DN
3541 return MATCH_ERROR;
3542 }
3543
3544 if (gfc_get_symbol (name, NULL, &r))
3545 return MATCH_ERROR;
3546
231b2fcc
TS
3547 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3548 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
6de9cd9a
DN
3549 return MATCH_ERROR;
3550
3551 *result = r;
3552
3553 return MATCH_YES;
3554}
3555
3556
a8b3b0b6
CR
3557/* Match a function suffix, which could be a combination of a result
3558 clause and BIND(C), either one, or neither. The draft does not
3559 require them to come in a specific order. */
3560
3561match
3562gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3563{
3564 match is_bind_c; /* Found bind(c). */
3565 match is_result; /* Found result clause. */
3566 match found_match; /* Status of whether we've found a good match. */
3567 int peek_char; /* Character we're going to peek at. */
3568
3569 /* Initialize to having found nothing. */
3570 found_match = MATCH_NO;
3571 is_bind_c = MATCH_NO;
3572 is_result = MATCH_NO;
3573
3574 /* Get the next char to narrow between result and bind(c). */
3575 gfc_gobble_whitespace ();
3576 peek_char = gfc_peek_char ();
3577
3578 switch (peek_char)
3579 {
3580 case 'r':
3581 /* Look for result clause. */
3582 is_result = match_result (sym, result);
3583 if (is_result == MATCH_YES)
3584 {
3585 /* Now see if there is a bind(c) after it. */
3586 is_bind_c = gfc_match_bind_c (sym);
3587 /* We've found the result clause and possibly bind(c). */
3588 found_match = MATCH_YES;
3589 }
3590 else
3591 /* This should only be MATCH_ERROR. */
3592 found_match = is_result;
3593 break;
3594 case 'b':
3595 /* Look for bind(c) first. */
3596 is_bind_c = gfc_match_bind_c (sym);
3597 if (is_bind_c == MATCH_YES)
3598 {
3599 /* Now see if a result clause followed it. */
3600 is_result = match_result (sym, result);
3601 found_match = MATCH_YES;
3602 }
3603 else
3604 {
3605 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3606 found_match = MATCH_ERROR;
3607 }
3608 break;
3609 default:
3610 gfc_error ("Unexpected junk after function declaration at %C");
3611 found_match = MATCH_ERROR;
3612 break;
3613 }
3614
a8b3b0b6
CR
3615 if (is_bind_c == MATCH_YES)
3616 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3617 == FAILURE)
3618 return MATCH_ERROR;
3619
3620 return found_match;
3621}
3622
3623
6de9cd9a
DN
3624/* Match a function declaration. */
3625
3626match
3627gfc_match_function_decl (void)
3628{
3629 char name[GFC_MAX_SYMBOL_LEN + 1];
3630 gfc_symbol *sym, *result;
3631 locus old_loc;
3632 match m;
a8b3b0b6
CR
3633 match suffix_match;
3634 match found_match; /* Status returned by match func. */
6de9cd9a
DN
3635
3636 if (gfc_current_state () != COMP_NONE
3637 && gfc_current_state () != COMP_INTERFACE
3638 && gfc_current_state () != COMP_CONTAINS)
3639 return MATCH_NO;
3640
3641 gfc_clear_ts (&current_ts);
3642
63645982 3643 old_loc = gfc_current_locus;
6de9cd9a
DN
3644
3645 m = match_prefix (&current_ts);
3646 if (m != MATCH_YES)
3647 {
63645982 3648 gfc_current_locus = old_loc;
6de9cd9a
DN
3649 return m;
3650 }
3651
3652 if (gfc_match ("function% %n", name) != MATCH_YES)
3653 {
63645982 3654 gfc_current_locus = old_loc;
6de9cd9a
DN
3655 return MATCH_NO;
3656 }
1a492601 3657 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
3658 return MATCH_ERROR;
3659 gfc_new_block = sym;
3660
3661 m = gfc_match_formal_arglist (sym, 0, 0);
3662 if (m == MATCH_NO)
2b9a33ae
TS
3663 {
3664 gfc_error ("Expected formal argument list in function "
636dff67 3665 "definition at %C");
2b9a33ae
TS
3666 m = MATCH_ERROR;
3667 goto cleanup;
3668 }
6de9cd9a
DN
3669 else if (m == MATCH_ERROR)
3670 goto cleanup;
3671
3672 result = NULL;
3673
a8b3b0b6
CR
3674 /* According to the draft, the bind(c) and result clause can
3675 come in either order after the formal_arg_list (i.e., either
3676 can be first, both can exist together or by themselves or neither
3677 one). Therefore, the match_result can't match the end of the
3678 string, and check for the bind(c) or result clause in either order. */
3679 found_match = gfc_match_eos ();
3680
3681 /* Make sure that it isn't already declared as BIND(C). If it is, it
3682 must have been marked BIND(C) with a BIND(C) attribute and that is
3683 not allowed for procedures. */
3684 if (sym->attr.is_bind_c == 1)
3685 {
3686 sym->attr.is_bind_c = 0;
3687 if (sym->old_symbol != NULL)
3688 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3689 "variables or common blocks",
3690 &(sym->old_symbol->declared_at));
3691 else
3692 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3693 "variables or common blocks", &gfc_current_locus);
6de9cd9a
DN
3694 }
3695
a8b3b0b6 3696 if (found_match != MATCH_YES)
6de9cd9a 3697 {
a8b3b0b6
CR
3698 /* If we haven't found the end-of-statement, look for a suffix. */
3699 suffix_match = gfc_match_suffix (sym, &result);
3700 if (suffix_match == MATCH_YES)
3701 /* Need to get the eos now. */
3702 found_match = gfc_match_eos ();
3703 else
3704 found_match = suffix_match;
6de9cd9a
DN
3705 }
3706
a8b3b0b6
CR
3707 if(found_match != MATCH_YES)
3708 m = MATCH_ERROR;
6de9cd9a
DN
3709 else
3710 {
a8b3b0b6
CR
3711 /* Make changes to the symbol. */
3712 m = MATCH_ERROR;
3713
3714 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3715 goto cleanup;
3716
3717 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3718 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3719 goto cleanup;
6de9cd9a 3720
a8b3b0b6
CR
3721 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3722 && !sym->attr.implicit_type)
3723 {
3724 gfc_error ("Function '%s' at %C already has a type of %s", name,
3725 gfc_basic_typename (sym->ts.type));
3726 goto cleanup;
3727 }
3728
3729 if (result == NULL)
3730 {
3731 sym->ts = current_ts;
3732 sym->result = sym;
3733 }
3734 else
3735 {
3736 result->ts = current_ts;
3737 sym->result = result;
3738 }
3739
3740 return MATCH_YES;
3741 }
6de9cd9a
DN
3742
3743cleanup:
63645982 3744 gfc_current_locus = old_loc;
6de9cd9a
DN
3745 return m;
3746}
3747
636dff67
SK
3748
3749/* This is mostly a copy of parse.c(add_global_procedure) but modified to
3750 pass the name of the entry, rather than the gfc_current_block name, and
3751 to return false upon finding an existing global entry. */
68ea355b
PT
3752
3753static bool
636dff67 3754add_global_entry (const char *name, int sub)
68ea355b
PT
3755{
3756 gfc_gsymbol *s;
3757
3758 s = gfc_get_gsymbol(name);
3759
3760 if (s->defined
636dff67
SK
3761 || (s->type != GSYM_UNKNOWN
3762 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
68ea355b
PT
3763 global_used(s, NULL);
3764 else
3765 {
3766 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3767 s->where = gfc_current_locus;
3768 s->defined = 1;
3769 return true;
3770 }
3771 return false;
3772}
6de9cd9a 3773
636dff67 3774
6de9cd9a
DN
3775/* Match an ENTRY statement. */
3776
3777match
3778gfc_match_entry (void)
3779{
3d79abbd
PB
3780 gfc_symbol *proc;
3781 gfc_symbol *result;
3782 gfc_symbol *entry;
6de9cd9a
DN
3783 char name[GFC_MAX_SYMBOL_LEN + 1];
3784 gfc_compile_state state;
3785 match m;
3d79abbd 3786 gfc_entry_list *el;
c96cfa49 3787 locus old_loc;
1a492601 3788 bool module_procedure;
6de9cd9a
DN
3789
3790 m = gfc_match_name (name);
3791 if (m != MATCH_YES)
3792 return m;
3793
3d79abbd 3794 state = gfc_current_state ();
4c93c95a 3795 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3d79abbd 3796 {
4c93c95a
FXC
3797 switch (state)
3798 {
3799 case COMP_PROGRAM:
3800 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3801 break;
3802 case COMP_MODULE:
3803 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3804 break;
3805 case COMP_BLOCK_DATA:
636dff67
SK
3806 gfc_error ("ENTRY statement at %C cannot appear within "
3807 "a BLOCK DATA");
4c93c95a
FXC
3808 break;
3809 case COMP_INTERFACE:
636dff67
SK
3810 gfc_error ("ENTRY statement at %C cannot appear within "
3811 "an INTERFACE");
4c93c95a
FXC
3812 break;
3813 case COMP_DERIVED:
636dff67
SK
3814 gfc_error ("ENTRY statement at %C cannot appear within "
3815 "a DERIVED TYPE block");
4c93c95a
FXC
3816 break;
3817 case COMP_IF:
636dff67
SK
3818 gfc_error ("ENTRY statement at %C cannot appear within "
3819 "an IF-THEN block");
4c93c95a
FXC
3820 break;
3821 case COMP_DO:
636dff67
SK
3822 gfc_error ("ENTRY statement at %C cannot appear within "
3823 "a DO block");
4c93c95a
FXC
3824 break;
3825 case COMP_SELECT:
636dff67
SK
3826 gfc_error ("ENTRY statement at %C cannot appear within "
3827 "a SELECT block");
4c93c95a
FXC
3828 break;
3829 case COMP_FORALL:
636dff67
SK
3830 gfc_error ("ENTRY statement at %C cannot appear within "
3831 "a FORALL block");
4c93c95a
FXC
3832 break;
3833 case COMP_WHERE:
636dff67
SK
3834 gfc_error ("ENTRY statement at %C cannot appear within "
3835 "a WHERE block");
4c93c95a
FXC
3836 break;
3837 case COMP_CONTAINS:
636dff67
SK
3838 gfc_error ("ENTRY statement at %C cannot appear within "
3839 "a contained subprogram");
4c93c95a
FXC
3840 break;
3841 default:
3842 gfc_internal_error ("gfc_match_entry(): Bad state");
3843 }
3d79abbd
PB
3844 return MATCH_ERROR;
3845 }
3846
1a492601 3847 module_procedure = gfc_current_ns->parent != NULL
636dff67
SK
3848 && gfc_current_ns->parent->proc_name
3849 && gfc_current_ns->parent->proc_name->attr.flavor
3850 == FL_MODULE;
1a492601 3851
3d79abbd
PB
3852 if (gfc_current_ns->parent != NULL
3853 && gfc_current_ns->parent->proc_name
1a492601 3854 && !module_procedure)
3d79abbd
PB
3855 {
3856 gfc_error("ENTRY statement at %C cannot appear in a "
3857 "contained procedure");
3858 return MATCH_ERROR;
3859 }
3860
1a492601
PT
3861 /* Module function entries need special care in get_proc_name
3862 because previous references within the function will have
3863 created symbols attached to the current namespace. */
3864 if (get_proc_name (name, &entry,
3865 gfc_current_ns->parent != NULL
3866 && module_procedure
3867 && gfc_current_ns->proc_name->attr.function))
6de9cd9a
DN
3868 return MATCH_ERROR;
3869
3d79abbd
PB
3870 proc = gfc_current_block ();
3871
3872 if (state == COMP_SUBROUTINE)
6de9cd9a 3873 {
231b2fcc 3874 /* An entry in a subroutine. */
68ea355b
PT
3875 if (!add_global_entry (name, 1))
3876 return MATCH_ERROR;
3877
6de9cd9a
DN
3878 m = gfc_match_formal_arglist (entry, 0, 1);
3879 if (m != MATCH_YES)
3880 return MATCH_ERROR;
3881
231b2fcc
TS
3882 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3883 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a 3884 return MATCH_ERROR;
3d79abbd
PB
3885 }
3886 else
3887 {
c96cfa49 3888 /* An entry in a function.
636dff67
SK
3889 We need to take special care because writing
3890 ENTRY f()
3891 as
3892 ENTRY f
3893 is allowed, whereas
3894 ENTRY f() RESULT (r)
3895 can't be written as
3896 ENTRY f RESULT (r). */
68ea355b
PT
3897 if (!add_global_entry (name, 0))
3898 return MATCH_ERROR;
3899
c96cfa49
TS
3900 old_loc = gfc_current_locus;
3901 if (gfc_match_eos () == MATCH_YES)
3902 {
3903 gfc_current_locus = old_loc;
3904 /* Match the empty argument list, and add the interface to
3905 the symbol. */
3906 m = gfc_match_formal_arglist (entry, 0, 1);
3907 }
3908 else
3909 m = gfc_match_formal_arglist (entry, 0, 0);
3910
6de9cd9a
DN
3911 if (m != MATCH_YES)
3912 return MATCH_ERROR;
3913
6de9cd9a
DN
3914 result = NULL;
3915
3916 if (gfc_match_eos () == MATCH_YES)
3917 {
231b2fcc
TS
3918 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3919 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
6de9cd9a
DN
3920 return MATCH_ERROR;
3921
d198b59a 3922 entry->result = entry;
6de9cd9a
DN
3923 }
3924 else
3925 {
3d79abbd 3926 m = match_result (proc, &result);
6de9cd9a
DN
3927 if (m == MATCH_NO)
3928 gfc_syntax_error (ST_ENTRY);
3929 if (m != MATCH_YES)
3930 return MATCH_ERROR;
3931
231b2fcc
TS
3932 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3933 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
636dff67
SK
3934 || gfc_add_function (&entry->attr, result->name, NULL)
3935 == FAILURE)
6de9cd9a 3936 return MATCH_ERROR;
d198b59a
JJ
3937
3938 entry->result = result;
6de9cd9a 3939 }
6de9cd9a
DN
3940 }
3941
3942 if (gfc_match_eos () != MATCH_YES)
3943 {
3944 gfc_syntax_error (ST_ENTRY);
3945 return MATCH_ERROR;
3946 }
3947
3d79abbd
PB
3948 entry->attr.recursive = proc->attr.recursive;
3949 entry->attr.elemental = proc->attr.elemental;
3950 entry->attr.pure = proc->attr.pure;
6de9cd9a 3951
3d79abbd
PB
3952 el = gfc_get_entry_list ();
3953 el->sym = entry;
3954 el->next = gfc_current_ns->entries;
3955 gfc_current_ns->entries = el;
3956 if (el->next)
3957 el->id = el->next->id + 1;
3958 else
3959 el->id = 1;
6de9cd9a 3960
3d79abbd
PB
3961 new_st.op = EXEC_ENTRY;
3962 new_st.ext.entry = el;
3963
3964 return MATCH_YES;
6de9cd9a
DN
3965}
3966
3967
3968/* Match a subroutine statement, including optional prefixes. */
3969
3970match
3971gfc_match_subroutine (void)
3972{
3973 char name[GFC_MAX_SYMBOL_LEN + 1];
3974 gfc_symbol *sym;
3975 match m;
a8b3b0b6
CR
3976 match is_bind_c;
3977 char peek_char;
6de9cd9a
DN
3978
3979 if (gfc_current_state () != COMP_NONE
3980 && gfc_current_state () != COMP_INTERFACE
3981 && gfc_current_state () != COMP_CONTAINS)
3982 return MATCH_NO;
3983
3984 m = match_prefix (NULL);
3985 if (m != MATCH_YES)
3986 return m;
3987
3988 m = gfc_match ("subroutine% %n", name);
3989 if (m != MATCH_YES)
3990 return m;
3991
1a492601 3992 if (get_proc_name (name, &sym, false))
6de9cd9a
DN
3993 return MATCH_ERROR;
3994 gfc_new_block = sym;
3995
a8b3b0b6
CR
3996 /* Check what next non-whitespace character is so we can tell if there
3997 where the required parens if we have a BIND(C). */
3998 gfc_gobble_whitespace ();
3999 peek_char = gfc_peek_char ();
4000
231b2fcc 4001 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4002 return MATCH_ERROR;
4003
4004 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4005 return MATCH_ERROR;
4006
a8b3b0b6
CR
4007 /* Make sure that it isn't already declared as BIND(C). If it is, it
4008 must have been marked BIND(C) with a BIND(C) attribute and that is
4009 not allowed for procedures. */
4010 if (sym->attr.is_bind_c == 1)
4011 {
4012 sym->attr.is_bind_c = 0;
4013 if (sym->old_symbol != NULL)
4014 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4015 "variables or common blocks",
4016 &(sym->old_symbol->declared_at));
4017 else
4018 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4019 "variables or common blocks", &gfc_current_locus);
4020 }
4021
4022 /* Here, we are just checking if it has the bind(c) attribute, and if
4023 so, then we need to make sure it's all correct. If it doesn't,
4024 we still need to continue matching the rest of the subroutine line. */
4025 is_bind_c = gfc_match_bind_c (sym);
4026 if (is_bind_c == MATCH_ERROR)
4027 {
4028 /* There was an attempt at the bind(c), but it was wrong. An
4029 error message should have been printed w/in the gfc_match_bind_c
4030 so here we'll just return the MATCH_ERROR. */
4031 return MATCH_ERROR;
4032 }
4033
4034 if (is_bind_c == MATCH_YES)
4035 {
4036 if (peek_char != '(')
4037 {
4038 gfc_error ("Missing required parentheses before BIND(C) at %C");
4039 return MATCH_ERROR;
4040 }
4041 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4042 == FAILURE)
4043 return MATCH_ERROR;
4044 }
4045
6de9cd9a
DN
4046 if (gfc_match_eos () != MATCH_YES)
4047 {
4048 gfc_syntax_error (ST_SUBROUTINE);
4049 return MATCH_ERROR;
4050 }
4051
4052 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4053 return MATCH_ERROR;
4054
4055 return MATCH_YES;
4056}
4057
4058
a8b3b0b6
CR
4059/* Match a BIND(C) specifier, with the optional 'name=' specifier if
4060 given, and set the binding label in either the given symbol (if not
86bf520d 4061 NULL), or in the current_ts. The symbol may be NULL because we may
a8b3b0b6
CR
4062 encounter the BIND(C) before the declaration itself. Return
4063 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4064 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4065 or MATCH_YES if the specifier was correct and the binding label and
4066 bind(c) fields were set correctly for the given symbol or the
4067 current_ts. */
4068
4069match
4070gfc_match_bind_c (gfc_symbol *sym)
4071{
4072 /* binding label, if exists */
4073 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4074 match double_quote;
4075 match single_quote;
4076 int has_name_equals = 0;
4077
4078 /* Initialize the flag that specifies whether we encountered a NAME=
4079 specifier or not. */
4080 has_name_equals = 0;
4081
4082 /* Init the first char to nil so we can catch if we don't have
4083 the label (name attr) or the symbol name yet. */
4084 binding_label[0] = '\0';
4085
4086 /* This much we have to be able to match, in this order, if
4087 there is a bind(c) label. */
4088 if (gfc_match (" bind ( c ") != MATCH_YES)
4089 return MATCH_NO;
4090
4091 /* Now see if there is a binding label, or if we've reached the
4092 end of the bind(c) attribute without one. */
4093 if (gfc_match_char (',') == MATCH_YES)
4094 {
4095 if (gfc_match (" name = ") != MATCH_YES)
4096 {
4097 gfc_error ("Syntax error in NAME= specifier for binding label "
4098 "at %C");
4099 /* should give an error message here */
4100 return MATCH_ERROR;
4101 }
4102
4103 has_name_equals = 1;
4104
4105 /* Get the opening quote. */
4106 double_quote = MATCH_YES;
4107 single_quote = MATCH_YES;
4108 double_quote = gfc_match_char ('"');
4109 if (double_quote != MATCH_YES)
4110 single_quote = gfc_match_char ('\'');
4111 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4112 {
4113 gfc_error ("Syntax error in NAME= specifier for binding label "
4114 "at %C");
4115 return MATCH_ERROR;
4116 }
4117
4118 /* Grab the binding label, using functions that will not lower
4119 case the names automatically. */
4120 if (gfc_match_name_C (binding_label) != MATCH_YES)
4121 return MATCH_ERROR;
4122
4123 /* Get the closing quotation. */
4124 if (double_quote == MATCH_YES)
4125 {
4126 if (gfc_match_char ('"') != MATCH_YES)
4127 {
4128 gfc_error ("Missing closing quote '\"' for binding label at %C");
4129 /* User started string with '"' so looked to match it. */
4130 return MATCH_ERROR;
4131 }
4132 }
4133 else
4134 {
4135 if (gfc_match_char ('\'') != MATCH_YES)
4136 {
4137 gfc_error ("Missing closing quote '\'' for binding label at %C");
4138 /* User started string with "'" char. */
4139 return MATCH_ERROR;
4140 }
4141 }
4142 }
4143
4144 /* Get the required right paren. */
4145 if (gfc_match_char (')') != MATCH_YES)
4146 {
4147 gfc_error ("Missing closing paren for binding label at %C");
4148 return MATCH_ERROR;
4149 }
4150
4151 /* Save the binding label to the symbol. If sym is null, we're
4152 probably matching the typespec attributes of a declaration and
4153 haven't gotten the name yet, and therefore, no symbol yet. */
4154 if (binding_label[0] != '\0')
4155 {
4156 if (sym != NULL)
4157 {
4158 strncpy (sym->binding_label, binding_label,
4159 strlen (binding_label)+1);
4160 }
4161 else
4162 strncpy (curr_binding_label, binding_label,
4163 strlen (binding_label) + 1);
4164 }
4165 else
4166 {
4167 /* No binding label, but if symbol isn't null, we
4168 can set the label for it here. */
4169 /* TODO: If the name= was given and no binding label (name=""), we simply
4170 will let fortran mangle the symbol name as it usually would.
4171 However, this could still let C call it if the user looked up the
4172 symbol in the object file. Should the name set during mangling in
4173 trans-decl.c be marked with characters that are invalid for C to
4174 prevent this? */
4175 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4176 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4177 }
4178
4179 return MATCH_YES;
4180}
4181
4182
1f2959f0 4183/* Return nonzero if we're currently compiling a contained procedure. */
ddc9ce91
TS
4184
4185static int
4186contained_procedure (void)
4187{
4188 gfc_state_data *s;
4189
4190 for (s=gfc_state_stack; s; s=s->previous)
4191 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
636dff67 4192 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
ddc9ce91
TS
4193 return 1;
4194
4195 return 0;
4196}
4197
d51347f9 4198/* Set the kind of each enumerator. The kind is selected such that it is
25d8f0a2
TS
4199 interoperable with the corresponding C enumeration type, making
4200 sure that -fshort-enums is honored. */
4201
4202static void
4203set_enum_kind(void)
4204{
4205 enumerator_history *current_history = NULL;
4206 int kind;
4207 int i;
4208
4209 if (max_enum == NULL || enum_history == NULL)
4210 return;
4211
4212 if (!gfc_option.fshort_enums)
d51347f9
TB
4213 return;
4214
25d8f0a2
TS
4215 i = 0;
4216 do
4217 {
4218 kind = gfc_integer_kinds[i++].kind;
4219 }
d51347f9 4220 while (kind < gfc_c_int_kind
25d8f0a2
TS
4221 && gfc_check_integer_range (max_enum->initializer->value.integer,
4222 kind) != ARITH_OK);
4223
4224 current_history = enum_history;
4225 while (current_history != NULL)
4226 {
4227 current_history->sym->ts.kind = kind;
4228 current_history = current_history->next;
4229 }
4230}
4231
636dff67 4232
6de9cd9a
DN
4233/* Match any of the various end-block statements. Returns the type of
4234 END to the caller. The END INTERFACE, END IF, END DO and END
4235 SELECT statements cannot be replaced by a single END statement. */
4236
4237match
636dff67 4238gfc_match_end (gfc_statement *st)
6de9cd9a
DN
4239{
4240 char name[GFC_MAX_SYMBOL_LEN + 1];
4241 gfc_compile_state state;
4242 locus old_loc;
4243 const char *block_name;
4244 const char *target;
ddc9ce91 4245 int eos_ok;
6de9cd9a
DN
4246 match m;
4247
63645982 4248 old_loc = gfc_current_locus;
6de9cd9a
DN
4249 if (gfc_match ("end") != MATCH_YES)
4250 return MATCH_NO;
4251
4252 state = gfc_current_state ();
636dff67
SK
4253 block_name = gfc_current_block () == NULL
4254 ? NULL : gfc_current_block ()->name;
6de9cd9a
DN
4255
4256 if (state == COMP_CONTAINS)
4257 {
4258 state = gfc_state_stack->previous->state;
636dff67
SK
4259 block_name = gfc_state_stack->previous->sym == NULL
4260 ? NULL : gfc_state_stack->previous->sym->name;
6de9cd9a
DN
4261 }
4262
4263 switch (state)
4264 {
4265 case COMP_NONE:
4266 case COMP_PROGRAM:
4267 *st = ST_END_PROGRAM;
4268 target = " program";
ddc9ce91 4269 eos_ok = 1;
6de9cd9a
DN
4270 break;
4271
4272 case COMP_SUBROUTINE:
4273 *st = ST_END_SUBROUTINE;
4274 target = " subroutine";
ddc9ce91 4275 eos_ok = !contained_procedure ();
6de9cd9a
DN
4276 break;
4277
4278 case COMP_FUNCTION:
4279 *st = ST_END_FUNCTION;
4280 target = " function";
ddc9ce91 4281 eos_ok = !contained_procedure ();
6de9cd9a
DN
4282 break;
4283
4284 case COMP_BLOCK_DATA:
4285 *st = ST_END_BLOCK_DATA;
4286 target = " block data";
ddc9ce91 4287 eos_ok = 1;
6de9cd9a
DN
4288 break;
4289
4290 case COMP_MODULE:
4291 *st = ST_END_MODULE;
4292 target = " module";
ddc9ce91 4293 eos_ok = 1;
6de9cd9a
DN
4294 break;
4295
4296 case COMP_INTERFACE:
4297 *st = ST_END_INTERFACE;
4298 target = " interface";
ddc9ce91 4299 eos_ok = 0;
6de9cd9a
DN
4300 break;
4301
4302 case COMP_DERIVED:
4303 *st = ST_END_TYPE;
4304 target = " type";
ddc9ce91 4305 eos_ok = 0;
6de9cd9a
DN
4306 break;
4307
4308 case COMP_IF:
4309 *st = ST_ENDIF;
4310 target = " if";
ddc9ce91 4311 eos_ok = 0;
6de9cd9a
DN
4312 break;
4313
4314 case COMP_DO:
4315 *st = ST_ENDDO;
4316 target = " do";
ddc9ce91 4317 eos_ok = 0;
6de9cd9a
DN
4318 break;
4319
4320 case COMP_SELECT:
4321 *st = ST_END_SELECT;
4322 target = " select";
ddc9ce91 4323 eos_ok = 0;
6de9cd9a
DN
4324 break;
4325
4326 case COMP_FORALL:
4327 *st = ST_END_FORALL;
4328 target = " forall";
ddc9ce91 4329 eos_ok = 0;
6de9cd9a
DN
4330 break;
4331
4332 case COMP_WHERE:
4333 *st = ST_END_WHERE;
4334 target = " where";
ddc9ce91 4335 eos_ok = 0;
6de9cd9a
DN
4336 break;
4337
25d8f0a2
TS
4338 case COMP_ENUM:
4339 *st = ST_END_ENUM;
4340 target = " enum";
4341 eos_ok = 0;
4342 last_initializer = NULL;
4343 set_enum_kind ();
4344 gfc_free_enum_history ();
4345 break;
4346
6de9cd9a
DN
4347 default:
4348 gfc_error ("Unexpected END statement at %C");
4349 goto cleanup;
4350 }
4351
4352 if (gfc_match_eos () == MATCH_YES)
4353 {
ddc9ce91 4354 if (!eos_ok)
6de9cd9a 4355 {
66e4ab31 4356 /* We would have required END [something]. */
59ce85b5
TS
4357 gfc_error ("%s statement expected at %L",
4358 gfc_ascii_statement (*st), &old_loc);
6de9cd9a
DN
4359 goto cleanup;
4360 }
4361
4362 return MATCH_YES;
4363 }
4364
4365 /* Verify that we've got the sort of end-block that we're expecting. */
4366 if (gfc_match (target) != MATCH_YES)
4367 {
4368 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4369 goto cleanup;
4370 }
4371
4372 /* If we're at the end, make sure a block name wasn't required. */
4373 if (gfc_match_eos () == MATCH_YES)
4374 {
4375
690af379
TS
4376 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4377 && *st != ST_END_FORALL && *st != ST_END_WHERE)
6de9cd9a
DN
4378 return MATCH_YES;
4379
4380 if (gfc_current_block () == NULL)
4381 return MATCH_YES;
4382
4383 gfc_error ("Expected block name of '%s' in %s statement at %C",
4384 block_name, gfc_ascii_statement (*st));
4385
4386 return MATCH_ERROR;
4387 }
4388
4389 /* END INTERFACE has a special handler for its several possible endings. */
4390 if (*st == ST_END_INTERFACE)
4391 return gfc_match_end_interface ();
4392
66e4ab31
SK
4393 /* We haven't hit the end of statement, so what is left must be an
4394 end-name. */
6de9cd9a
DN
4395 m = gfc_match_space ();
4396 if (m == MATCH_YES)
4397 m = gfc_match_name (name);
4398
4399 if (m == MATCH_NO)
4400 gfc_error ("Expected terminating name at %C");
4401 if (m != MATCH_YES)
4402 goto cleanup;
4403
4404 if (block_name == NULL)
4405 goto syntax;
4406
4407 if (strcmp (name, block_name) != 0)
4408 {
4409 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4410 gfc_ascii_statement (*st));
4411 goto cleanup;
4412 }
4413
4414 if (gfc_match_eos () == MATCH_YES)
4415 return MATCH_YES;
4416
4417syntax:
4418 gfc_syntax_error (*st);
4419
4420cleanup:
63645982 4421 gfc_current_locus = old_loc;
6de9cd9a
DN
4422 return MATCH_ERROR;
4423}
4424
4425
4426
4427/***************** Attribute declaration statements ****************/
4428
4429/* Set the attribute of a single variable. */
4430
4431static match
4432attr_decl1 (void)
4433{
4434 char name[GFC_MAX_SYMBOL_LEN + 1];
4435 gfc_array_spec *as;
4436 gfc_symbol *sym;
4437 locus var_locus;
4438 match m;
4439
4440 as = NULL;
4441
4442 m = gfc_match_name (name);
4443 if (m != MATCH_YES)
4444 goto cleanup;
4445
4446 if (find_special (name, &sym))
4447 return MATCH_ERROR;
4448
63645982 4449 var_locus = gfc_current_locus;
6de9cd9a
DN
4450
4451 /* Deal with possible array specification for certain attributes. */
4452 if (current_attr.dimension
4453 || current_attr.allocatable
4454 || current_attr.pointer
4455 || current_attr.target)
4456 {
4457 m = gfc_match_array_spec (&as);
4458 if (m == MATCH_ERROR)
4459 goto cleanup;
4460
4461 if (current_attr.dimension && m == MATCH_NO)
4462 {
636dff67
SK
4463 gfc_error ("Missing array specification at %L in DIMENSION "
4464 "statement", &var_locus);
6de9cd9a
DN
4465 m = MATCH_ERROR;
4466 goto cleanup;
4467 }
4468
4469 if ((current_attr.allocatable || current_attr.pointer)
4470 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4471 {
636dff67 4472 gfc_error ("Array specification must be deferred at %L", &var_locus);
6de9cd9a
DN
4473 m = MATCH_ERROR;
4474 goto cleanup;
4475 }
4476 }
4477
636dff67
SK
4478 /* Update symbol table. DIMENSION attribute is set
4479 in gfc_set_array_spec(). */
6de9cd9a
DN
4480 if (current_attr.dimension == 0
4481 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4482 {
4483 m = MATCH_ERROR;
4484 goto cleanup;
4485 }
4486
4487 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4488 {
4489 m = MATCH_ERROR;
4490 goto cleanup;
4491 }
d51347f9 4492
83d890b9
AL
4493 if (sym->attr.cray_pointee && sym->as != NULL)
4494 {
4495 /* Fix the array spec. */
4496 m = gfc_mod_pointee_as (sym->as);
4497 if (m == MATCH_ERROR)
4498 goto cleanup;
4499 }
6de9cd9a 4500
7114edca 4501 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
1902704e
PT
4502 {
4503 m = MATCH_ERROR;
4504 goto cleanup;
4505 }
4506
6de9cd9a
DN
4507 if ((current_attr.external || current_attr.intrinsic)
4508 && sym->attr.flavor != FL_PROCEDURE
231b2fcc 4509 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6de9cd9a
DN
4510 {
4511 m = MATCH_ERROR;
4512 goto cleanup;
4513 }
4514
4515 return MATCH_YES;
4516
4517cleanup:
4518 gfc_free_array_spec (as);
4519 return m;
4520}
4521
4522
4523/* Generic attribute declaration subroutine. Used for attributes that
4524 just have a list of names. */
4525
4526static match
4527attr_decl (void)
4528{
4529 match m;
4530
4531 /* Gobble the optional double colon, by simply ignoring the result
4532 of gfc_match(). */
4533 gfc_match (" ::");
4534
4535 for (;;)
4536 {
4537 m = attr_decl1 ();
4538 if (m != MATCH_YES)
4539 break;
4540
4541 if (gfc_match_eos () == MATCH_YES)
4542 {
4543 m = MATCH_YES;
4544 break;
4545 }
4546
4547 if (gfc_match_char (',') != MATCH_YES)
4548 {
4549 gfc_error ("Unexpected character in variable list at %C");
4550 m = MATCH_ERROR;
4551 break;
4552 }
4553 }
4554
4555 return m;
4556}
4557
4558
83d890b9
AL
4559/* This routine matches Cray Pointer declarations of the form:
4560 pointer ( <pointer>, <pointee> )
4561 or
d51347f9
TB
4562 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4563 The pointer, if already declared, should be an integer. Otherwise, we
83d890b9
AL
4564 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4565 be either a scalar, or an array declaration. No space is allocated for
d51347f9 4566 the pointee. For the statement
83d890b9
AL
4567 pointer (ipt, ar(10))
4568 any subsequent uses of ar will be translated (in C-notation) as
d51347f9 4569 ar(i) => ((<type> *) ipt)(i)
b122dc6a 4570 After gimplification, pointee variable will disappear in the code. */
83d890b9
AL
4571
4572static match
4573cray_pointer_decl (void)
4574{
4575 match m;
4576 gfc_array_spec *as;
4577 gfc_symbol *cptr; /* Pointer symbol. */
4578 gfc_symbol *cpte; /* Pointee symbol. */
4579 locus var_locus;
4580 bool done = false;
4581
4582 while (!done)
4583 {
4584 if (gfc_match_char ('(') != MATCH_YES)
4585 {
4586 gfc_error ("Expected '(' at %C");
d51347f9 4587 return MATCH_ERROR;
83d890b9 4588 }
d51347f9 4589
83d890b9
AL
4590 /* Match pointer. */
4591 var_locus = gfc_current_locus;
4592 gfc_clear_attr (&current_attr);
4593 gfc_add_cray_pointer (&current_attr, &var_locus);
4594 current_ts.type = BT_INTEGER;
4595 current_ts.kind = gfc_index_integer_kind;
4596
d51347f9 4597 m = gfc_match_symbol (&cptr, 0);
83d890b9
AL
4598 if (m != MATCH_YES)
4599 {
4600 gfc_error ("Expected variable name at %C");
4601 return m;
4602 }
d51347f9 4603
83d890b9
AL
4604 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
4605 return MATCH_ERROR;
4606
d51347f9 4607 gfc_set_sym_referenced (cptr);
83d890b9
AL
4608
4609 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
4610 {
4611 cptr->ts.type = BT_INTEGER;
d51347f9 4612 cptr->ts.kind = gfc_index_integer_kind;
83d890b9
AL
4613 }
4614 else if (cptr->ts.type != BT_INTEGER)
4615 {
e25a0da3 4616 gfc_error ("Cray pointer at %C must be an integer");
83d890b9
AL
4617 return MATCH_ERROR;
4618 }
4619 else if (cptr->ts.kind < gfc_index_integer_kind)
4620 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
e25a0da3 4621 " memory addresses require %d bytes",
636dff67 4622 cptr->ts.kind, gfc_index_integer_kind);
83d890b9
AL
4623
4624 if (gfc_match_char (',') != MATCH_YES)
4625 {
4626 gfc_error ("Expected \",\" at %C");
d51347f9 4627 return MATCH_ERROR;
83d890b9
AL
4628 }
4629
d51347f9 4630 /* Match Pointee. */
83d890b9
AL
4631 var_locus = gfc_current_locus;
4632 gfc_clear_attr (&current_attr);
4633 gfc_add_cray_pointee (&current_attr, &var_locus);
4634 current_ts.type = BT_UNKNOWN;
4635 current_ts.kind = 0;
4636
4637 m = gfc_match_symbol (&cpte, 0);
4638 if (m != MATCH_YES)
4639 {
4640 gfc_error ("Expected variable name at %C");
4641 return m;
4642 }
d51347f9 4643
83d890b9
AL
4644 /* Check for an optional array spec. */
4645 m = gfc_match_array_spec (&as);
4646 if (m == MATCH_ERROR)
4647 {
4648 gfc_free_array_spec (as);
4649 return m;
4650 }
4651 else if (m == MATCH_NO)
4652 {
4653 gfc_free_array_spec (as);
4654 as = NULL;
4655 }
4656
4657 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
4658 return MATCH_ERROR;
4659
4660 gfc_set_sym_referenced (cpte);
4661
4662 if (cpte->as == NULL)
4663 {
4664 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
4665 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4666 }
4667 else if (as != NULL)
4668 {
e25a0da3 4669 gfc_error ("Duplicate array spec for Cray pointee at %C");
83d890b9
AL
4670 gfc_free_array_spec (as);
4671 return MATCH_ERROR;
4672 }
4673
4674 as = NULL;
4675
4676 if (cpte->as != NULL)
4677 {
4678 /* Fix array spec. */
4679 m = gfc_mod_pointee_as (cpte->as);
4680 if (m == MATCH_ERROR)
4681 return m;
4682 }
4683
4684 /* Point the Pointee at the Pointer. */
b122dc6a 4685 cpte->cp_pointer = cptr;
83d890b9
AL
4686
4687 if (gfc_match_char (')') != MATCH_YES)
4688 {
4689 gfc_error ("Expected \")\" at %C");
4690 return MATCH_ERROR;
4691 }
4692 m = gfc_match_char (',');
4693 if (m != MATCH_YES)
4694 done = true; /* Stop searching for more declarations. */
4695
4696 }
4697
4698 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
4699 || gfc_match_eos () != MATCH_YES)
4700 {
4701 gfc_error ("Expected \",\" or end of statement at %C");
4702 return MATCH_ERROR;
4703 }
4704 return MATCH_YES;
4705}
4706
4707
6de9cd9a
DN
4708match
4709gfc_match_external (void)
4710{
4711
4712 gfc_clear_attr (&current_attr);
1902704e 4713 current_attr.external = 1;
6de9cd9a
DN
4714
4715 return attr_decl ();
4716}
4717
4718
6de9cd9a
DN
4719match
4720gfc_match_intent (void)
4721{
4722 sym_intent intent;
4723
4724 intent = match_intent_spec ();
4725 if (intent == INTENT_UNKNOWN)
4726 return MATCH_ERROR;
4727
4728 gfc_clear_attr (&current_attr);
1902704e 4729 current_attr.intent = intent;
6de9cd9a
DN
4730
4731 return attr_decl ();
4732}
4733
4734
4735match
4736gfc_match_intrinsic (void)
4737{
4738
4739 gfc_clear_attr (&current_attr);
1902704e 4740 current_attr.intrinsic = 1;
6de9cd9a
DN
4741
4742 return attr_decl ();
4743}
4744
4745
4746match
4747gfc_match_optional (void)
4748{
4749
4750 gfc_clear_attr (&current_attr);
1902704e 4751 current_attr.optional = 1;
6de9cd9a
DN
4752
4753 return attr_decl ();
4754}
4755
4756
4757match
4758gfc_match_pointer (void)
4759{
83d890b9
AL
4760 gfc_gobble_whitespace ();
4761 if (gfc_peek_char () == '(')
4762 {
4763 if (!gfc_option.flag_cray_pointer)
4764 {
636dff67
SK
4765 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4766 "flag");
83d890b9
AL
4767 return MATCH_ERROR;
4768 }
4769 return cray_pointer_decl ();
4770 }
4771 else
4772 {
4773 gfc_clear_attr (&current_attr);
1902704e 4774 current_attr.pointer = 1;
83d890b9
AL
4775
4776 return attr_decl ();
4777 }
6de9cd9a
DN
4778}
4779
4780
4781match
4782gfc_match_allocatable (void)
4783{
6de9cd9a 4784 gfc_clear_attr (&current_attr);
1902704e 4785 current_attr.allocatable = 1;
6de9cd9a
DN
4786
4787 return attr_decl ();
4788}
4789
4790
4791match
4792gfc_match_dimension (void)
4793{
6de9cd9a 4794 gfc_clear_attr (&current_attr);
1902704e 4795 current_attr.dimension = 1;
6de9cd9a
DN
4796
4797 return attr_decl ();
4798}
4799
4800
4801match
4802gfc_match_target (void)
4803{
6de9cd9a 4804 gfc_clear_attr (&current_attr);
1902704e 4805 current_attr.target = 1;
6de9cd9a
DN
4806
4807 return attr_decl ();
4808}
4809
4810
4811/* Match the list of entities being specified in a PUBLIC or PRIVATE
4812 statement. */
4813
4814static match
4815access_attr_decl (gfc_statement st)
4816{
4817 char name[GFC_MAX_SYMBOL_LEN + 1];
4818 interface_type type;
4819 gfc_user_op *uop;
4820 gfc_symbol *sym;
4821 gfc_intrinsic_op operator;
4822 match m;
4823
4824 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4825 goto done;
4826
4827 for (;;)
4828 {
4829 m = gfc_match_generic_spec (&type, name, &operator);
4830 if (m == MATCH_NO)
4831 goto syntax;
4832 if (m == MATCH_ERROR)
4833 return MATCH_ERROR;
4834
4835 switch (type)
4836 {
4837 case INTERFACE_NAMELESS:
4838 goto syntax;
4839
4840 case INTERFACE_GENERIC:
4841 if (gfc_get_symbol (name, NULL, &sym))
4842 goto done;
4843
636dff67
SK
4844 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
4845 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
231b2fcc 4846 sym->name, NULL) == FAILURE)
6de9cd9a
DN
4847 return MATCH_ERROR;
4848
4849 break;
4850
4851 case INTERFACE_INTRINSIC_OP:
4852 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
4853 {
4854 gfc_current_ns->operator_access[operator] =
4855 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4856 }
4857 else
4858 {
4859 gfc_error ("Access specification of the %s operator at %C has "
4860 "already been specified", gfc_op2string (operator));
4861 goto done;
4862 }
4863
4864 break;
4865
4866 case INTERFACE_USER_OP:
4867 uop = gfc_get_uop (name);
4868
4869 if (uop->access == ACCESS_UNKNOWN)
4870 {
636dff67
SK
4871 uop->access = (st == ST_PUBLIC)
4872 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6de9cd9a
DN
4873 }
4874 else
4875 {
636dff67
SK
4876 gfc_error ("Access specification of the .%s. operator at %C "
4877 "has already been specified", sym->name);
6de9cd9a
DN
4878 goto done;
4879 }
4880
4881 break;
4882 }
4883
4884 if (gfc_match_char (',') == MATCH_NO)
4885 break;
4886 }
4887
4888 if (gfc_match_eos () != MATCH_YES)
4889 goto syntax;
4890 return MATCH_YES;
4891
4892syntax:
4893 gfc_syntax_error (st);
4894
4895done:
4896 return MATCH_ERROR;
4897}
4898
4899
ee7e677f
TB
4900match
4901gfc_match_protected (void)
4902{
4903 gfc_symbol *sym;
4904 match m;
4905
4906 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4907 {
4908 gfc_error ("PROTECTED at %C only allowed in specification "
4909 "part of a module");
4910 return MATCH_ERROR;
4911
4912 }
4913
636dff67 4914 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
ee7e677f
TB
4915 == FAILURE)
4916 return MATCH_ERROR;
4917
4918 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4919 {
4920 return MATCH_ERROR;
4921 }
4922
4923 if (gfc_match_eos () == MATCH_YES)
4924 goto syntax;
4925
4926 for(;;)
4927 {
4928 m = gfc_match_symbol (&sym, 0);
4929 switch (m)
4930 {
4931 case MATCH_YES:
636dff67
SK
4932 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
4933 == FAILURE)
ee7e677f
TB
4934 return MATCH_ERROR;
4935 goto next_item;
4936
4937 case MATCH_NO:
4938 break;
4939
4940 case MATCH_ERROR:
4941 return MATCH_ERROR;
4942 }
4943
4944 next_item:
4945 if (gfc_match_eos () == MATCH_YES)
4946 break;
4947 if (gfc_match_char (',') != MATCH_YES)
4948 goto syntax;
4949 }
4950
4951 return MATCH_YES;
4952
4953syntax:
4954 gfc_error ("Syntax error in PROTECTED statement at %C");
4955 return MATCH_ERROR;
4956}
4957
4958
86bf520d 4959/* The PRIVATE statement is a bit weird in that it can be an attribute
6de9cd9a
DN
4960 declaration, but also works as a standlone statement inside of a
4961 type declaration or a module. */
4962
4963match
636dff67 4964gfc_match_private (gfc_statement *st)
6de9cd9a
DN
4965{
4966
4967 if (gfc_match ("private") != MATCH_YES)
4968 return MATCH_NO;
4969
d51347f9
TB
4970 if (gfc_current_state () != COMP_MODULE
4971 && (gfc_current_state () != COMP_DERIVED
4972 || !gfc_state_stack->previous
4973 || gfc_state_stack->previous->state != COMP_MODULE))
4974 {
4975 gfc_error ("PRIVATE statement at %C is only allowed in the "
4976 "specification part of a module");
4977 return MATCH_ERROR;
4978 }
4979
6de9cd9a
DN
4980 if (gfc_current_state () == COMP_DERIVED)
4981 {
4982 if (gfc_match_eos () == MATCH_YES)
4983 {
4984 *st = ST_PRIVATE;
4985 return MATCH_YES;
4986 }
4987
4988 gfc_syntax_error (ST_PRIVATE);
4989 return MATCH_ERROR;
4990 }
4991
4992 if (gfc_match_eos () == MATCH_YES)
4993 {
4994 *st = ST_PRIVATE;
4995 return MATCH_YES;
4996 }
4997
4998 *st = ST_ATTR_DECL;
4999 return access_attr_decl (ST_PRIVATE);
5000}
5001
5002
5003match
636dff67 5004gfc_match_public (gfc_statement *st)
6de9cd9a
DN
5005{
5006
5007 if (gfc_match ("public") != MATCH_YES)
5008 return MATCH_NO;
5009
d51347f9
TB
5010 if (gfc_current_state () != COMP_MODULE)
5011 {
5012 gfc_error ("PUBLIC statement at %C is only allowed in the "
5013 "specification part of a module");
5014 return MATCH_ERROR;
5015 }
5016
6de9cd9a
DN
5017 if (gfc_match_eos () == MATCH_YES)
5018 {
5019 *st = ST_PUBLIC;
5020 return MATCH_YES;
5021 }
5022
5023 *st = ST_ATTR_DECL;
5024 return access_attr_decl (ST_PUBLIC);
5025}
5026
5027
5028/* Workhorse for gfc_match_parameter. */
5029
5030static match
5031do_parm (void)
5032{
5033 gfc_symbol *sym;
5034 gfc_expr *init;
5035 match m;
5036
5037 m = gfc_match_symbol (&sym, 0);
5038 if (m == MATCH_NO)
5039 gfc_error ("Expected variable name at %C in PARAMETER statement");
5040
5041 if (m != MATCH_YES)
5042 return m;
5043
5044 if (gfc_match_char ('=') == MATCH_NO)
5045 {
5046 gfc_error ("Expected = sign in PARAMETER statement at %C");
5047 return MATCH_ERROR;
5048 }
5049
5050 m = gfc_match_init_expr (&init);
5051 if (m == MATCH_NO)
5052 gfc_error ("Expected expression at %C in PARAMETER statement");
5053 if (m != MATCH_YES)
5054 return m;
5055
5056 if (sym->ts.type == BT_UNKNOWN
5057 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5058 {
5059 m = MATCH_ERROR;
5060 goto cleanup;
5061 }
5062
5063 if (gfc_check_assign_symbol (sym, init) == FAILURE
231b2fcc 5064 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5065 {
5066 m = MATCH_ERROR;
5067 goto cleanup;
5068 }
5069
7e2eba4b
DE
5070 if (sym->ts.type == BT_CHARACTER
5071 && sym->ts.cl != NULL
5072 && sym->ts.cl->length != NULL
5073 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5074 && init->expr_type == EXPR_CONSTANT
5075 && init->ts.type == BT_CHARACTER
5076 && init->ts.kind == 1)
5077 gfc_set_constant_character_len (
2220652d 5078 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
7e2eba4b 5079
6de9cd9a
DN
5080 sym->value = init;
5081 return MATCH_YES;
5082
5083cleanup:
5084 gfc_free_expr (init);
5085 return m;
5086}
5087
5088
5089/* Match a parameter statement, with the weird syntax that these have. */
5090
5091match
5092gfc_match_parameter (void)
5093{
5094 match m;
5095
5096 if (gfc_match_char ('(') == MATCH_NO)
5097 return MATCH_NO;
5098
5099 for (;;)
5100 {
5101 m = do_parm ();
5102 if (m != MATCH_YES)
5103 break;
5104
5105 if (gfc_match (" )%t") == MATCH_YES)
5106 break;
5107
5108 if (gfc_match_char (',') != MATCH_YES)
5109 {
5110 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5111 m = MATCH_ERROR;
5112 break;
5113 }
5114 }
5115
5116 return m;
5117}
5118
5119
5120/* Save statements have a special syntax. */
5121
5122match
5123gfc_match_save (void)
5124{
9056bd70
TS
5125 char n[GFC_MAX_SYMBOL_LEN+1];
5126 gfc_common_head *c;
6de9cd9a
DN
5127 gfc_symbol *sym;
5128 match m;
5129
5130 if (gfc_match_eos () == MATCH_YES)
5131 {
5132 if (gfc_current_ns->seen_save)
5133 {
636dff67
SK
5134 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5135 "follows previous SAVE statement")
09e87839
AL
5136 == FAILURE)
5137 return MATCH_ERROR;
6de9cd9a
DN
5138 }
5139
5140 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5141 return MATCH_YES;
5142 }
5143
5144 if (gfc_current_ns->save_all)
5145 {
636dff67
SK
5146 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5147 "blanket SAVE statement")
09e87839
AL
5148 == FAILURE)
5149 return MATCH_ERROR;
6de9cd9a
DN
5150 }
5151
5152 gfc_match (" ::");
5153
5154 for (;;)
5155 {
5156 m = gfc_match_symbol (&sym, 0);
5157 switch (m)
5158 {
5159 case MATCH_YES:
636dff67
SK
5160 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5161 == FAILURE)
6de9cd9a
DN
5162 return MATCH_ERROR;
5163 goto next_item;
5164
5165 case MATCH_NO:
5166 break;
5167
5168 case MATCH_ERROR:
5169 return MATCH_ERROR;
5170 }
5171
9056bd70 5172 m = gfc_match (" / %n /", &n);
6de9cd9a
DN
5173 if (m == MATCH_ERROR)
5174 return MATCH_ERROR;
5175 if (m == MATCH_NO)
5176 goto syntax;
5177
53814b8f 5178 c = gfc_get_common (n, 0);
9056bd70
TS
5179 c->saved = 1;
5180
6de9cd9a
DN
5181 gfc_current_ns->seen_save = 1;
5182
5183 next_item:
5184 if (gfc_match_eos () == MATCH_YES)
5185 break;
5186 if (gfc_match_char (',') != MATCH_YES)
5187 goto syntax;
5188 }
5189
5190 return MATCH_YES;
5191
5192syntax:
5193 gfc_error ("Syntax error in SAVE statement at %C");
5194 return MATCH_ERROR;
5195}
5196
5197
06469efd
PT
5198match
5199gfc_match_value (void)
5200{
5201 gfc_symbol *sym;
5202 match m;
5203
636dff67 5204 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
06469efd
PT
5205 == FAILURE)
5206 return MATCH_ERROR;
5207
5208 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5209 {
5210 return MATCH_ERROR;
5211 }
5212
5213 if (gfc_match_eos () == MATCH_YES)
5214 goto syntax;
5215
5216 for(;;)
5217 {
5218 m = gfc_match_symbol (&sym, 0);
5219 switch (m)
5220 {
5221 case MATCH_YES:
636dff67
SK
5222 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5223 == FAILURE)
06469efd
PT
5224 return MATCH_ERROR;
5225 goto next_item;
5226
5227 case MATCH_NO:
5228 break;
5229
5230 case MATCH_ERROR:
5231 return MATCH_ERROR;
5232 }
5233
5234 next_item:
5235 if (gfc_match_eos () == MATCH_YES)
5236 break;
5237 if (gfc_match_char (',') != MATCH_YES)
5238 goto syntax;
5239 }
5240
5241 return MATCH_YES;
5242
5243syntax:
5244 gfc_error ("Syntax error in VALUE statement at %C");
5245 return MATCH_ERROR;
5246}
5247
66e4ab31 5248
775e6c3a
TB
5249match
5250gfc_match_volatile (void)
5251{
5252 gfc_symbol *sym;
5253 match m;
5254
636dff67 5255 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
775e6c3a
TB
5256 == FAILURE)
5257 return MATCH_ERROR;
5258
5259 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5260 {
5261 return MATCH_ERROR;
5262 }
5263
5264 if (gfc_match_eos () == MATCH_YES)
5265 goto syntax;
5266
5267 for(;;)
5268 {
9bce3c1c
TB
5269 /* VOLATILE is special because it can be added to host-associated
5270 symbols locally. */
5271 m = gfc_match_symbol (&sym, 1);
775e6c3a
TB
5272 switch (m)
5273 {
5274 case MATCH_YES:
636dff67
SK
5275 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5276 == FAILURE)
775e6c3a
TB
5277 return MATCH_ERROR;
5278 goto next_item;
5279
5280 case MATCH_NO:
5281 break;
5282
5283 case MATCH_ERROR:
5284 return MATCH_ERROR;
5285 }
5286
5287 next_item:
5288 if (gfc_match_eos () == MATCH_YES)
5289 break;
5290 if (gfc_match_char (',') != MATCH_YES)
5291 goto syntax;
5292 }
5293
5294 return MATCH_YES;
5295
5296syntax:
5297 gfc_error ("Syntax error in VOLATILE statement at %C");
5298 return MATCH_ERROR;
5299}
5300
5301
6de9cd9a
DN
5302/* Match a module procedure statement. Note that we have to modify
5303 symbols in the parent's namespace because the current one was there
49de9e73 5304 to receive symbols that are in an interface's formal argument list. */
6de9cd9a
DN
5305
5306match
5307gfc_match_modproc (void)
5308{
5309 char name[GFC_MAX_SYMBOL_LEN + 1];
5310 gfc_symbol *sym;
5311 match m;
060fca4a 5312 gfc_namespace *module_ns;
6de9cd9a
DN
5313
5314 if (gfc_state_stack->state != COMP_INTERFACE
5315 || gfc_state_stack->previous == NULL
5316 || current_interface.type == INTERFACE_NAMELESS)
5317 {
636dff67
SK
5318 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5319 "interface");
6de9cd9a
DN
5320 return MATCH_ERROR;
5321 }
5322
060fca4a
PT
5323 module_ns = gfc_current_ns->parent;
5324 for (; module_ns; module_ns = module_ns->parent)
5325 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5326 break;
5327
5328 if (module_ns == NULL)
5329 return MATCH_ERROR;
5330
6de9cd9a
DN
5331 for (;;)
5332 {
5333 m = gfc_match_name (name);
5334 if (m == MATCH_NO)
5335 goto syntax;
5336 if (m != MATCH_YES)
5337 return MATCH_ERROR;
5338
060fca4a 5339 if (gfc_get_symbol (name, module_ns, &sym))
6de9cd9a
DN
5340 return MATCH_ERROR;
5341
5342 if (sym->attr.proc != PROC_MODULE
231b2fcc
TS
5343 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5344 sym->name, NULL) == FAILURE)
6de9cd9a
DN
5345 return MATCH_ERROR;
5346
5347 if (gfc_add_interface (sym) == FAILURE)
5348 return MATCH_ERROR;
5349
71f77fd7
PT
5350 sym->attr.mod_proc = 1;
5351
6de9cd9a
DN
5352 if (gfc_match_eos () == MATCH_YES)
5353 break;
5354 if (gfc_match_char (',') != MATCH_YES)
5355 goto syntax;
5356 }
5357
5358 return MATCH_YES;
5359
5360syntax:
5361 gfc_syntax_error (ST_MODULE_PROC);
5362 return MATCH_ERROR;
5363}
5364
5365
a8b3b0b6
CR
5366/* Match the optional attribute specifiers for a type declaration.
5367 Return MATCH_ERROR if an error is encountered in one of the handled
5368 attributes (public, private, bind(c)), MATCH_NO if what's found is
5369 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5370 checking on attribute conflicts needs to be done. */
6de9cd9a
DN
5371
5372match
a8b3b0b6 5373gfc_get_type_attr_spec (symbol_attribute *attr)
6de9cd9a 5374{
a8b3b0b6 5375 /* See if the derived type is marked as private. */
6de9cd9a
DN
5376 if (gfc_match (" , private") == MATCH_YES)
5377 {
d51347f9 5378 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5379 {
d51347f9
TB
5380 gfc_error ("Derived type at %C can only be PRIVATE in the "
5381 "specification part of a module");
6de9cd9a
DN
5382 return MATCH_ERROR;
5383 }
5384
a8b3b0b6 5385 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6de9cd9a 5386 return MATCH_ERROR;
6de9cd9a 5387 }
a8b3b0b6 5388 else if (gfc_match (" , public") == MATCH_YES)
6de9cd9a 5389 {
d51347f9 5390 if (gfc_current_state () != COMP_MODULE)
6de9cd9a 5391 {
d51347f9
TB
5392 gfc_error ("Derived type at %C can only be PUBLIC in the "
5393 "specification part of a module");
6de9cd9a
DN
5394 return MATCH_ERROR;
5395 }
5396
a8b3b0b6 5397 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6de9cd9a 5398 return MATCH_ERROR;
6de9cd9a 5399 }
a8b3b0b6
CR
5400 else if(gfc_match(" , bind ( c )") == MATCH_YES)
5401 {
5402 /* If the type is defined to be bind(c) it then needs to make
5403 sure that all fields are interoperable. This will
5404 need to be a semantic check on the finished derived type.
5405 See 15.2.3 (lines 9-12) of F2003 draft. */
5406 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5407 return MATCH_ERROR;
5408
5409 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5410 }
5411 else
5412 return MATCH_NO;
5413
5414 /* If we get here, something matched. */
5415 return MATCH_YES;
5416}
5417
5418
5419/* Match the beginning of a derived type declaration. If a type name
5420 was the result of a function, then it is possible to have a symbol
5421 already to be known as a derived type yet have no components. */
5422
5423match
5424gfc_match_derived_decl (void)
5425{
5426 char name[GFC_MAX_SYMBOL_LEN + 1];
5427 symbol_attribute attr;
5428 gfc_symbol *sym;
5429 match m;
5430 match is_type_attr_spec = MATCH_NO;
5431
5432 if (gfc_current_state () == COMP_DERIVED)
5433 return MATCH_NO;
5434
5435 gfc_clear_attr (&attr);
5436
5437 do
5438 {
5439 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5440 if (is_type_attr_spec == MATCH_ERROR)
5441 return MATCH_ERROR;
5442 } while (is_type_attr_spec == MATCH_YES);
6de9cd9a
DN
5443
5444 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
5445 {
5446 gfc_error ("Expected :: in TYPE definition at %C");
5447 return MATCH_ERROR;
5448 }
5449
5450 m = gfc_match (" %n%t", name);
5451 if (m != MATCH_YES)
5452 return m;
5453
5454 /* Make sure the name isn't the name of an intrinsic type. The
1e863adf
TS
5455 'double {precision,complex}' types don't get past the name
5456 matcher, unless they're written as a single word or in fixed
5457 form. */
6de9cd9a
DN
5458 if (strcmp (name, "integer") == 0
5459 || strcmp (name, "real") == 0
5460 || strcmp (name, "character") == 0
5461 || strcmp (name, "logical") == 0
1e863adf
TS
5462 || strcmp (name, "complex") == 0
5463 || strcmp (name, "doubleprecision") == 0
5464 || strcmp (name, "doublecomplex") == 0)
6de9cd9a 5465 {
636dff67
SK
5466 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5467 "type", name);
6de9cd9a
DN
5468 return MATCH_ERROR;
5469 }
5470
5471 if (gfc_get_symbol (name, NULL, &sym))
5472 return MATCH_ERROR;
5473
5474 if (sym->ts.type != BT_UNKNOWN)
5475 {
5476 gfc_error ("Derived type name '%s' at %C already has a basic type "
5477 "of %s", sym->name, gfc_typename (&sym->ts));
5478 return MATCH_ERROR;
5479 }
5480
5481 /* The symbol may already have the derived attribute without the
5482 components. The ways this can happen is via a function
5483 definition, an INTRINSIC statement or a subtype in another
5484 derived type that is a pointer. The first part of the AND clause
f7b529fa 5485 is true if a the symbol is not the return value of a function. */
6de9cd9a 5486 if (sym->attr.flavor != FL_DERIVED
231b2fcc 5487 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5488 return MATCH_ERROR;
5489
5490 if (sym->components != NULL)
5491 {
636dff67
SK
5492 gfc_error ("Derived type definition of '%s' at %C has already been "
5493 "defined", sym->name);
6de9cd9a
DN
5494 return MATCH_ERROR;
5495 }
5496
5497 if (attr.access != ACCESS_UNKNOWN
231b2fcc 5498 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6de9cd9a
DN
5499 return MATCH_ERROR;
5500
a8b3b0b6
CR
5501 /* See if the derived type was labeled as bind(c). */
5502 if (attr.is_bind_c != 0)
5503 sym->attr.is_bind_c = attr.is_bind_c;
5504
6de9cd9a
DN
5505 gfc_new_block = sym;
5506
5507 return MATCH_YES;
5508}
83d890b9
AL
5509
5510
5511/* Cray Pointees can be declared as:
5512 pointer (ipt, a (n,m,...,*))
5513 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5514 cheat and set a constant bound of 1 for the last dimension, if this
5515 is the case. Since there is no bounds-checking for Cray Pointees,
5516 this will be okay. */
5517
5518try
5519gfc_mod_pointee_as (gfc_array_spec *as)
5520{
5521 as->cray_pointee = true; /* This will be useful to know later. */
5522 if (as->type == AS_ASSUMED_SIZE)
5523 {
5524 as->type = AS_EXPLICIT;
5525 as->upper[as->rank - 1] = gfc_int_expr (1);
5526 as->cp_was_assumed = true;
5527 }
5528 else if (as->type == AS_ASSUMED_SHAPE)
5529 {
5530 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5531 return MATCH_ERROR;
5532 }
5533 return MATCH_YES;
5534}
25d8f0a2
TS
5535
5536
5537/* Match the enum definition statement, here we are trying to match
5538 the first line of enum definition statement.
5539 Returns MATCH_YES if match is found. */
5540
5541match
5542gfc_match_enum (void)
5543{
5544 match m;
5545
5546 m = gfc_match_eos ();
5547 if (m != MATCH_YES)
5548 return m;
5549
6133c68a 5550 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
25d8f0a2
TS
5551 == FAILURE)
5552 return MATCH_ERROR;
5553
5554 return MATCH_YES;
5555}
5556
5557
6133c68a
TS
5558/* Match a variable name with an optional initializer. When this
5559 subroutine is called, a variable is expected to be parsed next.
5560 Depending on what is happening at the moment, updates either the
5561 symbol table or the current interface. */
5562
5563static match
5564enumerator_decl (void)
5565{
5566 char name[GFC_MAX_SYMBOL_LEN + 1];
5567 gfc_expr *initializer;
5568 gfc_array_spec *as = NULL;
5569 gfc_symbol *sym;
5570 locus var_locus;
5571 match m;
5572 try t;
5573 locus old_locus;
5574
5575 initializer = NULL;
5576 old_locus = gfc_current_locus;
5577
5578 /* When we get here, we've just matched a list of attributes and
5579 maybe a type and a double colon. The next thing we expect to see
5580 is the name of the symbol. */
5581 m = gfc_match_name (name);
5582 if (m != MATCH_YES)
5583 goto cleanup;
5584
5585 var_locus = gfc_current_locus;
5586
5587 /* OK, we've successfully matched the declaration. Now put the
5588 symbol in the current namespace. If we fail to create the symbol,
5589 bail out. */
5590 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5591 {
5592 m = MATCH_ERROR;
5593 goto cleanup;
5594 }
5595
5596 /* The double colon must be present in order to have initializers.
5597 Otherwise the statement is ambiguous with an assignment statement. */
5598 if (colon_seen)
5599 {
5600 if (gfc_match_char ('=') == MATCH_YES)
5601 {
5602 m = gfc_match_init_expr (&initializer);
5603 if (m == MATCH_NO)
5604 {
5605 gfc_error ("Expected an initialization expression at %C");
5606 m = MATCH_ERROR;
5607 }
5608
5609 if (m != MATCH_YES)
5610 goto cleanup;
5611 }
5612 }
5613
5614 /* If we do not have an initializer, the initialization value of the
5615 previous enumerator (stored in last_initializer) is incremented
5616 by 1 and is used to initialize the current enumerator. */
5617 if (initializer == NULL)
5618 initializer = gfc_enum_initializer (last_initializer, old_locus);
d51347f9 5619
6133c68a
TS
5620 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
5621 {
5622 gfc_error("ENUMERATOR %L not initialized with integer expression",
5623 &var_locus);
d51347f9 5624 m = MATCH_ERROR;
6133c68a
TS
5625 gfc_free_enum_history ();
5626 goto cleanup;
5627 }
5628
5629 /* Store this current initializer, for the next enumerator variable
5630 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5631 use last_initializer below. */
5632 last_initializer = initializer;
5633 t = add_init_expr_to_sym (name, &initializer, &var_locus);
5634
5635 /* Maintain enumerator history. */
5636 gfc_find_symbol (name, NULL, 0, &sym);
5637 create_enum_history (sym, last_initializer);
5638
5639 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
5640
5641cleanup:
5642 /* Free stuff up and return. */
5643 gfc_free_expr (initializer);
5644
5645 return m;
5646}
5647
5648
66e4ab31 5649/* Match the enumerator definition statement. */
25d8f0a2
TS
5650
5651match
5652gfc_match_enumerator_def (void)
5653{
5654 match m;
6133c68a 5655 try t;
d51347f9 5656
25d8f0a2 5657 gfc_clear_ts (&current_ts);
d51347f9 5658
25d8f0a2
TS
5659 m = gfc_match (" enumerator");
5660 if (m != MATCH_YES)
5661 return m;
6133c68a
TS
5662
5663 m = gfc_match (" :: ");
5664 if (m == MATCH_ERROR)
5665 return m;
5666
5667 colon_seen = (m == MATCH_YES);
d51347f9 5668
25d8f0a2
TS
5669 if (gfc_current_state () != COMP_ENUM)
5670 {
5671 gfc_error ("ENUM definition statement expected before %C");
5672 gfc_free_enum_history ();
5673 return MATCH_ERROR;
5674 }
5675
5676 (&current_ts)->type = BT_INTEGER;
5677 (&current_ts)->kind = gfc_c_int_kind;
d51347f9 5678
6133c68a
TS
5679 gfc_clear_attr (&current_attr);
5680 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
5681 if (t == FAILURE)
25d8f0a2 5682 {
6133c68a 5683 m = MATCH_ERROR;
25d8f0a2
TS
5684 goto cleanup;
5685 }
5686
25d8f0a2
TS
5687 for (;;)
5688 {
6133c68a 5689 m = enumerator_decl ();
25d8f0a2
TS
5690 if (m == MATCH_ERROR)
5691 goto cleanup;
5692 if (m == MATCH_NO)
5693 break;
5694
5695 if (gfc_match_eos () == MATCH_YES)
5696 goto cleanup;
5697 if (gfc_match_char (',') != MATCH_YES)
5698 break;
5699 }
5700
5701 if (gfc_current_state () == COMP_ENUM)
5702 {
5703 gfc_free_enum_history ();
5704 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5705 m = MATCH_ERROR;
5706 }
5707
5708cleanup:
5709 gfc_free_array_spec (current_as);
5710 current_as = NULL;
5711 return m;
5712
5713}
5714
This page took 1.513289 seconds and 5 git commands to generate.