]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/decl.c
decl.c (match_old_style_init): Add data attribute to symbol.
[gcc.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
28
29
30 /* This flag is set if an old-style length selector is matched
31 during a type-declaration statement. */
32
33 static int old_char_selector;
34
35 /* When variables acquire types and attributes from a declaration
36 statement, they get them from the following static variables. The
37 first part of a declaration sets these variables and the second
38 part copies these into symbol structures. */
39
40 static gfc_typespec current_ts;
41
42 static symbol_attribute current_attr;
43 static gfc_array_spec *current_as;
44 static int colon_seen;
45
46 /* Initializer of the previous enumerator. */
47
48 static gfc_expr *last_initializer;
49
50 /* History of all the enumerators is maintained, so that
51 kind values of all the enumerators could be updated depending
52 upon the maximum initialized value. */
53
54 typedef struct enumerator_history
55 {
56 gfc_symbol *sym;
57 gfc_expr *initializer;
58 struct enumerator_history *next;
59 }
60 enumerator_history;
61
62 /* Header of enum history chain. */
63
64 static enumerator_history *enum_history = NULL;
65
66 /* Pointer of enum history node containing largest initializer. */
67
68 static enumerator_history *max_enum = NULL;
69
70 /* gfc_new_block points to the symbol of a newly matched block. */
71
72 gfc_symbol *gfc_new_block;
73
74
75 /********************* DATA statement subroutines *********************/
76
77 /* Free a gfc_data_variable structure and everything beneath it. */
78
79 static void
80 free_variable (gfc_data_variable * p)
81 {
82 gfc_data_variable *q;
83
84 for (; p; p = q)
85 {
86 q = p->next;
87 gfc_free_expr (p->expr);
88 gfc_free_iterator (&p->iter, 0);
89 free_variable (p->list);
90
91 gfc_free (p);
92 }
93 }
94
95
96 /* Free a gfc_data_value structure and everything beneath it. */
97
98 static void
99 free_value (gfc_data_value * p)
100 {
101 gfc_data_value *q;
102
103 for (; p; p = q)
104 {
105 q = p->next;
106 gfc_free_expr (p->expr);
107 gfc_free (p);
108 }
109 }
110
111
112 /* Free a list of gfc_data structures. */
113
114 void
115 gfc_free_data (gfc_data * p)
116 {
117 gfc_data *q;
118
119 for (; p; p = q)
120 {
121 q = p->next;
122
123 free_variable (p->var);
124 free_value (p->value);
125
126 gfc_free (p);
127 }
128 }
129
130
131 static match var_element (gfc_data_variable *);
132
133 /* Match a list of variables terminated by an iterator and a right
134 parenthesis. */
135
136 static match
137 var_list (gfc_data_variable * parent)
138 {
139 gfc_data_variable *tail, var;
140 match m;
141
142 m = var_element (&var);
143 if (m == MATCH_ERROR)
144 return MATCH_ERROR;
145 if (m == MATCH_NO)
146 goto syntax;
147
148 tail = gfc_get_data_variable ();
149 *tail = var;
150
151 parent->list = tail;
152
153 for (;;)
154 {
155 if (gfc_match_char (',') != MATCH_YES)
156 goto syntax;
157
158 m = gfc_match_iterator (&parent->iter, 1);
159 if (m == MATCH_YES)
160 break;
161 if (m == MATCH_ERROR)
162 return MATCH_ERROR;
163
164 m = var_element (&var);
165 if (m == MATCH_ERROR)
166 return MATCH_ERROR;
167 if (m == MATCH_NO)
168 goto syntax;
169
170 tail->next = gfc_get_data_variable ();
171 tail = tail->next;
172
173 *tail = var;
174 }
175
176 if (gfc_match_char (')') != MATCH_YES)
177 goto syntax;
178 return MATCH_YES;
179
180 syntax:
181 gfc_syntax_error (ST_DATA);
182 return MATCH_ERROR;
183 }
184
185
186 /* Match a single element in a data variable list, which can be a
187 variable-iterator list. */
188
189 static match
190 var_element (gfc_data_variable * new)
191 {
192 match m;
193 gfc_symbol *sym;
194
195 memset (new, 0, sizeof (gfc_data_variable));
196
197 if (gfc_match_char ('(') == MATCH_YES)
198 return var_list (new);
199
200 m = gfc_match_variable (&new->expr, 0);
201 if (m != MATCH_YES)
202 return m;
203
204 sym = new->expr->symtree->n.sym;
205
206 if (!sym->attr.function && gfc_current_ns->parent && gfc_current_ns->parent == sym->ns)
207 {
208 gfc_error ("Host associated variable '%s' may not be in the DATA "
209 "statement at %C.", sym->name);
210 return MATCH_ERROR;
211 }
212
213 if (gfc_current_state () != COMP_BLOCK_DATA
214 && sym->attr.in_common
215 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
216 "common block variable '%s' in DATA statement at %C",
217 sym->name) == FAILURE)
218 return MATCH_ERROR;
219
220 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
221 return MATCH_ERROR;
222
223 return MATCH_YES;
224 }
225
226
227 /* Match the top-level list of data variables. */
228
229 static match
230 top_var_list (gfc_data * d)
231 {
232 gfc_data_variable var, *tail, *new;
233 match m;
234
235 tail = NULL;
236
237 for (;;)
238 {
239 m = var_element (&var);
240 if (m == MATCH_NO)
241 goto syntax;
242 if (m == MATCH_ERROR)
243 return MATCH_ERROR;
244
245 new = gfc_get_data_variable ();
246 *new = var;
247
248 if (tail == NULL)
249 d->var = new;
250 else
251 tail->next = new;
252
253 tail = new;
254
255 if (gfc_match_char ('/') == MATCH_YES)
256 break;
257 if (gfc_match_char (',') != MATCH_YES)
258 goto syntax;
259 }
260
261 return MATCH_YES;
262
263 syntax:
264 gfc_syntax_error (ST_DATA);
265 return MATCH_ERROR;
266 }
267
268
269 static match
270 match_data_constant (gfc_expr ** result)
271 {
272 char name[GFC_MAX_SYMBOL_LEN + 1];
273 gfc_symbol *sym;
274 gfc_expr *expr;
275 match m;
276
277 m = gfc_match_literal_constant (&expr, 1);
278 if (m == MATCH_YES)
279 {
280 *result = expr;
281 return MATCH_YES;
282 }
283
284 if (m == MATCH_ERROR)
285 return MATCH_ERROR;
286
287 m = gfc_match_null (result);
288 if (m != MATCH_NO)
289 return m;
290
291 m = gfc_match_name (name);
292 if (m != MATCH_YES)
293 return m;
294
295 if (gfc_find_symbol (name, NULL, 1, &sym))
296 return MATCH_ERROR;
297
298 if (sym == NULL
299 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
300 {
301 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
302 name);
303 return MATCH_ERROR;
304 }
305 else if (sym->attr.flavor == FL_DERIVED)
306 return gfc_match_structure_constructor (sym, result);
307
308 *result = gfc_copy_expr (sym->value);
309 return MATCH_YES;
310 }
311
312
313 /* Match a list of values in a DATA statement. The leading '/' has
314 already been seen at this point. */
315
316 static match
317 top_val_list (gfc_data * data)
318 {
319 gfc_data_value *new, *tail;
320 gfc_expr *expr;
321 const char *msg;
322 match m;
323
324 tail = NULL;
325
326 for (;;)
327 {
328 m = match_data_constant (&expr);
329 if (m == MATCH_NO)
330 goto syntax;
331 if (m == MATCH_ERROR)
332 return MATCH_ERROR;
333
334 new = gfc_get_data_value ();
335
336 if (tail == NULL)
337 data->value = new;
338 else
339 tail->next = new;
340
341 tail = new;
342
343 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
344 {
345 tail->expr = expr;
346 tail->repeat = 1;
347 }
348 else
349 {
350 signed int tmp;
351 msg = gfc_extract_int (expr, &tmp);
352 gfc_free_expr (expr);
353 if (msg != NULL)
354 {
355 gfc_error (msg);
356 return MATCH_ERROR;
357 }
358 tail->repeat = tmp;
359
360 m = match_data_constant (&tail->expr);
361 if (m == MATCH_NO)
362 goto syntax;
363 if (m == MATCH_ERROR)
364 return MATCH_ERROR;
365 }
366
367 if (gfc_match_char ('/') == MATCH_YES)
368 break;
369 if (gfc_match_char (',') == MATCH_NO)
370 goto syntax;
371 }
372
373 return MATCH_YES;
374
375 syntax:
376 gfc_syntax_error (ST_DATA);
377 return MATCH_ERROR;
378 }
379
380
381 /* Matches an old style initialization. */
382
383 static match
384 match_old_style_init (const char *name)
385 {
386 match m;
387 gfc_symtree *st;
388 gfc_symbol *sym;
389 gfc_data *newdata;
390
391 /* Set up data structure to hold initializers. */
392 gfc_find_sym_tree (name, NULL, 0, &st);
393 sym = st->n.sym;
394
395 newdata = gfc_get_data ();
396 newdata->var = gfc_get_data_variable ();
397 newdata->var->expr = gfc_get_variable_expr (st);
398
399 /* Match initial value list. This also eats the terminal
400 '/'. */
401 m = top_val_list (newdata);
402 if (m != MATCH_YES)
403 {
404 gfc_free (newdata);
405 return m;
406 }
407
408 if (gfc_pure (NULL))
409 {
410 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
411 gfc_free (newdata);
412 return MATCH_ERROR;
413 }
414
415 /* Mark the variable as having appeared in a data statement. */
416 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
417 {
418 gfc_free (newdata);
419 return MATCH_ERROR;
420 }
421
422 /* Chain in namespace list of DATA initializers. */
423 newdata->next = gfc_current_ns->data;
424 gfc_current_ns->data = newdata;
425
426 return m;
427 }
428
429 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
430 we are matching a DATA statement and are therefore issuing an error
431 if we encounter something unexpected, if not, we're trying to match
432 an old-style initialization expression of the form INTEGER I /2/. */
433
434 match
435 gfc_match_data (void)
436 {
437 gfc_data *new;
438 match m;
439
440 for (;;)
441 {
442 new = gfc_get_data ();
443 new->where = gfc_current_locus;
444
445 m = top_var_list (new);
446 if (m != MATCH_YES)
447 goto cleanup;
448
449 m = top_val_list (new);
450 if (m != MATCH_YES)
451 goto cleanup;
452
453 new->next = gfc_current_ns->data;
454 gfc_current_ns->data = new;
455
456 if (gfc_match_eos () == MATCH_YES)
457 break;
458
459 gfc_match_char (','); /* Optional comma */
460 }
461
462 if (gfc_pure (NULL))
463 {
464 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
465 return MATCH_ERROR;
466 }
467
468 return MATCH_YES;
469
470 cleanup:
471 gfc_free_data (new);
472 return MATCH_ERROR;
473 }
474
475
476 /************************ Declaration statements *********************/
477
478 /* Match an intent specification. Since this can only happen after an
479 INTENT word, a legal intent-spec must follow. */
480
481 static sym_intent
482 match_intent_spec (void)
483 {
484
485 if (gfc_match (" ( in out )") == MATCH_YES)
486 return INTENT_INOUT;
487 if (gfc_match (" ( in )") == MATCH_YES)
488 return INTENT_IN;
489 if (gfc_match (" ( out )") == MATCH_YES)
490 return INTENT_OUT;
491
492 gfc_error ("Bad INTENT specification at %C");
493 return INTENT_UNKNOWN;
494 }
495
496
497 /* Matches a character length specification, which is either a
498 specification expression or a '*'. */
499
500 static match
501 char_len_param_value (gfc_expr ** expr)
502 {
503
504 if (gfc_match_char ('*') == MATCH_YES)
505 {
506 *expr = NULL;
507 return MATCH_YES;
508 }
509
510 return gfc_match_expr (expr);
511 }
512
513
514 /* A character length is a '*' followed by a literal integer or a
515 char_len_param_value in parenthesis. */
516
517 static match
518 match_char_length (gfc_expr ** expr)
519 {
520 int length;
521 match m;
522
523 m = gfc_match_char ('*');
524 if (m != MATCH_YES)
525 return m;
526
527 m = gfc_match_small_literal_int (&length, NULL);
528 if (m == MATCH_ERROR)
529 return m;
530
531 if (m == MATCH_YES)
532 {
533 *expr = gfc_int_expr (length);
534 return m;
535 }
536
537 if (gfc_match_char ('(') == MATCH_NO)
538 goto syntax;
539
540 m = char_len_param_value (expr);
541 if (m == MATCH_ERROR)
542 return m;
543 if (m == MATCH_NO)
544 goto syntax;
545
546 if (gfc_match_char (')') == MATCH_NO)
547 {
548 gfc_free_expr (*expr);
549 *expr = NULL;
550 goto syntax;
551 }
552
553 return MATCH_YES;
554
555 syntax:
556 gfc_error ("Syntax error in character length specification at %C");
557 return MATCH_ERROR;
558 }
559
560
561 /* Special subroutine for finding a symbol. Check if the name is found
562 in the current name space. If not, and we're compiling a function or
563 subroutine and the parent compilation unit is an interface, then check
564 to see if the name we've been given is the name of the interface
565 (located in another namespace). */
566
567 static int
568 find_special (const char *name, gfc_symbol ** result)
569 {
570 gfc_state_data *s;
571 int i;
572
573 i = gfc_get_symbol (name, NULL, result);
574 if (i==0)
575 goto end;
576
577 if (gfc_current_state () != COMP_SUBROUTINE
578 && gfc_current_state () != COMP_FUNCTION)
579 goto end;
580
581 s = gfc_state_stack->previous;
582 if (s == NULL)
583 goto end;
584
585 if (s->state != COMP_INTERFACE)
586 goto end;
587 if (s->sym == NULL)
588 goto end; /* Nameless interface */
589
590 if (strcmp (name, s->sym->name) == 0)
591 {
592 *result = s->sym;
593 return 0;
594 }
595
596 end:
597 return i;
598 }
599
600
601 /* Special subroutine for getting a symbol node associated with a
602 procedure name, used in SUBROUTINE and FUNCTION statements. The
603 symbol is created in the parent using with symtree node in the
604 child unit pointing to the symbol. If the current namespace has no
605 parent, then the symbol is just created in the current unit. */
606
607 static int
608 get_proc_name (const char *name, gfc_symbol ** result,
609 bool module_fcn_entry)
610 {
611 gfc_symtree *st;
612 gfc_symbol *sym;
613 int rc;
614
615 /* Module functions have to be left in their own namespace because
616 they have potentially (almost certainly!) already been referenced.
617 In this sense, they are rather like external functions. This is
618 fixed up in resolve.c(resolve_entries), where the symbol name-
619 space is set to point to the master function, so that the fake
620 result mechanism can work. */
621 if (module_fcn_entry)
622 rc = gfc_get_symbol (name, NULL, result);
623 else
624 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
625
626 sym = *result;
627
628 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
629 {
630 /* Trap another encompassed procedure with the same name. All
631 these conditions are necessary to avoid picking up an entry
632 whose name clashes with that of the encompassing procedure;
633 this is handled using gsymbols to register unique,globally
634 accessible names. */
635 if (sym->attr.flavor != 0
636 && sym->attr.proc != 0
637 && sym->formal)
638 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
639 name, &sym->declared_at);
640
641 /* Trap declarations of attributes in encompassing scope. The
642 signature for this is that ts.kind is set. Legitimate
643 references only set ts.type. */
644 if (sym->ts.kind != 0
645 && sym->attr.proc == 0
646 && gfc_current_ns->parent != NULL
647 && sym->attr.access == 0
648 && !module_fcn_entry)
649 gfc_error_now ("Procedure '%s' at %C has an explicit interface"
650 " and must not have attributes declared at %L",
651 name, &sym->declared_at);
652 }
653
654 if (gfc_current_ns->parent == NULL || *result == NULL)
655 return rc;
656
657 /* Module function entries will already have a symtree in
658 the current namespace but will need one at module level. */
659 if (module_fcn_entry)
660 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
661 else
662 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
663
664 st->n.sym = sym;
665 sym->refs++;
666
667 /* See if the procedure should be a module procedure */
668
669 if (((sym->ns->proc_name != NULL
670 && sym->ns->proc_name->attr.flavor == FL_MODULE
671 && sym->attr.proc != PROC_MODULE) || module_fcn_entry)
672 && gfc_add_procedure (&sym->attr, PROC_MODULE,
673 sym->name, NULL) == FAILURE)
674 rc = 2;
675
676 return rc;
677 }
678
679
680 /* Function called by variable_decl() that adds a name to the symbol
681 table. */
682
683 static try
684 build_sym (const char *name, gfc_charlen * cl,
685 gfc_array_spec ** as, locus * var_locus)
686 {
687 symbol_attribute attr;
688 gfc_symbol *sym;
689
690 /* if (find_special (name, &sym)) */
691 if (gfc_get_symbol (name, NULL, &sym))
692 return FAILURE;
693
694 /* Start updating the symbol table. Add basic type attribute
695 if present. */
696 if (current_ts.type != BT_UNKNOWN
697 &&(sym->attr.implicit_type == 0
698 || !gfc_compare_types (&sym->ts, &current_ts))
699 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
700 return FAILURE;
701
702 if (sym->ts.type == BT_CHARACTER)
703 sym->ts.cl = cl;
704
705 /* Add dimension attribute if present. */
706 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
707 return FAILURE;
708 *as = NULL;
709
710 /* Add attribute to symbol. The copy is so that we can reset the
711 dimension attribute. */
712 attr = current_attr;
713 attr.dimension = 0;
714
715 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
716 return FAILURE;
717
718 return SUCCESS;
719 }
720
721 /* Set character constant to the given length. The constant will be padded or
722 truncated. */
723
724 void
725 gfc_set_constant_character_len (int len, gfc_expr * expr)
726 {
727 char * s;
728 int slen;
729
730 gcc_assert (expr->expr_type == EXPR_CONSTANT);
731 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
732
733 slen = expr->value.character.length;
734 if (len != slen)
735 {
736 s = gfc_getmem (len);
737 memcpy (s, expr->value.character.string, MIN (len, slen));
738 if (len > slen)
739 memset (&s[slen], ' ', len - slen);
740 gfc_free (expr->value.character.string);
741 expr->value.character.string = s;
742 expr->value.character.length = len;
743 }
744 }
745
746
747 /* Function to create and update the enumerator history
748 using the information passed as arguments.
749 Pointer "max_enum" is also updated, to point to
750 enum history node containing largest initializer.
751
752 SYM points to the symbol node of enumerator.
753 INIT points to its enumerator value. */
754
755 static void
756 create_enum_history(gfc_symbol *sym, gfc_expr *init)
757 {
758 enumerator_history *new_enum_history;
759 gcc_assert (sym != NULL && init != NULL);
760
761 new_enum_history = gfc_getmem (sizeof (enumerator_history));
762
763 new_enum_history->sym = sym;
764 new_enum_history->initializer = init;
765 new_enum_history->next = NULL;
766
767 if (enum_history == NULL)
768 {
769 enum_history = new_enum_history;
770 max_enum = enum_history;
771 }
772 else
773 {
774 new_enum_history->next = enum_history;
775 enum_history = new_enum_history;
776
777 if (mpz_cmp (max_enum->initializer->value.integer,
778 new_enum_history->initializer->value.integer) < 0)
779 max_enum = new_enum_history;
780 }
781 }
782
783
784 /* Function to free enum kind history. */
785
786 void
787 gfc_free_enum_history(void)
788 {
789 enumerator_history *current = enum_history;
790 enumerator_history *next;
791
792 while (current != NULL)
793 {
794 next = current->next;
795 gfc_free (current);
796 current = next;
797 }
798 max_enum = NULL;
799 enum_history = NULL;
800 }
801
802
803 /* Function called by variable_decl() that adds an initialization
804 expression to a symbol. */
805
806 static try
807 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
808 locus * var_locus)
809 {
810 symbol_attribute attr;
811 gfc_symbol *sym;
812 gfc_expr *init;
813
814 init = *initp;
815 if (find_special (name, &sym))
816 return FAILURE;
817
818 attr = sym->attr;
819
820 /* If this symbol is confirming an implicit parameter type,
821 then an initialization expression is not allowed. */
822 if (attr.flavor == FL_PARAMETER
823 && sym->value != NULL
824 && *initp != NULL)
825 {
826 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
827 sym->name);
828 return FAILURE;
829 }
830
831 if (attr.in_common
832 && !attr.data
833 && *initp != NULL)
834 {
835 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
836 sym->name);
837 return FAILURE;
838 }
839
840 if (init == NULL)
841 {
842 /* An initializer is required for PARAMETER declarations. */
843 if (attr.flavor == FL_PARAMETER)
844 {
845 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
846 return FAILURE;
847 }
848 }
849 else
850 {
851 /* If a variable appears in a DATA block, it cannot have an
852 initializer. */
853 if (sym->attr.data)
854 {
855 gfc_error
856 ("Variable '%s' at %C with an initializer already appears "
857 "in a DATA statement", sym->name);
858 return FAILURE;
859 }
860
861 /* Check if the assignment can happen. This has to be put off
862 until later for a derived type variable. */
863 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
864 && gfc_check_assign_symbol (sym, init) == FAILURE)
865 return FAILURE;
866
867 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
868 {
869 /* Update symbol character length according initializer. */
870 if (sym->ts.cl->length == NULL)
871 {
872 /* If there are multiple CHARACTER variables declared on
873 the same line, we don't want them to share the same
874 length. */
875 sym->ts.cl = gfc_get_charlen ();
876 sym->ts.cl->next = gfc_current_ns->cl_list;
877 gfc_current_ns->cl_list = sym->ts.cl;
878
879 if (init->expr_type == EXPR_CONSTANT)
880 sym->ts.cl->length =
881 gfc_int_expr (init->value.character.length);
882 else if (init->expr_type == EXPR_ARRAY)
883 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
884 }
885 /* Update initializer character length according symbol. */
886 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
887 {
888 int len = mpz_get_si (sym->ts.cl->length->value.integer);
889 gfc_constructor * p;
890
891 if (init->expr_type == EXPR_CONSTANT)
892 gfc_set_constant_character_len (len, init);
893 else if (init->expr_type == EXPR_ARRAY)
894 {
895 gfc_free_expr (init->ts.cl->length);
896 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
897 for (p = init->value.constructor; p; p = p->next)
898 gfc_set_constant_character_len (len, p->expr);
899 }
900 }
901 }
902
903 /* Add initializer. Make sure we keep the ranks sane. */
904 if (sym->attr.dimension && init->rank == 0)
905 init->rank = sym->as->rank;
906
907 sym->value = init;
908 *initp = NULL;
909 }
910
911 /* Maintain enumerator history. */
912 if (gfc_current_state () == COMP_ENUM)
913 create_enum_history (sym, init);
914
915 return SUCCESS;
916 }
917
918
919 /* Function called by variable_decl() that adds a name to a structure
920 being built. */
921
922 static try
923 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
924 gfc_array_spec ** as)
925 {
926 gfc_component *c;
927
928 /* If the current symbol is of the same derived type that we're
929 constructing, it must have the pointer attribute. */
930 if (current_ts.type == BT_DERIVED
931 && current_ts.derived == gfc_current_block ()
932 && current_attr.pointer == 0)
933 {
934 gfc_error ("Component at %C must have the POINTER attribute");
935 return FAILURE;
936 }
937
938 if (gfc_current_block ()->attr.pointer
939 && (*as)->rank != 0)
940 {
941 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
942 {
943 gfc_error ("Array component of structure at %C must have explicit "
944 "or deferred shape");
945 return FAILURE;
946 }
947 }
948
949 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
950 return FAILURE;
951
952 c->ts = current_ts;
953 c->ts.cl = cl;
954 gfc_set_component_attr (c, &current_attr);
955
956 c->initializer = *init;
957 *init = NULL;
958
959 c->as = *as;
960 if (c->as != NULL)
961 c->dimension = 1;
962 *as = NULL;
963
964 /* Check array components. */
965 if (!c->dimension)
966 return SUCCESS;
967
968 if (c->pointer)
969 {
970 if (c->as->type != AS_DEFERRED)
971 {
972 gfc_error ("Pointer array component of structure at %C "
973 "must have a deferred shape");
974 return FAILURE;
975 }
976 }
977 else
978 {
979 if (c->as->type != AS_EXPLICIT)
980 {
981 gfc_error
982 ("Array component of structure at %C must have an explicit "
983 "shape");
984 return FAILURE;
985 }
986 }
987
988 return SUCCESS;
989 }
990
991
992 /* Match a 'NULL()', and possibly take care of some side effects. */
993
994 match
995 gfc_match_null (gfc_expr ** result)
996 {
997 gfc_symbol *sym;
998 gfc_expr *e;
999 match m;
1000
1001 m = gfc_match (" null ( )");
1002 if (m != MATCH_YES)
1003 return m;
1004
1005 /* The NULL symbol now has to be/become an intrinsic function. */
1006 if (gfc_get_symbol ("null", NULL, &sym))
1007 {
1008 gfc_error ("NULL() initialization at %C is ambiguous");
1009 return MATCH_ERROR;
1010 }
1011
1012 gfc_intrinsic_symbol (sym);
1013
1014 if (sym->attr.proc != PROC_INTRINSIC
1015 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1016 sym->name, NULL) == FAILURE
1017 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1018 return MATCH_ERROR;
1019
1020 e = gfc_get_expr ();
1021 e->where = gfc_current_locus;
1022 e->expr_type = EXPR_NULL;
1023 e->ts.type = BT_UNKNOWN;
1024
1025 *result = e;
1026
1027 return MATCH_YES;
1028 }
1029
1030
1031 /* Match a variable name with an optional initializer. When this
1032 subroutine is called, a variable is expected to be parsed next.
1033 Depending on what is happening at the moment, updates either the
1034 symbol table or the current interface. */
1035
1036 static match
1037 variable_decl (int elem)
1038 {
1039 char name[GFC_MAX_SYMBOL_LEN + 1];
1040 gfc_expr *initializer, *char_len;
1041 gfc_array_spec *as;
1042 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1043 gfc_charlen *cl;
1044 locus var_locus;
1045 match m;
1046 try t;
1047 gfc_symbol *sym;
1048 locus old_locus;
1049
1050 initializer = NULL;
1051 as = NULL;
1052 cp_as = NULL;
1053 old_locus = gfc_current_locus;
1054
1055 /* When we get here, we've just matched a list of attributes and
1056 maybe a type and a double colon. The next thing we expect to see
1057 is the name of the symbol. */
1058 m = gfc_match_name (name);
1059 if (m != MATCH_YES)
1060 goto cleanup;
1061
1062 var_locus = gfc_current_locus;
1063
1064 /* Now we could see the optional array spec. or character length. */
1065 m = gfc_match_array_spec (&as);
1066 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1067 cp_as = gfc_copy_array_spec (as);
1068 else if (m == MATCH_ERROR)
1069 goto cleanup;
1070
1071 if (m == MATCH_NO)
1072 as = gfc_copy_array_spec (current_as);
1073 else if (gfc_current_state () == COMP_ENUM)
1074 {
1075 gfc_error ("Enumerator cannot be array at %C");
1076 gfc_free_enum_history ();
1077 m = MATCH_ERROR;
1078 goto cleanup;
1079 }
1080
1081
1082 char_len = NULL;
1083 cl = NULL;
1084
1085 if (current_ts.type == BT_CHARACTER)
1086 {
1087 switch (match_char_length (&char_len))
1088 {
1089 case MATCH_YES:
1090 cl = gfc_get_charlen ();
1091 cl->next = gfc_current_ns->cl_list;
1092 gfc_current_ns->cl_list = cl;
1093
1094 cl->length = char_len;
1095 break;
1096
1097 /* Non-constant lengths need to be copied after the first
1098 element. */
1099 case MATCH_NO:
1100 if (elem > 1 && current_ts.cl->length
1101 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1102 {
1103 cl = gfc_get_charlen ();
1104 cl->next = gfc_current_ns->cl_list;
1105 gfc_current_ns->cl_list = cl;
1106 cl->length = gfc_copy_expr (current_ts.cl->length);
1107 }
1108 else
1109 cl = current_ts.cl;
1110
1111 break;
1112
1113 case MATCH_ERROR:
1114 goto cleanup;
1115 }
1116 }
1117
1118 /* If this symbol has already shown up in a Cray Pointer declaration,
1119 then we want to set the type & bail out. */
1120 if (gfc_option.flag_cray_pointer)
1121 {
1122 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1123 if (sym != NULL && sym->attr.cray_pointee)
1124 {
1125 sym->ts.type = current_ts.type;
1126 sym->ts.kind = current_ts.kind;
1127 sym->ts.cl = cl;
1128 sym->ts.derived = current_ts.derived;
1129 m = MATCH_YES;
1130
1131 /* Check to see if we have an array specification. */
1132 if (cp_as != NULL)
1133 {
1134 if (sym->as != NULL)
1135 {
1136 gfc_error ("Duplicate array spec for Cray pointee at %C.");
1137 gfc_free_array_spec (cp_as);
1138 m = MATCH_ERROR;
1139 goto cleanup;
1140 }
1141 else
1142 {
1143 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1144 gfc_internal_error ("Couldn't set pointee array spec.");
1145
1146 /* Fix the array spec. */
1147 m = gfc_mod_pointee_as (sym->as);
1148 if (m == MATCH_ERROR)
1149 goto cleanup;
1150 }
1151 }
1152 goto cleanup;
1153 }
1154 else
1155 {
1156 gfc_free_array_spec (cp_as);
1157 }
1158 }
1159
1160
1161 /* OK, we've successfully matched the declaration. Now put the
1162 symbol in the current namespace, because it might be used in the
1163 optional initialization expression for this symbol, e.g. this is
1164 perfectly legal:
1165
1166 integer, parameter :: i = huge(i)
1167
1168 This is only true for parameters or variables of a basic type.
1169 For components of derived types, it is not true, so we don't
1170 create a symbol for those yet. If we fail to create the symbol,
1171 bail out. */
1172 if (gfc_current_state () != COMP_DERIVED
1173 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1174 {
1175 m = MATCH_ERROR;
1176 goto cleanup;
1177 }
1178
1179 /* In functions that have a RESULT variable defined, the function
1180 name always refers to function calls. Therefore, the name is
1181 not allowed to appear in specification statements. */
1182 if (gfc_current_state () == COMP_FUNCTION
1183 && gfc_current_block () != NULL
1184 && gfc_current_block ()->result != NULL
1185 && gfc_current_block ()->result != gfc_current_block ()
1186 && strcmp (gfc_current_block ()->name, name) == 0)
1187 {
1188 gfc_error ("Function name '%s' not allowed at %C", name);
1189 m = MATCH_ERROR;
1190 goto cleanup;
1191 }
1192
1193 /* We allow old-style initializations of the form
1194 integer i /2/, j(4) /3*3, 1/
1195 (if no colon has been seen). These are different from data
1196 statements in that initializers are only allowed to apply to the
1197 variable immediately preceding, i.e.
1198 integer i, j /1, 2/
1199 is not allowed. Therefore we have to do some work manually, that
1200 could otherwise be left to the matchers for DATA statements. */
1201
1202 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1203 {
1204 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1205 "initialization at %C") == FAILURE)
1206 return MATCH_ERROR;
1207
1208 return match_old_style_init (name);
1209 }
1210
1211 /* The double colon must be present in order to have initializers.
1212 Otherwise the statement is ambiguous with an assignment statement. */
1213 if (colon_seen)
1214 {
1215 if (gfc_match (" =>") == MATCH_YES)
1216 {
1217
1218 if (!current_attr.pointer)
1219 {
1220 gfc_error ("Initialization at %C isn't for a pointer variable");
1221 m = MATCH_ERROR;
1222 goto cleanup;
1223 }
1224
1225 m = gfc_match_null (&initializer);
1226 if (m == MATCH_NO)
1227 {
1228 gfc_error ("Pointer initialization requires a NULL() at %C");
1229 m = MATCH_ERROR;
1230 }
1231
1232 if (gfc_pure (NULL))
1233 {
1234 gfc_error
1235 ("Initialization of pointer at %C is not allowed in a "
1236 "PURE procedure");
1237 m = MATCH_ERROR;
1238 }
1239
1240 if (m != MATCH_YES)
1241 goto cleanup;
1242
1243 }
1244 else if (gfc_match_char ('=') == MATCH_YES)
1245 {
1246 if (current_attr.pointer)
1247 {
1248 gfc_error
1249 ("Pointer initialization at %C requires '=>', not '='");
1250 m = MATCH_ERROR;
1251 goto cleanup;
1252 }
1253
1254 m = gfc_match_init_expr (&initializer);
1255 if (m == MATCH_NO)
1256 {
1257 gfc_error ("Expected an initialization expression at %C");
1258 m = MATCH_ERROR;
1259 }
1260
1261 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1262 {
1263 gfc_error
1264 ("Initialization of variable at %C is not allowed in a "
1265 "PURE procedure");
1266 m = MATCH_ERROR;
1267 }
1268
1269 if (m != MATCH_YES)
1270 goto cleanup;
1271 }
1272 }
1273
1274 /* Check if we are parsing an enumeration and if the current enumerator
1275 variable has an initializer or not. If it does not have an
1276 initializer, the initialization value of the previous enumerator
1277 (stored in last_initializer) is incremented by 1 and is used to
1278 initialize the current enumerator. */
1279 if (gfc_current_state () == COMP_ENUM)
1280 {
1281 if (initializer == NULL)
1282 initializer = gfc_enum_initializer (last_initializer, old_locus);
1283
1284 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1285 {
1286 gfc_error("ENUMERATOR %L not initialized with integer expression",
1287 &var_locus);
1288 m = MATCH_ERROR;
1289 gfc_free_enum_history ();
1290 goto cleanup;
1291 }
1292
1293 /* Store this current initializer, for the next enumerator
1294 variable to be parsed. */
1295 last_initializer = initializer;
1296 }
1297
1298 /* Add the initializer. Note that it is fine if initializer is
1299 NULL here, because we sometimes also need to check if a
1300 declaration *must* have an initialization expression. */
1301 if (gfc_current_state () != COMP_DERIVED)
1302 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1303 else
1304 {
1305 if (current_ts.type == BT_DERIVED && !current_attr.pointer
1306 && !initializer)
1307 initializer = gfc_default_initializer (&current_ts);
1308 t = build_struct (name, cl, &initializer, &as);
1309 }
1310
1311 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1312
1313 cleanup:
1314 /* Free stuff up and return. */
1315 gfc_free_expr (initializer);
1316 gfc_free_array_spec (as);
1317
1318 return m;
1319 }
1320
1321
1322 /* Match an extended-f77 kind specification. */
1323
1324 match
1325 gfc_match_old_kind_spec (gfc_typespec * ts)
1326 {
1327 match m;
1328 int original_kind;
1329
1330 if (gfc_match_char ('*') != MATCH_YES)
1331 return MATCH_NO;
1332
1333 m = gfc_match_small_literal_int (&ts->kind, NULL);
1334 if (m != MATCH_YES)
1335 return MATCH_ERROR;
1336
1337 original_kind = ts->kind;
1338
1339 /* Massage the kind numbers for complex types. */
1340 if (ts->type == BT_COMPLEX)
1341 {
1342 if (ts->kind % 2)
1343 {
1344 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1345 gfc_basic_typename (ts->type), original_kind);
1346 return MATCH_ERROR;
1347 }
1348 ts->kind /= 2;
1349 }
1350
1351 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1352 {
1353 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1354 gfc_basic_typename (ts->type), original_kind);
1355 return MATCH_ERROR;
1356 }
1357
1358 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1359 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1360 return MATCH_ERROR;
1361
1362 return MATCH_YES;
1363 }
1364
1365
1366 /* Match a kind specification. Since kinds are generally optional, we
1367 usually return MATCH_NO if something goes wrong. If a "kind="
1368 string is found, then we know we have an error. */
1369
1370 match
1371 gfc_match_kind_spec (gfc_typespec * ts)
1372 {
1373 locus where;
1374 gfc_expr *e;
1375 match m, n;
1376 const char *msg;
1377
1378 m = MATCH_NO;
1379 e = NULL;
1380
1381 where = gfc_current_locus;
1382
1383 if (gfc_match_char ('(') == MATCH_NO)
1384 return MATCH_NO;
1385
1386 /* Also gobbles optional text. */
1387 if (gfc_match (" kind = ") == MATCH_YES)
1388 m = MATCH_ERROR;
1389
1390 n = gfc_match_init_expr (&e);
1391 if (n == MATCH_NO)
1392 gfc_error ("Expected initialization expression at %C");
1393 if (n != MATCH_YES)
1394 return MATCH_ERROR;
1395
1396 if (e->rank != 0)
1397 {
1398 gfc_error ("Expected scalar initialization expression at %C");
1399 m = MATCH_ERROR;
1400 goto no_match;
1401 }
1402
1403 msg = gfc_extract_int (e, &ts->kind);
1404 if (msg != NULL)
1405 {
1406 gfc_error (msg);
1407 m = MATCH_ERROR;
1408 goto no_match;
1409 }
1410
1411 gfc_free_expr (e);
1412 e = NULL;
1413
1414 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1415 {
1416 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1417 gfc_basic_typename (ts->type));
1418
1419 m = MATCH_ERROR;
1420 goto no_match;
1421 }
1422
1423 if (gfc_match_char (')') != MATCH_YES)
1424 {
1425 gfc_error ("Missing right paren at %C");
1426 goto no_match;
1427 }
1428
1429 return MATCH_YES;
1430
1431 no_match:
1432 gfc_free_expr (e);
1433 gfc_current_locus = where;
1434 return m;
1435 }
1436
1437
1438 /* Match the various kind/length specifications in a CHARACTER
1439 declaration. We don't return MATCH_NO. */
1440
1441 static match
1442 match_char_spec (gfc_typespec * ts)
1443 {
1444 int i, kind, seen_length;
1445 gfc_charlen *cl;
1446 gfc_expr *len;
1447 match m;
1448
1449 kind = gfc_default_character_kind;
1450 len = NULL;
1451 seen_length = 0;
1452
1453 /* Try the old-style specification first. */
1454 old_char_selector = 0;
1455
1456 m = match_char_length (&len);
1457 if (m != MATCH_NO)
1458 {
1459 if (m == MATCH_YES)
1460 old_char_selector = 1;
1461 seen_length = 1;
1462 goto done;
1463 }
1464
1465 m = gfc_match_char ('(');
1466 if (m != MATCH_YES)
1467 {
1468 m = MATCH_YES; /* character without length is a single char */
1469 goto done;
1470 }
1471
1472 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1473 if (gfc_match (" kind =") == MATCH_YES)
1474 {
1475 m = gfc_match_small_int (&kind);
1476 if (m == MATCH_ERROR)
1477 goto done;
1478 if (m == MATCH_NO)
1479 goto syntax;
1480
1481 if (gfc_match (" , len =") == MATCH_NO)
1482 goto rparen;
1483
1484 m = char_len_param_value (&len);
1485 if (m == MATCH_NO)
1486 goto syntax;
1487 if (m == MATCH_ERROR)
1488 goto done;
1489 seen_length = 1;
1490
1491 goto rparen;
1492 }
1493
1494 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
1495 if (gfc_match (" len =") == MATCH_YES)
1496 {
1497 m = char_len_param_value (&len);
1498 if (m == MATCH_NO)
1499 goto syntax;
1500 if (m == MATCH_ERROR)
1501 goto done;
1502 seen_length = 1;
1503
1504 if (gfc_match_char (')') == MATCH_YES)
1505 goto done;
1506
1507 if (gfc_match (" , kind =") != MATCH_YES)
1508 goto syntax;
1509
1510 gfc_match_small_int (&kind);
1511
1512 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1513 {
1514 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1515 return MATCH_YES;
1516 }
1517
1518 goto rparen;
1519 }
1520
1521 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1522 m = char_len_param_value (&len);
1523 if (m == MATCH_NO)
1524 goto syntax;
1525 if (m == MATCH_ERROR)
1526 goto done;
1527 seen_length = 1;
1528
1529 m = gfc_match_char (')');
1530 if (m == MATCH_YES)
1531 goto done;
1532
1533 if (gfc_match_char (',') != MATCH_YES)
1534 goto syntax;
1535
1536 gfc_match (" kind ="); /* Gobble optional text */
1537
1538 m = gfc_match_small_int (&kind);
1539 if (m == MATCH_ERROR)
1540 goto done;
1541 if (m == MATCH_NO)
1542 goto syntax;
1543
1544 rparen:
1545 /* Require a right-paren at this point. */
1546 m = gfc_match_char (')');
1547 if (m == MATCH_YES)
1548 goto done;
1549
1550 syntax:
1551 gfc_error ("Syntax error in CHARACTER declaration at %C");
1552 m = MATCH_ERROR;
1553
1554 done:
1555 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1556 {
1557 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1558 m = MATCH_ERROR;
1559 }
1560
1561 if (m != MATCH_YES)
1562 {
1563 gfc_free_expr (len);
1564 return m;
1565 }
1566
1567 /* Do some final massaging of the length values. */
1568 cl = gfc_get_charlen ();
1569 cl->next = gfc_current_ns->cl_list;
1570 gfc_current_ns->cl_list = cl;
1571
1572 if (seen_length == 0)
1573 cl->length = gfc_int_expr (1);
1574 else
1575 {
1576 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1577 cl->length = len;
1578 else
1579 {
1580 gfc_free_expr (len);
1581 cl->length = gfc_int_expr (0);
1582 }
1583 }
1584
1585 ts->cl = cl;
1586 ts->kind = kind;
1587
1588 return MATCH_YES;
1589 }
1590
1591
1592 /* Matches a type specification. If successful, sets the ts structure
1593 to the matched specification. This is necessary for FUNCTION and
1594 IMPLICIT statements.
1595
1596 If implicit_flag is nonzero, then we don't check for the optional
1597 kind specification. Not doing so is needed for matching an IMPLICIT
1598 statement correctly. */
1599
1600 static match
1601 match_type_spec (gfc_typespec * ts, int implicit_flag)
1602 {
1603 char name[GFC_MAX_SYMBOL_LEN + 1];
1604 gfc_symbol *sym;
1605 match m;
1606 int c;
1607
1608 gfc_clear_ts (ts);
1609
1610 if (gfc_match (" byte") == MATCH_YES)
1611 {
1612 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1613 == FAILURE)
1614 return MATCH_ERROR;
1615
1616 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1617 {
1618 gfc_error ("BYTE type used at %C "
1619 "is not available on the target machine");
1620 return MATCH_ERROR;
1621 }
1622
1623 ts->type = BT_INTEGER;
1624 ts->kind = 1;
1625 return MATCH_YES;
1626 }
1627
1628 if (gfc_match (" integer") == MATCH_YES)
1629 {
1630 ts->type = BT_INTEGER;
1631 ts->kind = gfc_default_integer_kind;
1632 goto get_kind;
1633 }
1634
1635 if (gfc_match (" character") == MATCH_YES)
1636 {
1637 ts->type = BT_CHARACTER;
1638 if (implicit_flag == 0)
1639 return match_char_spec (ts);
1640 else
1641 return MATCH_YES;
1642 }
1643
1644 if (gfc_match (" real") == MATCH_YES)
1645 {
1646 ts->type = BT_REAL;
1647 ts->kind = gfc_default_real_kind;
1648 goto get_kind;
1649 }
1650
1651 if (gfc_match (" double precision") == MATCH_YES)
1652 {
1653 ts->type = BT_REAL;
1654 ts->kind = gfc_default_double_kind;
1655 return MATCH_YES;
1656 }
1657
1658 if (gfc_match (" complex") == MATCH_YES)
1659 {
1660 ts->type = BT_COMPLEX;
1661 ts->kind = gfc_default_complex_kind;
1662 goto get_kind;
1663 }
1664
1665 if (gfc_match (" double complex") == MATCH_YES)
1666 {
1667 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1668 "conform to the Fortran 95 standard") == FAILURE)
1669 return MATCH_ERROR;
1670
1671 ts->type = BT_COMPLEX;
1672 ts->kind = gfc_default_double_kind;
1673 return MATCH_YES;
1674 }
1675
1676 if (gfc_match (" logical") == MATCH_YES)
1677 {
1678 ts->type = BT_LOGICAL;
1679 ts->kind = gfc_default_logical_kind;
1680 goto get_kind;
1681 }
1682
1683 m = gfc_match (" type ( %n )", name);
1684 if (m != MATCH_YES)
1685 return m;
1686
1687 /* Search for the name but allow the components to be defined later. */
1688 if (gfc_get_ha_symbol (name, &sym))
1689 {
1690 gfc_error ("Type name '%s' at %C is ambiguous", name);
1691 return MATCH_ERROR;
1692 }
1693
1694 if (sym->attr.flavor != FL_DERIVED
1695 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1696 return MATCH_ERROR;
1697
1698 ts->type = BT_DERIVED;
1699 ts->kind = 0;
1700 ts->derived = sym;
1701
1702 return MATCH_YES;
1703
1704 get_kind:
1705 /* For all types except double, derived and character, look for an
1706 optional kind specifier. MATCH_NO is actually OK at this point. */
1707 if (implicit_flag == 1)
1708 return MATCH_YES;
1709
1710 if (gfc_current_form == FORM_FREE)
1711 {
1712 c = gfc_peek_char();
1713 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1714 && c != ':' && c != ',')
1715 return MATCH_NO;
1716 }
1717
1718 m = gfc_match_kind_spec (ts);
1719 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1720 m = gfc_match_old_kind_spec (ts);
1721
1722 if (m == MATCH_NO)
1723 m = MATCH_YES; /* No kind specifier found. */
1724
1725 return m;
1726 }
1727
1728
1729 /* Match an IMPLICIT NONE statement. Actually, this statement is
1730 already matched in parse.c, or we would not end up here in the
1731 first place. So the only thing we need to check, is if there is
1732 trailing garbage. If not, the match is successful. */
1733
1734 match
1735 gfc_match_implicit_none (void)
1736 {
1737
1738 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1739 }
1740
1741
1742 /* Match the letter range(s) of an IMPLICIT statement. */
1743
1744 static match
1745 match_implicit_range (void)
1746 {
1747 int c, c1, c2, inner;
1748 locus cur_loc;
1749
1750 cur_loc = gfc_current_locus;
1751
1752 gfc_gobble_whitespace ();
1753 c = gfc_next_char ();
1754 if (c != '(')
1755 {
1756 gfc_error ("Missing character range in IMPLICIT at %C");
1757 goto bad;
1758 }
1759
1760 inner = 1;
1761 while (inner)
1762 {
1763 gfc_gobble_whitespace ();
1764 c1 = gfc_next_char ();
1765 if (!ISALPHA (c1))
1766 goto bad;
1767
1768 gfc_gobble_whitespace ();
1769 c = gfc_next_char ();
1770
1771 switch (c)
1772 {
1773 case ')':
1774 inner = 0; /* Fall through */
1775
1776 case ',':
1777 c2 = c1;
1778 break;
1779
1780 case '-':
1781 gfc_gobble_whitespace ();
1782 c2 = gfc_next_char ();
1783 if (!ISALPHA (c2))
1784 goto bad;
1785
1786 gfc_gobble_whitespace ();
1787 c = gfc_next_char ();
1788
1789 if ((c != ',') && (c != ')'))
1790 goto bad;
1791 if (c == ')')
1792 inner = 0;
1793
1794 break;
1795
1796 default:
1797 goto bad;
1798 }
1799
1800 if (c1 > c2)
1801 {
1802 gfc_error ("Letters must be in alphabetic order in "
1803 "IMPLICIT statement at %C");
1804 goto bad;
1805 }
1806
1807 /* See if we can add the newly matched range to the pending
1808 implicits from this IMPLICIT statement. We do not check for
1809 conflicts with whatever earlier IMPLICIT statements may have
1810 set. This is done when we've successfully finished matching
1811 the current one. */
1812 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1813 goto bad;
1814 }
1815
1816 return MATCH_YES;
1817
1818 bad:
1819 gfc_syntax_error (ST_IMPLICIT);
1820
1821 gfc_current_locus = cur_loc;
1822 return MATCH_ERROR;
1823 }
1824
1825
1826 /* Match an IMPLICIT statement, storing the types for
1827 gfc_set_implicit() if the statement is accepted by the parser.
1828 There is a strange looking, but legal syntactic construction
1829 possible. It looks like:
1830
1831 IMPLICIT INTEGER (a-b) (c-d)
1832
1833 This is legal if "a-b" is a constant expression that happens to
1834 equal one of the legal kinds for integers. The real problem
1835 happens with an implicit specification that looks like:
1836
1837 IMPLICIT INTEGER (a-b)
1838
1839 In this case, a typespec matcher that is "greedy" (as most of the
1840 matchers are) gobbles the character range as a kindspec, leaving
1841 nothing left. We therefore have to go a bit more slowly in the
1842 matching process by inhibiting the kindspec checking during
1843 typespec matching and checking for a kind later. */
1844
1845 match
1846 gfc_match_implicit (void)
1847 {
1848 gfc_typespec ts;
1849 locus cur_loc;
1850 int c;
1851 match m;
1852
1853 /* We don't allow empty implicit statements. */
1854 if (gfc_match_eos () == MATCH_YES)
1855 {
1856 gfc_error ("Empty IMPLICIT statement at %C");
1857 return MATCH_ERROR;
1858 }
1859
1860 do
1861 {
1862 /* First cleanup. */
1863 gfc_clear_new_implicit ();
1864
1865 /* A basic type is mandatory here. */
1866 m = match_type_spec (&ts, 1);
1867 if (m == MATCH_ERROR)
1868 goto error;
1869 if (m == MATCH_NO)
1870 goto syntax;
1871
1872 cur_loc = gfc_current_locus;
1873 m = match_implicit_range ();
1874
1875 if (m == MATCH_YES)
1876 {
1877 /* We may have <TYPE> (<RANGE>). */
1878 gfc_gobble_whitespace ();
1879 c = gfc_next_char ();
1880 if ((c == '\n') || (c == ','))
1881 {
1882 /* Check for CHARACTER with no length parameter. */
1883 if (ts.type == BT_CHARACTER && !ts.cl)
1884 {
1885 ts.kind = gfc_default_character_kind;
1886 ts.cl = gfc_get_charlen ();
1887 ts.cl->next = gfc_current_ns->cl_list;
1888 gfc_current_ns->cl_list = ts.cl;
1889 ts.cl->length = gfc_int_expr (1);
1890 }
1891
1892 /* Record the Successful match. */
1893 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1894 return MATCH_ERROR;
1895 continue;
1896 }
1897
1898 gfc_current_locus = cur_loc;
1899 }
1900
1901 /* Discard the (incorrectly) matched range. */
1902 gfc_clear_new_implicit ();
1903
1904 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1905 if (ts.type == BT_CHARACTER)
1906 m = match_char_spec (&ts);
1907 else
1908 {
1909 m = gfc_match_kind_spec (&ts);
1910 if (m == MATCH_NO)
1911 {
1912 m = gfc_match_old_kind_spec (&ts);
1913 if (m == MATCH_ERROR)
1914 goto error;
1915 if (m == MATCH_NO)
1916 goto syntax;
1917 }
1918 }
1919 if (m == MATCH_ERROR)
1920 goto error;
1921
1922 m = match_implicit_range ();
1923 if (m == MATCH_ERROR)
1924 goto error;
1925 if (m == MATCH_NO)
1926 goto syntax;
1927
1928 gfc_gobble_whitespace ();
1929 c = gfc_next_char ();
1930 if ((c != '\n') && (c != ','))
1931 goto syntax;
1932
1933 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1934 return MATCH_ERROR;
1935 }
1936 while (c == ',');
1937
1938 return MATCH_YES;
1939
1940 syntax:
1941 gfc_syntax_error (ST_IMPLICIT);
1942
1943 error:
1944 return MATCH_ERROR;
1945 }
1946
1947
1948 /* Matches an attribute specification including array specs. If
1949 successful, leaves the variables current_attr and current_as
1950 holding the specification. Also sets the colon_seen variable for
1951 later use by matchers associated with initializations.
1952
1953 This subroutine is a little tricky in the sense that we don't know
1954 if we really have an attr-spec until we hit the double colon.
1955 Until that time, we can only return MATCH_NO. This forces us to
1956 check for duplicate specification at this level. */
1957
1958 static match
1959 match_attr_spec (void)
1960 {
1961
1962 /* Modifiers that can exist in a type statement. */
1963 typedef enum
1964 { GFC_DECL_BEGIN = 0,
1965 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1966 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1967 DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1968 DECL_TARGET, DECL_COLON, DECL_NONE,
1969 GFC_DECL_END /* Sentinel */
1970 }
1971 decl_types;
1972
1973 /* GFC_DECL_END is the sentinel, index starts at 0. */
1974 #define NUM_DECL GFC_DECL_END
1975
1976 static mstring decls[] = {
1977 minit (", allocatable", DECL_ALLOCATABLE),
1978 minit (", dimension", DECL_DIMENSION),
1979 minit (", external", DECL_EXTERNAL),
1980 minit (", intent ( in )", DECL_IN),
1981 minit (", intent ( out )", DECL_OUT),
1982 minit (", intent ( in out )", DECL_INOUT),
1983 minit (", intrinsic", DECL_INTRINSIC),
1984 minit (", optional", DECL_OPTIONAL),
1985 minit (", parameter", DECL_PARAMETER),
1986 minit (", pointer", DECL_POINTER),
1987 minit (", private", DECL_PRIVATE),
1988 minit (", public", DECL_PUBLIC),
1989 minit (", save", DECL_SAVE),
1990 minit (", target", DECL_TARGET),
1991 minit ("::", DECL_COLON),
1992 minit (NULL, DECL_NONE)
1993 };
1994
1995 locus start, seen_at[NUM_DECL];
1996 int seen[NUM_DECL];
1997 decl_types d;
1998 const char *attr;
1999 match m;
2000 try t;
2001
2002 gfc_clear_attr (&current_attr);
2003 start = gfc_current_locus;
2004
2005 current_as = NULL;
2006 colon_seen = 0;
2007
2008 /* See if we get all of the keywords up to the final double colon. */
2009 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2010 seen[d] = 0;
2011
2012 for (;;)
2013 {
2014 d = (decl_types) gfc_match_strings (decls);
2015 if (d == DECL_NONE || d == DECL_COLON)
2016 break;
2017
2018 if (gfc_current_state () == COMP_ENUM)
2019 {
2020 gfc_error ("Enumerator cannot have attributes %C");
2021 return MATCH_ERROR;
2022 }
2023
2024 seen[d]++;
2025 seen_at[d] = gfc_current_locus;
2026
2027 if (d == DECL_DIMENSION)
2028 {
2029 m = gfc_match_array_spec (&current_as);
2030
2031 if (m == MATCH_NO)
2032 {
2033 gfc_error ("Missing dimension specification at %C");
2034 m = MATCH_ERROR;
2035 }
2036
2037 if (m == MATCH_ERROR)
2038 goto cleanup;
2039 }
2040 }
2041
2042 /* If we are parsing an enumeration and have ensured that no other
2043 attributes are present we can now set the parameter attribute. */
2044 if (gfc_current_state () == COMP_ENUM)
2045 {
2046 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
2047 if (t == FAILURE)
2048 {
2049 m = MATCH_ERROR;
2050 goto cleanup;
2051 }
2052 }
2053
2054 /* No double colon, so assume that we've been looking at something
2055 else the whole time. */
2056 if (d == DECL_NONE)
2057 {
2058 m = MATCH_NO;
2059 goto cleanup;
2060 }
2061
2062 /* Since we've seen a double colon, we have to be looking at an
2063 attr-spec. This means that we can now issue errors. */
2064 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2065 if (seen[d] > 1)
2066 {
2067 switch (d)
2068 {
2069 case DECL_ALLOCATABLE:
2070 attr = "ALLOCATABLE";
2071 break;
2072 case DECL_DIMENSION:
2073 attr = "DIMENSION";
2074 break;
2075 case DECL_EXTERNAL:
2076 attr = "EXTERNAL";
2077 break;
2078 case DECL_IN:
2079 attr = "INTENT (IN)";
2080 break;
2081 case DECL_OUT:
2082 attr = "INTENT (OUT)";
2083 break;
2084 case DECL_INOUT:
2085 attr = "INTENT (IN OUT)";
2086 break;
2087 case DECL_INTRINSIC:
2088 attr = "INTRINSIC";
2089 break;
2090 case DECL_OPTIONAL:
2091 attr = "OPTIONAL";
2092 break;
2093 case DECL_PARAMETER:
2094 attr = "PARAMETER";
2095 break;
2096 case DECL_POINTER:
2097 attr = "POINTER";
2098 break;
2099 case DECL_PRIVATE:
2100 attr = "PRIVATE";
2101 break;
2102 case DECL_PUBLIC:
2103 attr = "PUBLIC";
2104 break;
2105 case DECL_SAVE:
2106 attr = "SAVE";
2107 break;
2108 case DECL_TARGET:
2109 attr = "TARGET";
2110 break;
2111 default:
2112 attr = NULL; /* This shouldn't happen */
2113 }
2114
2115 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2116 m = MATCH_ERROR;
2117 goto cleanup;
2118 }
2119
2120 /* Now that we've dealt with duplicate attributes, add the attributes
2121 to the current attribute. */
2122 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2123 {
2124 if (seen[d] == 0)
2125 continue;
2126
2127 if (gfc_current_state () == COMP_DERIVED
2128 && d != DECL_DIMENSION && d != DECL_POINTER
2129 && d != DECL_COLON && d != DECL_NONE)
2130 {
2131
2132 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2133 &seen_at[d]);
2134 m = MATCH_ERROR;
2135 goto cleanup;
2136 }
2137
2138 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2139 && gfc_current_state () != COMP_MODULE)
2140 {
2141 if (d == DECL_PRIVATE)
2142 attr = "PRIVATE";
2143 else
2144 attr = "PUBLIC";
2145
2146 gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2147 attr, &seen_at[d]);
2148 m = MATCH_ERROR;
2149 goto cleanup;
2150 }
2151
2152 switch (d)
2153 {
2154 case DECL_ALLOCATABLE:
2155 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2156 break;
2157
2158 case DECL_DIMENSION:
2159 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2160 break;
2161
2162 case DECL_EXTERNAL:
2163 t = gfc_add_external (&current_attr, &seen_at[d]);
2164 break;
2165
2166 case DECL_IN:
2167 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2168 break;
2169
2170 case DECL_OUT:
2171 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2172 break;
2173
2174 case DECL_INOUT:
2175 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2176 break;
2177
2178 case DECL_INTRINSIC:
2179 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2180 break;
2181
2182 case DECL_OPTIONAL:
2183 t = gfc_add_optional (&current_attr, &seen_at[d]);
2184 break;
2185
2186 case DECL_PARAMETER:
2187 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2188 break;
2189
2190 case DECL_POINTER:
2191 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2192 break;
2193
2194 case DECL_PRIVATE:
2195 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2196 &seen_at[d]);
2197 break;
2198
2199 case DECL_PUBLIC:
2200 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2201 &seen_at[d]);
2202 break;
2203
2204 case DECL_SAVE:
2205 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2206 break;
2207
2208 case DECL_TARGET:
2209 t = gfc_add_target (&current_attr, &seen_at[d]);
2210 break;
2211
2212 default:
2213 gfc_internal_error ("match_attr_spec(): Bad attribute");
2214 }
2215
2216 if (t == FAILURE)
2217 {
2218 m = MATCH_ERROR;
2219 goto cleanup;
2220 }
2221 }
2222
2223 colon_seen = 1;
2224 return MATCH_YES;
2225
2226 cleanup:
2227 gfc_current_locus = start;
2228 gfc_free_array_spec (current_as);
2229 current_as = NULL;
2230 return m;
2231 }
2232
2233
2234 /* Match a data declaration statement. */
2235
2236 match
2237 gfc_match_data_decl (void)
2238 {
2239 gfc_symbol *sym;
2240 match m;
2241 int elem;
2242
2243 m = match_type_spec (&current_ts, 0);
2244 if (m != MATCH_YES)
2245 return m;
2246
2247 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2248 {
2249 sym = gfc_use_derived (current_ts.derived);
2250
2251 if (sym == NULL)
2252 {
2253 m = MATCH_ERROR;
2254 goto cleanup;
2255 }
2256
2257 current_ts.derived = sym;
2258 }
2259
2260 m = match_attr_spec ();
2261 if (m == MATCH_ERROR)
2262 {
2263 m = MATCH_NO;
2264 goto cleanup;
2265 }
2266
2267 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2268 {
2269
2270 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2271 goto ok;
2272
2273 gfc_find_symbol (current_ts.derived->name,
2274 current_ts.derived->ns->parent, 1, &sym);
2275
2276 /* Any symbol that we find had better be a type definition
2277 which has its components defined. */
2278 if (sym != NULL && sym->attr.flavor == FL_DERIVED
2279 && current_ts.derived->components != NULL)
2280 goto ok;
2281
2282 /* Now we have an error, which we signal, and then fix up
2283 because the knock-on is plain and simple confusing. */
2284 gfc_error_now ("Derived type at %C has not been previously defined "
2285 "and so cannot appear in a derived type definition.");
2286 current_attr.pointer = 1;
2287 goto ok;
2288 }
2289
2290 ok:
2291 /* If we have an old-style character declaration, and no new-style
2292 attribute specifications, then there a comma is optional between
2293 the type specification and the variable list. */
2294 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2295 gfc_match_char (',');
2296
2297 /* Give the types/attributes to symbols that follow. Give the element
2298 a number so that repeat character length expressions can be copied. */
2299 elem = 1;
2300 for (;;)
2301 {
2302 m = variable_decl (elem++);
2303 if (m == MATCH_ERROR)
2304 goto cleanup;
2305 if (m == MATCH_NO)
2306 break;
2307
2308 if (gfc_match_eos () == MATCH_YES)
2309 goto cleanup;
2310 if (gfc_match_char (',') != MATCH_YES)
2311 break;
2312 }
2313
2314 gfc_error ("Syntax error in data declaration at %C");
2315 m = MATCH_ERROR;
2316
2317 cleanup:
2318 gfc_free_array_spec (current_as);
2319 current_as = NULL;
2320 return m;
2321 }
2322
2323
2324 /* Match a prefix associated with a function or subroutine
2325 declaration. If the typespec pointer is nonnull, then a typespec
2326 can be matched. Note that if nothing matches, MATCH_YES is
2327 returned (the null string was matched). */
2328
2329 static match
2330 match_prefix (gfc_typespec * ts)
2331 {
2332 int seen_type;
2333
2334 gfc_clear_attr (&current_attr);
2335 seen_type = 0;
2336
2337 loop:
2338 if (!seen_type && ts != NULL
2339 && match_type_spec (ts, 0) == MATCH_YES
2340 && gfc_match_space () == MATCH_YES)
2341 {
2342
2343 seen_type = 1;
2344 goto loop;
2345 }
2346
2347 if (gfc_match ("elemental% ") == MATCH_YES)
2348 {
2349 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2350 return MATCH_ERROR;
2351
2352 goto loop;
2353 }
2354
2355 if (gfc_match ("pure% ") == MATCH_YES)
2356 {
2357 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2358 return MATCH_ERROR;
2359
2360 goto loop;
2361 }
2362
2363 if (gfc_match ("recursive% ") == MATCH_YES)
2364 {
2365 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2366 return MATCH_ERROR;
2367
2368 goto loop;
2369 }
2370
2371 /* At this point, the next item is not a prefix. */
2372 return MATCH_YES;
2373 }
2374
2375
2376 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2377
2378 static try
2379 copy_prefix (symbol_attribute * dest, locus * where)
2380 {
2381
2382 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2383 return FAILURE;
2384
2385 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2386 return FAILURE;
2387
2388 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2389 return FAILURE;
2390
2391 return SUCCESS;
2392 }
2393
2394
2395 /* Match a formal argument list. */
2396
2397 match
2398 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2399 {
2400 gfc_formal_arglist *head, *tail, *p, *q;
2401 char name[GFC_MAX_SYMBOL_LEN + 1];
2402 gfc_symbol *sym;
2403 match m;
2404
2405 head = tail = NULL;
2406
2407 if (gfc_match_char ('(') != MATCH_YES)
2408 {
2409 if (null_flag)
2410 goto ok;
2411 return MATCH_NO;
2412 }
2413
2414 if (gfc_match_char (')') == MATCH_YES)
2415 goto ok;
2416
2417 for (;;)
2418 {
2419 if (gfc_match_char ('*') == MATCH_YES)
2420 sym = NULL;
2421 else
2422 {
2423 m = gfc_match_name (name);
2424 if (m != MATCH_YES)
2425 goto cleanup;
2426
2427 if (gfc_get_symbol (name, NULL, &sym))
2428 goto cleanup;
2429 }
2430
2431 p = gfc_get_formal_arglist ();
2432
2433 if (head == NULL)
2434 head = tail = p;
2435 else
2436 {
2437 tail->next = p;
2438 tail = p;
2439 }
2440
2441 tail->sym = sym;
2442
2443 /* We don't add the VARIABLE flavor because the name could be a
2444 dummy procedure. We don't apply these attributes to formal
2445 arguments of statement functions. */
2446 if (sym != NULL && !st_flag
2447 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2448 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2449 {
2450 m = MATCH_ERROR;
2451 goto cleanup;
2452 }
2453
2454 /* The name of a program unit can be in a different namespace,
2455 so check for it explicitly. After the statement is accepted,
2456 the name is checked for especially in gfc_get_symbol(). */
2457 if (gfc_new_block != NULL && sym != NULL
2458 && strcmp (sym->name, gfc_new_block->name) == 0)
2459 {
2460 gfc_error ("Name '%s' at %C is the name of the procedure",
2461 sym->name);
2462 m = MATCH_ERROR;
2463 goto cleanup;
2464 }
2465
2466 if (gfc_match_char (')') == MATCH_YES)
2467 goto ok;
2468
2469 m = gfc_match_char (',');
2470 if (m != MATCH_YES)
2471 {
2472 gfc_error ("Unexpected junk in formal argument list at %C");
2473 goto cleanup;
2474 }
2475 }
2476
2477 ok:
2478 /* Check for duplicate symbols in the formal argument list. */
2479 if (head != NULL)
2480 {
2481 for (p = head; p->next; p = p->next)
2482 {
2483 if (p->sym == NULL)
2484 continue;
2485
2486 for (q = p->next; q; q = q->next)
2487 if (p->sym == q->sym)
2488 {
2489 gfc_error
2490 ("Duplicate symbol '%s' in formal argument list at %C",
2491 p->sym->name);
2492
2493 m = MATCH_ERROR;
2494 goto cleanup;
2495 }
2496 }
2497 }
2498
2499 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2500 FAILURE)
2501 {
2502 m = MATCH_ERROR;
2503 goto cleanup;
2504 }
2505
2506 return MATCH_YES;
2507
2508 cleanup:
2509 gfc_free_formal_arglist (head);
2510 return m;
2511 }
2512
2513
2514 /* Match a RESULT specification following a function declaration or
2515 ENTRY statement. Also matches the end-of-statement. */
2516
2517 static match
2518 match_result (gfc_symbol * function, gfc_symbol ** result)
2519 {
2520 char name[GFC_MAX_SYMBOL_LEN + 1];
2521 gfc_symbol *r;
2522 match m;
2523
2524 if (gfc_match (" result (") != MATCH_YES)
2525 return MATCH_NO;
2526
2527 m = gfc_match_name (name);
2528 if (m != MATCH_YES)
2529 return m;
2530
2531 if (gfc_match (" )%t") != MATCH_YES)
2532 {
2533 gfc_error ("Unexpected junk following RESULT variable at %C");
2534 return MATCH_ERROR;
2535 }
2536
2537 if (strcmp (function->name, name) == 0)
2538 {
2539 gfc_error
2540 ("RESULT variable at %C must be different than function name");
2541 return MATCH_ERROR;
2542 }
2543
2544 if (gfc_get_symbol (name, NULL, &r))
2545 return MATCH_ERROR;
2546
2547 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2548 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2549 return MATCH_ERROR;
2550
2551 *result = r;
2552
2553 return MATCH_YES;
2554 }
2555
2556
2557 /* Match a function declaration. */
2558
2559 match
2560 gfc_match_function_decl (void)
2561 {
2562 char name[GFC_MAX_SYMBOL_LEN + 1];
2563 gfc_symbol *sym, *result;
2564 locus old_loc;
2565 match m;
2566
2567 if (gfc_current_state () != COMP_NONE
2568 && gfc_current_state () != COMP_INTERFACE
2569 && gfc_current_state () != COMP_CONTAINS)
2570 return MATCH_NO;
2571
2572 gfc_clear_ts (&current_ts);
2573
2574 old_loc = gfc_current_locus;
2575
2576 m = match_prefix (&current_ts);
2577 if (m != MATCH_YES)
2578 {
2579 gfc_current_locus = old_loc;
2580 return m;
2581 }
2582
2583 if (gfc_match ("function% %n", name) != MATCH_YES)
2584 {
2585 gfc_current_locus = old_loc;
2586 return MATCH_NO;
2587 }
2588
2589 if (get_proc_name (name, &sym, false))
2590 return MATCH_ERROR;
2591 gfc_new_block = sym;
2592
2593 m = gfc_match_formal_arglist (sym, 0, 0);
2594 if (m == MATCH_NO)
2595 {
2596 gfc_error ("Expected formal argument list in function "
2597 "definition at %C");
2598 m = MATCH_ERROR;
2599 goto cleanup;
2600 }
2601 else if (m == MATCH_ERROR)
2602 goto cleanup;
2603
2604 result = NULL;
2605
2606 if (gfc_match_eos () != MATCH_YES)
2607 {
2608 /* See if a result variable is present. */
2609 m = match_result (sym, &result);
2610 if (m == MATCH_NO)
2611 gfc_error ("Unexpected junk after function declaration at %C");
2612
2613 if (m != MATCH_YES)
2614 {
2615 m = MATCH_ERROR;
2616 goto cleanup;
2617 }
2618 }
2619
2620 /* Make changes to the symbol. */
2621 m = MATCH_ERROR;
2622
2623 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2624 goto cleanup;
2625
2626 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2627 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2628 goto cleanup;
2629
2630 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2631 {
2632 gfc_error ("Function '%s' at %C already has a type of %s", name,
2633 gfc_basic_typename (sym->ts.type));
2634 goto cleanup;
2635 }
2636
2637 if (result == NULL)
2638 {
2639 sym->ts = current_ts;
2640 sym->result = sym;
2641 }
2642 else
2643 {
2644 result->ts = current_ts;
2645 sym->result = result;
2646 }
2647
2648 return MATCH_YES;
2649
2650 cleanup:
2651 gfc_current_locus = old_loc;
2652 return m;
2653 }
2654
2655 /* This is mostly a copy of parse.c(add_global_procedure) but modified to pass the
2656 name of the entry, rather than the gfc_current_block name, and to return false
2657 upon finding an existing global entry. */
2658
2659 static bool
2660 add_global_entry (const char * name, int sub)
2661 {
2662 gfc_gsymbol *s;
2663
2664 s = gfc_get_gsymbol(name);
2665
2666 if (s->defined
2667 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2668 global_used(s, NULL);
2669 else
2670 {
2671 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2672 s->where = gfc_current_locus;
2673 s->defined = 1;
2674 return true;
2675 }
2676 return false;
2677 }
2678
2679 /* Match an ENTRY statement. */
2680
2681 match
2682 gfc_match_entry (void)
2683 {
2684 gfc_symbol *proc;
2685 gfc_symbol *result;
2686 gfc_symbol *entry;
2687 char name[GFC_MAX_SYMBOL_LEN + 1];
2688 gfc_compile_state state;
2689 match m;
2690 gfc_entry_list *el;
2691 locus old_loc;
2692 bool module_procedure;
2693
2694 m = gfc_match_name (name);
2695 if (m != MATCH_YES)
2696 return m;
2697
2698 state = gfc_current_state ();
2699 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2700 {
2701 switch (state)
2702 {
2703 case COMP_PROGRAM:
2704 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2705 break;
2706 case COMP_MODULE:
2707 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2708 break;
2709 case COMP_BLOCK_DATA:
2710 gfc_error
2711 ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2712 break;
2713 case COMP_INTERFACE:
2714 gfc_error
2715 ("ENTRY statement at %C cannot appear within an INTERFACE");
2716 break;
2717 case COMP_DERIVED:
2718 gfc_error
2719 ("ENTRY statement at %C cannot appear "
2720 "within a DERIVED TYPE block");
2721 break;
2722 case COMP_IF:
2723 gfc_error
2724 ("ENTRY statement at %C cannot appear within an IF-THEN block");
2725 break;
2726 case COMP_DO:
2727 gfc_error
2728 ("ENTRY statement at %C cannot appear within a DO block");
2729 break;
2730 case COMP_SELECT:
2731 gfc_error
2732 ("ENTRY statement at %C cannot appear within a SELECT block");
2733 break;
2734 case COMP_FORALL:
2735 gfc_error
2736 ("ENTRY statement at %C cannot appear within a FORALL block");
2737 break;
2738 case COMP_WHERE:
2739 gfc_error
2740 ("ENTRY statement at %C cannot appear within a WHERE block");
2741 break;
2742 case COMP_CONTAINS:
2743 gfc_error
2744 ("ENTRY statement at %C cannot appear "
2745 "within a contained subprogram");
2746 break;
2747 default:
2748 gfc_internal_error ("gfc_match_entry(): Bad state");
2749 }
2750 return MATCH_ERROR;
2751 }
2752
2753 module_procedure = gfc_current_ns->parent != NULL
2754 && gfc_current_ns->parent->proc_name
2755 && gfc_current_ns->parent->proc_name->attr.flavor == FL_MODULE;
2756
2757 if (gfc_current_ns->parent != NULL
2758 && gfc_current_ns->parent->proc_name
2759 && !module_procedure)
2760 {
2761 gfc_error("ENTRY statement at %C cannot appear in a "
2762 "contained procedure");
2763 return MATCH_ERROR;
2764 }
2765
2766 /* Module function entries need special care in get_proc_name
2767 because previous references within the function will have
2768 created symbols attached to the current namespace. */
2769 if (get_proc_name (name, &entry,
2770 gfc_current_ns->parent != NULL
2771 && module_procedure
2772 && gfc_current_ns->proc_name->attr.function))
2773 return MATCH_ERROR;
2774
2775 proc = gfc_current_block ();
2776
2777 if (state == COMP_SUBROUTINE)
2778 {
2779 /* An entry in a subroutine. */
2780 if (!add_global_entry (name, 1))
2781 return MATCH_ERROR;
2782
2783 m = gfc_match_formal_arglist (entry, 0, 1);
2784 if (m != MATCH_YES)
2785 return MATCH_ERROR;
2786
2787 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2788 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2789 return MATCH_ERROR;
2790 }
2791 else
2792 {
2793 /* An entry in a function.
2794 We need to take special care because writing
2795 ENTRY f()
2796 as
2797 ENTRY f
2798 is allowed, whereas
2799 ENTRY f() RESULT (r)
2800 can't be written as
2801 ENTRY f RESULT (r). */
2802 if (!add_global_entry (name, 0))
2803 return MATCH_ERROR;
2804
2805 old_loc = gfc_current_locus;
2806 if (gfc_match_eos () == MATCH_YES)
2807 {
2808 gfc_current_locus = old_loc;
2809 /* Match the empty argument list, and add the interface to
2810 the symbol. */
2811 m = gfc_match_formal_arglist (entry, 0, 1);
2812 }
2813 else
2814 m = gfc_match_formal_arglist (entry, 0, 0);
2815
2816 if (m != MATCH_YES)
2817 return MATCH_ERROR;
2818
2819 result = NULL;
2820
2821 if (gfc_match_eos () == MATCH_YES)
2822 {
2823 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2824 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2825 return MATCH_ERROR;
2826
2827 entry->result = entry;
2828 }
2829 else
2830 {
2831 m = match_result (proc, &result);
2832 if (m == MATCH_NO)
2833 gfc_syntax_error (ST_ENTRY);
2834 if (m != MATCH_YES)
2835 return MATCH_ERROR;
2836
2837 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2838 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2839 || gfc_add_function (&entry->attr, result->name,
2840 NULL) == FAILURE)
2841 return MATCH_ERROR;
2842
2843 entry->result = result;
2844 }
2845
2846 if (proc->attr.recursive && result == NULL)
2847 {
2848 gfc_error ("RESULT attribute required in ENTRY statement at %C");
2849 return MATCH_ERROR;
2850 }
2851 }
2852
2853 if (gfc_match_eos () != MATCH_YES)
2854 {
2855 gfc_syntax_error (ST_ENTRY);
2856 return MATCH_ERROR;
2857 }
2858
2859 entry->attr.recursive = proc->attr.recursive;
2860 entry->attr.elemental = proc->attr.elemental;
2861 entry->attr.pure = proc->attr.pure;
2862
2863 el = gfc_get_entry_list ();
2864 el->sym = entry;
2865 el->next = gfc_current_ns->entries;
2866 gfc_current_ns->entries = el;
2867 if (el->next)
2868 el->id = el->next->id + 1;
2869 else
2870 el->id = 1;
2871
2872 new_st.op = EXEC_ENTRY;
2873 new_st.ext.entry = el;
2874
2875 return MATCH_YES;
2876 }
2877
2878
2879 /* Match a subroutine statement, including optional prefixes. */
2880
2881 match
2882 gfc_match_subroutine (void)
2883 {
2884 char name[GFC_MAX_SYMBOL_LEN + 1];
2885 gfc_symbol *sym;
2886 match m;
2887
2888 if (gfc_current_state () != COMP_NONE
2889 && gfc_current_state () != COMP_INTERFACE
2890 && gfc_current_state () != COMP_CONTAINS)
2891 return MATCH_NO;
2892
2893 m = match_prefix (NULL);
2894 if (m != MATCH_YES)
2895 return m;
2896
2897 m = gfc_match ("subroutine% %n", name);
2898 if (m != MATCH_YES)
2899 return m;
2900
2901 if (get_proc_name (name, &sym, false))
2902 return MATCH_ERROR;
2903 gfc_new_block = sym;
2904
2905 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2906 return MATCH_ERROR;
2907
2908 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2909 return MATCH_ERROR;
2910
2911 if (gfc_match_eos () != MATCH_YES)
2912 {
2913 gfc_syntax_error (ST_SUBROUTINE);
2914 return MATCH_ERROR;
2915 }
2916
2917 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2918 return MATCH_ERROR;
2919
2920 return MATCH_YES;
2921 }
2922
2923
2924 /* Return nonzero if we're currently compiling a contained procedure. */
2925
2926 static int
2927 contained_procedure (void)
2928 {
2929 gfc_state_data *s;
2930
2931 for (s=gfc_state_stack; s; s=s->previous)
2932 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2933 && s->previous != NULL
2934 && s->previous->state == COMP_CONTAINS)
2935 return 1;
2936
2937 return 0;
2938 }
2939
2940 /* Set the kind of each enumerator. The kind is selected such that it is
2941 interoperable with the corresponding C enumeration type, making
2942 sure that -fshort-enums is honored. */
2943
2944 static void
2945 set_enum_kind(void)
2946 {
2947 enumerator_history *current_history = NULL;
2948 int kind;
2949 int i;
2950
2951 if (max_enum == NULL || enum_history == NULL)
2952 return;
2953
2954 if (!gfc_option.fshort_enums)
2955 return;
2956
2957 i = 0;
2958 do
2959 {
2960 kind = gfc_integer_kinds[i++].kind;
2961 }
2962 while (kind < gfc_c_int_kind
2963 && gfc_check_integer_range (max_enum->initializer->value.integer,
2964 kind) != ARITH_OK);
2965
2966 current_history = enum_history;
2967 while (current_history != NULL)
2968 {
2969 current_history->sym->ts.kind = kind;
2970 current_history = current_history->next;
2971 }
2972 }
2973
2974 /* Match any of the various end-block statements. Returns the type of
2975 END to the caller. The END INTERFACE, END IF, END DO and END
2976 SELECT statements cannot be replaced by a single END statement. */
2977
2978 match
2979 gfc_match_end (gfc_statement * st)
2980 {
2981 char name[GFC_MAX_SYMBOL_LEN + 1];
2982 gfc_compile_state state;
2983 locus old_loc;
2984 const char *block_name;
2985 const char *target;
2986 int eos_ok;
2987 match m;
2988
2989 old_loc = gfc_current_locus;
2990 if (gfc_match ("end") != MATCH_YES)
2991 return MATCH_NO;
2992
2993 state = gfc_current_state ();
2994 block_name =
2995 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2996
2997 if (state == COMP_CONTAINS)
2998 {
2999 state = gfc_state_stack->previous->state;
3000 block_name = gfc_state_stack->previous->sym == NULL ? NULL
3001 : gfc_state_stack->previous->sym->name;
3002 }
3003
3004 switch (state)
3005 {
3006 case COMP_NONE:
3007 case COMP_PROGRAM:
3008 *st = ST_END_PROGRAM;
3009 target = " program";
3010 eos_ok = 1;
3011 break;
3012
3013 case COMP_SUBROUTINE:
3014 *st = ST_END_SUBROUTINE;
3015 target = " subroutine";
3016 eos_ok = !contained_procedure ();
3017 break;
3018
3019 case COMP_FUNCTION:
3020 *st = ST_END_FUNCTION;
3021 target = " function";
3022 eos_ok = !contained_procedure ();
3023 break;
3024
3025 case COMP_BLOCK_DATA:
3026 *st = ST_END_BLOCK_DATA;
3027 target = " block data";
3028 eos_ok = 1;
3029 break;
3030
3031 case COMP_MODULE:
3032 *st = ST_END_MODULE;
3033 target = " module";
3034 eos_ok = 1;
3035 break;
3036
3037 case COMP_INTERFACE:
3038 *st = ST_END_INTERFACE;
3039 target = " interface";
3040 eos_ok = 0;
3041 break;
3042
3043 case COMP_DERIVED:
3044 *st = ST_END_TYPE;
3045 target = " type";
3046 eos_ok = 0;
3047 break;
3048
3049 case COMP_IF:
3050 *st = ST_ENDIF;
3051 target = " if";
3052 eos_ok = 0;
3053 break;
3054
3055 case COMP_DO:
3056 *st = ST_ENDDO;
3057 target = " do";
3058 eos_ok = 0;
3059 break;
3060
3061 case COMP_SELECT:
3062 *st = ST_END_SELECT;
3063 target = " select";
3064 eos_ok = 0;
3065 break;
3066
3067 case COMP_FORALL:
3068 *st = ST_END_FORALL;
3069 target = " forall";
3070 eos_ok = 0;
3071 break;
3072
3073 case COMP_WHERE:
3074 *st = ST_END_WHERE;
3075 target = " where";
3076 eos_ok = 0;
3077 break;
3078
3079 case COMP_ENUM:
3080 *st = ST_END_ENUM;
3081 target = " enum";
3082 eos_ok = 0;
3083 last_initializer = NULL;
3084 set_enum_kind ();
3085 gfc_free_enum_history ();
3086 break;
3087
3088 default:
3089 gfc_error ("Unexpected END statement at %C");
3090 goto cleanup;
3091 }
3092
3093 if (gfc_match_eos () == MATCH_YES)
3094 {
3095 if (!eos_ok)
3096 {
3097 /* We would have required END [something] */
3098 gfc_error ("%s statement expected at %L",
3099 gfc_ascii_statement (*st), &old_loc);
3100 goto cleanup;
3101 }
3102
3103 return MATCH_YES;
3104 }
3105
3106 /* Verify that we've got the sort of end-block that we're expecting. */
3107 if (gfc_match (target) != MATCH_YES)
3108 {
3109 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3110 goto cleanup;
3111 }
3112
3113 /* If we're at the end, make sure a block name wasn't required. */
3114 if (gfc_match_eos () == MATCH_YES)
3115 {
3116
3117 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3118 return MATCH_YES;
3119
3120 if (gfc_current_block () == NULL)
3121 return MATCH_YES;
3122
3123 gfc_error ("Expected block name of '%s' in %s statement at %C",
3124 block_name, gfc_ascii_statement (*st));
3125
3126 return MATCH_ERROR;
3127 }
3128
3129 /* END INTERFACE has a special handler for its several possible endings. */
3130 if (*st == ST_END_INTERFACE)
3131 return gfc_match_end_interface ();
3132
3133 /* We haven't hit the end of statement, so what is left must be an end-name. */
3134 m = gfc_match_space ();
3135 if (m == MATCH_YES)
3136 m = gfc_match_name (name);
3137
3138 if (m == MATCH_NO)
3139 gfc_error ("Expected terminating name at %C");
3140 if (m != MATCH_YES)
3141 goto cleanup;
3142
3143 if (block_name == NULL)
3144 goto syntax;
3145
3146 if (strcmp (name, block_name) != 0)
3147 {
3148 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3149 gfc_ascii_statement (*st));
3150 goto cleanup;
3151 }
3152
3153 if (gfc_match_eos () == MATCH_YES)
3154 return MATCH_YES;
3155
3156 syntax:
3157 gfc_syntax_error (*st);
3158
3159 cleanup:
3160 gfc_current_locus = old_loc;
3161 return MATCH_ERROR;
3162 }
3163
3164
3165
3166 /***************** Attribute declaration statements ****************/
3167
3168 /* Set the attribute of a single variable. */
3169
3170 static match
3171 attr_decl1 (void)
3172 {
3173 char name[GFC_MAX_SYMBOL_LEN + 1];
3174 gfc_array_spec *as;
3175 gfc_symbol *sym;
3176 locus var_locus;
3177 match m;
3178
3179 as = NULL;
3180
3181 m = gfc_match_name (name);
3182 if (m != MATCH_YES)
3183 goto cleanup;
3184
3185 if (find_special (name, &sym))
3186 return MATCH_ERROR;
3187
3188 var_locus = gfc_current_locus;
3189
3190 /* Deal with possible array specification for certain attributes. */
3191 if (current_attr.dimension
3192 || current_attr.allocatable
3193 || current_attr.pointer
3194 || current_attr.target)
3195 {
3196 m = gfc_match_array_spec (&as);
3197 if (m == MATCH_ERROR)
3198 goto cleanup;
3199
3200 if (current_attr.dimension && m == MATCH_NO)
3201 {
3202 gfc_error
3203 ("Missing array specification at %L in DIMENSION statement",
3204 &var_locus);
3205 m = MATCH_ERROR;
3206 goto cleanup;
3207 }
3208
3209 if ((current_attr.allocatable || current_attr.pointer)
3210 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3211 {
3212 gfc_error ("Array specification must be deferred at %L",
3213 &var_locus);
3214 m = MATCH_ERROR;
3215 goto cleanup;
3216 }
3217 }
3218
3219 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
3220 if (current_attr.dimension == 0
3221 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3222 {
3223 m = MATCH_ERROR;
3224 goto cleanup;
3225 }
3226
3227 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3228 {
3229 m = MATCH_ERROR;
3230 goto cleanup;
3231 }
3232
3233 if (sym->attr.cray_pointee && sym->as != NULL)
3234 {
3235 /* Fix the array spec. */
3236 m = gfc_mod_pointee_as (sym->as);
3237 if (m == MATCH_ERROR)
3238 goto cleanup;
3239 }
3240
3241 if (gfc_add_attribute (&sym->attr, &var_locus, current_attr.intent) == FAILURE)
3242 {
3243 m = MATCH_ERROR;
3244 goto cleanup;
3245 }
3246
3247 if ((current_attr.external || current_attr.intrinsic)
3248 && sym->attr.flavor != FL_PROCEDURE
3249 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3250 {
3251 m = MATCH_ERROR;
3252 goto cleanup;
3253 }
3254
3255 return MATCH_YES;
3256
3257 cleanup:
3258 gfc_free_array_spec (as);
3259 return m;
3260 }
3261
3262
3263 /* Generic attribute declaration subroutine. Used for attributes that
3264 just have a list of names. */
3265
3266 static match
3267 attr_decl (void)
3268 {
3269 match m;
3270
3271 /* Gobble the optional double colon, by simply ignoring the result
3272 of gfc_match(). */
3273 gfc_match (" ::");
3274
3275 for (;;)
3276 {
3277 m = attr_decl1 ();
3278 if (m != MATCH_YES)
3279 break;
3280
3281 if (gfc_match_eos () == MATCH_YES)
3282 {
3283 m = MATCH_YES;
3284 break;
3285 }
3286
3287 if (gfc_match_char (',') != MATCH_YES)
3288 {
3289 gfc_error ("Unexpected character in variable list at %C");
3290 m = MATCH_ERROR;
3291 break;
3292 }
3293 }
3294
3295 return m;
3296 }
3297
3298
3299 /* This routine matches Cray Pointer declarations of the form:
3300 pointer ( <pointer>, <pointee> )
3301 or
3302 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3303 The pointer, if already declared, should be an integer. Otherwise, we
3304 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
3305 be either a scalar, or an array declaration. No space is allocated for
3306 the pointee. For the statement
3307 pointer (ipt, ar(10))
3308 any subsequent uses of ar will be translated (in C-notation) as
3309 ar(i) => ((<type> *) ipt)(i)
3310 After gimplification, pointee variable will disappear in the code. */
3311
3312 static match
3313 cray_pointer_decl (void)
3314 {
3315 match m;
3316 gfc_array_spec *as;
3317 gfc_symbol *cptr; /* Pointer symbol. */
3318 gfc_symbol *cpte; /* Pointee symbol. */
3319 locus var_locus;
3320 bool done = false;
3321
3322 while (!done)
3323 {
3324 if (gfc_match_char ('(') != MATCH_YES)
3325 {
3326 gfc_error ("Expected '(' at %C");
3327 return MATCH_ERROR;
3328 }
3329
3330 /* Match pointer. */
3331 var_locus = gfc_current_locus;
3332 gfc_clear_attr (&current_attr);
3333 gfc_add_cray_pointer (&current_attr, &var_locus);
3334 current_ts.type = BT_INTEGER;
3335 current_ts.kind = gfc_index_integer_kind;
3336
3337 m = gfc_match_symbol (&cptr, 0);
3338 if (m != MATCH_YES)
3339 {
3340 gfc_error ("Expected variable name at %C");
3341 return m;
3342 }
3343
3344 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3345 return MATCH_ERROR;
3346
3347 gfc_set_sym_referenced (cptr);
3348
3349 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
3350 {
3351 cptr->ts.type = BT_INTEGER;
3352 cptr->ts.kind = gfc_index_integer_kind;
3353 }
3354 else if (cptr->ts.type != BT_INTEGER)
3355 {
3356 gfc_error ("Cray pointer at %C must be an integer.");
3357 return MATCH_ERROR;
3358 }
3359 else if (cptr->ts.kind < gfc_index_integer_kind)
3360 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3361 " memory addresses require %d bytes.",
3362 cptr->ts.kind,
3363 gfc_index_integer_kind);
3364
3365 if (gfc_match_char (',') != MATCH_YES)
3366 {
3367 gfc_error ("Expected \",\" at %C");
3368 return MATCH_ERROR;
3369 }
3370
3371 /* Match Pointee. */
3372 var_locus = gfc_current_locus;
3373 gfc_clear_attr (&current_attr);
3374 gfc_add_cray_pointee (&current_attr, &var_locus);
3375 current_ts.type = BT_UNKNOWN;
3376 current_ts.kind = 0;
3377
3378 m = gfc_match_symbol (&cpte, 0);
3379 if (m != MATCH_YES)
3380 {
3381 gfc_error ("Expected variable name at %C");
3382 return m;
3383 }
3384
3385 /* Check for an optional array spec. */
3386 m = gfc_match_array_spec (&as);
3387 if (m == MATCH_ERROR)
3388 {
3389 gfc_free_array_spec (as);
3390 return m;
3391 }
3392 else if (m == MATCH_NO)
3393 {
3394 gfc_free_array_spec (as);
3395 as = NULL;
3396 }
3397
3398 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3399 return MATCH_ERROR;
3400
3401 gfc_set_sym_referenced (cpte);
3402
3403 if (cpte->as == NULL)
3404 {
3405 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3406 gfc_internal_error ("Couldn't set Cray pointee array spec.");
3407 }
3408 else if (as != NULL)
3409 {
3410 gfc_error ("Duplicate array spec for Cray pointee at %C.");
3411 gfc_free_array_spec (as);
3412 return MATCH_ERROR;
3413 }
3414
3415 as = NULL;
3416
3417 if (cpte->as != NULL)
3418 {
3419 /* Fix array spec. */
3420 m = gfc_mod_pointee_as (cpte->as);
3421 if (m == MATCH_ERROR)
3422 return m;
3423 }
3424
3425 /* Point the Pointee at the Pointer. */
3426 cpte->cp_pointer = cptr;
3427
3428 if (gfc_match_char (')') != MATCH_YES)
3429 {
3430 gfc_error ("Expected \")\" at %C");
3431 return MATCH_ERROR;
3432 }
3433 m = gfc_match_char (',');
3434 if (m != MATCH_YES)
3435 done = true; /* Stop searching for more declarations. */
3436
3437 }
3438
3439 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
3440 || gfc_match_eos () != MATCH_YES)
3441 {
3442 gfc_error ("Expected \",\" or end of statement at %C");
3443 return MATCH_ERROR;
3444 }
3445 return MATCH_YES;
3446 }
3447
3448
3449 match
3450 gfc_match_external (void)
3451 {
3452
3453 gfc_clear_attr (&current_attr);
3454 current_attr.external = 1;
3455
3456 return attr_decl ();
3457 }
3458
3459
3460
3461 match
3462 gfc_match_intent (void)
3463 {
3464 sym_intent intent;
3465
3466 intent = match_intent_spec ();
3467 if (intent == INTENT_UNKNOWN)
3468 return MATCH_ERROR;
3469
3470 gfc_clear_attr (&current_attr);
3471 current_attr.intent = intent;
3472
3473 return attr_decl ();
3474 }
3475
3476
3477 match
3478 gfc_match_intrinsic (void)
3479 {
3480
3481 gfc_clear_attr (&current_attr);
3482 current_attr.intrinsic = 1;
3483
3484 return attr_decl ();
3485 }
3486
3487
3488 match
3489 gfc_match_optional (void)
3490 {
3491
3492 gfc_clear_attr (&current_attr);
3493 current_attr.optional = 1;
3494
3495 return attr_decl ();
3496 }
3497
3498
3499 match
3500 gfc_match_pointer (void)
3501 {
3502 gfc_gobble_whitespace ();
3503 if (gfc_peek_char () == '(')
3504 {
3505 if (!gfc_option.flag_cray_pointer)
3506 {
3507 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3508 " flag.");
3509 return MATCH_ERROR;
3510 }
3511 return cray_pointer_decl ();
3512 }
3513 else
3514 {
3515 gfc_clear_attr (&current_attr);
3516 current_attr.pointer = 1;
3517
3518 return attr_decl ();
3519 }
3520 }
3521
3522
3523 match
3524 gfc_match_allocatable (void)
3525 {
3526
3527 gfc_clear_attr (&current_attr);
3528 current_attr.allocatable = 1;
3529
3530 return attr_decl ();
3531 }
3532
3533
3534 match
3535 gfc_match_dimension (void)
3536 {
3537
3538 gfc_clear_attr (&current_attr);
3539 current_attr.dimension = 1;
3540
3541 return attr_decl ();
3542 }
3543
3544
3545 match
3546 gfc_match_target (void)
3547 {
3548
3549 gfc_clear_attr (&current_attr);
3550 current_attr.target = 1;
3551
3552 return attr_decl ();
3553 }
3554
3555
3556 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3557 statement. */
3558
3559 static match
3560 access_attr_decl (gfc_statement st)
3561 {
3562 char name[GFC_MAX_SYMBOL_LEN + 1];
3563 interface_type type;
3564 gfc_user_op *uop;
3565 gfc_symbol *sym;
3566 gfc_intrinsic_op operator;
3567 match m;
3568
3569 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3570 goto done;
3571
3572 for (;;)
3573 {
3574 m = gfc_match_generic_spec (&type, name, &operator);
3575 if (m == MATCH_NO)
3576 goto syntax;
3577 if (m == MATCH_ERROR)
3578 return MATCH_ERROR;
3579
3580 switch (type)
3581 {
3582 case INTERFACE_NAMELESS:
3583 goto syntax;
3584
3585 case INTERFACE_GENERIC:
3586 if (gfc_get_symbol (name, NULL, &sym))
3587 goto done;
3588
3589 if (gfc_add_access (&sym->attr,
3590 (st ==
3591 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3592 sym->name, NULL) == FAILURE)
3593 return MATCH_ERROR;
3594
3595 break;
3596
3597 case INTERFACE_INTRINSIC_OP:
3598 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3599 {
3600 gfc_current_ns->operator_access[operator] =
3601 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3602 }
3603 else
3604 {
3605 gfc_error ("Access specification of the %s operator at %C has "
3606 "already been specified", gfc_op2string (operator));
3607 goto done;
3608 }
3609
3610 break;
3611
3612 case INTERFACE_USER_OP:
3613 uop = gfc_get_uop (name);
3614
3615 if (uop->access == ACCESS_UNKNOWN)
3616 {
3617 uop->access =
3618 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3619 }
3620 else
3621 {
3622 gfc_error
3623 ("Access specification of the .%s. operator at %C has "
3624 "already been specified", sym->name);
3625 goto done;
3626 }
3627
3628 break;
3629 }
3630
3631 if (gfc_match_char (',') == MATCH_NO)
3632 break;
3633 }
3634
3635 if (gfc_match_eos () != MATCH_YES)
3636 goto syntax;
3637 return MATCH_YES;
3638
3639 syntax:
3640 gfc_syntax_error (st);
3641
3642 done:
3643 return MATCH_ERROR;
3644 }
3645
3646
3647 /* The PRIVATE statement is a bit weird in that it can be a attribute
3648 declaration, but also works as a standlone statement inside of a
3649 type declaration or a module. */
3650
3651 match
3652 gfc_match_private (gfc_statement * st)
3653 {
3654
3655 if (gfc_match ("private") != MATCH_YES)
3656 return MATCH_NO;
3657
3658 if (gfc_current_state () == COMP_DERIVED)
3659 {
3660 if (gfc_match_eos () == MATCH_YES)
3661 {
3662 *st = ST_PRIVATE;
3663 return MATCH_YES;
3664 }
3665
3666 gfc_syntax_error (ST_PRIVATE);
3667 return MATCH_ERROR;
3668 }
3669
3670 if (gfc_match_eos () == MATCH_YES)
3671 {
3672 *st = ST_PRIVATE;
3673 return MATCH_YES;
3674 }
3675
3676 *st = ST_ATTR_DECL;
3677 return access_attr_decl (ST_PRIVATE);
3678 }
3679
3680
3681 match
3682 gfc_match_public (gfc_statement * st)
3683 {
3684
3685 if (gfc_match ("public") != MATCH_YES)
3686 return MATCH_NO;
3687
3688 if (gfc_match_eos () == MATCH_YES)
3689 {
3690 *st = ST_PUBLIC;
3691 return MATCH_YES;
3692 }
3693
3694 *st = ST_ATTR_DECL;
3695 return access_attr_decl (ST_PUBLIC);
3696 }
3697
3698
3699 /* Workhorse for gfc_match_parameter. */
3700
3701 static match
3702 do_parm (void)
3703 {
3704 gfc_symbol *sym;
3705 gfc_expr *init;
3706 match m;
3707
3708 m = gfc_match_symbol (&sym, 0);
3709 if (m == MATCH_NO)
3710 gfc_error ("Expected variable name at %C in PARAMETER statement");
3711
3712 if (m != MATCH_YES)
3713 return m;
3714
3715 if (gfc_match_char ('=') == MATCH_NO)
3716 {
3717 gfc_error ("Expected = sign in PARAMETER statement at %C");
3718 return MATCH_ERROR;
3719 }
3720
3721 m = gfc_match_init_expr (&init);
3722 if (m == MATCH_NO)
3723 gfc_error ("Expected expression at %C in PARAMETER statement");
3724 if (m != MATCH_YES)
3725 return m;
3726
3727 if (sym->ts.type == BT_UNKNOWN
3728 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3729 {
3730 m = MATCH_ERROR;
3731 goto cleanup;
3732 }
3733
3734 if (gfc_check_assign_symbol (sym, init) == FAILURE
3735 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3736 {
3737 m = MATCH_ERROR;
3738 goto cleanup;
3739 }
3740
3741 if (sym->ts.type == BT_CHARACTER
3742 && sym->ts.cl != NULL
3743 && sym->ts.cl->length != NULL
3744 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3745 && init->expr_type == EXPR_CONSTANT
3746 && init->ts.type == BT_CHARACTER
3747 && init->ts.kind == 1)
3748 gfc_set_constant_character_len (
3749 mpz_get_si (sym->ts.cl->length->value.integer), init);
3750
3751 sym->value = init;
3752 return MATCH_YES;
3753
3754 cleanup:
3755 gfc_free_expr (init);
3756 return m;
3757 }
3758
3759
3760 /* Match a parameter statement, with the weird syntax that these have. */
3761
3762 match
3763 gfc_match_parameter (void)
3764 {
3765 match m;
3766
3767 if (gfc_match_char ('(') == MATCH_NO)
3768 return MATCH_NO;
3769
3770 for (;;)
3771 {
3772 m = do_parm ();
3773 if (m != MATCH_YES)
3774 break;
3775
3776 if (gfc_match (" )%t") == MATCH_YES)
3777 break;
3778
3779 if (gfc_match_char (',') != MATCH_YES)
3780 {
3781 gfc_error ("Unexpected characters in PARAMETER statement at %C");
3782 m = MATCH_ERROR;
3783 break;
3784 }
3785 }
3786
3787 return m;
3788 }
3789
3790
3791 /* Save statements have a special syntax. */
3792
3793 match
3794 gfc_match_save (void)
3795 {
3796 char n[GFC_MAX_SYMBOL_LEN+1];
3797 gfc_common_head *c;
3798 gfc_symbol *sym;
3799 match m;
3800
3801 if (gfc_match_eos () == MATCH_YES)
3802 {
3803 if (gfc_current_ns->seen_save)
3804 {
3805 if (gfc_notify_std (GFC_STD_LEGACY,
3806 "Blanket SAVE statement at %C follows previous "
3807 "SAVE statement")
3808 == FAILURE)
3809 return MATCH_ERROR;
3810 }
3811
3812 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3813 return MATCH_YES;
3814 }
3815
3816 if (gfc_current_ns->save_all)
3817 {
3818 if (gfc_notify_std (GFC_STD_LEGACY,
3819 "SAVE statement at %C follows blanket SAVE statement")
3820 == FAILURE)
3821 return MATCH_ERROR;
3822 }
3823
3824 gfc_match (" ::");
3825
3826 for (;;)
3827 {
3828 m = gfc_match_symbol (&sym, 0);
3829 switch (m)
3830 {
3831 case MATCH_YES:
3832 if (gfc_add_save (&sym->attr, sym->name,
3833 &gfc_current_locus) == FAILURE)
3834 return MATCH_ERROR;
3835 goto next_item;
3836
3837 case MATCH_NO:
3838 break;
3839
3840 case MATCH_ERROR:
3841 return MATCH_ERROR;
3842 }
3843
3844 m = gfc_match (" / %n /", &n);
3845 if (m == MATCH_ERROR)
3846 return MATCH_ERROR;
3847 if (m == MATCH_NO)
3848 goto syntax;
3849
3850 c = gfc_get_common (n, 0);
3851 c->saved = 1;
3852
3853 gfc_current_ns->seen_save = 1;
3854
3855 next_item:
3856 if (gfc_match_eos () == MATCH_YES)
3857 break;
3858 if (gfc_match_char (',') != MATCH_YES)
3859 goto syntax;
3860 }
3861
3862 return MATCH_YES;
3863
3864 syntax:
3865 gfc_error ("Syntax error in SAVE statement at %C");
3866 return MATCH_ERROR;
3867 }
3868
3869
3870 /* Match a module procedure statement. Note that we have to modify
3871 symbols in the parent's namespace because the current one was there
3872 to receive symbols that are in an interface's formal argument list. */
3873
3874 match
3875 gfc_match_modproc (void)
3876 {
3877 char name[GFC_MAX_SYMBOL_LEN + 1];
3878 gfc_symbol *sym;
3879 match m;
3880
3881 if (gfc_state_stack->state != COMP_INTERFACE
3882 || gfc_state_stack->previous == NULL
3883 || current_interface.type == INTERFACE_NAMELESS)
3884 {
3885 gfc_error
3886 ("MODULE PROCEDURE at %C must be in a generic module interface");
3887 return MATCH_ERROR;
3888 }
3889
3890 for (;;)
3891 {
3892 m = gfc_match_name (name);
3893 if (m == MATCH_NO)
3894 goto syntax;
3895 if (m != MATCH_YES)
3896 return MATCH_ERROR;
3897
3898 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3899 return MATCH_ERROR;
3900
3901 if (sym->attr.proc != PROC_MODULE
3902 && gfc_add_procedure (&sym->attr, PROC_MODULE,
3903 sym->name, NULL) == FAILURE)
3904 return MATCH_ERROR;
3905
3906 if (gfc_add_interface (sym) == FAILURE)
3907 return MATCH_ERROR;
3908
3909 if (gfc_match_eos () == MATCH_YES)
3910 break;
3911 if (gfc_match_char (',') != MATCH_YES)
3912 goto syntax;
3913 }
3914
3915 return MATCH_YES;
3916
3917 syntax:
3918 gfc_syntax_error (ST_MODULE_PROC);
3919 return MATCH_ERROR;
3920 }
3921
3922
3923 /* Match the beginning of a derived type declaration. If a type name
3924 was the result of a function, then it is possible to have a symbol
3925 already to be known as a derived type yet have no components. */
3926
3927 match
3928 gfc_match_derived_decl (void)
3929 {
3930 char name[GFC_MAX_SYMBOL_LEN + 1];
3931 symbol_attribute attr;
3932 gfc_symbol *sym;
3933 match m;
3934
3935 if (gfc_current_state () == COMP_DERIVED)
3936 return MATCH_NO;
3937
3938 gfc_clear_attr (&attr);
3939
3940 loop:
3941 if (gfc_match (" , private") == MATCH_YES)
3942 {
3943 if (gfc_find_state (COMP_MODULE) == FAILURE)
3944 {
3945 gfc_error
3946 ("Derived type at %C can only be PRIVATE within a MODULE");
3947 return MATCH_ERROR;
3948 }
3949
3950 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3951 return MATCH_ERROR;
3952 goto loop;
3953 }
3954
3955 if (gfc_match (" , public") == MATCH_YES)
3956 {
3957 if (gfc_find_state (COMP_MODULE) == FAILURE)
3958 {
3959 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3960 return MATCH_ERROR;
3961 }
3962
3963 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3964 return MATCH_ERROR;
3965 goto loop;
3966 }
3967
3968 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3969 {
3970 gfc_error ("Expected :: in TYPE definition at %C");
3971 return MATCH_ERROR;
3972 }
3973
3974 m = gfc_match (" %n%t", name);
3975 if (m != MATCH_YES)
3976 return m;
3977
3978 /* Make sure the name isn't the name of an intrinsic type. The
3979 'double precision' type doesn't get past the name matcher. */
3980 if (strcmp (name, "integer") == 0
3981 || strcmp (name, "real") == 0
3982 || strcmp (name, "character") == 0
3983 || strcmp (name, "logical") == 0
3984 || strcmp (name, "complex") == 0)
3985 {
3986 gfc_error
3987 ("Type name '%s' at %C cannot be the same as an intrinsic type",
3988 name);
3989 return MATCH_ERROR;
3990 }
3991
3992 if (gfc_get_symbol (name, NULL, &sym))
3993 return MATCH_ERROR;
3994
3995 if (sym->ts.type != BT_UNKNOWN)
3996 {
3997 gfc_error ("Derived type name '%s' at %C already has a basic type "
3998 "of %s", sym->name, gfc_typename (&sym->ts));
3999 return MATCH_ERROR;
4000 }
4001
4002 /* The symbol may already have the derived attribute without the
4003 components. The ways this can happen is via a function
4004 definition, an INTRINSIC statement or a subtype in another
4005 derived type that is a pointer. The first part of the AND clause
4006 is true if a the symbol is not the return value of a function. */
4007 if (sym->attr.flavor != FL_DERIVED
4008 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4009 return MATCH_ERROR;
4010
4011 if (sym->components != NULL)
4012 {
4013 gfc_error
4014 ("Derived type definition of '%s' at %C has already been defined",
4015 sym->name);
4016 return MATCH_ERROR;
4017 }
4018
4019 if (attr.access != ACCESS_UNKNOWN
4020 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4021 return MATCH_ERROR;
4022
4023 gfc_new_block = sym;
4024
4025 return MATCH_YES;
4026 }
4027
4028
4029 /* Cray Pointees can be declared as:
4030 pointer (ipt, a (n,m,...,*))
4031 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
4032 cheat and set a constant bound of 1 for the last dimension, if this
4033 is the case. Since there is no bounds-checking for Cray Pointees,
4034 this will be okay. */
4035
4036 try
4037 gfc_mod_pointee_as (gfc_array_spec *as)
4038 {
4039 as->cray_pointee = true; /* This will be useful to know later. */
4040 if (as->type == AS_ASSUMED_SIZE)
4041 {
4042 as->type = AS_EXPLICIT;
4043 as->upper[as->rank - 1] = gfc_int_expr (1);
4044 as->cp_was_assumed = true;
4045 }
4046 else if (as->type == AS_ASSUMED_SHAPE)
4047 {
4048 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4049 return MATCH_ERROR;
4050 }
4051 return MATCH_YES;
4052 }
4053
4054
4055 /* Match the enum definition statement, here we are trying to match
4056 the first line of enum definition statement.
4057 Returns MATCH_YES if match is found. */
4058
4059 match
4060 gfc_match_enum (void)
4061 {
4062 match m;
4063
4064 m = gfc_match_eos ();
4065 if (m != MATCH_YES)
4066 return m;
4067
4068 if (gfc_notify_std (GFC_STD_F2003,
4069 "New in Fortran 2003: ENUM AND ENUMERATOR at %C")
4070 == FAILURE)
4071 return MATCH_ERROR;
4072
4073 return MATCH_YES;
4074 }
4075
4076
4077 /* Match the enumerator definition statement. */
4078
4079 match
4080 gfc_match_enumerator_def (void)
4081 {
4082 match m;
4083 int elem;
4084
4085 gfc_clear_ts (&current_ts);
4086
4087 m = gfc_match (" enumerator");
4088 if (m != MATCH_YES)
4089 return m;
4090
4091 if (gfc_current_state () != COMP_ENUM)
4092 {
4093 gfc_error ("ENUM definition statement expected before %C");
4094 gfc_free_enum_history ();
4095 return MATCH_ERROR;
4096 }
4097
4098 (&current_ts)->type = BT_INTEGER;
4099 (&current_ts)->kind = gfc_c_int_kind;
4100
4101 m = match_attr_spec ();
4102 if (m == MATCH_ERROR)
4103 {
4104 m = MATCH_NO;
4105 goto cleanup;
4106 }
4107
4108 elem = 1;
4109 for (;;)
4110 {
4111 m = variable_decl (elem++);
4112 if (m == MATCH_ERROR)
4113 goto cleanup;
4114 if (m == MATCH_NO)
4115 break;
4116
4117 if (gfc_match_eos () == MATCH_YES)
4118 goto cleanup;
4119 if (gfc_match_char (',') != MATCH_YES)
4120 break;
4121 }
4122
4123 if (gfc_current_state () == COMP_ENUM)
4124 {
4125 gfc_free_enum_history ();
4126 gfc_error ("Syntax error in ENUMERATOR definition at %C");
4127 m = MATCH_ERROR;
4128 }
4129
4130 cleanup:
4131 gfc_free_array_spec (current_as);
4132 current_as = NULL;
4133 return m;
4134
4135 }
4136
This page took 0.220765 seconds and 5 git commands to generate.