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