1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector
;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts
;
41 static symbol_attribute current_attr
;
42 static gfc_array_spec
*current_as
;
43 static int colon_seen
;
45 /* The current binding label (if any). */
46 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
47 /* Need to know how many identifiers are on the current data declaration
48 line in case we're given the BIND(C) attribute with a NAME= specifier. */
49 static int num_idents_on_line
;
50 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
51 can supply a name if the curr_binding_label is nil and NAME= was not. */
52 static int has_name_equals
= 0;
54 /* Initializer of the previous enumerator. */
56 static gfc_expr
*last_initializer
;
58 /* History of all the enumerators is maintained, so that
59 kind values of all the enumerators could be updated depending
60 upon the maximum initialized value. */
62 typedef struct enumerator_history
65 gfc_expr
*initializer
;
66 struct enumerator_history
*next
;
70 /* Header of enum history chain. */
72 static enumerator_history
*enum_history
= NULL
;
74 /* Pointer of enum history node containing largest initializer. */
76 static enumerator_history
*max_enum
= NULL
;
78 /* gfc_new_block points to the symbol of a newly matched block. */
80 gfc_symbol
*gfc_new_block
;
83 /********************* DATA statement subroutines *********************/
85 static bool in_match_data
= false;
88 gfc_in_match_data (void)
94 gfc_set_in_match_data (bool set_value
)
96 in_match_data
= set_value
;
99 /* Free a gfc_data_variable structure and everything beneath it. */
102 free_variable (gfc_data_variable
*p
)
104 gfc_data_variable
*q
;
109 gfc_free_expr (p
->expr
);
110 gfc_free_iterator (&p
->iter
, 0);
111 free_variable (p
->list
);
117 /* Free a gfc_data_value structure and everything beneath it. */
120 free_value (gfc_data_value
*p
)
127 gfc_free_expr (p
->expr
);
133 /* Free a list of gfc_data structures. */
136 gfc_free_data (gfc_data
*p
)
143 free_variable (p
->var
);
144 free_value (p
->value
);
150 /* Free all data in a namespace. */
153 gfc_free_data_all (gfc_namespace
*ns
)
166 static match
var_element (gfc_data_variable
*);
168 /* Match a list of variables terminated by an iterator and a right
172 var_list (gfc_data_variable
*parent
)
174 gfc_data_variable
*tail
, var
;
177 m
= var_element (&var
);
178 if (m
== MATCH_ERROR
)
183 tail
= gfc_get_data_variable ();
190 if (gfc_match_char (',') != MATCH_YES
)
193 m
= gfc_match_iterator (&parent
->iter
, 1);
196 if (m
== MATCH_ERROR
)
199 m
= var_element (&var
);
200 if (m
== MATCH_ERROR
)
205 tail
->next
= gfc_get_data_variable ();
211 if (gfc_match_char (')') != MATCH_YES
)
216 gfc_syntax_error (ST_DATA
);
221 /* Match a single element in a data variable list, which can be a
222 variable-iterator list. */
225 var_element (gfc_data_variable
*new)
230 memset (new, 0, sizeof (gfc_data_variable
));
232 if (gfc_match_char ('(') == MATCH_YES
)
233 return var_list (new);
235 m
= gfc_match_variable (&new->expr
, 0);
239 sym
= new->expr
->symtree
->n
.sym
;
241 if (!sym
->attr
.function
&& gfc_current_ns
->parent
242 && gfc_current_ns
->parent
== sym
->ns
)
244 gfc_error ("Host associated variable '%s' may not be in the DATA "
245 "statement at %C", sym
->name
);
249 if (gfc_current_state () != COMP_BLOCK_DATA
250 && sym
->attr
.in_common
251 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
252 "common block variable '%s' in DATA statement at %C",
253 sym
->name
) == FAILURE
)
256 if (gfc_add_data (&sym
->attr
, sym
->name
, &new->expr
->where
) == FAILURE
)
263 /* Match the top-level list of data variables. */
266 top_var_list (gfc_data
*d
)
268 gfc_data_variable var
, *tail
, *new;
275 m
= var_element (&var
);
278 if (m
== MATCH_ERROR
)
281 new = gfc_get_data_variable ();
291 if (gfc_match_char ('/') == MATCH_YES
)
293 if (gfc_match_char (',') != MATCH_YES
)
300 gfc_syntax_error (ST_DATA
);
301 gfc_free_data_all (gfc_current_ns
);
307 match_data_constant (gfc_expr
**result
)
309 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
315 m
= gfc_match_literal_constant (&expr
, 1);
322 if (m
== MATCH_ERROR
)
325 m
= gfc_match_null (result
);
329 old_loc
= gfc_current_locus
;
331 /* Should this be a structure component, try to match it
332 before matching a name. */
333 m
= gfc_match_rvalue (result
);
334 if (m
== MATCH_ERROR
)
337 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
339 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
344 gfc_current_locus
= old_loc
;
346 m
= gfc_match_name (name
);
350 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
354 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
356 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
360 else if (sym
->attr
.flavor
== FL_DERIVED
)
361 return gfc_match_structure_constructor (sym
, result
);
363 *result
= gfc_copy_expr (sym
->value
);
368 /* Match a list of values in a DATA statement. The leading '/' has
369 already been seen at this point. */
372 top_val_list (gfc_data
*data
)
374 gfc_data_value
*new, *tail
;
383 m
= match_data_constant (&expr
);
386 if (m
== MATCH_ERROR
)
389 new = gfc_get_data_value ();
398 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
406 msg
= gfc_extract_int (expr
, &tmp
);
407 gfc_free_expr (expr
);
415 m
= match_data_constant (&tail
->expr
);
418 if (m
== MATCH_ERROR
)
422 if (gfc_match_char ('/') == MATCH_YES
)
424 if (gfc_match_char (',') == MATCH_NO
)
431 gfc_syntax_error (ST_DATA
);
432 gfc_free_data_all (gfc_current_ns
);
437 /* Matches an old style initialization. */
440 match_old_style_init (const char *name
)
447 /* Set up data structure to hold initializers. */
448 gfc_find_sym_tree (name
, NULL
, 0, &st
);
451 newdata
= gfc_get_data ();
452 newdata
->var
= gfc_get_data_variable ();
453 newdata
->var
->expr
= gfc_get_variable_expr (st
);
454 newdata
->where
= gfc_current_locus
;
456 /* Match initial value list. This also eats the terminal '/'. */
457 m
= top_val_list (newdata
);
466 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
471 /* Mark the variable as having appeared in a data statement. */
472 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
478 /* Chain in namespace list of DATA initializers. */
479 newdata
->next
= gfc_current_ns
->data
;
480 gfc_current_ns
->data
= newdata
;
486 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
487 we are matching a DATA statement and are therefore issuing an error
488 if we encounter something unexpected, if not, we're trying to match
489 an old-style initialization expression of the form INTEGER I /2/. */
492 gfc_match_data (void)
497 gfc_set_in_match_data (true);
501 new = gfc_get_data ();
502 new->where
= gfc_current_locus
;
504 m
= top_var_list (new);
508 m
= top_val_list (new);
512 new->next
= gfc_current_ns
->data
;
513 gfc_current_ns
->data
= new;
515 if (gfc_match_eos () == MATCH_YES
)
518 gfc_match_char (','); /* Optional comma */
521 gfc_set_in_match_data (false);
525 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
532 gfc_set_in_match_data (false);
538 /************************ Declaration statements *********************/
540 /* Match an intent specification. Since this can only happen after an
541 INTENT word, a legal intent-spec must follow. */
544 match_intent_spec (void)
547 if (gfc_match (" ( in out )") == MATCH_YES
)
549 if (gfc_match (" ( in )") == MATCH_YES
)
551 if (gfc_match (" ( out )") == MATCH_YES
)
554 gfc_error ("Bad INTENT specification at %C");
555 return INTENT_UNKNOWN
;
559 /* Matches a character length specification, which is either a
560 specification expression or a '*'. */
563 char_len_param_value (gfc_expr
**expr
)
565 if (gfc_match_char ('*') == MATCH_YES
)
571 return gfc_match_expr (expr
);
575 /* A character length is a '*' followed by a literal integer or a
576 char_len_param_value in parenthesis. */
579 match_char_length (gfc_expr
**expr
)
584 m
= gfc_match_char ('*');
588 m
= gfc_match_small_literal_int (&length
, NULL
);
589 if (m
== MATCH_ERROR
)
594 *expr
= gfc_int_expr (length
);
598 if (gfc_match_char ('(') == MATCH_NO
)
601 m
= char_len_param_value (expr
);
602 if (m
== MATCH_ERROR
)
607 if (gfc_match_char (')') == MATCH_NO
)
609 gfc_free_expr (*expr
);
617 gfc_error ("Syntax error in character length specification at %C");
622 /* Special subroutine for finding a symbol. Check if the name is found
623 in the current name space. If not, and we're compiling a function or
624 subroutine and the parent compilation unit is an interface, then check
625 to see if the name we've been given is the name of the interface
626 (located in another namespace). */
629 find_special (const char *name
, gfc_symbol
**result
)
634 i
= gfc_get_symbol (name
, NULL
, result
);
638 if (gfc_current_state () != COMP_SUBROUTINE
639 && gfc_current_state () != COMP_FUNCTION
)
642 s
= gfc_state_stack
->previous
;
646 if (s
->state
!= COMP_INTERFACE
)
649 goto end
; /* Nameless interface. */
651 if (strcmp (name
, s
->sym
->name
) == 0)
662 /* Special subroutine for getting a symbol node associated with a
663 procedure name, used in SUBROUTINE and FUNCTION statements. The
664 symbol is created in the parent using with symtree node in the
665 child unit pointing to the symbol. If the current namespace has no
666 parent, then the symbol is just created in the current unit. */
669 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
675 /* Module functions have to be left in their own namespace because
676 they have potentially (almost certainly!) already been referenced.
677 In this sense, they are rather like external functions. This is
678 fixed up in resolve.c(resolve_entries), where the symbol name-
679 space is set to point to the master function, so that the fake
680 result mechanism can work. */
681 if (module_fcn_entry
)
683 /* Present if entry is declared to be a module procedure. */
684 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
686 rc
= gfc_get_symbol (name
, NULL
, result
);
689 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
692 gfc_current_ns
->refs
++;
694 if (sym
&& !sym
->new && gfc_current_state () != COMP_INTERFACE
)
696 /* Trap another encompassed procedure with the same name. All
697 these conditions are necessary to avoid picking up an entry
698 whose name clashes with that of the encompassing procedure;
699 this is handled using gsymbols to register unique,globally
701 if (sym
->attr
.flavor
!= 0
702 && sym
->attr
.proc
!= 0
703 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
704 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
705 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
706 name
, &sym
->declared_at
);
708 /* Trap a procedure with a name the same as interface in the
709 encompassing scope. */
710 if (sym
->attr
.generic
!= 0
711 && (sym
->attr
.subroutine
|| sym
->attr
.function
))
712 gfc_error_now ("Name '%s' at %C is already defined"
713 " as a generic interface at %L",
714 name
, &sym
->declared_at
);
716 /* Trap declarations of attributes in encompassing scope. The
717 signature for this is that ts.kind is set. Legitimate
718 references only set ts.type. */
719 if (sym
->ts
.kind
!= 0
720 && !sym
->attr
.implicit_type
721 && sym
->attr
.proc
== 0
722 && gfc_current_ns
->parent
!= NULL
723 && sym
->attr
.access
== 0
724 && !module_fcn_entry
)
725 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
726 "and must not have attributes declared at %L",
727 name
, &sym
->declared_at
);
730 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
733 /* Module function entries will already have a symtree in
734 the current namespace but will need one at module level. */
735 if (module_fcn_entry
)
737 /* Present if entry is declared to be a module procedure. */
738 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
740 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
743 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
748 /* See if the procedure should be a module procedure. */
750 if (((sym
->ns
->proc_name
!= NULL
751 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
752 && sym
->attr
.proc
!= PROC_MODULE
)
753 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
754 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
755 sym
->name
, NULL
) == FAILURE
)
762 /* Verify that the given symbol representing a parameter is C
763 interoperable, by checking to see if it was marked as such after
764 its declaration. If the given symbol is not interoperable, a
765 warning is reported, thus removing the need to return the status to
766 the calling function. The standard does not require the user use
767 one of the iso_c_binding named constants to declare an
768 interoperable parameter, but we can't be sure if the param is C
769 interop or not if the user doesn't. For example, integer(4) may be
770 legal Fortran, but doesn't have meaning in C. It may interop with
771 a number of the C types, which causes a problem because the
772 compiler can't know which one. This code is almost certainly not
773 portable, and the user will get what they deserve if the C type
774 across platforms isn't always interoperable with integer(4). If
775 the user had used something like integer(c_int) or integer(c_long),
776 the compiler could have automatically handled the varying sizes
780 verify_c_interop_param (gfc_symbol
*sym
)
782 int is_c_interop
= 0;
783 try retval
= SUCCESS
;
785 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
786 Don't repeat the checks here. */
787 if (sym
->attr
.implicit_type
)
790 /* For subroutines or functions that are passed to a BIND(C) procedure,
791 they're interoperable if they're BIND(C) and their params are all
793 if (sym
->attr
.flavor
== FL_PROCEDURE
)
795 if (sym
->attr
.is_bind_c
== 0)
797 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
798 "attribute to be C interoperable", sym
->name
,
799 &(sym
->declared_at
));
805 if (sym
->attr
.is_c_interop
== 1)
806 /* We've already checked this procedure; don't check it again. */
809 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
814 /* See if we've stored a reference to a procedure that owns sym. */
815 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
817 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
820 (verify_c_interop (&(sym
->ts
), sym
->name
, &(sym
->declared_at
))
823 if (is_c_interop
!= 1)
825 /* Make personalized messages to give better feedback. */
826 if (sym
->ts
.type
== BT_DERIVED
)
827 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
828 " procedure '%s' but is not C interoperable "
829 "because derived type '%s' is not C interoperable",
830 sym
->name
, &(sym
->declared_at
),
831 sym
->ns
->proc_name
->name
,
832 sym
->ts
.derived
->name
);
834 gfc_warning ("Variable '%s' at %L is a parameter to the "
835 "BIND(C) procedure '%s' but may not be C "
837 sym
->name
, &(sym
->declared_at
),
838 sym
->ns
->proc_name
->name
);
841 /* We have to make sure that any param to a bind(c) routine does
842 not have the allocatable, pointer, or optional attributes,
843 according to J3/04-007, section 5.1. */
844 if (sym
->attr
.allocatable
== 1)
846 gfc_error ("Variable '%s' at %L cannot have the "
847 "ALLOCATABLE attribute because procedure '%s'"
848 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
849 sym
->ns
->proc_name
->name
);
853 if (sym
->attr
.pointer
== 1)
855 gfc_error ("Variable '%s' at %L cannot have the "
856 "POINTER attribute because procedure '%s'"
857 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
858 sym
->ns
->proc_name
->name
);
862 if (sym
->attr
.optional
== 1)
864 gfc_error ("Variable '%s' at %L cannot have the "
865 "OPTIONAL attribute because procedure '%s'"
866 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
867 sym
->ns
->proc_name
->name
);
871 /* Make sure that if it has the dimension attribute, that it is
872 either assumed size or explicit shape. */
875 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
877 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
878 "argument to the procedure '%s' at %L because "
879 "the procedure is BIND(C)", sym
->name
,
880 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
881 &(sym
->ns
->proc_name
->declared_at
));
885 if (sym
->as
->type
== AS_DEFERRED
)
887 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
888 "argument to the procedure '%s' at %L because "
889 "the procedure is BIND(C)", sym
->name
,
890 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
891 &(sym
->ns
->proc_name
->declared_at
));
902 /* Function called by variable_decl() that adds a name to the symbol table. */
905 build_sym (const char *name
, gfc_charlen
*cl
,
906 gfc_array_spec
**as
, locus
*var_locus
)
908 symbol_attribute attr
;
911 if (gfc_get_symbol (name
, NULL
, &sym
))
914 /* Start updating the symbol table. Add basic type attribute if present. */
915 if (current_ts
.type
!= BT_UNKNOWN
916 && (sym
->attr
.implicit_type
== 0
917 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
918 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
921 if (sym
->ts
.type
== BT_CHARACTER
)
924 /* Add dimension attribute if present. */
925 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
929 /* Add attribute to symbol. The copy is so that we can reset the
930 dimension attribute. */
934 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
937 /* Finish any work that may need to be done for the binding label,
938 if it's a bind(c). The bind(c) attr is found before the symbol
939 is made, and before the symbol name (for data decls), so the
940 current_ts is holding the binding label, or nothing if the
941 name= attr wasn't given. Therefore, test here if we're dealing
942 with a bind(c) and make sure the binding label is set correctly. */
943 if (sym
->attr
.is_bind_c
== 1)
945 if (sym
->binding_label
[0] == '\0')
947 /* Here, we're not checking the numIdents (the last param).
948 This could be an error we're letting slip through! */
949 if (set_binding_label (sym
->binding_label
, sym
->name
, 1) == FAILURE
)
954 /* See if we know we're in a common block, and if it's a bind(c)
955 common then we need to make sure we're an interoperable type. */
956 if (sym
->attr
.in_common
== 1)
958 /* Test the common block object. */
959 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
960 && sym
->ts
.is_c_interop
!= 1)
962 gfc_error_now ("Variable '%s' in common block '%s' at %C "
963 "must be declared with a C interoperable "
964 "kind since common block '%s' is BIND(C)",
965 sym
->name
, sym
->common_block
->name
,
966 sym
->common_block
->name
);
971 sym
->attr
.implied_index
= 0;
977 /* Set character constant to the given length. The constant will be padded or
981 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, bool array
)
986 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
987 gcc_assert (expr
->ts
.type
== BT_CHARACTER
&& expr
->ts
.kind
== 1);
989 slen
= expr
->value
.character
.length
;
992 s
= gfc_getmem (len
+ 1);
993 memcpy (s
, expr
->value
.character
.string
, MIN (len
, slen
));
995 memset (&s
[slen
], ' ', len
- slen
);
997 if (gfc_option
.warn_character_truncation
&& slen
> len
)
998 gfc_warning_now ("CHARACTER expression at %L is being truncated "
999 "(%d/%d)", &expr
->where
, slen
, len
);
1001 /* Apply the standard by 'hand' otherwise it gets cleared for
1003 if (array
&& slen
< len
&& !(gfc_option
.allow_std
& GFC_STD_GNU
))
1004 gfc_error_now ("The CHARACTER elements of the array constructor "
1005 "at %L must have the same length (%d/%d)",
1006 &expr
->where
, slen
, len
);
1009 gfc_free (expr
->value
.character
.string
);
1010 expr
->value
.character
.string
= s
;
1011 expr
->value
.character
.length
= len
;
1016 /* Function to create and update the enumerator history
1017 using the information passed as arguments.
1018 Pointer "max_enum" is also updated, to point to
1019 enum history node containing largest initializer.
1021 SYM points to the symbol node of enumerator.
1022 INIT points to its enumerator value. */
1025 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1027 enumerator_history
*new_enum_history
;
1028 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1030 new_enum_history
= gfc_getmem (sizeof (enumerator_history
));
1032 new_enum_history
->sym
= sym
;
1033 new_enum_history
->initializer
= init
;
1034 new_enum_history
->next
= NULL
;
1036 if (enum_history
== NULL
)
1038 enum_history
= new_enum_history
;
1039 max_enum
= enum_history
;
1043 new_enum_history
->next
= enum_history
;
1044 enum_history
= new_enum_history
;
1046 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1047 new_enum_history
->initializer
->value
.integer
) < 0)
1048 max_enum
= new_enum_history
;
1053 /* Function to free enum kind history. */
1056 gfc_free_enum_history (void)
1058 enumerator_history
*current
= enum_history
;
1059 enumerator_history
*next
;
1061 while (current
!= NULL
)
1063 next
= current
->next
;
1068 enum_history
= NULL
;
1072 /* Function called by variable_decl() that adds an initialization
1073 expression to a symbol. */
1076 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1078 symbol_attribute attr
;
1083 if (find_special (name
, &sym
))
1088 /* If this symbol is confirming an implicit parameter type,
1089 then an initialization expression is not allowed. */
1090 if (attr
.flavor
== FL_PARAMETER
1091 && sym
->value
!= NULL
1094 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1103 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1110 /* An initializer is required for PARAMETER declarations. */
1111 if (attr
.flavor
== FL_PARAMETER
)
1113 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1119 /* If a variable appears in a DATA block, it cannot have an
1123 gfc_error ("Variable '%s' at %C with an initializer already "
1124 "appears in a DATA statement", sym
->name
);
1128 /* Check if the assignment can happen. This has to be put off
1129 until later for a derived type variable. */
1130 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1131 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1134 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.cl
)
1136 /* Update symbol character length according initializer. */
1137 if (sym
->ts
.cl
->length
== NULL
)
1139 /* If there are multiple CHARACTER variables declared on the
1140 same line, we don't want them to share the same length. */
1141 sym
->ts
.cl
= gfc_get_charlen ();
1142 sym
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1143 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1145 if (sym
->attr
.flavor
== FL_PARAMETER
1146 && init
->expr_type
== EXPR_ARRAY
)
1147 sym
->ts
.cl
->length
= gfc_copy_expr (init
->ts
.cl
->length
);
1149 /* Update initializer character length according symbol. */
1150 else if (sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1152 int len
= mpz_get_si (sym
->ts
.cl
->length
->value
.integer
);
1153 gfc_constructor
* p
;
1155 if (init
->expr_type
== EXPR_CONSTANT
)
1156 gfc_set_constant_character_len (len
, init
, false);
1157 else if (init
->expr_type
== EXPR_ARRAY
)
1159 /* Build a new charlen to prevent simplification from
1160 deleting the length before it is resolved. */
1161 init
->ts
.cl
= gfc_get_charlen ();
1162 init
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1163 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1164 init
->ts
.cl
->length
= gfc_copy_expr (sym
->ts
.cl
->length
);
1166 for (p
= init
->value
.constructor
; p
; p
= p
->next
)
1167 gfc_set_constant_character_len (len
, p
->expr
, false);
1172 /* Need to check if the expression we initialized this
1173 to was one of the iso_c_binding named constants. If so,
1174 and we're a parameter (constant), let it be iso_c.
1176 integer(c_int), parameter :: my_int = c_int
1177 integer(my_int) :: my_int_2
1178 If we mark my_int as iso_c (since we can see it's value
1179 is equal to one of the named constants), then my_int_2
1180 will be considered C interoperable. */
1181 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1183 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1184 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1185 /* attr bits needed for module files. */
1186 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1187 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1188 if (init
->ts
.is_iso_c
)
1189 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1192 /* Add initializer. Make sure we keep the ranks sane. */
1193 if (sym
->attr
.dimension
&& init
->rank
== 0)
1199 if (sym
->attr
.flavor
== FL_PARAMETER
1200 && init
->expr_type
== EXPR_CONSTANT
1201 && spec_size (sym
->as
, &size
) == SUCCESS
1202 && mpz_cmp_si (size
, 0) > 0)
1204 array
= gfc_start_constructor (init
->ts
.type
, init
->ts
.kind
,
1207 array
->value
.constructor
= c
= NULL
;
1208 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1210 if (array
->value
.constructor
== NULL
)
1212 array
->value
.constructor
= c
= gfc_get_constructor ();
1217 c
->next
= gfc_get_constructor ();
1219 c
->expr
= gfc_copy_expr (init
);
1223 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1224 for (n
= 0; n
< sym
->as
->rank
; n
++)
1225 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1230 init
->rank
= sym
->as
->rank
;
1241 /* Function called by variable_decl() that adds a name to a structure
1245 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1246 gfc_array_spec
**as
)
1250 /* If the current symbol is of the same derived type that we're
1251 constructing, it must have the pointer attribute. */
1252 if (current_ts
.type
== BT_DERIVED
1253 && current_ts
.derived
== gfc_current_block ()
1254 && current_attr
.pointer
== 0)
1256 gfc_error ("Component at %C must have the POINTER attribute");
1260 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1262 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1264 gfc_error ("Array component of structure at %C must have explicit "
1265 "or deferred shape");
1270 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1275 gfc_set_component_attr (c
, ¤t_attr
);
1277 c
->initializer
= *init
;
1285 /* Check array components. */
1290 gfc_error ("Allocatable component at %C must be an array");
1299 if (c
->as
->type
!= AS_DEFERRED
)
1301 gfc_error ("Pointer array component of structure at %C must have a "
1306 else if (c
->allocatable
)
1308 if (c
->as
->type
!= AS_DEFERRED
)
1310 gfc_error ("Allocatable component of structure at %C must have a "
1317 if (c
->as
->type
!= AS_EXPLICIT
)
1319 gfc_error ("Array component of structure at %C must have an "
1329 /* Match a 'NULL()', and possibly take care of some side effects. */
1332 gfc_match_null (gfc_expr
**result
)
1338 m
= gfc_match (" null ( )");
1342 /* The NULL symbol now has to be/become an intrinsic function. */
1343 if (gfc_get_symbol ("null", NULL
, &sym
))
1345 gfc_error ("NULL() initialization at %C is ambiguous");
1349 gfc_intrinsic_symbol (sym
);
1351 if (sym
->attr
.proc
!= PROC_INTRINSIC
1352 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1353 sym
->name
, NULL
) == FAILURE
1354 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1357 e
= gfc_get_expr ();
1358 e
->where
= gfc_current_locus
;
1359 e
->expr_type
= EXPR_NULL
;
1360 e
->ts
.type
= BT_UNKNOWN
;
1368 /* Match a variable name with an optional initializer. When this
1369 subroutine is called, a variable is expected to be parsed next.
1370 Depending on what is happening at the moment, updates either the
1371 symbol table or the current interface. */
1374 variable_decl (int elem
)
1376 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1377 gfc_expr
*initializer
, *char_len
;
1379 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1390 old_locus
= gfc_current_locus
;
1392 /* When we get here, we've just matched a list of attributes and
1393 maybe a type and a double colon. The next thing we expect to see
1394 is the name of the symbol. */
1395 m
= gfc_match_name (name
);
1399 var_locus
= gfc_current_locus
;
1401 /* Now we could see the optional array spec. or character length. */
1402 m
= gfc_match_array_spec (&as
);
1403 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1404 cp_as
= gfc_copy_array_spec (as
);
1405 else if (m
== MATCH_ERROR
)
1409 as
= gfc_copy_array_spec (current_as
);
1414 if (current_ts
.type
== BT_CHARACTER
)
1416 switch (match_char_length (&char_len
))
1419 cl
= gfc_get_charlen ();
1420 cl
->next
= gfc_current_ns
->cl_list
;
1421 gfc_current_ns
->cl_list
= cl
;
1423 cl
->length
= char_len
;
1426 /* Non-constant lengths need to be copied after the first
1429 if (elem
> 1 && current_ts
.cl
->length
1430 && current_ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
)
1432 cl
= gfc_get_charlen ();
1433 cl
->next
= gfc_current_ns
->cl_list
;
1434 gfc_current_ns
->cl_list
= cl
;
1435 cl
->length
= gfc_copy_expr (current_ts
.cl
->length
);
1447 /* If this symbol has already shown up in a Cray Pointer declaration,
1448 then we want to set the type & bail out. */
1449 if (gfc_option
.flag_cray_pointer
)
1451 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1452 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1454 sym
->ts
.type
= current_ts
.type
;
1455 sym
->ts
.kind
= current_ts
.kind
;
1457 sym
->ts
.derived
= current_ts
.derived
;
1458 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1459 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1462 /* Check to see if we have an array specification. */
1465 if (sym
->as
!= NULL
)
1467 gfc_error ("Duplicate array spec for Cray pointee at %C");
1468 gfc_free_array_spec (cp_as
);
1474 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1475 gfc_internal_error ("Couldn't set pointee array spec.");
1477 /* Fix the array spec. */
1478 m
= gfc_mod_pointee_as (sym
->as
);
1479 if (m
== MATCH_ERROR
)
1487 gfc_free_array_spec (cp_as
);
1492 /* OK, we've successfully matched the declaration. Now put the
1493 symbol in the current namespace, because it might be used in the
1494 optional initialization expression for this symbol, e.g. this is
1497 integer, parameter :: i = huge(i)
1499 This is only true for parameters or variables of a basic type.
1500 For components of derived types, it is not true, so we don't
1501 create a symbol for those yet. If we fail to create the symbol,
1503 if (gfc_current_state () != COMP_DERIVED
1504 && build_sym (name
, cl
, &as
, &var_locus
) == FAILURE
)
1510 /* An interface body specifies all of the procedure's
1511 characteristics and these shall be consistent with those
1512 specified in the procedure definition, except that the interface
1513 may specify a procedure that is not pure if the procedure is
1514 defined to be pure(12.3.2). */
1515 if (current_ts
.type
== BT_DERIVED
1516 && gfc_current_ns
->proc_name
1517 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1518 && current_ts
.derived
->ns
!= gfc_current_ns
1519 && !gfc_current_ns
->has_import_set
)
1521 gfc_error ("the type of '%s' at %C has not been declared within the "
1527 /* In functions that have a RESULT variable defined, the function
1528 name always refers to function calls. Therefore, the name is
1529 not allowed to appear in specification statements. */
1530 if (gfc_current_state () == COMP_FUNCTION
1531 && gfc_current_block () != NULL
1532 && gfc_current_block ()->result
!= NULL
1533 && gfc_current_block ()->result
!= gfc_current_block ()
1534 && strcmp (gfc_current_block ()->name
, name
) == 0)
1536 gfc_error ("Function name '%s' not allowed at %C", name
);
1541 /* We allow old-style initializations of the form
1542 integer i /2/, j(4) /3*3, 1/
1543 (if no colon has been seen). These are different from data
1544 statements in that initializers are only allowed to apply to the
1545 variable immediately preceding, i.e.
1547 is not allowed. Therefore we have to do some work manually, that
1548 could otherwise be left to the matchers for DATA statements. */
1550 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1552 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1553 "initialization at %C") == FAILURE
)
1556 return match_old_style_init (name
);
1559 /* The double colon must be present in order to have initializers.
1560 Otherwise the statement is ambiguous with an assignment statement. */
1563 if (gfc_match (" =>") == MATCH_YES
)
1565 if (!current_attr
.pointer
)
1567 gfc_error ("Initialization at %C isn't for a pointer variable");
1572 m
= gfc_match_null (&initializer
);
1575 gfc_error ("Pointer initialization requires a NULL() at %C");
1579 if (gfc_pure (NULL
))
1581 gfc_error ("Initialization of pointer at %C is not allowed in "
1582 "a PURE procedure");
1590 else if (gfc_match_char ('=') == MATCH_YES
)
1592 if (current_attr
.pointer
)
1594 gfc_error ("Pointer initialization at %C requires '=>', "
1600 m
= gfc_match_init_expr (&initializer
);
1603 gfc_error ("Expected an initialization expression at %C");
1607 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
))
1609 gfc_error ("Initialization of variable at %C is not allowed in "
1610 "a PURE procedure");
1619 if (initializer
!= NULL
&& current_attr
.allocatable
1620 && gfc_current_state () == COMP_DERIVED
)
1622 gfc_error ("Initialization of allocatable component at %C is not "
1628 /* Add the initializer. Note that it is fine if initializer is
1629 NULL here, because we sometimes also need to check if a
1630 declaration *must* have an initialization expression. */
1631 if (gfc_current_state () != COMP_DERIVED
)
1632 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
1635 if (current_ts
.type
== BT_DERIVED
1636 && !current_attr
.pointer
&& !initializer
)
1637 initializer
= gfc_default_initializer (¤t_ts
);
1638 t
= build_struct (name
, cl
, &initializer
, &as
);
1641 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
1644 /* Free stuff up and return. */
1645 gfc_free_expr (initializer
);
1646 gfc_free_array_spec (as
);
1652 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1653 This assumes that the byte size is equal to the kind number for
1654 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1657 gfc_match_old_kind_spec (gfc_typespec
*ts
)
1662 if (gfc_match_char ('*') != MATCH_YES
)
1665 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
1669 original_kind
= ts
->kind
;
1671 /* Massage the kind numbers for complex types. */
1672 if (ts
->type
== BT_COMPLEX
)
1676 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1677 gfc_basic_typename (ts
->type
), original_kind
);
1683 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1685 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1686 gfc_basic_typename (ts
->type
), original_kind
);
1690 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
1691 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
1698 /* Match a kind specification. Since kinds are generally optional, we
1699 usually return MATCH_NO if something goes wrong. If a "kind="
1700 string is found, then we know we have an error. */
1703 gfc_match_kind_spec (gfc_typespec
*ts
)
1713 where
= gfc_current_locus
;
1715 if (gfc_match_char ('(') == MATCH_NO
)
1718 /* Also gobbles optional text. */
1719 if (gfc_match (" kind = ") == MATCH_YES
)
1722 n
= gfc_match_init_expr (&e
);
1724 gfc_error ("Expected initialization expression at %C");
1730 gfc_error ("Expected scalar initialization expression at %C");
1735 msg
= gfc_extract_int (e
, &ts
->kind
);
1743 /* Before throwing away the expression, let's see if we had a
1744 C interoperable kind (and store the fact). */
1745 if (e
->ts
.is_c_interop
== 1)
1747 /* Mark this as c interoperable if being declared with one
1748 of the named constants from iso_c_binding. */
1749 ts
->is_c_interop
= e
->ts
.is_iso_c
;
1750 ts
->f90_type
= e
->ts
.f90_type
;
1756 /* Ignore errors to this point, if we've gotten here. This means
1757 we ignore the m=MATCH_ERROR from above. */
1758 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1760 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
1761 gfc_basic_typename (ts
->type
));
1764 else if (gfc_match_char (')') != MATCH_YES
)
1766 gfc_error ("Missing right parenthesis at %C");
1770 /* All tests passed. */
1773 if(m
== MATCH_ERROR
)
1774 gfc_current_locus
= where
;
1776 /* Return what we know from the test(s). */
1781 gfc_current_locus
= where
;
1786 /* Match the various kind/length specifications in a CHARACTER
1787 declaration. We don't return MATCH_NO. */
1790 match_char_spec (gfc_typespec
*ts
)
1792 int kind
, seen_length
;
1796 gfc_expr
*kind_expr
= NULL
;
1797 kind
= gfc_default_character_kind
;
1801 /* Try the old-style specification first. */
1802 old_char_selector
= 0;
1804 m
= match_char_length (&len
);
1808 old_char_selector
= 1;
1813 m
= gfc_match_char ('(');
1816 m
= MATCH_YES
; /* Character without length is a single char. */
1820 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1821 if (gfc_match (" kind =") == MATCH_YES
)
1823 m
= gfc_match_small_int_expr(&kind
, &kind_expr
);
1825 if (m
== MATCH_ERROR
)
1830 if (gfc_match (" , len =") == MATCH_NO
)
1833 m
= char_len_param_value (&len
);
1836 if (m
== MATCH_ERROR
)
1843 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1844 if (gfc_match (" len =") == MATCH_YES
)
1846 m
= char_len_param_value (&len
);
1849 if (m
== MATCH_ERROR
)
1853 if (gfc_match_char (')') == MATCH_YES
)
1856 if (gfc_match (" , kind =") != MATCH_YES
)
1859 gfc_match_small_int_expr(&kind
, &kind_expr
);
1861 if (gfc_validate_kind (BT_CHARACTER
, kind
, true) < 0)
1863 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind
);
1870 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1871 m
= char_len_param_value (&len
);
1874 if (m
== MATCH_ERROR
)
1878 m
= gfc_match_char (')');
1882 if (gfc_match_char (',') != MATCH_YES
)
1885 gfc_match (" kind ="); /* Gobble optional text. */
1887 m
= gfc_match_small_int_expr(&kind
, &kind_expr
);
1888 if (m
== MATCH_ERROR
)
1894 /* Require a right-paren at this point. */
1895 m
= gfc_match_char (')');
1900 gfc_error ("Syntax error in CHARACTER declaration at %C");
1902 gfc_free_expr (len
);
1906 if (gfc_validate_kind (BT_CHARACTER
, kind
, true) < 0)
1908 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind
);
1912 if (seen_length
== 1 && len
!= NULL
1913 && len
->ts
.type
!= BT_INTEGER
&& len
->ts
.type
!= BT_UNKNOWN
)
1915 gfc_error ("Expression at %C must be of INTEGER type");
1921 gfc_free_expr (len
);
1922 gfc_free_expr (kind_expr
);
1926 /* Do some final massaging of the length values. */
1927 cl
= gfc_get_charlen ();
1928 cl
->next
= gfc_current_ns
->cl_list
;
1929 gfc_current_ns
->cl_list
= cl
;
1931 if (seen_length
== 0)
1932 cl
->length
= gfc_int_expr (1);
1939 /* We have to know if it was a c interoperable kind so we can
1940 do accurate type checking of bind(c) procs, etc. */
1941 if (kind_expr
!= NULL
)
1943 /* Mark this as c interoperable if being declared with one
1944 of the named constants from iso_c_binding. */
1945 ts
->is_c_interop
= kind_expr
->ts
.is_iso_c
;
1946 gfc_free_expr (kind_expr
);
1948 else if (len
!= NULL
)
1950 /* Here, we might have parsed something such as:
1952 In this case, the parsing code above grabs the c_char when
1953 looking for the length (line 1690, roughly). it's the last
1954 testcase for parsing the kind params of a character variable.
1955 However, it's not actually the length. this seems like it
1957 To see if the user used a C interop kind, test the expr
1958 of the so called length, and see if it's C interoperable. */
1959 ts
->is_c_interop
= len
->ts
.is_iso_c
;
1966 /* Matches a type specification. If successful, sets the ts structure
1967 to the matched specification. This is necessary for FUNCTION and
1968 IMPLICIT statements.
1970 If implicit_flag is nonzero, then we don't check for the optional
1971 kind specification. Not doing so is needed for matching an IMPLICIT
1972 statement correctly. */
1975 match_type_spec (gfc_typespec
*ts
, int implicit_flag
)
1977 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1984 /* Clear the current binding label, in case one is given. */
1985 curr_binding_label
[0] = '\0';
1987 if (gfc_match (" byte") == MATCH_YES
)
1989 if (gfc_notify_std(GFC_STD_GNU
, "Extension: BYTE type at %C")
1993 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
1995 gfc_error ("BYTE type used at %C "
1996 "is not available on the target machine");
2000 ts
->type
= BT_INTEGER
;
2005 if (gfc_match (" integer") == MATCH_YES
)
2007 ts
->type
= BT_INTEGER
;
2008 ts
->kind
= gfc_default_integer_kind
;
2012 if (gfc_match (" character") == MATCH_YES
)
2014 ts
->type
= BT_CHARACTER
;
2015 if (implicit_flag
== 0)
2016 return match_char_spec (ts
);
2021 if (gfc_match (" real") == MATCH_YES
)
2024 ts
->kind
= gfc_default_real_kind
;
2028 if (gfc_match (" double precision") == MATCH_YES
)
2031 ts
->kind
= gfc_default_double_kind
;
2035 if (gfc_match (" complex") == MATCH_YES
)
2037 ts
->type
= BT_COMPLEX
;
2038 ts
->kind
= gfc_default_complex_kind
;
2042 if (gfc_match (" double complex") == MATCH_YES
)
2044 if (gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C does not "
2045 "conform to the Fortran 95 standard") == FAILURE
)
2048 ts
->type
= BT_COMPLEX
;
2049 ts
->kind
= gfc_default_double_kind
;
2053 if (gfc_match (" logical") == MATCH_YES
)
2055 ts
->type
= BT_LOGICAL
;
2056 ts
->kind
= gfc_default_logical_kind
;
2060 m
= gfc_match (" type ( %n )", name
);
2064 /* Search for the name but allow the components to be defined later. */
2065 if (gfc_get_ha_symbol (name
, &sym
))
2067 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2071 if (sym
->attr
.flavor
!= FL_DERIVED
2072 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2075 ts
->type
= BT_DERIVED
;
2082 /* For all types except double, derived and character, look for an
2083 optional kind specifier. MATCH_NO is actually OK at this point. */
2084 if (implicit_flag
== 1)
2087 if (gfc_current_form
== FORM_FREE
)
2089 c
= gfc_peek_char();
2090 if (!gfc_is_whitespace(c
) && c
!= '*' && c
!= '('
2091 && c
!= ':' && c
!= ',')
2095 m
= gfc_match_kind_spec (ts
);
2096 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2097 m
= gfc_match_old_kind_spec (ts
);
2100 m
= MATCH_YES
; /* No kind specifier found. */
2106 /* Match an IMPLICIT NONE statement. Actually, this statement is
2107 already matched in parse.c, or we would not end up here in the
2108 first place. So the only thing we need to check, is if there is
2109 trailing garbage. If not, the match is successful. */
2112 gfc_match_implicit_none (void)
2114 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2118 /* Match the letter range(s) of an IMPLICIT statement. */
2121 match_implicit_range (void)
2123 int c
, c1
, c2
, inner
;
2126 cur_loc
= gfc_current_locus
;
2128 gfc_gobble_whitespace ();
2129 c
= gfc_next_char ();
2132 gfc_error ("Missing character range in IMPLICIT at %C");
2139 gfc_gobble_whitespace ();
2140 c1
= gfc_next_char ();
2144 gfc_gobble_whitespace ();
2145 c
= gfc_next_char ();
2150 inner
= 0; /* Fall through. */
2157 gfc_gobble_whitespace ();
2158 c2
= gfc_next_char ();
2162 gfc_gobble_whitespace ();
2163 c
= gfc_next_char ();
2165 if ((c
!= ',') && (c
!= ')'))
2178 gfc_error ("Letters must be in alphabetic order in "
2179 "IMPLICIT statement at %C");
2183 /* See if we can add the newly matched range to the pending
2184 implicits from this IMPLICIT statement. We do not check for
2185 conflicts with whatever earlier IMPLICIT statements may have
2186 set. This is done when we've successfully finished matching
2188 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2195 gfc_syntax_error (ST_IMPLICIT
);
2197 gfc_current_locus
= cur_loc
;
2202 /* Match an IMPLICIT statement, storing the types for
2203 gfc_set_implicit() if the statement is accepted by the parser.
2204 There is a strange looking, but legal syntactic construction
2205 possible. It looks like:
2207 IMPLICIT INTEGER (a-b) (c-d)
2209 This is legal if "a-b" is a constant expression that happens to
2210 equal one of the legal kinds for integers. The real problem
2211 happens with an implicit specification that looks like:
2213 IMPLICIT INTEGER (a-b)
2215 In this case, a typespec matcher that is "greedy" (as most of the
2216 matchers are) gobbles the character range as a kindspec, leaving
2217 nothing left. We therefore have to go a bit more slowly in the
2218 matching process by inhibiting the kindspec checking during
2219 typespec matching and checking for a kind later. */
2222 gfc_match_implicit (void)
2229 /* We don't allow empty implicit statements. */
2230 if (gfc_match_eos () == MATCH_YES
)
2232 gfc_error ("Empty IMPLICIT statement at %C");
2238 /* First cleanup. */
2239 gfc_clear_new_implicit ();
2241 /* A basic type is mandatory here. */
2242 m
= match_type_spec (&ts
, 1);
2243 if (m
== MATCH_ERROR
)
2248 cur_loc
= gfc_current_locus
;
2249 m
= match_implicit_range ();
2253 /* We may have <TYPE> (<RANGE>). */
2254 gfc_gobble_whitespace ();
2255 c
= gfc_next_char ();
2256 if ((c
== '\n') || (c
== ','))
2258 /* Check for CHARACTER with no length parameter. */
2259 if (ts
.type
== BT_CHARACTER
&& !ts
.cl
)
2261 ts
.kind
= gfc_default_character_kind
;
2262 ts
.cl
= gfc_get_charlen ();
2263 ts
.cl
->next
= gfc_current_ns
->cl_list
;
2264 gfc_current_ns
->cl_list
= ts
.cl
;
2265 ts
.cl
->length
= gfc_int_expr (1);
2268 /* Record the Successful match. */
2269 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2274 gfc_current_locus
= cur_loc
;
2277 /* Discard the (incorrectly) matched range. */
2278 gfc_clear_new_implicit ();
2280 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2281 if (ts
.type
== BT_CHARACTER
)
2282 m
= match_char_spec (&ts
);
2285 m
= gfc_match_kind_spec (&ts
);
2288 m
= gfc_match_old_kind_spec (&ts
);
2289 if (m
== MATCH_ERROR
)
2295 if (m
== MATCH_ERROR
)
2298 m
= match_implicit_range ();
2299 if (m
== MATCH_ERROR
)
2304 gfc_gobble_whitespace ();
2305 c
= gfc_next_char ();
2306 if ((c
!= '\n') && (c
!= ','))
2309 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2317 gfc_syntax_error (ST_IMPLICIT
);
2325 gfc_match_import (void)
2327 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2332 if (gfc_current_ns
->proc_name
== NULL
2333 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2335 gfc_error ("IMPORT statement at %C only permitted in "
2336 "an INTERFACE body");
2340 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2344 if (gfc_match_eos () == MATCH_YES
)
2346 /* All host variables should be imported. */
2347 gfc_current_ns
->has_import_set
= 1;
2351 if (gfc_match (" ::") == MATCH_YES
)
2353 if (gfc_match_eos () == MATCH_YES
)
2355 gfc_error ("Expecting list of named entities at %C");
2362 m
= gfc_match (" %n", name
);
2366 if (gfc_current_ns
->parent
!= NULL
2367 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2369 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2372 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2373 && gfc_find_symbol (name
,
2374 gfc_current_ns
->proc_name
->ns
->parent
,
2377 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2383 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2384 "at %C - does not exist.", name
);
2388 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
2390 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2395 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
2398 sym
->ns
= gfc_current_ns
;
2410 if (gfc_match_eos () == MATCH_YES
)
2412 if (gfc_match_char (',') != MATCH_YES
)
2419 gfc_error ("Syntax error in IMPORT statement at %C");
2424 /* Matches an attribute specification including array specs. If
2425 successful, leaves the variables current_attr and current_as
2426 holding the specification. Also sets the colon_seen variable for
2427 later use by matchers associated with initializations.
2429 This subroutine is a little tricky in the sense that we don't know
2430 if we really have an attr-spec until we hit the double colon.
2431 Until that time, we can only return MATCH_NO. This forces us to
2432 check for duplicate specification at this level. */
2435 match_attr_spec (void)
2437 /* Modifiers that can exist in a type statement. */
2439 { GFC_DECL_BEGIN
= 0,
2440 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
2441 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
2442 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
2443 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
2444 DECL_IS_BIND_C
, DECL_COLON
, DECL_NONE
,
2445 GFC_DECL_END
/* Sentinel */
2449 /* GFC_DECL_END is the sentinel, index starts at 0. */
2450 #define NUM_DECL GFC_DECL_END
2452 static mstring decls
[] = {
2453 minit (", allocatable", DECL_ALLOCATABLE
),
2454 minit (", dimension", DECL_DIMENSION
),
2455 minit (", external", DECL_EXTERNAL
),
2456 minit (", intent ( in )", DECL_IN
),
2457 minit (", intent ( out )", DECL_OUT
),
2458 minit (", intent ( in out )", DECL_INOUT
),
2459 minit (", intrinsic", DECL_INTRINSIC
),
2460 minit (", optional", DECL_OPTIONAL
),
2461 minit (", parameter", DECL_PARAMETER
),
2462 minit (", pointer", DECL_POINTER
),
2463 minit (", protected", DECL_PROTECTED
),
2464 minit (", private", DECL_PRIVATE
),
2465 minit (", public", DECL_PUBLIC
),
2466 minit (", save", DECL_SAVE
),
2467 minit (", target", DECL_TARGET
),
2468 minit (", value", DECL_VALUE
),
2469 minit (", volatile", DECL_VOLATILE
),
2470 minit ("::", DECL_COLON
),
2471 minit (NULL
, DECL_NONE
)
2474 locus start
, seen_at
[NUM_DECL
];
2482 gfc_clear_attr (¤t_attr
);
2483 start
= gfc_current_locus
;
2488 /* See if we get all of the keywords up to the final double colon. */
2489 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2494 d
= (decl_types
) gfc_match_strings (decls
);
2498 /* See if we can find the bind(c) since all else failed.
2499 We need to skip over any whitespace and stop on the ','. */
2500 gfc_gobble_whitespace ();
2501 peek_char
= gfc_peek_char ();
2502 if (peek_char
== ',')
2504 /* Chomp the comma. */
2505 peek_char
= gfc_next_char ();
2506 /* Try and match the bind(c). */
2507 if (gfc_match_bind_c (NULL
) == MATCH_YES
)
2516 if (d
== DECL_NONE
|| d
== DECL_COLON
)
2520 seen_at
[d
] = gfc_current_locus
;
2522 if (d
== DECL_DIMENSION
)
2524 m
= gfc_match_array_spec (¤t_as
);
2528 gfc_error ("Missing dimension specification at %C");
2532 if (m
== MATCH_ERROR
)
2537 /* No double colon, so assume that we've been looking at something
2538 else the whole time. */
2545 /* Since we've seen a double colon, we have to be looking at an
2546 attr-spec. This means that we can now issue errors. */
2547 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2552 case DECL_ALLOCATABLE
:
2553 attr
= "ALLOCATABLE";
2555 case DECL_DIMENSION
:
2562 attr
= "INTENT (IN)";
2565 attr
= "INTENT (OUT)";
2568 attr
= "INTENT (IN OUT)";
2570 case DECL_INTRINSIC
:
2576 case DECL_PARAMETER
:
2582 case DECL_PROTECTED
:
2597 case DECL_IS_BIND_C
:
2607 attr
= NULL
; /* This shouldn't happen. */
2610 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
2615 /* Now that we've dealt with duplicate attributes, add the attributes
2616 to the current attribute. */
2617 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2622 if (gfc_current_state () == COMP_DERIVED
2623 && d
!= DECL_DIMENSION
&& d
!= DECL_POINTER
2624 && d
!= DECL_COLON
&& d
!= DECL_PRIVATE
2625 && d
!= DECL_PUBLIC
&& d
!= DECL_NONE
)
2627 if (d
== DECL_ALLOCATABLE
)
2629 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
2630 "attribute at %C in a TYPE definition")
2639 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2646 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
2647 && gfc_current_state () != COMP_MODULE
)
2649 if (d
== DECL_PRIVATE
)
2653 if (gfc_current_state () == COMP_DERIVED
2654 && gfc_state_stack
->previous
2655 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
2657 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
2658 "at %L in a TYPE definition", attr
,
2668 gfc_error ("%s attribute at %L is not allowed outside of the "
2669 "specification part of a module", attr
, &seen_at
[d
]);
2677 case DECL_ALLOCATABLE
:
2678 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
2681 case DECL_DIMENSION
:
2682 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
2686 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
2690 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
2694 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
2698 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
2701 case DECL_INTRINSIC
:
2702 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
2706 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
2709 case DECL_PARAMETER
:
2710 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
2714 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
2717 case DECL_PROTECTED
:
2718 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
2720 gfc_error ("PROTECTED at %C only allowed in specification "
2721 "part of a module");
2726 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
2731 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
2735 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
2740 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
2745 t
= gfc_add_save (¤t_attr
, NULL
, &seen_at
[d
]);
2749 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
2752 case DECL_IS_BIND_C
:
2753 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
2757 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
2762 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
2766 if (gfc_notify_std (GFC_STD_F2003
,
2767 "Fortran 2003: VOLATILE attribute at %C")
2771 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
2775 gfc_internal_error ("match_attr_spec(): Bad attribute");
2789 gfc_current_locus
= start
;
2790 gfc_free_array_spec (current_as
);
2796 /* Set the binding label, dest_label, either with the binding label
2797 stored in the given gfc_typespec, ts, or if none was provided, it
2798 will be the symbol name in all lower case, as required by the draft
2799 (J3/04-007, section 15.4.1). If a binding label was given and
2800 there is more than one argument (num_idents), it is an error. */
2803 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
2805 if (curr_binding_label
[0] != '\0')
2807 if (num_idents
> 1 || num_idents_on_line
> 1)
2809 gfc_error ("Multiple identifiers provided with "
2810 "single NAME= specifier at %C");
2814 /* Binding label given; store in temp holder til have sym. */
2815 strncpy (dest_label
, curr_binding_label
,
2816 strlen (curr_binding_label
) + 1);
2820 /* No binding label given, and the NAME= specifier did not exist,
2821 which means there was no NAME="". */
2822 if (sym_name
!= NULL
&& has_name_equals
== 0)
2823 strncpy (dest_label
, sym_name
, strlen (sym_name
) + 1);
2830 /* Set the status of the given common block as being BIND(C) or not,
2831 depending on the given parameter, is_bind_c. */
2834 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
2836 com_block
->is_bind_c
= is_bind_c
;
2841 /* Verify that the given gfc_typespec is for a C interoperable type. */
2844 verify_c_interop (gfc_typespec
*ts
, const char *name
, locus
*where
)
2848 /* Make sure the kind used is appropriate for the type.
2849 The f90_type is unknown if an integer constant was
2850 used (e.g., real(4), bind(c) :: myFloat). */
2851 if (ts
->f90_type
!= BT_UNKNOWN
)
2853 t
= gfc_validate_c_kind (ts
);
2856 /* Print an error, but continue parsing line. */
2857 gfc_error_now ("C kind parameter is for type %s but "
2858 "symbol '%s' at %L is of type %s",
2859 gfc_basic_typename (ts
->f90_type
),
2861 gfc_basic_typename (ts
->type
));
2865 /* Make sure the kind is C interoperable. This does not care about the
2866 possible error above. */
2867 if (ts
->type
== BT_DERIVED
&& ts
->derived
!= NULL
)
2868 return (ts
->derived
->ts
.is_c_interop
? SUCCESS
: FAILURE
);
2869 else if (ts
->is_c_interop
!= 1)
2876 /* Verify that the variables of a given common block, which has been
2877 defined with the attribute specifier bind(c), to be of a C
2878 interoperable type. Errors will be reported here, if
2882 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
2884 gfc_symbol
*curr_sym
= NULL
;
2885 try retval
= SUCCESS
;
2887 curr_sym
= com_block
->head
;
2889 /* Make sure we have at least one symbol. */
2890 if (curr_sym
== NULL
)
2893 /* Here we know we have a symbol, so we'll execute this loop
2897 /* The second to last param, 1, says this is in a common block. */
2898 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
2899 curr_sym
= curr_sym
->common_next
;
2900 } while (curr_sym
!= NULL
);
2906 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2907 an appropriate error message is reported. */
2910 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
2911 int is_in_common
, gfc_common_head
*com_block
)
2913 try retval
= SUCCESS
;
2915 /* Here, we know we have the bind(c) attribute, so if we have
2916 enough type info, then verify that it's a C interop kind.
2917 The info could be in the symbol already, or possibly still in
2918 the given ts (current_ts), so look in both. */
2919 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
2921 if (verify_c_interop (&(tmp_sym
->ts
), tmp_sym
->name
,
2922 &(tmp_sym
->declared_at
)) != SUCCESS
)
2924 /* See if we're dealing with a sym in a common block or not. */
2925 if (is_in_common
== 1)
2927 gfc_warning ("Variable '%s' in common block '%s' at %L "
2928 "may not be a C interoperable "
2929 "kind though common block '%s' is BIND(C)",
2930 tmp_sym
->name
, com_block
->name
,
2931 &(tmp_sym
->declared_at
), com_block
->name
);
2935 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
2936 gfc_error ("Type declaration '%s' at %L is not C "
2937 "interoperable but it is BIND(C)",
2938 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2940 gfc_warning ("Variable '%s' at %L "
2941 "may not be a C interoperable "
2942 "kind but it is bind(c)",
2943 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2947 /* Variables declared w/in a common block can't be bind(c)
2948 since there's no way for C to see these variables, so there's
2949 semantically no reason for the attribute. */
2950 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
2952 gfc_error ("Variable '%s' in common block '%s' at "
2953 "%L cannot be declared with BIND(C) "
2954 "since it is not a global",
2955 tmp_sym
->name
, com_block
->name
,
2956 &(tmp_sym
->declared_at
));
2960 /* Scalar variables that are bind(c) can not have the pointer
2961 or allocatable attributes. */
2962 if (tmp_sym
->attr
.is_bind_c
== 1)
2964 if (tmp_sym
->attr
.pointer
== 1)
2966 gfc_error ("Variable '%s' at %L cannot have both the "
2967 "POINTER and BIND(C) attributes",
2968 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2972 if (tmp_sym
->attr
.allocatable
== 1)
2974 gfc_error ("Variable '%s' at %L cannot have both the "
2975 "ALLOCATABLE and BIND(C) attributes",
2976 tmp_sym
->name
, &(tmp_sym
->declared_at
));
2980 /* If it is a BIND(C) function, make sure the return value is a
2981 scalar value. The previous tests in this function made sure
2982 the type is interoperable. */
2983 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->as
!= NULL
)
2984 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2985 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
2987 /* BIND(C) functions can not return a character string. */
2988 if (tmp_sym
->attr
.function
== 1 && tmp_sym
->ts
.type
== BT_CHARACTER
)
2989 if (tmp_sym
->ts
.cl
== NULL
|| tmp_sym
->ts
.cl
->length
== NULL
2990 || tmp_sym
->ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
2991 || mpz_cmp_si (tmp_sym
->ts
.cl
->length
->value
.integer
, 1) != 0)
2992 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2993 "be a character string", tmp_sym
->name
,
2994 &(tmp_sym
->declared_at
));
2998 /* See if the symbol has been marked as private. If it has, make sure
2999 there is no binding label and warn the user if there is one. */
3000 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3001 && tmp_sym
->binding_label
[0] != '\0')
3002 /* Use gfc_warning_now because we won't say that the symbol fails
3003 just because of this. */
3004 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3005 "given the binding label '%s'", tmp_sym
->name
,
3006 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3012 /* Set the appropriate fields for a symbol that's been declared as
3013 BIND(C) (the is_bind_c flag and the binding label), and verify that
3014 the type is C interoperable. Errors are reported by the functions
3015 used to set/test these fields. */
3018 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3020 try retval
= SUCCESS
;
3022 /* TODO: Do we need to make sure the vars aren't marked private? */
3024 /* Set the is_bind_c bit in symbol_attribute. */
3025 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3027 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3028 num_idents
) != SUCCESS
)
3035 /* Set the fields marking the given common block as BIND(C), including
3036 a binding label, and report any errors encountered. */
3039 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3041 try retval
= SUCCESS
;
3043 /* destLabel, common name, typespec (which may have binding label). */
3044 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3048 /* Set the given common block (com_block) to being bind(c) (1). */
3049 set_com_block_bind_c (com_block
, 1);
3055 /* Retrieve the list of one or more identifiers that the given bind(c)
3056 attribute applies to. */
3059 get_bind_c_idents (void)
3061 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3063 gfc_symbol
*tmp_sym
= NULL
;
3065 gfc_common_head
*com_block
= NULL
;
3067 if (gfc_match_name (name
) == MATCH_YES
)
3069 found_id
= MATCH_YES
;
3070 gfc_get_ha_symbol (name
, &tmp_sym
);
3072 else if (match_common_name (name
) == MATCH_YES
)
3074 found_id
= MATCH_YES
;
3075 com_block
= gfc_get_common (name
, 0);
3079 gfc_error ("Need either entity or common block name for "
3080 "attribute specification statement at %C");
3084 /* Save the current identifier and look for more. */
3087 /* Increment the number of identifiers found for this spec stmt. */
3090 /* Make sure we have a sym or com block, and verify that it can
3091 be bind(c). Set the appropriate field(s) and look for more
3093 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3095 if (tmp_sym
!= NULL
)
3097 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3103 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3108 /* Look to see if we have another identifier. */
3110 if (gfc_match_eos () == MATCH_YES
)
3111 found_id
= MATCH_NO
;
3112 else if (gfc_match_char (',') != MATCH_YES
)
3113 found_id
= MATCH_NO
;
3114 else if (gfc_match_name (name
) == MATCH_YES
)
3116 found_id
= MATCH_YES
;
3117 gfc_get_ha_symbol (name
, &tmp_sym
);
3119 else if (match_common_name (name
) == MATCH_YES
)
3121 found_id
= MATCH_YES
;
3122 com_block
= gfc_get_common (name
, 0);
3126 gfc_error ("Missing entity or common block name for "
3127 "attribute specification statement at %C");
3133 gfc_internal_error ("Missing symbol");
3135 } while (found_id
== MATCH_YES
);
3137 /* if we get here we were successful */
3142 /* Try and match a BIND(C) attribute specification statement. */
3145 gfc_match_bind_c_stmt (void)
3147 match found_match
= MATCH_NO
;
3152 /* This may not be necessary. */
3154 /* Clear the temporary binding label holder. */
3155 curr_binding_label
[0] = '\0';
3157 /* Look for the bind(c). */
3158 found_match
= gfc_match_bind_c (NULL
);
3160 if (found_match
== MATCH_YES
)
3162 /* Look for the :: now, but it is not required. */
3165 /* Get the identifier(s) that needs to be updated. This may need to
3166 change to hand the flag(s) for the attr specified so all identifiers
3167 found can have all appropriate parts updated (assuming that the same
3168 spec stmt can have multiple attrs, such as both bind(c) and
3170 if (get_bind_c_idents () != SUCCESS
)
3171 /* Error message should have printed already. */
3179 /* Match a data declaration statement. */
3182 gfc_match_data_decl (void)
3188 num_idents_on_line
= 0;
3190 m
= match_type_spec (¤t_ts
, 0);
3194 if (current_ts
.type
== BT_DERIVED
&& gfc_current_state () != COMP_DERIVED
)
3196 sym
= gfc_use_derived (current_ts
.derived
);
3204 current_ts
.derived
= sym
;
3207 m
= match_attr_spec ();
3208 if (m
== MATCH_ERROR
)
3214 if (current_ts
.type
== BT_DERIVED
&& current_ts
.derived
->components
== NULL
)
3217 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
3220 gfc_find_symbol (current_ts
.derived
->name
,
3221 current_ts
.derived
->ns
->parent
, 1, &sym
);
3223 /* Any symbol that we find had better be a type definition
3224 which has its components defined. */
3225 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
3226 && current_ts
.derived
->components
!= NULL
)
3229 /* Now we have an error, which we signal, and then fix up
3230 because the knock-on is plain and simple confusing. */
3231 gfc_error_now ("Derived type at %C has not been previously defined "
3232 "and so cannot appear in a derived type definition");
3233 current_attr
.pointer
= 1;
3238 /* If we have an old-style character declaration, and no new-style
3239 attribute specifications, then there a comma is optional between
3240 the type specification and the variable list. */
3241 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
3242 gfc_match_char (',');
3244 /* Give the types/attributes to symbols that follow. Give the element
3245 a number so that repeat character length expressions can be copied. */
3249 num_idents_on_line
++;
3250 m
= variable_decl (elem
++);
3251 if (m
== MATCH_ERROR
)
3256 if (gfc_match_eos () == MATCH_YES
)
3258 if (gfc_match_char (',') != MATCH_YES
)
3262 if (gfc_error_flag_test () == 0)
3263 gfc_error ("Syntax error in data declaration at %C");
3266 gfc_free_data_all (gfc_current_ns
);
3269 gfc_free_array_spec (current_as
);
3275 /* Match a prefix associated with a function or subroutine
3276 declaration. If the typespec pointer is nonnull, then a typespec
3277 can be matched. Note that if nothing matches, MATCH_YES is
3278 returned (the null string was matched). */
3281 match_prefix (gfc_typespec
*ts
)
3285 gfc_clear_attr (¤t_attr
);
3289 if (!seen_type
&& ts
!= NULL
3290 && match_type_spec (ts
, 0) == MATCH_YES
3291 && gfc_match_space () == MATCH_YES
)
3298 if (gfc_match ("elemental% ") == MATCH_YES
)
3300 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
3306 if (gfc_match ("pure% ") == MATCH_YES
)
3308 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
3314 if (gfc_match ("recursive% ") == MATCH_YES
)
3316 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
3322 /* At this point, the next item is not a prefix. */
3327 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3330 copy_prefix (symbol_attribute
*dest
, locus
*where
)
3332 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
3335 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
3338 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
3345 /* Match a formal argument list. */
3348 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
3350 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
3351 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3357 if (gfc_match_char ('(') != MATCH_YES
)
3364 if (gfc_match_char (')') == MATCH_YES
)
3369 if (gfc_match_char ('*') == MATCH_YES
)
3373 m
= gfc_match_name (name
);
3377 if (gfc_get_symbol (name
, NULL
, &sym
))
3381 p
= gfc_get_formal_arglist ();
3393 /* We don't add the VARIABLE flavor because the name could be a
3394 dummy procedure. We don't apply these attributes to formal
3395 arguments of statement functions. */
3396 if (sym
!= NULL
&& !st_flag
3397 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
3398 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
3404 /* The name of a program unit can be in a different namespace,
3405 so check for it explicitly. After the statement is accepted,
3406 the name is checked for especially in gfc_get_symbol(). */
3407 if (gfc_new_block
!= NULL
&& sym
!= NULL
3408 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
3410 gfc_error ("Name '%s' at %C is the name of the procedure",
3416 if (gfc_match_char (')') == MATCH_YES
)
3419 m
= gfc_match_char (',');
3422 gfc_error ("Unexpected junk in formal argument list at %C");
3428 /* Check for duplicate symbols in the formal argument list. */
3431 for (p
= head
; p
->next
; p
= p
->next
)
3436 for (q
= p
->next
; q
; q
= q
->next
)
3437 if (p
->sym
== q
->sym
)
3439 gfc_error ("Duplicate symbol '%s' in formal argument list "
3440 "at %C", p
->sym
->name
);
3448 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
3458 gfc_free_formal_arglist (head
);
3463 /* Match a RESULT specification following a function declaration or
3464 ENTRY statement. Also matches the end-of-statement. */
3467 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
3469 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3473 if (gfc_match (" result (") != MATCH_YES
)
3476 m
= gfc_match_name (name
);
3480 /* Get the right paren, and that's it because there could be the
3481 bind(c) attribute after the result clause. */
3482 if (gfc_match_char(')') != MATCH_YES
)
3484 /* TODO: should report the missing right paren here. */
3488 if (strcmp (function
->name
, name
) == 0)
3490 gfc_error ("RESULT variable at %C must be different than function name");
3494 if (gfc_get_symbol (name
, NULL
, &r
))
3497 if (gfc_add_flavor (&r
->attr
, FL_VARIABLE
, r
->name
, NULL
) == FAILURE
3498 || gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
3507 /* Match a function suffix, which could be a combination of a result
3508 clause and BIND(C), either one, or neither. The draft does not
3509 require them to come in a specific order. */
3512 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
3514 match is_bind_c
; /* Found bind(c). */
3515 match is_result
; /* Found result clause. */
3516 match found_match
; /* Status of whether we've found a good match. */
3517 int peek_char
; /* Character we're going to peek at. */
3519 /* Initialize to having found nothing. */
3520 found_match
= MATCH_NO
;
3521 is_bind_c
= MATCH_NO
;
3522 is_result
= MATCH_NO
;
3524 /* Get the next char to narrow between result and bind(c). */
3525 gfc_gobble_whitespace ();
3526 peek_char
= gfc_peek_char ();
3531 /* Look for result clause. */
3532 is_result
= match_result (sym
, result
);
3533 if (is_result
== MATCH_YES
)
3535 /* Now see if there is a bind(c) after it. */
3536 is_bind_c
= gfc_match_bind_c (sym
);
3537 /* We've found the result clause and possibly bind(c). */
3538 found_match
= MATCH_YES
;
3541 /* This should only be MATCH_ERROR. */
3542 found_match
= is_result
;
3545 /* Look for bind(c) first. */
3546 is_bind_c
= gfc_match_bind_c (sym
);
3547 if (is_bind_c
== MATCH_YES
)
3549 /* Now see if a result clause followed it. */
3550 is_result
= match_result (sym
, result
);
3551 found_match
= MATCH_YES
;
3555 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3556 found_match
= MATCH_ERROR
;
3560 gfc_error ("Unexpected junk after function declaration at %C");
3561 found_match
= MATCH_ERROR
;
3565 if (is_result
== MATCH_ERROR
|| is_bind_c
== MATCH_ERROR
)
3567 gfc_error ("Error in function suffix at %C");
3571 if (is_bind_c
== MATCH_YES
)
3572 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
3580 /* Match a function declaration. */
3583 gfc_match_function_decl (void)
3585 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3586 gfc_symbol
*sym
, *result
;
3590 match found_match
; /* Status returned by match func. */
3592 if (gfc_current_state () != COMP_NONE
3593 && gfc_current_state () != COMP_INTERFACE
3594 && gfc_current_state () != COMP_CONTAINS
)
3597 gfc_clear_ts (¤t_ts
);
3599 old_loc
= gfc_current_locus
;
3601 m
= match_prefix (¤t_ts
);
3604 gfc_current_locus
= old_loc
;
3608 if (gfc_match ("function% %n", name
) != MATCH_YES
)
3610 gfc_current_locus
= old_loc
;
3613 if (get_proc_name (name
, &sym
, false))
3615 gfc_new_block
= sym
;
3617 m
= gfc_match_formal_arglist (sym
, 0, 0);
3620 gfc_error ("Expected formal argument list in function "
3621 "definition at %C");
3625 else if (m
== MATCH_ERROR
)
3630 /* According to the draft, the bind(c) and result clause can
3631 come in either order after the formal_arg_list (i.e., either
3632 can be first, both can exist together or by themselves or neither
3633 one). Therefore, the match_result can't match the end of the
3634 string, and check for the bind(c) or result clause in either order. */
3635 found_match
= gfc_match_eos ();
3637 /* Make sure that it isn't already declared as BIND(C). If it is, it
3638 must have been marked BIND(C) with a BIND(C) attribute and that is
3639 not allowed for procedures. */
3640 if (sym
->attr
.is_bind_c
== 1)
3642 sym
->attr
.is_bind_c
= 0;
3643 if (sym
->old_symbol
!= NULL
)
3644 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3645 "variables or common blocks",
3646 &(sym
->old_symbol
->declared_at
));
3648 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3649 "variables or common blocks", &gfc_current_locus
);
3652 if (found_match
!= MATCH_YES
)
3654 /* If we haven't found the end-of-statement, look for a suffix. */
3655 suffix_match
= gfc_match_suffix (sym
, &result
);
3656 if (suffix_match
== MATCH_YES
)
3657 /* Need to get the eos now. */
3658 found_match
= gfc_match_eos ();
3660 found_match
= suffix_match
;
3663 if(found_match
!= MATCH_YES
)
3667 /* Make changes to the symbol. */
3670 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3673 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
3674 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
3677 if (current_ts
.type
!= BT_UNKNOWN
&& sym
->ts
.type
!= BT_UNKNOWN
3678 && !sym
->attr
.implicit_type
)
3680 gfc_error ("Function '%s' at %C already has a type of %s", name
,
3681 gfc_basic_typename (sym
->ts
.type
));
3687 sym
->ts
= current_ts
;
3692 result
->ts
= current_ts
;
3693 sym
->result
= result
;
3700 gfc_current_locus
= old_loc
;
3705 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3706 pass the name of the entry, rather than the gfc_current_block name, and
3707 to return false upon finding an existing global entry. */
3710 add_global_entry (const char *name
, int sub
)
3714 s
= gfc_get_gsymbol(name
);
3717 || (s
->type
!= GSYM_UNKNOWN
3718 && s
->type
!= (sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
)))
3719 global_used(s
, NULL
);
3722 s
->type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
3723 s
->where
= gfc_current_locus
;
3731 /* Match an ENTRY statement. */
3734 gfc_match_entry (void)
3739 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3740 gfc_compile_state state
;
3744 bool module_procedure
;
3746 m
= gfc_match_name (name
);
3750 state
= gfc_current_state ();
3751 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
3756 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3759 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3761 case COMP_BLOCK_DATA
:
3762 gfc_error ("ENTRY statement at %C cannot appear within "
3765 case COMP_INTERFACE
:
3766 gfc_error ("ENTRY statement at %C cannot appear within "
3770 gfc_error ("ENTRY statement at %C cannot appear within "
3771 "a DERIVED TYPE block");
3774 gfc_error ("ENTRY statement at %C cannot appear within "
3775 "an IF-THEN block");
3778 gfc_error ("ENTRY statement at %C cannot appear within "
3782 gfc_error ("ENTRY statement at %C cannot appear within "
3786 gfc_error ("ENTRY statement at %C cannot appear within "
3790 gfc_error ("ENTRY statement at %C cannot appear within "
3794 gfc_error ("ENTRY statement at %C cannot appear within "
3795 "a contained subprogram");
3798 gfc_internal_error ("gfc_match_entry(): Bad state");
3803 module_procedure
= gfc_current_ns
->parent
!= NULL
3804 && gfc_current_ns
->parent
->proc_name
3805 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
3808 if (gfc_current_ns
->parent
!= NULL
3809 && gfc_current_ns
->parent
->proc_name
3810 && !module_procedure
)
3812 gfc_error("ENTRY statement at %C cannot appear in a "
3813 "contained procedure");
3817 /* Module function entries need special care in get_proc_name
3818 because previous references within the function will have
3819 created symbols attached to the current namespace. */
3820 if (get_proc_name (name
, &entry
,
3821 gfc_current_ns
->parent
!= NULL
3823 && gfc_current_ns
->proc_name
->attr
.function
))
3826 proc
= gfc_current_block ();
3828 if (state
== COMP_SUBROUTINE
)
3830 /* An entry in a subroutine. */
3831 if (!add_global_entry (name
, 1))
3834 m
= gfc_match_formal_arglist (entry
, 0, 1);
3838 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
3839 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
3844 /* An entry in a function.
3845 We need to take special care because writing
3850 ENTRY f() RESULT (r)
3852 ENTRY f RESULT (r). */
3853 if (!add_global_entry (name
, 0))
3856 old_loc
= gfc_current_locus
;
3857 if (gfc_match_eos () == MATCH_YES
)
3859 gfc_current_locus
= old_loc
;
3860 /* Match the empty argument list, and add the interface to
3862 m
= gfc_match_formal_arglist (entry
, 0, 1);
3865 m
= gfc_match_formal_arglist (entry
, 0, 0);
3872 if (gfc_match_eos () == MATCH_YES
)
3874 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
3875 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
3878 entry
->result
= entry
;
3882 m
= match_result (proc
, &result
);
3884 gfc_syntax_error (ST_ENTRY
);
3888 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
3889 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
3890 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
3894 entry
->result
= result
;
3898 if (gfc_match_eos () != MATCH_YES
)
3900 gfc_syntax_error (ST_ENTRY
);
3904 entry
->attr
.recursive
= proc
->attr
.recursive
;
3905 entry
->attr
.elemental
= proc
->attr
.elemental
;
3906 entry
->attr
.pure
= proc
->attr
.pure
;
3908 el
= gfc_get_entry_list ();
3910 el
->next
= gfc_current_ns
->entries
;
3911 gfc_current_ns
->entries
= el
;
3913 el
->id
= el
->next
->id
+ 1;
3917 new_st
.op
= EXEC_ENTRY
;
3918 new_st
.ext
.entry
= el
;
3924 /* Match a subroutine statement, including optional prefixes. */
3927 gfc_match_subroutine (void)
3929 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3935 if (gfc_current_state () != COMP_NONE
3936 && gfc_current_state () != COMP_INTERFACE
3937 && gfc_current_state () != COMP_CONTAINS
)
3940 m
= match_prefix (NULL
);
3944 m
= gfc_match ("subroutine% %n", name
);
3948 if (get_proc_name (name
, &sym
, false))
3950 gfc_new_block
= sym
;
3952 /* Check what next non-whitespace character is so we can tell if there
3953 where the required parens if we have a BIND(C). */
3954 gfc_gobble_whitespace ();
3955 peek_char
= gfc_peek_char ();
3957 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
3960 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
3963 /* Make sure that it isn't already declared as BIND(C). If it is, it
3964 must have been marked BIND(C) with a BIND(C) attribute and that is
3965 not allowed for procedures. */
3966 if (sym
->attr
.is_bind_c
== 1)
3968 sym
->attr
.is_bind_c
= 0;
3969 if (sym
->old_symbol
!= NULL
)
3970 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3971 "variables or common blocks",
3972 &(sym
->old_symbol
->declared_at
));
3974 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3975 "variables or common blocks", &gfc_current_locus
);
3978 /* Here, we are just checking if it has the bind(c) attribute, and if
3979 so, then we need to make sure it's all correct. If it doesn't,
3980 we still need to continue matching the rest of the subroutine line. */
3981 is_bind_c
= gfc_match_bind_c (sym
);
3982 if (is_bind_c
== MATCH_ERROR
)
3984 /* There was an attempt at the bind(c), but it was wrong. An
3985 error message should have been printed w/in the gfc_match_bind_c
3986 so here we'll just return the MATCH_ERROR. */
3990 if (is_bind_c
== MATCH_YES
)
3992 if (peek_char
!= '(')
3994 gfc_error ("Missing required parentheses before BIND(C) at %C");
3997 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
4002 if (gfc_match_eos () != MATCH_YES
)
4004 gfc_syntax_error (ST_SUBROUTINE
);
4008 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4015 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4016 given, and set the binding label in either the given symbol (if not
4017 NULL), or in the current_ts. The symbol may be NULL becuase we may
4018 encounter the BIND(C) before the declaration itself. Return
4019 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4020 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4021 or MATCH_YES if the specifier was correct and the binding label and
4022 bind(c) fields were set correctly for the given symbol or the
4026 gfc_match_bind_c (gfc_symbol
*sym
)
4028 /* binding label, if exists */
4029 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
4032 int has_name_equals
= 0;
4034 /* Initialize the flag that specifies whether we encountered a NAME=
4035 specifier or not. */
4036 has_name_equals
= 0;
4038 /* Init the first char to nil so we can catch if we don't have
4039 the label (name attr) or the symbol name yet. */
4040 binding_label
[0] = '\0';
4042 /* This much we have to be able to match, in this order, if
4043 there is a bind(c) label. */
4044 if (gfc_match (" bind ( c ") != MATCH_YES
)
4047 /* Now see if there is a binding label, or if we've reached the
4048 end of the bind(c) attribute without one. */
4049 if (gfc_match_char (',') == MATCH_YES
)
4051 if (gfc_match (" name = ") != MATCH_YES
)
4053 gfc_error ("Syntax error in NAME= specifier for binding label "
4055 /* should give an error message here */
4059 has_name_equals
= 1;
4061 /* Get the opening quote. */
4062 double_quote
= MATCH_YES
;
4063 single_quote
= MATCH_YES
;
4064 double_quote
= gfc_match_char ('"');
4065 if (double_quote
!= MATCH_YES
)
4066 single_quote
= gfc_match_char ('\'');
4067 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
4069 gfc_error ("Syntax error in NAME= specifier for binding label "
4074 /* Grab the binding label, using functions that will not lower
4075 case the names automatically. */
4076 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
4079 /* Get the closing quotation. */
4080 if (double_quote
== MATCH_YES
)
4082 if (gfc_match_char ('"') != MATCH_YES
)
4084 gfc_error ("Missing closing quote '\"' for binding label at %C");
4085 /* User started string with '"' so looked to match it. */
4091 if (gfc_match_char ('\'') != MATCH_YES
)
4093 gfc_error ("Missing closing quote '\'' for binding label at %C");
4094 /* User started string with "'" char. */
4100 /* Get the required right paren. */
4101 if (gfc_match_char (')') != MATCH_YES
)
4103 gfc_error ("Missing closing paren for binding label at %C");
4107 /* Save the binding label to the symbol. If sym is null, we're
4108 probably matching the typespec attributes of a declaration and
4109 haven't gotten the name yet, and therefore, no symbol yet. */
4110 if (binding_label
[0] != '\0')
4114 strncpy (sym
->binding_label
, binding_label
,
4115 strlen (binding_label
)+1);
4118 strncpy (curr_binding_label
, binding_label
,
4119 strlen (binding_label
) + 1);
4123 /* No binding label, but if symbol isn't null, we
4124 can set the label for it here. */
4125 /* TODO: If the name= was given and no binding label (name=""), we simply
4126 will let fortran mangle the symbol name as it usually would.
4127 However, this could still let C call it if the user looked up the
4128 symbol in the object file. Should the name set during mangling in
4129 trans-decl.c be marked with characters that are invalid for C to
4131 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
4132 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
4139 /* Return nonzero if we're currently compiling a contained procedure. */
4142 contained_procedure (void)
4146 for (s
=gfc_state_stack
; s
; s
=s
->previous
)
4147 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
4148 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
4154 /* Set the kind of each enumerator. The kind is selected such that it is
4155 interoperable with the corresponding C enumeration type, making
4156 sure that -fshort-enums is honored. */
4161 enumerator_history
*current_history
= NULL
;
4165 if (max_enum
== NULL
|| enum_history
== NULL
)
4168 if (!gfc_option
.fshort_enums
)
4174 kind
= gfc_integer_kinds
[i
++].kind
;
4176 while (kind
< gfc_c_int_kind
4177 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
4180 current_history
= enum_history
;
4181 while (current_history
!= NULL
)
4183 current_history
->sym
->ts
.kind
= kind
;
4184 current_history
= current_history
->next
;
4189 /* Match any of the various end-block statements. Returns the type of
4190 END to the caller. The END INTERFACE, END IF, END DO and END
4191 SELECT statements cannot be replaced by a single END statement. */
4194 gfc_match_end (gfc_statement
*st
)
4196 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4197 gfc_compile_state state
;
4199 const char *block_name
;
4204 old_loc
= gfc_current_locus
;
4205 if (gfc_match ("end") != MATCH_YES
)
4208 state
= gfc_current_state ();
4209 block_name
= gfc_current_block () == NULL
4210 ? NULL
: gfc_current_block ()->name
;
4212 if (state
== COMP_CONTAINS
)
4214 state
= gfc_state_stack
->previous
->state
;
4215 block_name
= gfc_state_stack
->previous
->sym
== NULL
4216 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
4223 *st
= ST_END_PROGRAM
;
4224 target
= " program";
4228 case COMP_SUBROUTINE
:
4229 *st
= ST_END_SUBROUTINE
;
4230 target
= " subroutine";
4231 eos_ok
= !contained_procedure ();
4235 *st
= ST_END_FUNCTION
;
4236 target
= " function";
4237 eos_ok
= !contained_procedure ();
4240 case COMP_BLOCK_DATA
:
4241 *st
= ST_END_BLOCK_DATA
;
4242 target
= " block data";
4247 *st
= ST_END_MODULE
;
4252 case COMP_INTERFACE
:
4253 *st
= ST_END_INTERFACE
;
4254 target
= " interface";
4277 *st
= ST_END_SELECT
;
4283 *st
= ST_END_FORALL
;
4298 last_initializer
= NULL
;
4300 gfc_free_enum_history ();
4304 gfc_error ("Unexpected END statement at %C");
4308 if (gfc_match_eos () == MATCH_YES
)
4312 /* We would have required END [something]. */
4313 gfc_error ("%s statement expected at %L",
4314 gfc_ascii_statement (*st
), &old_loc
);
4321 /* Verify that we've got the sort of end-block that we're expecting. */
4322 if (gfc_match (target
) != MATCH_YES
)
4324 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
4328 /* If we're at the end, make sure a block name wasn't required. */
4329 if (gfc_match_eos () == MATCH_YES
)
4332 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
4333 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
)
4336 if (gfc_current_block () == NULL
)
4339 gfc_error ("Expected block name of '%s' in %s statement at %C",
4340 block_name
, gfc_ascii_statement (*st
));
4345 /* END INTERFACE has a special handler for its several possible endings. */
4346 if (*st
== ST_END_INTERFACE
)
4347 return gfc_match_end_interface ();
4349 /* We haven't hit the end of statement, so what is left must be an
4351 m
= gfc_match_space ();
4353 m
= gfc_match_name (name
);
4356 gfc_error ("Expected terminating name at %C");
4360 if (block_name
== NULL
)
4363 if (strcmp (name
, block_name
) != 0)
4365 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
4366 gfc_ascii_statement (*st
));
4370 if (gfc_match_eos () == MATCH_YES
)
4374 gfc_syntax_error (*st
);
4377 gfc_current_locus
= old_loc
;
4383 /***************** Attribute declaration statements ****************/
4385 /* Set the attribute of a single variable. */
4390 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4398 m
= gfc_match_name (name
);
4402 if (find_special (name
, &sym
))
4405 var_locus
= gfc_current_locus
;
4407 /* Deal with possible array specification for certain attributes. */
4408 if (current_attr
.dimension
4409 || current_attr
.allocatable
4410 || current_attr
.pointer
4411 || current_attr
.target
)
4413 m
= gfc_match_array_spec (&as
);
4414 if (m
== MATCH_ERROR
)
4417 if (current_attr
.dimension
&& m
== MATCH_NO
)
4419 gfc_error ("Missing array specification at %L in DIMENSION "
4420 "statement", &var_locus
);
4425 if ((current_attr
.allocatable
|| current_attr
.pointer
)
4426 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
4428 gfc_error ("Array specification must be deferred at %L", &var_locus
);
4434 /* Update symbol table. DIMENSION attribute is set
4435 in gfc_set_array_spec(). */
4436 if (current_attr
.dimension
== 0
4437 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4443 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
4449 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
4451 /* Fix the array spec. */
4452 m
= gfc_mod_pointee_as (sym
->as
);
4453 if (m
== MATCH_ERROR
)
4457 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
4463 if ((current_attr
.external
|| current_attr
.intrinsic
)
4464 && sym
->attr
.flavor
!= FL_PROCEDURE
4465 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
4474 gfc_free_array_spec (as
);
4479 /* Generic attribute declaration subroutine. Used for attributes that
4480 just have a list of names. */
4487 /* Gobble the optional double colon, by simply ignoring the result
4497 if (gfc_match_eos () == MATCH_YES
)
4503 if (gfc_match_char (',') != MATCH_YES
)
4505 gfc_error ("Unexpected character in variable list at %C");
4515 /* This routine matches Cray Pointer declarations of the form:
4516 pointer ( <pointer>, <pointee> )
4518 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4519 The pointer, if already declared, should be an integer. Otherwise, we
4520 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4521 be either a scalar, or an array declaration. No space is allocated for
4522 the pointee. For the statement
4523 pointer (ipt, ar(10))
4524 any subsequent uses of ar will be translated (in C-notation) as
4525 ar(i) => ((<type> *) ipt)(i)
4526 After gimplification, pointee variable will disappear in the code. */
4529 cray_pointer_decl (void)
4533 gfc_symbol
*cptr
; /* Pointer symbol. */
4534 gfc_symbol
*cpte
; /* Pointee symbol. */
4540 if (gfc_match_char ('(') != MATCH_YES
)
4542 gfc_error ("Expected '(' at %C");
4546 /* Match pointer. */
4547 var_locus
= gfc_current_locus
;
4548 gfc_clear_attr (¤t_attr
);
4549 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
4550 current_ts
.type
= BT_INTEGER
;
4551 current_ts
.kind
= gfc_index_integer_kind
;
4553 m
= gfc_match_symbol (&cptr
, 0);
4556 gfc_error ("Expected variable name at %C");
4560 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
4563 gfc_set_sym_referenced (cptr
);
4565 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
4567 cptr
->ts
.type
= BT_INTEGER
;
4568 cptr
->ts
.kind
= gfc_index_integer_kind
;
4570 else if (cptr
->ts
.type
!= BT_INTEGER
)
4572 gfc_error ("Cray pointer at %C must be an integer");
4575 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
4576 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
4577 " memory addresses require %d bytes",
4578 cptr
->ts
.kind
, gfc_index_integer_kind
);
4580 if (gfc_match_char (',') != MATCH_YES
)
4582 gfc_error ("Expected \",\" at %C");
4586 /* Match Pointee. */
4587 var_locus
= gfc_current_locus
;
4588 gfc_clear_attr (¤t_attr
);
4589 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
4590 current_ts
.type
= BT_UNKNOWN
;
4591 current_ts
.kind
= 0;
4593 m
= gfc_match_symbol (&cpte
, 0);
4596 gfc_error ("Expected variable name at %C");
4600 /* Check for an optional array spec. */
4601 m
= gfc_match_array_spec (&as
);
4602 if (m
== MATCH_ERROR
)
4604 gfc_free_array_spec (as
);
4607 else if (m
== MATCH_NO
)
4609 gfc_free_array_spec (as
);
4613 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
4616 gfc_set_sym_referenced (cpte
);
4618 if (cpte
->as
== NULL
)
4620 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
4621 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4623 else if (as
!= NULL
)
4625 gfc_error ("Duplicate array spec for Cray pointee at %C");
4626 gfc_free_array_spec (as
);
4632 if (cpte
->as
!= NULL
)
4634 /* Fix array spec. */
4635 m
= gfc_mod_pointee_as (cpte
->as
);
4636 if (m
== MATCH_ERROR
)
4640 /* Point the Pointee at the Pointer. */
4641 cpte
->cp_pointer
= cptr
;
4643 if (gfc_match_char (')') != MATCH_YES
)
4645 gfc_error ("Expected \")\" at %C");
4648 m
= gfc_match_char (',');
4650 done
= true; /* Stop searching for more declarations. */
4654 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
4655 || gfc_match_eos () != MATCH_YES
)
4657 gfc_error ("Expected \",\" or end of statement at %C");
4665 gfc_match_external (void)
4668 gfc_clear_attr (¤t_attr
);
4669 current_attr
.external
= 1;
4671 return attr_decl ();
4676 gfc_match_intent (void)
4680 intent
= match_intent_spec ();
4681 if (intent
== INTENT_UNKNOWN
)
4684 gfc_clear_attr (¤t_attr
);
4685 current_attr
.intent
= intent
;
4687 return attr_decl ();
4692 gfc_match_intrinsic (void)
4695 gfc_clear_attr (¤t_attr
);
4696 current_attr
.intrinsic
= 1;
4698 return attr_decl ();
4703 gfc_match_optional (void)
4706 gfc_clear_attr (¤t_attr
);
4707 current_attr
.optional
= 1;
4709 return attr_decl ();
4714 gfc_match_pointer (void)
4716 gfc_gobble_whitespace ();
4717 if (gfc_peek_char () == '(')
4719 if (!gfc_option
.flag_cray_pointer
)
4721 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4725 return cray_pointer_decl ();
4729 gfc_clear_attr (¤t_attr
);
4730 current_attr
.pointer
= 1;
4732 return attr_decl ();
4738 gfc_match_allocatable (void)
4740 gfc_clear_attr (¤t_attr
);
4741 current_attr
.allocatable
= 1;
4743 return attr_decl ();
4748 gfc_match_dimension (void)
4750 gfc_clear_attr (¤t_attr
);
4751 current_attr
.dimension
= 1;
4753 return attr_decl ();
4758 gfc_match_target (void)
4760 gfc_clear_attr (¤t_attr
);
4761 current_attr
.target
= 1;
4763 return attr_decl ();
4767 /* Match the list of entities being specified in a PUBLIC or PRIVATE
4771 access_attr_decl (gfc_statement st
)
4773 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4774 interface_type type
;
4777 gfc_intrinsic_op
operator;
4780 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
4785 m
= gfc_match_generic_spec (&type
, name
, &operator);
4788 if (m
== MATCH_ERROR
)
4793 case INTERFACE_NAMELESS
:
4796 case INTERFACE_GENERIC
:
4797 if (gfc_get_symbol (name
, NULL
, &sym
))
4800 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
4801 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
4802 sym
->name
, NULL
) == FAILURE
)
4807 case INTERFACE_INTRINSIC_OP
:
4808 if (gfc_current_ns
->operator_access
[operator] == ACCESS_UNKNOWN
)
4810 gfc_current_ns
->operator_access
[operator] =
4811 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
4815 gfc_error ("Access specification of the %s operator at %C has "
4816 "already been specified", gfc_op2string (operator));
4822 case INTERFACE_USER_OP
:
4823 uop
= gfc_get_uop (name
);
4825 if (uop
->access
== ACCESS_UNKNOWN
)
4827 uop
->access
= (st
== ST_PUBLIC
)
4828 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
4832 gfc_error ("Access specification of the .%s. operator at %C "
4833 "has already been specified", sym
->name
);
4840 if (gfc_match_char (',') == MATCH_NO
)
4844 if (gfc_match_eos () != MATCH_YES
)
4849 gfc_syntax_error (st
);
4857 gfc_match_protected (void)
4862 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4864 gfc_error ("PROTECTED at %C only allowed in specification "
4865 "part of a module");
4870 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
4874 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
4879 if (gfc_match_eos () == MATCH_YES
)
4884 m
= gfc_match_symbol (&sym
, 0);
4888 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
4901 if (gfc_match_eos () == MATCH_YES
)
4903 if (gfc_match_char (',') != MATCH_YES
)
4910 gfc_error ("Syntax error in PROTECTED statement at %C");
4915 /* The PRIVATE statement is a bit weird in that it can be a attribute
4916 declaration, but also works as a standlone statement inside of a
4917 type declaration or a module. */
4920 gfc_match_private (gfc_statement
*st
)
4923 if (gfc_match ("private") != MATCH_YES
)
4926 if (gfc_current_state () != COMP_MODULE
4927 && (gfc_current_state () != COMP_DERIVED
4928 || !gfc_state_stack
->previous
4929 || gfc_state_stack
->previous
->state
!= COMP_MODULE
))
4931 gfc_error ("PRIVATE statement at %C is only allowed in the "
4932 "specification part of a module");
4936 if (gfc_current_state () == COMP_DERIVED
)
4938 if (gfc_match_eos () == MATCH_YES
)
4944 gfc_syntax_error (ST_PRIVATE
);
4948 if (gfc_match_eos () == MATCH_YES
)
4955 return access_attr_decl (ST_PRIVATE
);
4960 gfc_match_public (gfc_statement
*st
)
4963 if (gfc_match ("public") != MATCH_YES
)
4966 if (gfc_current_state () != COMP_MODULE
)
4968 gfc_error ("PUBLIC statement at %C is only allowed in the "
4969 "specification part of a module");
4973 if (gfc_match_eos () == MATCH_YES
)
4980 return access_attr_decl (ST_PUBLIC
);
4984 /* Workhorse for gfc_match_parameter. */
4993 m
= gfc_match_symbol (&sym
, 0);
4995 gfc_error ("Expected variable name at %C in PARAMETER statement");
5000 if (gfc_match_char ('=') == MATCH_NO
)
5002 gfc_error ("Expected = sign in PARAMETER statement at %C");
5006 m
= gfc_match_init_expr (&init
);
5008 gfc_error ("Expected expression at %C in PARAMETER statement");
5012 if (sym
->ts
.type
== BT_UNKNOWN
5013 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
5019 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
5020 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
5026 if (sym
->ts
.type
== BT_CHARACTER
5027 && sym
->ts
.cl
!= NULL
5028 && sym
->ts
.cl
->length
!= NULL
5029 && sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
5030 && init
->expr_type
== EXPR_CONSTANT
5031 && init
->ts
.type
== BT_CHARACTER
5032 && init
->ts
.kind
== 1)
5033 gfc_set_constant_character_len (
5034 mpz_get_si (sym
->ts
.cl
->length
->value
.integer
), init
, false);
5040 gfc_free_expr (init
);
5045 /* Match a parameter statement, with the weird syntax that these have. */
5048 gfc_match_parameter (void)
5052 if (gfc_match_char ('(') == MATCH_NO
)
5061 if (gfc_match (" )%t") == MATCH_YES
)
5064 if (gfc_match_char (',') != MATCH_YES
)
5066 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5076 /* Save statements have a special syntax. */
5079 gfc_match_save (void)
5081 char n
[GFC_MAX_SYMBOL_LEN
+1];
5086 if (gfc_match_eos () == MATCH_YES
)
5088 if (gfc_current_ns
->seen_save
)
5090 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
5091 "follows previous SAVE statement")
5096 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
5100 if (gfc_current_ns
->save_all
)
5102 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
5103 "blanket SAVE statement")
5112 m
= gfc_match_symbol (&sym
, 0);
5116 if (gfc_add_save (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5128 m
= gfc_match (" / %n /", &n
);
5129 if (m
== MATCH_ERROR
)
5134 c
= gfc_get_common (n
, 0);
5137 gfc_current_ns
->seen_save
= 1;
5140 if (gfc_match_eos () == MATCH_YES
)
5142 if (gfc_match_char (',') != MATCH_YES
)
5149 gfc_error ("Syntax error in SAVE statement at %C");
5155 gfc_match_value (void)
5160 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
5164 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5169 if (gfc_match_eos () == MATCH_YES
)
5174 m
= gfc_match_symbol (&sym
, 0);
5178 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5191 if (gfc_match_eos () == MATCH_YES
)
5193 if (gfc_match_char (',') != MATCH_YES
)
5200 gfc_error ("Syntax error in VALUE statement at %C");
5206 gfc_match_volatile (void)
5211 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
5215 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5220 if (gfc_match_eos () == MATCH_YES
)
5225 /* VOLATILE is special because it can be added to host-associated
5227 m
= gfc_match_symbol (&sym
, 1);
5231 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5244 if (gfc_match_eos () == MATCH_YES
)
5246 if (gfc_match_char (',') != MATCH_YES
)
5253 gfc_error ("Syntax error in VOLATILE statement at %C");
5258 /* Match a module procedure statement. Note that we have to modify
5259 symbols in the parent's namespace because the current one was there
5260 to receive symbols that are in an interface's formal argument list. */
5263 gfc_match_modproc (void)
5265 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5268 gfc_namespace
*module_ns
;
5270 if (gfc_state_stack
->state
!= COMP_INTERFACE
5271 || gfc_state_stack
->previous
== NULL
5272 || current_interface
.type
== INTERFACE_NAMELESS
)
5274 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5279 module_ns
= gfc_current_ns
->parent
;
5280 for (; module_ns
; module_ns
= module_ns
->parent
)
5281 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
5284 if (module_ns
== NULL
)
5289 m
= gfc_match_name (name
);
5295 if (gfc_get_symbol (name
, module_ns
, &sym
))
5298 if (sym
->attr
.proc
!= PROC_MODULE
5299 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
5300 sym
->name
, NULL
) == FAILURE
)
5303 if (gfc_add_interface (sym
) == FAILURE
)
5306 sym
->attr
.mod_proc
= 1;
5308 if (gfc_match_eos () == MATCH_YES
)
5310 if (gfc_match_char (',') != MATCH_YES
)
5317 gfc_syntax_error (ST_MODULE_PROC
);
5322 /* Match the optional attribute specifiers for a type declaration.
5323 Return MATCH_ERROR if an error is encountered in one of the handled
5324 attributes (public, private, bind(c)), MATCH_NO if what's found is
5325 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5326 checking on attribute conflicts needs to be done. */
5329 gfc_get_type_attr_spec (symbol_attribute
*attr
)
5331 /* See if the derived type is marked as private. */
5332 if (gfc_match (" , private") == MATCH_YES
)
5334 if (gfc_current_state () != COMP_MODULE
)
5336 gfc_error ("Derived type at %C can only be PRIVATE in the "
5337 "specification part of a module");
5341 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
5344 else if (gfc_match (" , public") == MATCH_YES
)
5346 if (gfc_current_state () != COMP_MODULE
)
5348 gfc_error ("Derived type at %C can only be PUBLIC in the "
5349 "specification part of a module");
5353 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
5356 else if(gfc_match(" , bind ( c )") == MATCH_YES
)
5358 /* If the type is defined to be bind(c) it then needs to make
5359 sure that all fields are interoperable. This will
5360 need to be a semantic check on the finished derived type.
5361 See 15.2.3 (lines 9-12) of F2003 draft. */
5362 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
5365 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5370 /* If we get here, something matched. */
5375 /* Match the beginning of a derived type declaration. If a type name
5376 was the result of a function, then it is possible to have a symbol
5377 already to be known as a derived type yet have no components. */
5380 gfc_match_derived_decl (void)
5382 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5383 symbol_attribute attr
;
5386 match is_type_attr_spec
= MATCH_NO
;
5388 if (gfc_current_state () == COMP_DERIVED
)
5391 gfc_clear_attr (&attr
);
5395 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
);
5396 if (is_type_attr_spec
== MATCH_ERROR
)
5398 } while (is_type_attr_spec
== MATCH_YES
);
5400 if (gfc_match (" ::") != MATCH_YES
&& attr
.access
!= ACCESS_UNKNOWN
)
5402 gfc_error ("Expected :: in TYPE definition at %C");
5406 m
= gfc_match (" %n%t", name
);
5410 /* Make sure the name isn't the name of an intrinsic type. The
5411 'double {precision,complex}' types don't get past the name
5412 matcher, unless they're written as a single word or in fixed
5414 if (strcmp (name
, "integer") == 0
5415 || strcmp (name
, "real") == 0
5416 || strcmp (name
, "character") == 0
5417 || strcmp (name
, "logical") == 0
5418 || strcmp (name
, "complex") == 0
5419 || strcmp (name
, "doubleprecision") == 0
5420 || strcmp (name
, "doublecomplex") == 0)
5422 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5427 if (gfc_get_symbol (name
, NULL
, &sym
))
5430 if (sym
->ts
.type
!= BT_UNKNOWN
)
5432 gfc_error ("Derived type name '%s' at %C already has a basic type "
5433 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
5437 /* The symbol may already have the derived attribute without the
5438 components. The ways this can happen is via a function
5439 definition, an INTRINSIC statement or a subtype in another
5440 derived type that is a pointer. The first part of the AND clause
5441 is true if a the symbol is not the return value of a function. */
5442 if (sym
->attr
.flavor
!= FL_DERIVED
5443 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
5446 if (sym
->components
!= NULL
)
5448 gfc_error ("Derived type definition of '%s' at %C has already been "
5449 "defined", sym
->name
);
5453 if (attr
.access
!= ACCESS_UNKNOWN
5454 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
5457 /* See if the derived type was labeled as bind(c). */
5458 if (attr
.is_bind_c
!= 0)
5459 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
5461 gfc_new_block
= sym
;
5467 /* Cray Pointees can be declared as:
5468 pointer (ipt, a (n,m,...,*))
5469 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5470 cheat and set a constant bound of 1 for the last dimension, if this
5471 is the case. Since there is no bounds-checking for Cray Pointees,
5472 this will be okay. */
5475 gfc_mod_pointee_as (gfc_array_spec
*as
)
5477 as
->cray_pointee
= true; /* This will be useful to know later. */
5478 if (as
->type
== AS_ASSUMED_SIZE
)
5480 as
->type
= AS_EXPLICIT
;
5481 as
->upper
[as
->rank
- 1] = gfc_int_expr (1);
5482 as
->cp_was_assumed
= true;
5484 else if (as
->type
== AS_ASSUMED_SHAPE
)
5486 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5493 /* Match the enum definition statement, here we are trying to match
5494 the first line of enum definition statement.
5495 Returns MATCH_YES if match is found. */
5498 gfc_match_enum (void)
5502 m
= gfc_match_eos ();
5506 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
5514 /* Match a variable name with an optional initializer. When this
5515 subroutine is called, a variable is expected to be parsed next.
5516 Depending on what is happening at the moment, updates either the
5517 symbol table or the current interface. */
5520 enumerator_decl (void)
5522 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5523 gfc_expr
*initializer
;
5524 gfc_array_spec
*as
= NULL
;
5532 old_locus
= gfc_current_locus
;
5534 /* When we get here, we've just matched a list of attributes and
5535 maybe a type and a double colon. The next thing we expect to see
5536 is the name of the symbol. */
5537 m
= gfc_match_name (name
);
5541 var_locus
= gfc_current_locus
;
5543 /* OK, we've successfully matched the declaration. Now put the
5544 symbol in the current namespace. If we fail to create the symbol,
5546 if (build_sym (name
, NULL
, &as
, &var_locus
) == FAILURE
)
5552 /* The double colon must be present in order to have initializers.
5553 Otherwise the statement is ambiguous with an assignment statement. */
5556 if (gfc_match_char ('=') == MATCH_YES
)
5558 m
= gfc_match_init_expr (&initializer
);
5561 gfc_error ("Expected an initialization expression at %C");
5570 /* If we do not have an initializer, the initialization value of the
5571 previous enumerator (stored in last_initializer) is incremented
5572 by 1 and is used to initialize the current enumerator. */
5573 if (initializer
== NULL
)
5574 initializer
= gfc_enum_initializer (last_initializer
, old_locus
);
5576 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
5578 gfc_error("ENUMERATOR %L not initialized with integer expression",
5581 gfc_free_enum_history ();
5585 /* Store this current initializer, for the next enumerator variable
5586 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5587 use last_initializer below. */
5588 last_initializer
= initializer
;
5589 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
5591 /* Maintain enumerator history. */
5592 gfc_find_symbol (name
, NULL
, 0, &sym
);
5593 create_enum_history (sym
, last_initializer
);
5595 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
5598 /* Free stuff up and return. */
5599 gfc_free_expr (initializer
);
5605 /* Match the enumerator definition statement. */
5608 gfc_match_enumerator_def (void)
5613 gfc_clear_ts (¤t_ts
);
5615 m
= gfc_match (" enumerator");
5619 m
= gfc_match (" :: ");
5620 if (m
== MATCH_ERROR
)
5623 colon_seen
= (m
== MATCH_YES
);
5625 if (gfc_current_state () != COMP_ENUM
)
5627 gfc_error ("ENUM definition statement expected before %C");
5628 gfc_free_enum_history ();
5632 (¤t_ts
)->type
= BT_INTEGER
;
5633 (¤t_ts
)->kind
= gfc_c_int_kind
;
5635 gfc_clear_attr (¤t_attr
);
5636 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
5645 m
= enumerator_decl ();
5646 if (m
== MATCH_ERROR
)
5651 if (gfc_match_eos () == MATCH_YES
)
5653 if (gfc_match_char (',') != MATCH_YES
)
5657 if (gfc_current_state () == COMP_ENUM
)
5659 gfc_free_enum_history ();
5660 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5665 gfc_free_array_spec (current_as
);