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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector
;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts
;
48 static symbol_attribute current_attr
;
49 static gfc_array_spec
*current_as
;
50 static int colon_seen
;
52 /* The current binding label (if any). */
53 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line
;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals
= 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr
*last_initializer
;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr
*initializer
;
73 struct enumerator_history
*next
;
77 /* Header of enum history chain. */
79 static enumerator_history
*enum_history
= NULL
;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history
*max_enum
= NULL
;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol
*gfc_new_block
;
89 locus gfc_function_kind_locus
;
90 locus gfc_function_type_locus
;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data
= false;
98 gfc_in_match_data (void)
100 return in_match_data
;
104 set_in_match_data (bool set_value
)
106 in_match_data
= set_value
;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable
*p
)
114 gfc_data_variable
*q
;
119 gfc_free_expr (p
->expr
);
120 gfc_free_iterator (&p
->iter
, 0);
121 free_variable (p
->list
);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value
*p
)
137 gfc_free_expr (p
->expr
);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data
*p
)
153 free_variable (p
->var
);
154 free_value (p
->value
);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace
*ns
)
176 static match
var_element (gfc_data_variable
*);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable
*parent
)
184 gfc_data_variable
*tail
, var
;
187 m
= var_element (&var
);
188 if (m
== MATCH_ERROR
)
193 tail
= gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES
)
203 m
= gfc_match_iterator (&parent
->iter
, 1);
206 if (m
== MATCH_ERROR
)
209 m
= var_element (&var
);
210 if (m
== MATCH_ERROR
)
215 tail
->next
= gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES
)
226 gfc_syntax_error (ST_DATA
);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable
*new)
240 memset (new, 0, sizeof (gfc_data_variable
));
242 if (gfc_match_char ('(') == MATCH_YES
)
243 return var_list (new);
245 m
= gfc_match_variable (&new->expr
, 0);
249 sym
= new->expr
->symtree
->n
.sym
;
251 if (!sym
->attr
.function
&& gfc_current_ns
->parent
252 && gfc_current_ns
->parent
== sym
->ns
)
254 gfc_error ("Host associated variable '%s' may not be in the DATA "
255 "statement at %C", sym
->name
);
259 if (gfc_current_state () != COMP_BLOCK_DATA
260 && sym
->attr
.in_common
261 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
262 "common block variable '%s' in DATA statement at %C",
263 sym
->name
) == FAILURE
)
266 if (gfc_add_data (&sym
->attr
, sym
->name
, &new->expr
->where
) == FAILURE
)
273 /* Match the top-level list of data variables. */
276 top_var_list (gfc_data
*d
)
278 gfc_data_variable var
, *tail
, *new;
285 m
= var_element (&var
);
288 if (m
== MATCH_ERROR
)
291 new = gfc_get_data_variable ();
301 if (gfc_match_char ('/') == MATCH_YES
)
303 if (gfc_match_char (',') != MATCH_YES
)
310 gfc_syntax_error (ST_DATA
);
311 gfc_free_data_all (gfc_current_ns
);
317 match_data_constant (gfc_expr
**result
)
319 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
325 m
= gfc_match_literal_constant (&expr
, 1);
332 if (m
== MATCH_ERROR
)
335 m
= gfc_match_null (result
);
339 old_loc
= gfc_current_locus
;
341 /* Should this be a structure component, try to match it
342 before matching a name. */
343 m
= gfc_match_rvalue (result
);
344 if (m
== MATCH_ERROR
)
347 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
349 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
354 gfc_current_locus
= old_loc
;
356 m
= gfc_match_name (name
);
360 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
364 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
366 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
370 else if (sym
->attr
.flavor
== FL_DERIVED
)
371 return gfc_match_structure_constructor (sym
, result
);
373 *result
= gfc_copy_expr (sym
->value
);
378 /* Match a list of values in a DATA statement. The leading '/' has
379 already been seen at this point. */
382 top_val_list (gfc_data
*data
)
384 gfc_data_value
*new, *tail
;
393 m
= match_data_constant (&expr
);
396 if (m
== MATCH_ERROR
)
399 new = gfc_get_data_value ();
408 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
416 msg
= gfc_extract_int (expr
, &tmp
);
417 gfc_free_expr (expr
);
425 m
= match_data_constant (&tail
->expr
);
428 if (m
== MATCH_ERROR
)
432 if (gfc_match_char ('/') == MATCH_YES
)
434 if (gfc_match_char (',') == MATCH_NO
)
441 gfc_syntax_error (ST_DATA
);
442 gfc_free_data_all (gfc_current_ns
);
447 /* Matches an old style initialization. */
450 match_old_style_init (const char *name
)
457 /* Set up data structure to hold initializers. */
458 gfc_find_sym_tree (name
, NULL
, 0, &st
);
461 newdata
= gfc_get_data ();
462 newdata
->var
= gfc_get_data_variable ();
463 newdata
->var
->expr
= gfc_get_variable_expr (st
);
464 newdata
->where
= gfc_current_locus
;
466 /* Match initial value list. This also eats the terminal '/'. */
467 m
= top_val_list (newdata
);
476 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
481 /* Mark the variable as having appeared in a data statement. */
482 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
488 /* Chain in namespace list of DATA initializers. */
489 newdata
->next
= gfc_current_ns
->data
;
490 gfc_current_ns
->data
= newdata
;
496 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
497 we are matching a DATA statement and are therefore issuing an error
498 if we encounter something unexpected, if not, we're trying to match
499 an old-style initialization expression of the form INTEGER I /2/. */
502 gfc_match_data (void)
507 set_in_match_data (true);
511 new = gfc_get_data ();
512 new->where
= gfc_current_locus
;
514 m
= top_var_list (new);
518 m
= top_val_list (new);
522 new->next
= gfc_current_ns
->data
;
523 gfc_current_ns
->data
= new;
525 if (gfc_match_eos () == MATCH_YES
)
528 gfc_match_char (','); /* Optional comma */
531 set_in_match_data (false);
535 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
542 set_in_match_data (false);
548 /************************ Declaration statements *********************/
550 /* Match an intent specification. Since this can only happen after an
551 INTENT word, a legal intent-spec must follow. */
554 match_intent_spec (void)
557 if (gfc_match (" ( in out )") == MATCH_YES
)
559 if (gfc_match (" ( in )") == MATCH_YES
)
561 if (gfc_match (" ( out )") == MATCH_YES
)
564 gfc_error ("Bad INTENT specification at %C");
565 return INTENT_UNKNOWN
;
569 /* Matches a character length specification, which is either a
570 specification expression or a '*'. */
573 char_len_param_value (gfc_expr
**expr
)
575 if (gfc_match_char ('*') == MATCH_YES
)
581 return gfc_match_expr (expr
);
585 /* A character length is a '*' followed by a literal integer or a
586 char_len_param_value in parenthesis. */
589 match_char_length (gfc_expr
**expr
)
594 m
= gfc_match_char ('*');
598 m
= gfc_match_small_literal_int (&length
, NULL
);
599 if (m
== MATCH_ERROR
)
604 *expr
= gfc_int_expr (length
);
608 if (gfc_match_char ('(') == MATCH_NO
)
611 m
= char_len_param_value (expr
);
612 if (m
== MATCH_ERROR
)
617 if (gfc_match_char (')') == MATCH_NO
)
619 gfc_free_expr (*expr
);
627 gfc_error ("Syntax error in character length specification at %C");
632 /* Special subroutine for finding a symbol. Check if the name is found
633 in the current name space. If not, and we're compiling a function or
634 subroutine and the parent compilation unit is an interface, then check
635 to see if the name we've been given is the name of the interface
636 (located in another namespace). */
639 find_special (const char *name
, gfc_symbol
**result
)
644 i
= gfc_get_symbol (name
, NULL
, result
);
648 if (gfc_current_state () != COMP_SUBROUTINE
649 && gfc_current_state () != COMP_FUNCTION
)
652 s
= gfc_state_stack
->previous
;
656 if (s
->state
!= COMP_INTERFACE
)
659 goto end
; /* Nameless interface. */
661 if (strcmp (name
, s
->sym
->name
) == 0)
672 /* Special subroutine for getting a symbol node associated with a
673 procedure name, used in SUBROUTINE and FUNCTION statements. The
674 symbol is created in the parent using with symtree node in the
675 child unit pointing to the symbol. If the current namespace has no
676 parent, then the symbol is just created in the current unit. */
679 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
685 /* Module functions have to be left in their own namespace because
686 they have potentially (almost certainly!) already been referenced.
687 In this sense, they are rather like external functions. This is
688 fixed up in resolve.c(resolve_entries), where the symbol name-
689 space is set to point to the master function, so that the fake
690 result mechanism can work. */
691 if (module_fcn_entry
)
693 /* Present if entry is declared to be a module procedure. */
694 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
697 rc
= gfc_get_symbol (name
, NULL
, result
);
698 else if (gfc_get_symbol (name
, NULL
, &sym
) == 0
700 && sym
->ts
.type
!= BT_UNKNOWN
701 && (*result
)->ts
.type
== BT_UNKNOWN
702 && sym
->attr
.flavor
== FL_UNKNOWN
)
703 /* Pick up the typespec for the entry, if declared in the function
704 body. Note that this symbol is FL_UNKNOWN because it will
705 only have appeared in a type declaration. The local symtree
706 is set to point to the module symbol and a unique symtree
707 to the local version. This latter ensures a correct clearing
710 (*result
)->ts
= sym
->ts
;
711 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
713 st
= gfc_get_unique_symtree (gfc_current_ns
);
718 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
724 gfc_current_ns
->refs
++;
726 if (sym
&& !sym
->new && gfc_current_state () != COMP_INTERFACE
)
728 /* Trap another encompassed procedure with the same name. All
729 these conditions are necessary to avoid picking up an entry
730 whose name clashes with that of the encompassing procedure;
731 this is handled using gsymbols to register unique,globally
733 if (sym
->attr
.flavor
!= 0
734 && sym
->attr
.proc
!= 0
735 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
736 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
737 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
738 name
, &sym
->declared_at
);
740 /* Trap a procedure with a name the same as interface in the
741 encompassing scope. */
742 if (sym
->attr
.generic
!= 0
743 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
744 && !sym
->attr
.mod_proc
)
745 gfc_error_now ("Name '%s' at %C is already defined"
746 " as a generic interface at %L",
747 name
, &sym
->declared_at
);
749 /* Trap declarations of attributes in encompassing scope. The
750 signature for this is that ts.kind is set. Legitimate
751 references only set ts.type. */
752 if (sym
->ts
.kind
!= 0
753 && !sym
->attr
.implicit_type
754 && sym
->attr
.proc
== 0
755 && gfc_current_ns
->parent
!= NULL
756 && sym
->attr
.access
== 0
757 && !module_fcn_entry
)
758 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
759 "and must not have attributes declared at %L",
760 name
, &sym
->declared_at
);
763 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
766 /* Module function entries will already have a symtree in
767 the current namespace but will need one at module level. */
768 if (module_fcn_entry
)
770 /* Present if entry is declared to be a module procedure. */
771 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
773 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
776 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
781 /* See if the procedure should be a module procedure. */
783 if (((sym
->ns
->proc_name
!= NULL
784 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
785 && sym
->attr
.proc
!= PROC_MODULE
)
786 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
787 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
788 sym
->name
, NULL
) == FAILURE
)
795 /* Verify that the given symbol representing a parameter is C
796 interoperable, by checking to see if it was marked as such after
797 its declaration. If the given symbol is not interoperable, a
798 warning is reported, thus removing the need to return the status to
799 the calling function. The standard does not require the user use
800 one of the iso_c_binding named constants to declare an
801 interoperable parameter, but we can't be sure if the param is C
802 interop or not if the user doesn't. For example, integer(4) may be
803 legal Fortran, but doesn't have meaning in C. It may interop with
804 a number of the C types, which causes a problem because the
805 compiler can't know which one. This code is almost certainly not
806 portable, and the user will get what they deserve if the C type
807 across platforms isn't always interoperable with integer(4). If
808 the user had used something like integer(c_int) or integer(c_long),
809 the compiler could have automatically handled the varying sizes
813 verify_c_interop_param (gfc_symbol
*sym
)
815 int is_c_interop
= 0;
816 try retval
= SUCCESS
;
818 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
819 Don't repeat the checks here. */
820 if (sym
->attr
.implicit_type
)
823 /* For subroutines or functions that are passed to a BIND(C) procedure,
824 they're interoperable if they're BIND(C) and their params are all
826 if (sym
->attr
.flavor
== FL_PROCEDURE
)
828 if (sym
->attr
.is_bind_c
== 0)
830 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
831 "attribute to be C interoperable", sym
->name
,
832 &(sym
->declared_at
));
838 if (sym
->attr
.is_c_interop
== 1)
839 /* We've already checked this procedure; don't check it again. */
842 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
847 /* See if we've stored a reference to a procedure that owns sym. */
848 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
850 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
853 (verify_c_interop (&(sym
->ts
), sym
->name
, &(sym
->declared_at
))
856 if (is_c_interop
!= 1)
858 /* Make personalized messages to give better feedback. */
859 if (sym
->ts
.type
== BT_DERIVED
)
860 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
861 " procedure '%s' but is not C interoperable "
862 "because derived type '%s' is not C interoperable",
863 sym
->name
, &(sym
->declared_at
),
864 sym
->ns
->proc_name
->name
,
865 sym
->ts
.derived
->name
);
867 gfc_warning ("Variable '%s' at %L is a parameter to the "
868 "BIND(C) procedure '%s' but may not be C "
870 sym
->name
, &(sym
->declared_at
),
871 sym
->ns
->proc_name
->name
);
874 /* Character strings are only C interoperable if they have a
876 if (sym
->ts
.type
== BT_CHARACTER
)
878 gfc_charlen
*cl
= sym
->ts
.cl
;
879 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
880 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
882 gfc_error ("Character argument '%s' at %L "
883 "must be length 1 because "
884 "procedure '%s' is BIND(C)",
885 sym
->name
, &sym
->declared_at
,
886 sym
->ns
->proc_name
->name
);
891 /* We have to make sure that any param to a bind(c) routine does
892 not have the allocatable, pointer, or optional attributes,
893 according to J3/04-007, section 5.1. */
894 if (sym
->attr
.allocatable
== 1)
896 gfc_error ("Variable '%s' at %L cannot have the "
897 "ALLOCATABLE attribute because procedure '%s'"
898 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
899 sym
->ns
->proc_name
->name
);
903 if (sym
->attr
.pointer
== 1)
905 gfc_error ("Variable '%s' at %L cannot have the "
906 "POINTER attribute because procedure '%s'"
907 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
908 sym
->ns
->proc_name
->name
);
912 if (sym
->attr
.optional
== 1)
914 gfc_error ("Variable '%s' at %L cannot have the "
915 "OPTIONAL attribute because procedure '%s'"
916 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
917 sym
->ns
->proc_name
->name
);
921 /* Make sure that if it has the dimension attribute, that it is
922 either assumed size or explicit shape. */
925 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
927 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
928 "argument to the procedure '%s' at %L because "
929 "the procedure is BIND(C)", sym
->name
,
930 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
931 &(sym
->ns
->proc_name
->declared_at
));
935 if (sym
->as
->type
== AS_DEFERRED
)
937 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
938 "argument to the procedure '%s' at %L because "
939 "the procedure is BIND(C)", sym
->name
,
940 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
941 &(sym
->ns
->proc_name
->declared_at
));
952 /* Function called by variable_decl() that adds a name to the symbol table. */
955 build_sym (const char *name
, gfc_charlen
*cl
,
956 gfc_array_spec
**as
, locus
*var_locus
)
958 symbol_attribute attr
;
961 if (gfc_get_symbol (name
, NULL
, &sym
))
964 /* Start updating the symbol table. Add basic type attribute if present. */
965 if (current_ts
.type
!= BT_UNKNOWN
966 && (sym
->attr
.implicit_type
== 0
967 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
968 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
971 if (sym
->ts
.type
== BT_CHARACTER
)
974 /* Add dimension attribute if present. */
975 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
979 /* Add attribute to symbol. The copy is so that we can reset the
980 dimension attribute. */
984 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
987 /* Finish any work that may need to be done for the binding label,
988 if it's a bind(c). The bind(c) attr is found before the symbol
989 is made, and before the symbol name (for data decls), so the
990 current_ts is holding the binding label, or nothing if the
991 name= attr wasn't given. Therefore, test here if we're dealing
992 with a bind(c) and make sure the binding label is set correctly. */
993 if (sym
->attr
.is_bind_c
== 1)
995 if (sym
->binding_label
[0] == '\0')
997 /* Set the binding label and verify that if a NAME= was specified
998 then only one identifier was in the entity-decl-list. */
999 if (set_binding_label (sym
->binding_label
, sym
->name
,
1000 num_idents_on_line
) == FAILURE
)
1005 /* See if we know we're in a common block, and if it's a bind(c)
1006 common then we need to make sure we're an interoperable type. */
1007 if (sym
->attr
.in_common
== 1)
1009 /* Test the common block object. */
1010 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1011 && sym
->ts
.is_c_interop
!= 1)
1013 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1014 "must be declared with a C interoperable "
1015 "kind since common block '%s' is BIND(C)",
1016 sym
->name
, sym
->common_block
->name
,
1017 sym
->common_block
->name
);
1022 sym
->attr
.implied_index
= 0;
1028 /* Set character constant to the given length. The constant will be padded or
1032 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, bool array
)
1037 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1038 gcc_assert (expr
->ts
.type
== BT_CHARACTER
&& expr
->ts
.kind
== 1);
1040 slen
= expr
->value
.character
.length
;
1043 s
= gfc_getmem (len
+ 1);
1044 memcpy (s
, expr
->value
.character
.string
, MIN (len
, slen
));
1046 memset (&s
[slen
], ' ', len
- slen
);
1048 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1049 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1050 "(%d/%d)", &expr
->where
, slen
, len
);
1052 /* Apply the standard by 'hand' otherwise it gets cleared for
1054 if (array
&& slen
< len
&& !(gfc_option
.allow_std
& GFC_STD_GNU
))
1055 gfc_error_now ("The CHARACTER elements of the array constructor "
1056 "at %L must have the same length (%d/%d)",
1057 &expr
->where
, slen
, len
);
1060 gfc_free (expr
->value
.character
.string
);
1061 expr
->value
.character
.string
= s
;
1062 expr
->value
.character
.length
= len
;
1067 /* Function to create and update the enumerator history
1068 using the information passed as arguments.
1069 Pointer "max_enum" is also updated, to point to
1070 enum history node containing largest initializer.
1072 SYM points to the symbol node of enumerator.
1073 INIT points to its enumerator value. */
1076 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1078 enumerator_history
*new_enum_history
;
1079 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1081 new_enum_history
= gfc_getmem (sizeof (enumerator_history
));
1083 new_enum_history
->sym
= sym
;
1084 new_enum_history
->initializer
= init
;
1085 new_enum_history
->next
= NULL
;
1087 if (enum_history
== NULL
)
1089 enum_history
= new_enum_history
;
1090 max_enum
= enum_history
;
1094 new_enum_history
->next
= enum_history
;
1095 enum_history
= new_enum_history
;
1097 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1098 new_enum_history
->initializer
->value
.integer
) < 0)
1099 max_enum
= new_enum_history
;
1104 /* Function to free enum kind history. */
1107 gfc_free_enum_history (void)
1109 enumerator_history
*current
= enum_history
;
1110 enumerator_history
*next
;
1112 while (current
!= NULL
)
1114 next
= current
->next
;
1119 enum_history
= NULL
;
1123 /* Function called by variable_decl() that adds an initialization
1124 expression to a symbol. */
1127 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1129 symbol_attribute attr
;
1134 if (find_special (name
, &sym
))
1139 /* If this symbol is confirming an implicit parameter type,
1140 then an initialization expression is not allowed. */
1141 if (attr
.flavor
== FL_PARAMETER
1142 && sym
->value
!= NULL
1145 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1154 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1161 /* An initializer is required for PARAMETER declarations. */
1162 if (attr
.flavor
== FL_PARAMETER
)
1164 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1170 /* If a variable appears in a DATA block, it cannot have an
1174 gfc_error ("Variable '%s' at %C with an initializer already "
1175 "appears in a DATA statement", sym
->name
);
1179 /* Check if the assignment can happen. This has to be put off
1180 until later for a derived type variable. */
1181 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1182 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1185 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.cl
)
1187 /* Update symbol character length according initializer. */
1188 if (sym
->ts
.cl
->length
== NULL
)
1191 /* If there are multiple CHARACTER variables declared on the
1192 same line, we don't want them to share the same length. */
1193 sym
->ts
.cl
= gfc_get_charlen ();
1194 sym
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1195 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1197 if (sym
->attr
.flavor
== FL_PARAMETER
)
1199 if (init
->expr_type
== EXPR_CONSTANT
)
1201 clen
= init
->value
.character
.length
;
1202 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
1204 else if (init
->expr_type
== EXPR_ARRAY
)
1206 gfc_expr
*p
= init
->value
.constructor
->expr
;
1207 clen
= p
->value
.character
.length
;
1208 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
1210 else if (init
->ts
.cl
&& init
->ts
.cl
->length
)
1211 sym
->ts
.cl
->length
=
1212 gfc_copy_expr (sym
->value
->ts
.cl
->length
);
1215 /* Update initializer character length according symbol. */
1216 else if (sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1218 int len
= mpz_get_si (sym
->ts
.cl
->length
->value
.integer
);
1219 gfc_constructor
* p
;
1221 if (init
->expr_type
== EXPR_CONSTANT
)
1222 gfc_set_constant_character_len (len
, init
, false);
1223 else if (init
->expr_type
== EXPR_ARRAY
)
1225 /* Build a new charlen to prevent simplification from
1226 deleting the length before it is resolved. */
1227 init
->ts
.cl
= gfc_get_charlen ();
1228 init
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1229 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1230 init
->ts
.cl
->length
= gfc_copy_expr (sym
->ts
.cl
->length
);
1232 for (p
= init
->value
.constructor
; p
; p
= p
->next
)
1233 gfc_set_constant_character_len (len
, p
->expr
, false);
1238 /* Need to check if the expression we initialized this
1239 to was one of the iso_c_binding named constants. If so,
1240 and we're a parameter (constant), let it be iso_c.
1242 integer(c_int), parameter :: my_int = c_int
1243 integer(my_int) :: my_int_2
1244 If we mark my_int as iso_c (since we can see it's value
1245 is equal to one of the named constants), then my_int_2
1246 will be considered C interoperable. */
1247 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1249 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1250 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1251 /* attr bits needed for module files. */
1252 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1253 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1254 if (init
->ts
.is_iso_c
)
1255 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1258 /* Add initializer. Make sure we keep the ranks sane. */
1259 if (sym
->attr
.dimension
&& init
->rank
== 0)
1265 if (sym
->attr
.flavor
== FL_PARAMETER
1266 && init
->expr_type
== EXPR_CONSTANT
1267 && spec_size (sym
->as
, &size
) == SUCCESS
1268 && mpz_cmp_si (size
, 0) > 0)
1270 array
= gfc_start_constructor (init
->ts
.type
, init
->ts
.kind
,
1273 array
->value
.constructor
= c
= NULL
;
1274 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1276 if (array
->value
.constructor
== NULL
)
1278 array
->value
.constructor
= c
= gfc_get_constructor ();
1283 c
->next
= gfc_get_constructor ();
1285 c
->expr
= gfc_copy_expr (init
);
1289 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1290 for (n
= 0; n
< sym
->as
->rank
; n
++)
1291 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1296 init
->rank
= sym
->as
->rank
;
1300 if (sym
->attr
.save
== SAVE_NONE
)
1301 sym
->attr
.save
= SAVE_IMPLICIT
;
1309 /* Function called by variable_decl() that adds a name to a structure
1313 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1314 gfc_array_spec
**as
)
1318 /* If the current symbol is of the same derived type that we're
1319 constructing, it must have the pointer attribute. */
1320 if (current_ts
.type
== BT_DERIVED
1321 && current_ts
.derived
== gfc_current_block ()
1322 && current_attr
.pointer
== 0)
1324 gfc_error ("Component at %C must have the POINTER attribute");
1328 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1330 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1332 gfc_error ("Array component of structure at %C must have explicit "
1333 "or deferred shape");
1338 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1343 gfc_set_component_attr (c
, ¤t_attr
);
1345 c
->initializer
= *init
;
1353 /* Check array components. */
1358 gfc_error ("Allocatable component at %C must be an array");
1367 if (c
->as
->type
!= AS_DEFERRED
)
1369 gfc_error ("Pointer array component of structure at %C must have a "
1374 else if (c
->allocatable
)
1376 if (c
->as
->type
!= AS_DEFERRED
)
1378 gfc_error ("Allocatable component of structure at %C must have a "
1385 if (c
->as
->type
!= AS_EXPLICIT
)
1387 gfc_error ("Array component of structure at %C must have an "
1397 /* Match a 'NULL()', and possibly take care of some side effects. */
1400 gfc_match_null (gfc_expr
**result
)
1406 m
= gfc_match (" null ( )");
1410 /* The NULL symbol now has to be/become an intrinsic function. */
1411 if (gfc_get_symbol ("null", NULL
, &sym
))
1413 gfc_error ("NULL() initialization at %C is ambiguous");
1417 gfc_intrinsic_symbol (sym
);
1419 if (sym
->attr
.proc
!= PROC_INTRINSIC
1420 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1421 sym
->name
, NULL
) == FAILURE
1422 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1425 e
= gfc_get_expr ();
1426 e
->where
= gfc_current_locus
;
1427 e
->expr_type
= EXPR_NULL
;
1428 e
->ts
.type
= BT_UNKNOWN
;
1436 /* Match a variable name with an optional initializer. When this
1437 subroutine is called, a variable is expected to be parsed next.
1438 Depending on what is happening at the moment, updates either the
1439 symbol table or the current interface. */
1442 variable_decl (int elem
)
1444 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1445 gfc_expr
*initializer
, *char_len
;
1447 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1458 old_locus
= gfc_current_locus
;
1460 /* When we get here, we've just matched a list of attributes and
1461 maybe a type and a double colon. The next thing we expect to see
1462 is the name of the symbol. */
1463 m
= gfc_match_name (name
);
1467 var_locus
= gfc_current_locus
;
1469 /* Now we could see the optional array spec. or character length. */
1470 m
= gfc_match_array_spec (&as
);
1471 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1472 cp_as
= gfc_copy_array_spec (as
);
1473 else if (m
== MATCH_ERROR
)
1477 as
= gfc_copy_array_spec (current_as
);
1482 if (current_ts
.type
== BT_CHARACTER
)
1484 switch (match_char_length (&char_len
))
1487 cl
= gfc_get_charlen ();
1488 cl
->next
= gfc_current_ns
->cl_list
;
1489 gfc_current_ns
->cl_list
= cl
;
1491 cl
->length
= char_len
;
1494 /* Non-constant lengths need to be copied after the first
1495 element. Also copy assumed lengths. */
1498 && (current_ts
.cl
->length
== NULL
1499 || current_ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1501 cl
= gfc_get_charlen ();
1502 cl
->next
= gfc_current_ns
->cl_list
;
1503 gfc_current_ns
->cl_list
= cl
;
1504 cl
->length
= gfc_copy_expr (current_ts
.cl
->length
);
1516 /* If this symbol has already shown up in a Cray Pointer declaration,
1517 then we want to set the type & bail out. */
1518 if (gfc_option
.flag_cray_pointer
)
1520 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1521 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1523 sym
->ts
.type
= current_ts
.type
;
1524 sym
->ts
.kind
= current_ts
.kind
;
1526 sym
->ts
.derived
= current_ts
.derived
;
1527 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1528 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1531 /* Check to see if we have an array specification. */
1534 if (sym
->as
!= NULL
)
1536 gfc_error ("Duplicate array spec for Cray pointee at %C");
1537 gfc_free_array_spec (cp_as
);
1543 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1544 gfc_internal_error ("Couldn't set pointee array spec.");
1546 /* Fix the array spec. */
1547 m
= gfc_mod_pointee_as (sym
->as
);
1548 if (m
== MATCH_ERROR
)
1556 gfc_free_array_spec (cp_as
);
1561 /* OK, we've successfully matched the declaration. Now put the
1562 symbol in the current namespace, because it might be used in the
1563 optional initialization expression for this symbol, e.g. this is
1566 integer, parameter :: i = huge(i)
1568 This is only true for parameters or variables of a basic type.
1569 For components of derived types, it is not true, so we don't
1570 create a symbol for those yet. If we fail to create the symbol,
1572 if (gfc_current_state () != COMP_DERIVED
1573 && build_sym (name
, cl
, &as
, &var_locus
) == FAILURE
)
1579 /* An interface body specifies all of the procedure's
1580 characteristics and these shall be consistent with those
1581 specified in the procedure definition, except that the interface
1582 may specify a procedure that is not pure if the procedure is
1583 defined to be pure(12.3.2). */
1584 if (current_ts
.type
== BT_DERIVED
1585 && gfc_current_ns
->proc_name
1586 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1587 && current_ts
.derived
->ns
!= gfc_current_ns
)
1590 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.derived
->name
);
1591 if (!(current_ts
.derived
->attr
.imported
1593 && st
->n
.sym
== current_ts
.derived
)
1594 && !gfc_current_ns
->has_import_set
)
1596 gfc_error ("the type of '%s' at %C has not been declared within the "
1603 /* In functions that have a RESULT variable defined, the function
1604 name always refers to function calls. Therefore, the name is
1605 not allowed to appear in specification statements. */
1606 if (gfc_current_state () == COMP_FUNCTION
1607 && gfc_current_block () != NULL
1608 && gfc_current_block ()->result
!= NULL
1609 && gfc_current_block ()->result
!= gfc_current_block ()
1610 && strcmp (gfc_current_block ()->name
, name
) == 0)
1612 gfc_error ("Function name '%s' not allowed at %C", name
);
1617 /* We allow old-style initializations of the form
1618 integer i /2/, j(4) /3*3, 1/
1619 (if no colon has been seen). These are different from data
1620 statements in that initializers are only allowed to apply to the
1621 variable immediately preceding, i.e.
1623 is not allowed. Therefore we have to do some work manually, that
1624 could otherwise be left to the matchers for DATA statements. */
1626 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1628 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1629 "initialization at %C") == FAILURE
)
1632 return match_old_style_init (name
);
1635 /* The double colon must be present in order to have initializers.
1636 Otherwise the statement is ambiguous with an assignment statement. */
1639 if (gfc_match (" =>") == MATCH_YES
)
1641 if (!current_attr
.pointer
)
1643 gfc_error ("Initialization at %C isn't for a pointer variable");
1648 m
= gfc_match_null (&initializer
);
1651 gfc_error ("Pointer initialization requires a NULL() at %C");
1655 if (gfc_pure (NULL
))
1657 gfc_error ("Initialization of pointer at %C is not allowed in "
1658 "a PURE procedure");
1666 else if (gfc_match_char ('=') == MATCH_YES
)
1668 if (current_attr
.pointer
)
1670 gfc_error ("Pointer initialization at %C requires '=>', "
1676 m
= gfc_match_init_expr (&initializer
);
1679 gfc_error ("Expected an initialization expression at %C");
1683 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
))
1685 gfc_error ("Initialization of variable at %C is not allowed in "
1686 "a PURE procedure");
1695 if (initializer
!= NULL
&& current_attr
.allocatable
1696 && gfc_current_state () == COMP_DERIVED
)
1698 gfc_error ("Initialization of allocatable component at %C is not "
1704 /* Add the initializer. Note that it is fine if initializer is
1705 NULL here, because we sometimes also need to check if a
1706 declaration *must* have an initialization expression. */
1707 if (gfc_current_state () != COMP_DERIVED
)
1708 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
1711 if (current_ts
.type
== BT_DERIVED
1712 && !current_attr
.pointer
&& !initializer
)
1713 initializer
= gfc_default_initializer (¤t_ts
);
1714 t
= build_struct (name
, cl
, &initializer
, &as
);
1717 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
1720 /* Free stuff up and return. */
1721 gfc_free_expr (initializer
);
1722 gfc_free_array_spec (as
);
1728 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1729 This assumes that the byte size is equal to the kind number for
1730 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1733 gfc_match_old_kind_spec (gfc_typespec
*ts
)
1738 if (gfc_match_char ('*') != MATCH_YES
)
1741 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
1745 original_kind
= ts
->kind
;
1747 /* Massage the kind numbers for complex types. */
1748 if (ts
->type
== BT_COMPLEX
)
1752 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1753 gfc_basic_typename (ts
->type
), original_kind
);
1759 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1761 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1762 gfc_basic_typename (ts
->type
), original_kind
);
1766 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
1767 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
1774 /* Match a kind specification. Since kinds are generally optional, we
1775 usually return MATCH_NO if something goes wrong. If a "kind="
1776 string is found, then we know we have an error. */
1779 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
1790 where
= loc
= gfc_current_locus
;
1795 if (gfc_match_char ('(') == MATCH_NO
)
1798 /* Also gobbles optional text. */
1799 if (gfc_match (" kind = ") == MATCH_YES
)
1802 loc
= gfc_current_locus
;
1805 n
= gfc_match_init_expr (&e
);
1809 if (gfc_current_state () == COMP_INTERFACE
1810 || gfc_current_state () == COMP_NONE
1811 || gfc_current_state () == COMP_CONTAINS
)
1813 /* Signal using kind = -1 that the expression might include
1814 use associated or imported parameters and try again after
1815 the specification expressions..... */
1816 if (gfc_match_char (')') != MATCH_YES
)
1818 gfc_error ("Missing right parenthesis at %C");
1825 gfc_function_kind_locus
= loc
;
1826 gfc_undo_symbols ();
1831 /* ....or else, the match is real. */
1833 gfc_error ("Expected initialization expression at %C");
1841 gfc_error ("Expected scalar initialization expression at %C");
1846 msg
= gfc_extract_int (e
, &ts
->kind
);
1854 /* Before throwing away the expression, let's see if we had a
1855 C interoperable kind (and store the fact). */
1856 if (e
->ts
.is_c_interop
== 1)
1858 /* Mark this as c interoperable if being declared with one
1859 of the named constants from iso_c_binding. */
1860 ts
->is_c_interop
= e
->ts
.is_iso_c
;
1861 ts
->f90_type
= e
->ts
.f90_type
;
1867 /* Ignore errors to this point, if we've gotten here. This means
1868 we ignore the m=MATCH_ERROR from above. */
1869 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1871 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
1872 gfc_basic_typename (ts
->type
));
1875 else if (gfc_match_char (')') != MATCH_YES
)
1877 gfc_error ("Missing right parenthesis at %C");
1881 /* All tests passed. */
1884 if(m
== MATCH_ERROR
)
1885 gfc_current_locus
= where
;
1887 /* Return what we know from the test(s). */
1892 gfc_current_locus
= where
;
1898 match_char_kind (int * kind
, int * is_iso_c
)
1907 where
= gfc_current_locus
;
1909 n
= gfc_match_init_expr (&e
);
1911 gfc_error ("Expected initialization expression at %C");
1917 gfc_error ("Expected scalar initialization expression at %C");
1922 msg
= gfc_extract_int (e
, kind
);
1923 *is_iso_c
= e
->ts
.is_iso_c
;
1933 /* Ignore errors to this point, if we've gotten here. This means
1934 we ignore the m=MATCH_ERROR from above. */
1935 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
1937 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
1941 /* All tests passed. */
1944 if (m
== MATCH_ERROR
)
1945 gfc_current_locus
= where
;
1947 /* Return what we know from the test(s). */
1952 gfc_current_locus
= where
;
1956 /* Match the various kind/length specifications in a CHARACTER
1957 declaration. We don't return MATCH_NO. */
1960 match_char_spec (gfc_typespec
*ts
)
1962 int kind
, seen_length
, is_iso_c
;
1972 /* Try the old-style specification first. */
1973 old_char_selector
= 0;
1975 m
= match_char_length (&len
);
1979 old_char_selector
= 1;
1984 m
= gfc_match_char ('(');
1987 m
= MATCH_YES
; /* Character without length is a single char. */
1991 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1992 if (gfc_match (" kind =") == MATCH_YES
)
1994 m
= match_char_kind (&kind
, &is_iso_c
);
1996 if (m
== MATCH_ERROR
)
2001 if (gfc_match (" , len =") == MATCH_NO
)
2004 m
= char_len_param_value (&len
);
2007 if (m
== MATCH_ERROR
)
2014 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2015 if (gfc_match (" len =") == MATCH_YES
)
2017 m
= char_len_param_value (&len
);
2020 if (m
== MATCH_ERROR
)
2024 if (gfc_match_char (')') == MATCH_YES
)
2027 if (gfc_match (" , kind =") != MATCH_YES
)
2030 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2036 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2037 m
= char_len_param_value (&len
);
2040 if (m
== MATCH_ERROR
)
2044 m
= gfc_match_char (')');
2048 if (gfc_match_char (',') != MATCH_YES
)
2051 gfc_match (" kind ="); /* Gobble optional text. */
2053 m
= match_char_kind (&kind
, &is_iso_c
);
2054 if (m
== MATCH_ERROR
)
2060 /* Require a right-paren at this point. */
2061 m
= gfc_match_char (')');
2066 gfc_error ("Syntax error in CHARACTER declaration at %C");
2068 gfc_free_expr (len
);
2074 gfc_free_expr (len
);
2078 /* Do some final massaging of the length values. */
2079 cl
= gfc_get_charlen ();
2080 cl
->next
= gfc_current_ns
->cl_list
;
2081 gfc_current_ns
->cl_list
= cl
;
2083 if (seen_length
== 0)
2084 cl
->length
= gfc_int_expr (1);
2089 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2091 /* We have to know if it was a c interoperable kind so we can
2092 do accurate type checking of bind(c) procs, etc. */
2094 /* Mark this as c interoperable if being declared with one
2095 of the named constants from iso_c_binding. */
2096 ts
->is_c_interop
= is_iso_c
;
2097 else if (len
!= NULL
)
2098 /* Here, we might have parsed something such as: character(c_char)
2099 In this case, the parsing code above grabs the c_char when
2100 looking for the length (line 1690, roughly). it's the last
2101 testcase for parsing the kind params of a character variable.
2102 However, it's not actually the length. this seems like it
2104 To see if the user used a C interop kind, test the expr
2105 of the so called length, and see if it's C interoperable. */
2106 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2112 /* Matches a type specification. If successful, sets the ts structure
2113 to the matched specification. This is necessary for FUNCTION and
2114 IMPLICIT statements.
2116 If implicit_flag is nonzero, then we don't check for the optional
2117 kind specification. Not doing so is needed for matching an IMPLICIT
2118 statement correctly. */
2121 gfc_match_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2123 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2127 locus loc
= gfc_current_locus
;
2131 /* Clear the current binding label, in case one is given. */
2132 curr_binding_label
[0] = '\0';
2134 if (gfc_match (" byte") == MATCH_YES
)
2136 if (gfc_notify_std(GFC_STD_GNU
, "Extension: BYTE type at %C")
2140 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2142 gfc_error ("BYTE type used at %C "
2143 "is not available on the target machine");
2147 ts
->type
= BT_INTEGER
;
2152 if (gfc_match (" integer") == MATCH_YES
)
2154 ts
->type
= BT_INTEGER
;
2155 ts
->kind
= gfc_default_integer_kind
;
2159 if (gfc_match (" character") == MATCH_YES
)
2161 ts
->type
= BT_CHARACTER
;
2162 if (implicit_flag
== 0)
2163 return match_char_spec (ts
);
2168 if (gfc_match (" real") == MATCH_YES
)
2171 ts
->kind
= gfc_default_real_kind
;
2175 if (gfc_match (" double precision") == MATCH_YES
)
2178 ts
->kind
= gfc_default_double_kind
;
2182 if (gfc_match (" complex") == MATCH_YES
)
2184 ts
->type
= BT_COMPLEX
;
2185 ts
->kind
= gfc_default_complex_kind
;
2189 if (gfc_match (" double complex") == MATCH_YES
)
2191 if (gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C does not "
2192 "conform to the Fortran 95 standard") == FAILURE
)
2195 ts
->type
= BT_COMPLEX
;
2196 ts
->kind
= gfc_default_double_kind
;
2200 if (gfc_match (" logical") == MATCH_YES
)
2202 ts
->type
= BT_LOGICAL
;
2203 ts
->kind
= gfc_default_logical_kind
;
2207 m
= gfc_match (" type ( %n )", name
);
2211 if (gfc_current_state () == COMP_INTERFACE
2212 || gfc_current_state () == COMP_NONE
)
2214 gfc_function_type_locus
= loc
;
2215 ts
->type
= BT_UNKNOWN
;
2220 /* Search for the name but allow the components to be defined later. If
2221 type = -1, this typespec has been seen in a function declaration but
2222 the type could not legally be accessed at that point. */
2223 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2225 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2228 else if (ts
->kind
== -1)
2230 if (gfc_find_symbol (name
, NULL
, 0, &sym
))
2232 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2240 if (sym
->attr
.flavor
!= FL_DERIVED
2241 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2244 ts
->type
= BT_DERIVED
;
2251 /* For all types except double, derived and character, look for an
2252 optional kind specifier. MATCH_NO is actually OK at this point. */
2253 if (implicit_flag
== 1)
2256 if (gfc_current_form
== FORM_FREE
)
2258 c
= gfc_peek_char();
2259 if (!gfc_is_whitespace(c
) && c
!= '*' && c
!= '('
2260 && c
!= ':' && c
!= ',')
2264 m
= gfc_match_kind_spec (ts
, false);
2265 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2266 m
= gfc_match_old_kind_spec (ts
);
2269 m
= MATCH_YES
; /* No kind specifier found. */
2275 /* Match an IMPLICIT NONE statement. Actually, this statement is
2276 already matched in parse.c, or we would not end up here in the
2277 first place. So the only thing we need to check, is if there is
2278 trailing garbage. If not, the match is successful. */
2281 gfc_match_implicit_none (void)
2283 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2287 /* Match the letter range(s) of an IMPLICIT statement. */
2290 match_implicit_range (void)
2292 int c
, c1
, c2
, inner
;
2295 cur_loc
= gfc_current_locus
;
2297 gfc_gobble_whitespace ();
2298 c
= gfc_next_char ();
2301 gfc_error ("Missing character range in IMPLICIT at %C");
2308 gfc_gobble_whitespace ();
2309 c1
= gfc_next_char ();
2313 gfc_gobble_whitespace ();
2314 c
= gfc_next_char ();
2319 inner
= 0; /* Fall through. */
2326 gfc_gobble_whitespace ();
2327 c2
= gfc_next_char ();
2331 gfc_gobble_whitespace ();
2332 c
= gfc_next_char ();
2334 if ((c
!= ',') && (c
!= ')'))
2347 gfc_error ("Letters must be in alphabetic order in "
2348 "IMPLICIT statement at %C");
2352 /* See if we can add the newly matched range to the pending
2353 implicits from this IMPLICIT statement. We do not check for
2354 conflicts with whatever earlier IMPLICIT statements may have
2355 set. This is done when we've successfully finished matching
2357 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2364 gfc_syntax_error (ST_IMPLICIT
);
2366 gfc_current_locus
= cur_loc
;
2371 /* Match an IMPLICIT statement, storing the types for
2372 gfc_set_implicit() if the statement is accepted by the parser.
2373 There is a strange looking, but legal syntactic construction
2374 possible. It looks like:
2376 IMPLICIT INTEGER (a-b) (c-d)
2378 This is legal if "a-b" is a constant expression that happens to
2379 equal one of the legal kinds for integers. The real problem
2380 happens with an implicit specification that looks like:
2382 IMPLICIT INTEGER (a-b)
2384 In this case, a typespec matcher that is "greedy" (as most of the
2385 matchers are) gobbles the character range as a kindspec, leaving
2386 nothing left. We therefore have to go a bit more slowly in the
2387 matching process by inhibiting the kindspec checking during
2388 typespec matching and checking for a kind later. */
2391 gfc_match_implicit (void)
2398 /* We don't allow empty implicit statements. */
2399 if (gfc_match_eos () == MATCH_YES
)
2401 gfc_error ("Empty IMPLICIT statement at %C");
2407 /* First cleanup. */
2408 gfc_clear_new_implicit ();
2410 /* A basic type is mandatory here. */
2411 m
= gfc_match_type_spec (&ts
, 1);
2412 if (m
== MATCH_ERROR
)
2417 cur_loc
= gfc_current_locus
;
2418 m
= match_implicit_range ();
2422 /* We may have <TYPE> (<RANGE>). */
2423 gfc_gobble_whitespace ();
2424 c
= gfc_next_char ();
2425 if ((c
== '\n') || (c
== ','))
2427 /* Check for CHARACTER with no length parameter. */
2428 if (ts
.type
== BT_CHARACTER
&& !ts
.cl
)
2430 ts
.kind
= gfc_default_character_kind
;
2431 ts
.cl
= gfc_get_charlen ();
2432 ts
.cl
->next
= gfc_current_ns
->cl_list
;
2433 gfc_current_ns
->cl_list
= ts
.cl
;
2434 ts
.cl
->length
= gfc_int_expr (1);
2437 /* Record the Successful match. */
2438 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2443 gfc_current_locus
= cur_loc
;
2446 /* Discard the (incorrectly) matched range. */
2447 gfc_clear_new_implicit ();
2449 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2450 if (ts
.type
== BT_CHARACTER
)
2451 m
= match_char_spec (&ts
);
2454 m
= gfc_match_kind_spec (&ts
, false);
2457 m
= gfc_match_old_kind_spec (&ts
);
2458 if (m
== MATCH_ERROR
)
2464 if (m
== MATCH_ERROR
)
2467 m
= match_implicit_range ();
2468 if (m
== MATCH_ERROR
)
2473 gfc_gobble_whitespace ();
2474 c
= gfc_next_char ();
2475 if ((c
!= '\n') && (c
!= ','))
2478 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2486 gfc_syntax_error (ST_IMPLICIT
);
2494 gfc_match_import (void)
2496 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2501 if (gfc_current_ns
->proc_name
== NULL
2502 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2504 gfc_error ("IMPORT statement at %C only permitted in "
2505 "an INTERFACE body");
2509 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2513 if (gfc_match_eos () == MATCH_YES
)
2515 /* All host variables should be imported. */
2516 gfc_current_ns
->has_import_set
= 1;
2520 if (gfc_match (" ::") == MATCH_YES
)
2522 if (gfc_match_eos () == MATCH_YES
)
2524 gfc_error ("Expecting list of named entities at %C");
2531 m
= gfc_match (" %n", name
);
2535 if (gfc_current_ns
->parent
!= NULL
2536 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2538 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2541 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2542 && gfc_find_symbol (name
,
2543 gfc_current_ns
->proc_name
->ns
->parent
,
2546 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2552 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2553 "at %C - does not exist.", name
);
2557 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
2559 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2564 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
2567 sym
->attr
.imported
= 1;
2579 if (gfc_match_eos () == MATCH_YES
)
2581 if (gfc_match_char (',') != MATCH_YES
)
2588 gfc_error ("Syntax error in IMPORT statement at %C");
2593 /* A minimal implementation of gfc_match without whitespace, escape
2594 characters or variable arguments. Returns true if the next
2595 characters match the TARGET template exactly. */
2598 match_string_p (const char *target
)
2602 for (p
= target
; *p
; p
++)
2603 if (gfc_next_char () != *p
)
2608 /* Matches an attribute specification including array specs. If
2609 successful, leaves the variables current_attr and current_as
2610 holding the specification. Also sets the colon_seen variable for
2611 later use by matchers associated with initializations.
2613 This subroutine is a little tricky in the sense that we don't know
2614 if we really have an attr-spec until we hit the double colon.
2615 Until that time, we can only return MATCH_NO. This forces us to
2616 check for duplicate specification at this level. */
2619 match_attr_spec (void)
2621 /* Modifiers that can exist in a type statement. */
2623 { GFC_DECL_BEGIN
= 0,
2624 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
2625 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
2626 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
2627 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
2628 DECL_IS_BIND_C
, DECL_NONE
,
2629 GFC_DECL_END
/* Sentinel */
2633 /* GFC_DECL_END is the sentinel, index starts at 0. */
2634 #define NUM_DECL GFC_DECL_END
2636 locus start
, seen_at
[NUM_DECL
];
2643 gfc_clear_attr (¤t_attr
);
2644 start
= gfc_current_locus
;
2649 /* See if we get all of the keywords up to the final double colon. */
2650 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2658 gfc_gobble_whitespace ();
2660 ch
= gfc_next_char ();
2663 /* This is the successful exit condition for the loop. */
2664 if (gfc_next_char () == ':')
2669 gfc_gobble_whitespace ();
2670 switch (gfc_peek_char ())
2673 if (match_string_p ("allocatable"))
2674 d
= DECL_ALLOCATABLE
;
2678 /* Try and match the bind(c). */
2679 m
= gfc_match_bind_c (NULL
);
2682 else if (m
== MATCH_ERROR
)
2687 if (match_string_p ("dimension"))
2692 if (match_string_p ("external"))
2697 if (match_string_p ("int"))
2699 ch
= gfc_next_char ();
2702 if (match_string_p ("nt"))
2704 /* Matched "intent". */
2705 /* TODO: Call match_intent_spec from here. */
2706 if (gfc_match (" ( in out )") == MATCH_YES
)
2708 else if (gfc_match (" ( in )") == MATCH_YES
)
2710 else if (gfc_match (" ( out )") == MATCH_YES
)
2716 if (match_string_p ("insic"))
2718 /* Matched "intrinsic". */
2726 if (match_string_p ("optional"))
2732 switch (gfc_next_char ())
2735 if (match_string_p ("rameter"))
2737 /* Matched "parameter". */
2743 if (match_string_p ("inter"))
2745 /* Matched "pointer". */
2751 ch
= gfc_next_char ();
2754 if (match_string_p ("vate"))
2756 /* Matched "private". */
2762 if (match_string_p ("tected"))
2764 /* Matched "protected". */
2771 if (match_string_p ("blic"))
2773 /* Matched "public". */
2781 if (match_string_p ("save"))
2786 if (match_string_p ("target"))
2792 ch
= gfc_next_char ();
2795 if (match_string_p ("lue"))
2797 /* Matched "value". */
2803 if (match_string_p ("latile"))
2805 /* Matched "volatile". */
2813 /* No double colon and no recognizable decl_type, so assume that
2814 we've been looking at something else the whole time. */
2822 seen_at
[d
] = gfc_current_locus
;
2824 if (d
== DECL_DIMENSION
)
2826 m
= gfc_match_array_spec (¤t_as
);
2830 gfc_error ("Missing dimension specification at %C");
2834 if (m
== MATCH_ERROR
)
2839 /* Since we've seen a double colon, we have to be looking at an
2840 attr-spec. This means that we can now issue errors. */
2841 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2846 case DECL_ALLOCATABLE
:
2847 attr
= "ALLOCATABLE";
2849 case DECL_DIMENSION
:
2856 attr
= "INTENT (IN)";
2859 attr
= "INTENT (OUT)";
2862 attr
= "INTENT (IN OUT)";
2864 case DECL_INTRINSIC
:
2870 case DECL_PARAMETER
:
2876 case DECL_PROTECTED
:
2891 case DECL_IS_BIND_C
:
2901 attr
= NULL
; /* This shouldn't happen. */
2904 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
2909 /* Now that we've dealt with duplicate attributes, add the attributes
2910 to the current attribute. */
2911 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2916 if (gfc_current_state () == COMP_DERIVED
2917 && d
!= DECL_DIMENSION
&& d
!= DECL_POINTER
2918 && d
!= DECL_PRIVATE
&& d
!= DECL_PUBLIC
2921 if (d
== DECL_ALLOCATABLE
)
2923 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
2924 "attribute at %C in a TYPE definition")
2933 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2940 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
2941 && gfc_current_state () != COMP_MODULE
)
2943 if (d
== DECL_PRIVATE
)
2947 if (gfc_current_state () == COMP_DERIVED
2948 && gfc_state_stack
->previous
2949 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
2951 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
2952 "at %L in a TYPE definition", attr
,
2962 gfc_error ("%s attribute at %L is not allowed outside of the "
2963 "specification part of a module", attr
, &seen_at
[d
]);
2971 case DECL_ALLOCATABLE
:
2972 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
2975 case DECL_DIMENSION
:
2976 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
2980 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
2984 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
2988 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
2992 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
2995 case DECL_INTRINSIC
:
2996 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3000 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3003 case DECL_PARAMETER
:
3004 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3008 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3011 case DECL_PROTECTED
:
3012 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3014 gfc_error ("PROTECTED at %C only allowed in specification "
3015 "part of a module");
3020 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3025 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3029 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3034 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3039 t
= gfc_add_save (¤t_attr
, NULL
, &seen_at
[d
]);
3043 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3046 case DECL_IS_BIND_C
:
3047 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3051 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3056 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3060 if (gfc_notify_std (GFC_STD_F2003
,
3061 "Fortran 2003: VOLATILE attribute at %C")
3065 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3069 gfc_internal_error ("match_attr_spec(): Bad attribute");
3083 gfc_current_locus
= start
;
3084 gfc_free_array_spec (current_as
);
3090 /* Set the binding label, dest_label, either with the binding label
3091 stored in the given gfc_typespec, ts, or if none was provided, it
3092 will be the symbol name in all lower case, as required by the draft
3093 (J3/04-007, section 15.4.1). If a binding label was given and
3094 there is more than one argument (num_idents), it is an error. */
3097 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3099 if (num_idents
> 1 && has_name_equals
)
3101 gfc_error ("Multiple identifiers provided with "
3102 "single NAME= specifier at %C");
3106 if (curr_binding_label
[0] != '\0')
3108 /* Binding label given; store in temp holder til have sym. */
3109 strncpy (dest_label
, curr_binding_label
,
3110 strlen (curr_binding_label
) + 1);
3114 /* No binding label given, and the NAME= specifier did not exist,
3115 which means there was no NAME="". */
3116 if (sym_name
!= NULL
&& has_name_equals
== 0)
3117 strncpy (dest_label
, sym_name
, strlen (sym_name
) + 1);
3124 /* Set the status of the given common block as being BIND(C) or not,
3125 depending on the given parameter, is_bind_c. */
3128 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3130 com_block
->is_bind_c
= is_bind_c
;
3135 /* Verify that the given gfc_typespec is for a C interoperable type. */
3138 verify_c_interop (gfc_typespec
*ts
, const char *name
, locus
*where
)
3142 /* Make sure the kind used is appropriate for the type.
3143 The f90_type is unknown if an integer constant was
3144 used (e.g., real(4), bind(c) :: myFloat). */
3145 if (ts
->f90_type
!= BT_UNKNOWN
)
3147 t
= gfc_validate_c_kind (ts
);
3150 /* Print an error, but continue parsing line. */
3151 gfc_error_now ("C kind parameter is for type %s but "
3152 "symbol '%s' at %L is of type %s",
3153 gfc_basic_typename (ts
->f90_type
),
3155 gfc_basic_typename (ts
->type
));
3159 /* Make sure the kind is C interoperable. This does not care about the
3160 possible error above. */
3161 if (ts
->type
== BT_DERIVED
&& ts
->derived
!= NULL
)
3162 return (ts
->derived
->ts
.is_c_interop
? SUCCESS
: FAILURE
);
3163 else if (ts
->is_c_interop
!= 1)
3170 /* Verify that the variables of a given common block, which has been
3171 defined with the attribute specifier bind(c), to be of a C
3172 interoperable type. Errors will be reported here, if
3176 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3178 gfc_symbol
*curr_sym
= NULL
;
3179 try retval
= SUCCESS
;
3181 curr_sym
= com_block
->head
;
3183 /* Make sure we have at least one symbol. */
3184 if (curr_sym
== NULL
)
3187 /* Here we know we have a symbol, so we'll execute this loop
3191 /* The second to last param, 1, says this is in a common block. */
3192 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3193 curr_sym
= curr_sym
->common_next
;
3194 } while (curr_sym
!= NULL
);
3200 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3201 an appropriate error message is reported. */
3204 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3205 int is_in_common
, gfc_common_head
*com_block
)
3207 try retval
= SUCCESS
;
3209 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3211 tmp_sym
= tmp_sym
->result
;
3212 /* Make sure it wasn't an implicitly typed result. */
3213 if (tmp_sym
->attr
.implicit_type
)
3215 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3216 "%L may not be C interoperable", tmp_sym
->name
,
3217 &tmp_sym
->declared_at
);
3218 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3219 /* Mark it as C interoperable to prevent duplicate warnings. */
3220 tmp_sym
->ts
.is_c_interop
= 1;
3221 tmp_sym
->attr
.is_c_interop
= 1;
3225 /* Here, we know we have the bind(c) attribute, so if we have
3226 enough type info, then verify that it's a C interop kind.
3227 The info could be in the symbol already, or possibly still in
3228 the given ts (current_ts), so look in both. */
3229 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3231 if (verify_c_interop (&(tmp_sym
->ts
), tmp_sym
->name
,
3232 &(tmp_sym
->declared_at
)) != SUCCESS
)
3234 /* See if we're dealing with a sym in a common block or not. */
3235 if (is_in_common
== 1)
3237 gfc_warning ("Variable '%s' in common block '%s' at %L "
3238 "may not be a C interoperable "
3239 "kind though common block '%s' is BIND(C)",
3240 tmp_sym
->name
, com_block
->name
,
3241 &(tmp_sym
->declared_at
), com_block
->name
);
3245 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3246 gfc_error ("Type declaration '%s' at %L is not C "
3247 "interoperable but it is BIND(C)",
3248 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3250 gfc_warning ("Variable '%s' at %L "
3251 "may not be a C interoperable "
3252 "kind but it is bind(c)",
3253 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3257 /* Variables declared w/in a common block can't be bind(c)
3258 since there's no way for C to see these variables, so there's
3259 semantically no reason for the attribute. */
3260 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3262 gfc_error ("Variable '%s' in common block '%s' at "
3263 "%L cannot be declared with BIND(C) "
3264 "since it is not a global",
3265 tmp_sym
->name
, com_block
->name
,
3266 &(tmp_sym
->declared_at
));
3270 /* Scalar variables that are bind(c) can not have the pointer
3271 or allocatable attributes. */
3272 if (tmp_sym
->attr
.is_bind_c
== 1)
3274 if (tmp_sym
->attr
.pointer
== 1)
3276 gfc_error ("Variable '%s' at %L cannot have both the "
3277 "POINTER and BIND(C) attributes",
3278 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3282 if (tmp_sym
->attr
.allocatable
== 1)
3284 gfc_error ("Variable '%s' at %L cannot have both the "
3285 "ALLOCATABLE and BIND(C) attributes",
3286 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3290 /* If it is a BIND(C) function, make sure the return value is a
3291 scalar value. The previous tests in this function made sure
3292 the type is interoperable. */
3293 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->as
!= NULL
)
3294 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3295 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3297 /* BIND(C) functions can not return a character string. */
3298 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->ts
.type
== BT_CHARACTER
)
3299 if (tmp_sym
->ts
.cl
== NULL
|| tmp_sym
->ts
.cl
->length
== NULL
3300 || tmp_sym
->ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
3301 || mpz_cmp_si (tmp_sym
->ts
.cl
->length
->value
.integer
, 1) != 0)
3302 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3303 "be a character string", tmp_sym
->name
,
3304 &(tmp_sym
->declared_at
));
3308 /* See if the symbol has been marked as private. If it has, make sure
3309 there is no binding label and warn the user if there is one. */
3310 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3311 && tmp_sym
->binding_label
[0] != '\0')
3312 /* Use gfc_warning_now because we won't say that the symbol fails
3313 just because of this. */
3314 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3315 "given the binding label '%s'", tmp_sym
->name
,
3316 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3322 /* Set the appropriate fields for a symbol that's been declared as
3323 BIND(C) (the is_bind_c flag and the binding label), and verify that
3324 the type is C interoperable. Errors are reported by the functions
3325 used to set/test these fields. */
3328 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3330 try retval
= SUCCESS
;
3332 /* TODO: Do we need to make sure the vars aren't marked private? */
3334 /* Set the is_bind_c bit in symbol_attribute. */
3335 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3337 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3338 num_idents
) != SUCCESS
)
3345 /* Set the fields marking the given common block as BIND(C), including
3346 a binding label, and report any errors encountered. */
3349 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3351 try retval
= SUCCESS
;
3353 /* destLabel, common name, typespec (which may have binding label). */
3354 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3358 /* Set the given common block (com_block) to being bind(c) (1). */
3359 set_com_block_bind_c (com_block
, 1);
3365 /* Retrieve the list of one or more identifiers that the given bind(c)
3366 attribute applies to. */
3369 get_bind_c_idents (void)
3371 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3373 gfc_symbol
*tmp_sym
= NULL
;
3375 gfc_common_head
*com_block
= NULL
;
3377 if (gfc_match_name (name
) == MATCH_YES
)
3379 found_id
= MATCH_YES
;
3380 gfc_get_ha_symbol (name
, &tmp_sym
);
3382 else if (match_common_name (name
) == MATCH_YES
)
3384 found_id
= MATCH_YES
;
3385 com_block
= gfc_get_common (name
, 0);
3389 gfc_error ("Need either entity or common block name for "
3390 "attribute specification statement at %C");
3394 /* Save the current identifier and look for more. */
3397 /* Increment the number of identifiers found for this spec stmt. */
3400 /* Make sure we have a sym or com block, and verify that it can
3401 be bind(c). Set the appropriate field(s) and look for more
3403 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3405 if (tmp_sym
!= NULL
)
3407 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3413 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3418 /* Look to see if we have another identifier. */
3420 if (gfc_match_eos () == MATCH_YES
)
3421 found_id
= MATCH_NO
;
3422 else if (gfc_match_char (',') != MATCH_YES
)
3423 found_id
= MATCH_NO
;
3424 else if (gfc_match_name (name
) == MATCH_YES
)
3426 found_id
= MATCH_YES
;
3427 gfc_get_ha_symbol (name
, &tmp_sym
);
3429 else if (match_common_name (name
) == MATCH_YES
)
3431 found_id
= MATCH_YES
;
3432 com_block
= gfc_get_common (name
, 0);
3436 gfc_error ("Missing entity or common block name for "
3437 "attribute specification statement at %C");
3443 gfc_internal_error ("Missing symbol");
3445 } while (found_id
== MATCH_YES
);
3447 /* if we get here we were successful */
3452 /* Try and match a BIND(C) attribute specification statement. */
3455 gfc_match_bind_c_stmt (void)
3457 match found_match
= MATCH_NO
;
3462 /* This may not be necessary. */
3464 /* Clear the temporary binding label holder. */
3465 curr_binding_label
[0] = '\0';
3467 /* Look for the bind(c). */
3468 found_match
= gfc_match_bind_c (NULL
);
3470 if (found_match
== MATCH_YES
)
3472 /* Look for the :: now, but it is not required. */
3475 /* Get the identifier(s) that needs to be updated. This may need to
3476 change to hand the flag(s) for the attr specified so all identifiers
3477 found can have all appropriate parts updated (assuming that the same
3478 spec stmt can have multiple attrs, such as both bind(c) and
3480 if (get_bind_c_idents () != SUCCESS
)
3481 /* Error message should have printed already. */
3489 /* Match a data declaration statement. */
3492 gfc_match_data_decl (void)
3498 num_idents_on_line
= 0;
3500 m
= gfc_match_type_spec (¤t_ts
, 0);
3504 if (current_ts
.type
== BT_DERIVED
&& gfc_current_state () != COMP_DERIVED
)
3506 sym
= gfc_use_derived (current_ts
.derived
);
3514 current_ts
.derived
= sym
;
3517 m
= match_attr_spec ();
3518 if (m
== MATCH_ERROR
)
3524 if (current_ts
.type
== BT_DERIVED
&& current_ts
.derived
->components
== NULL
3525 && !current_ts
.derived
->attr
.zero_comp
)
3528 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
3531 gfc_find_symbol (current_ts
.derived
->name
,
3532 current_ts
.derived
->ns
->parent
, 1, &sym
);
3534 /* Any symbol that we find had better be a type definition
3535 which has its components defined. */
3536 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
3537 && (current_ts
.derived
->components
!= NULL
3538 || current_ts
.derived
->attr
.zero_comp
))
3541 /* Now we have an error, which we signal, and then fix up
3542 because the knock-on is plain and simple confusing. */
3543 gfc_error_now ("Derived type at %C has not been previously defined "
3544 "and so cannot appear in a derived type definition");
3545 current_attr
.pointer
= 1;
3550 /* If we have an old-style character declaration, and no new-style
3551 attribute specifications, then there a comma is optional between
3552 the type specification and the variable list. */
3553 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
3554 gfc_match_char (',');
3556 /* Give the types/attributes to symbols that follow. Give the element
3557 a number so that repeat character length expressions can be copied. */
3561 num_idents_on_line
++;
3562 m
= variable_decl (elem
++);
3563 if (m
== MATCH_ERROR
)
3568 if (gfc_match_eos () == MATCH_YES
)
3570 if (gfc_match_char (',') != MATCH_YES
)
3574 if (gfc_error_flag_test () == 0)
3575 gfc_error ("Syntax error in data declaration at %C");
3578 gfc_free_data_all (gfc_current_ns
);
3581 gfc_free_array_spec (current_as
);
3587 /* Match a prefix associated with a function or subroutine
3588 declaration. If the typespec pointer is nonnull, then a typespec
3589 can be matched. Note that if nothing matches, MATCH_YES is
3590 returned (the null string was matched). */
3593 match_prefix (gfc_typespec
*ts
)
3597 gfc_clear_attr (¤t_attr
);
3601 if (!seen_type
&& ts
!= NULL
3602 && gfc_match_type_spec (ts
, 0) == MATCH_YES
3603 && gfc_match_space () == MATCH_YES
)
3610 if (gfc_match ("elemental% ") == MATCH_YES
)
3612 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
3618 if (gfc_match ("pure% ") == MATCH_YES
)
3620 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
3626 if (gfc_match ("recursive% ") == MATCH_YES
)
3628 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
3634 /* At this point, the next item is not a prefix. */
3639 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3642 copy_prefix (symbol_attribute
*dest
, locus
*where
)
3644 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
3647 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
3650 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
3657 /* Match a formal argument list. */
3660 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
3662 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
3663 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3669 if (gfc_match_char ('(') != MATCH_YES
)
3676 if (gfc_match_char (')') == MATCH_YES
)
3681 if (gfc_match_char ('*') == MATCH_YES
)
3685 m
= gfc_match_name (name
);
3689 if (gfc_get_symbol (name
, NULL
, &sym
))
3693 p
= gfc_get_formal_arglist ();
3705 /* We don't add the VARIABLE flavor because the name could be a
3706 dummy procedure. We don't apply these attributes to formal
3707 arguments of statement functions. */
3708 if (sym
!= NULL
&& !st_flag
3709 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
3710 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
3716 /* The name of a program unit can be in a different namespace,
3717 so check for it explicitly. After the statement is accepted,
3718 the name is checked for especially in gfc_get_symbol(). */
3719 if (gfc_new_block
!= NULL
&& sym
!= NULL
3720 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
3722 gfc_error ("Name '%s' at %C is the name of the procedure",
3728 if (gfc_match_char (')') == MATCH_YES
)
3731 m
= gfc_match_char (',');
3734 gfc_error ("Unexpected junk in formal argument list at %C");
3740 /* Check for duplicate symbols in the formal argument list. */
3743 for (p
= head
; p
->next
; p
= p
->next
)
3748 for (q
= p
->next
; q
; q
= q
->next
)
3749 if (p
->sym
== q
->sym
)
3751 gfc_error ("Duplicate symbol '%s' in formal argument list "
3752 "at %C", p
->sym
->name
);
3760 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
3770 gfc_free_formal_arglist (head
);
3775 /* Match a RESULT specification following a function declaration or
3776 ENTRY statement. Also matches the end-of-statement. */
3779 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
3781 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3785 if (gfc_match (" result (") != MATCH_YES
)
3788 m
= gfc_match_name (name
);
3792 /* Get the right paren, and that's it because there could be the
3793 bind(c) attribute after the result clause. */
3794 if (gfc_match_char(')') != MATCH_YES
)
3796 /* TODO: should report the missing right paren here. */
3800 if (strcmp (function
->name
, name
) == 0)
3802 gfc_error ("RESULT variable at %C must be different than function name");
3806 if (gfc_get_symbol (name
, NULL
, &r
))
3809 if (gfc_add_flavor (&r
->attr
, FL_VARIABLE
, r
->name
, NULL
) == FAILURE
3810 || gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
3819 /* Match a function suffix, which could be a combination of a result
3820 clause and BIND(C), either one, or neither. The draft does not
3821 require them to come in a specific order. */
3824 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
3826 match is_bind_c
; /* Found bind(c). */
3827 match is_result
; /* Found result clause. */
3828 match found_match
; /* Status of whether we've found a good match. */
3829 int peek_char
; /* Character we're going to peek at. */
3831 /* Initialize to having found nothing. */
3832 found_match
= MATCH_NO
;
3833 is_bind_c
= MATCH_NO
;
3834 is_result
= MATCH_NO
;
3836 /* Get the next char to narrow between result and bind(c). */
3837 gfc_gobble_whitespace ();
3838 peek_char
= gfc_peek_char ();
3843 /* Look for result clause. */
3844 is_result
= match_result (sym
, result
);
3845 if (is_result
== MATCH_YES
)
3847 /* Now see if there is a bind(c) after it. */
3848 is_bind_c
= gfc_match_bind_c (sym
);
3849 /* We've found the result clause and possibly bind(c). */
3850 found_match
= MATCH_YES
;
3853 /* This should only be MATCH_ERROR. */
3854 found_match
= is_result
;
3857 /* Look for bind(c) first. */
3858 is_bind_c
= gfc_match_bind_c (sym
);
3859 if (is_bind_c
== MATCH_YES
)
3861 /* Now see if a result clause followed it. */
3862 is_result
= match_result (sym
, result
);
3863 found_match
= MATCH_YES
;
3867 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3868 found_match
= MATCH_ERROR
;
3872 gfc_error ("Unexpected junk after function declaration at %C");
3873 found_match
= MATCH_ERROR
;
3877 if (is_bind_c
== MATCH_YES
)
3878 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
3886 /* Match a PROCEDURE declaration (R1211). */
3889 match_procedure_decl (void)
3892 locus old_loc
, entry_loc
;
3893 gfc_symbol
*sym
, *proc_if
= NULL
;
3896 old_loc
= entry_loc
= gfc_current_locus
;
3898 gfc_clear_ts (¤t_ts
);
3900 if (gfc_match (" (") != MATCH_YES
)
3902 gfc_current_locus
= entry_loc
;
3906 /* Get the type spec. for the procedure interface. */
3907 old_loc
= gfc_current_locus
;
3908 m
= gfc_match_type_spec (¤t_ts
, 0);
3909 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_char () == ')'))
3912 if (m
== MATCH_ERROR
)
3915 gfc_current_locus
= old_loc
;
3917 /* Get the name of the procedure or abstract interface
3918 to inherit the interface from. */
3919 m
= gfc_match_symbol (&proc_if
, 1);
3923 else if (m
== MATCH_ERROR
)
3926 /* Various interface checks. */
3929 if (proc_if
->generic
)
3931 gfc_error ("Interface '%s' at %C may not be generic", proc_if
->name
);
3934 if (proc_if
->attr
.proc
== PROC_ST_FUNCTION
)
3936 gfc_error ("Interface '%s' at %C may not be a statement function",
3940 /* Handle intrinsic procedures. */
3941 if (gfc_intrinsic_name (proc_if
->name
, 0)
3942 || gfc_intrinsic_name (proc_if
->name
, 1))
3943 proc_if
->attr
.intrinsic
= 1;
3944 if (proc_if
->attr
.intrinsic
3945 && !gfc_intrinsic_actual_ok (proc_if
->name
, 0))
3947 gfc_error ("Intrinsic procedure '%s' not allowed "
3948 "in PROCEDURE statement at %C", proc_if
->name
);
3951 /* TODO: Allow intrinsics with gfc_intrinsic_actual_ok
3952 (proc_if->name, 0) after PR33162 is fixed. */
3953 if (proc_if
->attr
.intrinsic
)
3955 gfc_error ("Fortran 2003: Support for intrinsic procedure '%s' "
3956 "in PROCEDURE statement at %C not yet implemented "
3957 "in gfortran", proc_if
->name
);
3964 if (gfc_match (" )") != MATCH_YES
)
3966 gfc_current_locus
= entry_loc
;
3970 /* Parse attributes. */
3971 m
= match_attr_spec();
3972 if (m
== MATCH_ERROR
)
3975 /* Get procedure symbols. */
3979 m
= gfc_match_symbol (&sym
, 0);
3982 else if (m
== MATCH_ERROR
)
3985 /* Add current_attr to the symbol attributes. */
3986 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
3989 if (sym
->attr
.is_bind_c
)
3991 /* Check for C1218. */
3992 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
3994 gfc_error ("BIND(C) attribute at %C requires "
3995 "an interface with BIND(C)");
3998 /* Check for C1217. */
3999 if (has_name_equals
&& sym
->attr
.pointer
)
4001 gfc_error ("BIND(C) procedure with NAME may not have "
4002 "POINTER attribute at %C");
4005 if (has_name_equals
&& sym
->attr
.dummy
)
4007 gfc_error ("Dummy procedure at %C may not have "
4008 "BIND(C) attribute with NAME");
4011 /* Set binding label for BIND(C). */
4012 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4016 if (!sym
->attr
.pointer
&& gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4018 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4021 /* Set interface. */
4022 if (proc_if
!= NULL
)
4023 sym
->interface
= proc_if
;
4024 else if (current_ts
.type
!= BT_UNKNOWN
)
4026 sym
->interface
= gfc_new_symbol ("", gfc_current_ns
);
4027 sym
->interface
->ts
= current_ts
;
4028 sym
->interface
->attr
.function
= 1;
4029 sym
->ts
= sym
->interface
->ts
;
4030 sym
->attr
.function
= sym
->interface
->attr
.function
;
4033 if (gfc_match_eos () == MATCH_YES
)
4035 if (gfc_match_char (',') != MATCH_YES
)
4040 gfc_error ("Syntax error in PROCEDURE statement at %C");
4045 /* Match a PROCEDURE declaration inside an interface (R1206). */
4048 match_procedure_in_interface (void)
4052 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4054 if (current_interface
.type
== INTERFACE_NAMELESS
4055 || current_interface
.type
== INTERFACE_ABSTRACT
)
4057 gfc_error ("PROCEDURE at %C must be in a generic interface");
4063 m
= gfc_match_name (name
);
4066 else if (m
== MATCH_ERROR
)
4068 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4071 if (gfc_add_interface (sym
) == FAILURE
)
4074 sym
->attr
.procedure
= 1;
4076 if (gfc_match_eos () == MATCH_YES
)
4078 if (gfc_match_char (',') != MATCH_YES
)
4085 gfc_error ("Syntax error in PROCEDURE statement at %C");
4090 /* General matcher for PROCEDURE declarations. */
4093 gfc_match_procedure (void)
4097 switch (gfc_current_state ())
4102 case COMP_SUBROUTINE
:
4104 m
= match_procedure_decl ();
4106 case COMP_INTERFACE
:
4107 m
= match_procedure_in_interface ();
4110 gfc_error ("Fortran 2003: Procedure components at %C are "
4111 "not yet implemented in gfortran");
4120 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4128 /* Match a function declaration. */
4131 gfc_match_function_decl (void)
4133 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4134 gfc_symbol
*sym
, *result
;
4138 match found_match
; /* Status returned by match func. */
4140 if (gfc_current_state () != COMP_NONE
4141 && gfc_current_state () != COMP_INTERFACE
4142 && gfc_current_state () != COMP_CONTAINS
)
4145 gfc_clear_ts (¤t_ts
);
4147 old_loc
= gfc_current_locus
;
4149 m
= match_prefix (¤t_ts
);
4152 gfc_current_locus
= old_loc
;
4156 if (gfc_match ("function% %n", name
) != MATCH_YES
)
4158 gfc_current_locus
= old_loc
;
4161 if (get_proc_name (name
, &sym
, false))
4163 gfc_new_block
= sym
;
4165 m
= gfc_match_formal_arglist (sym
, 0, 0);
4168 gfc_error ("Expected formal argument list in function "
4169 "definition at %C");
4173 else if (m
== MATCH_ERROR
)
4178 /* According to the draft, the bind(c) and result clause can
4179 come in either order after the formal_arg_list (i.e., either
4180 can be first, both can exist together or by themselves or neither
4181 one). Therefore, the match_result can't match the end of the
4182 string, and check for the bind(c) or result clause in either order. */
4183 found_match
= gfc_match_eos ();
4185 /* Make sure that it isn't already declared as BIND(C). If it is, it
4186 must have been marked BIND(C) with a BIND(C) attribute and that is
4187 not allowed for procedures. */
4188 if (sym
->attr
.is_bind_c
== 1)
4190 sym
->attr
.is_bind_c
= 0;
4191 if (sym
->old_symbol
!= NULL
)
4192 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4193 "variables or common blocks",
4194 &(sym
->old_symbol
->declared_at
));
4196 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4197 "variables or common blocks", &gfc_current_locus
);
4200 if (found_match
!= MATCH_YES
)
4202 /* If we haven't found the end-of-statement, look for a suffix. */
4203 suffix_match
= gfc_match_suffix (sym
, &result
);
4204 if (suffix_match
== MATCH_YES
)
4205 /* Need to get the eos now. */
4206 found_match
= gfc_match_eos ();
4208 found_match
= suffix_match
;
4211 if(found_match
!= MATCH_YES
)
4215 /* Make changes to the symbol. */
4218 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4221 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
4222 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4225 if (current_ts
.type
!= BT_UNKNOWN
&& sym
->ts
.type
!= BT_UNKNOWN
4226 && !sym
->attr
.implicit_type
)
4228 gfc_error ("Function '%s' at %C already has a type of %s", name
,
4229 gfc_basic_typename (sym
->ts
.type
));
4235 sym
->ts
= current_ts
;
4240 result
->ts
= current_ts
;
4241 sym
->result
= result
;
4248 gfc_current_locus
= old_loc
;
4253 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4254 pass the name of the entry, rather than the gfc_current_block name, and
4255 to return false upon finding an existing global entry. */
4258 add_global_entry (const char *name
, int sub
)
4262 s
= gfc_get_gsymbol(name
);
4265 || (s
->type
!= GSYM_UNKNOWN
4266 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
4267 gfc_global_used(s
, NULL
);
4270 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4271 s
->where
= gfc_current_locus
;
4279 /* Match an ENTRY statement. */
4282 gfc_match_entry (void)
4287 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4288 gfc_compile_state state
;
4292 bool module_procedure
;
4294 m
= gfc_match_name (name
);
4298 state
= gfc_current_state ();
4299 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
4304 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4307 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4309 case COMP_BLOCK_DATA
:
4310 gfc_error ("ENTRY statement at %C cannot appear within "
4313 case COMP_INTERFACE
:
4314 gfc_error ("ENTRY statement at %C cannot appear within "
4318 gfc_error ("ENTRY statement at %C cannot appear within "
4319 "a DERIVED TYPE block");
4322 gfc_error ("ENTRY statement at %C cannot appear within "
4323 "an IF-THEN block");
4326 gfc_error ("ENTRY statement at %C cannot appear within "
4330 gfc_error ("ENTRY statement at %C cannot appear within "
4334 gfc_error ("ENTRY statement at %C cannot appear within "
4338 gfc_error ("ENTRY statement at %C cannot appear within "
4342 gfc_error ("ENTRY statement at %C cannot appear within "
4343 "a contained subprogram");
4346 gfc_internal_error ("gfc_match_entry(): Bad state");
4351 module_procedure
= gfc_current_ns
->parent
!= NULL
4352 && gfc_current_ns
->parent
->proc_name
4353 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
4356 if (gfc_current_ns
->parent
!= NULL
4357 && gfc_current_ns
->parent
->proc_name
4358 && !module_procedure
)
4360 gfc_error("ENTRY statement at %C cannot appear in a "
4361 "contained procedure");
4365 /* Module function entries need special care in get_proc_name
4366 because previous references within the function will have
4367 created symbols attached to the current namespace. */
4368 if (get_proc_name (name
, &entry
,
4369 gfc_current_ns
->parent
!= NULL
4371 && gfc_current_ns
->proc_name
->attr
.function
))
4374 proc
= gfc_current_block ();
4376 if (state
== COMP_SUBROUTINE
)
4378 /* An entry in a subroutine. */
4379 if (!add_global_entry (name
, 1))
4382 m
= gfc_match_formal_arglist (entry
, 0, 1);
4386 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
4387 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
4392 /* An entry in a function.
4393 We need to take special care because writing
4398 ENTRY f() RESULT (r)
4400 ENTRY f RESULT (r). */
4401 if (!add_global_entry (name
, 0))
4404 old_loc
= gfc_current_locus
;
4405 if (gfc_match_eos () == MATCH_YES
)
4407 gfc_current_locus
= old_loc
;
4408 /* Match the empty argument list, and add the interface to
4410 m
= gfc_match_formal_arglist (entry
, 0, 1);
4413 m
= gfc_match_formal_arglist (entry
, 0, 0);
4420 if (gfc_match_eos () == MATCH_YES
)
4422 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
4423 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
4426 entry
->result
= entry
;
4430 m
= match_result (proc
, &result
);
4432 gfc_syntax_error (ST_ENTRY
);
4436 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
4437 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
4438 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
4442 entry
->result
= result
;
4446 if (gfc_match_eos () != MATCH_YES
)
4448 gfc_syntax_error (ST_ENTRY
);
4452 entry
->attr
.recursive
= proc
->attr
.recursive
;
4453 entry
->attr
.elemental
= proc
->attr
.elemental
;
4454 entry
->attr
.pure
= proc
->attr
.pure
;
4456 el
= gfc_get_entry_list ();
4458 el
->next
= gfc_current_ns
->entries
;
4459 gfc_current_ns
->entries
= el
;
4461 el
->id
= el
->next
->id
+ 1;
4465 new_st
.op
= EXEC_ENTRY
;
4466 new_st
.ext
.entry
= el
;
4472 /* Match a subroutine statement, including optional prefixes. */
4475 gfc_match_subroutine (void)
4477 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4483 if (gfc_current_state () != COMP_NONE
4484 && gfc_current_state () != COMP_INTERFACE
4485 && gfc_current_state () != COMP_CONTAINS
)
4488 m
= match_prefix (NULL
);
4492 m
= gfc_match ("subroutine% %n", name
);
4496 if (get_proc_name (name
, &sym
, false))
4498 gfc_new_block
= sym
;
4500 /* Check what next non-whitespace character is so we can tell if there
4501 where the required parens if we have a BIND(C). */
4502 gfc_gobble_whitespace ();
4503 peek_char
= gfc_peek_char ();
4505 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4508 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
4511 /* Make sure that it isn't already declared as BIND(C). If it is, it
4512 must have been marked BIND(C) with a BIND(C) attribute and that is
4513 not allowed for procedures. */
4514 if (sym
->attr
.is_bind_c
== 1)
4516 sym
->attr
.is_bind_c
= 0;
4517 if (sym
->old_symbol
!= NULL
)
4518 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4519 "variables or common blocks",
4520 &(sym
->old_symbol
->declared_at
));
4522 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4523 "variables or common blocks", &gfc_current_locus
);
4526 /* Here, we are just checking if it has the bind(c) attribute, and if
4527 so, then we need to make sure it's all correct. If it doesn't,
4528 we still need to continue matching the rest of the subroutine line. */
4529 is_bind_c
= gfc_match_bind_c (sym
);
4530 if (is_bind_c
== MATCH_ERROR
)
4532 /* There was an attempt at the bind(c), but it was wrong. An
4533 error message should have been printed w/in the gfc_match_bind_c
4534 so here we'll just return the MATCH_ERROR. */
4538 if (is_bind_c
== MATCH_YES
)
4540 if (peek_char
!= '(')
4542 gfc_error ("Missing required parentheses before BIND(C) at %C");
4545 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
4550 if (gfc_match_eos () != MATCH_YES
)
4552 gfc_syntax_error (ST_SUBROUTINE
);
4556 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4563 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4564 given, and set the binding label in either the given symbol (if not
4565 NULL), or in the current_ts. The symbol may be NULL because we may
4566 encounter the BIND(C) before the declaration itself. Return
4567 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4568 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4569 or MATCH_YES if the specifier was correct and the binding label and
4570 bind(c) fields were set correctly for the given symbol or the
4574 gfc_match_bind_c (gfc_symbol
*sym
)
4576 /* binding label, if exists */
4577 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
4581 /* Initialize the flag that specifies whether we encountered a NAME=
4582 specifier or not. */
4583 has_name_equals
= 0;
4585 /* Init the first char to nil so we can catch if we don't have
4586 the label (name attr) or the symbol name yet. */
4587 binding_label
[0] = '\0';
4589 /* This much we have to be able to match, in this order, if
4590 there is a bind(c) label. */
4591 if (gfc_match (" bind ( c ") != MATCH_YES
)
4594 /* Now see if there is a binding label, or if we've reached the
4595 end of the bind(c) attribute without one. */
4596 if (gfc_match_char (',') == MATCH_YES
)
4598 if (gfc_match (" name = ") != MATCH_YES
)
4600 gfc_error ("Syntax error in NAME= specifier for binding label "
4602 /* should give an error message here */
4606 has_name_equals
= 1;
4608 /* Get the opening quote. */
4609 double_quote
= MATCH_YES
;
4610 single_quote
= MATCH_YES
;
4611 double_quote
= gfc_match_char ('"');
4612 if (double_quote
!= MATCH_YES
)
4613 single_quote
= gfc_match_char ('\'');
4614 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
4616 gfc_error ("Syntax error in NAME= specifier for binding label "
4621 /* Grab the binding label, using functions that will not lower
4622 case the names automatically. */
4623 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
4626 /* Get the closing quotation. */
4627 if (double_quote
== MATCH_YES
)
4629 if (gfc_match_char ('"') != MATCH_YES
)
4631 gfc_error ("Missing closing quote '\"' for binding label at %C");
4632 /* User started string with '"' so looked to match it. */
4638 if (gfc_match_char ('\'') != MATCH_YES
)
4640 gfc_error ("Missing closing quote '\'' for binding label at %C");
4641 /* User started string with "'" char. */
4647 /* Get the required right paren. */
4648 if (gfc_match_char (')') != MATCH_YES
)
4650 gfc_error ("Missing closing paren for binding label at %C");
4654 /* Save the binding label to the symbol. If sym is null, we're
4655 probably matching the typespec attributes of a declaration and
4656 haven't gotten the name yet, and therefore, no symbol yet. */
4657 if (binding_label
[0] != '\0')
4661 strncpy (sym
->binding_label
, binding_label
,
4662 strlen (binding_label
)+1);
4665 strncpy (curr_binding_label
, binding_label
,
4666 strlen (binding_label
) + 1);
4670 /* No binding label, but if symbol isn't null, we
4671 can set the label for it here. */
4672 /* TODO: If the name= was given and no binding label (name=""), we simply
4673 will let fortran mangle the symbol name as it usually would.
4674 However, this could still let C call it if the user looked up the
4675 symbol in the object file. Should the name set during mangling in
4676 trans-decl.c be marked with characters that are invalid for C to
4678 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
4679 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
4682 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
4683 && current_interface
.type
== INTERFACE_ABSTRACT
)
4685 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4693 /* Return nonzero if we're currently compiling a contained procedure. */
4696 contained_procedure (void)
4700 for (s
=gfc_state_stack
; s
; s
=s
->previous
)
4701 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
4702 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
4708 /* Set the kind of each enumerator. The kind is selected such that it is
4709 interoperable with the corresponding C enumeration type, making
4710 sure that -fshort-enums is honored. */
4715 enumerator_history
*current_history
= NULL
;
4719 if (max_enum
== NULL
|| enum_history
== NULL
)
4722 if (!gfc_option
.fshort_enums
)
4728 kind
= gfc_integer_kinds
[i
++].kind
;
4730 while (kind
< gfc_c_int_kind
4731 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
4734 current_history
= enum_history
;
4735 while (current_history
!= NULL
)
4737 current_history
->sym
->ts
.kind
= kind
;
4738 current_history
= current_history
->next
;
4743 /* Match any of the various end-block statements. Returns the type of
4744 END to the caller. The END INTERFACE, END IF, END DO and END
4745 SELECT statements cannot be replaced by a single END statement. */
4748 gfc_match_end (gfc_statement
*st
)
4750 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4751 gfc_compile_state state
;
4753 const char *block_name
;
4758 old_loc
= gfc_current_locus
;
4759 if (gfc_match ("end") != MATCH_YES
)
4762 state
= gfc_current_state ();
4763 block_name
= gfc_current_block () == NULL
4764 ? NULL
: gfc_current_block ()->name
;
4766 if (state
== COMP_CONTAINS
)
4768 state
= gfc_state_stack
->previous
->state
;
4769 block_name
= gfc_state_stack
->previous
->sym
== NULL
4770 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
4777 *st
= ST_END_PROGRAM
;
4778 target
= " program";
4782 case COMP_SUBROUTINE
:
4783 *st
= ST_END_SUBROUTINE
;
4784 target
= " subroutine";
4785 eos_ok
= !contained_procedure ();
4789 *st
= ST_END_FUNCTION
;
4790 target
= " function";
4791 eos_ok
= !contained_procedure ();
4794 case COMP_BLOCK_DATA
:
4795 *st
= ST_END_BLOCK_DATA
;
4796 target
= " block data";
4801 *st
= ST_END_MODULE
;
4806 case COMP_INTERFACE
:
4807 *st
= ST_END_INTERFACE
;
4808 target
= " interface";
4831 *st
= ST_END_SELECT
;
4837 *st
= ST_END_FORALL
;
4852 last_initializer
= NULL
;
4854 gfc_free_enum_history ();
4858 gfc_error ("Unexpected END statement at %C");
4862 if (gfc_match_eos () == MATCH_YES
)
4866 /* We would have required END [something]. */
4867 gfc_error ("%s statement expected at %L",
4868 gfc_ascii_statement (*st
), &old_loc
);
4875 /* Verify that we've got the sort of end-block that we're expecting. */
4876 if (gfc_match (target
) != MATCH_YES
)
4878 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
4882 /* If we're at the end, make sure a block name wasn't required. */
4883 if (gfc_match_eos () == MATCH_YES
)
4886 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
4887 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
)
4890 if (gfc_current_block () == NULL
)
4893 gfc_error ("Expected block name of '%s' in %s statement at %C",
4894 block_name
, gfc_ascii_statement (*st
));
4899 /* END INTERFACE has a special handler for its several possible endings. */
4900 if (*st
== ST_END_INTERFACE
)
4901 return gfc_match_end_interface ();
4903 /* We haven't hit the end of statement, so what is left must be an
4905 m
= gfc_match_space ();
4907 m
= gfc_match_name (name
);
4910 gfc_error ("Expected terminating name at %C");
4914 if (block_name
== NULL
)
4917 if (strcmp (name
, block_name
) != 0)
4919 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
4920 gfc_ascii_statement (*st
));
4924 if (gfc_match_eos () == MATCH_YES
)
4928 gfc_syntax_error (*st
);
4931 gfc_current_locus
= old_loc
;
4937 /***************** Attribute declaration statements ****************/
4939 /* Set the attribute of a single variable. */
4944 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4952 m
= gfc_match_name (name
);
4956 if (find_special (name
, &sym
))
4959 var_locus
= gfc_current_locus
;
4961 /* Deal with possible array specification for certain attributes. */
4962 if (current_attr
.dimension
4963 || current_attr
.allocatable
4964 || current_attr
.pointer
4965 || current_attr
.target
)
4967 m
= gfc_match_array_spec (&as
);
4968 if (m
== MATCH_ERROR
)
4971 if (current_attr
.dimension
&& m
== MATCH_NO
)
4973 gfc_error ("Missing array specification at %L in DIMENSION "
4974 "statement", &var_locus
);
4979 if ((current_attr
.allocatable
|| current_attr
.pointer
)
4980 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
4982 gfc_error ("Array specification must be deferred at %L", &var_locus
);
4988 /* Update symbol table. DIMENSION attribute is set
4989 in gfc_set_array_spec(). */
4990 if (current_attr
.dimension
== 0
4991 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4997 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
5003 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
5005 /* Fix the array spec. */
5006 m
= gfc_mod_pointee_as (sym
->as
);
5007 if (m
== MATCH_ERROR
)
5011 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
5017 if ((current_attr
.external
|| current_attr
.intrinsic
)
5018 && sym
->attr
.flavor
!= FL_PROCEDURE
5019 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
5028 gfc_free_array_spec (as
);
5033 /* Generic attribute declaration subroutine. Used for attributes that
5034 just have a list of names. */
5041 /* Gobble the optional double colon, by simply ignoring the result
5051 if (gfc_match_eos () == MATCH_YES
)
5057 if (gfc_match_char (',') != MATCH_YES
)
5059 gfc_error ("Unexpected character in variable list at %C");
5069 /* This routine matches Cray Pointer declarations of the form:
5070 pointer ( <pointer>, <pointee> )
5072 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5073 The pointer, if already declared, should be an integer. Otherwise, we
5074 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5075 be either a scalar, or an array declaration. No space is allocated for
5076 the pointee. For the statement
5077 pointer (ipt, ar(10))
5078 any subsequent uses of ar will be translated (in C-notation) as
5079 ar(i) => ((<type> *) ipt)(i)
5080 After gimplification, pointee variable will disappear in the code. */
5083 cray_pointer_decl (void)
5087 gfc_symbol
*cptr
; /* Pointer symbol. */
5088 gfc_symbol
*cpte
; /* Pointee symbol. */
5094 if (gfc_match_char ('(') != MATCH_YES
)
5096 gfc_error ("Expected '(' at %C");
5100 /* Match pointer. */
5101 var_locus
= gfc_current_locus
;
5102 gfc_clear_attr (¤t_attr
);
5103 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
5104 current_ts
.type
= BT_INTEGER
;
5105 current_ts
.kind
= gfc_index_integer_kind
;
5107 m
= gfc_match_symbol (&cptr
, 0);
5110 gfc_error ("Expected variable name at %C");
5114 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
5117 gfc_set_sym_referenced (cptr
);
5119 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
5121 cptr
->ts
.type
= BT_INTEGER
;
5122 cptr
->ts
.kind
= gfc_index_integer_kind
;
5124 else if (cptr
->ts
.type
!= BT_INTEGER
)
5126 gfc_error ("Cray pointer at %C must be an integer");
5129 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
5130 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5131 " memory addresses require %d bytes",
5132 cptr
->ts
.kind
, gfc_index_integer_kind
);
5134 if (gfc_match_char (',') != MATCH_YES
)
5136 gfc_error ("Expected \",\" at %C");
5140 /* Match Pointee. */
5141 var_locus
= gfc_current_locus
;
5142 gfc_clear_attr (¤t_attr
);
5143 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
5144 current_ts
.type
= BT_UNKNOWN
;
5145 current_ts
.kind
= 0;
5147 m
= gfc_match_symbol (&cpte
, 0);
5150 gfc_error ("Expected variable name at %C");
5154 /* Check for an optional array spec. */
5155 m
= gfc_match_array_spec (&as
);
5156 if (m
== MATCH_ERROR
)
5158 gfc_free_array_spec (as
);
5161 else if (m
== MATCH_NO
)
5163 gfc_free_array_spec (as
);
5167 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
5170 gfc_set_sym_referenced (cpte
);
5172 if (cpte
->as
== NULL
)
5174 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
5175 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5177 else if (as
!= NULL
)
5179 gfc_error ("Duplicate array spec for Cray pointee at %C");
5180 gfc_free_array_spec (as
);
5186 if (cpte
->as
!= NULL
)
5188 /* Fix array spec. */
5189 m
= gfc_mod_pointee_as (cpte
->as
);
5190 if (m
== MATCH_ERROR
)
5194 /* Point the Pointee at the Pointer. */
5195 cpte
->cp_pointer
= cptr
;
5197 if (gfc_match_char (')') != MATCH_YES
)
5199 gfc_error ("Expected \")\" at %C");
5202 m
= gfc_match_char (',');
5204 done
= true; /* Stop searching for more declarations. */
5208 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
5209 || gfc_match_eos () != MATCH_YES
)
5211 gfc_error ("Expected \",\" or end of statement at %C");
5219 gfc_match_external (void)
5222 gfc_clear_attr (¤t_attr
);
5223 current_attr
.external
= 1;
5225 return attr_decl ();
5230 gfc_match_intent (void)
5234 intent
= match_intent_spec ();
5235 if (intent
== INTENT_UNKNOWN
)
5238 gfc_clear_attr (¤t_attr
);
5239 current_attr
.intent
= intent
;
5241 return attr_decl ();
5246 gfc_match_intrinsic (void)
5249 gfc_clear_attr (¤t_attr
);
5250 current_attr
.intrinsic
= 1;
5252 return attr_decl ();
5257 gfc_match_optional (void)
5260 gfc_clear_attr (¤t_attr
);
5261 current_attr
.optional
= 1;
5263 return attr_decl ();
5268 gfc_match_pointer (void)
5270 gfc_gobble_whitespace ();
5271 if (gfc_peek_char () == '(')
5273 if (!gfc_option
.flag_cray_pointer
)
5275 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5279 return cray_pointer_decl ();
5283 gfc_clear_attr (¤t_attr
);
5284 current_attr
.pointer
= 1;
5286 return attr_decl ();
5292 gfc_match_allocatable (void)
5294 gfc_clear_attr (¤t_attr
);
5295 current_attr
.allocatable
= 1;
5297 return attr_decl ();
5302 gfc_match_dimension (void)
5304 gfc_clear_attr (¤t_attr
);
5305 current_attr
.dimension
= 1;
5307 return attr_decl ();
5312 gfc_match_target (void)
5314 gfc_clear_attr (¤t_attr
);
5315 current_attr
.target
= 1;
5317 return attr_decl ();
5321 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5325 access_attr_decl (gfc_statement st
)
5327 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5328 interface_type type
;
5331 gfc_intrinsic_op
operator;
5334 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5339 m
= gfc_match_generic_spec (&type
, name
, &operator);
5342 if (m
== MATCH_ERROR
)
5347 case INTERFACE_NAMELESS
:
5348 case INTERFACE_ABSTRACT
:
5351 case INTERFACE_GENERIC
:
5352 if (gfc_get_symbol (name
, NULL
, &sym
))
5355 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
5356 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
5357 sym
->name
, NULL
) == FAILURE
)
5362 case INTERFACE_INTRINSIC_OP
:
5363 if (gfc_current_ns
->operator_access
[operator] == ACCESS_UNKNOWN
)
5365 gfc_current_ns
->operator_access
[operator] =
5366 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
5370 gfc_error ("Access specification of the %s operator at %C has "
5371 "already been specified", gfc_op2string (operator));
5377 case INTERFACE_USER_OP
:
5378 uop
= gfc_get_uop (name
);
5380 if (uop
->access
== ACCESS_UNKNOWN
)
5382 uop
->access
= (st
== ST_PUBLIC
)
5383 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
5387 gfc_error ("Access specification of the .%s. operator at %C "
5388 "has already been specified", sym
->name
);
5395 if (gfc_match_char (',') == MATCH_NO
)
5399 if (gfc_match_eos () != MATCH_YES
)
5404 gfc_syntax_error (st
);
5412 gfc_match_protected (void)
5417 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5419 gfc_error ("PROTECTED at %C only allowed in specification "
5420 "part of a module");
5425 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
5429 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5434 if (gfc_match_eos () == MATCH_YES
)
5439 m
= gfc_match_symbol (&sym
, 0);
5443 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5456 if (gfc_match_eos () == MATCH_YES
)
5458 if (gfc_match_char (',') != MATCH_YES
)
5465 gfc_error ("Syntax error in PROTECTED statement at %C");
5470 /* The PRIVATE statement is a bit weird in that it can be an attribute
5471 declaration, but also works as a standlone statement inside of a
5472 type declaration or a module. */
5475 gfc_match_private (gfc_statement
*st
)
5478 if (gfc_match ("private") != MATCH_YES
)
5481 if (gfc_current_state () != COMP_MODULE
5482 && (gfc_current_state () != COMP_DERIVED
5483 || !gfc_state_stack
->previous
5484 || gfc_state_stack
->previous
->state
!= COMP_MODULE
))
5486 gfc_error ("PRIVATE statement at %C is only allowed in the "
5487 "specification part of a module");
5491 if (gfc_current_state () == COMP_DERIVED
)
5493 if (gfc_match_eos () == MATCH_YES
)
5499 gfc_syntax_error (ST_PRIVATE
);
5503 if (gfc_match_eos () == MATCH_YES
)
5510 return access_attr_decl (ST_PRIVATE
);
5515 gfc_match_public (gfc_statement
*st
)
5518 if (gfc_match ("public") != MATCH_YES
)
5521 if (gfc_current_state () != COMP_MODULE
)
5523 gfc_error ("PUBLIC statement at %C is only allowed in the "
5524 "specification part of a module");
5528 if (gfc_match_eos () == MATCH_YES
)
5535 return access_attr_decl (ST_PUBLIC
);
5539 /* Workhorse for gfc_match_parameter. */
5548 m
= gfc_match_symbol (&sym
, 0);
5550 gfc_error ("Expected variable name at %C in PARAMETER statement");
5555 if (gfc_match_char ('=') == MATCH_NO
)
5557 gfc_error ("Expected = sign in PARAMETER statement at %C");
5561 m
= gfc_match_init_expr (&init
);
5563 gfc_error ("Expected expression at %C in PARAMETER statement");
5567 if (sym
->ts
.type
== BT_UNKNOWN
5568 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
5574 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
5575 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
5581 if (sym
->ts
.type
== BT_CHARACTER
5582 && sym
->ts
.cl
!= NULL
5583 && sym
->ts
.cl
->length
!= NULL
5584 && sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
5585 && init
->expr_type
== EXPR_CONSTANT
5586 && init
->ts
.type
== BT_CHARACTER
5587 && init
->ts
.kind
== 1)
5588 gfc_set_constant_character_len (
5589 mpz_get_si (sym
->ts
.cl
->length
->value
.integer
), init
, false);
5595 gfc_free_expr (init
);
5600 /* Match a parameter statement, with the weird syntax that these have. */
5603 gfc_match_parameter (void)
5607 if (gfc_match_char ('(') == MATCH_NO
)
5616 if (gfc_match (" )%t") == MATCH_YES
)
5619 if (gfc_match_char (',') != MATCH_YES
)
5621 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5631 /* Save statements have a special syntax. */
5634 gfc_match_save (void)
5636 char n
[GFC_MAX_SYMBOL_LEN
+1];
5641 if (gfc_match_eos () == MATCH_YES
)
5643 if (gfc_current_ns
->seen_save
)
5645 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
5646 "follows previous SAVE statement")
5651 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
5655 if (gfc_current_ns
->save_all
)
5657 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
5658 "blanket SAVE statement")
5667 m
= gfc_match_symbol (&sym
, 0);
5671 if (gfc_add_save (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5683 m
= gfc_match (" / %n /", &n
);
5684 if (m
== MATCH_ERROR
)
5689 c
= gfc_get_common (n
, 0);
5692 gfc_current_ns
->seen_save
= 1;
5695 if (gfc_match_eos () == MATCH_YES
)
5697 if (gfc_match_char (',') != MATCH_YES
)
5704 gfc_error ("Syntax error in SAVE statement at %C");
5710 gfc_match_value (void)
5715 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
5719 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5724 if (gfc_match_eos () == MATCH_YES
)
5729 m
= gfc_match_symbol (&sym
, 0);
5733 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5746 if (gfc_match_eos () == MATCH_YES
)
5748 if (gfc_match_char (',') != MATCH_YES
)
5755 gfc_error ("Syntax error in VALUE statement at %C");
5761 gfc_match_volatile (void)
5766 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
5770 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5775 if (gfc_match_eos () == MATCH_YES
)
5780 /* VOLATILE is special because it can be added to host-associated
5782 m
= gfc_match_symbol (&sym
, 1);
5786 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5799 if (gfc_match_eos () == MATCH_YES
)
5801 if (gfc_match_char (',') != MATCH_YES
)
5808 gfc_error ("Syntax error in VOLATILE statement at %C");
5813 /* Match a module procedure statement. Note that we have to modify
5814 symbols in the parent's namespace because the current one was there
5815 to receive symbols that are in an interface's formal argument list. */
5818 gfc_match_modproc (void)
5820 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5823 gfc_namespace
*module_ns
;
5825 if (gfc_state_stack
->state
!= COMP_INTERFACE
5826 || gfc_state_stack
->previous
== NULL
5827 || current_interface
.type
== INTERFACE_NAMELESS
5828 || current_interface
.type
== INTERFACE_ABSTRACT
)
5830 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5835 module_ns
= gfc_current_ns
->parent
;
5836 for (; module_ns
; module_ns
= module_ns
->parent
)
5837 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5840 if (module_ns
== NULL
)
5845 m
= gfc_match_name (name
);
5851 if (gfc_get_symbol (name
, module_ns
, &sym
))
5854 if (sym
->attr
.proc
!= PROC_MODULE
5855 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
5856 sym
->name
, NULL
) == FAILURE
)
5859 if (gfc_add_interface (sym
) == FAILURE
)
5862 sym
->attr
.mod_proc
= 1;
5864 if (gfc_match_eos () == MATCH_YES
)
5866 if (gfc_match_char (',') != MATCH_YES
)
5873 gfc_syntax_error (ST_MODULE_PROC
);
5878 /* Match the optional attribute specifiers for a type declaration.
5879 Return MATCH_ERROR if an error is encountered in one of the handled
5880 attributes (public, private, bind(c)), MATCH_NO if what's found is
5881 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5882 checking on attribute conflicts needs to be done. */
5885 gfc_get_type_attr_spec (symbol_attribute
*attr
)
5887 /* See if the derived type is marked as private. */
5888 if (gfc_match (" , private") == MATCH_YES
)
5890 if (gfc_current_state () != COMP_MODULE
)
5892 gfc_error ("Derived type at %C can only be PRIVATE in the "
5893 "specification part of a module");
5897 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
5900 else if (gfc_match (" , public") == MATCH_YES
)
5902 if (gfc_current_state () != COMP_MODULE
)
5904 gfc_error ("Derived type at %C can only be PUBLIC in the "
5905 "specification part of a module");
5909 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
5912 else if (gfc_match(" , bind ( c )") == MATCH_YES
)
5914 /* If the type is defined to be bind(c) it then needs to make
5915 sure that all fields are interoperable. This will
5916 need to be a semantic check on the finished derived type.
5917 See 15.2.3 (lines 9-12) of F2003 draft. */
5918 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
5921 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5926 /* If we get here, something matched. */
5931 /* Match the beginning of a derived type declaration. If a type name
5932 was the result of a function, then it is possible to have a symbol
5933 already to be known as a derived type yet have no components. */
5936 gfc_match_derived_decl (void)
5938 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5939 symbol_attribute attr
;
5942 match is_type_attr_spec
= MATCH_NO
;
5943 bool seen_attr
= false;
5945 if (gfc_current_state () == COMP_DERIVED
)
5948 gfc_clear_attr (&attr
);
5952 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
);
5953 if (is_type_attr_spec
== MATCH_ERROR
)
5955 if (is_type_attr_spec
== MATCH_YES
)
5957 } while (is_type_attr_spec
== MATCH_YES
);
5959 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
5961 gfc_error ("Expected :: in TYPE definition at %C");
5965 m
= gfc_match (" %n%t", name
);
5969 /* Make sure the name is not the name of an intrinsic type. */
5970 if (gfc_is_intrinsic_typename (name
))
5972 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5977 if (gfc_get_symbol (name
, NULL
, &sym
))
5980 if (sym
->ts
.type
!= BT_UNKNOWN
)
5982 gfc_error ("Derived type name '%s' at %C already has a basic type "
5983 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
5987 /* The symbol may already have the derived attribute without the
5988 components. The ways this can happen is via a function
5989 definition, an INTRINSIC statement or a subtype in another
5990 derived type that is a pointer. The first part of the AND clause
5991 is true if a the symbol is not the return value of a function. */
5992 if (sym
->attr
.flavor
!= FL_DERIVED
5993 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
5996 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
5998 gfc_error ("Derived type definition of '%s' at %C has already been "
5999 "defined", sym
->name
);
6003 if (attr
.access
!= ACCESS_UNKNOWN
6004 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
6007 /* See if the derived type was labeled as bind(c). */
6008 if (attr
.is_bind_c
!= 0)
6009 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
6011 gfc_new_block
= sym
;
6017 /* Cray Pointees can be declared as:
6018 pointer (ipt, a (n,m,...,*))
6019 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6020 cheat and set a constant bound of 1 for the last dimension, if this
6021 is the case. Since there is no bounds-checking for Cray Pointees,
6022 this will be okay. */
6025 gfc_mod_pointee_as (gfc_array_spec
*as
)
6027 as
->cray_pointee
= true; /* This will be useful to know later. */
6028 if (as
->type
== AS_ASSUMED_SIZE
)
6030 as
->type
= AS_EXPLICIT
;
6031 as
->upper
[as
->rank
- 1] = gfc_int_expr (1);
6032 as
->cp_was_assumed
= true;
6034 else if (as
->type
== AS_ASSUMED_SHAPE
)
6036 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6043 /* Match the enum definition statement, here we are trying to match
6044 the first line of enum definition statement.
6045 Returns MATCH_YES if match is found. */
6048 gfc_match_enum (void)
6052 m
= gfc_match_eos ();
6056 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
6064 /* Match a variable name with an optional initializer. When this
6065 subroutine is called, a variable is expected to be parsed next.
6066 Depending on what is happening at the moment, updates either the
6067 symbol table or the current interface. */
6070 enumerator_decl (void)
6072 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6073 gfc_expr
*initializer
;
6074 gfc_array_spec
*as
= NULL
;
6082 old_locus
= gfc_current_locus
;
6084 /* When we get here, we've just matched a list of attributes and
6085 maybe a type and a double colon. The next thing we expect to see
6086 is the name of the symbol. */
6087 m
= gfc_match_name (name
);
6091 var_locus
= gfc_current_locus
;
6093 /* OK, we've successfully matched the declaration. Now put the
6094 symbol in the current namespace. If we fail to create the symbol,
6096 if (build_sym (name
, NULL
, &as
, &var_locus
) == FAILURE
)
6102 /* The double colon must be present in order to have initializers.
6103 Otherwise the statement is ambiguous with an assignment statement. */
6106 if (gfc_match_char ('=') == MATCH_YES
)
6108 m
= gfc_match_init_expr (&initializer
);
6111 gfc_error ("Expected an initialization expression at %C");
6120 /* If we do not have an initializer, the initialization value of the
6121 previous enumerator (stored in last_initializer) is incremented
6122 by 1 and is used to initialize the current enumerator. */
6123 if (initializer
== NULL
)
6124 initializer
= gfc_enum_initializer (last_initializer
, old_locus
);
6126 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
6128 gfc_error("ENUMERATOR %L not initialized with integer expression",
6131 gfc_free_enum_history ();
6135 /* Store this current initializer, for the next enumerator variable
6136 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6137 use last_initializer below. */
6138 last_initializer
= initializer
;
6139 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
6141 /* Maintain enumerator history. */
6142 gfc_find_symbol (name
, NULL
, 0, &sym
);
6143 create_enum_history (sym
, last_initializer
);
6145 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6148 /* Free stuff up and return. */
6149 gfc_free_expr (initializer
);
6155 /* Match the enumerator definition statement. */
6158 gfc_match_enumerator_def (void)
6163 gfc_clear_ts (¤t_ts
);
6165 m
= gfc_match (" enumerator");
6169 m
= gfc_match (" :: ");
6170 if (m
== MATCH_ERROR
)
6173 colon_seen
= (m
== MATCH_YES
);
6175 if (gfc_current_state () != COMP_ENUM
)
6177 gfc_error ("ENUM definition statement expected before %C");
6178 gfc_free_enum_history ();
6182 (¤t_ts
)->type
= BT_INTEGER
;
6183 (¤t_ts
)->kind
= gfc_c_int_kind
;
6185 gfc_clear_attr (¤t_attr
);
6186 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
6195 m
= enumerator_decl ();
6196 if (m
== MATCH_ERROR
)
6201 if (gfc_match_eos () == MATCH_YES
)
6203 if (gfc_match_char (',') != MATCH_YES
)
6207 if (gfc_current_state () == COMP_ENUM
)
6209 gfc_free_enum_history ();
6210 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6215 gfc_free_array_spec (current_as
);