* target.h (struct gcc_target): Add new field to struct cxx: import_export_class.
[official-gcc.git] / gcc / fortran / decl.c
blob5c5b7281115b26667bc3bfa819a134451e2e9461
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 #include "config.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include <string.h>
30 /* This flag is set if a an old-style length selector is matched
31 during a type-declaration statement. */
33 static int old_char_selector;
35 /* When variables aquire 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. */
40 static gfc_typespec current_ts;
42 static symbol_attribute current_attr;
43 static gfc_array_spec *current_as;
44 static int colon_seen;
46 /* gfc_new_block points to the symbol of a newly matched block. */
48 gfc_symbol *gfc_new_block;
51 /* Match an intent specification. Since this can only happen after an
52 INTENT word, a legal intent-spec must follow. */
54 static sym_intent
55 match_intent_spec (void)
58 if (gfc_match (" ( in out )") == MATCH_YES)
59 return INTENT_INOUT;
60 if (gfc_match (" ( in )") == MATCH_YES)
61 return INTENT_IN;
62 if (gfc_match (" ( out )") == MATCH_YES)
63 return INTENT_OUT;
65 gfc_error ("Bad INTENT specification at %C");
66 return INTENT_UNKNOWN;
70 /* Matches a character length specification, which is either a
71 specification expression or a '*'. */
73 static match
74 char_len_param_value (gfc_expr ** expr)
77 if (gfc_match_char ('*') == MATCH_YES)
79 *expr = NULL;
80 return MATCH_YES;
83 return gfc_match_expr (expr);
87 /* A character length is a '*' followed by a literal integer or a
88 char_len_param_value in parenthesis. */
90 static match
91 match_char_length (gfc_expr ** expr)
93 int length;
94 match m;
96 m = gfc_match_char ('*');
97 if (m != MATCH_YES)
98 return m;
100 m = gfc_match_small_literal_int (&length);
101 if (m == MATCH_ERROR)
102 return m;
104 if (m == MATCH_YES)
106 *expr = gfc_int_expr (length);
107 return m;
110 if (gfc_match_char ('(') == MATCH_NO)
111 goto syntax;
113 m = char_len_param_value (expr);
114 if (m == MATCH_ERROR)
115 return m;
116 if (m == MATCH_NO)
117 goto syntax;
119 if (gfc_match_char (')') == MATCH_NO)
121 gfc_free_expr (*expr);
122 *expr = NULL;
123 goto syntax;
126 return MATCH_YES;
128 syntax:
129 gfc_error ("Syntax error in character length specification at %C");
130 return MATCH_ERROR;
134 /* Special subroutine for finding a symbol. If we're compiling a
135 function or subroutine and the parent compilation unit is an
136 interface, then check to see if the name we've been given is the
137 name of the interface (located in another namespace). If so,
138 return that symbol. If not, use gfc_get_symbol(). */
140 static int
141 find_special (const char *name, gfc_symbol ** result)
143 gfc_state_data *s;
145 if (gfc_current_state () != COMP_SUBROUTINE
146 && gfc_current_state () != COMP_FUNCTION)
147 goto normal;
149 s = gfc_state_stack->previous;
150 if (s == NULL)
151 goto normal;
153 if (s->state != COMP_INTERFACE)
154 goto normal;
155 if (s->sym == NULL)
156 goto normal; /* Nameless interface */
158 if (strcmp (name, s->sym->name) == 0)
160 *result = s->sym;
161 return 0;
164 normal:
165 return gfc_get_symbol (name, NULL, result);
169 /* Special subroutine for getting a symbol node associated with a
170 procedure name, used in SUBROUTINE and FUNCTION statements. The
171 symbol is created in the parent using with symtree node in the
172 child unit pointing to the symbol. If the current namespace has no
173 parent, then the symbol is just created in the current unit. */
175 static int
176 get_proc_name (const char *name, gfc_symbol ** result)
178 gfc_symtree *st;
179 gfc_symbol *sym;
180 int rc;
182 if (gfc_current_ns->parent == NULL)
183 return gfc_get_symbol (name, NULL, result);
185 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
186 if (*result == NULL)
187 return rc;
189 /* Deal with ENTRY problem */
191 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
193 sym = *result;
194 st->n.sym = sym;
195 sym->refs++;
197 /* See if the procedure should be a module procedure */
199 if (sym->ns->proc_name != NULL
200 && sym->ns->proc_name->attr.flavor == FL_MODULE
201 && sym->attr.proc != PROC_MODULE
202 && gfc_add_procedure (&sym->attr, PROC_MODULE, NULL) == FAILURE)
203 rc = 2;
205 return rc;
209 /* Function called by variable_decl() that adds a name to the symbol
210 table. */
212 static try
213 build_sym (const char *name, gfc_charlen * cl,
214 gfc_array_spec ** as, locus * var_locus)
216 symbol_attribute attr;
217 gfc_symbol *sym;
219 if (find_special (name, &sym))
220 return FAILURE;
222 /* Start updating the symbol table. Add basic type attribute
223 if present. */
224 if (current_ts.type != BT_UNKNOWN
225 &&(sym->attr.implicit_type == 0
226 || !gfc_compare_types (&sym->ts, &current_ts))
227 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
228 return FAILURE;
230 if (sym->ts.type == BT_CHARACTER)
231 sym->ts.cl = cl;
233 /* Add dimension attribute if present. */
234 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
235 return FAILURE;
236 *as = NULL;
238 /* Add attribute to symbol. The copy is so that we can reset the
239 dimension attribute. */
240 attr = current_attr;
241 attr.dimension = 0;
243 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
244 return FAILURE;
246 return SUCCESS;
250 /* Function called by variable_decl() that adds an initialization
251 expression to a symbol. */
253 static try
254 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
255 locus * var_locus)
257 symbol_attribute attr;
258 gfc_symbol *sym;
259 gfc_expr *init;
261 init = *initp;
262 if (find_special (name, &sym))
263 return FAILURE;
265 attr = sym->attr;
267 /* If this symbol is confirming an implicit parameter type,
268 then an initialization expression is not allowed. */
269 if (attr.flavor == FL_PARAMETER
270 && sym->value != NULL
271 && *initp != NULL)
273 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
274 sym->name);
275 return FAILURE;
278 if (attr.in_common
279 && !attr.data
280 && *initp != NULL)
282 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
283 sym->name);
284 return FAILURE;
287 if (init == NULL)
289 /* An initializer is required for PARAMETER declarations. */
290 if (attr.flavor == FL_PARAMETER)
292 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
293 return FAILURE;
296 else
298 /* If a variable appears in a DATA block, it cannot have an
299 initializer. */
300 if (sym->attr.data)
302 gfc_error
303 ("Variable '%s' at %C with an initializer already appears "
304 "in a DATA statement", sym->name);
305 return FAILURE;
308 /* Checking a derived type parameter has to be put off until later. */
309 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
310 && gfc_check_assign_symbol (sym, init) == FAILURE)
311 return FAILURE;
313 /* Add initializer. Make sure we keep the ranks sane. */
314 if (sym->attr.dimension && init->rank == 0)
315 init->rank = sym->as->rank;
317 sym->value = init;
318 *initp = NULL;
321 return SUCCESS;
325 /* Function called by variable_decl() that adds a name to a structure
326 being built. */
328 static try
329 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
330 gfc_array_spec ** as)
332 gfc_component *c;
334 /* If the current symbol is of the same derived type that we're
335 constructing, it must have the pointer attribute. */
336 if (current_ts.type == BT_DERIVED
337 && current_ts.derived == gfc_current_block ()
338 && current_attr.pointer == 0)
340 gfc_error ("Component at %C must have the POINTER attribute");
341 return FAILURE;
344 if (gfc_current_block ()->attr.pointer
345 && (*as)->rank != 0)
347 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
349 gfc_error ("Array component of structure at %C must have explicit "
350 "or deferred shape");
351 return FAILURE;
355 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
356 return FAILURE;
358 c->ts = current_ts;
359 c->ts.cl = cl;
360 gfc_set_component_attr (c, &current_attr);
362 c->initializer = *init;
363 *init = NULL;
365 c->as = *as;
366 if (c->as != NULL)
367 c->dimension = 1;
368 *as = NULL;
370 /* Check array components. */
371 if (!c->dimension)
372 return SUCCESS;
374 if (c->pointer)
376 if (c->as->type != AS_DEFERRED)
378 gfc_error ("Pointer array component of structure at %C "
379 "must have a deferred shape");
380 return FAILURE;
383 else
385 if (c->as->type != AS_EXPLICIT)
387 gfc_error
388 ("Array component of structure at %C must have an explicit "
389 "shape");
390 return FAILURE;
394 return SUCCESS;
398 /* Match a 'NULL()', and possibly take care of some side effects. */
400 match
401 gfc_match_null (gfc_expr ** result)
403 gfc_symbol *sym;
404 gfc_expr *e;
405 match m;
407 m = gfc_match (" null ( )");
408 if (m != MATCH_YES)
409 return m;
411 /* The NULL symbol now has to be/become an intrinsic function. */
412 if (gfc_get_symbol ("null", NULL, &sym))
414 gfc_error ("NULL() initialization at %C is ambiguous");
415 return MATCH_ERROR;
418 gfc_intrinsic_symbol (sym);
420 if (sym->attr.proc != PROC_INTRINSIC
421 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC, NULL) == FAILURE
422 || gfc_add_function (&sym->attr, NULL) == FAILURE))
423 return MATCH_ERROR;
425 e = gfc_get_expr ();
426 e->where = gfc_current_locus;
427 e->expr_type = EXPR_NULL;
428 e->ts.type = BT_UNKNOWN;
430 *result = e;
432 return MATCH_YES;
436 /* Match a variable name with an optional initializer. When this
437 subroutine is called, a variable is expected to be parsed next.
438 Depending on what is happening at the moment, updates either the
439 symbol table or the current interface. */
441 static match
442 variable_decl (void)
444 char name[GFC_MAX_SYMBOL_LEN + 1];
445 gfc_expr *initializer, *char_len;
446 gfc_array_spec *as;
447 gfc_charlen *cl;
448 locus var_locus;
449 match m;
450 try t;
452 initializer = NULL;
453 as = NULL;
455 /* When we get here, we've just matched a list of attributes and
456 maybe a type and a double colon. The next thing we expect to see
457 is the name of the symbol. */
458 m = gfc_match_name (name);
459 if (m != MATCH_YES)
460 goto cleanup;
462 var_locus = gfc_current_locus;
464 /* Now we could see the optional array spec. or character length. */
465 m = gfc_match_array_spec (&as);
466 if (m == MATCH_ERROR)
467 goto cleanup;
468 if (m == MATCH_NO)
469 as = gfc_copy_array_spec (current_as);
471 char_len = NULL;
472 cl = NULL;
474 if (current_ts.type == BT_CHARACTER)
476 switch (match_char_length (&char_len))
478 case MATCH_YES:
479 cl = gfc_get_charlen ();
480 cl->next = gfc_current_ns->cl_list;
481 gfc_current_ns->cl_list = cl;
483 cl->length = char_len;
484 break;
486 case MATCH_NO:
487 cl = current_ts.cl;
488 break;
490 case MATCH_ERROR:
491 goto cleanup;
495 /* OK, we've successfully matched the declaration. Now put the
496 symbol in the current namespace, because it might be used in the
497 optional intialization expression for this symbol, e.g. this is
498 perfectly legal:
500 integer, parameter :: i = huge(i)
502 This is only true for parameters or variables of a basic type.
503 For components of derived types, it is not true, so we don't
504 create a symbol for those yet. If we fail to create the symbol,
505 bail out. */
506 if (gfc_current_state () != COMP_DERIVED
507 && build_sym (name, cl, &as, &var_locus) == FAILURE)
509 m = MATCH_ERROR;
510 goto cleanup;
513 /* In functions that have a RESULT variable defined, the function
514 name always refers to function calls. Therefore, the name is
515 not allowed to appear in specification statements. */
516 if (gfc_current_state () == COMP_FUNCTION
517 && gfc_current_block () != NULL
518 && gfc_current_block ()->result != NULL
519 && gfc_current_block ()->result != gfc_current_block ()
520 && strcmp (gfc_current_block ()->name, name) == 0)
522 gfc_error ("Function name '%s' not allowed at %C", name);
523 m = MATCH_ERROR;
524 goto cleanup;
527 /* The double colon must be present in order to have initializers.
528 Otherwise the statement is ambiguous with an assignment statement. */
529 if (colon_seen)
531 if (gfc_match (" =>") == MATCH_YES)
534 if (!current_attr.pointer)
536 gfc_error ("Initialization at %C isn't for a pointer variable");
537 m = MATCH_ERROR;
538 goto cleanup;
541 m = gfc_match_null (&initializer);
542 if (m == MATCH_NO)
544 gfc_error ("Pointer initialization requires a NULL at %C");
545 m = MATCH_ERROR;
548 if (gfc_pure (NULL))
550 gfc_error
551 ("Initialization of pointer at %C is not allowed in a "
552 "PURE procedure");
553 m = MATCH_ERROR;
556 if (m != MATCH_YES)
557 goto cleanup;
559 initializer->ts = current_ts;
562 else if (gfc_match_char ('=') == MATCH_YES)
564 if (current_attr.pointer)
566 gfc_error
567 ("Pointer initialization at %C requires '=>', not '='");
568 m = MATCH_ERROR;
569 goto cleanup;
572 m = gfc_match_init_expr (&initializer);
573 if (m == MATCH_NO)
575 gfc_error ("Expected an initialization expression at %C");
576 m = MATCH_ERROR;
579 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
581 gfc_error
582 ("Initialization of variable at %C is not allowed in a "
583 "PURE procedure");
584 m = MATCH_ERROR;
587 if (m != MATCH_YES)
588 goto cleanup;
592 /* Add the initializer. Note that it is fine if initializer is
593 NULL here, because we sometimes also need to check if a
594 declaration *must* have an initialization expression. */
595 if (gfc_current_state () != COMP_DERIVED)
596 t = add_init_expr_to_sym (name, &initializer, &var_locus);
597 else
599 if (current_ts.type == BT_DERIVED && !initializer)
600 initializer = gfc_default_initializer (&current_ts);
601 t = build_struct (name, cl, &initializer, &as);
604 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
606 cleanup:
607 /* Free stuff up and return. */
608 gfc_free_expr (initializer);
609 gfc_free_array_spec (as);
611 return m;
615 /* Match an extended-f77 kind specification. */
617 match
618 gfc_match_old_kind_spec (gfc_typespec * ts)
620 match m;
622 if (gfc_match_char ('*') != MATCH_YES)
623 return MATCH_NO;
625 m = gfc_match_small_literal_int (&ts->kind);
626 if (m != MATCH_YES)
627 return MATCH_ERROR;
629 /* Massage the kind numbers for complex types. */
630 if (ts->type == BT_COMPLEX && ts->kind == 8)
631 ts->kind = 4;
632 if (ts->type == BT_COMPLEX && ts->kind == 16)
633 ts->kind = 8;
635 if (gfc_validate_kind (ts->type, ts->kind) == -1)
637 gfc_error ("Old-style kind %d not supported for type %s at %C",
638 ts->kind, gfc_basic_typename (ts->type));
640 return MATCH_ERROR;
643 return MATCH_YES;
647 /* Match a kind specification. Since kinds are generally optional, we
648 usually return MATCH_NO if something goes wrong. If a "kind="
649 string is found, then we know we have an error. */
651 match
652 gfc_match_kind_spec (gfc_typespec * ts)
654 locus where;
655 gfc_expr *e;
656 match m, n;
657 const char *msg;
659 m = MATCH_NO;
660 e = NULL;
662 where = gfc_current_locus;
664 if (gfc_match_char ('(') == MATCH_NO)
665 return MATCH_NO;
667 /* Also gobbles optional text. */
668 if (gfc_match (" kind = ") == MATCH_YES)
669 m = MATCH_ERROR;
671 n = gfc_match_init_expr (&e);
672 if (n == MATCH_NO)
673 gfc_error ("Expected initialization expression at %C");
674 if (n != MATCH_YES)
675 return MATCH_ERROR;
677 if (e->rank != 0)
679 gfc_error ("Expected scalar initialization expression at %C");
680 m = MATCH_ERROR;
681 goto no_match;
684 msg = gfc_extract_int (e, &ts->kind);
685 if (msg != NULL)
687 gfc_error (msg);
688 m = MATCH_ERROR;
689 goto no_match;
692 gfc_free_expr (e);
693 e = NULL;
695 if (gfc_validate_kind (ts->type, ts->kind) == -1)
697 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
698 gfc_basic_typename (ts->type));
700 m = MATCH_ERROR;
701 goto no_match;
704 if (gfc_match_char (')') != MATCH_YES)
706 gfc_error ("Missing right paren at %C");
707 goto no_match;
710 return MATCH_YES;
712 no_match:
713 gfc_free_expr (e);
714 gfc_current_locus = where;
715 return m;
719 /* Match the various kind/length specifications in a CHARACTER
720 declaration. We don't return MATCH_NO. */
722 static match
723 match_char_spec (gfc_typespec * ts)
725 int i, kind, seen_length;
726 gfc_charlen *cl;
727 gfc_expr *len;
728 match m;
730 kind = gfc_default_character_kind ();
731 len = NULL;
732 seen_length = 0;
734 /* Try the old-style specification first. */
735 old_char_selector = 0;
737 m = match_char_length (&len);
738 if (m != MATCH_NO)
740 if (m == MATCH_YES)
741 old_char_selector = 1;
742 seen_length = 1;
743 goto done;
746 m = gfc_match_char ('(');
747 if (m != MATCH_YES)
749 m = MATCH_YES; /* character without length is a single char */
750 goto done;
753 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
754 if (gfc_match (" kind =") == MATCH_YES)
756 m = gfc_match_small_int (&kind);
757 if (m == MATCH_ERROR)
758 goto done;
759 if (m == MATCH_NO)
760 goto syntax;
762 if (gfc_match (" , len =") == MATCH_NO)
763 goto rparen;
765 m = char_len_param_value (&len);
766 if (m == MATCH_NO)
767 goto syntax;
768 if (m == MATCH_ERROR)
769 goto done;
770 seen_length = 1;
772 goto rparen;
775 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
776 if (gfc_match (" len =") == MATCH_YES)
778 m = char_len_param_value (&len);
779 if (m == MATCH_NO)
780 goto syntax;
781 if (m == MATCH_ERROR)
782 goto done;
783 seen_length = 1;
785 if (gfc_match_char (')') == MATCH_YES)
786 goto done;
788 if (gfc_match (" , kind =") != MATCH_YES)
789 goto syntax;
791 gfc_match_small_int (&kind);
793 if (gfc_validate_kind (BT_CHARACTER, kind) == -1)
795 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
796 return MATCH_YES;
799 goto rparen;
802 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
803 m = char_len_param_value (&len);
804 if (m == MATCH_NO)
805 goto syntax;
806 if (m == MATCH_ERROR)
807 goto done;
808 seen_length = 1;
810 m = gfc_match_char (')');
811 if (m == MATCH_YES)
812 goto done;
814 if (gfc_match_char (',') != MATCH_YES)
815 goto syntax;
817 gfc_match (" kind ="); /* Gobble optional text */
819 m = gfc_match_small_int (&kind);
820 if (m == MATCH_ERROR)
821 goto done;
822 if (m == MATCH_NO)
823 goto syntax;
825 rparen:
826 /* Require a right-paren at this point. */
827 m = gfc_match_char (')');
828 if (m == MATCH_YES)
829 goto done;
831 syntax:
832 gfc_error ("Syntax error in CHARACTER declaration at %C");
833 m = MATCH_ERROR;
835 done:
836 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind) == -1)
838 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
839 m = MATCH_ERROR;
842 if (m != MATCH_YES)
844 gfc_free_expr (len);
845 return m;
848 /* Do some final massaging of the length values. */
849 cl = gfc_get_charlen ();
850 cl->next = gfc_current_ns->cl_list;
851 gfc_current_ns->cl_list = cl;
853 if (seen_length == 0)
854 cl->length = gfc_int_expr (1);
855 else
857 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
858 cl->length = len;
859 else
861 gfc_free_expr (len);
862 cl->length = gfc_int_expr (0);
866 ts->cl = cl;
867 ts->kind = kind;
869 return MATCH_YES;
873 /* Matches a type specification. If successful, sets the ts structure
874 to the matched specification. This is necessary for FUNCTION and
875 IMPLICIT statements.
877 If implicit_flag is nonzero, then we don't check for the optional
878 kind specification. Not doing so is needed for matching an IMPLICIT
879 statement correctly. */
881 static match
882 match_type_spec (gfc_typespec * ts, int implicit_flag)
884 char name[GFC_MAX_SYMBOL_LEN + 1];
885 gfc_symbol *sym;
886 match m;
887 int c;
889 gfc_clear_ts (ts);
891 if (gfc_match (" integer") == MATCH_YES)
893 ts->type = BT_INTEGER;
894 ts->kind = gfc_default_integer_kind ();
895 goto get_kind;
898 if (gfc_match (" character") == MATCH_YES)
900 ts->type = BT_CHARACTER;
901 if (implicit_flag == 0)
902 return match_char_spec (ts);
903 else
904 return MATCH_YES;
907 if (gfc_match (" real") == MATCH_YES)
909 ts->type = BT_REAL;
910 ts->kind = gfc_default_real_kind ();
911 goto get_kind;
914 if (gfc_match (" double precision") == MATCH_YES)
916 ts->type = BT_REAL;
917 ts->kind = gfc_default_double_kind ();
918 return MATCH_YES;
921 if (gfc_match (" complex") == MATCH_YES)
923 ts->type = BT_COMPLEX;
924 ts->kind = gfc_default_complex_kind ();
925 goto get_kind;
928 if (gfc_match (" double complex") == MATCH_YES)
930 ts->type = BT_COMPLEX;
931 ts->kind = gfc_default_double_kind ();
932 return MATCH_YES;
935 if (gfc_match (" logical") == MATCH_YES)
937 ts->type = BT_LOGICAL;
938 ts->kind = gfc_default_logical_kind ();
939 goto get_kind;
942 m = gfc_match (" type ( %n )", name);
943 if (m != MATCH_YES)
944 return m;
946 /* Search for the name but allow the components to be defined later. */
947 if (gfc_get_ha_symbol (name, &sym))
949 gfc_error ("Type name '%s' at %C is ambiguous", name);
950 return MATCH_ERROR;
953 if (sym->attr.flavor != FL_DERIVED
954 && gfc_add_flavor (&sym->attr, FL_DERIVED, NULL) == FAILURE)
955 return MATCH_ERROR;
957 ts->type = BT_DERIVED;
958 ts->kind = 0;
959 ts->derived = sym;
961 return MATCH_YES;
963 get_kind:
964 /* For all types except double, derived and character, look for an
965 optional kind specifier. MATCH_NO is actually OK at this point. */
966 if (implicit_flag == 1)
967 return MATCH_YES;
969 if (gfc_current_form == FORM_FREE)
971 c = gfc_peek_char();
972 if (!gfc_is_whitespace(c) && c != '*' && c != '('
973 && c != ':' && c != ',')
974 return MATCH_NO;
977 m = gfc_match_kind_spec (ts);
978 if (m == MATCH_NO && ts->type != BT_CHARACTER)
979 m = gfc_match_old_kind_spec (ts);
981 if (m == MATCH_NO)
982 m = MATCH_YES; /* No kind specifier found. */
984 return m;
988 /* Match an IMPLICIT NONE statement. Actually, this statement is
989 already matched in parse.c, or we would not end up here in the
990 first place. So the only thing we need to check, is if there is
991 trailing garbage. If not, the match is successful. */
993 match
994 gfc_match_implicit_none (void)
997 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1001 /* Match the letter range(s) of an IMPLICIT statement. */
1003 static match
1004 match_implicit_range (gfc_typespec * ts)
1006 int c, c1, c2, inner;
1007 locus cur_loc;
1009 cur_loc = gfc_current_locus;
1011 gfc_gobble_whitespace ();
1012 c = gfc_next_char ();
1013 if (c != '(')
1015 gfc_error ("Missing character range in IMPLICIT at %C");
1016 goto bad;
1019 inner = 1;
1020 while (inner)
1022 gfc_gobble_whitespace ();
1023 c1 = gfc_next_char ();
1024 if (!ISALPHA (c1))
1025 goto bad;
1027 gfc_gobble_whitespace ();
1028 c = gfc_next_char ();
1030 switch (c)
1032 case ')':
1033 inner = 0; /* Fall through */
1035 case ',':
1036 c2 = c1;
1037 break;
1039 case '-':
1040 gfc_gobble_whitespace ();
1041 c2 = gfc_next_char ();
1042 if (!ISALPHA (c2))
1043 goto bad;
1045 gfc_gobble_whitespace ();
1046 c = gfc_next_char ();
1048 if ((c != ',') && (c != ')'))
1049 goto bad;
1050 if (c == ')')
1051 inner = 0;
1053 break;
1055 default:
1056 goto bad;
1059 if (c1 > c2)
1061 gfc_error ("Letters must be in alphabetic order in "
1062 "IMPLICIT statement at %C");
1063 goto bad;
1066 /* See if we can add the newly matched range to the pending
1067 implicits from this IMPLICIT statement. We do not check for
1068 conflicts with whatever earlier IMPLICIT statements may have
1069 set. This is done when we've successfully finished matching
1070 the current one. */
1071 if (gfc_add_new_implicit_range (c1, c2, ts) != SUCCESS)
1072 goto bad;
1075 return MATCH_YES;
1077 bad:
1078 gfc_syntax_error (ST_IMPLICIT);
1080 gfc_current_locus = cur_loc;
1081 return MATCH_ERROR;
1085 /* Match an IMPLICIT statement, storing the types for
1086 gfc_set_implicit() if the statement is accepted by the parser.
1087 There is a strange looking, but legal syntactic construction
1088 possible. It looks like:
1090 IMPLICIT INTEGER (a-b) (c-d)
1092 This is legal if "a-b" is a constant expression that happens to
1093 equal one of the legal kinds for integers. The real problem
1094 happens with an implicit specification that looks like:
1096 IMPLICIT INTEGER (a-b)
1098 In this case, a typespec matcher that is "greedy" (as most of the
1099 matchers are) gobbles the character range as a kindspec, leaving
1100 nothing left. We therefore have to go a bit more slowly in the
1101 matching process by inhibiting the kindspec checking during
1102 typespec matching and checking for a kind later. */
1104 match
1105 gfc_match_implicit (void)
1107 gfc_typespec ts;
1108 locus cur_loc;
1109 int c;
1110 match m;
1112 /* We don't allow empty implicit statements. */
1113 if (gfc_match_eos () == MATCH_YES)
1115 gfc_error ("Empty IMPLICIT statement at %C");
1116 return MATCH_ERROR;
1119 /* First cleanup. */
1120 gfc_clear_new_implicit ();
1124 /* A basic type is mandatory here. */
1125 m = match_type_spec (&ts, 1);
1126 if (m == MATCH_ERROR)
1127 goto error;
1128 if (m == MATCH_NO)
1129 goto syntax;
1131 cur_loc = gfc_current_locus;
1132 m = match_implicit_range (&ts);
1134 if (m != MATCH_YES && ts.type == BT_CHARACTER)
1136 /* looks like we are matching CHARACTER (<len>) (<range>) */
1137 m = match_char_spec (&ts);
1140 if (m == MATCH_YES)
1142 /* Looks like we have the <TYPE> (<RANGE>). */
1143 gfc_gobble_whitespace ();
1144 c = gfc_next_char ();
1145 if ((c == '\n') || (c == ','))
1146 continue;
1148 gfc_current_locus = cur_loc;
1151 /* Last chance -- check <TYPE> (<KIND>) (<RANGE>). */
1152 m = gfc_match_kind_spec (&ts);
1153 if (m == MATCH_ERROR)
1154 goto error;
1155 if (m == MATCH_NO)
1157 m = gfc_match_old_kind_spec (&ts);
1158 if (m == MATCH_ERROR)
1159 goto error;
1160 if (m == MATCH_NO)
1161 goto syntax;
1164 m = match_implicit_range (&ts);
1165 if (m == MATCH_ERROR)
1166 goto error;
1167 if (m == MATCH_NO)
1168 goto syntax;
1170 gfc_gobble_whitespace ();
1171 c = gfc_next_char ();
1172 if ((c != '\n') && (c != ','))
1173 goto syntax;
1176 while (c == ',');
1178 /* All we need to now is try to merge the new implicit types back
1179 into the existing types. This will fail if another implicit
1180 type is already defined for a letter. */
1181 return (gfc_merge_new_implicit () == SUCCESS) ?
1182 MATCH_YES : MATCH_ERROR;
1184 syntax:
1185 gfc_syntax_error (ST_IMPLICIT);
1187 error:
1188 return MATCH_ERROR;
1192 /* Matches an attribute specification including array specs. If
1193 successful, leaves the variables current_attr and current_as
1194 holding the specification. Also sets the colon_seen variable for
1195 later use by matchers associated with initializations.
1197 This subroutine is a little tricky in the sense that we don't know
1198 if we really have an attr-spec until we hit the double colon.
1199 Until that time, we can only return MATCH_NO. This forces us to
1200 check for duplicate specification at this level. */
1202 static match
1203 match_attr_spec (void)
1206 /* Modifiers that can exist in a type statement. */
1207 typedef enum
1208 { GFC_DECL_BEGIN = 0,
1209 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1210 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1211 DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1212 DECL_TARGET, DECL_COLON, DECL_NONE,
1213 GFC_DECL_END /* Sentinel */
1215 decl_types;
1217 /* GFC_DECL_END is the sentinel, index starts at 0. */
1218 #define NUM_DECL GFC_DECL_END
1220 static mstring decls[] = {
1221 minit (", allocatable", DECL_ALLOCATABLE),
1222 minit (", dimension", DECL_DIMENSION),
1223 minit (", external", DECL_EXTERNAL),
1224 minit (", intent ( in )", DECL_IN),
1225 minit (", intent ( out )", DECL_OUT),
1226 minit (", intent ( in out )", DECL_INOUT),
1227 minit (", intrinsic", DECL_INTRINSIC),
1228 minit (", optional", DECL_OPTIONAL),
1229 minit (", parameter", DECL_PARAMETER),
1230 minit (", pointer", DECL_POINTER),
1231 minit (", private", DECL_PRIVATE),
1232 minit (", public", DECL_PUBLIC),
1233 minit (", save", DECL_SAVE),
1234 minit (", target", DECL_TARGET),
1235 minit ("::", DECL_COLON),
1236 minit (NULL, DECL_NONE)
1239 locus start, seen_at[NUM_DECL];
1240 int seen[NUM_DECL];
1241 decl_types d;
1242 const char *attr;
1243 match m;
1244 try t;
1246 gfc_clear_attr (&current_attr);
1247 start = gfc_current_locus;
1249 current_as = NULL;
1250 colon_seen = 0;
1252 /* See if we get all of the keywords up to the final double colon. */
1253 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1254 seen[d] = 0;
1256 for (;;)
1258 d = (decl_types) gfc_match_strings (decls);
1259 if (d == DECL_NONE || d == DECL_COLON)
1260 break;
1262 seen[d]++;
1263 seen_at[d] = gfc_current_locus;
1265 if (d == DECL_DIMENSION)
1267 m = gfc_match_array_spec (&current_as);
1269 if (m == MATCH_NO)
1271 gfc_error ("Missing dimension specification at %C");
1272 m = MATCH_ERROR;
1275 if (m == MATCH_ERROR)
1276 goto cleanup;
1280 /* No double colon, so assume that we've been looking at something
1281 else the whole time. */
1282 if (d == DECL_NONE)
1284 m = MATCH_NO;
1285 goto cleanup;
1288 /* Since we've seen a double colon, we have to be looking at an
1289 attr-spec. This means that we can now issue errors. */
1290 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1291 if (seen[d] > 1)
1293 switch (d)
1295 case DECL_ALLOCATABLE:
1296 attr = "ALLOCATABLE";
1297 break;
1298 case DECL_DIMENSION:
1299 attr = "DIMENSION";
1300 break;
1301 case DECL_EXTERNAL:
1302 attr = "EXTERNAL";
1303 break;
1304 case DECL_IN:
1305 attr = "INTENT (IN)";
1306 break;
1307 case DECL_OUT:
1308 attr = "INTENT (OUT)";
1309 break;
1310 case DECL_INOUT:
1311 attr = "INTENT (IN OUT)";
1312 break;
1313 case DECL_INTRINSIC:
1314 attr = "INTRINSIC";
1315 break;
1316 case DECL_OPTIONAL:
1317 attr = "OPTIONAL";
1318 break;
1319 case DECL_PARAMETER:
1320 attr = "PARAMETER";
1321 break;
1322 case DECL_POINTER:
1323 attr = "POINTER";
1324 break;
1325 case DECL_PRIVATE:
1326 attr = "PRIVATE";
1327 break;
1328 case DECL_PUBLIC:
1329 attr = "PUBLIC";
1330 break;
1331 case DECL_SAVE:
1332 attr = "SAVE";
1333 break;
1334 case DECL_TARGET:
1335 attr = "TARGET";
1336 break;
1337 default:
1338 attr = NULL; /* This shouldn't happen */
1341 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
1342 m = MATCH_ERROR;
1343 goto cleanup;
1346 /* Now that we've dealt with duplicate attributes, add the attributes
1347 to the current attribute. */
1348 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1350 if (seen[d] == 0)
1351 continue;
1353 if (gfc_current_state () == COMP_DERIVED
1354 && d != DECL_DIMENSION && d != DECL_POINTER
1355 && d != DECL_COLON && d != DECL_NONE)
1358 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
1359 &seen_at[d]);
1360 m = MATCH_ERROR;
1361 goto cleanup;
1364 switch (d)
1366 case DECL_ALLOCATABLE:
1367 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
1368 break;
1370 case DECL_DIMENSION:
1371 t = gfc_add_dimension (&current_attr, &seen_at[d]);
1372 break;
1374 case DECL_EXTERNAL:
1375 t = gfc_add_external (&current_attr, &seen_at[d]);
1376 break;
1378 case DECL_IN:
1379 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
1380 break;
1382 case DECL_OUT:
1383 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
1384 break;
1386 case DECL_INOUT:
1387 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
1388 break;
1390 case DECL_INTRINSIC:
1391 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
1392 break;
1394 case DECL_OPTIONAL:
1395 t = gfc_add_optional (&current_attr, &seen_at[d]);
1396 break;
1398 case DECL_PARAMETER:
1399 t = gfc_add_flavor (&current_attr, FL_PARAMETER, &seen_at[d]);
1400 break;
1402 case DECL_POINTER:
1403 t = gfc_add_pointer (&current_attr, &seen_at[d]);
1404 break;
1406 case DECL_PRIVATE:
1407 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, &seen_at[d]);
1408 break;
1410 case DECL_PUBLIC:
1411 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, &seen_at[d]);
1412 break;
1414 case DECL_SAVE:
1415 t = gfc_add_save (&current_attr, &seen_at[d]);
1416 break;
1418 case DECL_TARGET:
1419 t = gfc_add_target (&current_attr, &seen_at[d]);
1420 break;
1422 default:
1423 gfc_internal_error ("match_attr_spec(): Bad attribute");
1426 if (t == FAILURE)
1428 m = MATCH_ERROR;
1429 goto cleanup;
1433 colon_seen = 1;
1434 return MATCH_YES;
1436 cleanup:
1437 gfc_current_locus = start;
1438 gfc_free_array_spec (current_as);
1439 current_as = NULL;
1440 return m;
1444 /* Match a data declaration statement. */
1446 match
1447 gfc_match_data_decl (void)
1449 gfc_symbol *sym;
1450 match m;
1452 m = match_type_spec (&current_ts, 0);
1453 if (m != MATCH_YES)
1454 return m;
1456 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
1458 sym = gfc_use_derived (current_ts.derived);
1460 if (sym == NULL)
1462 m = MATCH_ERROR;
1463 goto cleanup;
1466 current_ts.derived = sym;
1469 m = match_attr_spec ();
1470 if (m == MATCH_ERROR)
1472 m = MATCH_NO;
1473 goto cleanup;
1476 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
1479 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
1480 goto ok;
1482 if (gfc_find_symbol (current_ts.derived->name,
1483 current_ts.derived->ns->parent, 1, &sym) == 0)
1484 goto ok;
1486 /* Hope that an ambiguous symbol is itself masked by a type definition. */
1487 if (sym != NULL && sym->attr.flavor == FL_DERIVED)
1488 goto ok;
1490 gfc_error ("Derived type at %C has not been previously defined");
1491 m = MATCH_ERROR;
1492 goto cleanup;
1496 /* If we have an old-style character declaration, and no new-style
1497 attribute specifications, then there a comma is optional between
1498 the type specification and the variable list. */
1499 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
1500 gfc_match_char (',');
1502 /* Give the types/attributes to symbols that follow. */
1503 for (;;)
1505 m = variable_decl ();
1506 if (m == MATCH_ERROR)
1507 goto cleanup;
1508 if (m == MATCH_NO)
1509 break;
1511 if (gfc_match_eos () == MATCH_YES)
1512 goto cleanup;
1513 if (gfc_match_char (',') != MATCH_YES)
1514 break;
1517 gfc_error ("Syntax error in data declaration at %C");
1518 m = MATCH_ERROR;
1520 cleanup:
1521 gfc_free_array_spec (current_as);
1522 current_as = NULL;
1523 return m;
1527 /* Match a prefix associated with a function or subroutine
1528 declaration. If the typespec pointer is nonnull, then a typespec
1529 can be matched. Note that if nothing matches, MATCH_YES is
1530 returned (the null string was matched). */
1532 static match
1533 match_prefix (gfc_typespec * ts)
1535 int seen_type;
1537 gfc_clear_attr (&current_attr);
1538 seen_type = 0;
1540 loop:
1541 if (!seen_type && ts != NULL
1542 && match_type_spec (ts, 0) == MATCH_YES
1543 && gfc_match_space () == MATCH_YES)
1546 seen_type = 1;
1547 goto loop;
1550 if (gfc_match ("elemental% ") == MATCH_YES)
1552 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
1553 return MATCH_ERROR;
1555 goto loop;
1558 if (gfc_match ("pure% ") == MATCH_YES)
1560 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
1561 return MATCH_ERROR;
1563 goto loop;
1566 if (gfc_match ("recursive% ") == MATCH_YES)
1568 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
1569 return MATCH_ERROR;
1571 goto loop;
1574 /* At this point, the next item is not a prefix. */
1575 return MATCH_YES;
1579 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
1581 static try
1582 copy_prefix (symbol_attribute * dest, locus * where)
1585 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
1586 return FAILURE;
1588 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
1589 return FAILURE;
1591 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
1592 return FAILURE;
1594 return SUCCESS;
1598 /* Match a formal argument list. */
1600 match
1601 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
1603 gfc_formal_arglist *head, *tail, *p, *q;
1604 char name[GFC_MAX_SYMBOL_LEN + 1];
1605 gfc_symbol *sym;
1606 match m;
1608 head = tail = NULL;
1610 if (gfc_match_char ('(') != MATCH_YES)
1612 if (null_flag)
1613 goto ok;
1614 return MATCH_NO;
1617 if (gfc_match_char (')') == MATCH_YES)
1618 goto ok;
1620 for (;;)
1622 if (gfc_match_char ('*') == MATCH_YES)
1623 sym = NULL;
1624 else
1626 m = gfc_match_name (name);
1627 if (m != MATCH_YES)
1628 goto cleanup;
1630 if (gfc_get_symbol (name, NULL, &sym))
1631 goto cleanup;
1634 p = gfc_get_formal_arglist ();
1636 if (head == NULL)
1637 head = tail = p;
1638 else
1640 tail->next = p;
1641 tail = p;
1644 tail->sym = sym;
1646 /* We don't add the VARIABLE flavor because the name could be a
1647 dummy procedure. We don't apply these attributes to formal
1648 arguments of statement functions. */
1649 if (sym != NULL && !st_flag
1650 && (gfc_add_dummy (&sym->attr, NULL) == FAILURE
1651 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
1653 m = MATCH_ERROR;
1654 goto cleanup;
1657 /* The name of a program unit can be in a different namespace,
1658 so check for it explicitly. After the statement is accepted,
1659 the name is checked for especially in gfc_get_symbol(). */
1660 if (gfc_new_block != NULL && sym != NULL
1661 && strcmp (sym->name, gfc_new_block->name) == 0)
1663 gfc_error ("Name '%s' at %C is the name of the procedure",
1664 sym->name);
1665 m = MATCH_ERROR;
1666 goto cleanup;
1669 if (gfc_match_char (')') == MATCH_YES)
1670 goto ok;
1672 m = gfc_match_char (',');
1673 if (m != MATCH_YES)
1675 gfc_error ("Unexpected junk in formal argument list at %C");
1676 goto cleanup;
1681 /* Check for duplicate symbols in the formal argument list. */
1682 if (head != NULL)
1684 for (p = head; p->next; p = p->next)
1686 if (p->sym == NULL)
1687 continue;
1689 for (q = p->next; q; q = q->next)
1690 if (p->sym == q->sym)
1692 gfc_error
1693 ("Duplicate symbol '%s' in formal argument list at %C",
1694 p->sym->name);
1696 m = MATCH_ERROR;
1697 goto cleanup;
1702 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
1703 FAILURE)
1705 m = MATCH_ERROR;
1706 goto cleanup;
1709 return MATCH_YES;
1711 cleanup:
1712 gfc_free_formal_arglist (head);
1713 return m;
1717 /* Match a RESULT specification following a function declaration or
1718 ENTRY statement. Also matches the end-of-statement. */
1720 static match
1721 match_result (gfc_symbol * function, gfc_symbol ** result)
1723 char name[GFC_MAX_SYMBOL_LEN + 1];
1724 gfc_symbol *r;
1725 match m;
1727 if (gfc_match (" result (") != MATCH_YES)
1728 return MATCH_NO;
1730 m = gfc_match_name (name);
1731 if (m != MATCH_YES)
1732 return m;
1734 if (gfc_match (" )%t") != MATCH_YES)
1736 gfc_error ("Unexpected junk following RESULT variable at %C");
1737 return MATCH_ERROR;
1740 if (strcmp (function->name, name) == 0)
1742 gfc_error
1743 ("RESULT variable at %C must be different than function name");
1744 return MATCH_ERROR;
1747 if (gfc_get_symbol (name, NULL, &r))
1748 return MATCH_ERROR;
1750 if (gfc_add_flavor (&r->attr, FL_VARIABLE, NULL) == FAILURE
1751 || gfc_add_result (&r->attr, NULL) == FAILURE)
1752 return MATCH_ERROR;
1754 *result = r;
1756 return MATCH_YES;
1760 /* Match a function declaration. */
1762 match
1763 gfc_match_function_decl (void)
1765 char name[GFC_MAX_SYMBOL_LEN + 1];
1766 gfc_symbol *sym, *result;
1767 locus old_loc;
1768 match m;
1770 if (gfc_current_state () != COMP_NONE
1771 && gfc_current_state () != COMP_INTERFACE
1772 && gfc_current_state () != COMP_CONTAINS)
1773 return MATCH_NO;
1775 gfc_clear_ts (&current_ts);
1777 old_loc = gfc_current_locus;
1779 m = match_prefix (&current_ts);
1780 if (m != MATCH_YES)
1782 gfc_current_locus = old_loc;
1783 return m;
1786 if (gfc_match ("function% %n", name) != MATCH_YES)
1788 gfc_current_locus = old_loc;
1789 return MATCH_NO;
1792 if (get_proc_name (name, &sym))
1793 return MATCH_ERROR;
1794 gfc_new_block = sym;
1796 m = gfc_match_formal_arglist (sym, 0, 0);
1797 if (m == MATCH_NO)
1798 gfc_error ("Expected formal argument list in function definition at %C");
1799 else if (m == MATCH_ERROR)
1800 goto cleanup;
1802 result = NULL;
1804 if (gfc_match_eos () != MATCH_YES)
1806 /* See if a result variable is present. */
1807 m = match_result (sym, &result);
1808 if (m == MATCH_NO)
1809 gfc_error ("Unexpected junk after function declaration at %C");
1811 if (m != MATCH_YES)
1813 m = MATCH_ERROR;
1814 goto cleanup;
1818 /* Make changes to the symbol. */
1819 m = MATCH_ERROR;
1821 if (gfc_add_function (&sym->attr, NULL) == FAILURE)
1822 goto cleanup;
1824 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
1825 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
1826 goto cleanup;
1828 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
1830 gfc_error ("Function '%s' at %C already has a type of %s", name,
1831 gfc_basic_typename (sym->ts.type));
1832 goto cleanup;
1835 if (result == NULL)
1837 sym->ts = current_ts;
1838 sym->result = sym;
1840 else
1842 result->ts = current_ts;
1843 sym->result = result;
1846 return MATCH_YES;
1848 cleanup:
1849 gfc_current_locus = old_loc;
1850 return m;
1854 /* Match an ENTRY statement. */
1856 match
1857 gfc_match_entry (void)
1859 gfc_symbol *function, *result, *entry;
1860 char name[GFC_MAX_SYMBOL_LEN + 1];
1861 gfc_compile_state state;
1862 match m;
1864 m = gfc_match_name (name);
1865 if (m != MATCH_YES)
1866 return m;
1868 if (get_proc_name (name, &entry))
1869 return MATCH_ERROR;
1871 gfc_enclosing_unit (&state);
1872 switch (state)
1874 case COMP_SUBROUTINE:
1875 m = gfc_match_formal_arglist (entry, 0, 1);
1876 if (m != MATCH_YES)
1877 return MATCH_ERROR;
1879 if (gfc_current_state () != COMP_SUBROUTINE)
1880 goto exec_construct;
1882 if (gfc_add_entry (&entry->attr, NULL) == FAILURE
1883 || gfc_add_subroutine (&entry->attr, NULL) == FAILURE)
1884 return MATCH_ERROR;
1886 break;
1888 case COMP_FUNCTION:
1889 m = gfc_match_formal_arglist (entry, 0, 0);
1890 if (m != MATCH_YES)
1891 return MATCH_ERROR;
1893 if (gfc_current_state () != COMP_FUNCTION)
1894 goto exec_construct;
1895 function = gfc_state_stack->sym;
1897 result = NULL;
1899 if (gfc_match_eos () == MATCH_YES)
1901 if (gfc_add_entry (&entry->attr, NULL) == FAILURE
1902 || gfc_add_function (&entry->attr, NULL) == FAILURE)
1903 return MATCH_ERROR;
1905 entry->result = function->result;
1908 else
1910 m = match_result (function, &result);
1911 if (m == MATCH_NO)
1912 gfc_syntax_error (ST_ENTRY);
1913 if (m != MATCH_YES)
1914 return MATCH_ERROR;
1916 if (gfc_add_result (&result->attr, NULL) == FAILURE
1917 || gfc_add_entry (&entry->attr, NULL) == FAILURE
1918 || gfc_add_function (&entry->attr, NULL) == FAILURE)
1919 return MATCH_ERROR;
1922 if (function->attr.recursive && result == NULL)
1924 gfc_error ("RESULT attribute required in ENTRY statement at %C");
1925 return MATCH_ERROR;
1928 break;
1930 default:
1931 goto exec_construct;
1934 if (gfc_match_eos () != MATCH_YES)
1936 gfc_syntax_error (ST_ENTRY);
1937 return MATCH_ERROR;
1940 return MATCH_YES;
1942 exec_construct:
1943 gfc_error ("ENTRY statement at %C cannot appear within %s",
1944 gfc_state_name (gfc_current_state ()));
1946 return MATCH_ERROR;
1950 /* Match a subroutine statement, including optional prefixes. */
1952 match
1953 gfc_match_subroutine (void)
1955 char name[GFC_MAX_SYMBOL_LEN + 1];
1956 gfc_symbol *sym;
1957 match m;
1959 if (gfc_current_state () != COMP_NONE
1960 && gfc_current_state () != COMP_INTERFACE
1961 && gfc_current_state () != COMP_CONTAINS)
1962 return MATCH_NO;
1964 m = match_prefix (NULL);
1965 if (m != MATCH_YES)
1966 return m;
1968 m = gfc_match ("subroutine% %n", name);
1969 if (m != MATCH_YES)
1970 return m;
1972 if (get_proc_name (name, &sym))
1973 return MATCH_ERROR;
1974 gfc_new_block = sym;
1976 if (gfc_add_subroutine (&sym->attr, NULL) == FAILURE)
1977 return MATCH_ERROR;
1979 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
1980 return MATCH_ERROR;
1982 if (gfc_match_eos () != MATCH_YES)
1984 gfc_syntax_error (ST_SUBROUTINE);
1985 return MATCH_ERROR;
1988 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
1989 return MATCH_ERROR;
1991 return MATCH_YES;
1995 /* Return nonzero if we're currenly compiling a contained procedure. */
1997 static int
1998 contained_procedure (void)
2000 gfc_state_data *s;
2002 for (s=gfc_state_stack; s; s=s->previous)
2003 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2004 && s->previous != NULL
2005 && s->previous->state == COMP_CONTAINS)
2006 return 1;
2008 return 0;
2011 /* Match any of the various end-block statements. Returns the type of
2012 END to the caller. The END INTERFACE, END IF, END DO and END
2013 SELECT statements cannot be replaced by a single END statement. */
2015 match
2016 gfc_match_end (gfc_statement * st)
2018 char name[GFC_MAX_SYMBOL_LEN + 1];
2019 gfc_compile_state state;
2020 locus old_loc;
2021 const char *block_name;
2022 const char *target;
2023 int eos_ok;
2024 match m;
2026 old_loc = gfc_current_locus;
2027 if (gfc_match ("end") != MATCH_YES)
2028 return MATCH_NO;
2030 state = gfc_current_state ();
2031 block_name =
2032 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2034 if (state == COMP_CONTAINS)
2036 state = gfc_state_stack->previous->state;
2037 block_name = gfc_state_stack->previous->sym == NULL ? NULL
2038 : gfc_state_stack->previous->sym->name;
2041 switch (state)
2043 case COMP_NONE:
2044 case COMP_PROGRAM:
2045 *st = ST_END_PROGRAM;
2046 target = " program";
2047 eos_ok = 1;
2048 break;
2050 case COMP_SUBROUTINE:
2051 *st = ST_END_SUBROUTINE;
2052 target = " subroutine";
2053 eos_ok = !contained_procedure ();
2054 break;
2056 case COMP_FUNCTION:
2057 *st = ST_END_FUNCTION;
2058 target = " function";
2059 eos_ok = !contained_procedure ();
2060 break;
2062 case COMP_BLOCK_DATA:
2063 *st = ST_END_BLOCK_DATA;
2064 target = " block data";
2065 eos_ok = 1;
2066 break;
2068 case COMP_MODULE:
2069 *st = ST_END_MODULE;
2070 target = " module";
2071 eos_ok = 1;
2072 break;
2074 case COMP_INTERFACE:
2075 *st = ST_END_INTERFACE;
2076 target = " interface";
2077 eos_ok = 0;
2078 break;
2080 case COMP_DERIVED:
2081 *st = ST_END_TYPE;
2082 target = " type";
2083 eos_ok = 0;
2084 break;
2086 case COMP_IF:
2087 *st = ST_ENDIF;
2088 target = " if";
2089 eos_ok = 0;
2090 break;
2092 case COMP_DO:
2093 *st = ST_ENDDO;
2094 target = " do";
2095 eos_ok = 0;
2096 break;
2098 case COMP_SELECT:
2099 *st = ST_END_SELECT;
2100 target = " select";
2101 eos_ok = 0;
2102 break;
2104 case COMP_FORALL:
2105 *st = ST_END_FORALL;
2106 target = " forall";
2107 eos_ok = 0;
2108 break;
2110 case COMP_WHERE:
2111 *st = ST_END_WHERE;
2112 target = " where";
2113 eos_ok = 0;
2114 break;
2116 default:
2117 gfc_error ("Unexpected END statement at %C");
2118 goto cleanup;
2121 if (gfc_match_eos () == MATCH_YES)
2123 if (!eos_ok)
2125 /* We would have required END [something] */
2126 gfc_error ("%s statement expected at %C",
2127 gfc_ascii_statement (*st));
2128 goto cleanup;
2131 return MATCH_YES;
2134 /* Verify that we've got the sort of end-block that we're expecting. */
2135 if (gfc_match (target) != MATCH_YES)
2137 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
2138 goto cleanup;
2141 /* If we're at the end, make sure a block name wasn't required. */
2142 if (gfc_match_eos () == MATCH_YES)
2145 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
2146 return MATCH_YES;
2148 if (gfc_current_block () == NULL)
2149 return MATCH_YES;
2151 gfc_error ("Expected block name of '%s' in %s statement at %C",
2152 block_name, gfc_ascii_statement (*st));
2154 return MATCH_ERROR;
2157 /* END INTERFACE has a special handler for its several possible endings. */
2158 if (*st == ST_END_INTERFACE)
2159 return gfc_match_end_interface ();
2161 /* We haven't hit the end of statement, so what is left must be an end-name. */
2162 m = gfc_match_space ();
2163 if (m == MATCH_YES)
2164 m = gfc_match_name (name);
2166 if (m == MATCH_NO)
2167 gfc_error ("Expected terminating name at %C");
2168 if (m != MATCH_YES)
2169 goto cleanup;
2171 if (block_name == NULL)
2172 goto syntax;
2174 if (strcmp (name, block_name) != 0)
2176 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
2177 gfc_ascii_statement (*st));
2178 goto cleanup;
2181 if (gfc_match_eos () == MATCH_YES)
2182 return MATCH_YES;
2184 syntax:
2185 gfc_syntax_error (*st);
2187 cleanup:
2188 gfc_current_locus = old_loc;
2189 return MATCH_ERROR;
2194 /***************** Attribute declaration statements ****************/
2196 /* Set the attribute of a single variable. */
2198 static match
2199 attr_decl1 (void)
2201 char name[GFC_MAX_SYMBOL_LEN + 1];
2202 gfc_array_spec *as;
2203 gfc_symbol *sym;
2204 locus var_locus;
2205 match m;
2207 as = NULL;
2209 m = gfc_match_name (name);
2210 if (m != MATCH_YES)
2211 goto cleanup;
2213 if (find_special (name, &sym))
2214 return MATCH_ERROR;
2216 var_locus = gfc_current_locus;
2218 /* Deal with possible array specification for certain attributes. */
2219 if (current_attr.dimension
2220 || current_attr.allocatable
2221 || current_attr.pointer
2222 || current_attr.target)
2224 m = gfc_match_array_spec (&as);
2225 if (m == MATCH_ERROR)
2226 goto cleanup;
2228 if (current_attr.dimension && m == MATCH_NO)
2230 gfc_error
2231 ("Missing array specification at %L in DIMENSION statement",
2232 &var_locus);
2233 m = MATCH_ERROR;
2234 goto cleanup;
2237 if ((current_attr.allocatable || current_attr.pointer)
2238 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
2240 gfc_error ("Array specification must be deferred at %L",
2241 &var_locus);
2242 m = MATCH_ERROR;
2243 goto cleanup;
2247 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
2248 if (current_attr.dimension == 0
2249 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
2251 m = MATCH_ERROR;
2252 goto cleanup;
2255 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
2257 m = MATCH_ERROR;
2258 goto cleanup;
2261 if ((current_attr.external || current_attr.intrinsic)
2262 && sym->attr.flavor != FL_PROCEDURE
2263 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, NULL) == FAILURE)
2265 m = MATCH_ERROR;
2266 goto cleanup;
2269 return MATCH_YES;
2271 cleanup:
2272 gfc_free_array_spec (as);
2273 return m;
2277 /* Generic attribute declaration subroutine. Used for attributes that
2278 just have a list of names. */
2280 static match
2281 attr_decl (void)
2283 match m;
2285 /* Gobble the optional double colon, by simply ignoring the result
2286 of gfc_match(). */
2287 gfc_match (" ::");
2289 for (;;)
2291 m = attr_decl1 ();
2292 if (m != MATCH_YES)
2293 break;
2295 if (gfc_match_eos () == MATCH_YES)
2297 m = MATCH_YES;
2298 break;
2301 if (gfc_match_char (',') != MATCH_YES)
2303 gfc_error ("Unexpected character in variable list at %C");
2304 m = MATCH_ERROR;
2305 break;
2309 return m;
2313 match
2314 gfc_match_external (void)
2317 gfc_clear_attr (&current_attr);
2318 gfc_add_external (&current_attr, NULL);
2320 return attr_decl ();
2325 match
2326 gfc_match_intent (void)
2328 sym_intent intent;
2330 intent = match_intent_spec ();
2331 if (intent == INTENT_UNKNOWN)
2332 return MATCH_ERROR;
2334 gfc_clear_attr (&current_attr);
2335 gfc_add_intent (&current_attr, intent, NULL); /* Can't fail */
2337 return attr_decl ();
2341 match
2342 gfc_match_intrinsic (void)
2345 gfc_clear_attr (&current_attr);
2346 gfc_add_intrinsic (&current_attr, NULL);
2348 return attr_decl ();
2352 match
2353 gfc_match_optional (void)
2356 gfc_clear_attr (&current_attr);
2357 gfc_add_optional (&current_attr, NULL);
2359 return attr_decl ();
2363 match
2364 gfc_match_pointer (void)
2367 gfc_clear_attr (&current_attr);
2368 gfc_add_pointer (&current_attr, NULL);
2370 return attr_decl ();
2374 match
2375 gfc_match_allocatable (void)
2378 gfc_clear_attr (&current_attr);
2379 gfc_add_allocatable (&current_attr, NULL);
2381 return attr_decl ();
2385 match
2386 gfc_match_dimension (void)
2389 gfc_clear_attr (&current_attr);
2390 gfc_add_dimension (&current_attr, NULL);
2392 return attr_decl ();
2396 match
2397 gfc_match_target (void)
2400 gfc_clear_attr (&current_attr);
2401 gfc_add_target (&current_attr, NULL);
2403 return attr_decl ();
2407 /* Match the list of entities being specified in a PUBLIC or PRIVATE
2408 statement. */
2410 static match
2411 access_attr_decl (gfc_statement st)
2413 char name[GFC_MAX_SYMBOL_LEN + 1];
2414 interface_type type;
2415 gfc_user_op *uop;
2416 gfc_symbol *sym;
2417 gfc_intrinsic_op operator;
2418 match m;
2420 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
2421 goto done;
2423 for (;;)
2425 m = gfc_match_generic_spec (&type, name, &operator);
2426 if (m == MATCH_NO)
2427 goto syntax;
2428 if (m == MATCH_ERROR)
2429 return MATCH_ERROR;
2431 switch (type)
2433 case INTERFACE_NAMELESS:
2434 goto syntax;
2436 case INTERFACE_GENERIC:
2437 if (gfc_get_symbol (name, NULL, &sym))
2438 goto done;
2440 if (gfc_add_access (&sym->attr,
2441 (st ==
2442 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
2443 NULL) == FAILURE)
2444 return MATCH_ERROR;
2446 break;
2448 case INTERFACE_INTRINSIC_OP:
2449 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
2451 gfc_current_ns->operator_access[operator] =
2452 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2454 else
2456 gfc_error ("Access specification of the %s operator at %C has "
2457 "already been specified", gfc_op2string (operator));
2458 goto done;
2461 break;
2463 case INTERFACE_USER_OP:
2464 uop = gfc_get_uop (name);
2466 if (uop->access == ACCESS_UNKNOWN)
2468 uop->access =
2469 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2471 else
2473 gfc_error
2474 ("Access specification of the .%s. operator at %C has "
2475 "already been specified", sym->name);
2476 goto done;
2479 break;
2482 if (gfc_match_char (',') == MATCH_NO)
2483 break;
2486 if (gfc_match_eos () != MATCH_YES)
2487 goto syntax;
2488 return MATCH_YES;
2490 syntax:
2491 gfc_syntax_error (st);
2493 done:
2494 return MATCH_ERROR;
2498 /* The PRIVATE statement is a bit weird in that it can be a attribute
2499 declaration, but also works as a standlone statement inside of a
2500 type declaration or a module. */
2502 match
2503 gfc_match_private (gfc_statement * st)
2506 if (gfc_match ("private") != MATCH_YES)
2507 return MATCH_NO;
2509 if (gfc_current_state () == COMP_DERIVED)
2511 if (gfc_match_eos () == MATCH_YES)
2513 *st = ST_PRIVATE;
2514 return MATCH_YES;
2517 gfc_syntax_error (ST_PRIVATE);
2518 return MATCH_ERROR;
2521 if (gfc_match_eos () == MATCH_YES)
2523 *st = ST_PRIVATE;
2524 return MATCH_YES;
2527 *st = ST_ATTR_DECL;
2528 return access_attr_decl (ST_PRIVATE);
2532 match
2533 gfc_match_public (gfc_statement * st)
2536 if (gfc_match ("public") != MATCH_YES)
2537 return MATCH_NO;
2539 if (gfc_match_eos () == MATCH_YES)
2541 *st = ST_PUBLIC;
2542 return MATCH_YES;
2545 *st = ST_ATTR_DECL;
2546 return access_attr_decl (ST_PUBLIC);
2550 /* Workhorse for gfc_match_parameter. */
2552 static match
2553 do_parm (void)
2555 gfc_symbol *sym;
2556 gfc_expr *init;
2557 match m;
2559 m = gfc_match_symbol (&sym, 0);
2560 if (m == MATCH_NO)
2561 gfc_error ("Expected variable name at %C in PARAMETER statement");
2563 if (m != MATCH_YES)
2564 return m;
2566 if (gfc_match_char ('=') == MATCH_NO)
2568 gfc_error ("Expected = sign in PARAMETER statement at %C");
2569 return MATCH_ERROR;
2572 m = gfc_match_init_expr (&init);
2573 if (m == MATCH_NO)
2574 gfc_error ("Expected expression at %C in PARAMETER statement");
2575 if (m != MATCH_YES)
2576 return m;
2578 if (sym->ts.type == BT_UNKNOWN
2579 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2581 m = MATCH_ERROR;
2582 goto cleanup;
2585 if (gfc_check_assign_symbol (sym, init) == FAILURE
2586 || gfc_add_flavor (&sym->attr, FL_PARAMETER, NULL) == FAILURE)
2588 m = MATCH_ERROR;
2589 goto cleanup;
2592 sym->value = init;
2593 return MATCH_YES;
2595 cleanup:
2596 gfc_free_expr (init);
2597 return m;
2601 /* Match a parameter statement, with the weird syntax that these have. */
2603 match
2604 gfc_match_parameter (void)
2606 match m;
2608 if (gfc_match_char ('(') == MATCH_NO)
2609 return MATCH_NO;
2611 for (;;)
2613 m = do_parm ();
2614 if (m != MATCH_YES)
2615 break;
2617 if (gfc_match (" )%t") == MATCH_YES)
2618 break;
2620 if (gfc_match_char (',') != MATCH_YES)
2622 gfc_error ("Unexpected characters in PARAMETER statement at %C");
2623 m = MATCH_ERROR;
2624 break;
2628 return m;
2632 /* Save statements have a special syntax. */
2634 match
2635 gfc_match_save (void)
2637 char n[GFC_MAX_SYMBOL_LEN+1];
2638 gfc_common_head *c;
2639 gfc_symbol *sym;
2640 match m;
2642 if (gfc_match_eos () == MATCH_YES)
2644 if (gfc_current_ns->seen_save)
2646 gfc_error ("Blanket SAVE statement at %C follows previous "
2647 "SAVE statement");
2649 return MATCH_ERROR;
2652 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
2653 return MATCH_YES;
2656 if (gfc_current_ns->save_all)
2658 gfc_error ("SAVE statement at %C follows blanket SAVE statement");
2659 return MATCH_ERROR;
2662 gfc_match (" ::");
2664 for (;;)
2666 m = gfc_match_symbol (&sym, 0);
2667 switch (m)
2669 case MATCH_YES:
2670 if (gfc_add_save (&sym->attr, &gfc_current_locus) == FAILURE)
2671 return MATCH_ERROR;
2672 goto next_item;
2674 case MATCH_NO:
2675 break;
2677 case MATCH_ERROR:
2678 return MATCH_ERROR;
2681 m = gfc_match (" / %n /", &n);
2682 if (m == MATCH_ERROR)
2683 return MATCH_ERROR;
2684 if (m == MATCH_NO)
2685 goto syntax;
2687 c = gfc_get_common (n);
2689 if (c->use_assoc)
2691 gfc_error("COMMON block '%s' at %C is already USE associated", n);
2692 return MATCH_ERROR;
2695 c->saved = 1;
2697 gfc_current_ns->seen_save = 1;
2699 next_item:
2700 if (gfc_match_eos () == MATCH_YES)
2701 break;
2702 if (gfc_match_char (',') != MATCH_YES)
2703 goto syntax;
2706 return MATCH_YES;
2708 syntax:
2709 gfc_error ("Syntax error in SAVE statement at %C");
2710 return MATCH_ERROR;
2714 /* Match a module procedure statement. Note that we have to modify
2715 symbols in the parent's namespace because the current one was there
2716 to receive symbols that are in a interface's formal argument list. */
2718 match
2719 gfc_match_modproc (void)
2721 char name[GFC_MAX_SYMBOL_LEN + 1];
2722 gfc_symbol *sym;
2723 match m;
2725 if (gfc_state_stack->state != COMP_INTERFACE
2726 || gfc_state_stack->previous == NULL
2727 || current_interface.type == INTERFACE_NAMELESS)
2729 gfc_error
2730 ("MODULE PROCEDURE at %C must be in a generic module interface");
2731 return MATCH_ERROR;
2734 for (;;)
2736 m = gfc_match_name (name);
2737 if (m == MATCH_NO)
2738 goto syntax;
2739 if (m != MATCH_YES)
2740 return MATCH_ERROR;
2742 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
2743 return MATCH_ERROR;
2745 if (sym->attr.proc != PROC_MODULE
2746 && gfc_add_procedure (&sym->attr, PROC_MODULE, NULL) == FAILURE)
2747 return MATCH_ERROR;
2749 if (gfc_add_interface (sym) == FAILURE)
2750 return MATCH_ERROR;
2752 if (gfc_match_eos () == MATCH_YES)
2753 break;
2754 if (gfc_match_char (',') != MATCH_YES)
2755 goto syntax;
2758 return MATCH_YES;
2760 syntax:
2761 gfc_syntax_error (ST_MODULE_PROC);
2762 return MATCH_ERROR;
2766 /* Match the beginning of a derived type declaration. If a type name
2767 was the result of a function, then it is possible to have a symbol
2768 already to be known as a derived type yet have no components. */
2770 match
2771 gfc_match_derived_decl (void)
2773 char name[GFC_MAX_SYMBOL_LEN + 1];
2774 symbol_attribute attr;
2775 gfc_symbol *sym;
2776 match m;
2778 if (gfc_current_state () == COMP_DERIVED)
2779 return MATCH_NO;
2781 gfc_clear_attr (&attr);
2783 loop:
2784 if (gfc_match (" , private") == MATCH_YES)
2786 if (gfc_find_state (COMP_MODULE) == FAILURE)
2788 gfc_error
2789 ("Derived type at %C can only be PRIVATE within a MODULE");
2790 return MATCH_ERROR;
2793 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL) == FAILURE)
2794 return MATCH_ERROR;
2795 goto loop;
2798 if (gfc_match (" , public") == MATCH_YES)
2800 if (gfc_find_state (COMP_MODULE) == FAILURE)
2802 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
2803 return MATCH_ERROR;
2806 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL) == FAILURE)
2807 return MATCH_ERROR;
2808 goto loop;
2811 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
2813 gfc_error ("Expected :: in TYPE definition at %C");
2814 return MATCH_ERROR;
2817 m = gfc_match (" %n%t", name);
2818 if (m != MATCH_YES)
2819 return m;
2821 /* Make sure the name isn't the name of an intrinsic type. The
2822 'double precision' type doesn't get past the name matcher. */
2823 if (strcmp (name, "integer") == 0
2824 || strcmp (name, "real") == 0
2825 || strcmp (name, "character") == 0
2826 || strcmp (name, "logical") == 0
2827 || strcmp (name, "complex") == 0)
2829 gfc_error
2830 ("Type name '%s' at %C cannot be the same as an intrinsic type",
2831 name);
2832 return MATCH_ERROR;
2835 if (gfc_get_symbol (name, NULL, &sym))
2836 return MATCH_ERROR;
2838 if (sym->ts.type != BT_UNKNOWN)
2840 gfc_error ("Derived type name '%s' at %C already has a basic type "
2841 "of %s", sym->name, gfc_typename (&sym->ts));
2842 return MATCH_ERROR;
2845 /* The symbol may already have the derived attribute without the
2846 components. The ways this can happen is via a function
2847 definition, an INTRINSIC statement or a subtype in another
2848 derived type that is a pointer. The first part of the AND clause
2849 is true if a the symbol is not the return value of a function. */
2850 if (sym->attr.flavor != FL_DERIVED
2851 && gfc_add_flavor (&sym->attr, FL_DERIVED, NULL) == FAILURE)
2852 return MATCH_ERROR;
2854 if (sym->components != NULL)
2856 gfc_error
2857 ("Derived type definition of '%s' at %C has already been defined",
2858 sym->name);
2859 return MATCH_ERROR;
2862 if (attr.access != ACCESS_UNKNOWN
2863 && gfc_add_access (&sym->attr, attr.access, NULL) == FAILURE)
2864 return MATCH_ERROR;
2866 gfc_new_block = sym;
2868 return MATCH_YES;