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