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