1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector
;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts
;
41 static symbol_attribute current_attr
;
42 static gfc_array_spec
*current_as
;
43 static int colon_seen
;
45 /* The current binding label (if any). */
46 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
47 /* Need to know how many identifiers are on the current data declaration
48 line in case we're given the BIND(C) attribute with a NAME= specifier. */
49 static int num_idents_on_line
;
50 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
51 can supply a name if the curr_binding_label is nil and NAME= was not. */
52 static int has_name_equals
= 0;
54 /* Initializer of the previous enumerator. */
56 static gfc_expr
*last_initializer
;
58 /* History of all the enumerators is maintained, so that
59 kind values of all the enumerators could be updated depending
60 upon the maximum initialized value. */
62 typedef struct enumerator_history
65 gfc_expr
*initializer
;
66 struct enumerator_history
*next
;
70 /* Header of enum history chain. */
72 static enumerator_history
*enum_history
= NULL
;
74 /* Pointer of enum history node containing largest initializer. */
76 static enumerator_history
*max_enum
= NULL
;
78 /* gfc_new_block points to the symbol of a newly matched block. */
80 gfc_symbol
*gfc_new_block
;
83 /********************* DATA statement subroutines *********************/
85 static bool in_match_data
= false;
88 gfc_in_match_data (void)
94 gfc_set_in_match_data (bool set_value
)
96 in_match_data
= set_value
;
99 /* Free a gfc_data_variable structure and everything beneath it. */
102 free_variable (gfc_data_variable
*p
)
104 gfc_data_variable
*q
;
109 gfc_free_expr (p
->expr
);
110 gfc_free_iterator (&p
->iter
, 0);
111 free_variable (p
->list
);
117 /* Free a gfc_data_value structure and everything beneath it. */
120 free_value (gfc_data_value
*p
)
127 gfc_free_expr (p
->expr
);
133 /* Free a list of gfc_data structures. */
136 gfc_free_data (gfc_data
*p
)
143 free_variable (p
->var
);
144 free_value (p
->value
);
150 /* Free all data in a namespace. */
153 gfc_free_data_all (gfc_namespace
*ns
)
166 static match
var_element (gfc_data_variable
*);
168 /* Match a list of variables terminated by an iterator and a right
172 var_list (gfc_data_variable
*parent
)
174 gfc_data_variable
*tail
, var
;
177 m
= var_element (&var
);
178 if (m
== MATCH_ERROR
)
183 tail
= gfc_get_data_variable ();
190 if (gfc_match_char (',') != MATCH_YES
)
193 m
= gfc_match_iterator (&parent
->iter
, 1);
196 if (m
== MATCH_ERROR
)
199 m
= var_element (&var
);
200 if (m
== MATCH_ERROR
)
205 tail
->next
= gfc_get_data_variable ();
211 if (gfc_match_char (')') != MATCH_YES
)
216 gfc_syntax_error (ST_DATA
);
221 /* Match a single element in a data variable list, which can be a
222 variable-iterator list. */
225 var_element (gfc_data_variable
*new)
230 memset (new, 0, sizeof (gfc_data_variable
));
232 if (gfc_match_char ('(') == MATCH_YES
)
233 return var_list (new);
235 m
= gfc_match_variable (&new->expr
, 0);
239 sym
= new->expr
->symtree
->n
.sym
;
241 if (!sym
->attr
.function
&& gfc_current_ns
->parent
242 && gfc_current_ns
->parent
== sym
->ns
)
244 gfc_error ("Host associated variable '%s' may not be in the DATA "
245 "statement at %C", sym
->name
);
249 if (gfc_current_state () != COMP_BLOCK_DATA
250 && sym
->attr
.in_common
251 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
252 "common block variable '%s' in DATA statement at %C",
253 sym
->name
) == FAILURE
)
256 if (gfc_add_data (&sym
->attr
, sym
->name
, &new->expr
->where
) == FAILURE
)
263 /* Match the top-level list of data variables. */
266 top_var_list (gfc_data
*d
)
268 gfc_data_variable var
, *tail
, *new;
275 m
= var_element (&var
);
278 if (m
== MATCH_ERROR
)
281 new = gfc_get_data_variable ();
291 if (gfc_match_char ('/') == MATCH_YES
)
293 if (gfc_match_char (',') != MATCH_YES
)
300 gfc_syntax_error (ST_DATA
);
301 gfc_free_data_all (gfc_current_ns
);
307 match_data_constant (gfc_expr
**result
)
309 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
315 m
= gfc_match_literal_constant (&expr
, 1);
322 if (m
== MATCH_ERROR
)
325 m
= gfc_match_null (result
);
329 old_loc
= gfc_current_locus
;
331 /* Should this be a structure component, try to match it
332 before matching a name. */
333 m
= gfc_match_rvalue (result
);
334 if (m
== MATCH_ERROR
)
337 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
339 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
344 gfc_current_locus
= old_loc
;
346 m
= gfc_match_name (name
);
350 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
354 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
356 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
360 else if (sym
->attr
.flavor
== FL_DERIVED
)
361 return gfc_match_structure_constructor (sym
, result
);
363 *result
= gfc_copy_expr (sym
->value
);
368 /* Match a list of values in a DATA statement. The leading '/' has
369 already been seen at this point. */
372 top_val_list (gfc_data
*data
)
374 gfc_data_value
*new, *tail
;
383 m
= match_data_constant (&expr
);
386 if (m
== MATCH_ERROR
)
389 new = gfc_get_data_value ();
398 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
406 msg
= gfc_extract_int (expr
, &tmp
);
407 gfc_free_expr (expr
);
415 m
= match_data_constant (&tail
->expr
);
418 if (m
== MATCH_ERROR
)
422 if (gfc_match_char ('/') == MATCH_YES
)
424 if (gfc_match_char (',') == MATCH_NO
)
431 gfc_syntax_error (ST_DATA
);
432 gfc_free_data_all (gfc_current_ns
);
437 /* Matches an old style initialization. */
440 match_old_style_init (const char *name
)
447 /* Set up data structure to hold initializers. */
448 gfc_find_sym_tree (name
, NULL
, 0, &st
);
451 newdata
= gfc_get_data ();
452 newdata
->var
= gfc_get_data_variable ();
453 newdata
->var
->expr
= gfc_get_variable_expr (st
);
454 newdata
->where
= gfc_current_locus
;
456 /* Match initial value list. This also eats the terminal '/'. */
457 m
= top_val_list (newdata
);
466 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
471 /* Mark the variable as having appeared in a data statement. */
472 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
478 /* Chain in namespace list of DATA initializers. */
479 newdata
->next
= gfc_current_ns
->data
;
480 gfc_current_ns
->data
= newdata
;
486 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
487 we are matching a DATA statement and are therefore issuing an error
488 if we encounter something unexpected, if not, we're trying to match
489 an old-style initialization expression of the form INTEGER I /2/. */
492 gfc_match_data (void)
497 gfc_set_in_match_data (true);
501 new = gfc_get_data ();
502 new->where
= gfc_current_locus
;
504 m
= top_var_list (new);
508 m
= top_val_list (new);
512 new->next
= gfc_current_ns
->data
;
513 gfc_current_ns
->data
= new;
515 if (gfc_match_eos () == MATCH_YES
)
518 gfc_match_char (','); /* Optional comma */
521 gfc_set_in_match_data (false);
525 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
532 gfc_set_in_match_data (false);
538 /************************ Declaration statements *********************/
540 /* Match an intent specification. Since this can only happen after an
541 INTENT word, a legal intent-spec must follow. */
544 match_intent_spec (void)
547 if (gfc_match (" ( in out )") == MATCH_YES
)
549 if (gfc_match (" ( in )") == MATCH_YES
)
551 if (gfc_match (" ( out )") == MATCH_YES
)
554 gfc_error ("Bad INTENT specification at %C");
555 return INTENT_UNKNOWN
;
559 /* Matches a character length specification, which is either a
560 specification expression or a '*'. */
563 char_len_param_value (gfc_expr
**expr
)
565 if (gfc_match_char ('*') == MATCH_YES
)
571 return gfc_match_expr (expr
);
575 /* A character length is a '*' followed by a literal integer or a
576 char_len_param_value in parenthesis. */
579 match_char_length (gfc_expr
**expr
)
584 m
= gfc_match_char ('*');
588 m
= gfc_match_small_literal_int (&length
, NULL
);
589 if (m
== MATCH_ERROR
)
594 *expr
= gfc_int_expr (length
);
598 if (gfc_match_char ('(') == MATCH_NO
)
601 m
= char_len_param_value (expr
);
602 if (m
== MATCH_ERROR
)
607 if (gfc_match_char (')') == MATCH_NO
)
609 gfc_free_expr (*expr
);
617 gfc_error ("Syntax error in character length specification at %C");
622 /* Special subroutine for finding a symbol. Check if the name is found
623 in the current name space. If not, and we're compiling a function or
624 subroutine and the parent compilation unit is an interface, then check
625 to see if the name we've been given is the name of the interface
626 (located in another namespace). */
629 find_special (const char *name
, gfc_symbol
**result
)
634 i
= gfc_get_symbol (name
, NULL
, result
);
638 if (gfc_current_state () != COMP_SUBROUTINE
639 && gfc_current_state () != COMP_FUNCTION
)
642 s
= gfc_state_stack
->previous
;
646 if (s
->state
!= COMP_INTERFACE
)
649 goto end
; /* Nameless interface. */
651 if (strcmp (name
, s
->sym
->name
) == 0)
662 /* Special subroutine for getting a symbol node associated with a
663 procedure name, used in SUBROUTINE and FUNCTION statements. The
664 symbol is created in the parent using with symtree node in the
665 child unit pointing to the symbol. If the current namespace has no
666 parent, then the symbol is just created in the current unit. */
669 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
675 /* Module functions have to be left in their own namespace because
676 they have potentially (almost certainly!) already been referenced.
677 In this sense, they are rather like external functions. This is
678 fixed up in resolve.c(resolve_entries), where the symbol name-
679 space is set to point to the master function, so that the fake
680 result mechanism can work. */
681 if (module_fcn_entry
)
683 /* Present if entry is declared to be a module procedure. */
684 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
686 rc
= gfc_get_symbol (name
, NULL
, result
);
689 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
692 gfc_current_ns
->refs
++;
694 if (sym
&& !sym
->new && gfc_current_state () != COMP_INTERFACE
)
696 /* Trap another encompassed procedure with the same name. All
697 these conditions are necessary to avoid picking up an entry
698 whose name clashes with that of the encompassing procedure;
699 this is handled using gsymbols to register unique,globally
701 if (sym
->attr
.flavor
!= 0
702 && sym
->attr
.proc
!= 0
703 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
704 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
705 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
706 name
, &sym
->declared_at
);
708 /* Trap a procedure with a name the same as interface in the
709 encompassing scope. */
710 if (sym
->attr
.generic
!= 0
711 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
712 && !sym
->attr
.mod_proc
)
713 gfc_error_now ("Name '%s' at %C is already defined"
714 " as a generic interface at %L",
715 name
, &sym
->declared_at
);
717 /* Trap declarations of attributes in encompassing scope. The
718 signature for this is that ts.kind is set. Legitimate
719 references only set ts.type. */
720 if (sym
->ts
.kind
!= 0
721 && !sym
->attr
.implicit_type
722 && sym
->attr
.proc
== 0
723 && gfc_current_ns
->parent
!= NULL
724 && sym
->attr
.access
== 0
725 && !module_fcn_entry
)
726 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
727 "and must not have attributes declared at %L",
728 name
, &sym
->declared_at
);
731 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
734 /* Module function entries will already have a symtree in
735 the current namespace but will need one at module level. */
736 if (module_fcn_entry
)
738 /* Present if entry is declared to be a module procedure. */
739 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
741 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
744 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
749 /* See if the procedure should be a module procedure. */
751 if (((sym
->ns
->proc_name
!= NULL
752 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
753 && sym
->attr
.proc
!= PROC_MODULE
)
754 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
755 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
756 sym
->name
, NULL
) == FAILURE
)
763 /* Verify that the given symbol representing a parameter is C
764 interoperable, by checking to see if it was marked as such after
765 its declaration. If the given symbol is not interoperable, a
766 warning is reported, thus removing the need to return the status to
767 the calling function. The standard does not require the user use
768 one of the iso_c_binding named constants to declare an
769 interoperable parameter, but we can't be sure if the param is C
770 interop or not if the user doesn't. For example, integer(4) may be
771 legal Fortran, but doesn't have meaning in C. It may interop with
772 a number of the C types, which causes a problem because the
773 compiler can't know which one. This code is almost certainly not
774 portable, and the user will get what they deserve if the C type
775 across platforms isn't always interoperable with integer(4). If
776 the user had used something like integer(c_int) or integer(c_long),
777 the compiler could have automatically handled the varying sizes
781 verify_c_interop_param (gfc_symbol
*sym
)
783 int is_c_interop
= 0;
784 try retval
= SUCCESS
;
786 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
787 Don't repeat the checks here. */
788 if (sym
->attr
.implicit_type
)
791 /* For subroutines or functions that are passed to a BIND(C) procedure,
792 they're interoperable if they're BIND(C) and their params are all
794 if (sym
->attr
.flavor
== FL_PROCEDURE
)
796 if (sym
->attr
.is_bind_c
== 0)
798 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
799 "attribute to be C interoperable", sym
->name
,
800 &(sym
->declared_at
));
806 if (sym
->attr
.is_c_interop
== 1)
807 /* We've already checked this procedure; don't check it again. */
810 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
815 /* See if we've stored a reference to a procedure that owns sym. */
816 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
818 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
821 (verify_c_interop (&(sym
->ts
), sym
->name
, &(sym
->declared_at
))
824 if (is_c_interop
!= 1)
826 /* Make personalized messages to give better feedback. */
827 if (sym
->ts
.type
== BT_DERIVED
)
828 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
829 " procedure '%s' but is not C interoperable "
830 "because derived type '%s' is not C interoperable",
831 sym
->name
, &(sym
->declared_at
),
832 sym
->ns
->proc_name
->name
,
833 sym
->ts
.derived
->name
);
835 gfc_warning ("Variable '%s' at %L is a parameter to the "
836 "BIND(C) procedure '%s' but may not be C "
838 sym
->name
, &(sym
->declared_at
),
839 sym
->ns
->proc_name
->name
);
842 /* Character strings are only C interoperable if they have a
844 if (sym
->ts
.type
== BT_CHARACTER
)
846 gfc_charlen
*cl
= sym
->ts
.cl
;
847 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
848 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
850 gfc_error ("Character argument '%s' at %L "
851 "must be length 1 because "
852 "procedure '%s' is BIND(C)",
853 sym
->name
, &sym
->declared_at
,
854 sym
->ns
->proc_name
->name
);
859 /* We have to make sure that any param to a bind(c) routine does
860 not have the allocatable, pointer, or optional attributes,
861 according to J3/04-007, section 5.1. */
862 if (sym
->attr
.allocatable
== 1)
864 gfc_error ("Variable '%s' at %L cannot have the "
865 "ALLOCATABLE attribute because procedure '%s'"
866 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
867 sym
->ns
->proc_name
->name
);
871 if (sym
->attr
.pointer
== 1)
873 gfc_error ("Variable '%s' at %L cannot have the "
874 "POINTER attribute because procedure '%s'"
875 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
876 sym
->ns
->proc_name
->name
);
880 if (sym
->attr
.optional
== 1)
882 gfc_error ("Variable '%s' at %L cannot have the "
883 "OPTIONAL attribute because procedure '%s'"
884 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
885 sym
->ns
->proc_name
->name
);
889 /* Make sure that if it has the dimension attribute, that it is
890 either assumed size or explicit shape. */
893 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
895 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
896 "argument to the procedure '%s' at %L because "
897 "the procedure is BIND(C)", sym
->name
,
898 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
899 &(sym
->ns
->proc_name
->declared_at
));
903 if (sym
->as
->type
== AS_DEFERRED
)
905 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
906 "argument to the procedure '%s' at %L because "
907 "the procedure is BIND(C)", sym
->name
,
908 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
909 &(sym
->ns
->proc_name
->declared_at
));
920 /* Function called by variable_decl() that adds a name to the symbol table. */
923 build_sym (const char *name
, gfc_charlen
*cl
,
924 gfc_array_spec
**as
, locus
*var_locus
)
926 symbol_attribute attr
;
929 if (gfc_get_symbol (name
, NULL
, &sym
))
932 /* Start updating the symbol table. Add basic type attribute if present. */
933 if (current_ts
.type
!= BT_UNKNOWN
934 && (sym
->attr
.implicit_type
== 0
935 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
936 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
939 if (sym
->ts
.type
== BT_CHARACTER
)
942 /* Add dimension attribute if present. */
943 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
947 /* Add attribute to symbol. The copy is so that we can reset the
948 dimension attribute. */
952 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
955 /* Finish any work that may need to be done for the binding label,
956 if it's a bind(c). The bind(c) attr is found before the symbol
957 is made, and before the symbol name (for data decls), so the
958 current_ts is holding the binding label, or nothing if the
959 name= attr wasn't given. Therefore, test here if we're dealing
960 with a bind(c) and make sure the binding label is set correctly. */
961 if (sym
->attr
.is_bind_c
== 1)
963 if (sym
->binding_label
[0] == '\0')
965 /* Here, we're not checking the numIdents (the last param).
966 This could be an error we're letting slip through! */
967 if (set_binding_label (sym
->binding_label
, sym
->name
, 1) == FAILURE
)
972 /* See if we know we're in a common block, and if it's a bind(c)
973 common then we need to make sure we're an interoperable type. */
974 if (sym
->attr
.in_common
== 1)
976 /* Test the common block object. */
977 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
978 && sym
->ts
.is_c_interop
!= 1)
980 gfc_error_now ("Variable '%s' in common block '%s' at %C "
981 "must be declared with a C interoperable "
982 "kind since common block '%s' is BIND(C)",
983 sym
->name
, sym
->common_block
->name
,
984 sym
->common_block
->name
);
989 sym
->attr
.implied_index
= 0;
995 /* Set character constant to the given length. The constant will be padded or
999 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, bool array
)
1004 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1005 gcc_assert (expr
->ts
.type
== BT_CHARACTER
&& expr
->ts
.kind
== 1);
1007 slen
= expr
->value
.character
.length
;
1010 s
= gfc_getmem (len
+ 1);
1011 memcpy (s
, expr
->value
.character
.string
, MIN (len
, slen
));
1013 memset (&s
[slen
], ' ', len
- slen
);
1015 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1016 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1017 "(%d/%d)", &expr
->where
, slen
, len
);
1019 /* Apply the standard by 'hand' otherwise it gets cleared for
1021 if (array
&& slen
< len
&& !(gfc_option
.allow_std
& GFC_STD_GNU
))
1022 gfc_error_now ("The CHARACTER elements of the array constructor "
1023 "at %L must have the same length (%d/%d)",
1024 &expr
->where
, slen
, len
);
1027 gfc_free (expr
->value
.character
.string
);
1028 expr
->value
.character
.string
= s
;
1029 expr
->value
.character
.length
= len
;
1034 /* Function to create and update the enumerator history
1035 using the information passed as arguments.
1036 Pointer "max_enum" is also updated, to point to
1037 enum history node containing largest initializer.
1039 SYM points to the symbol node of enumerator.
1040 INIT points to its enumerator value. */
1043 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1045 enumerator_history
*new_enum_history
;
1046 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1048 new_enum_history
= gfc_getmem (sizeof (enumerator_history
));
1050 new_enum_history
->sym
= sym
;
1051 new_enum_history
->initializer
= init
;
1052 new_enum_history
->next
= NULL
;
1054 if (enum_history
== NULL
)
1056 enum_history
= new_enum_history
;
1057 max_enum
= enum_history
;
1061 new_enum_history
->next
= enum_history
;
1062 enum_history
= new_enum_history
;
1064 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1065 new_enum_history
->initializer
->value
.integer
) < 0)
1066 max_enum
= new_enum_history
;
1071 /* Function to free enum kind history. */
1074 gfc_free_enum_history (void)
1076 enumerator_history
*current
= enum_history
;
1077 enumerator_history
*next
;
1079 while (current
!= NULL
)
1081 next
= current
->next
;
1086 enum_history
= NULL
;
1090 /* Function called by variable_decl() that adds an initialization
1091 expression to a symbol. */
1094 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1096 symbol_attribute attr
;
1101 if (find_special (name
, &sym
))
1106 /* If this symbol is confirming an implicit parameter type,
1107 then an initialization expression is not allowed. */
1108 if (attr
.flavor
== FL_PARAMETER
1109 && sym
->value
!= NULL
1112 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1121 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1128 /* An initializer is required for PARAMETER declarations. */
1129 if (attr
.flavor
== FL_PARAMETER
)
1131 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1137 /* If a variable appears in a DATA block, it cannot have an
1141 gfc_error ("Variable '%s' at %C with an initializer already "
1142 "appears in a DATA statement", sym
->name
);
1146 /* Check if the assignment can happen. This has to be put off
1147 until later for a derived type variable. */
1148 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1149 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1152 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.cl
)
1154 /* Update symbol character length according initializer. */
1155 if (sym
->ts
.cl
->length
== NULL
)
1157 /* If there are multiple CHARACTER variables declared on the
1158 same line, we don't want them to share the same length. */
1159 sym
->ts
.cl
= gfc_get_charlen ();
1160 sym
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1161 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1163 if (sym
->attr
.flavor
== FL_PARAMETER
1164 && init
->expr_type
== EXPR_ARRAY
)
1165 sym
->ts
.cl
->length
= gfc_copy_expr (init
->ts
.cl
->length
);
1167 /* Update initializer character length according symbol. */
1168 else if (sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1170 int len
= mpz_get_si (sym
->ts
.cl
->length
->value
.integer
);
1171 gfc_constructor
* p
;
1173 if (init
->expr_type
== EXPR_CONSTANT
)
1174 gfc_set_constant_character_len (len
, init
, false);
1175 else if (init
->expr_type
== EXPR_ARRAY
)
1177 /* Build a new charlen to prevent simplification from
1178 deleting the length before it is resolved. */
1179 init
->ts
.cl
= gfc_get_charlen ();
1180 init
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1181 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1182 init
->ts
.cl
->length
= gfc_copy_expr (sym
->ts
.cl
->length
);
1184 for (p
= init
->value
.constructor
; p
; p
= p
->next
)
1185 gfc_set_constant_character_len (len
, p
->expr
, false);
1190 /* Need to check if the expression we initialized this
1191 to was one of the iso_c_binding named constants. If so,
1192 and we're a parameter (constant), let it be iso_c.
1194 integer(c_int), parameter :: my_int = c_int
1195 integer(my_int) :: my_int_2
1196 If we mark my_int as iso_c (since we can see it's value
1197 is equal to one of the named constants), then my_int_2
1198 will be considered C interoperable. */
1199 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1201 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1202 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1203 /* attr bits needed for module files. */
1204 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1205 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1206 if (init
->ts
.is_iso_c
)
1207 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1210 /* Add initializer. Make sure we keep the ranks sane. */
1211 if (sym
->attr
.dimension
&& init
->rank
== 0)
1217 if (sym
->attr
.flavor
== FL_PARAMETER
1218 && init
->expr_type
== EXPR_CONSTANT
1219 && spec_size (sym
->as
, &size
) == SUCCESS
1220 && mpz_cmp_si (size
, 0) > 0)
1222 array
= gfc_start_constructor (init
->ts
.type
, init
->ts
.kind
,
1225 array
->value
.constructor
= c
= NULL
;
1226 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1228 if (array
->value
.constructor
== NULL
)
1230 array
->value
.constructor
= c
= gfc_get_constructor ();
1235 c
->next
= gfc_get_constructor ();
1237 c
->expr
= gfc_copy_expr (init
);
1241 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1242 for (n
= 0; n
< sym
->as
->rank
; n
++)
1243 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1248 init
->rank
= sym
->as
->rank
;
1252 if (sym
->attr
.save
== SAVE_NONE
)
1253 sym
->attr
.save
= SAVE_IMPLICIT
;
1261 /* Function called by variable_decl() that adds a name to a structure
1265 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1266 gfc_array_spec
**as
)
1270 /* If the current symbol is of the same derived type that we're
1271 constructing, it must have the pointer attribute. */
1272 if (current_ts
.type
== BT_DERIVED
1273 && current_ts
.derived
== gfc_current_block ()
1274 && current_attr
.pointer
== 0)
1276 gfc_error ("Component at %C must have the POINTER attribute");
1280 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1282 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1284 gfc_error ("Array component of structure at %C must have explicit "
1285 "or deferred shape");
1290 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1295 gfc_set_component_attr (c
, ¤t_attr
);
1297 c
->initializer
= *init
;
1305 /* Check array components. */
1310 gfc_error ("Allocatable component at %C must be an array");
1319 if (c
->as
->type
!= AS_DEFERRED
)
1321 gfc_error ("Pointer array component of structure at %C must have a "
1326 else if (c
->allocatable
)
1328 if (c
->as
->type
!= AS_DEFERRED
)
1330 gfc_error ("Allocatable component of structure at %C must have a "
1337 if (c
->as
->type
!= AS_EXPLICIT
)
1339 gfc_error ("Array component of structure at %C must have an "
1349 /* Match a 'NULL()', and possibly take care of some side effects. */
1352 gfc_match_null (gfc_expr
**result
)
1358 m
= gfc_match (" null ( )");
1362 /* The NULL symbol now has to be/become an intrinsic function. */
1363 if (gfc_get_symbol ("null", NULL
, &sym
))
1365 gfc_error ("NULL() initialization at %C is ambiguous");
1369 gfc_intrinsic_symbol (sym
);
1371 if (sym
->attr
.proc
!= PROC_INTRINSIC
1372 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1373 sym
->name
, NULL
) == FAILURE
1374 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1377 e
= gfc_get_expr ();
1378 e
->where
= gfc_current_locus
;
1379 e
->expr_type
= EXPR_NULL
;
1380 e
->ts
.type
= BT_UNKNOWN
;
1388 /* Match a variable name with an optional initializer. When this
1389 subroutine is called, a variable is expected to be parsed next.
1390 Depending on what is happening at the moment, updates either the
1391 symbol table or the current interface. */
1394 variable_decl (int elem
)
1396 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1397 gfc_expr
*initializer
, *char_len
;
1399 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1410 old_locus
= gfc_current_locus
;
1412 /* When we get here, we've just matched a list of attributes and
1413 maybe a type and a double colon. The next thing we expect to see
1414 is the name of the symbol. */
1415 m
= gfc_match_name (name
);
1419 var_locus
= gfc_current_locus
;
1421 /* Now we could see the optional array spec. or character length. */
1422 m
= gfc_match_array_spec (&as
);
1423 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1424 cp_as
= gfc_copy_array_spec (as
);
1425 else if (m
== MATCH_ERROR
)
1429 as
= gfc_copy_array_spec (current_as
);
1434 if (current_ts
.type
== BT_CHARACTER
)
1436 switch (match_char_length (&char_len
))
1439 cl
= gfc_get_charlen ();
1440 cl
->next
= gfc_current_ns
->cl_list
;
1441 gfc_current_ns
->cl_list
= cl
;
1443 cl
->length
= char_len
;
1446 /* Non-constant lengths need to be copied after the first
1449 if (elem
> 1 && current_ts
.cl
->length
1450 && current_ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
1452 cl
= gfc_get_charlen ();
1453 cl
->next
= gfc_current_ns
->cl_list
;
1454 gfc_current_ns
->cl_list
= cl
;
1455 cl
->length
= gfc_copy_expr (current_ts
.cl
->length
);
1467 /* If this symbol has already shown up in a Cray Pointer declaration,
1468 then we want to set the type & bail out. */
1469 if (gfc_option
.flag_cray_pointer
)
1471 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1472 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1474 sym
->ts
.type
= current_ts
.type
;
1475 sym
->ts
.kind
= current_ts
.kind
;
1477 sym
->ts
.derived
= current_ts
.derived
;
1478 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1479 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1482 /* Check to see if we have an array specification. */
1485 if (sym
->as
!= NULL
)
1487 gfc_error ("Duplicate array spec for Cray pointee at %C");
1488 gfc_free_array_spec (cp_as
);
1494 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1495 gfc_internal_error ("Couldn't set pointee array spec.");
1497 /* Fix the array spec. */
1498 m
= gfc_mod_pointee_as (sym
->as
);
1499 if (m
== MATCH_ERROR
)
1507 gfc_free_array_spec (cp_as
);
1512 /* OK, we've successfully matched the declaration. Now put the
1513 symbol in the current namespace, because it might be used in the
1514 optional initialization expression for this symbol, e.g. this is
1517 integer, parameter :: i = huge(i)
1519 This is only true for parameters or variables of a basic type.
1520 For components of derived types, it is not true, so we don't
1521 create a symbol for those yet. If we fail to create the symbol,
1523 if (gfc_current_state () != COMP_DERIVED
1524 && build_sym (name
, cl
, &as
, &var_locus
) == FAILURE
)
1530 /* An interface body specifies all of the procedure's
1531 characteristics and these shall be consistent with those
1532 specified in the procedure definition, except that the interface
1533 may specify a procedure that is not pure if the procedure is
1534 defined to be pure(12.3.2). */
1535 if (current_ts
.type
== BT_DERIVED
1536 && gfc_current_ns
->proc_name
1537 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1538 && current_ts
.derived
->ns
!= gfc_current_ns
1539 && !gfc_current_ns
->has_import_set
)
1541 gfc_error ("the type of '%s' at %C has not been declared within the "
1547 /* In functions that have a RESULT variable defined, the function
1548 name always refers to function calls. Therefore, the name is
1549 not allowed to appear in specification statements. */
1550 if (gfc_current_state () == COMP_FUNCTION
1551 && gfc_current_block () != NULL
1552 && gfc_current_block ()->result
!= NULL
1553 && gfc_current_block ()->result
!= gfc_current_block ()
1554 && strcmp (gfc_current_block ()->name
, name
) == 0)
1556 gfc_error ("Function name '%s' not allowed at %C", name
);
1561 /* We allow old-style initializations of the form
1562 integer i /2/, j(4) /3*3, 1/
1563 (if no colon has been seen). These are different from data
1564 statements in that initializers are only allowed to apply to the
1565 variable immediately preceding, i.e.
1567 is not allowed. Therefore we have to do some work manually, that
1568 could otherwise be left to the matchers for DATA statements. */
1570 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1572 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1573 "initialization at %C") == FAILURE
)
1576 return match_old_style_init (name
);
1579 /* The double colon must be present in order to have initializers.
1580 Otherwise the statement is ambiguous with an assignment statement. */
1583 if (gfc_match (" =>") == MATCH_YES
)
1585 if (!current_attr
.pointer
)
1587 gfc_error ("Initialization at %C isn't for a pointer variable");
1592 m
= gfc_match_null (&initializer
);
1595 gfc_error ("Pointer initialization requires a NULL() at %C");
1599 if (gfc_pure (NULL
))
1601 gfc_error ("Initialization of pointer at %C is not allowed in "
1602 "a PURE procedure");
1610 else if (gfc_match_char ('=') == MATCH_YES
)
1612 if (current_attr
.pointer
)
1614 gfc_error ("Pointer initialization at %C requires '=>', "
1620 m
= gfc_match_init_expr (&initializer
);
1623 gfc_error ("Expected an initialization expression at %C");
1627 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
))
1629 gfc_error ("Initialization of variable at %C is not allowed in "
1630 "a PURE procedure");
1639 if (initializer
!= NULL
&& current_attr
.allocatable
1640 && gfc_current_state () == COMP_DERIVED
)
1642 gfc_error ("Initialization of allocatable component at %C is not "
1648 /* Add the initializer. Note that it is fine if initializer is
1649 NULL here, because we sometimes also need to check if a
1650 declaration *must* have an initialization expression. */
1651 if (gfc_current_state () != COMP_DERIVED
)
1652 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
1655 if (current_ts
.type
== BT_DERIVED
1656 && !current_attr
.pointer
&& !initializer
)
1657 initializer
= gfc_default_initializer (¤t_ts
);
1658 t
= build_struct (name
, cl
, &initializer
, &as
);
1661 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
1664 /* Free stuff up and return. */
1665 gfc_free_expr (initializer
);
1666 gfc_free_array_spec (as
);
1672 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1673 This assumes that the byte size is equal to the kind number for
1674 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1677 gfc_match_old_kind_spec (gfc_typespec
*ts
)
1682 if (gfc_match_char ('*') != MATCH_YES
)
1685 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
1689 original_kind
= ts
->kind
;
1691 /* Massage the kind numbers for complex types. */
1692 if (ts
->type
== BT_COMPLEX
)
1696 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1697 gfc_basic_typename (ts
->type
), original_kind
);
1703 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1705 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1706 gfc_basic_typename (ts
->type
), original_kind
);
1710 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
1711 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
1718 /* Match a kind specification. Since kinds are generally optional, we
1719 usually return MATCH_NO if something goes wrong. If a "kind="
1720 string is found, then we know we have an error. */
1723 gfc_match_kind_spec (gfc_typespec
*ts
)
1733 where
= gfc_current_locus
;
1735 if (gfc_match_char ('(') == MATCH_NO
)
1738 /* Also gobbles optional text. */
1739 if (gfc_match (" kind = ") == MATCH_YES
)
1742 n
= gfc_match_init_expr (&e
);
1744 gfc_error ("Expected initialization expression at %C");
1750 gfc_error ("Expected scalar initialization expression at %C");
1755 msg
= gfc_extract_int (e
, &ts
->kind
);
1763 /* Before throwing away the expression, let's see if we had a
1764 C interoperable kind (and store the fact). */
1765 if (e
->ts
.is_c_interop
== 1)
1767 /* Mark this as c interoperable if being declared with one
1768 of the named constants from iso_c_binding. */
1769 ts
->is_c_interop
= e
->ts
.is_iso_c
;
1770 ts
->f90_type
= e
->ts
.f90_type
;
1776 /* Ignore errors to this point, if we've gotten here. This means
1777 we ignore the m=MATCH_ERROR from above. */
1778 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1780 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
1781 gfc_basic_typename (ts
->type
));
1784 else if (gfc_match_char (')') != MATCH_YES
)
1786 gfc_error ("Missing right parenthesis at %C");
1790 /* All tests passed. */
1793 if(m
== MATCH_ERROR
)
1794 gfc_current_locus
= where
;
1796 /* Return what we know from the test(s). */
1801 gfc_current_locus
= where
;
1806 /* Match the various kind/length specifications in a CHARACTER
1807 declaration. We don't return MATCH_NO. */
1810 match_char_spec (gfc_typespec
*ts
)
1812 int kind
, seen_length
;
1816 gfc_expr
*kind_expr
= NULL
;
1817 kind
= gfc_default_character_kind
;
1821 /* Try the old-style specification first. */
1822 old_char_selector
= 0;
1824 m
= match_char_length (&len
);
1828 old_char_selector
= 1;
1833 m
= gfc_match_char ('(');
1836 m
= MATCH_YES
; /* Character without length is a single char. */
1840 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1841 if (gfc_match (" kind =") == MATCH_YES
)
1843 m
= gfc_match_small_int_expr(&kind
, &kind_expr
);
1845 if (m
== MATCH_ERROR
)
1850 if (gfc_match (" , len =") == MATCH_NO
)
1853 m
= char_len_param_value (&len
);
1856 if (m
== MATCH_ERROR
)
1863 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1864 if (gfc_match (" len =") == MATCH_YES
)
1866 m
= char_len_param_value (&len
);
1869 if (m
== MATCH_ERROR
)
1873 if (gfc_match_char (')') == MATCH_YES
)
1876 if (gfc_match (" , kind =") != MATCH_YES
)
1879 gfc_match_small_int_expr(&kind
, &kind_expr
);
1881 if (gfc_validate_kind (BT_CHARACTER
, kind
, true) < 0)
1883 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind
);
1890 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1891 m
= char_len_param_value (&len
);
1894 if (m
== MATCH_ERROR
)
1898 m
= gfc_match_char (')');
1902 if (gfc_match_char (',') != MATCH_YES
)
1905 gfc_match (" kind ="); /* Gobble optional text. */
1907 m
= gfc_match_small_int_expr(&kind
, &kind_expr
);
1908 if (m
== MATCH_ERROR
)
1914 /* Require a right-paren at this point. */
1915 m
= gfc_match_char (')');
1920 gfc_error ("Syntax error in CHARACTER declaration at %C");
1922 gfc_free_expr (len
);
1926 if (gfc_validate_kind (BT_CHARACTER
, kind
, true) < 0)
1928 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind
);
1932 if (seen_length
== 1 && len
!= NULL
1933 && len
->ts
.type
!= BT_INTEGER
&& len
->ts
.type
!= BT_UNKNOWN
)
1935 gfc_error ("Expression at %C must be of INTEGER type");
1941 gfc_free_expr (len
);
1942 gfc_free_expr (kind_expr
);
1946 /* Do some final massaging of the length values. */
1947 cl
= gfc_get_charlen ();
1948 cl
->next
= gfc_current_ns
->cl_list
;
1949 gfc_current_ns
->cl_list
= cl
;
1951 if (seen_length
== 0)
1952 cl
->length
= gfc_int_expr (1);
1959 /* We have to know if it was a c interoperable kind so we can
1960 do accurate type checking of bind(c) procs, etc. */
1961 if (kind_expr
!= NULL
)
1963 /* Mark this as c interoperable if being declared with one
1964 of the named constants from iso_c_binding. */
1965 ts
->is_c_interop
= kind_expr
->ts
.is_iso_c
;
1966 gfc_free_expr (kind_expr
);
1968 else if (len
!= NULL
)
1970 /* Here, we might have parsed something such as:
1972 In this case, the parsing code above grabs the c_char when
1973 looking for the length (line 1690, roughly). it's the last
1974 testcase for parsing the kind params of a character variable.
1975 However, it's not actually the length. this seems like it
1977 To see if the user used a C interop kind, test the expr
1978 of the so called length, and see if it's C interoperable. */
1979 ts
->is_c_interop
= len
->ts
.is_iso_c
;
1986 /* Matches a type specification. If successful, sets the ts structure
1987 to the matched specification. This is necessary for FUNCTION and
1988 IMPLICIT statements.
1990 If implicit_flag is nonzero, then we don't check for the optional
1991 kind specification. Not doing so is needed for matching an IMPLICIT
1992 statement correctly. */
1995 match_type_spec (gfc_typespec
*ts
, int implicit_flag
)
1997 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2004 /* Clear the current binding label, in case one is given. */
2005 curr_binding_label
[0] = '\0';
2007 if (gfc_match (" byte") == MATCH_YES
)
2009 if (gfc_notify_std(GFC_STD_GNU
, "Extension: BYTE type at %C")
2013 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2015 gfc_error ("BYTE type used at %C "
2016 "is not available on the target machine");
2020 ts
->type
= BT_INTEGER
;
2025 if (gfc_match (" integer") == MATCH_YES
)
2027 ts
->type
= BT_INTEGER
;
2028 ts
->kind
= gfc_default_integer_kind
;
2032 if (gfc_match (" character") == MATCH_YES
)
2034 ts
->type
= BT_CHARACTER
;
2035 if (implicit_flag
== 0)
2036 return match_char_spec (ts
);
2041 if (gfc_match (" real") == MATCH_YES
)
2044 ts
->kind
= gfc_default_real_kind
;
2048 if (gfc_match (" double precision") == MATCH_YES
)
2051 ts
->kind
= gfc_default_double_kind
;
2055 if (gfc_match (" complex") == MATCH_YES
)
2057 ts
->type
= BT_COMPLEX
;
2058 ts
->kind
= gfc_default_complex_kind
;
2062 if (gfc_match (" double complex") == MATCH_YES
)
2064 if (gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C does not "
2065 "conform to the Fortran 95 standard") == FAILURE
)
2068 ts
->type
= BT_COMPLEX
;
2069 ts
->kind
= gfc_default_double_kind
;
2073 if (gfc_match (" logical") == MATCH_YES
)
2075 ts
->type
= BT_LOGICAL
;
2076 ts
->kind
= gfc_default_logical_kind
;
2080 m
= gfc_match (" type ( %n )", name
);
2084 /* Search for the name but allow the components to be defined later. */
2085 if (gfc_get_ha_symbol (name
, &sym
))
2087 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2091 if (sym
->attr
.flavor
!= FL_DERIVED
2092 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2095 ts
->type
= BT_DERIVED
;
2102 /* For all types except double, derived and character, look for an
2103 optional kind specifier. MATCH_NO is actually OK at this point. */
2104 if (implicit_flag
== 1)
2107 if (gfc_current_form
== FORM_FREE
)
2109 c
= gfc_peek_char();
2110 if (!gfc_is_whitespace(c
) && c
!= '*' && c
!= '('
2111 && c
!= ':' && c
!= ',')
2115 m
= gfc_match_kind_spec (ts
);
2116 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2117 m
= gfc_match_old_kind_spec (ts
);
2120 m
= MATCH_YES
; /* No kind specifier found. */
2126 /* Match an IMPLICIT NONE statement. Actually, this statement is
2127 already matched in parse.c, or we would not end up here in the
2128 first place. So the only thing we need to check, is if there is
2129 trailing garbage. If not, the match is successful. */
2132 gfc_match_implicit_none (void)
2134 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2138 /* Match the letter range(s) of an IMPLICIT statement. */
2141 match_implicit_range (void)
2143 int c
, c1
, c2
, inner
;
2146 cur_loc
= gfc_current_locus
;
2148 gfc_gobble_whitespace ();
2149 c
= gfc_next_char ();
2152 gfc_error ("Missing character range in IMPLICIT at %C");
2159 gfc_gobble_whitespace ();
2160 c1
= gfc_next_char ();
2164 gfc_gobble_whitespace ();
2165 c
= gfc_next_char ();
2170 inner
= 0; /* Fall through. */
2177 gfc_gobble_whitespace ();
2178 c2
= gfc_next_char ();
2182 gfc_gobble_whitespace ();
2183 c
= gfc_next_char ();
2185 if ((c
!= ',') && (c
!= ')'))
2198 gfc_error ("Letters must be in alphabetic order in "
2199 "IMPLICIT statement at %C");
2203 /* See if we can add the newly matched range to the pending
2204 implicits from this IMPLICIT statement. We do not check for
2205 conflicts with whatever earlier IMPLICIT statements may have
2206 set. This is done when we've successfully finished matching
2208 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2215 gfc_syntax_error (ST_IMPLICIT
);
2217 gfc_current_locus
= cur_loc
;
2222 /* Match an IMPLICIT statement, storing the types for
2223 gfc_set_implicit() if the statement is accepted by the parser.
2224 There is a strange looking, but legal syntactic construction
2225 possible. It looks like:
2227 IMPLICIT INTEGER (a-b) (c-d)
2229 This is legal if "a-b" is a constant expression that happens to
2230 equal one of the legal kinds for integers. The real problem
2231 happens with an implicit specification that looks like:
2233 IMPLICIT INTEGER (a-b)
2235 In this case, a typespec matcher that is "greedy" (as most of the
2236 matchers are) gobbles the character range as a kindspec, leaving
2237 nothing left. We therefore have to go a bit more slowly in the
2238 matching process by inhibiting the kindspec checking during
2239 typespec matching and checking for a kind later. */
2242 gfc_match_implicit (void)
2249 /* We don't allow empty implicit statements. */
2250 if (gfc_match_eos () == MATCH_YES
)
2252 gfc_error ("Empty IMPLICIT statement at %C");
2258 /* First cleanup. */
2259 gfc_clear_new_implicit ();
2261 /* A basic type is mandatory here. */
2262 m
= match_type_spec (&ts
, 1);
2263 if (m
== MATCH_ERROR
)
2268 cur_loc
= gfc_current_locus
;
2269 m
= match_implicit_range ();
2273 /* We may have <TYPE> (<RANGE>). */
2274 gfc_gobble_whitespace ();
2275 c
= gfc_next_char ();
2276 if ((c
== '\n') || (c
== ','))
2278 /* Check for CHARACTER with no length parameter. */
2279 if (ts
.type
== BT_CHARACTER
&& !ts
.cl
)
2281 ts
.kind
= gfc_default_character_kind
;
2282 ts
.cl
= gfc_get_charlen ();
2283 ts
.cl
->next
= gfc_current_ns
->cl_list
;
2284 gfc_current_ns
->cl_list
= ts
.cl
;
2285 ts
.cl
->length
= gfc_int_expr (1);
2288 /* Record the Successful match. */
2289 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2294 gfc_current_locus
= cur_loc
;
2297 /* Discard the (incorrectly) matched range. */
2298 gfc_clear_new_implicit ();
2300 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2301 if (ts
.type
== BT_CHARACTER
)
2302 m
= match_char_spec (&ts
);
2305 m
= gfc_match_kind_spec (&ts
);
2308 m
= gfc_match_old_kind_spec (&ts
);
2309 if (m
== MATCH_ERROR
)
2315 if (m
== MATCH_ERROR
)
2318 m
= match_implicit_range ();
2319 if (m
== MATCH_ERROR
)
2324 gfc_gobble_whitespace ();
2325 c
= gfc_next_char ();
2326 if ((c
!= '\n') && (c
!= ','))
2329 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2337 gfc_syntax_error (ST_IMPLICIT
);
2345 gfc_match_import (void)
2347 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2352 if (gfc_current_ns
->proc_name
== NULL
2353 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2355 gfc_error ("IMPORT statement at %C only permitted in "
2356 "an INTERFACE body");
2360 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2364 if (gfc_match_eos () == MATCH_YES
)
2366 /* All host variables should be imported. */
2367 gfc_current_ns
->has_import_set
= 1;
2371 if (gfc_match (" ::") == MATCH_YES
)
2373 if (gfc_match_eos () == MATCH_YES
)
2375 gfc_error ("Expecting list of named entities at %C");
2382 m
= gfc_match (" %n", name
);
2386 if (gfc_current_ns
->parent
!= NULL
2387 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2389 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2392 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2393 && gfc_find_symbol (name
,
2394 gfc_current_ns
->proc_name
->ns
->parent
,
2397 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2403 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2404 "at %C - does not exist.", name
);
2408 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
2410 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2415 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
2418 sym
->ns
= gfc_current_ns
;
2430 if (gfc_match_eos () == MATCH_YES
)
2432 if (gfc_match_char (',') != MATCH_YES
)
2439 gfc_error ("Syntax error in IMPORT statement at %C");
2444 /* Matches an attribute specification including array specs. If
2445 successful, leaves the variables current_attr and current_as
2446 holding the specification. Also sets the colon_seen variable for
2447 later use by matchers associated with initializations.
2449 This subroutine is a little tricky in the sense that we don't know
2450 if we really have an attr-spec until we hit the double colon.
2451 Until that time, we can only return MATCH_NO. This forces us to
2452 check for duplicate specification at this level. */
2455 match_attr_spec (void)
2457 /* Modifiers that can exist in a type statement. */
2459 { GFC_DECL_BEGIN
= 0,
2460 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
2461 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
2462 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
2463 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
2464 DECL_IS_BIND_C
, DECL_COLON
, DECL_NONE
,
2465 GFC_DECL_END
/* Sentinel */
2469 /* GFC_DECL_END is the sentinel, index starts at 0. */
2470 #define NUM_DECL GFC_DECL_END
2472 static mstring decls
[] = {
2473 minit (", allocatable", DECL_ALLOCATABLE
),
2474 minit (", dimension", DECL_DIMENSION
),
2475 minit (", external", DECL_EXTERNAL
),
2476 minit (", intent ( in )", DECL_IN
),
2477 minit (", intent ( out )", DECL_OUT
),
2478 minit (", intent ( in out )", DECL_INOUT
),
2479 minit (", intrinsic", DECL_INTRINSIC
),
2480 minit (", optional", DECL_OPTIONAL
),
2481 minit (", parameter", DECL_PARAMETER
),
2482 minit (", pointer", DECL_POINTER
),
2483 minit (", protected", DECL_PROTECTED
),
2484 minit (", private", DECL_PRIVATE
),
2485 minit (", public", DECL_PUBLIC
),
2486 minit (", save", DECL_SAVE
),
2487 minit (", target", DECL_TARGET
),
2488 minit (", value", DECL_VALUE
),
2489 minit (", volatile", DECL_VOLATILE
),
2490 minit ("::", DECL_COLON
),
2491 minit (NULL
, DECL_NONE
)
2494 locus start
, seen_at
[NUM_DECL
];
2502 gfc_clear_attr (¤t_attr
);
2503 start
= gfc_current_locus
;
2508 /* See if we get all of the keywords up to the final double colon. */
2509 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2514 d
= (decl_types
) gfc_match_strings (decls
);
2518 /* See if we can find the bind(c) since all else failed.
2519 We need to skip over any whitespace and stop on the ','. */
2520 gfc_gobble_whitespace ();
2521 peek_char
= gfc_peek_char ();
2522 if (peek_char
== ',')
2524 /* Chomp the comma. */
2525 peek_char
= gfc_next_char ();
2526 /* Try and match the bind(c). */
2527 if (gfc_match_bind_c (NULL
) == MATCH_YES
)
2532 if (d
== DECL_NONE
|| d
== DECL_COLON
)
2536 seen_at
[d
] = gfc_current_locus
;
2538 if (d
== DECL_DIMENSION
)
2540 m
= gfc_match_array_spec (¤t_as
);
2544 gfc_error ("Missing dimension specification at %C");
2548 if (m
== MATCH_ERROR
)
2553 /* No double colon, so assume that we've been looking at something
2554 else the whole time. */
2561 /* Since we've seen a double colon, we have to be looking at an
2562 attr-spec. This means that we can now issue errors. */
2563 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2568 case DECL_ALLOCATABLE
:
2569 attr
= "ALLOCATABLE";
2571 case DECL_DIMENSION
:
2578 attr
= "INTENT (IN)";
2581 attr
= "INTENT (OUT)";
2584 attr
= "INTENT (IN OUT)";
2586 case DECL_INTRINSIC
:
2592 case DECL_PARAMETER
:
2598 case DECL_PROTECTED
:
2613 case DECL_IS_BIND_C
:
2623 attr
= NULL
; /* This shouldn't happen. */
2626 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
2631 /* Now that we've dealt with duplicate attributes, add the attributes
2632 to the current attribute. */
2633 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2638 if (gfc_current_state () == COMP_DERIVED
2639 && d
!= DECL_DIMENSION
&& d
!= DECL_POINTER
2640 && d
!= DECL_COLON
&& d
!= DECL_PRIVATE
2641 && d
!= DECL_PUBLIC
&& d
!= DECL_NONE
)
2643 if (d
== DECL_ALLOCATABLE
)
2645 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
2646 "attribute at %C in a TYPE definition")
2655 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2662 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
2663 && gfc_current_state () != COMP_MODULE
)
2665 if (d
== DECL_PRIVATE
)
2669 if (gfc_current_state () == COMP_DERIVED
2670 && gfc_state_stack
->previous
2671 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
2673 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
2674 "at %L in a TYPE definition", attr
,
2684 gfc_error ("%s attribute at %L is not allowed outside of the "
2685 "specification part of a module", attr
, &seen_at
[d
]);
2693 case DECL_ALLOCATABLE
:
2694 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
2697 case DECL_DIMENSION
:
2698 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
2702 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
2706 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
2710 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
2714 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
2717 case DECL_INTRINSIC
:
2718 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
2722 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
2725 case DECL_PARAMETER
:
2726 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
2730 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
2733 case DECL_PROTECTED
:
2734 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
2736 gfc_error ("PROTECTED at %C only allowed in specification "
2737 "part of a module");
2742 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
2747 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
2751 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
2756 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
2761 t
= gfc_add_save (¤t_attr
, NULL
, &seen_at
[d
]);
2765 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
2768 case DECL_IS_BIND_C
:
2769 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
2773 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
2778 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
2782 if (gfc_notify_std (GFC_STD_F2003
,
2783 "Fortran 2003: VOLATILE attribute at %C")
2787 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
2791 gfc_internal_error ("match_attr_spec(): Bad attribute");
2805 gfc_current_locus
= start
;
2806 gfc_free_array_spec (current_as
);
2812 /* Set the binding label, dest_label, either with the binding label
2813 stored in the given gfc_typespec, ts, or if none was provided, it
2814 will be the symbol name in all lower case, as required by the draft
2815 (J3/04-007, section 15.4.1). If a binding label was given and
2816 there is more than one argument (num_idents), it is an error. */
2819 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
2821 if (curr_binding_label
[0] != '\0')
2823 if (num_idents
> 1 || num_idents_on_line
> 1)
2825 gfc_error ("Multiple identifiers provided with "
2826 "single NAME= specifier at %C");
2830 /* Binding label given; store in temp holder til have sym. */
2831 strncpy (dest_label
, curr_binding_label
,
2832 strlen (curr_binding_label
) + 1);
2836 /* No binding label given, and the NAME= specifier did not exist,
2837 which means there was no NAME="". */
2838 if (sym_name
!= NULL
&& has_name_equals
== 0)
2839 strncpy (dest_label
, sym_name
, strlen (sym_name
) + 1);
2846 /* Set the status of the given common block as being BIND(C) or not,
2847 depending on the given parameter, is_bind_c. */
2850 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
2852 com_block
->is_bind_c
= is_bind_c
;
2857 /* Verify that the given gfc_typespec is for a C interoperable type. */
2860 verify_c_interop (gfc_typespec
*ts
, const char *name
, locus
*where
)
2864 /* Make sure the kind used is appropriate for the type.
2865 The f90_type is unknown if an integer constant was
2866 used (e.g., real(4), bind(c) :: myFloat). */
2867 if (ts
->f90_type
!= BT_UNKNOWN
)
2869 t
= gfc_validate_c_kind (ts
);
2872 /* Print an error, but continue parsing line. */
2873 gfc_error_now ("C kind parameter is for type %s but "
2874 "symbol '%s' at %L is of type %s",
2875 gfc_basic_typename (ts
->f90_type
),
2877 gfc_basic_typename (ts
->type
));
2881 /* Make sure the kind is C interoperable. This does not care about the
2882 possible error above. */
2883 if (ts
->type
== BT_DERIVED
&& ts
->derived
!= NULL
)
2884 return (ts
->derived
->ts
.is_c_interop
? SUCCESS
: FAILURE
);
2885 else if (ts
->is_c_interop
!= 1)
2892 /* Verify that the variables of a given common block, which has been
2893 defined with the attribute specifier bind(c), to be of a C
2894 interoperable type. Errors will be reported here, if
2898 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
2900 gfc_symbol
*curr_sym
= NULL
;
2901 try retval
= SUCCESS
;
2903 curr_sym
= com_block
->head
;
2905 /* Make sure we have at least one symbol. */
2906 if (curr_sym
== NULL
)
2909 /* Here we know we have a symbol, so we'll execute this loop
2913 /* The second to last param, 1, says this is in a common block. */
2914 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
2915 curr_sym
= curr_sym
->common_next
;
2916 } while (curr_sym
!= NULL
);
2922 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2923 an appropriate error message is reported. */
2926 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
2927 int is_in_common
, gfc_common_head
*com_block
)
2929 try retval
= SUCCESS
;
2931 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
2933 tmp_sym
= tmp_sym
->result
;
2934 /* Make sure it wasn't an implicitly typed result. */
2935 if (tmp_sym
->attr
.implicit_type
)
2937 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
2938 "%L may not be C interoperable", tmp_sym
->name
,
2939 &tmp_sym
->declared_at
);
2940 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
2941 /* Mark it as C interoperable to prevent duplicate warnings. */
2942 tmp_sym
->ts
.is_c_interop
= 1;
2943 tmp_sym
->attr
.is_c_interop
= 1;
2947 /* Here, we know we have the bind(c) attribute, so if we have
2948 enough type info, then verify that it's a C interop kind.
2949 The info could be in the symbol already, or possibly still in
2950 the given ts (current_ts), so look in both. */
2951 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
2953 if (verify_c_interop (&(tmp_sym
->ts
), tmp_sym
->name
,
2954 &(tmp_sym
->declared_at
)) != SUCCESS
)
2956 /* See if we're dealing with a sym in a common block or not. */
2957 if (is_in_common
== 1)
2959 gfc_warning ("Variable '%s' in common block '%s' at %L "
2960 "may not be a C interoperable "
2961 "kind though common block '%s' is BIND(C)",
2962 tmp_sym
->name
, com_block
->name
,
2963 &(tmp_sym
->declared_at
), com_block
->name
);
2967 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
2968 gfc_error ("Type declaration '%s' at %L is not C "
2969 "interoperable but it is BIND(C)",
2970 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2972 gfc_warning ("Variable '%s' at %L "
2973 "may not be a C interoperable "
2974 "kind but it is bind(c)",
2975 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2979 /* Variables declared w/in a common block can't be bind(c)
2980 since there's no way for C to see these variables, so there's
2981 semantically no reason for the attribute. */
2982 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
2984 gfc_error ("Variable '%s' in common block '%s' at "
2985 "%L cannot be declared with BIND(C) "
2986 "since it is not a global",
2987 tmp_sym
->name
, com_block
->name
,
2988 &(tmp_sym
->declared_at
));
2992 /* Scalar variables that are bind(c) can not have the pointer
2993 or allocatable attributes. */
2994 if (tmp_sym
->attr
.is_bind_c
== 1)
2996 if (tmp_sym
->attr
.pointer
== 1)
2998 gfc_error ("Variable '%s' at %L cannot have both the "
2999 "POINTER and BIND(C) attributes",
3000 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3004 if (tmp_sym
->attr
.allocatable
== 1)
3006 gfc_error ("Variable '%s' at %L cannot have both the "
3007 "ALLOCATABLE and BIND(C) attributes",
3008 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3012 /* If it is a BIND(C) function, make sure the return value is a
3013 scalar value. The previous tests in this function made sure
3014 the type is interoperable. */
3015 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->as
!= NULL
)
3016 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3017 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3019 /* BIND(C) functions can not return a character string. */
3020 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->ts
.type
== BT_CHARACTER
)
3021 if (tmp_sym
->ts
.cl
== NULL
|| tmp_sym
->ts
.cl
->length
== NULL
3022 || tmp_sym
->ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
3023 || mpz_cmp_si (tmp_sym
->ts
.cl
->length
->value
.integer
, 1) != 0)
3024 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3025 "be a character string", tmp_sym
->name
,
3026 &(tmp_sym
->declared_at
));
3030 /* See if the symbol has been marked as private. If it has, make sure
3031 there is no binding label and warn the user if there is one. */
3032 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3033 && tmp_sym
->binding_label
[0] != '\0')
3034 /* Use gfc_warning_now because we won't say that the symbol fails
3035 just because of this. */
3036 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3037 "given the binding label '%s'", tmp_sym
->name
,
3038 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3044 /* Set the appropriate fields for a symbol that's been declared as
3045 BIND(C) (the is_bind_c flag and the binding label), and verify that
3046 the type is C interoperable. Errors are reported by the functions
3047 used to set/test these fields. */
3050 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3052 try retval
= SUCCESS
;
3054 /* TODO: Do we need to make sure the vars aren't marked private? */
3056 /* Set the is_bind_c bit in symbol_attribute. */
3057 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3059 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3060 num_idents
) != SUCCESS
)
3067 /* Set the fields marking the given common block as BIND(C), including
3068 a binding label, and report any errors encountered. */
3071 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3073 try retval
= SUCCESS
;
3075 /* destLabel, common name, typespec (which may have binding label). */
3076 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3080 /* Set the given common block (com_block) to being bind(c) (1). */
3081 set_com_block_bind_c (com_block
, 1);
3087 /* Retrieve the list of one or more identifiers that the given bind(c)
3088 attribute applies to. */
3091 get_bind_c_idents (void)
3093 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3095 gfc_symbol
*tmp_sym
= NULL
;
3097 gfc_common_head
*com_block
= NULL
;
3099 if (gfc_match_name (name
) == MATCH_YES
)
3101 found_id
= MATCH_YES
;
3102 gfc_get_ha_symbol (name
, &tmp_sym
);
3104 else if (match_common_name (name
) == MATCH_YES
)
3106 found_id
= MATCH_YES
;
3107 com_block
= gfc_get_common (name
, 0);
3111 gfc_error ("Need either entity or common block name for "
3112 "attribute specification statement at %C");
3116 /* Save the current identifier and look for more. */
3119 /* Increment the number of identifiers found for this spec stmt. */
3122 /* Make sure we have a sym or com block, and verify that it can
3123 be bind(c). Set the appropriate field(s) and look for more
3125 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3127 if (tmp_sym
!= NULL
)
3129 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3135 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3140 /* Look to see if we have another identifier. */
3142 if (gfc_match_eos () == MATCH_YES
)
3143 found_id
= MATCH_NO
;
3144 else if (gfc_match_char (',') != MATCH_YES
)
3145 found_id
= MATCH_NO
;
3146 else if (gfc_match_name (name
) == MATCH_YES
)
3148 found_id
= MATCH_YES
;
3149 gfc_get_ha_symbol (name
, &tmp_sym
);
3151 else if (match_common_name (name
) == MATCH_YES
)
3153 found_id
= MATCH_YES
;
3154 com_block
= gfc_get_common (name
, 0);
3158 gfc_error ("Missing entity or common block name for "
3159 "attribute specification statement at %C");
3165 gfc_internal_error ("Missing symbol");
3167 } while (found_id
== MATCH_YES
);
3169 /* if we get here we were successful */
3174 /* Try and match a BIND(C) attribute specification statement. */
3177 gfc_match_bind_c_stmt (void)
3179 match found_match
= MATCH_NO
;
3184 /* This may not be necessary. */
3186 /* Clear the temporary binding label holder. */
3187 curr_binding_label
[0] = '\0';
3189 /* Look for the bind(c). */
3190 found_match
= gfc_match_bind_c (NULL
);
3192 if (found_match
== MATCH_YES
)
3194 /* Look for the :: now, but it is not required. */
3197 /* Get the identifier(s) that needs to be updated. This may need to
3198 change to hand the flag(s) for the attr specified so all identifiers
3199 found can have all appropriate parts updated (assuming that the same
3200 spec stmt can have multiple attrs, such as both bind(c) and
3202 if (get_bind_c_idents () != SUCCESS
)
3203 /* Error message should have printed already. */
3211 /* Match a data declaration statement. */
3214 gfc_match_data_decl (void)
3220 num_idents_on_line
= 0;
3222 m
= match_type_spec (¤t_ts
, 0);
3226 if (current_ts
.type
== BT_DERIVED
&& gfc_current_state () != COMP_DERIVED
)
3228 sym
= gfc_use_derived (current_ts
.derived
);
3236 current_ts
.derived
= sym
;
3239 m
= match_attr_spec ();
3240 if (m
== MATCH_ERROR
)
3246 if (current_ts
.type
== BT_DERIVED
&& current_ts
.derived
->components
== NULL
)
3249 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
3252 gfc_find_symbol (current_ts
.derived
->name
,
3253 current_ts
.derived
->ns
->parent
, 1, &sym
);
3255 /* Any symbol that we find had better be a type definition
3256 which has its components defined. */
3257 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
3258 && current_ts
.derived
->components
!= NULL
)
3261 /* Now we have an error, which we signal, and then fix up
3262 because the knock-on is plain and simple confusing. */
3263 gfc_error_now ("Derived type at %C has not been previously defined "
3264 "and so cannot appear in a derived type definition");
3265 current_attr
.pointer
= 1;
3270 /* If we have an old-style character declaration, and no new-style
3271 attribute specifications, then there a comma is optional between
3272 the type specification and the variable list. */
3273 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
3274 gfc_match_char (',');
3276 /* Give the types/attributes to symbols that follow. Give the element
3277 a number so that repeat character length expressions can be copied. */
3281 num_idents_on_line
++;
3282 m
= variable_decl (elem
++);
3283 if (m
== MATCH_ERROR
)
3288 if (gfc_match_eos () == MATCH_YES
)
3290 if (gfc_match_char (',') != MATCH_YES
)
3294 if (gfc_error_flag_test () == 0)
3295 gfc_error ("Syntax error in data declaration at %C");
3298 gfc_free_data_all (gfc_current_ns
);
3301 gfc_free_array_spec (current_as
);
3307 /* Match a prefix associated with a function or subroutine
3308 declaration. If the typespec pointer is nonnull, then a typespec
3309 can be matched. Note that if nothing matches, MATCH_YES is
3310 returned (the null string was matched). */
3313 match_prefix (gfc_typespec
*ts
)
3317 gfc_clear_attr (¤t_attr
);
3321 if (!seen_type
&& ts
!= NULL
3322 && match_type_spec (ts
, 0) == MATCH_YES
3323 && gfc_match_space () == MATCH_YES
)
3330 if (gfc_match ("elemental% ") == MATCH_YES
)
3332 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
3338 if (gfc_match ("pure% ") == MATCH_YES
)
3340 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
3346 if (gfc_match ("recursive% ") == MATCH_YES
)
3348 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
3354 /* At this point, the next item is not a prefix. */
3359 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3362 copy_prefix (symbol_attribute
*dest
, locus
*where
)
3364 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
3367 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
3370 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
3377 /* Match a formal argument list. */
3380 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
3382 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
3383 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3389 if (gfc_match_char ('(') != MATCH_YES
)
3396 if (gfc_match_char (')') == MATCH_YES
)
3401 if (gfc_match_char ('*') == MATCH_YES
)
3405 m
= gfc_match_name (name
);
3409 if (gfc_get_symbol (name
, NULL
, &sym
))
3413 p
= gfc_get_formal_arglist ();
3425 /* We don't add the VARIABLE flavor because the name could be a
3426 dummy procedure. We don't apply these attributes to formal
3427 arguments of statement functions. */
3428 if (sym
!= NULL
&& !st_flag
3429 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
3430 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
3436 /* The name of a program unit can be in a different namespace,
3437 so check for it explicitly. After the statement is accepted,
3438 the name is checked for especially in gfc_get_symbol(). */
3439 if (gfc_new_block
!= NULL
&& sym
!= NULL
3440 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
3442 gfc_error ("Name '%s' at %C is the name of the procedure",
3448 if (gfc_match_char (')') == MATCH_YES
)
3451 m
= gfc_match_char (',');
3454 gfc_error ("Unexpected junk in formal argument list at %C");
3460 /* Check for duplicate symbols in the formal argument list. */
3463 for (p
= head
; p
->next
; p
= p
->next
)
3468 for (q
= p
->next
; q
; q
= q
->next
)
3469 if (p
->sym
== q
->sym
)
3471 gfc_error ("Duplicate symbol '%s' in formal argument list "
3472 "at %C", p
->sym
->name
);
3480 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
3490 gfc_free_formal_arglist (head
);
3495 /* Match a RESULT specification following a function declaration or
3496 ENTRY statement. Also matches the end-of-statement. */
3499 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
3501 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3505 if (gfc_match (" result (") != MATCH_YES
)
3508 m
= gfc_match_name (name
);
3512 /* Get the right paren, and that's it because there could be the
3513 bind(c) attribute after the result clause. */
3514 if (gfc_match_char(')') != MATCH_YES
)
3516 /* TODO: should report the missing right paren here. */
3520 if (strcmp (function
->name
, name
) == 0)
3522 gfc_error ("RESULT variable at %C must be different than function name");
3526 if (gfc_get_symbol (name
, NULL
, &r
))
3529 if (gfc_add_flavor (&r
->attr
, FL_VARIABLE
, r
->name
, NULL
) == FAILURE
3530 || gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
3539 /* Match a function suffix, which could be a combination of a result
3540 clause and BIND(C), either one, or neither. The draft does not
3541 require them to come in a specific order. */
3544 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
3546 match is_bind_c
; /* Found bind(c). */
3547 match is_result
; /* Found result clause. */
3548 match found_match
; /* Status of whether we've found a good match. */
3549 int peek_char
; /* Character we're going to peek at. */
3551 /* Initialize to having found nothing. */
3552 found_match
= MATCH_NO
;
3553 is_bind_c
= MATCH_NO
;
3554 is_result
= MATCH_NO
;
3556 /* Get the next char to narrow between result and bind(c). */
3557 gfc_gobble_whitespace ();
3558 peek_char
= gfc_peek_char ();
3563 /* Look for result clause. */
3564 is_result
= match_result (sym
, result
);
3565 if (is_result
== MATCH_YES
)
3567 /* Now see if there is a bind(c) after it. */
3568 is_bind_c
= gfc_match_bind_c (sym
);
3569 /* We've found the result clause and possibly bind(c). */
3570 found_match
= MATCH_YES
;
3573 /* This should only be MATCH_ERROR. */
3574 found_match
= is_result
;
3577 /* Look for bind(c) first. */
3578 is_bind_c
= gfc_match_bind_c (sym
);
3579 if (is_bind_c
== MATCH_YES
)
3581 /* Now see if a result clause followed it. */
3582 is_result
= match_result (sym
, result
);
3583 found_match
= MATCH_YES
;
3587 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3588 found_match
= MATCH_ERROR
;
3592 gfc_error ("Unexpected junk after function declaration at %C");
3593 found_match
= MATCH_ERROR
;
3597 if (is_bind_c
== MATCH_YES
)
3598 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
3606 /* Match a function declaration. */
3609 gfc_match_function_decl (void)
3611 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3612 gfc_symbol
*sym
, *result
;
3616 match found_match
; /* Status returned by match func. */
3618 if (gfc_current_state () != COMP_NONE
3619 && gfc_current_state () != COMP_INTERFACE
3620 && gfc_current_state () != COMP_CONTAINS
)
3623 gfc_clear_ts (¤t_ts
);
3625 old_loc
= gfc_current_locus
;
3627 m
= match_prefix (¤t_ts
);
3630 gfc_current_locus
= old_loc
;
3634 if (gfc_match ("function% %n", name
) != MATCH_YES
)
3636 gfc_current_locus
= old_loc
;
3639 if (get_proc_name (name
, &sym
, false))
3641 gfc_new_block
= sym
;
3643 m
= gfc_match_formal_arglist (sym
, 0, 0);
3646 gfc_error ("Expected formal argument list in function "
3647 "definition at %C");
3651 else if (m
== MATCH_ERROR
)
3656 /* According to the draft, the bind(c) and result clause can
3657 come in either order after the formal_arg_list (i.e., either
3658 can be first, both can exist together or by themselves or neither
3659 one). Therefore, the match_result can't match the end of the
3660 string, and check for the bind(c) or result clause in either order. */
3661 found_match
= gfc_match_eos ();
3663 /* Make sure that it isn't already declared as BIND(C). If it is, it
3664 must have been marked BIND(C) with a BIND(C) attribute and that is
3665 not allowed for procedures. */
3666 if (sym
->attr
.is_bind_c
== 1)
3668 sym
->attr
.is_bind_c
= 0;
3669 if (sym
->old_symbol
!= NULL
)
3670 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3671 "variables or common blocks",
3672 &(sym
->old_symbol
->declared_at
));
3674 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3675 "variables or common blocks", &gfc_current_locus
);
3678 if (found_match
!= MATCH_YES
)
3680 /* If we haven't found the end-of-statement, look for a suffix. */
3681 suffix_match
= gfc_match_suffix (sym
, &result
);
3682 if (suffix_match
== MATCH_YES
)
3683 /* Need to get the eos now. */
3684 found_match
= gfc_match_eos ();
3686 found_match
= suffix_match
;
3689 if(found_match
!= MATCH_YES
)
3693 /* Make changes to the symbol. */
3696 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3699 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
3700 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
3703 if (current_ts
.type
!= BT_UNKNOWN
&& sym
->ts
.type
!= BT_UNKNOWN
3704 && !sym
->attr
.implicit_type
)
3706 gfc_error ("Function '%s' at %C already has a type of %s", name
,
3707 gfc_basic_typename (sym
->ts
.type
));
3713 sym
->ts
= current_ts
;
3718 result
->ts
= current_ts
;
3719 sym
->result
= result
;
3726 gfc_current_locus
= old_loc
;
3731 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3732 pass the name of the entry, rather than the gfc_current_block name, and
3733 to return false upon finding an existing global entry. */
3736 add_global_entry (const char *name
, int sub
)
3740 s
= gfc_get_gsymbol(name
);
3743 || (s
->type
!= GSYM_UNKNOWN
3744 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3745 global_used(s
, NULL
);
3748 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3749 s
->where
= gfc_current_locus
;
3757 /* Match an ENTRY statement. */
3760 gfc_match_entry (void)
3765 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3766 gfc_compile_state state
;
3770 bool module_procedure
;
3772 m
= gfc_match_name (name
);
3776 state
= gfc_current_state ();
3777 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
3782 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3785 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3787 case COMP_BLOCK_DATA
:
3788 gfc_error ("ENTRY statement at %C cannot appear within "
3791 case COMP_INTERFACE
:
3792 gfc_error ("ENTRY statement at %C cannot appear within "
3796 gfc_error ("ENTRY statement at %C cannot appear within "
3797 "a DERIVED TYPE block");
3800 gfc_error ("ENTRY statement at %C cannot appear within "
3801 "an IF-THEN block");
3804 gfc_error ("ENTRY statement at %C cannot appear within "
3808 gfc_error ("ENTRY statement at %C cannot appear within "
3812 gfc_error ("ENTRY statement at %C cannot appear within "
3816 gfc_error ("ENTRY statement at %C cannot appear within "
3820 gfc_error ("ENTRY statement at %C cannot appear within "
3821 "a contained subprogram");
3824 gfc_internal_error ("gfc_match_entry(): Bad state");
3829 module_procedure
= gfc_current_ns
->parent
!= NULL
3830 && gfc_current_ns
->parent
->proc_name
3831 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
3834 if (gfc_current_ns
->parent
!= NULL
3835 && gfc_current_ns
->parent
->proc_name
3836 && !module_procedure
)
3838 gfc_error("ENTRY statement at %C cannot appear in a "
3839 "contained procedure");
3843 /* Module function entries need special care in get_proc_name
3844 because previous references within the function will have
3845 created symbols attached to the current namespace. */
3846 if (get_proc_name (name
, &entry
,
3847 gfc_current_ns
->parent
!= NULL
3849 && gfc_current_ns
->proc_name
->attr
.function
))
3852 proc
= gfc_current_block ();
3854 if (state
== COMP_SUBROUTINE
)
3856 /* An entry in a subroutine. */
3857 if (!add_global_entry (name
, 1))
3860 m
= gfc_match_formal_arglist (entry
, 0, 1);
3864 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
3865 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
3870 /* An entry in a function.
3871 We need to take special care because writing
3876 ENTRY f() RESULT (r)
3878 ENTRY f RESULT (r). */
3879 if (!add_global_entry (name
, 0))
3882 old_loc
= gfc_current_locus
;
3883 if (gfc_match_eos () == MATCH_YES
)
3885 gfc_current_locus
= old_loc
;
3886 /* Match the empty argument list, and add the interface to
3888 m
= gfc_match_formal_arglist (entry
, 0, 1);
3891 m
= gfc_match_formal_arglist (entry
, 0, 0);
3898 if (gfc_match_eos () == MATCH_YES
)
3900 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
3901 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
3904 entry
->result
= entry
;
3908 m
= match_result (proc
, &result
);
3910 gfc_syntax_error (ST_ENTRY
);
3914 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
3915 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
3916 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
3920 entry
->result
= result
;
3924 if (gfc_match_eos () != MATCH_YES
)
3926 gfc_syntax_error (ST_ENTRY
);
3930 entry
->attr
.recursive
= proc
->attr
.recursive
;
3931 entry
->attr
.elemental
= proc
->attr
.elemental
;
3932 entry
->attr
.pure
= proc
->attr
.pure
;
3934 el
= gfc_get_entry_list ();
3936 el
->next
= gfc_current_ns
->entries
;
3937 gfc_current_ns
->entries
= el
;
3939 el
->id
= el
->next
->id
+ 1;
3943 new_st
.op
= EXEC_ENTRY
;
3944 new_st
.ext
.entry
= el
;
3950 /* Match a subroutine statement, including optional prefixes. */
3953 gfc_match_subroutine (void)
3955 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3961 if (gfc_current_state () != COMP_NONE
3962 && gfc_current_state () != COMP_INTERFACE
3963 && gfc_current_state () != COMP_CONTAINS
)
3966 m
= match_prefix (NULL
);
3970 m
= gfc_match ("subroutine% %n", name
);
3974 if (get_proc_name (name
, &sym
, false))
3976 gfc_new_block
= sym
;
3978 /* Check what next non-whitespace character is so we can tell if there
3979 where the required parens if we have a BIND(C). */
3980 gfc_gobble_whitespace ();
3981 peek_char
= gfc_peek_char ();
3983 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3986 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
3989 /* Make sure that it isn't already declared as BIND(C). If it is, it
3990 must have been marked BIND(C) with a BIND(C) attribute and that is
3991 not allowed for procedures. */
3992 if (sym
->attr
.is_bind_c
== 1)
3994 sym
->attr
.is_bind_c
= 0;
3995 if (sym
->old_symbol
!= NULL
)
3996 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3997 "variables or common blocks",
3998 &(sym
->old_symbol
->declared_at
));
4000 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4001 "variables or common blocks", &gfc_current_locus
);
4004 /* Here, we are just checking if it has the bind(c) attribute, and if
4005 so, then we need to make sure it's all correct. If it doesn't,
4006 we still need to continue matching the rest of the subroutine line. */
4007 is_bind_c
= gfc_match_bind_c (sym
);
4008 if (is_bind_c
== MATCH_ERROR
)
4010 /* There was an attempt at the bind(c), but it was wrong. An
4011 error message should have been printed w/in the gfc_match_bind_c
4012 so here we'll just return the MATCH_ERROR. */
4016 if (is_bind_c
== MATCH_YES
)
4018 if (peek_char
!= '(')
4020 gfc_error ("Missing required parentheses before BIND(C) at %C");
4023 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
4028 if (gfc_match_eos () != MATCH_YES
)
4030 gfc_syntax_error (ST_SUBROUTINE
);
4034 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4041 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4042 given, and set the binding label in either the given symbol (if not
4043 NULL), or in the current_ts. The symbol may be NULL because we may
4044 encounter the BIND(C) before the declaration itself. Return
4045 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4046 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4047 or MATCH_YES if the specifier was correct and the binding label and
4048 bind(c) fields were set correctly for the given symbol or the
4052 gfc_match_bind_c (gfc_symbol
*sym
)
4054 /* binding label, if exists */
4055 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
4058 int has_name_equals
= 0;
4060 /* Initialize the flag that specifies whether we encountered a NAME=
4061 specifier or not. */
4062 has_name_equals
= 0;
4064 /* Init the first char to nil so we can catch if we don't have
4065 the label (name attr) or the symbol name yet. */
4066 binding_label
[0] = '\0';
4068 /* This much we have to be able to match, in this order, if
4069 there is a bind(c) label. */
4070 if (gfc_match (" bind ( c ") != MATCH_YES
)
4073 /* Now see if there is a binding label, or if we've reached the
4074 end of the bind(c) attribute without one. */
4075 if (gfc_match_char (',') == MATCH_YES
)
4077 if (gfc_match (" name = ") != MATCH_YES
)
4079 gfc_error ("Syntax error in NAME= specifier for binding label "
4081 /* should give an error message here */
4085 has_name_equals
= 1;
4087 /* Get the opening quote. */
4088 double_quote
= MATCH_YES
;
4089 single_quote
= MATCH_YES
;
4090 double_quote
= gfc_match_char ('"');
4091 if (double_quote
!= MATCH_YES
)
4092 single_quote
= gfc_match_char ('\'');
4093 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
4095 gfc_error ("Syntax error in NAME= specifier for binding label "
4100 /* Grab the binding label, using functions that will not lower
4101 case the names automatically. */
4102 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
4105 /* Get the closing quotation. */
4106 if (double_quote
== MATCH_YES
)
4108 if (gfc_match_char ('"') != MATCH_YES
)
4110 gfc_error ("Missing closing quote '\"' for binding label at %C");
4111 /* User started string with '"' so looked to match it. */
4117 if (gfc_match_char ('\'') != MATCH_YES
)
4119 gfc_error ("Missing closing quote '\'' for binding label at %C");
4120 /* User started string with "'" char. */
4126 /* Get the required right paren. */
4127 if (gfc_match_char (')') != MATCH_YES
)
4129 gfc_error ("Missing closing paren for binding label at %C");
4133 /* Save the binding label to the symbol. If sym is null, we're
4134 probably matching the typespec attributes of a declaration and
4135 haven't gotten the name yet, and therefore, no symbol yet. */
4136 if (binding_label
[0] != '\0')
4140 strncpy (sym
->binding_label
, binding_label
,
4141 strlen (binding_label
)+1);
4144 strncpy (curr_binding_label
, binding_label
,
4145 strlen (binding_label
) + 1);
4149 /* No binding label, but if symbol isn't null, we
4150 can set the label for it here. */
4151 /* TODO: If the name= was given and no binding label (name=""), we simply
4152 will let fortran mangle the symbol name as it usually would.
4153 However, this could still let C call it if the user looked up the
4154 symbol in the object file. Should the name set during mangling in
4155 trans-decl.c be marked with characters that are invalid for C to
4157 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
4158 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
4165 /* Return nonzero if we're currently compiling a contained procedure. */
4168 contained_procedure (void)
4172 for (s
=gfc_state_stack
; s
; s
=s
->previous
)
4173 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
4174 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
4180 /* Set the kind of each enumerator. The kind is selected such that it is
4181 interoperable with the corresponding C enumeration type, making
4182 sure that -fshort-enums is honored. */
4187 enumerator_history
*current_history
= NULL
;
4191 if (max_enum
== NULL
|| enum_history
== NULL
)
4194 if (!gfc_option
.fshort_enums
)
4200 kind
= gfc_integer_kinds
[i
++].kind
;
4202 while (kind
< gfc_c_int_kind
4203 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
4206 current_history
= enum_history
;
4207 while (current_history
!= NULL
)
4209 current_history
->sym
->ts
.kind
= kind
;
4210 current_history
= current_history
->next
;
4215 /* Match any of the various end-block statements. Returns the type of
4216 END to the caller. The END INTERFACE, END IF, END DO and END
4217 SELECT statements cannot be replaced by a single END statement. */
4220 gfc_match_end (gfc_statement
*st
)
4222 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4223 gfc_compile_state state
;
4225 const char *block_name
;
4230 old_loc
= gfc_current_locus
;
4231 if (gfc_match ("end") != MATCH_YES
)
4234 state
= gfc_current_state ();
4235 block_name
= gfc_current_block () == NULL
4236 ? NULL
: gfc_current_block ()->name
;
4238 if (state
== COMP_CONTAINS
)
4240 state
= gfc_state_stack
->previous
->state
;
4241 block_name
= gfc_state_stack
->previous
->sym
== NULL
4242 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
4249 *st
= ST_END_PROGRAM
;
4250 target
= " program";
4254 case COMP_SUBROUTINE
:
4255 *st
= ST_END_SUBROUTINE
;
4256 target
= " subroutine";
4257 eos_ok
= !contained_procedure ();
4261 *st
= ST_END_FUNCTION
;
4262 target
= " function";
4263 eos_ok
= !contained_procedure ();
4266 case COMP_BLOCK_DATA
:
4267 *st
= ST_END_BLOCK_DATA
;
4268 target
= " block data";
4273 *st
= ST_END_MODULE
;
4278 case COMP_INTERFACE
:
4279 *st
= ST_END_INTERFACE
;
4280 target
= " interface";
4303 *st
= ST_END_SELECT
;
4309 *st
= ST_END_FORALL
;
4324 last_initializer
= NULL
;
4326 gfc_free_enum_history ();
4330 gfc_error ("Unexpected END statement at %C");
4334 if (gfc_match_eos () == MATCH_YES
)
4338 /* We would have required END [something]. */
4339 gfc_error ("%s statement expected at %L",
4340 gfc_ascii_statement (*st
), &old_loc
);
4347 /* Verify that we've got the sort of end-block that we're expecting. */
4348 if (gfc_match (target
) != MATCH_YES
)
4350 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
4354 /* If we're at the end, make sure a block name wasn't required. */
4355 if (gfc_match_eos () == MATCH_YES
)
4358 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
4359 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
)
4362 if (gfc_current_block () == NULL
)
4365 gfc_error ("Expected block name of '%s' in %s statement at %C",
4366 block_name
, gfc_ascii_statement (*st
));
4371 /* END INTERFACE has a special handler for its several possible endings. */
4372 if (*st
== ST_END_INTERFACE
)
4373 return gfc_match_end_interface ();
4375 /* We haven't hit the end of statement, so what is left must be an
4377 m
= gfc_match_space ();
4379 m
= gfc_match_name (name
);
4382 gfc_error ("Expected terminating name at %C");
4386 if (block_name
== NULL
)
4389 if (strcmp (name
, block_name
) != 0)
4391 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
4392 gfc_ascii_statement (*st
));
4396 if (gfc_match_eos () == MATCH_YES
)
4400 gfc_syntax_error (*st
);
4403 gfc_current_locus
= old_loc
;
4409 /***************** Attribute declaration statements ****************/
4411 /* Set the attribute of a single variable. */
4416 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4424 m
= gfc_match_name (name
);
4428 if (find_special (name
, &sym
))
4431 var_locus
= gfc_current_locus
;
4433 /* Deal with possible array specification for certain attributes. */
4434 if (current_attr
.dimension
4435 || current_attr
.allocatable
4436 || current_attr
.pointer
4437 || current_attr
.target
)
4439 m
= gfc_match_array_spec (&as
);
4440 if (m
== MATCH_ERROR
)
4443 if (current_attr
.dimension
&& m
== MATCH_NO
)
4445 gfc_error ("Missing array specification at %L in DIMENSION "
4446 "statement", &var_locus
);
4451 if ((current_attr
.allocatable
|| current_attr
.pointer
)
4452 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
4454 gfc_error ("Array specification must be deferred at %L", &var_locus
);
4460 /* Update symbol table. DIMENSION attribute is set
4461 in gfc_set_array_spec(). */
4462 if (current_attr
.dimension
== 0
4463 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4469 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
4475 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
4477 /* Fix the array spec. */
4478 m
= gfc_mod_pointee_as (sym
->as
);
4479 if (m
== MATCH_ERROR
)
4483 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
4489 if ((current_attr
.external
|| current_attr
.intrinsic
)
4490 && sym
->attr
.flavor
!= FL_PROCEDURE
4491 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
4500 gfc_free_array_spec (as
);
4505 /* Generic attribute declaration subroutine. Used for attributes that
4506 just have a list of names. */
4513 /* Gobble the optional double colon, by simply ignoring the result
4523 if (gfc_match_eos () == MATCH_YES
)
4529 if (gfc_match_char (',') != MATCH_YES
)
4531 gfc_error ("Unexpected character in variable list at %C");
4541 /* This routine matches Cray Pointer declarations of the form:
4542 pointer ( <pointer>, <pointee> )
4544 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4545 The pointer, if already declared, should be an integer. Otherwise, we
4546 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4547 be either a scalar, or an array declaration. No space is allocated for
4548 the pointee. For the statement
4549 pointer (ipt, ar(10))
4550 any subsequent uses of ar will be translated (in C-notation) as
4551 ar(i) => ((<type> *) ipt)(i)
4552 After gimplification, pointee variable will disappear in the code. */
4555 cray_pointer_decl (void)
4559 gfc_symbol
*cptr
; /* Pointer symbol. */
4560 gfc_symbol
*cpte
; /* Pointee symbol. */
4566 if (gfc_match_char ('(') != MATCH_YES
)
4568 gfc_error ("Expected '(' at %C");
4572 /* Match pointer. */
4573 var_locus
= gfc_current_locus
;
4574 gfc_clear_attr (¤t_attr
);
4575 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
4576 current_ts
.type
= BT_INTEGER
;
4577 current_ts
.kind
= gfc_index_integer_kind
;
4579 m
= gfc_match_symbol (&cptr
, 0);
4582 gfc_error ("Expected variable name at %C");
4586 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
4589 gfc_set_sym_referenced (cptr
);
4591 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
4593 cptr
->ts
.type
= BT_INTEGER
;
4594 cptr
->ts
.kind
= gfc_index_integer_kind
;
4596 else if (cptr
->ts
.type
!= BT_INTEGER
)
4598 gfc_error ("Cray pointer at %C must be an integer");
4601 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
4602 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
4603 " memory addresses require %d bytes",
4604 cptr
->ts
.kind
, gfc_index_integer_kind
);
4606 if (gfc_match_char (',') != MATCH_YES
)
4608 gfc_error ("Expected \",\" at %C");
4612 /* Match Pointee. */
4613 var_locus
= gfc_current_locus
;
4614 gfc_clear_attr (¤t_attr
);
4615 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
4616 current_ts
.type
= BT_UNKNOWN
;
4617 current_ts
.kind
= 0;
4619 m
= gfc_match_symbol (&cpte
, 0);
4622 gfc_error ("Expected variable name at %C");
4626 /* Check for an optional array spec. */
4627 m
= gfc_match_array_spec (&as
);
4628 if (m
== MATCH_ERROR
)
4630 gfc_free_array_spec (as
);
4633 else if (m
== MATCH_NO
)
4635 gfc_free_array_spec (as
);
4639 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
4642 gfc_set_sym_referenced (cpte
);
4644 if (cpte
->as
== NULL
)
4646 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
4647 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4649 else if (as
!= NULL
)
4651 gfc_error ("Duplicate array spec for Cray pointee at %C");
4652 gfc_free_array_spec (as
);
4658 if (cpte
->as
!= NULL
)
4660 /* Fix array spec. */
4661 m
= gfc_mod_pointee_as (cpte
->as
);
4662 if (m
== MATCH_ERROR
)
4666 /* Point the Pointee at the Pointer. */
4667 cpte
->cp_pointer
= cptr
;
4669 if (gfc_match_char (')') != MATCH_YES
)
4671 gfc_error ("Expected \")\" at %C");
4674 m
= gfc_match_char (',');
4676 done
= true; /* Stop searching for more declarations. */
4680 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
4681 || gfc_match_eos () != MATCH_YES
)
4683 gfc_error ("Expected \",\" or end of statement at %C");
4691 gfc_match_external (void)
4694 gfc_clear_attr (¤t_attr
);
4695 current_attr
.external
= 1;
4697 return attr_decl ();
4702 gfc_match_intent (void)
4706 intent
= match_intent_spec ();
4707 if (intent
== INTENT_UNKNOWN
)
4710 gfc_clear_attr (¤t_attr
);
4711 current_attr
.intent
= intent
;
4713 return attr_decl ();
4718 gfc_match_intrinsic (void)
4721 gfc_clear_attr (¤t_attr
);
4722 current_attr
.intrinsic
= 1;
4724 return attr_decl ();
4729 gfc_match_optional (void)
4732 gfc_clear_attr (¤t_attr
);
4733 current_attr
.optional
= 1;
4735 return attr_decl ();
4740 gfc_match_pointer (void)
4742 gfc_gobble_whitespace ();
4743 if (gfc_peek_char () == '(')
4745 if (!gfc_option
.flag_cray_pointer
)
4747 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4751 return cray_pointer_decl ();
4755 gfc_clear_attr (¤t_attr
);
4756 current_attr
.pointer
= 1;
4758 return attr_decl ();
4764 gfc_match_allocatable (void)
4766 gfc_clear_attr (¤t_attr
);
4767 current_attr
.allocatable
= 1;
4769 return attr_decl ();
4774 gfc_match_dimension (void)
4776 gfc_clear_attr (¤t_attr
);
4777 current_attr
.dimension
= 1;
4779 return attr_decl ();
4784 gfc_match_target (void)
4786 gfc_clear_attr (¤t_attr
);
4787 current_attr
.target
= 1;
4789 return attr_decl ();
4793 /* Match the list of entities being specified in a PUBLIC or PRIVATE
4797 access_attr_decl (gfc_statement st
)
4799 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4800 interface_type type
;
4803 gfc_intrinsic_op
operator;
4806 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
4811 m
= gfc_match_generic_spec (&type
, name
, &operator);
4814 if (m
== MATCH_ERROR
)
4819 case INTERFACE_NAMELESS
:
4822 case INTERFACE_GENERIC
:
4823 if (gfc_get_symbol (name
, NULL
, &sym
))
4826 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
4827 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
4828 sym
->name
, NULL
) == FAILURE
)
4833 case INTERFACE_INTRINSIC_OP
:
4834 if (gfc_current_ns
->operator_access
[operator] == ACCESS_UNKNOWN
)
4836 gfc_current_ns
->operator_access
[operator] =
4837 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
4841 gfc_error ("Access specification of the %s operator at %C has "
4842 "already been specified", gfc_op2string (operator));
4848 case INTERFACE_USER_OP
:
4849 uop
= gfc_get_uop (name
);
4851 if (uop
->access
== ACCESS_UNKNOWN
)
4853 uop
->access
= (st
== ST_PUBLIC
)
4854 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
4858 gfc_error ("Access specification of the .%s. operator at %C "
4859 "has already been specified", sym
->name
);
4866 if (gfc_match_char (',') == MATCH_NO
)
4870 if (gfc_match_eos () != MATCH_YES
)
4875 gfc_syntax_error (st
);
4883 gfc_match_protected (void)
4888 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4890 gfc_error ("PROTECTED at %C only allowed in specification "
4891 "part of a module");
4896 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
4900 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
4905 if (gfc_match_eos () == MATCH_YES
)
4910 m
= gfc_match_symbol (&sym
, 0);
4914 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
4927 if (gfc_match_eos () == MATCH_YES
)
4929 if (gfc_match_char (',') != MATCH_YES
)
4936 gfc_error ("Syntax error in PROTECTED statement at %C");
4941 /* The PRIVATE statement is a bit weird in that it can be an attribute
4942 declaration, but also works as a standlone statement inside of a
4943 type declaration or a module. */
4946 gfc_match_private (gfc_statement
*st
)
4949 if (gfc_match ("private") != MATCH_YES
)
4952 if (gfc_current_state () != COMP_MODULE
4953 && (gfc_current_state () != COMP_DERIVED
4954 || !gfc_state_stack
->previous
4955 || gfc_state_stack
->previous
->state
!= COMP_MODULE
))
4957 gfc_error ("PRIVATE statement at %C is only allowed in the "
4958 "specification part of a module");
4962 if (gfc_current_state () == COMP_DERIVED
)
4964 if (gfc_match_eos () == MATCH_YES
)
4970 gfc_syntax_error (ST_PRIVATE
);
4974 if (gfc_match_eos () == MATCH_YES
)
4981 return access_attr_decl (ST_PRIVATE
);
4986 gfc_match_public (gfc_statement
*st
)
4989 if (gfc_match ("public") != MATCH_YES
)
4992 if (gfc_current_state () != COMP_MODULE
)
4994 gfc_error ("PUBLIC statement at %C is only allowed in the "
4995 "specification part of a module");
4999 if (gfc_match_eos () == MATCH_YES
)
5006 return access_attr_decl (ST_PUBLIC
);
5010 /* Workhorse for gfc_match_parameter. */
5019 m
= gfc_match_symbol (&sym
, 0);
5021 gfc_error ("Expected variable name at %C in PARAMETER statement");
5026 if (gfc_match_char ('=') == MATCH_NO
)
5028 gfc_error ("Expected = sign in PARAMETER statement at %C");
5032 m
= gfc_match_init_expr (&init
);
5034 gfc_error ("Expected expression at %C in PARAMETER statement");
5038 if (sym
->ts
.type
== BT_UNKNOWN
5039 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
5045 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
5046 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
5052 if (sym
->ts
.type
== BT_CHARACTER
5053 && sym
->ts
.cl
!= NULL
5054 && sym
->ts
.cl
->length
!= NULL
5055 && sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
5056 && init
->expr_type
== EXPR_CONSTANT
5057 && init
->ts
.type
== BT_CHARACTER
5058 && init
->ts
.kind
== 1)
5059 gfc_set_constant_character_len (
5060 mpz_get_si (sym
->ts
.cl
->length
->value
.integer
), init
, false);
5066 gfc_free_expr (init
);
5071 /* Match a parameter statement, with the weird syntax that these have. */
5074 gfc_match_parameter (void)
5078 if (gfc_match_char ('(') == MATCH_NO
)
5087 if (gfc_match (" )%t") == MATCH_YES
)
5090 if (gfc_match_char (',') != MATCH_YES
)
5092 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5102 /* Save statements have a special syntax. */
5105 gfc_match_save (void)
5107 char n
[GFC_MAX_SYMBOL_LEN
+1];
5112 if (gfc_match_eos () == MATCH_YES
)
5114 if (gfc_current_ns
->seen_save
)
5116 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
5117 "follows previous SAVE statement")
5122 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
5126 if (gfc_current_ns
->save_all
)
5128 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
5129 "blanket SAVE statement")
5138 m
= gfc_match_symbol (&sym
, 0);
5142 if (gfc_add_save (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5154 m
= gfc_match (" / %n /", &n
);
5155 if (m
== MATCH_ERROR
)
5160 c
= gfc_get_common (n
, 0);
5163 gfc_current_ns
->seen_save
= 1;
5166 if (gfc_match_eos () == MATCH_YES
)
5168 if (gfc_match_char (',') != MATCH_YES
)
5175 gfc_error ("Syntax error in SAVE statement at %C");
5181 gfc_match_value (void)
5186 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
5190 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5195 if (gfc_match_eos () == MATCH_YES
)
5200 m
= gfc_match_symbol (&sym
, 0);
5204 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5217 if (gfc_match_eos () == MATCH_YES
)
5219 if (gfc_match_char (',') != MATCH_YES
)
5226 gfc_error ("Syntax error in VALUE statement at %C");
5232 gfc_match_volatile (void)
5237 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
5241 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5246 if (gfc_match_eos () == MATCH_YES
)
5251 /* VOLATILE is special because it can be added to host-associated
5253 m
= gfc_match_symbol (&sym
, 1);
5257 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5270 if (gfc_match_eos () == MATCH_YES
)
5272 if (gfc_match_char (',') != MATCH_YES
)
5279 gfc_error ("Syntax error in VOLATILE statement at %C");
5284 /* Match a module procedure statement. Note that we have to modify
5285 symbols in the parent's namespace because the current one was there
5286 to receive symbols that are in an interface's formal argument list. */
5289 gfc_match_modproc (void)
5291 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5294 gfc_namespace
*module_ns
;
5296 if (gfc_state_stack
->state
!= COMP_INTERFACE
5297 || gfc_state_stack
->previous
== NULL
5298 || current_interface
.type
== INTERFACE_NAMELESS
)
5300 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5305 module_ns
= gfc_current_ns
->parent
;
5306 for (; module_ns
; module_ns
= module_ns
->parent
)
5307 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5310 if (module_ns
== NULL
)
5315 m
= gfc_match_name (name
);
5321 if (gfc_get_symbol (name
, module_ns
, &sym
))
5324 if (sym
->attr
.proc
!= PROC_MODULE
5325 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
5326 sym
->name
, NULL
) == FAILURE
)
5329 if (gfc_add_interface (sym
) == FAILURE
)
5332 sym
->attr
.mod_proc
= 1;
5334 if (gfc_match_eos () == MATCH_YES
)
5336 if (gfc_match_char (',') != MATCH_YES
)
5343 gfc_syntax_error (ST_MODULE_PROC
);
5348 /* Match the optional attribute specifiers for a type declaration.
5349 Return MATCH_ERROR if an error is encountered in one of the handled
5350 attributes (public, private, bind(c)), MATCH_NO if what's found is
5351 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5352 checking on attribute conflicts needs to be done. */
5355 gfc_get_type_attr_spec (symbol_attribute
*attr
)
5357 /* See if the derived type is marked as private. */
5358 if (gfc_match (" , private") == MATCH_YES
)
5360 if (gfc_current_state () != COMP_MODULE
)
5362 gfc_error ("Derived type at %C can only be PRIVATE in the "
5363 "specification part of a module");
5367 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
5370 else if (gfc_match (" , public") == MATCH_YES
)
5372 if (gfc_current_state () != COMP_MODULE
)
5374 gfc_error ("Derived type at %C can only be PUBLIC in the "
5375 "specification part of a module");
5379 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
5382 else if(gfc_match(" , bind ( c )") == MATCH_YES
)
5384 /* If the type is defined to be bind(c) it then needs to make
5385 sure that all fields are interoperable. This will
5386 need to be a semantic check on the finished derived type.
5387 See 15.2.3 (lines 9-12) of F2003 draft. */
5388 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
5391 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5396 /* If we get here, something matched. */
5401 /* Match the beginning of a derived type declaration. If a type name
5402 was the result of a function, then it is possible to have a symbol
5403 already to be known as a derived type yet have no components. */
5406 gfc_match_derived_decl (void)
5408 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5409 symbol_attribute attr
;
5412 match is_type_attr_spec
= MATCH_NO
;
5414 if (gfc_current_state () == COMP_DERIVED
)
5417 gfc_clear_attr (&attr
);
5421 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
);
5422 if (is_type_attr_spec
== MATCH_ERROR
)
5424 } while (is_type_attr_spec
== MATCH_YES
);
5426 if (gfc_match (" ::") != MATCH_YES
&& attr
.access
!= ACCESS_UNKNOWN
)
5428 gfc_error ("Expected :: in TYPE definition at %C");
5432 m
= gfc_match (" %n%t", name
);
5436 /* Make sure the name isn't the name of an intrinsic type. The
5437 'double {precision,complex}' types don't get past the name
5438 matcher, unless they're written as a single word or in fixed
5440 if (strcmp (name
, "integer") == 0
5441 || strcmp (name
, "real") == 0
5442 || strcmp (name
, "character") == 0
5443 || strcmp (name
, "logical") == 0
5444 || strcmp (name
, "complex") == 0
5445 || strcmp (name
, "doubleprecision") == 0
5446 || strcmp (name
, "doublecomplex") == 0)
5448 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5453 if (gfc_get_symbol (name
, NULL
, &sym
))
5456 if (sym
->ts
.type
!= BT_UNKNOWN
)
5458 gfc_error ("Derived type name '%s' at %C already has a basic type "
5459 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
5463 /* The symbol may already have the derived attribute without the
5464 components. The ways this can happen is via a function
5465 definition, an INTRINSIC statement or a subtype in another
5466 derived type that is a pointer. The first part of the AND clause
5467 is true if a the symbol is not the return value of a function. */
5468 if (sym
->attr
.flavor
!= FL_DERIVED
5469 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
5472 if (sym
->components
!= NULL
)
5474 gfc_error ("Derived type definition of '%s' at %C has already been "
5475 "defined", sym
->name
);
5479 if (attr
.access
!= ACCESS_UNKNOWN
5480 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
5483 /* See if the derived type was labeled as bind(c). */
5484 if (attr
.is_bind_c
!= 0)
5485 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
5487 gfc_new_block
= sym
;
5493 /* Cray Pointees can be declared as:
5494 pointer (ipt, a (n,m,...,*))
5495 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5496 cheat and set a constant bound of 1 for the last dimension, if this
5497 is the case. Since there is no bounds-checking for Cray Pointees,
5498 this will be okay. */
5501 gfc_mod_pointee_as (gfc_array_spec
*as
)
5503 as
->cray_pointee
= true; /* This will be useful to know later. */
5504 if (as
->type
== AS_ASSUMED_SIZE
)
5506 as
->type
= AS_EXPLICIT
;
5507 as
->upper
[as
->rank
- 1] = gfc_int_expr (1);
5508 as
->cp_was_assumed
= true;
5510 else if (as
->type
== AS_ASSUMED_SHAPE
)
5512 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5519 /* Match the enum definition statement, here we are trying to match
5520 the first line of enum definition statement.
5521 Returns MATCH_YES if match is found. */
5524 gfc_match_enum (void)
5528 m
= gfc_match_eos ();
5532 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
5540 /* Match a variable name with an optional initializer. When this
5541 subroutine is called, a variable is expected to be parsed next.
5542 Depending on what is happening at the moment, updates either the
5543 symbol table or the current interface. */
5546 enumerator_decl (void)
5548 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5549 gfc_expr
*initializer
;
5550 gfc_array_spec
*as
= NULL
;
5558 old_locus
= gfc_current_locus
;
5560 /* When we get here, we've just matched a list of attributes and
5561 maybe a type and a double colon. The next thing we expect to see
5562 is the name of the symbol. */
5563 m
= gfc_match_name (name
);
5567 var_locus
= gfc_current_locus
;
5569 /* OK, we've successfully matched the declaration. Now put the
5570 symbol in the current namespace. If we fail to create the symbol,
5572 if (build_sym (name
, NULL
, &as
, &var_locus
) == FAILURE
)
5578 /* The double colon must be present in order to have initializers.
5579 Otherwise the statement is ambiguous with an assignment statement. */
5582 if (gfc_match_char ('=') == MATCH_YES
)
5584 m
= gfc_match_init_expr (&initializer
);
5587 gfc_error ("Expected an initialization expression at %C");
5596 /* If we do not have an initializer, the initialization value of the
5597 previous enumerator (stored in last_initializer) is incremented
5598 by 1 and is used to initialize the current enumerator. */
5599 if (initializer
== NULL
)
5600 initializer
= gfc_enum_initializer (last_initializer
, old_locus
);
5602 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
5604 gfc_error("ENUMERATOR %L not initialized with integer expression",
5607 gfc_free_enum_history ();
5611 /* Store this current initializer, for the next enumerator variable
5612 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5613 use last_initializer below. */
5614 last_initializer
= initializer
;
5615 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
5617 /* Maintain enumerator history. */
5618 gfc_find_symbol (name
, NULL
, 0, &sym
);
5619 create_enum_history (sym
, last_initializer
);
5621 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
5624 /* Free stuff up and return. */
5625 gfc_free_expr (initializer
);
5631 /* Match the enumerator definition statement. */
5634 gfc_match_enumerator_def (void)
5639 gfc_clear_ts (¤t_ts
);
5641 m
= gfc_match (" enumerator");
5645 m
= gfc_match (" :: ");
5646 if (m
== MATCH_ERROR
)
5649 colon_seen
= (m
== MATCH_YES
);
5651 if (gfc_current_state () != COMP_ENUM
)
5653 gfc_error ("ENUM definition statement expected before %C");
5654 gfc_free_enum_history ();
5658 (¤t_ts
)->type
= BT_INTEGER
;
5659 (¤t_ts
)->kind
= gfc_c_int_kind
;
5661 gfc_clear_attr (¤t_attr
);
5662 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
5671 m
= enumerator_decl ();
5672 if (m
== MATCH_ERROR
)
5677 if (gfc_match_eos () == MATCH_YES
)
5679 if (gfc_match_char (',') != MATCH_YES
)
5683 if (gfc_current_state () == COMP_ENUM
)
5685 gfc_free_enum_history ();
5686 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5691 gfc_free_array_spec (current_as
);