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