1 /* Declaration statement matcher
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "constructor.h"
32 #include "double-int.h"
39 #include "stringpool.h"
41 /* Macros to access allocate memory for gfc_data_variable,
42 gfc_data_value and gfc_data. */
43 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
44 #define gfc_get_data_value() XCNEW (gfc_data_value)
45 #define gfc_get_data() XCNEW (gfc_data)
48 static bool set_binding_label (const char **, const char *, int);
51 /* This flag is set if an old-style length selector is matched
52 during a type-declaration statement. */
54 static int old_char_selector
;
56 /* When variables acquire types and attributes from a declaration
57 statement, they get them from the following static variables. The
58 first part of a declaration sets these variables and the second
59 part copies these into symbol structures. */
61 static gfc_typespec current_ts
;
63 static symbol_attribute current_attr
;
64 static gfc_array_spec
*current_as
;
65 static int colon_seen
;
67 /* The current binding label (if any). */
68 static const char* curr_binding_label
;
69 /* Need to know how many identifiers are on the current data declaration
70 line in case we're given the BIND(C) attribute with a NAME= specifier. */
71 static int num_idents_on_line
;
72 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
73 can supply a name if the curr_binding_label is nil and NAME= was not. */
74 static int has_name_equals
= 0;
76 /* Initializer of the previous enumerator. */
78 static gfc_expr
*last_initializer
;
80 /* History of all the enumerators is maintained, so that
81 kind values of all the enumerators could be updated depending
82 upon the maximum initialized value. */
84 typedef struct enumerator_history
87 gfc_expr
*initializer
;
88 struct enumerator_history
*next
;
92 /* Header of enum history chain. */
94 static enumerator_history
*enum_history
= NULL
;
96 /* Pointer of enum history node containing largest initializer. */
98 static enumerator_history
*max_enum
= NULL
;
100 /* gfc_new_block points to the symbol of a newly matched block. */
102 gfc_symbol
*gfc_new_block
;
104 bool gfc_matching_function
;
107 /********************* DATA statement subroutines *********************/
109 static bool in_match_data
= false;
112 gfc_in_match_data (void)
114 return in_match_data
;
118 set_in_match_data (bool set_value
)
120 in_match_data
= set_value
;
123 /* Free a gfc_data_variable structure and everything beneath it. */
126 free_variable (gfc_data_variable
*p
)
128 gfc_data_variable
*q
;
133 gfc_free_expr (p
->expr
);
134 gfc_free_iterator (&p
->iter
, 0);
135 free_variable (p
->list
);
141 /* Free a gfc_data_value structure and everything beneath it. */
144 free_value (gfc_data_value
*p
)
151 mpz_clear (p
->repeat
);
152 gfc_free_expr (p
->expr
);
158 /* Free a list of gfc_data structures. */
161 gfc_free_data (gfc_data
*p
)
168 free_variable (p
->var
);
169 free_value (p
->value
);
175 /* Free all data in a namespace. */
178 gfc_free_data_all (gfc_namespace
*ns
)
190 /* Reject data parsed since the last restore point was marked. */
193 gfc_reject_data (gfc_namespace
*ns
)
197 while (ns
->data
&& ns
->data
!= ns
->old_data
)
205 static match
var_element (gfc_data_variable
*);
207 /* Match a list of variables terminated by an iterator and a right
211 var_list (gfc_data_variable
*parent
)
213 gfc_data_variable
*tail
, var
;
216 m
= var_element (&var
);
217 if (m
== MATCH_ERROR
)
222 tail
= gfc_get_data_variable ();
229 if (gfc_match_char (',') != MATCH_YES
)
232 m
= gfc_match_iterator (&parent
->iter
, 1);
235 if (m
== MATCH_ERROR
)
238 m
= var_element (&var
);
239 if (m
== MATCH_ERROR
)
244 tail
->next
= gfc_get_data_variable ();
250 if (gfc_match_char (')') != MATCH_YES
)
255 gfc_syntax_error (ST_DATA
);
260 /* Match a single element in a data variable list, which can be a
261 variable-iterator list. */
264 var_element (gfc_data_variable
*new_var
)
269 memset (new_var
, 0, sizeof (gfc_data_variable
));
271 if (gfc_match_char ('(') == MATCH_YES
)
272 return var_list (new_var
);
274 m
= gfc_match_variable (&new_var
->expr
, 0);
278 sym
= new_var
->expr
->symtree
->n
.sym
;
280 /* Symbol should already have an associated type. */
281 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
284 if (!sym
->attr
.function
&& gfc_current_ns
->parent
285 && gfc_current_ns
->parent
== sym
->ns
)
287 gfc_error ("Host associated variable %qs may not be in the DATA "
288 "statement at %C", sym
->name
);
292 if (gfc_current_state () != COMP_BLOCK_DATA
293 && sym
->attr
.in_common
294 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
295 "common block variable %qs in DATA statement at %C",
299 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
306 /* Match the top-level list of data variables. */
309 top_var_list (gfc_data
*d
)
311 gfc_data_variable var
, *tail
, *new_var
;
318 m
= var_element (&var
);
321 if (m
== MATCH_ERROR
)
324 new_var
= gfc_get_data_variable ();
330 tail
->next
= new_var
;
334 if (gfc_match_char ('/') == MATCH_YES
)
336 if (gfc_match_char (',') != MATCH_YES
)
343 gfc_syntax_error (ST_DATA
);
344 gfc_free_data_all (gfc_current_ns
);
350 match_data_constant (gfc_expr
**result
)
352 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
353 gfc_symbol
*sym
, *dt_sym
= NULL
;
358 m
= gfc_match_literal_constant (&expr
, 1);
365 if (m
== MATCH_ERROR
)
368 m
= gfc_match_null (result
);
372 old_loc
= gfc_current_locus
;
374 /* Should this be a structure component, try to match it
375 before matching a name. */
376 m
= gfc_match_rvalue (result
);
377 if (m
== MATCH_ERROR
)
380 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
382 if (!gfc_simplify_expr (*result
, 0))
386 else if (m
== MATCH_YES
)
387 gfc_free_expr (*result
);
389 gfc_current_locus
= old_loc
;
391 m
= gfc_match_name (name
);
395 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
398 if (sym
&& sym
->attr
.generic
)
399 dt_sym
= gfc_find_dt_in_generic (sym
);
402 || (sym
->attr
.flavor
!= FL_PARAMETER
403 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
405 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
409 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
410 return gfc_match_structure_constructor (dt_sym
, result
);
412 /* Check to see if the value is an initialization array expression. */
413 if (sym
->value
->expr_type
== EXPR_ARRAY
)
415 gfc_current_locus
= old_loc
;
417 m
= gfc_match_init_expr (result
);
418 if (m
== MATCH_ERROR
)
423 if (!gfc_simplify_expr (*result
, 0))
426 if ((*result
)->expr_type
== EXPR_CONSTANT
)
430 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
436 *result
= gfc_copy_expr (sym
->value
);
441 /* Match a list of values in a DATA statement. The leading '/' has
442 already been seen at this point. */
445 top_val_list (gfc_data
*data
)
447 gfc_data_value
*new_val
, *tail
;
455 m
= match_data_constant (&expr
);
458 if (m
== MATCH_ERROR
)
461 new_val
= gfc_get_data_value ();
462 mpz_init (new_val
->repeat
);
465 data
->value
= new_val
;
467 tail
->next
= new_val
;
471 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
474 mpz_set_ui (tail
->repeat
, 1);
478 mpz_set (tail
->repeat
, expr
->value
.integer
);
479 gfc_free_expr (expr
);
481 m
= match_data_constant (&tail
->expr
);
484 if (m
== MATCH_ERROR
)
488 if (gfc_match_char ('/') == MATCH_YES
)
490 if (gfc_match_char (',') == MATCH_NO
)
497 gfc_syntax_error (ST_DATA
);
498 gfc_free_data_all (gfc_current_ns
);
503 /* Matches an old style initialization. */
506 match_old_style_init (const char *name
)
513 /* Set up data structure to hold initializers. */
514 gfc_find_sym_tree (name
, NULL
, 0, &st
);
517 newdata
= gfc_get_data ();
518 newdata
->var
= gfc_get_data_variable ();
519 newdata
->var
->expr
= gfc_get_variable_expr (st
);
520 newdata
->where
= gfc_current_locus
;
522 /* Match initial value list. This also eats the terminal '/'. */
523 m
= top_val_list (newdata
);
532 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
536 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
538 /* Mark the variable as having appeared in a data statement. */
539 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
545 /* Chain in namespace list of DATA initializers. */
546 newdata
->next
= gfc_current_ns
->data
;
547 gfc_current_ns
->data
= newdata
;
553 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
554 we are matching a DATA statement and are therefore issuing an error
555 if we encounter something unexpected, if not, we're trying to match
556 an old-style initialization expression of the form INTEGER I /2/. */
559 gfc_match_data (void)
564 set_in_match_data (true);
568 new_data
= gfc_get_data ();
569 new_data
->where
= gfc_current_locus
;
571 m
= top_var_list (new_data
);
575 m
= top_val_list (new_data
);
579 new_data
->next
= gfc_current_ns
->data
;
580 gfc_current_ns
->data
= new_data
;
582 if (gfc_match_eos () == MATCH_YES
)
585 gfc_match_char (','); /* Optional comma */
588 set_in_match_data (false);
592 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
595 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
600 set_in_match_data (false);
601 gfc_free_data (new_data
);
606 /************************ Declaration statements *********************/
609 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
612 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
616 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
617 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
619 gfc_error ("The assumed-rank array at %C shall not have a codimension");
623 if (to
->rank
== 0 && from
->rank
> 0)
625 to
->rank
= from
->rank
;
626 to
->type
= from
->type
;
627 to
->cray_pointee
= from
->cray_pointee
;
628 to
->cp_was_assumed
= from
->cp_was_assumed
;
630 for (i
= 0; i
< to
->corank
; i
++)
632 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
633 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
635 for (i
= 0; i
< from
->rank
; i
++)
639 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
640 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
644 to
->lower
[i
] = from
->lower
[i
];
645 to
->upper
[i
] = from
->upper
[i
];
649 else if (to
->corank
== 0 && from
->corank
> 0)
651 to
->corank
= from
->corank
;
652 to
->cotype
= from
->cotype
;
654 for (i
= 0; i
< from
->corank
; i
++)
658 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
659 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
663 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
664 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
673 /* Match an intent specification. Since this can only happen after an
674 INTENT word, a legal intent-spec must follow. */
677 match_intent_spec (void)
680 if (gfc_match (" ( in out )") == MATCH_YES
)
682 if (gfc_match (" ( in )") == MATCH_YES
)
684 if (gfc_match (" ( out )") == MATCH_YES
)
687 gfc_error ("Bad INTENT specification at %C");
688 return INTENT_UNKNOWN
;
692 /* Matches a character length specification, which is either a
693 specification expression, '*', or ':'. */
696 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
703 if (gfc_match_char ('*') == MATCH_YES
)
706 if (gfc_match_char (':') == MATCH_YES
)
708 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type "
717 m
= gfc_match_expr (expr
);
720 && !gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
723 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
725 if ((*expr
)->value
.function
.actual
726 && (*expr
)->value
.function
.actual
->expr
->symtree
)
729 e
= (*expr
)->value
.function
.actual
->expr
;
730 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
731 && e
->expr_type
== EXPR_VARIABLE
)
733 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
735 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
736 && e
->symtree
->n
.sym
->ts
.u
.cl
737 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
745 gfc_error ("Conflict in attributes of function argument at %C");
750 /* A character length is a '*' followed by a literal integer or a
751 char_len_param_value in parenthesis. */
754 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
760 m
= gfc_match_char ('*');
764 m
= gfc_match_small_literal_int (&length
, NULL
);
765 if (m
== MATCH_ERROR
)
770 if (obsolescent_check
771 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
773 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
777 if (gfc_match_char ('(') == MATCH_NO
)
780 m
= char_len_param_value (expr
, deferred
);
781 if (m
!= MATCH_YES
&& gfc_matching_function
)
787 if (m
== MATCH_ERROR
)
792 if (gfc_match_char (')') == MATCH_NO
)
794 gfc_free_expr (*expr
);
802 gfc_error ("Syntax error in character length specification at %C");
807 /* Special subroutine for finding a symbol. Check if the name is found
808 in the current name space. If not, and we're compiling a function or
809 subroutine and the parent compilation unit is an interface, then check
810 to see if the name we've been given is the name of the interface
811 (located in another namespace). */
814 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
820 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
823 *result
= st
? st
->n
.sym
: NULL
;
827 if (gfc_current_state () != COMP_SUBROUTINE
828 && gfc_current_state () != COMP_FUNCTION
)
831 s
= gfc_state_stack
->previous
;
835 if (s
->state
!= COMP_INTERFACE
)
838 goto end
; /* Nameless interface. */
840 if (strcmp (name
, s
->sym
->name
) == 0)
851 /* Special subroutine for getting a symbol node associated with a
852 procedure name, used in SUBROUTINE and FUNCTION statements. The
853 symbol is created in the parent using with symtree node in the
854 child unit pointing to the symbol. If the current namespace has no
855 parent, then the symbol is just created in the current unit. */
858 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
864 /* Module functions have to be left in their own namespace because
865 they have potentially (almost certainly!) already been referenced.
866 In this sense, they are rather like external functions. This is
867 fixed up in resolve.c(resolve_entries), where the symbol name-
868 space is set to point to the master function, so that the fake
869 result mechanism can work. */
870 if (module_fcn_entry
)
872 /* Present if entry is declared to be a module procedure. */
873 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
876 rc
= gfc_get_symbol (name
, NULL
, result
);
877 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
878 && (*result
)->ts
.type
== BT_UNKNOWN
879 && sym
->attr
.flavor
== FL_UNKNOWN
)
880 /* Pick up the typespec for the entry, if declared in the function
881 body. Note that this symbol is FL_UNKNOWN because it will
882 only have appeared in a type declaration. The local symtree
883 is set to point to the module symbol and a unique symtree
884 to the local version. This latter ensures a correct clearing
887 /* If the ENTRY proceeds its specification, we need to ensure
888 that this does not raise a "has no IMPLICIT type" error. */
889 if (sym
->ts
.type
== BT_UNKNOWN
)
890 sym
->attr
.untyped
= 1;
892 (*result
)->ts
= sym
->ts
;
894 /* Put the symbol in the procedure namespace so that, should
895 the ENTRY precede its specification, the specification
897 (*result
)->ns
= gfc_current_ns
;
899 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
901 st
= gfc_get_unique_symtree (gfc_current_ns
);
906 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
913 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
915 /* Trap another encompassed procedure with the same name. All
916 these conditions are necessary to avoid picking up an entry
917 whose name clashes with that of the encompassing procedure;
918 this is handled using gsymbols to register unique,globally
920 if (sym
->attr
.flavor
!= 0
921 && sym
->attr
.proc
!= 0
922 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
923 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
924 gfc_error_now_1 ("Procedure '%s' at %C is already defined at %L",
925 name
, &sym
->declared_at
);
927 /* Trap a procedure with a name the same as interface in the
928 encompassing scope. */
929 if (sym
->attr
.generic
!= 0
930 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
931 && !sym
->attr
.mod_proc
)
932 gfc_error_now_1 ("Name '%s' at %C is already defined"
933 " as a generic interface at %L",
934 name
, &sym
->declared_at
);
936 /* Trap declarations of attributes in encompassing scope. The
937 signature for this is that ts.kind is set. Legitimate
938 references only set ts.type. */
939 if (sym
->ts
.kind
!= 0
940 && !sym
->attr
.implicit_type
941 && sym
->attr
.proc
== 0
942 && gfc_current_ns
->parent
!= NULL
943 && sym
->attr
.access
== 0
944 && !module_fcn_entry
)
945 gfc_error_now_1 ("Procedure '%s' at %C has an explicit interface "
946 "and must not have attributes declared at %L",
947 name
, &sym
->declared_at
);
950 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
953 /* Module function entries will already have a symtree in
954 the current namespace but will need one at module level. */
955 if (module_fcn_entry
)
957 /* Present if entry is declared to be a module procedure. */
958 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
960 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
963 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
968 /* See if the procedure should be a module procedure. */
970 if (((sym
->ns
->proc_name
!= NULL
971 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
972 && sym
->attr
.proc
!= PROC_MODULE
)
973 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
974 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
981 /* Verify that the given symbol representing a parameter is C
982 interoperable, by checking to see if it was marked as such after
983 its declaration. If the given symbol is not interoperable, a
984 warning is reported, thus removing the need to return the status to
985 the calling function. The standard does not require the user use
986 one of the iso_c_binding named constants to declare an
987 interoperable parameter, but we can't be sure if the param is C
988 interop or not if the user doesn't. For example, integer(4) may be
989 legal Fortran, but doesn't have meaning in C. It may interop with
990 a number of the C types, which causes a problem because the
991 compiler can't know which one. This code is almost certainly not
992 portable, and the user will get what they deserve if the C type
993 across platforms isn't always interoperable with integer(4). If
994 the user had used something like integer(c_int) or integer(c_long),
995 the compiler could have automatically handled the varying sizes
999 gfc_verify_c_interop_param (gfc_symbol
*sym
)
1001 int is_c_interop
= 0;
1004 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1005 Don't repeat the checks here. */
1006 if (sym
->attr
.implicit_type
)
1009 /* For subroutines or functions that are passed to a BIND(C) procedure,
1010 they're interoperable if they're BIND(C) and their params are all
1012 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1014 if (sym
->attr
.is_bind_c
== 0)
1016 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1017 "attribute to be C interoperable", sym
->name
,
1018 &(sym
->declared_at
));
1023 if (sym
->attr
.is_c_interop
== 1)
1024 /* We've already checked this procedure; don't check it again. */
1027 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1032 /* See if we've stored a reference to a procedure that owns sym. */
1033 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1035 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1037 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1039 if (is_c_interop
!= 1)
1041 /* Make personalized messages to give better feedback. */
1042 if (sym
->ts
.type
== BT_DERIVED
)
1043 gfc_error ("Variable %qs at %L is a dummy argument to the "
1044 "BIND(C) procedure %qs but is not C interoperable "
1045 "because derived type %qs is not C interoperable",
1046 sym
->name
, &(sym
->declared_at
),
1047 sym
->ns
->proc_name
->name
,
1048 sym
->ts
.u
.derived
->name
);
1049 else if (sym
->ts
.type
== BT_CLASS
)
1050 gfc_error ("Variable %qs at %L is a dummy argument to the "
1051 "BIND(C) procedure %qs but is not C interoperable "
1052 "because it is polymorphic",
1053 sym
->name
, &(sym
->declared_at
),
1054 sym
->ns
->proc_name
->name
);
1055 else if (warn_c_binding_type
)
1056 gfc_warning (OPT_Wc_binding_type
,
1057 "Variable %qs at %L is a dummy argument of the "
1058 "BIND(C) procedure %qs but may not be C "
1060 sym
->name
, &(sym
->declared_at
),
1061 sym
->ns
->proc_name
->name
);
1064 /* Character strings are only C interoperable if they have a
1066 if (sym
->ts
.type
== BT_CHARACTER
)
1068 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1069 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1070 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1072 gfc_error ("Character argument %qs at %L "
1073 "must be length 1 because "
1074 "procedure %qs is BIND(C)",
1075 sym
->name
, &sym
->declared_at
,
1076 sym
->ns
->proc_name
->name
);
1081 /* We have to make sure that any param to a bind(c) routine does
1082 not have the allocatable, pointer, or optional attributes,
1083 according to J3/04-007, section 5.1. */
1084 if (sym
->attr
.allocatable
== 1
1085 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1086 "ALLOCATABLE attribute in procedure %qs "
1087 "with BIND(C)", sym
->name
,
1088 &(sym
->declared_at
),
1089 sym
->ns
->proc_name
->name
))
1092 if (sym
->attr
.pointer
== 1
1093 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1094 "POINTER attribute in procedure %qs "
1095 "with BIND(C)", sym
->name
,
1096 &(sym
->declared_at
),
1097 sym
->ns
->proc_name
->name
))
1100 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1102 gfc_error ("Scalar variable %qs at %L with POINTER or "
1103 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1104 " supported", sym
->name
, &(sym
->declared_at
),
1105 sym
->ns
->proc_name
->name
);
1109 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1111 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1112 "and the VALUE attribute because procedure %qs "
1113 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1114 sym
->ns
->proc_name
->name
);
1117 else if (sym
->attr
.optional
== 1
1118 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs "
1119 "at %L with OPTIONAL attribute in "
1120 "procedure %qs which is BIND(C)",
1121 sym
->name
, &(sym
->declared_at
),
1122 sym
->ns
->proc_name
->name
))
1125 /* Make sure that if it has the dimension attribute, that it is
1126 either assumed size or explicit shape. Deferred shape is already
1127 covered by the pointer/allocatable attribute. */
1128 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1129 && !gfc_notify_std_1 (GFC_STD_F2008_TS
, "Assumed-shape array '%s' "
1130 "at %L as dummy argument to the BIND(C) "
1131 "procedure '%s' at %L", sym
->name
,
1132 &(sym
->declared_at
),
1133 sym
->ns
->proc_name
->name
,
1134 &(sym
->ns
->proc_name
->declared_at
)))
1144 /* Function called by variable_decl() that adds a name to the symbol table. */
1147 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1148 gfc_array_spec
**as
, locus
*var_locus
)
1150 symbol_attribute attr
;
1153 if (gfc_get_symbol (name
, NULL
, &sym
))
1156 /* Start updating the symbol table. Add basic type attribute if present. */
1157 if (current_ts
.type
!= BT_UNKNOWN
1158 && (sym
->attr
.implicit_type
== 0
1159 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1160 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1163 if (sym
->ts
.type
== BT_CHARACTER
)
1166 sym
->ts
.deferred
= cl_deferred
;
1169 /* Add dimension attribute if present. */
1170 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1174 /* Add attribute to symbol. The copy is so that we can reset the
1175 dimension attribute. */
1176 attr
= current_attr
;
1178 attr
.codimension
= 0;
1180 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1183 /* Finish any work that may need to be done for the binding label,
1184 if it's a bind(c). The bind(c) attr is found before the symbol
1185 is made, and before the symbol name (for data decls), so the
1186 current_ts is holding the binding label, or nothing if the
1187 name= attr wasn't given. Therefore, test here if we're dealing
1188 with a bind(c) and make sure the binding label is set correctly. */
1189 if (sym
->attr
.is_bind_c
== 1)
1191 if (!sym
->binding_label
)
1193 /* Set the binding label and verify that if a NAME= was specified
1194 then only one identifier was in the entity-decl-list. */
1195 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1196 num_idents_on_line
))
1201 /* See if we know we're in a common block, and if it's a bind(c)
1202 common then we need to make sure we're an interoperable type. */
1203 if (sym
->attr
.in_common
== 1)
1205 /* Test the common block object. */
1206 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1207 && sym
->ts
.is_c_interop
!= 1)
1209 gfc_error_now ("Variable %qs in common block %qs at %C "
1210 "must be declared with a C interoperable "
1211 "kind since common block %qs is BIND(C)",
1212 sym
->name
, sym
->common_block
->name
,
1213 sym
->common_block
->name
);
1218 sym
->attr
.implied_index
= 0;
1220 if (sym
->ts
.type
== BT_CLASS
)
1221 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1227 /* Set character constant to the given length. The constant will be padded or
1228 truncated. If we're inside an array constructor without a typespec, we
1229 additionally check that all elements have the same length; check_len -1
1230 means no checking. */
1233 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1238 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1239 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1241 slen
= expr
->value
.character
.length
;
1244 s
= gfc_get_wide_string (len
+ 1);
1245 memcpy (s
, expr
->value
.character
.string
,
1246 MIN (len
, slen
) * sizeof (gfc_char_t
));
1248 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1250 if (warn_character_truncation
&& slen
> len
)
1251 gfc_warning_now (OPT_Wcharacter_truncation
,
1252 "CHARACTER expression at %L is being truncated "
1253 "(%d/%d)", &expr
->where
, slen
, len
);
1255 /* Apply the standard by 'hand' otherwise it gets cleared for
1257 if (check_len
!= -1 && slen
!= check_len
1258 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1259 gfc_error_now ("The CHARACTER elements of the array constructor "
1260 "at %L must have the same length (%d/%d)",
1261 &expr
->where
, slen
, check_len
);
1264 free (expr
->value
.character
.string
);
1265 expr
->value
.character
.string
= s
;
1266 expr
->value
.character
.length
= len
;
1271 /* Function to create and update the enumerator history
1272 using the information passed as arguments.
1273 Pointer "max_enum" is also updated, to point to
1274 enum history node containing largest initializer.
1276 SYM points to the symbol node of enumerator.
1277 INIT points to its enumerator value. */
1280 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1282 enumerator_history
*new_enum_history
;
1283 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1285 new_enum_history
= XCNEW (enumerator_history
);
1287 new_enum_history
->sym
= sym
;
1288 new_enum_history
->initializer
= init
;
1289 new_enum_history
->next
= NULL
;
1291 if (enum_history
== NULL
)
1293 enum_history
= new_enum_history
;
1294 max_enum
= enum_history
;
1298 new_enum_history
->next
= enum_history
;
1299 enum_history
= new_enum_history
;
1301 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1302 new_enum_history
->initializer
->value
.integer
) < 0)
1303 max_enum
= new_enum_history
;
1308 /* Function to free enum kind history. */
1311 gfc_free_enum_history (void)
1313 enumerator_history
*current
= enum_history
;
1314 enumerator_history
*next
;
1316 while (current
!= NULL
)
1318 next
= current
->next
;
1323 enum_history
= NULL
;
1327 /* Function called by variable_decl() that adds an initialization
1328 expression to a symbol. */
1331 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1333 symbol_attribute attr
;
1338 if (find_special (name
, &sym
, false))
1343 /* If this symbol is confirming an implicit parameter type,
1344 then an initialization expression is not allowed. */
1345 if (attr
.flavor
== FL_PARAMETER
1346 && sym
->value
!= NULL
1349 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1356 /* An initializer is required for PARAMETER declarations. */
1357 if (attr
.flavor
== FL_PARAMETER
)
1359 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1365 /* If a variable appears in a DATA block, it cannot have an
1369 gfc_error ("Variable %qs at %C with an initializer already "
1370 "appears in a DATA statement", sym
->name
);
1374 /* Check if the assignment can happen. This has to be put off
1375 until later for derived type variables and procedure pointers. */
1376 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1377 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1378 && !sym
->attr
.proc_pointer
1379 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1382 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1383 && init
->ts
.type
== BT_CHARACTER
)
1385 /* Update symbol character length according initializer. */
1386 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1389 if (sym
->ts
.u
.cl
->length
== NULL
)
1392 /* If there are multiple CHARACTER variables declared on the
1393 same line, we don't want them to share the same length. */
1394 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1396 if (sym
->attr
.flavor
== FL_PARAMETER
)
1398 if (init
->expr_type
== EXPR_CONSTANT
)
1400 clen
= init
->value
.character
.length
;
1401 sym
->ts
.u
.cl
->length
1402 = gfc_get_int_expr (gfc_default_integer_kind
,
1405 else if (init
->expr_type
== EXPR_ARRAY
)
1407 clen
= mpz_get_si (init
->ts
.u
.cl
->length
->value
.integer
);
1408 sym
->ts
.u
.cl
->length
1409 = gfc_get_int_expr (gfc_default_integer_kind
,
1412 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1413 sym
->ts
.u
.cl
->length
=
1414 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1417 /* Update initializer character length according symbol. */
1418 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1420 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1422 if (init
->expr_type
== EXPR_CONSTANT
)
1423 gfc_set_constant_character_len (len
, init
, -1);
1424 else if (init
->expr_type
== EXPR_ARRAY
)
1428 /* Build a new charlen to prevent simplification from
1429 deleting the length before it is resolved. */
1430 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1431 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1433 for (c
= gfc_constructor_first (init
->value
.constructor
);
1434 c
; c
= gfc_constructor_next (c
))
1435 gfc_set_constant_character_len (len
, c
->expr
, -1);
1440 /* If sym is implied-shape, set its upper bounds from init. */
1441 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1442 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1446 if (init
->rank
== 0)
1448 gfc_error ("Can't initialize implied-shape array at %L"
1449 " with scalar", &sym
->declared_at
);
1452 gcc_assert (sym
->as
->rank
== init
->rank
);
1454 /* Shape should be present, we get an initialization expression. */
1455 gcc_assert (init
->shape
);
1457 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1463 lower
= sym
->as
->lower
[dim
];
1464 if (lower
->expr_type
!= EXPR_CONSTANT
)
1466 gfc_error ("Non-constant lower bound in implied-shape"
1467 " declaration at %L", &lower
->where
);
1471 /* All dimensions must be without upper bound. */
1472 gcc_assert (!sym
->as
->upper
[dim
]);
1475 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1476 mpz_add (e
->value
.integer
,
1477 lower
->value
.integer
, init
->shape
[dim
]);
1478 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1479 sym
->as
->upper
[dim
] = e
;
1482 sym
->as
->type
= AS_EXPLICIT
;
1485 /* Need to check if the expression we initialized this
1486 to was one of the iso_c_binding named constants. If so,
1487 and we're a parameter (constant), let it be iso_c.
1489 integer(c_int), parameter :: my_int = c_int
1490 integer(my_int) :: my_int_2
1491 If we mark my_int as iso_c (since we can see it's value
1492 is equal to one of the named constants), then my_int_2
1493 will be considered C interoperable. */
1494 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1496 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1497 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1498 /* attr bits needed for module files. */
1499 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1500 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1501 if (init
->ts
.is_iso_c
)
1502 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1505 /* Add initializer. Make sure we keep the ranks sane. */
1506 if (sym
->attr
.dimension
&& init
->rank
== 0)
1511 if (sym
->attr
.flavor
== FL_PARAMETER
1512 && init
->expr_type
== EXPR_CONSTANT
1513 && spec_size (sym
->as
, &size
)
1514 && mpz_cmp_si (size
, 0) > 0)
1516 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1518 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1519 gfc_constructor_append_expr (&array
->value
.constructor
,
1522 : gfc_copy_expr (init
),
1525 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1526 for (n
= 0; n
< sym
->as
->rank
; n
++)
1527 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1532 init
->rank
= sym
->as
->rank
;
1536 if (sym
->attr
.save
== SAVE_NONE
)
1537 sym
->attr
.save
= SAVE_IMPLICIT
;
1545 /* Function called by variable_decl() that adds a name to a structure
1549 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1550 gfc_array_spec
**as
)
1555 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1556 constructing, it must have the pointer attribute. */
1557 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1558 && current_ts
.u
.derived
== gfc_current_block ()
1559 && current_attr
.pointer
== 0)
1561 gfc_error ("Component at %C must have the POINTER attribute");
1565 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1567 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1569 gfc_error ("Array component of structure at %C must have explicit "
1570 "or deferred shape");
1575 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1579 if (c
->ts
.type
== BT_CHARACTER
)
1581 c
->attr
= current_attr
;
1583 c
->initializer
= *init
;
1590 c
->attr
.codimension
= 1;
1592 c
->attr
.dimension
= 1;
1596 /* Should this ever get more complicated, combine with similar section
1597 in add_init_expr_to_sym into a separate function. */
1598 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1600 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1604 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1605 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1606 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1608 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1610 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1611 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1612 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1613 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1615 gfc_constructor
*ctor
;
1616 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1621 bool has_ts
= (c
->initializer
->ts
.u
.cl
1622 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1624 /* Remember the length of the first element for checking
1625 that all elements *in the constructor* have the same
1626 length. This need not be the length of the LHS! */
1627 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1628 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1629 first_len
= ctor
->expr
->value
.character
.length
;
1631 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1632 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1634 gfc_set_constant_character_len (len
, ctor
->expr
,
1635 has_ts
? -1 : first_len
);
1636 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1642 /* Check array components. */
1643 if (!c
->attr
.dimension
)
1646 if (c
->attr
.pointer
)
1648 if (c
->as
->type
!= AS_DEFERRED
)
1650 gfc_error ("Pointer array component of structure at %C must have a "
1655 else if (c
->attr
.allocatable
)
1657 if (c
->as
->type
!= AS_DEFERRED
)
1659 gfc_error ("Allocatable component of structure at %C must have a "
1666 if (c
->as
->type
!= AS_EXPLICIT
)
1668 gfc_error ("Array component of structure at %C must have an "
1675 if (c
->ts
.type
== BT_CLASS
)
1677 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1687 /* Match a 'NULL()', and possibly take care of some side effects. */
1690 gfc_match_null (gfc_expr
**result
)
1693 match m
, m2
= MATCH_NO
;
1695 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1701 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1703 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1706 old_loc
= gfc_current_locus
;
1707 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1710 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1714 gfc_current_locus
= old_loc
;
1719 /* The NULL symbol now has to be/become an intrinsic function. */
1720 if (gfc_get_symbol ("null", NULL
, &sym
))
1722 gfc_error ("NULL() initialization at %C is ambiguous");
1726 gfc_intrinsic_symbol (sym
);
1728 if (sym
->attr
.proc
!= PROC_INTRINSIC
1729 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1730 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1731 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1734 *result
= gfc_get_null_expr (&gfc_current_locus
);
1736 /* Invalid per F2008, C512. */
1737 if (m2
== MATCH_YES
)
1739 gfc_error ("NULL() initialization at %C may not have MOLD");
1747 /* Match the initialization expr for a data pointer or procedure pointer. */
1750 match_pointer_init (gfc_expr
**init
, int procptr
)
1754 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1756 gfc_error ("Initialization of pointer at %C is not allowed in "
1757 "a PURE procedure");
1760 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
1762 /* Match NULL() initialization. */
1763 m
= gfc_match_null (init
);
1767 /* Match non-NULL initialization. */
1768 gfc_matching_ptr_assignment
= !procptr
;
1769 gfc_matching_procptr_assignment
= procptr
;
1770 m
= gfc_match_rvalue (init
);
1771 gfc_matching_ptr_assignment
= 0;
1772 gfc_matching_procptr_assignment
= 0;
1773 if (m
== MATCH_ERROR
)
1775 else if (m
== MATCH_NO
)
1777 gfc_error ("Error in pointer initialization at %C");
1781 if (!procptr
&& !gfc_resolve_expr (*init
))
1784 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1785 "initialization at %C"))
1793 check_function_name (char *name
)
1795 /* In functions that have a RESULT variable defined, the function name always
1796 refers to function calls. Therefore, the name is not allowed to appear in
1797 specification statements. When checking this, be careful about
1798 'hidden' procedure pointer results ('ppr@'). */
1800 if (gfc_current_state () == COMP_FUNCTION
)
1802 gfc_symbol
*block
= gfc_current_block ();
1803 if (block
&& block
->result
&& block
->result
!= block
1804 && strcmp (block
->result
->name
, "ppr@") != 0
1805 && strcmp (block
->name
, name
) == 0)
1807 gfc_error ("Function name %qs not allowed at %C", name
);
1816 /* Match a variable name with an optional initializer. When this
1817 subroutine is called, a variable is expected to be parsed next.
1818 Depending on what is happening at the moment, updates either the
1819 symbol table or the current interface. */
1822 variable_decl (int elem
)
1824 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1825 gfc_expr
*initializer
, *char_len
;
1827 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1839 /* When we get here, we've just matched a list of attributes and
1840 maybe a type and a double colon. The next thing we expect to see
1841 is the name of the symbol. */
1842 m
= gfc_match_name (name
);
1846 var_locus
= gfc_current_locus
;
1848 /* Now we could see the optional array spec. or character length. */
1849 m
= gfc_match_array_spec (&as
, true, true);
1850 if (m
== MATCH_ERROR
)
1854 as
= gfc_copy_array_spec (current_as
);
1856 && !merge_array_spec (current_as
, as
, true))
1862 if (flag_cray_pointer
)
1863 cp_as
= gfc_copy_array_spec (as
);
1865 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1866 determine (and check) whether it can be implied-shape. If it
1867 was parsed as assumed-size, change it because PARAMETERs can not
1871 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1874 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1879 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1880 && current_attr
.flavor
== FL_PARAMETER
)
1881 as
->type
= AS_IMPLIED_SHAPE
;
1883 if (as
->type
== AS_IMPLIED_SHAPE
1884 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
1894 cl_deferred
= false;
1896 if (current_ts
.type
== BT_CHARACTER
)
1898 switch (match_char_length (&char_len
, &cl_deferred
, false))
1901 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1903 cl
->length
= char_len
;
1906 /* Non-constant lengths need to be copied after the first
1907 element. Also copy assumed lengths. */
1910 && (current_ts
.u
.cl
->length
== NULL
1911 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1913 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1914 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1917 cl
= current_ts
.u
.cl
;
1919 cl_deferred
= current_ts
.deferred
;
1928 /* If this symbol has already shown up in a Cray Pointer declaration,
1929 and this is not a component declaration,
1930 then we want to set the type & bail out. */
1931 if (flag_cray_pointer
&& gfc_current_state () != COMP_DERIVED
)
1933 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1934 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1936 sym
->ts
.type
= current_ts
.type
;
1937 sym
->ts
.kind
= current_ts
.kind
;
1939 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1940 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1941 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1944 /* Check to see if we have an array specification. */
1947 if (sym
->as
!= NULL
)
1949 gfc_error ("Duplicate array spec for Cray pointee at %C");
1950 gfc_free_array_spec (cp_as
);
1956 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
1957 gfc_internal_error ("Couldn't set pointee array spec.");
1959 /* Fix the array spec. */
1960 m
= gfc_mod_pointee_as (sym
->as
);
1961 if (m
== MATCH_ERROR
)
1969 gfc_free_array_spec (cp_as
);
1973 /* Procedure pointer as function result. */
1974 if (gfc_current_state () == COMP_FUNCTION
1975 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1976 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1977 strcpy (name
, "ppr@");
1979 if (gfc_current_state () == COMP_FUNCTION
1980 && strcmp (name
, gfc_current_block ()->name
) == 0
1981 && gfc_current_block ()->result
1982 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1983 strcpy (name
, "ppr@");
1985 /* OK, we've successfully matched the declaration. Now put the
1986 symbol in the current namespace, because it might be used in the
1987 optional initialization expression for this symbol, e.g. this is
1990 integer, parameter :: i = huge(i)
1992 This is only true for parameters or variables of a basic type.
1993 For components of derived types, it is not true, so we don't
1994 create a symbol for those yet. If we fail to create the symbol,
1996 if (gfc_current_state () != COMP_DERIVED
1997 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
2003 if (!check_function_name (name
))
2009 /* We allow old-style initializations of the form
2010 integer i /2/, j(4) /3*3, 1/
2011 (if no colon has been seen). These are different from data
2012 statements in that initializers are only allowed to apply to the
2013 variable immediately preceding, i.e.
2015 is not allowed. Therefore we have to do some work manually, that
2016 could otherwise be left to the matchers for DATA statements. */
2018 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2020 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2021 "initialization at %C"))
2023 else if (gfc_current_state () == COMP_DERIVED
)
2025 gfc_error ("Invalid old style initialization for derived type "
2031 return match_old_style_init (name
);
2034 /* The double colon must be present in order to have initializers.
2035 Otherwise the statement is ambiguous with an assignment statement. */
2038 if (gfc_match (" =>") == MATCH_YES
)
2040 if (!current_attr
.pointer
)
2042 gfc_error ("Initialization at %C isn't for a pointer variable");
2047 m
= match_pointer_init (&initializer
, 0);
2051 else if (gfc_match_char ('=') == MATCH_YES
)
2053 if (current_attr
.pointer
)
2055 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2061 m
= gfc_match_init_expr (&initializer
);
2064 gfc_error ("Expected an initialization expression at %C");
2068 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2069 && gfc_state_stack
->state
!= COMP_DERIVED
)
2071 gfc_error ("Initialization of variable at %C is not allowed in "
2072 "a PURE procedure");
2076 if (current_attr
.flavor
!= FL_PARAMETER
2077 && gfc_state_stack
->state
!= COMP_DERIVED
)
2078 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2085 if (initializer
!= NULL
&& current_attr
.allocatable
2086 && gfc_current_state () == COMP_DERIVED
)
2088 gfc_error ("Initialization of allocatable component at %C is not "
2094 /* Add the initializer. Note that it is fine if initializer is
2095 NULL here, because we sometimes also need to check if a
2096 declaration *must* have an initialization expression. */
2097 if (gfc_current_state () != COMP_DERIVED
)
2098 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2101 if (current_ts
.type
== BT_DERIVED
2102 && !current_attr
.pointer
&& !initializer
)
2103 initializer
= gfc_default_initializer (¤t_ts
);
2104 t
= build_struct (name
, cl
, &initializer
, &as
);
2107 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2110 /* Free stuff up and return. */
2111 gfc_free_expr (initializer
);
2112 gfc_free_array_spec (as
);
2118 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2119 This assumes that the byte size is equal to the kind number for
2120 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2123 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2128 if (gfc_match_char ('*') != MATCH_YES
)
2131 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2135 original_kind
= ts
->kind
;
2137 /* Massage the kind numbers for complex types. */
2138 if (ts
->type
== BT_COMPLEX
)
2142 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2143 gfc_basic_typename (ts
->type
), original_kind
);
2150 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2153 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2157 if (flag_real4_kind
== 8)
2159 if (flag_real4_kind
== 10)
2161 if (flag_real4_kind
== 16)
2167 if (flag_real8_kind
== 4)
2169 if (flag_real8_kind
== 10)
2171 if (flag_real8_kind
== 16)
2176 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2178 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2179 gfc_basic_typename (ts
->type
), original_kind
);
2183 if (!gfc_notify_std (GFC_STD_GNU
,
2184 "Nonstandard type declaration %s*%d at %C",
2185 gfc_basic_typename(ts
->type
), original_kind
))
2192 /* Match a kind specification. Since kinds are generally optional, we
2193 usually return MATCH_NO if something goes wrong. If a "kind="
2194 string is found, then we know we have an error. */
2197 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2209 where
= loc
= gfc_current_locus
;
2214 if (gfc_match_char ('(') == MATCH_NO
)
2217 /* Also gobbles optional text. */
2218 if (gfc_match (" kind = ") == MATCH_YES
)
2221 loc
= gfc_current_locus
;
2224 n
= gfc_match_init_expr (&e
);
2228 if (gfc_matching_function
)
2230 /* The function kind expression might include use associated or
2231 imported parameters and try again after the specification
2233 if (gfc_match_char (')') != MATCH_YES
)
2235 gfc_error ("Missing right parenthesis at %C");
2241 gfc_undo_symbols ();
2246 /* ....or else, the match is real. */
2248 gfc_error ("Expected initialization expression at %C");
2256 gfc_error ("Expected scalar initialization expression at %C");
2261 msg
= gfc_extract_int (e
, &ts
->kind
);
2270 /* Before throwing away the expression, let's see if we had a
2271 C interoperable kind (and store the fact). */
2272 if (e
->ts
.is_c_interop
== 1)
2274 /* Mark this as C interoperable if being declared with one
2275 of the named constants from iso_c_binding. */
2276 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2277 ts
->f90_type
= e
->ts
.f90_type
;
2283 /* Ignore errors to this point, if we've gotten here. This means
2284 we ignore the m=MATCH_ERROR from above. */
2285 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2287 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2288 gfc_basic_typename (ts
->type
));
2289 gfc_current_locus
= where
;
2293 /* Warn if, e.g., c_int is used for a REAL variable, but not
2294 if, e.g., c_double is used for COMPLEX as the standard
2295 explicitly says that the kind type parameter for complex and real
2296 variable is the same, i.e. c_float == c_float_complex. */
2297 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2298 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2299 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2300 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2301 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2302 gfc_basic_typename (ts
->type
));
2304 gfc_gobble_whitespace ();
2305 if ((c
= gfc_next_ascii_char ()) != ')'
2306 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2308 if (ts
->type
== BT_CHARACTER
)
2309 gfc_error ("Missing right parenthesis or comma at %C");
2311 gfc_error ("Missing right parenthesis at %C");
2315 /* All tests passed. */
2318 if(m
== MATCH_ERROR
)
2319 gfc_current_locus
= where
;
2321 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2324 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2328 if (flag_real4_kind
== 8)
2330 if (flag_real4_kind
== 10)
2332 if (flag_real4_kind
== 16)
2338 if (flag_real8_kind
== 4)
2340 if (flag_real8_kind
== 10)
2342 if (flag_real8_kind
== 16)
2347 /* Return what we know from the test(s). */
2352 gfc_current_locus
= where
;
2358 match_char_kind (int * kind
, int * is_iso_c
)
2367 where
= gfc_current_locus
;
2369 n
= gfc_match_init_expr (&e
);
2371 if (n
!= MATCH_YES
&& gfc_matching_function
)
2373 /* The expression might include use-associated or imported
2374 parameters and try again after the specification
2377 gfc_undo_symbols ();
2382 gfc_error ("Expected initialization expression at %C");
2388 gfc_error ("Expected scalar initialization expression at %C");
2393 msg
= gfc_extract_int (e
, kind
);
2394 *is_iso_c
= e
->ts
.is_iso_c
;
2404 /* Ignore errors to this point, if we've gotten here. This means
2405 we ignore the m=MATCH_ERROR from above. */
2406 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2408 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2412 /* All tests passed. */
2415 if (m
== MATCH_ERROR
)
2416 gfc_current_locus
= where
;
2418 /* Return what we know from the test(s). */
2423 gfc_current_locus
= where
;
2428 /* Match the various kind/length specifications in a CHARACTER
2429 declaration. We don't return MATCH_NO. */
2432 gfc_match_char_spec (gfc_typespec
*ts
)
2434 int kind
, seen_length
, is_iso_c
;
2446 /* Try the old-style specification first. */
2447 old_char_selector
= 0;
2449 m
= match_char_length (&len
, &deferred
, true);
2453 old_char_selector
= 1;
2458 m
= gfc_match_char ('(');
2461 m
= MATCH_YES
; /* Character without length is a single char. */
2465 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2466 if (gfc_match (" kind =") == MATCH_YES
)
2468 m
= match_char_kind (&kind
, &is_iso_c
);
2470 if (m
== MATCH_ERROR
)
2475 if (gfc_match (" , len =") == MATCH_NO
)
2478 m
= char_len_param_value (&len
, &deferred
);
2481 if (m
== MATCH_ERROR
)
2488 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2489 if (gfc_match (" len =") == MATCH_YES
)
2491 m
= char_len_param_value (&len
, &deferred
);
2494 if (m
== MATCH_ERROR
)
2498 if (gfc_match_char (')') == MATCH_YES
)
2501 if (gfc_match (" , kind =") != MATCH_YES
)
2504 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2510 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2511 m
= char_len_param_value (&len
, &deferred
);
2514 if (m
== MATCH_ERROR
)
2518 m
= gfc_match_char (')');
2522 if (gfc_match_char (',') != MATCH_YES
)
2525 gfc_match (" kind ="); /* Gobble optional text. */
2527 m
= match_char_kind (&kind
, &is_iso_c
);
2528 if (m
== MATCH_ERROR
)
2534 /* Require a right-paren at this point. */
2535 m
= gfc_match_char (')');
2540 gfc_error ("Syntax error in CHARACTER declaration at %C");
2542 gfc_free_expr (len
);
2546 /* Deal with character functions after USE and IMPORT statements. */
2547 if (gfc_matching_function
)
2549 gfc_free_expr (len
);
2550 gfc_undo_symbols ();
2556 gfc_free_expr (len
);
2560 /* Do some final massaging of the length values. */
2561 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2563 if (seen_length
== 0)
2564 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2569 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2570 ts
->deferred
= deferred
;
2572 /* We have to know if it was a C interoperable kind so we can
2573 do accurate type checking of bind(c) procs, etc. */
2575 /* Mark this as C interoperable if being declared with one
2576 of the named constants from iso_c_binding. */
2577 ts
->is_c_interop
= is_iso_c
;
2578 else if (len
!= NULL
)
2579 /* Here, we might have parsed something such as: character(c_char)
2580 In this case, the parsing code above grabs the c_char when
2581 looking for the length (line 1690, roughly). it's the last
2582 testcase for parsing the kind params of a character variable.
2583 However, it's not actually the length. this seems like it
2585 To see if the user used a C interop kind, test the expr
2586 of the so called length, and see if it's C interoperable. */
2587 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2593 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2594 structure to the matched specification. This is necessary for FUNCTION and
2595 IMPLICIT statements.
2597 If implicit_flag is nonzero, then we don't check for the optional
2598 kind specification. Not doing so is needed for matching an IMPLICIT
2599 statement correctly. */
2602 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2604 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2605 gfc_symbol
*sym
, *dt_sym
;
2608 bool seen_deferred_kind
, matched_type
;
2609 const char *dt_name
;
2611 /* A belt and braces check that the typespec is correctly being treated
2612 as a deferred characteristic association. */
2613 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2614 && (gfc_current_block ()->result
->ts
.kind
== -1)
2615 && (ts
->kind
== -1);
2617 if (seen_deferred_kind
)
2620 /* Clear the current binding label, in case one is given. */
2621 curr_binding_label
= NULL
;
2623 if (gfc_match (" byte") == MATCH_YES
)
2625 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2628 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2630 gfc_error ("BYTE type used at %C "
2631 "is not available on the target machine");
2635 ts
->type
= BT_INTEGER
;
2641 m
= gfc_match (" type (");
2642 matched_type
= (m
== MATCH_YES
);
2645 gfc_gobble_whitespace ();
2646 if (gfc_peek_ascii_char () == '*')
2648 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2650 if (gfc_current_state () == COMP_DERIVED
)
2652 gfc_error ("Assumed type at %C is not allowed for components");
2655 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2658 ts
->type
= BT_ASSUMED
;
2662 m
= gfc_match ("%n", name
);
2663 matched_type
= (m
== MATCH_YES
);
2666 if ((matched_type
&& strcmp ("integer", name
) == 0)
2667 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2669 ts
->type
= BT_INTEGER
;
2670 ts
->kind
= gfc_default_integer_kind
;
2674 if ((matched_type
&& strcmp ("character", name
) == 0)
2675 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2678 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2679 "intrinsic-type-spec at %C"))
2682 ts
->type
= BT_CHARACTER
;
2683 if (implicit_flag
== 0)
2684 m
= gfc_match_char_spec (ts
);
2688 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2694 if ((matched_type
&& strcmp ("real", name
) == 0)
2695 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2698 ts
->kind
= gfc_default_real_kind
;
2703 && (strcmp ("doubleprecision", name
) == 0
2704 || (strcmp ("double", name
) == 0
2705 && gfc_match (" precision") == MATCH_YES
)))
2706 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2709 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2710 "intrinsic-type-spec at %C"))
2712 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2716 ts
->kind
= gfc_default_double_kind
;
2720 if ((matched_type
&& strcmp ("complex", name
) == 0)
2721 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2723 ts
->type
= BT_COMPLEX
;
2724 ts
->kind
= gfc_default_complex_kind
;
2729 && (strcmp ("doublecomplex", name
) == 0
2730 || (strcmp ("double", name
) == 0
2731 && gfc_match (" complex") == MATCH_YES
)))
2732 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2734 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2738 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2739 "intrinsic-type-spec at %C"))
2742 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2745 ts
->type
= BT_COMPLEX
;
2746 ts
->kind
= gfc_default_double_kind
;
2750 if ((matched_type
&& strcmp ("logical", name
) == 0)
2751 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2753 ts
->type
= BT_LOGICAL
;
2754 ts
->kind
= gfc_default_logical_kind
;
2759 m
= gfc_match_char (')');
2762 ts
->type
= BT_DERIVED
;
2765 /* Match CLASS declarations. */
2766 m
= gfc_match (" class ( * )");
2767 if (m
== MATCH_ERROR
)
2769 else if (m
== MATCH_YES
)
2773 ts
->type
= BT_CLASS
;
2774 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2777 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2778 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2780 gfc_set_sym_referenced (upe
);
2782 upe
->ts
.type
= BT_VOID
;
2783 upe
->attr
.unlimited_polymorphic
= 1;
2784 /* This is essential to force the construction of
2785 unlimited polymorphic component class containers. */
2786 upe
->attr
.zero_comp
= 1;
2787 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2788 &gfc_current_locus
))
2793 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2795 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2799 ts
->u
.derived
= upe
;
2803 m
= gfc_match (" class ( %n )", name
);
2806 ts
->type
= BT_CLASS
;
2808 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2812 /* Defer association of the derived type until the end of the
2813 specification block. However, if the derived type can be
2814 found, add it to the typespec. */
2815 if (gfc_matching_function
)
2817 ts
->u
.derived
= NULL
;
2818 if (gfc_current_state () != COMP_INTERFACE
2819 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2821 sym
= gfc_find_dt_in_generic (sym
);
2822 ts
->u
.derived
= sym
;
2827 /* Search for the name but allow the components to be defined later. If
2828 type = -1, this typespec has been seen in a function declaration but
2829 the type could not be accessed at that point. The actual derived type is
2830 stored in a symtree with the first letter of the name capitalized; the
2831 symtree with the all lower-case name contains the associated
2832 generic function. */
2833 dt_name
= gfc_get_string ("%c%s",
2834 (char) TOUPPER ((unsigned char) name
[0]),
2835 (const char*)&name
[1]);
2840 gfc_get_ha_symbol (name
, &sym
);
2841 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2843 gfc_error ("Type name %qs at %C is ambiguous", name
);
2846 if (sym
->generic
&& !dt_sym
)
2847 dt_sym
= gfc_find_dt_in_generic (sym
);
2849 else if (ts
->kind
== -1)
2851 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2852 || gfc_current_ns
->has_import_set
;
2853 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2854 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2856 gfc_error ("Type name %qs at %C is ambiguous", name
);
2859 if (sym
&& sym
->generic
&& !dt_sym
)
2860 dt_sym
= gfc_find_dt_in_generic (sym
);
2867 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2868 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2869 || sym
->attr
.subroutine
)
2871 gfc_error_1 ("Type name '%s' at %C conflicts with previously declared "
2872 "entity at %L, which has the same name", name
,
2877 gfc_save_symbol_data (sym
);
2878 gfc_set_sym_referenced (sym
);
2879 if (!sym
->attr
.generic
2880 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
2883 if (!sym
->attr
.function
2884 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
2889 gfc_interface
*intr
, *head
;
2891 /* Use upper case to save the actual derived-type symbol. */
2892 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2893 dt_sym
->name
= gfc_get_string (sym
->name
);
2894 head
= sym
->generic
;
2895 intr
= gfc_get_interface ();
2897 intr
->where
= gfc_current_locus
;
2899 sym
->generic
= intr
;
2900 sym
->attr
.if_source
= IFSRC_DECL
;
2903 gfc_save_symbol_data (dt_sym
);
2905 gfc_set_sym_referenced (dt_sym
);
2907 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2908 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
2911 ts
->u
.derived
= dt_sym
;
2917 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2918 "intrinsic-type-spec at %C"))
2921 /* For all types except double, derived and character, look for an
2922 optional kind specifier. MATCH_NO is actually OK at this point. */
2923 if (implicit_flag
== 1)
2925 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2931 if (gfc_current_form
== FORM_FREE
)
2933 c
= gfc_peek_ascii_char ();
2934 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2935 && c
!= ':' && c
!= ',')
2937 if (matched_type
&& c
== ')')
2939 gfc_next_ascii_char ();
2946 m
= gfc_match_kind_spec (ts
, false);
2947 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2948 m
= gfc_match_old_kind_spec (ts
);
2950 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2953 /* Defer association of the KIND expression of function results
2954 until after USE and IMPORT statements. */
2955 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2956 || gfc_matching_function
)
2960 m
= MATCH_YES
; /* No kind specifier found. */
2966 /* Match an IMPLICIT NONE statement. Actually, this statement is
2967 already matched in parse.c, or we would not end up here in the
2968 first place. So the only thing we need to check, is if there is
2969 trailing garbage. If not, the match is successful. */
2972 gfc_match_implicit_none (void)
2976 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2978 bool external
= false;
2979 locus cur_loc
= gfc_current_locus
;
2981 if (gfc_current_ns
->seen_implicit_none
2982 || gfc_current_ns
->has_implicit_none_export
)
2984 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
2988 gfc_gobble_whitespace ();
2989 c
= gfc_peek_ascii_char ();
2992 (void) gfc_next_ascii_char ();
2993 if (!gfc_notify_std (GFC_STD_F2015
, "IMPORT NONE with spec list at %C"))
2996 gfc_gobble_whitespace ();
2997 if (gfc_peek_ascii_char () == ')')
2999 (void) gfc_next_ascii_char ();
3005 m
= gfc_match (" %n", name
);
3009 if (strcmp (name
, "type") == 0)
3011 else if (strcmp (name
, "external") == 0)
3016 gfc_gobble_whitespace ();
3017 c
= gfc_next_ascii_char ();
3028 if (gfc_match_eos () != MATCH_YES
)
3031 gfc_set_implicit_none (type
, external
, &cur_loc
);
3037 /* Match the letter range(s) of an IMPLICIT statement. */
3040 match_implicit_range (void)
3046 cur_loc
= gfc_current_locus
;
3048 gfc_gobble_whitespace ();
3049 c
= gfc_next_ascii_char ();
3052 gfc_error ("Missing character range in IMPLICIT at %C");
3059 gfc_gobble_whitespace ();
3060 c1
= gfc_next_ascii_char ();
3064 gfc_gobble_whitespace ();
3065 c
= gfc_next_ascii_char ();
3070 inner
= 0; /* Fall through. */
3077 gfc_gobble_whitespace ();
3078 c2
= gfc_next_ascii_char ();
3082 gfc_gobble_whitespace ();
3083 c
= gfc_next_ascii_char ();
3085 if ((c
!= ',') && (c
!= ')'))
3098 gfc_error ("Letters must be in alphabetic order in "
3099 "IMPLICIT statement at %C");
3103 /* See if we can add the newly matched range to the pending
3104 implicits from this IMPLICIT statement. We do not check for
3105 conflicts with whatever earlier IMPLICIT statements may have
3106 set. This is done when we've successfully finished matching
3108 if (!gfc_add_new_implicit_range (c1
, c2
))
3115 gfc_syntax_error (ST_IMPLICIT
);
3117 gfc_current_locus
= cur_loc
;
3122 /* Match an IMPLICIT statement, storing the types for
3123 gfc_set_implicit() if the statement is accepted by the parser.
3124 There is a strange looking, but legal syntactic construction
3125 possible. It looks like:
3127 IMPLICIT INTEGER (a-b) (c-d)
3129 This is legal if "a-b" is a constant expression that happens to
3130 equal one of the legal kinds for integers. The real problem
3131 happens with an implicit specification that looks like:
3133 IMPLICIT INTEGER (a-b)
3135 In this case, a typespec matcher that is "greedy" (as most of the
3136 matchers are) gobbles the character range as a kindspec, leaving
3137 nothing left. We therefore have to go a bit more slowly in the
3138 matching process by inhibiting the kindspec checking during
3139 typespec matching and checking for a kind later. */
3142 gfc_match_implicit (void)
3149 if (gfc_current_ns
->seen_implicit_none
)
3151 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3158 /* We don't allow empty implicit statements. */
3159 if (gfc_match_eos () == MATCH_YES
)
3161 gfc_error ("Empty IMPLICIT statement at %C");
3167 /* First cleanup. */
3168 gfc_clear_new_implicit ();
3170 /* A basic type is mandatory here. */
3171 m
= gfc_match_decl_type_spec (&ts
, 1);
3172 if (m
== MATCH_ERROR
)
3177 cur_loc
= gfc_current_locus
;
3178 m
= match_implicit_range ();
3182 /* We may have <TYPE> (<RANGE>). */
3183 gfc_gobble_whitespace ();
3184 c
= gfc_peek_ascii_char ();
3185 if (c
== ',' || c
== '\n' || c
== ';' || c
== '!')
3187 /* Check for CHARACTER with no length parameter. */
3188 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3190 ts
.kind
= gfc_default_character_kind
;
3191 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3192 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3196 /* Record the Successful match. */
3197 if (!gfc_merge_new_implicit (&ts
))
3200 c
= gfc_next_ascii_char ();
3201 else if (gfc_match_eos () == MATCH_ERROR
)
3206 gfc_current_locus
= cur_loc
;
3209 /* Discard the (incorrectly) matched range. */
3210 gfc_clear_new_implicit ();
3212 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3213 if (ts
.type
== BT_CHARACTER
)
3214 m
= gfc_match_char_spec (&ts
);
3217 m
= gfc_match_kind_spec (&ts
, false);
3220 m
= gfc_match_old_kind_spec (&ts
);
3221 if (m
== MATCH_ERROR
)
3227 if (m
== MATCH_ERROR
)
3230 m
= match_implicit_range ();
3231 if (m
== MATCH_ERROR
)
3236 gfc_gobble_whitespace ();
3237 c
= gfc_next_ascii_char ();
3238 if (c
!= ',' && gfc_match_eos () != MATCH_YES
)
3241 if (!gfc_merge_new_implicit (&ts
))
3249 gfc_syntax_error (ST_IMPLICIT
);
3257 gfc_match_import (void)
3259 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3264 if (gfc_current_ns
->proc_name
== NULL
3265 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3267 gfc_error ("IMPORT statement at %C only permitted in "
3268 "an INTERFACE body");
3272 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3275 if (gfc_match_eos () == MATCH_YES
)
3277 /* All host variables should be imported. */
3278 gfc_current_ns
->has_import_set
= 1;
3282 if (gfc_match (" ::") == MATCH_YES
)
3284 if (gfc_match_eos () == MATCH_YES
)
3286 gfc_error ("Expecting list of named entities at %C");
3294 m
= gfc_match (" %n", name
);
3298 if (gfc_current_ns
->parent
!= NULL
3299 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3301 gfc_error ("Type name %qs at %C is ambiguous", name
);
3304 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3305 && gfc_find_symbol (name
,
3306 gfc_current_ns
->proc_name
->ns
->parent
,
3309 gfc_error ("Type name %qs at %C is ambiguous", name
);
3315 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3316 "at %C - does not exist.", name
);
3320 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3322 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3327 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3330 sym
->attr
.imported
= 1;
3332 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3334 /* The actual derived type is stored in a symtree with the first
3335 letter of the name capitalized; the symtree with the all
3336 lower-case name contains the associated generic function. */
3337 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3338 gfc_get_string ("%c%s",
3339 (char) TOUPPER ((unsigned char) name
[0]),
3343 sym
->attr
.imported
= 1;
3356 if (gfc_match_eos () == MATCH_YES
)
3358 if (gfc_match_char (',') != MATCH_YES
)
3365 gfc_error ("Syntax error in IMPORT statement at %C");
3370 /* A minimal implementation of gfc_match without whitespace, escape
3371 characters or variable arguments. Returns true if the next
3372 characters match the TARGET template exactly. */
3375 match_string_p (const char *target
)
3379 for (p
= target
; *p
; p
++)
3380 if ((char) gfc_next_ascii_char () != *p
)
3385 /* Matches an attribute specification including array specs. If
3386 successful, leaves the variables current_attr and current_as
3387 holding the specification. Also sets the colon_seen variable for
3388 later use by matchers associated with initializations.
3390 This subroutine is a little tricky in the sense that we don't know
3391 if we really have an attr-spec until we hit the double colon.
3392 Until that time, we can only return MATCH_NO. This forces us to
3393 check for duplicate specification at this level. */
3396 match_attr_spec (void)
3398 /* Modifiers that can exist in a type statement. */
3400 { GFC_DECL_BEGIN
= 0,
3401 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3402 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3403 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3404 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3405 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3406 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3409 /* GFC_DECL_END is the sentinel, index starts at 0. */
3410 #define NUM_DECL GFC_DECL_END
3412 locus start
, seen_at
[NUM_DECL
];
3419 gfc_clear_attr (¤t_attr
);
3420 start
= gfc_current_locus
;
3425 /* See if we get all of the keywords up to the final double colon. */
3426 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3434 gfc_gobble_whitespace ();
3436 ch
= gfc_next_ascii_char ();
3439 /* This is the successful exit condition for the loop. */
3440 if (gfc_next_ascii_char () == ':')
3445 gfc_gobble_whitespace ();
3446 switch (gfc_peek_ascii_char ())
3449 gfc_next_ascii_char ();
3450 switch (gfc_next_ascii_char ())
3453 if (match_string_p ("locatable"))
3455 /* Matched "allocatable". */
3456 d
= DECL_ALLOCATABLE
;
3461 if (match_string_p ("ynchronous"))
3463 /* Matched "asynchronous". */
3464 d
= DECL_ASYNCHRONOUS
;
3471 /* Try and match the bind(c). */
3472 m
= gfc_match_bind_c (NULL
, true);
3475 else if (m
== MATCH_ERROR
)
3480 gfc_next_ascii_char ();
3481 if ('o' != gfc_next_ascii_char ())
3483 switch (gfc_next_ascii_char ())
3486 if (match_string_p ("imension"))
3488 d
= DECL_CODIMENSION
;
3492 if (match_string_p ("tiguous"))
3494 d
= DECL_CONTIGUOUS
;
3501 if (match_string_p ("dimension"))
3506 if (match_string_p ("external"))
3511 if (match_string_p ("int"))
3513 ch
= gfc_next_ascii_char ();
3516 if (match_string_p ("nt"))
3518 /* Matched "intent". */
3519 /* TODO: Call match_intent_spec from here. */
3520 if (gfc_match (" ( in out )") == MATCH_YES
)
3522 else if (gfc_match (" ( in )") == MATCH_YES
)
3524 else if (gfc_match (" ( out )") == MATCH_YES
)
3530 if (match_string_p ("insic"))
3532 /* Matched "intrinsic". */
3540 if (match_string_p ("optional"))
3545 gfc_next_ascii_char ();
3546 switch (gfc_next_ascii_char ())
3549 if (match_string_p ("rameter"))
3551 /* Matched "parameter". */
3557 if (match_string_p ("inter"))
3559 /* Matched "pointer". */
3565 ch
= gfc_next_ascii_char ();
3568 if (match_string_p ("vate"))
3570 /* Matched "private". */
3576 if (match_string_p ("tected"))
3578 /* Matched "protected". */
3585 if (match_string_p ("blic"))
3587 /* Matched "public". */
3595 if (match_string_p ("save"))
3600 if (match_string_p ("target"))
3605 gfc_next_ascii_char ();
3606 ch
= gfc_next_ascii_char ();
3609 if (match_string_p ("lue"))
3611 /* Matched "value". */
3617 if (match_string_p ("latile"))
3619 /* Matched "volatile". */
3627 /* No double colon and no recognizable decl_type, so assume that
3628 we've been looking at something else the whole time. */
3635 /* Check to make sure any parens are paired up correctly. */
3636 if (gfc_match_parens () == MATCH_ERROR
)
3643 seen_at
[d
] = gfc_current_locus
;
3645 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3647 gfc_array_spec
*as
= NULL
;
3649 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3650 d
== DECL_CODIMENSION
);
3652 if (current_as
== NULL
)
3654 else if (m
== MATCH_YES
)
3656 if (!merge_array_spec (as
, current_as
, false))
3663 if (d
== DECL_CODIMENSION
)
3664 gfc_error ("Missing codimension specification at %C");
3666 gfc_error ("Missing dimension specification at %C");
3670 if (m
== MATCH_ERROR
)
3675 /* Since we've seen a double colon, we have to be looking at an
3676 attr-spec. This means that we can now issue errors. */
3677 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3682 case DECL_ALLOCATABLE
:
3683 attr
= "ALLOCATABLE";
3685 case DECL_ASYNCHRONOUS
:
3686 attr
= "ASYNCHRONOUS";
3688 case DECL_CODIMENSION
:
3689 attr
= "CODIMENSION";
3691 case DECL_CONTIGUOUS
:
3692 attr
= "CONTIGUOUS";
3694 case DECL_DIMENSION
:
3701 attr
= "INTENT (IN)";
3704 attr
= "INTENT (OUT)";
3707 attr
= "INTENT (IN OUT)";
3709 case DECL_INTRINSIC
:
3715 case DECL_PARAMETER
:
3721 case DECL_PROTECTED
:
3736 case DECL_IS_BIND_C
:
3746 attr
= NULL
; /* This shouldn't happen. */
3749 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3754 /* Now that we've dealt with duplicate attributes, add the attributes
3755 to the current attribute. */
3756 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3761 if (gfc_current_state () == COMP_DERIVED
3762 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3763 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3764 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3766 if (d
== DECL_ALLOCATABLE
)
3768 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3769 "attribute at %C in a TYPE definition"))
3777 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3784 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3785 && gfc_current_state () != COMP_MODULE
)
3787 if (d
== DECL_PRIVATE
)
3791 if (gfc_current_state () == COMP_DERIVED
3792 && gfc_state_stack
->previous
3793 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3795 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3796 "at %L in a TYPE definition", attr
,
3805 gfc_error ("%s attribute at %L is not allowed outside of the "
3806 "specification part of a module", attr
, &seen_at
[d
]);
3814 case DECL_ALLOCATABLE
:
3815 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3818 case DECL_ASYNCHRONOUS
:
3819 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3822 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3825 case DECL_CODIMENSION
:
3826 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3829 case DECL_CONTIGUOUS
:
3830 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3833 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3836 case DECL_DIMENSION
:
3837 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3841 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3845 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3849 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3853 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3856 case DECL_INTRINSIC
:
3857 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3861 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3864 case DECL_PARAMETER
:
3865 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3869 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3872 case DECL_PROTECTED
:
3873 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3875 gfc_error ("PROTECTED at %C only allowed in specification "
3876 "part of a module");
3881 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
3884 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3888 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3893 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3898 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3902 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3905 case DECL_IS_BIND_C
:
3906 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3910 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
3913 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3917 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
3920 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3924 gfc_internal_error ("match_attr_spec(): Bad attribute");
3934 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3935 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3936 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3937 current_attr
.save
= SAVE_IMPLICIT
;
3943 gfc_current_locus
= start
;
3944 gfc_free_array_spec (current_as
);
3950 /* Set the binding label, dest_label, either with the binding label
3951 stored in the given gfc_typespec, ts, or if none was provided, it
3952 will be the symbol name in all lower case, as required by the draft
3953 (J3/04-007, section 15.4.1). If a binding label was given and
3954 there is more than one argument (num_idents), it is an error. */
3957 set_binding_label (const char **dest_label
, const char *sym_name
,
3960 if (num_idents
> 1 && has_name_equals
)
3962 gfc_error ("Multiple identifiers provided with "
3963 "single NAME= specifier at %C");
3967 if (curr_binding_label
)
3968 /* Binding label given; store in temp holder till have sym. */
3969 *dest_label
= curr_binding_label
;
3972 /* No binding label given, and the NAME= specifier did not exist,
3973 which means there was no NAME="". */
3974 if (sym_name
!= NULL
&& has_name_equals
== 0)
3975 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3982 /* Set the status of the given common block as being BIND(C) or not,
3983 depending on the given parameter, is_bind_c. */
3986 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3988 com_block
->is_bind_c
= is_bind_c
;
3993 /* Verify that the given gfc_typespec is for a C interoperable type. */
3996 gfc_verify_c_interop (gfc_typespec
*ts
)
3998 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3999 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
4001 else if (ts
->type
== BT_CLASS
)
4003 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
4010 /* Verify that the variables of a given common block, which has been
4011 defined with the attribute specifier bind(c), to be of a C
4012 interoperable type. Errors will be reported here, if
4016 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
4018 gfc_symbol
*curr_sym
= NULL
;
4021 curr_sym
= com_block
->head
;
4023 /* Make sure we have at least one symbol. */
4024 if (curr_sym
== NULL
)
4027 /* Here we know we have a symbol, so we'll execute this loop
4031 /* The second to last param, 1, says this is in a common block. */
4032 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
4033 curr_sym
= curr_sym
->common_next
;
4034 } while (curr_sym
!= NULL
);
4040 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4041 an appropriate error message is reported. */
4044 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
4045 int is_in_common
, gfc_common_head
*com_block
)
4047 bool bind_c_function
= false;
4050 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
4051 bind_c_function
= true;
4053 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
4055 tmp_sym
= tmp_sym
->result
;
4056 /* Make sure it wasn't an implicitly typed result. */
4057 if (tmp_sym
->attr
.implicit_type
&& warn_c_binding_type
)
4059 gfc_warning (OPT_Wc_binding_type
,
4060 "Implicitly declared BIND(C) function %qs at "
4061 "%L may not be C interoperable", tmp_sym
->name
,
4062 &tmp_sym
->declared_at
);
4063 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
4064 /* Mark it as C interoperable to prevent duplicate warnings. */
4065 tmp_sym
->ts
.is_c_interop
= 1;
4066 tmp_sym
->attr
.is_c_interop
= 1;
4070 /* Here, we know we have the bind(c) attribute, so if we have
4071 enough type info, then verify that it's a C interop kind.
4072 The info could be in the symbol already, or possibly still in
4073 the given ts (current_ts), so look in both. */
4074 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
4076 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
4078 /* See if we're dealing with a sym in a common block or not. */
4079 if (is_in_common
== 1 && warn_c_binding_type
)
4081 gfc_warning (OPT_Wc_binding_type
,
4082 "Variable %qs in common block %qs at %L "
4083 "may not be a C interoperable "
4084 "kind though common block %qs is BIND(C)",
4085 tmp_sym
->name
, com_block
->name
,
4086 &(tmp_sym
->declared_at
), com_block
->name
);
4090 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
4091 gfc_error ("Type declaration %qs at %L is not C "
4092 "interoperable but it is BIND(C)",
4093 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4094 else if (warn_c_binding_type
)
4095 gfc_warning (OPT_Wc_binding_type
, "Variable %qs at %L "
4096 "may not be a C interoperable "
4097 "kind but it is BIND(C)",
4098 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4102 /* Variables declared w/in a common block can't be bind(c)
4103 since there's no way for C to see these variables, so there's
4104 semantically no reason for the attribute. */
4105 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4107 gfc_error ("Variable %qs in common block %qs at "
4108 "%L cannot be declared with BIND(C) "
4109 "since it is not a global",
4110 tmp_sym
->name
, com_block
->name
,
4111 &(tmp_sym
->declared_at
));
4115 /* Scalar variables that are bind(c) can not have the pointer
4116 or allocatable attributes. */
4117 if (tmp_sym
->attr
.is_bind_c
== 1)
4119 if (tmp_sym
->attr
.pointer
== 1)
4121 gfc_error ("Variable %qs at %L cannot have both the "
4122 "POINTER and BIND(C) attributes",
4123 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4127 if (tmp_sym
->attr
.allocatable
== 1)
4129 gfc_error ("Variable %qs at %L cannot have both the "
4130 "ALLOCATABLE and BIND(C) attributes",
4131 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4137 /* If it is a BIND(C) function, make sure the return value is a
4138 scalar value. The previous tests in this function made sure
4139 the type is interoperable. */
4140 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4141 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4142 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4144 /* BIND(C) functions can not return a character string. */
4145 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4146 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4147 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4148 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4149 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4150 "be a character string", tmp_sym
->name
,
4151 &(tmp_sym
->declared_at
));
4154 /* See if the symbol has been marked as private. If it has, make sure
4155 there is no binding label and warn the user if there is one. */
4156 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4157 && tmp_sym
->binding_label
)
4158 /* Use gfc_warning_now because we won't say that the symbol fails
4159 just because of this. */
4160 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4161 "given the binding label %qs", tmp_sym
->name
,
4162 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4168 /* Set the appropriate fields for a symbol that's been declared as
4169 BIND(C) (the is_bind_c flag and the binding label), and verify that
4170 the type is C interoperable. Errors are reported by the functions
4171 used to set/test these fields. */
4174 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4178 /* TODO: Do we need to make sure the vars aren't marked private? */
4180 /* Set the is_bind_c bit in symbol_attribute. */
4181 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4183 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4190 /* Set the fields marking the given common block as BIND(C), including
4191 a binding label, and report any errors encountered. */
4194 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4198 /* destLabel, common name, typespec (which may have binding label). */
4199 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4203 /* Set the given common block (com_block) to being bind(c) (1). */
4204 set_com_block_bind_c (com_block
, 1);
4210 /* Retrieve the list of one or more identifiers that the given bind(c)
4211 attribute applies to. */
4214 get_bind_c_idents (void)
4216 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4218 gfc_symbol
*tmp_sym
= NULL
;
4220 gfc_common_head
*com_block
= NULL
;
4222 if (gfc_match_name (name
) == MATCH_YES
)
4224 found_id
= MATCH_YES
;
4225 gfc_get_ha_symbol (name
, &tmp_sym
);
4227 else if (match_common_name (name
) == MATCH_YES
)
4229 found_id
= MATCH_YES
;
4230 com_block
= gfc_get_common (name
, 0);
4234 gfc_error ("Need either entity or common block name for "
4235 "attribute specification statement at %C");
4239 /* Save the current identifier and look for more. */
4242 /* Increment the number of identifiers found for this spec stmt. */
4245 /* Make sure we have a sym or com block, and verify that it can
4246 be bind(c). Set the appropriate field(s) and look for more
4248 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4250 if (tmp_sym
!= NULL
)
4252 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4257 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4261 /* Look to see if we have another identifier. */
4263 if (gfc_match_eos () == MATCH_YES
)
4264 found_id
= MATCH_NO
;
4265 else if (gfc_match_char (',') != MATCH_YES
)
4266 found_id
= MATCH_NO
;
4267 else if (gfc_match_name (name
) == MATCH_YES
)
4269 found_id
= MATCH_YES
;
4270 gfc_get_ha_symbol (name
, &tmp_sym
);
4272 else if (match_common_name (name
) == MATCH_YES
)
4274 found_id
= MATCH_YES
;
4275 com_block
= gfc_get_common (name
, 0);
4279 gfc_error ("Missing entity or common block name for "
4280 "attribute specification statement at %C");
4286 gfc_internal_error ("Missing symbol");
4288 } while (found_id
== MATCH_YES
);
4290 /* if we get here we were successful */
4295 /* Try and match a BIND(C) attribute specification statement. */
4298 gfc_match_bind_c_stmt (void)
4300 match found_match
= MATCH_NO
;
4305 /* This may not be necessary. */
4307 /* Clear the temporary binding label holder. */
4308 curr_binding_label
= NULL
;
4310 /* Look for the bind(c). */
4311 found_match
= gfc_match_bind_c (NULL
, true);
4313 if (found_match
== MATCH_YES
)
4315 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4318 /* Look for the :: now, but it is not required. */
4321 /* Get the identifier(s) that needs to be updated. This may need to
4322 change to hand the flag(s) for the attr specified so all identifiers
4323 found can have all appropriate parts updated (assuming that the same
4324 spec stmt can have multiple attrs, such as both bind(c) and
4326 if (!get_bind_c_idents ())
4327 /* Error message should have printed already. */
4335 /* Match a data declaration statement. */
4338 gfc_match_data_decl (void)
4344 num_idents_on_line
= 0;
4346 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4350 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4351 && gfc_current_state () != COMP_DERIVED
)
4353 sym
= gfc_use_derived (current_ts
.u
.derived
);
4361 current_ts
.u
.derived
= sym
;
4364 m
= match_attr_spec ();
4365 if (m
== MATCH_ERROR
)
4371 if (current_ts
.type
== BT_CLASS
4372 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4375 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4376 && current_ts
.u
.derived
->components
== NULL
4377 && !current_ts
.u
.derived
->attr
.zero_comp
)
4380 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4383 gfc_find_symbol (current_ts
.u
.derived
->name
,
4384 current_ts
.u
.derived
->ns
, 1, &sym
);
4386 /* Any symbol that we find had better be a type definition
4387 which has its components defined. */
4388 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4389 && (current_ts
.u
.derived
->components
!= NULL
4390 || current_ts
.u
.derived
->attr
.zero_comp
))
4393 gfc_error ("Derived type at %C has not been previously defined "
4394 "and so cannot appear in a derived type definition");
4400 /* If we have an old-style character declaration, and no new-style
4401 attribute specifications, then there a comma is optional between
4402 the type specification and the variable list. */
4403 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4404 gfc_match_char (',');
4406 /* Give the types/attributes to symbols that follow. Give the element
4407 a number so that repeat character length expressions can be copied. */
4411 num_idents_on_line
++;
4412 m
= variable_decl (elem
++);
4413 if (m
== MATCH_ERROR
)
4418 if (gfc_match_eos () == MATCH_YES
)
4420 if (gfc_match_char (',') != MATCH_YES
)
4424 if (!gfc_error_flag_test ())
4425 gfc_error ("Syntax error in data declaration at %C");
4428 gfc_free_data_all (gfc_current_ns
);
4431 gfc_free_array_spec (current_as
);
4437 /* Match a prefix associated with a function or subroutine
4438 declaration. If the typespec pointer is nonnull, then a typespec
4439 can be matched. Note that if nothing matches, MATCH_YES is
4440 returned (the null string was matched). */
4443 gfc_match_prefix (gfc_typespec
*ts
)
4449 gfc_clear_attr (¤t_attr
);
4451 seen_impure
= false;
4453 gcc_assert (!gfc_matching_prefix
);
4454 gfc_matching_prefix
= true;
4458 found_prefix
= false;
4460 if (!seen_type
&& ts
!= NULL
4461 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4462 && gfc_match_space () == MATCH_YES
)
4466 found_prefix
= true;
4469 if (gfc_match ("elemental% ") == MATCH_YES
)
4471 if (!gfc_add_elemental (¤t_attr
, NULL
))
4474 found_prefix
= true;
4477 if (gfc_match ("pure% ") == MATCH_YES
)
4479 if (!gfc_add_pure (¤t_attr
, NULL
))
4482 found_prefix
= true;
4485 if (gfc_match ("recursive% ") == MATCH_YES
)
4487 if (!gfc_add_recursive (¤t_attr
, NULL
))
4490 found_prefix
= true;
4493 /* IMPURE is a somewhat special case, as it needs not set an actual
4494 attribute but rather only prevents ELEMENTAL routines from being
4495 automatically PURE. */
4496 if (gfc_match ("impure% ") == MATCH_YES
)
4498 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4502 found_prefix
= true;
4505 while (found_prefix
);
4507 /* IMPURE and PURE must not both appear, of course. */
4508 if (seen_impure
&& current_attr
.pure
)
4510 gfc_error ("PURE and IMPURE must not appear both at %C");
4514 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4515 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4517 if (!gfc_add_pure (¤t_attr
, NULL
))
4521 /* At this point, the next item is not a prefix. */
4522 gcc_assert (gfc_matching_prefix
);
4523 gfc_matching_prefix
= false;
4527 gcc_assert (gfc_matching_prefix
);
4528 gfc_matching_prefix
= false;
4533 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4536 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4538 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4541 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4544 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4551 /* Match a formal argument list. */
4554 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4556 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4557 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4563 if (gfc_match_char ('(') != MATCH_YES
)
4570 if (gfc_match_char (')') == MATCH_YES
)
4575 if (gfc_match_char ('*') == MATCH_YES
)
4578 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4587 m
= gfc_match_name (name
);
4591 if (gfc_get_symbol (name
, NULL
, &sym
))
4595 p
= gfc_get_formal_arglist ();
4607 /* We don't add the VARIABLE flavor because the name could be a
4608 dummy procedure. We don't apply these attributes to formal
4609 arguments of statement functions. */
4610 if (sym
!= NULL
&& !st_flag
4611 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4612 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4618 /* The name of a program unit can be in a different namespace,
4619 so check for it explicitly. After the statement is accepted,
4620 the name is checked for especially in gfc_get_symbol(). */
4621 if (gfc_new_block
!= NULL
&& sym
!= NULL
4622 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4624 gfc_error ("Name %qs at %C is the name of the procedure",
4630 if (gfc_match_char (')') == MATCH_YES
)
4633 m
= gfc_match_char (',');
4636 gfc_error ("Unexpected junk in formal argument list at %C");
4642 /* Check for duplicate symbols in the formal argument list. */
4645 for (p
= head
; p
->next
; p
= p
->next
)
4650 for (q
= p
->next
; q
; q
= q
->next
)
4651 if (p
->sym
== q
->sym
)
4653 gfc_error ("Duplicate symbol %qs in formal argument list "
4654 "at %C", p
->sym
->name
);
4662 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4671 gfc_free_formal_arglist (head
);
4676 /* Match a RESULT specification following a function declaration or
4677 ENTRY statement. Also matches the end-of-statement. */
4680 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4682 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4686 if (gfc_match (" result (") != MATCH_YES
)
4689 m
= gfc_match_name (name
);
4693 /* Get the right paren, and that's it because there could be the
4694 bind(c) attribute after the result clause. */
4695 if (gfc_match_char (')') != MATCH_YES
)
4697 /* TODO: should report the missing right paren here. */
4701 if (strcmp (function
->name
, name
) == 0)
4703 gfc_error ("RESULT variable at %C must be different than function name");
4707 if (gfc_get_symbol (name
, NULL
, &r
))
4710 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4719 /* Match a function suffix, which could be a combination of a result
4720 clause and BIND(C), either one, or neither. The draft does not
4721 require them to come in a specific order. */
4724 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4726 match is_bind_c
; /* Found bind(c). */
4727 match is_result
; /* Found result clause. */
4728 match found_match
; /* Status of whether we've found a good match. */
4729 char peek_char
; /* Character we're going to peek at. */
4730 bool allow_binding_name
;
4732 /* Initialize to having found nothing. */
4733 found_match
= MATCH_NO
;
4734 is_bind_c
= MATCH_NO
;
4735 is_result
= MATCH_NO
;
4737 /* Get the next char to narrow between result and bind(c). */
4738 gfc_gobble_whitespace ();
4739 peek_char
= gfc_peek_ascii_char ();
4741 /* C binding names are not allowed for internal procedures. */
4742 if (gfc_current_state () == COMP_CONTAINS
4743 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4744 allow_binding_name
= false;
4746 allow_binding_name
= true;
4751 /* Look for result clause. */
4752 is_result
= match_result (sym
, result
);
4753 if (is_result
== MATCH_YES
)
4755 /* Now see if there is a bind(c) after it. */
4756 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4757 /* We've found the result clause and possibly bind(c). */
4758 found_match
= MATCH_YES
;
4761 /* This should only be MATCH_ERROR. */
4762 found_match
= is_result
;
4765 /* Look for bind(c) first. */
4766 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4767 if (is_bind_c
== MATCH_YES
)
4769 /* Now see if a result clause followed it. */
4770 is_result
= match_result (sym
, result
);
4771 found_match
= MATCH_YES
;
4775 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4776 found_match
= MATCH_ERROR
;
4780 gfc_error ("Unexpected junk after function declaration at %C");
4781 found_match
= MATCH_ERROR
;
4785 if (is_bind_c
== MATCH_YES
)
4787 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4788 if (gfc_current_state () == COMP_CONTAINS
4789 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4790 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
4791 "at %L may not be specified for an internal "
4792 "procedure", &gfc_current_locus
))
4795 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
4803 /* Procedure pointer return value without RESULT statement:
4804 Add "hidden" result variable named "ppr@". */
4807 add_hidden_procptr_result (gfc_symbol
*sym
)
4811 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4814 /* First usage case: PROCEDURE and EXTERNAL statements. */
4815 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4816 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4817 && sym
->attr
.external
;
4818 /* Second usage case: INTERFACE statements. */
4819 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4820 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4821 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4827 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4831 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4832 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4833 st2
->n
.sym
= stree
->n
.sym
;
4835 sym
->result
= stree
->n
.sym
;
4837 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4838 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4839 sym
->result
->attr
.external
= sym
->attr
.external
;
4840 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4841 sym
->result
->ts
= sym
->ts
;
4842 sym
->attr
.proc_pointer
= 0;
4843 sym
->attr
.pointer
= 0;
4844 sym
->attr
.external
= 0;
4845 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4847 sym
->result
->attr
.pointer
= 0;
4848 sym
->result
->attr
.proc_pointer
= 1;
4851 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4853 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4854 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4855 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4856 && sym
== gfc_current_ns
->proc_name
4857 && sym
== sym
->result
->ns
->proc_name
4858 && strcmp ("ppr@", sym
->result
->name
) == 0)
4860 sym
->result
->attr
.proc_pointer
= 1;
4861 sym
->attr
.pointer
= 0;
4869 /* Match the interface for a PROCEDURE declaration,
4870 including brackets (R1212). */
4873 match_procedure_interface (gfc_symbol
**proc_if
)
4877 locus old_loc
, entry_loc
;
4878 gfc_namespace
*old_ns
= gfc_current_ns
;
4879 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4881 old_loc
= entry_loc
= gfc_current_locus
;
4882 gfc_clear_ts (¤t_ts
);
4884 if (gfc_match (" (") != MATCH_YES
)
4886 gfc_current_locus
= entry_loc
;
4890 /* Get the type spec. for the procedure interface. */
4891 old_loc
= gfc_current_locus
;
4892 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4893 gfc_gobble_whitespace ();
4894 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4897 if (m
== MATCH_ERROR
)
4900 /* Procedure interface is itself a procedure. */
4901 gfc_current_locus
= old_loc
;
4902 m
= gfc_match_name (name
);
4904 /* First look to see if it is already accessible in the current
4905 namespace because it is use associated or contained. */
4907 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4910 /* If it is still not found, then try the parent namespace, if it
4911 exists and create the symbol there if it is still not found. */
4912 if (gfc_current_ns
->parent
)
4913 gfc_current_ns
= gfc_current_ns
->parent
;
4914 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4917 gfc_current_ns
= old_ns
;
4918 *proc_if
= st
->n
.sym
;
4923 /* Resolve interface if possible. That way, attr.procedure is only set
4924 if it is declared by a later procedure-declaration-stmt, which is
4925 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4926 while ((*proc_if
)->ts
.interface
)
4927 *proc_if
= (*proc_if
)->ts
.interface
;
4929 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
4930 && (*proc_if
)->ts
.type
== BT_UNKNOWN
4931 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
4932 (*proc_if
)->name
, NULL
))
4937 if (gfc_match (" )") != MATCH_YES
)
4939 gfc_current_locus
= entry_loc
;
4947 /* Match a PROCEDURE declaration (R1211). */
4950 match_procedure_decl (void)
4953 gfc_symbol
*sym
, *proc_if
= NULL
;
4955 gfc_expr
*initializer
= NULL
;
4957 /* Parse interface (with brackets). */
4958 m
= match_procedure_interface (&proc_if
);
4962 /* Parse attributes (with colons). */
4963 m
= match_attr_spec();
4964 if (m
== MATCH_ERROR
)
4967 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4969 current_attr
.is_bind_c
= 1;
4970 has_name_equals
= 0;
4971 curr_binding_label
= NULL
;
4974 /* Get procedure symbols. */
4977 m
= gfc_match_symbol (&sym
, 0);
4980 else if (m
== MATCH_ERROR
)
4983 /* Add current_attr to the symbol attributes. */
4984 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
4987 if (sym
->attr
.is_bind_c
)
4989 /* Check for C1218. */
4990 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4992 gfc_error ("BIND(C) attribute at %C requires "
4993 "an interface with BIND(C)");
4996 /* Check for C1217. */
4997 if (has_name_equals
&& sym
->attr
.pointer
)
4999 gfc_error ("BIND(C) procedure with NAME may not have "
5000 "POINTER attribute at %C");
5003 if (has_name_equals
&& sym
->attr
.dummy
)
5005 gfc_error ("Dummy procedure at %C may not have "
5006 "BIND(C) attribute with NAME");
5009 /* Set binding label for BIND(C). */
5010 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
5014 if (!gfc_add_external (&sym
->attr
, NULL
))
5017 if (add_hidden_procptr_result (sym
))
5020 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
5023 /* Set interface. */
5024 if (proc_if
!= NULL
)
5026 if (sym
->ts
.type
!= BT_UNKNOWN
)
5028 gfc_error ("Procedure %qs at %L already has basic type of %s",
5029 sym
->name
, &gfc_current_locus
,
5030 gfc_basic_typename (sym
->ts
.type
));
5033 sym
->ts
.interface
= proc_if
;
5034 sym
->attr
.untyped
= 1;
5035 sym
->attr
.if_source
= IFSRC_IFBODY
;
5037 else if (current_ts
.type
!= BT_UNKNOWN
)
5039 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5041 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5042 sym
->ts
.interface
->ts
= current_ts
;
5043 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5044 sym
->ts
.interface
->attr
.function
= 1;
5045 sym
->attr
.function
= 1;
5046 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
5049 if (gfc_match (" =>") == MATCH_YES
)
5051 if (!current_attr
.pointer
)
5053 gfc_error ("Initialization at %C isn't for a pointer variable");
5058 m
= match_pointer_init (&initializer
, 1);
5062 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
5067 if (gfc_match_eos () == MATCH_YES
)
5069 if (gfc_match_char (',') != MATCH_YES
)
5074 gfc_error ("Syntax error in PROCEDURE statement at %C");
5078 /* Free stuff up and return. */
5079 gfc_free_expr (initializer
);
5085 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
5088 /* Match a procedure pointer component declaration (R445). */
5091 match_ppc_decl (void)
5094 gfc_symbol
*proc_if
= NULL
;
5098 gfc_expr
*initializer
= NULL
;
5099 gfc_typebound_proc
* tb
;
5100 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5102 /* Parse interface (with brackets). */
5103 m
= match_procedure_interface (&proc_if
);
5107 /* Parse attributes. */
5108 tb
= XCNEW (gfc_typebound_proc
);
5109 tb
->where
= gfc_current_locus
;
5110 m
= match_binding_attributes (tb
, false, true);
5111 if (m
== MATCH_ERROR
)
5114 gfc_clear_attr (¤t_attr
);
5115 current_attr
.procedure
= 1;
5116 current_attr
.proc_pointer
= 1;
5117 current_attr
.access
= tb
->access
;
5118 current_attr
.flavor
= FL_PROCEDURE
;
5120 /* Match the colons (required). */
5121 if (gfc_match (" ::") != MATCH_YES
)
5123 gfc_error ("Expected %<::%> after binding-attributes at %C");
5127 /* Check for C450. */
5128 if (!tb
->nopass
&& proc_if
== NULL
)
5130 gfc_error("NOPASS or explicit interface required at %C");
5134 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5137 /* Match PPC names. */
5141 m
= gfc_match_name (name
);
5144 else if (m
== MATCH_ERROR
)
5147 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5150 /* Add current_attr to the symbol attributes. */
5151 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5154 if (!gfc_add_external (&c
->attr
, NULL
))
5157 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5164 c
->tb
= XCNEW (gfc_typebound_proc
);
5165 c
->tb
->where
= gfc_current_locus
;
5169 /* Set interface. */
5170 if (proc_if
!= NULL
)
5172 c
->ts
.interface
= proc_if
;
5173 c
->attr
.untyped
= 1;
5174 c
->attr
.if_source
= IFSRC_IFBODY
;
5176 else if (ts
.type
!= BT_UNKNOWN
)
5179 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5180 c
->ts
.interface
->result
= c
->ts
.interface
;
5181 c
->ts
.interface
->ts
= ts
;
5182 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5183 c
->ts
.interface
->attr
.function
= 1;
5184 c
->attr
.function
= 1;
5185 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5188 if (gfc_match (" =>") == MATCH_YES
)
5190 m
= match_pointer_init (&initializer
, 1);
5193 gfc_free_expr (initializer
);
5196 c
->initializer
= initializer
;
5199 if (gfc_match_eos () == MATCH_YES
)
5201 if (gfc_match_char (',') != MATCH_YES
)
5206 gfc_error ("Syntax error in procedure pointer component at %C");
5211 /* Match a PROCEDURE declaration inside an interface (R1206). */
5214 match_procedure_in_interface (void)
5218 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5221 if (current_interface
.type
== INTERFACE_NAMELESS
5222 || current_interface
.type
== INTERFACE_ABSTRACT
)
5224 gfc_error ("PROCEDURE at %C must be in a generic interface");
5228 /* Check if the F2008 optional double colon appears. */
5229 gfc_gobble_whitespace ();
5230 old_locus
= gfc_current_locus
;
5231 if (gfc_match ("::") == MATCH_YES
)
5233 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5234 "MODULE PROCEDURE statement at %L", &old_locus
))
5238 gfc_current_locus
= old_locus
;
5242 m
= gfc_match_name (name
);
5245 else if (m
== MATCH_ERROR
)
5247 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5250 if (!gfc_add_interface (sym
))
5253 if (gfc_match_eos () == MATCH_YES
)
5255 if (gfc_match_char (',') != MATCH_YES
)
5262 gfc_error ("Syntax error in PROCEDURE statement at %C");
5267 /* General matcher for PROCEDURE declarations. */
5269 static match
match_procedure_in_type (void);
5272 gfc_match_procedure (void)
5276 switch (gfc_current_state ())
5281 case COMP_SUBROUTINE
:
5284 m
= match_procedure_decl ();
5286 case COMP_INTERFACE
:
5287 m
= match_procedure_in_interface ();
5290 m
= match_ppc_decl ();
5292 case COMP_DERIVED_CONTAINS
:
5293 m
= match_procedure_in_type ();
5302 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5309 /* Warn if a matched procedure has the same name as an intrinsic; this is
5310 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5311 parser-state-stack to find out whether we're in a module. */
5314 do_warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5318 in_module
= (gfc_state_stack
->previous
5319 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5321 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5325 /* Match a function declaration. */
5328 gfc_match_function_decl (void)
5330 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5331 gfc_symbol
*sym
, *result
;
5335 match found_match
; /* Status returned by match func. */
5337 if (gfc_current_state () != COMP_NONE
5338 && gfc_current_state () != COMP_INTERFACE
5339 && gfc_current_state () != COMP_CONTAINS
)
5342 gfc_clear_ts (¤t_ts
);
5344 old_loc
= gfc_current_locus
;
5346 m
= gfc_match_prefix (¤t_ts
);
5349 gfc_current_locus
= old_loc
;
5353 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5355 gfc_current_locus
= old_loc
;
5358 if (get_proc_name (name
, &sym
, false))
5361 if (add_hidden_procptr_result (sym
))
5364 gfc_new_block
= sym
;
5366 m
= gfc_match_formal_arglist (sym
, 0, 0);
5369 gfc_error ("Expected formal argument list in function "
5370 "definition at %C");
5374 else if (m
== MATCH_ERROR
)
5379 /* According to the draft, the bind(c) and result clause can
5380 come in either order after the formal_arg_list (i.e., either
5381 can be first, both can exist together or by themselves or neither
5382 one). Therefore, the match_result can't match the end of the
5383 string, and check for the bind(c) or result clause in either order. */
5384 found_match
= gfc_match_eos ();
5386 /* Make sure that it isn't already declared as BIND(C). If it is, it
5387 must have been marked BIND(C) with a BIND(C) attribute and that is
5388 not allowed for procedures. */
5389 if (sym
->attr
.is_bind_c
== 1)
5391 sym
->attr
.is_bind_c
= 0;
5392 if (sym
->old_symbol
!= NULL
)
5393 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5394 "variables or common blocks",
5395 &(sym
->old_symbol
->declared_at
));
5397 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5398 "variables or common blocks", &gfc_current_locus
);
5401 if (found_match
!= MATCH_YES
)
5403 /* If we haven't found the end-of-statement, look for a suffix. */
5404 suffix_match
= gfc_match_suffix (sym
, &result
);
5405 if (suffix_match
== MATCH_YES
)
5406 /* Need to get the eos now. */
5407 found_match
= gfc_match_eos ();
5409 found_match
= suffix_match
;
5412 if(found_match
!= MATCH_YES
)
5416 /* Make changes to the symbol. */
5419 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5422 if (!gfc_missing_attr (&sym
->attr
, NULL
)
5423 || !copy_prefix (&sym
->attr
, &sym
->declared_at
))
5426 /* Delay matching the function characteristics until after the
5427 specification block by signalling kind=-1. */
5428 sym
->declared_at
= old_loc
;
5429 if (current_ts
.type
!= BT_UNKNOWN
)
5430 current_ts
.kind
= -1;
5432 current_ts
.kind
= 0;
5436 if (current_ts
.type
!= BT_UNKNOWN
5437 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5443 if (current_ts
.type
!= BT_UNKNOWN
5444 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5446 sym
->result
= result
;
5449 /* Warn if this procedure has the same name as an intrinsic. */
5450 do_warn_intrinsic_shadow (sym
, true);
5456 gfc_current_locus
= old_loc
;
5461 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5462 pass the name of the entry, rather than the gfc_current_block name, and
5463 to return false upon finding an existing global entry. */
5466 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5470 enum gfc_symbol_type type
;
5472 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5474 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5475 name is a global identifier. */
5476 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5478 s
= gfc_get_gsymbol (name
);
5480 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5482 gfc_global_used (s
, where
);
5491 s
->ns
= gfc_current_ns
;
5495 /* Don't add the symbol multiple times. */
5497 && (!gfc_notification_std (GFC_STD_F2008
)
5498 || strcmp (name
, binding_label
) != 0))
5500 s
= gfc_get_gsymbol (binding_label
);
5502 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5504 gfc_global_used (s
, where
);
5511 s
->binding_label
= binding_label
;
5514 s
->ns
= gfc_current_ns
;
5522 /* Match an ENTRY statement. */
5525 gfc_match_entry (void)
5530 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5531 gfc_compile_state state
;
5535 bool module_procedure
;
5539 m
= gfc_match_name (name
);
5543 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5546 state
= gfc_current_state ();
5547 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5552 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5555 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5557 case COMP_BLOCK_DATA
:
5558 gfc_error ("ENTRY statement at %C cannot appear within "
5561 case COMP_INTERFACE
:
5562 gfc_error ("ENTRY statement at %C cannot appear within "
5566 gfc_error ("ENTRY statement at %C cannot appear within "
5567 "a DERIVED TYPE block");
5570 gfc_error ("ENTRY statement at %C cannot appear within "
5571 "an IF-THEN block");
5574 case COMP_DO_CONCURRENT
:
5575 gfc_error ("ENTRY statement at %C cannot appear within "
5579 gfc_error ("ENTRY statement at %C cannot appear within "
5583 gfc_error ("ENTRY statement at %C cannot appear within "
5587 gfc_error ("ENTRY statement at %C cannot appear within "
5591 gfc_error ("ENTRY statement at %C cannot appear within "
5592 "a contained subprogram");
5595 gfc_internal_error ("gfc_match_entry(): Bad state");
5600 module_procedure
= gfc_current_ns
->parent
!= NULL
5601 && gfc_current_ns
->parent
->proc_name
5602 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5605 if (gfc_current_ns
->parent
!= NULL
5606 && gfc_current_ns
->parent
->proc_name
5607 && !module_procedure
)
5609 gfc_error("ENTRY statement at %C cannot appear in a "
5610 "contained procedure");
5614 /* Module function entries need special care in get_proc_name
5615 because previous references within the function will have
5616 created symbols attached to the current namespace. */
5617 if (get_proc_name (name
, &entry
,
5618 gfc_current_ns
->parent
!= NULL
5619 && module_procedure
))
5622 proc
= gfc_current_block ();
5624 /* Make sure that it isn't already declared as BIND(C). If it is, it
5625 must have been marked BIND(C) with a BIND(C) attribute and that is
5626 not allowed for procedures. */
5627 if (entry
->attr
.is_bind_c
== 1)
5629 entry
->attr
.is_bind_c
= 0;
5630 if (entry
->old_symbol
!= NULL
)
5631 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5632 "variables or common blocks",
5633 &(entry
->old_symbol
->declared_at
));
5635 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5636 "variables or common blocks", &gfc_current_locus
);
5639 /* Check what next non-whitespace character is so we can tell if there
5640 is the required parens if we have a BIND(C). */
5641 old_loc
= gfc_current_locus
;
5642 gfc_gobble_whitespace ();
5643 peek_char
= gfc_peek_ascii_char ();
5645 if (state
== COMP_SUBROUTINE
)
5647 m
= gfc_match_formal_arglist (entry
, 0, 1);
5651 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5652 never be an internal procedure. */
5653 is_bind_c
= gfc_match_bind_c (entry
, true);
5654 if (is_bind_c
== MATCH_ERROR
)
5656 if (is_bind_c
== MATCH_YES
)
5658 if (peek_char
!= '(')
5660 gfc_error ("Missing required parentheses before BIND(C) at %C");
5663 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5664 &(entry
->declared_at
), 1))
5668 if (!gfc_current_ns
->parent
5669 && !add_global_entry (name
, entry
->binding_label
, true,
5673 /* An entry in a subroutine. */
5674 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5675 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5680 /* An entry in a function.
5681 We need to take special care because writing
5686 ENTRY f() RESULT (r)
5688 ENTRY f RESULT (r). */
5689 if (gfc_match_eos () == MATCH_YES
)
5691 gfc_current_locus
= old_loc
;
5692 /* Match the empty argument list, and add the interface to
5694 m
= gfc_match_formal_arglist (entry
, 0, 1);
5697 m
= gfc_match_formal_arglist (entry
, 0, 0);
5704 if (gfc_match_eos () == MATCH_YES
)
5706 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5707 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5710 entry
->result
= entry
;
5714 m
= gfc_match_suffix (entry
, &result
);
5716 gfc_syntax_error (ST_ENTRY
);
5722 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5723 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5724 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5726 entry
->result
= result
;
5730 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5731 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5733 entry
->result
= entry
;
5737 if (!gfc_current_ns
->parent
5738 && !add_global_entry (name
, entry
->binding_label
, false,
5743 if (gfc_match_eos () != MATCH_YES
)
5745 gfc_syntax_error (ST_ENTRY
);
5749 entry
->attr
.recursive
= proc
->attr
.recursive
;
5750 entry
->attr
.elemental
= proc
->attr
.elemental
;
5751 entry
->attr
.pure
= proc
->attr
.pure
;
5753 el
= gfc_get_entry_list ();
5755 el
->next
= gfc_current_ns
->entries
;
5756 gfc_current_ns
->entries
= el
;
5758 el
->id
= el
->next
->id
+ 1;
5762 new_st
.op
= EXEC_ENTRY
;
5763 new_st
.ext
.entry
= el
;
5769 /* Match a subroutine statement, including optional prefixes. */
5772 gfc_match_subroutine (void)
5774 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5779 bool allow_binding_name
;
5781 if (gfc_current_state () != COMP_NONE
5782 && gfc_current_state () != COMP_INTERFACE
5783 && gfc_current_state () != COMP_CONTAINS
)
5786 m
= gfc_match_prefix (NULL
);
5790 m
= gfc_match ("subroutine% %n", name
);
5794 if (get_proc_name (name
, &sym
, false))
5797 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5798 the symbol existed before. */
5799 sym
->declared_at
= gfc_current_locus
;
5801 if (add_hidden_procptr_result (sym
))
5804 gfc_new_block
= sym
;
5806 /* Check what next non-whitespace character is so we can tell if there
5807 is the required parens if we have a BIND(C). */
5808 gfc_gobble_whitespace ();
5809 peek_char
= gfc_peek_ascii_char ();
5811 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
5814 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5817 /* Make sure that it isn't already declared as BIND(C). If it is, it
5818 must have been marked BIND(C) with a BIND(C) attribute and that is
5819 not allowed for procedures. */
5820 if (sym
->attr
.is_bind_c
== 1)
5822 sym
->attr
.is_bind_c
= 0;
5823 if (sym
->old_symbol
!= NULL
)
5824 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5825 "variables or common blocks",
5826 &(sym
->old_symbol
->declared_at
));
5828 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5829 "variables or common blocks", &gfc_current_locus
);
5832 /* C binding names are not allowed for internal procedures. */
5833 if (gfc_current_state () == COMP_CONTAINS
5834 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5835 allow_binding_name
= false;
5837 allow_binding_name
= true;
5839 /* Here, we are just checking if it has the bind(c) attribute, and if
5840 so, then we need to make sure it's all correct. If it doesn't,
5841 we still need to continue matching the rest of the subroutine line. */
5842 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5843 if (is_bind_c
== MATCH_ERROR
)
5845 /* There was an attempt at the bind(c), but it was wrong. An
5846 error message should have been printed w/in the gfc_match_bind_c
5847 so here we'll just return the MATCH_ERROR. */
5851 if (is_bind_c
== MATCH_YES
)
5853 /* The following is allowed in the Fortran 2008 draft. */
5854 if (gfc_current_state () == COMP_CONTAINS
5855 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5856 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5857 "at %L may not be specified for an internal "
5858 "procedure", &gfc_current_locus
))
5861 if (peek_char
!= '(')
5863 gfc_error ("Missing required parentheses before BIND(C) at %C");
5866 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
5867 &(sym
->declared_at
), 1))
5871 if (gfc_match_eos () != MATCH_YES
)
5873 gfc_syntax_error (ST_SUBROUTINE
);
5877 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5880 /* Warn if it has the same name as an intrinsic. */
5881 do_warn_intrinsic_shadow (sym
, false);
5887 /* Check that the NAME identifier in a BIND attribute or statement
5888 is conform to C identifier rules. */
5891 check_bind_name_identifier (char **name
)
5893 char *n
= *name
, *p
;
5895 /* Remove leading spaces. */
5899 /* On an empty string, free memory and set name to NULL. */
5907 /* Remove trailing spaces. */
5908 p
= n
+ strlen(n
) - 1;
5912 /* Insert the identifier into the symbol table. */
5917 /* Now check that identifier is valid under C rules. */
5920 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5925 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
5927 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5935 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5936 given, and set the binding label in either the given symbol (if not
5937 NULL), or in the current_ts. The symbol may be NULL because we may
5938 encounter the BIND(C) before the declaration itself. Return
5939 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5940 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5941 or MATCH_YES if the specifier was correct and the binding label and
5942 bind(c) fields were set correctly for the given symbol or the
5943 current_ts. If allow_binding_name is false, no binding name may be
5947 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5949 char *binding_label
= NULL
;
5952 /* Initialize the flag that specifies whether we encountered a NAME=
5953 specifier or not. */
5954 has_name_equals
= 0;
5956 /* This much we have to be able to match, in this order, if
5957 there is a bind(c) label. */
5958 if (gfc_match (" bind ( c ") != MATCH_YES
)
5961 /* Now see if there is a binding label, or if we've reached the
5962 end of the bind(c) attribute without one. */
5963 if (gfc_match_char (',') == MATCH_YES
)
5965 if (gfc_match (" name = ") != MATCH_YES
)
5967 gfc_error ("Syntax error in NAME= specifier for binding label "
5969 /* should give an error message here */
5973 has_name_equals
= 1;
5975 if (gfc_match_init_expr (&e
) != MATCH_YES
)
5981 if (!gfc_simplify_expr(e
, 0))
5983 gfc_error ("NAME= specifier at %C should be a constant expression");
5988 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
5989 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
5991 gfc_error ("NAME= specifier at %C should be a scalar of "
5992 "default character kind");
5997 // Get a C string from the Fortran string constant
5998 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
5999 e
->value
.character
.length
);
6002 // Check that it is valid (old gfc_match_name_C)
6003 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
6007 /* Get the required right paren. */
6008 if (gfc_match_char (')') != MATCH_YES
)
6010 gfc_error ("Missing closing paren for binding label at %C");
6014 if (has_name_equals
&& !allow_binding_name
)
6016 gfc_error ("No binding name is allowed in BIND(C) at %C");
6020 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
6022 gfc_error ("For dummy procedure %s, no binding name is "
6023 "allowed in BIND(C) at %C", sym
->name
);
6028 /* Save the binding label to the symbol. If sym is null, we're
6029 probably matching the typespec attributes of a declaration and
6030 haven't gotten the name yet, and therefore, no symbol yet. */
6034 sym
->binding_label
= binding_label
;
6036 curr_binding_label
= binding_label
;
6038 else if (allow_binding_name
)
6040 /* No binding label, but if symbol isn't null, we
6041 can set the label for it here.
6042 If name="" or allow_binding_name is false, no C binding name is
6044 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
6045 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
6048 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
6049 && current_interface
.type
== INTERFACE_ABSTRACT
)
6051 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6059 /* Return nonzero if we're currently compiling a contained procedure. */
6062 contained_procedure (void)
6064 gfc_state_data
*s
= gfc_state_stack
;
6066 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
6067 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
6073 /* Set the kind of each enumerator. The kind is selected such that it is
6074 interoperable with the corresponding C enumeration type, making
6075 sure that -fshort-enums is honored. */
6080 enumerator_history
*current_history
= NULL
;
6084 if (max_enum
== NULL
|| enum_history
== NULL
)
6087 if (!flag_short_enums
)
6093 kind
= gfc_integer_kinds
[i
++].kind
;
6095 while (kind
< gfc_c_int_kind
6096 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6099 current_history
= enum_history
;
6100 while (current_history
!= NULL
)
6102 current_history
->sym
->ts
.kind
= kind
;
6103 current_history
= current_history
->next
;
6108 /* Match any of the various end-block statements. Returns the type of
6109 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6110 and END BLOCK statements cannot be replaced by a single END statement. */
6113 gfc_match_end (gfc_statement
*st
)
6115 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6116 gfc_compile_state state
;
6118 const char *block_name
;
6122 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6123 gfc_namespace
**nsp
;
6125 old_loc
= gfc_current_locus
;
6126 if (gfc_match ("end") != MATCH_YES
)
6129 state
= gfc_current_state ();
6130 block_name
= gfc_current_block () == NULL
6131 ? NULL
: gfc_current_block ()->name
;
6135 case COMP_ASSOCIATE
:
6137 if (!strncmp (block_name
, "block@", strlen("block@")))
6142 case COMP_DERIVED_CONTAINS
:
6143 state
= gfc_state_stack
->previous
->state
;
6144 block_name
= gfc_state_stack
->previous
->sym
== NULL
6145 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6156 *st
= ST_END_PROGRAM
;
6157 target
= " program";
6161 case COMP_SUBROUTINE
:
6162 *st
= ST_END_SUBROUTINE
;
6163 target
= " subroutine";
6164 eos_ok
= !contained_procedure ();
6168 *st
= ST_END_FUNCTION
;
6169 target
= " function";
6170 eos_ok
= !contained_procedure ();
6173 case COMP_BLOCK_DATA
:
6174 *st
= ST_END_BLOCK_DATA
;
6175 target
= " block data";
6180 *st
= ST_END_MODULE
;
6185 case COMP_INTERFACE
:
6186 *st
= ST_END_INTERFACE
;
6187 target
= " interface";
6192 case COMP_DERIVED_CONTAINS
:
6198 case COMP_ASSOCIATE
:
6199 *st
= ST_END_ASSOCIATE
;
6200 target
= " associate";
6217 case COMP_DO_CONCURRENT
:
6224 *st
= ST_END_CRITICAL
;
6225 target
= " critical";
6230 case COMP_SELECT_TYPE
:
6231 *st
= ST_END_SELECT
;
6237 *st
= ST_END_FORALL
;
6252 last_initializer
= NULL
;
6254 gfc_free_enum_history ();
6258 gfc_error ("Unexpected END statement at %C");
6262 old_loc
= gfc_current_locus
;
6263 if (gfc_match_eos () == MATCH_YES
)
6265 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6267 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6268 "instead of %s statement at %L",
6269 gfc_ascii_statement(*st
), &old_loc
))
6274 /* We would have required END [something]. */
6275 gfc_error ("%s statement expected at %L",
6276 gfc_ascii_statement (*st
), &old_loc
);
6283 /* Verify that we've got the sort of end-block that we're expecting. */
6284 if (gfc_match (target
) != MATCH_YES
)
6286 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st
),
6291 old_loc
= gfc_current_locus
;
6292 /* If we're at the end, make sure a block name wasn't required. */
6293 if (gfc_match_eos () == MATCH_YES
)
6296 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6297 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6298 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6304 gfc_error ("Expected block name of %qs in %s statement at %L",
6305 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6310 /* END INTERFACE has a special handler for its several possible endings. */
6311 if (*st
== ST_END_INTERFACE
)
6312 return gfc_match_end_interface ();
6314 /* We haven't hit the end of statement, so what is left must be an
6316 m
= gfc_match_space ();
6318 m
= gfc_match_name (name
);
6321 gfc_error ("Expected terminating name at %C");
6325 if (block_name
== NULL
)
6328 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6330 gfc_error ("Expected label %qs for %s statement at %C", block_name
,
6331 gfc_ascii_statement (*st
));
6334 /* Procedure pointer as function result. */
6335 else if (strcmp (block_name
, "ppr@") == 0
6336 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6338 gfc_error ("Expected label %qs for %s statement at %C",
6339 gfc_current_block ()->ns
->proc_name
->name
,
6340 gfc_ascii_statement (*st
));
6344 if (gfc_match_eos () == MATCH_YES
)
6348 gfc_syntax_error (*st
);
6351 gfc_current_locus
= old_loc
;
6353 /* If we are missing an END BLOCK, we created a half-ready namespace.
6354 Remove it from the parent namespace's sibling list. */
6356 if (state
== COMP_BLOCK
)
6358 parent_ns
= gfc_current_ns
->parent
;
6360 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6366 if (ns
== gfc_current_ns
)
6368 if (prev_ns
== NULL
)
6371 prev_ns
->sibling
= ns
->sibling
;
6377 gfc_free_namespace (gfc_current_ns
);
6378 gfc_current_ns
= parent_ns
;
6386 /***************** Attribute declaration statements ****************/
6388 /* Set the attribute of a single variable. */
6393 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6396 /* Workaround -Wmaybe-uninitialized false positive during
6397 profiledbootstrap by initializing them. */
6398 gfc_symbol
*sym
= NULL
;
6404 m
= gfc_match_name (name
);
6408 if (find_special (name
, &sym
, false))
6411 if (!check_function_name (name
))
6417 var_locus
= gfc_current_locus
;
6419 /* Deal with possible array specification for certain attributes. */
6420 if (current_attr
.dimension
6421 || current_attr
.codimension
6422 || current_attr
.allocatable
6423 || current_attr
.pointer
6424 || current_attr
.target
)
6426 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6427 !current_attr
.dimension
6428 && !current_attr
.pointer
6429 && !current_attr
.target
);
6430 if (m
== MATCH_ERROR
)
6433 if (current_attr
.dimension
&& m
== MATCH_NO
)
6435 gfc_error ("Missing array specification at %L in DIMENSION "
6436 "statement", &var_locus
);
6441 if (current_attr
.dimension
&& sym
->value
)
6443 gfc_error ("Dimensions specified for %s at %L after its "
6444 "initialisation", sym
->name
, &var_locus
);
6449 if (current_attr
.codimension
&& m
== MATCH_NO
)
6451 gfc_error ("Missing array specification at %L in CODIMENSION "
6452 "statement", &var_locus
);
6457 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6458 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6460 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6466 /* Update symbol table. DIMENSION attribute is set in
6467 gfc_set_array_spec(). For CLASS variables, this must be applied
6468 to the first component, or '_data' field. */
6469 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6471 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6479 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6480 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6487 if (sym
->ts
.type
== BT_CLASS
6488 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6494 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6500 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6502 /* Fix the array spec. */
6503 m
= gfc_mod_pointee_as (sym
->as
);
6504 if (m
== MATCH_ERROR
)
6508 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6514 if ((current_attr
.external
|| current_attr
.intrinsic
)
6515 && sym
->attr
.flavor
!= FL_PROCEDURE
6516 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6522 add_hidden_procptr_result (sym
);
6527 gfc_free_array_spec (as
);
6532 /* Generic attribute declaration subroutine. Used for attributes that
6533 just have a list of names. */
6540 /* Gobble the optional double colon, by simply ignoring the result
6550 if (gfc_match_eos () == MATCH_YES
)
6556 if (gfc_match_char (',') != MATCH_YES
)
6558 gfc_error ("Unexpected character in variable list at %C");
6568 /* This routine matches Cray Pointer declarations of the form:
6569 pointer ( <pointer>, <pointee> )
6571 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6572 The pointer, if already declared, should be an integer. Otherwise, we
6573 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6574 be either a scalar, or an array declaration. No space is allocated for
6575 the pointee. For the statement
6576 pointer (ipt, ar(10))
6577 any subsequent uses of ar will be translated (in C-notation) as
6578 ar(i) => ((<type> *) ipt)(i)
6579 After gimplification, pointee variable will disappear in the code. */
6582 cray_pointer_decl (void)
6585 gfc_array_spec
*as
= NULL
;
6586 gfc_symbol
*cptr
; /* Pointer symbol. */
6587 gfc_symbol
*cpte
; /* Pointee symbol. */
6593 if (gfc_match_char ('(') != MATCH_YES
)
6595 gfc_error ("Expected %<(%> at %C");
6599 /* Match pointer. */
6600 var_locus
= gfc_current_locus
;
6601 gfc_clear_attr (¤t_attr
);
6602 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6603 current_ts
.type
= BT_INTEGER
;
6604 current_ts
.kind
= gfc_index_integer_kind
;
6606 m
= gfc_match_symbol (&cptr
, 0);
6609 gfc_error ("Expected variable name at %C");
6613 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6616 gfc_set_sym_referenced (cptr
);
6618 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6620 cptr
->ts
.type
= BT_INTEGER
;
6621 cptr
->ts
.kind
= gfc_index_integer_kind
;
6623 else if (cptr
->ts
.type
!= BT_INTEGER
)
6625 gfc_error ("Cray pointer at %C must be an integer");
6628 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6629 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6630 " memory addresses require %d bytes",
6631 cptr
->ts
.kind
, gfc_index_integer_kind
);
6633 if (gfc_match_char (',') != MATCH_YES
)
6635 gfc_error ("Expected \",\" at %C");
6639 /* Match Pointee. */
6640 var_locus
= gfc_current_locus
;
6641 gfc_clear_attr (¤t_attr
);
6642 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6643 current_ts
.type
= BT_UNKNOWN
;
6644 current_ts
.kind
= 0;
6646 m
= gfc_match_symbol (&cpte
, 0);
6649 gfc_error ("Expected variable name at %C");
6653 /* Check for an optional array spec. */
6654 m
= gfc_match_array_spec (&as
, true, false);
6655 if (m
== MATCH_ERROR
)
6657 gfc_free_array_spec (as
);
6660 else if (m
== MATCH_NO
)
6662 gfc_free_array_spec (as
);
6666 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6669 gfc_set_sym_referenced (cpte
);
6671 if (cpte
->as
== NULL
)
6673 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6674 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6676 else if (as
!= NULL
)
6678 gfc_error ("Duplicate array spec for Cray pointee at %C");
6679 gfc_free_array_spec (as
);
6685 if (cpte
->as
!= NULL
)
6687 /* Fix array spec. */
6688 m
= gfc_mod_pointee_as (cpte
->as
);
6689 if (m
== MATCH_ERROR
)
6693 /* Point the Pointee at the Pointer. */
6694 cpte
->cp_pointer
= cptr
;
6696 if (gfc_match_char (')') != MATCH_YES
)
6698 gfc_error ("Expected \")\" at %C");
6701 m
= gfc_match_char (',');
6703 done
= true; /* Stop searching for more declarations. */
6707 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6708 || gfc_match_eos () != MATCH_YES
)
6710 gfc_error ("Expected %<,%> or end of statement at %C");
6718 gfc_match_external (void)
6721 gfc_clear_attr (¤t_attr
);
6722 current_attr
.external
= 1;
6724 return attr_decl ();
6729 gfc_match_intent (void)
6733 /* This is not allowed within a BLOCK construct! */
6734 if (gfc_current_state () == COMP_BLOCK
)
6736 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6740 intent
= match_intent_spec ();
6741 if (intent
== INTENT_UNKNOWN
)
6744 gfc_clear_attr (¤t_attr
);
6745 current_attr
.intent
= intent
;
6747 return attr_decl ();
6752 gfc_match_intrinsic (void)
6755 gfc_clear_attr (¤t_attr
);
6756 current_attr
.intrinsic
= 1;
6758 return attr_decl ();
6763 gfc_match_optional (void)
6765 /* This is not allowed within a BLOCK construct! */
6766 if (gfc_current_state () == COMP_BLOCK
)
6768 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6772 gfc_clear_attr (¤t_attr
);
6773 current_attr
.optional
= 1;
6775 return attr_decl ();
6780 gfc_match_pointer (void)
6782 gfc_gobble_whitespace ();
6783 if (gfc_peek_ascii_char () == '(')
6785 if (!flag_cray_pointer
)
6787 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6791 return cray_pointer_decl ();
6795 gfc_clear_attr (¤t_attr
);
6796 current_attr
.pointer
= 1;
6798 return attr_decl ();
6804 gfc_match_allocatable (void)
6806 gfc_clear_attr (¤t_attr
);
6807 current_attr
.allocatable
= 1;
6809 return attr_decl ();
6814 gfc_match_codimension (void)
6816 gfc_clear_attr (¤t_attr
);
6817 current_attr
.codimension
= 1;
6819 return attr_decl ();
6824 gfc_match_contiguous (void)
6826 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
6829 gfc_clear_attr (¤t_attr
);
6830 current_attr
.contiguous
= 1;
6832 return attr_decl ();
6837 gfc_match_dimension (void)
6839 gfc_clear_attr (¤t_attr
);
6840 current_attr
.dimension
= 1;
6842 return attr_decl ();
6847 gfc_match_target (void)
6849 gfc_clear_attr (¤t_attr
);
6850 current_attr
.target
= 1;
6852 return attr_decl ();
6856 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6860 access_attr_decl (gfc_statement st
)
6862 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6863 interface_type type
;
6865 gfc_symbol
*sym
, *dt_sym
;
6866 gfc_intrinsic_op op
;
6869 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6874 m
= gfc_match_generic_spec (&type
, name
, &op
);
6877 if (m
== MATCH_ERROR
)
6882 case INTERFACE_NAMELESS
:
6883 case INTERFACE_ABSTRACT
:
6886 case INTERFACE_GENERIC
:
6887 if (gfc_get_symbol (name
, NULL
, &sym
))
6890 if (!gfc_add_access (&sym
->attr
,
6892 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6896 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6897 && !gfc_add_access (&dt_sym
->attr
,
6899 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6905 case INTERFACE_INTRINSIC_OP
:
6906 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6908 gfc_intrinsic_op other_op
;
6910 gfc_current_ns
->operator_access
[op
] =
6911 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6913 /* Handle the case if there is another op with the same
6914 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6915 other_op
= gfc_equivalent_op (op
);
6917 if (other_op
!= INTRINSIC_NONE
)
6918 gfc_current_ns
->operator_access
[other_op
] =
6919 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6924 gfc_error ("Access specification of the %s operator at %C has "
6925 "already been specified", gfc_op2string (op
));
6931 case INTERFACE_USER_OP
:
6932 uop
= gfc_get_uop (name
);
6934 if (uop
->access
== ACCESS_UNKNOWN
)
6936 uop
->access
= (st
== ST_PUBLIC
)
6937 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6941 gfc_error ("Access specification of the .%s. operator at %C "
6942 "has already been specified", sym
->name
);
6949 if (gfc_match_char (',') == MATCH_NO
)
6953 if (gfc_match_eos () != MATCH_YES
)
6958 gfc_syntax_error (st
);
6966 gfc_match_protected (void)
6971 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6973 gfc_error ("PROTECTED at %C only allowed in specification "
6974 "part of a module");
6979 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
6982 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6987 if (gfc_match_eos () == MATCH_YES
)
6992 m
= gfc_match_symbol (&sym
, 0);
6996 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7008 if (gfc_match_eos () == MATCH_YES
)
7010 if (gfc_match_char (',') != MATCH_YES
)
7017 gfc_error ("Syntax error in PROTECTED statement at %C");
7022 /* The PRIVATE statement is a bit weird in that it can be an attribute
7023 declaration, but also works as a standalone statement inside of a
7024 type declaration or a module. */
7027 gfc_match_private (gfc_statement
*st
)
7030 if (gfc_match ("private") != MATCH_YES
)
7033 if (gfc_current_state () != COMP_MODULE
7034 && !(gfc_current_state () == COMP_DERIVED
7035 && gfc_state_stack
->previous
7036 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
7037 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7038 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
7039 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
7041 gfc_error ("PRIVATE statement at %C is only allowed in the "
7042 "specification part of a module");
7046 if (gfc_current_state () == COMP_DERIVED
)
7048 if (gfc_match_eos () == MATCH_YES
)
7054 gfc_syntax_error (ST_PRIVATE
);
7058 if (gfc_match_eos () == MATCH_YES
)
7065 return access_attr_decl (ST_PRIVATE
);
7070 gfc_match_public (gfc_statement
*st
)
7073 if (gfc_match ("public") != MATCH_YES
)
7076 if (gfc_current_state () != COMP_MODULE
)
7078 gfc_error ("PUBLIC statement at %C is only allowed in the "
7079 "specification part of a module");
7083 if (gfc_match_eos () == MATCH_YES
)
7090 return access_attr_decl (ST_PUBLIC
);
7094 /* Workhorse for gfc_match_parameter. */
7104 m
= gfc_match_symbol (&sym
, 0);
7106 gfc_error ("Expected variable name at %C in PARAMETER statement");
7111 if (gfc_match_char ('=') == MATCH_NO
)
7113 gfc_error ("Expected = sign in PARAMETER statement at %C");
7117 m
= gfc_match_init_expr (&init
);
7119 gfc_error ("Expected expression at %C in PARAMETER statement");
7123 if (sym
->ts
.type
== BT_UNKNOWN
7124 && !gfc_set_default_type (sym
, 1, NULL
))
7130 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7131 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7139 gfc_error ("Initializing already initialized variable at %C");
7144 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7145 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7148 gfc_free_expr (init
);
7153 /* Match a parameter statement, with the weird syntax that these have. */
7156 gfc_match_parameter (void)
7160 if (gfc_match_char ('(') == MATCH_NO
)
7169 if (gfc_match (" )%t") == MATCH_YES
)
7172 if (gfc_match_char (',') != MATCH_YES
)
7174 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7184 /* Save statements have a special syntax. */
7187 gfc_match_save (void)
7189 char n
[GFC_MAX_SYMBOL_LEN
+1];
7194 if (gfc_match_eos () == MATCH_YES
)
7196 if (gfc_current_ns
->seen_save
)
7198 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7199 "follows previous SAVE statement"))
7203 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7207 if (gfc_current_ns
->save_all
)
7209 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7210 "blanket SAVE statement"))
7218 m
= gfc_match_symbol (&sym
, 0);
7222 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7223 &gfc_current_locus
))
7234 m
= gfc_match (" / %n /", &n
);
7235 if (m
== MATCH_ERROR
)
7240 c
= gfc_get_common (n
, 0);
7243 gfc_current_ns
->seen_save
= 1;
7246 if (gfc_match_eos () == MATCH_YES
)
7248 if (gfc_match_char (',') != MATCH_YES
)
7255 gfc_error ("Syntax error in SAVE statement at %C");
7261 gfc_match_value (void)
7266 /* This is not allowed within a BLOCK construct! */
7267 if (gfc_current_state () == COMP_BLOCK
)
7269 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7273 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7276 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7281 if (gfc_match_eos () == MATCH_YES
)
7286 m
= gfc_match_symbol (&sym
, 0);
7290 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7302 if (gfc_match_eos () == MATCH_YES
)
7304 if (gfc_match_char (',') != MATCH_YES
)
7311 gfc_error ("Syntax error in VALUE statement at %C");
7317 gfc_match_volatile (void)
7322 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7325 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7330 if (gfc_match_eos () == MATCH_YES
)
7335 /* VOLATILE is special because it can be added to host-associated
7336 symbols locally. Except for coarrays. */
7337 m
= gfc_match_symbol (&sym
, 1);
7341 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7342 for variable in a BLOCK which is defined outside of the BLOCK. */
7343 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7345 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7346 "%C, which is use-/host-associated", sym
->name
);
7349 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7361 if (gfc_match_eos () == MATCH_YES
)
7363 if (gfc_match_char (',') != MATCH_YES
)
7370 gfc_error ("Syntax error in VOLATILE statement at %C");
7376 gfc_match_asynchronous (void)
7381 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7384 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7389 if (gfc_match_eos () == MATCH_YES
)
7394 /* ASYNCHRONOUS is special because it can be added to host-associated
7396 m
= gfc_match_symbol (&sym
, 1);
7400 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7412 if (gfc_match_eos () == MATCH_YES
)
7414 if (gfc_match_char (',') != MATCH_YES
)
7421 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7426 /* Match a module procedure statement. Note that we have to modify
7427 symbols in the parent's namespace because the current one was there
7428 to receive symbols that are in an interface's formal argument list. */
7431 gfc_match_modproc (void)
7433 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7437 gfc_namespace
*module_ns
;
7438 gfc_interface
*old_interface_head
, *interface
;
7440 if (gfc_state_stack
->state
!= COMP_INTERFACE
7441 || gfc_state_stack
->previous
== NULL
7442 || current_interface
.type
== INTERFACE_NAMELESS
7443 || current_interface
.type
== INTERFACE_ABSTRACT
)
7445 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7450 module_ns
= gfc_current_ns
->parent
;
7451 for (; module_ns
; module_ns
= module_ns
->parent
)
7452 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7453 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7454 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7455 && !module_ns
->proc_name
->attr
.contained
))
7458 if (module_ns
== NULL
)
7461 /* Store the current state of the interface. We will need it if we
7462 end up with a syntax error and need to recover. */
7463 old_interface_head
= gfc_current_interface_head ();
7465 /* Check if the F2008 optional double colon appears. */
7466 gfc_gobble_whitespace ();
7467 old_locus
= gfc_current_locus
;
7468 if (gfc_match ("::") == MATCH_YES
)
7470 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7471 "MODULE PROCEDURE statement at %L", &old_locus
))
7475 gfc_current_locus
= old_locus
;
7480 old_locus
= gfc_current_locus
;
7482 m
= gfc_match_name (name
);
7488 /* Check for syntax error before starting to add symbols to the
7489 current namespace. */
7490 if (gfc_match_eos () == MATCH_YES
)
7493 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7496 /* Now we're sure the syntax is valid, we process this item
7498 if (gfc_get_symbol (name
, module_ns
, &sym
))
7501 if (sym
->attr
.intrinsic
)
7503 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7504 "PROCEDURE", &old_locus
);
7508 if (sym
->attr
.proc
!= PROC_MODULE
7509 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7512 if (!gfc_add_interface (sym
))
7515 sym
->attr
.mod_proc
= 1;
7516 sym
->declared_at
= old_locus
;
7525 /* Restore the previous state of the interface. */
7526 interface
= gfc_current_interface_head ();
7527 gfc_set_current_interface_head (old_interface_head
);
7529 /* Free the new interfaces. */
7530 while (interface
!= old_interface_head
)
7532 gfc_interface
*i
= interface
->next
;
7537 /* And issue a syntax error. */
7538 gfc_syntax_error (ST_MODULE_PROC
);
7543 /* Check a derived type that is being extended. */
7546 check_extended_derived_type (char *name
)
7548 gfc_symbol
*extended
;
7550 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7552 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7556 extended
= gfc_find_dt_in_generic (extended
);
7561 gfc_error ("Symbol %qs at %C has not been previously defined", name
);
7565 if (extended
->attr
.flavor
!= FL_DERIVED
)
7567 gfc_error ("%qs in EXTENDS expression at %C is not a "
7568 "derived type", name
);
7572 if (extended
->attr
.is_bind_c
)
7574 gfc_error ("%qs cannot be extended at %C because it "
7575 "is BIND(C)", extended
->name
);
7579 if (extended
->attr
.sequence
)
7581 gfc_error ("%qs cannot be extended at %C because it "
7582 "is a SEQUENCE type", extended
->name
);
7590 /* Match the optional attribute specifiers for a type declaration.
7591 Return MATCH_ERROR if an error is encountered in one of the handled
7592 attributes (public, private, bind(c)), MATCH_NO if what's found is
7593 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7594 checking on attribute conflicts needs to be done. */
7597 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7599 /* See if the derived type is marked as private. */
7600 if (gfc_match (" , private") == MATCH_YES
)
7602 if (gfc_current_state () != COMP_MODULE
)
7604 gfc_error ("Derived type at %C can only be PRIVATE in the "
7605 "specification part of a module");
7609 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7612 else if (gfc_match (" , public") == MATCH_YES
)
7614 if (gfc_current_state () != COMP_MODULE
)
7616 gfc_error ("Derived type at %C can only be PUBLIC in the "
7617 "specification part of a module");
7621 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7624 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7626 /* If the type is defined to be bind(c) it then needs to make
7627 sure that all fields are interoperable. This will
7628 need to be a semantic check on the finished derived type.
7629 See 15.2.3 (lines 9-12) of F2003 draft. */
7630 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
7633 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7635 else if (gfc_match (" , abstract") == MATCH_YES
)
7637 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
7640 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
7643 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
7645 if (!gfc_add_extension (attr
, &gfc_current_locus
))
7651 /* If we get here, something matched. */
7656 /* Match the beginning of a derived type declaration. If a type name
7657 was the result of a function, then it is possible to have a symbol
7658 already to be known as a derived type yet have no components. */
7661 gfc_match_derived_decl (void)
7663 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7664 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7665 symbol_attribute attr
;
7666 gfc_symbol
*sym
, *gensym
;
7667 gfc_symbol
*extended
;
7669 match is_type_attr_spec
= MATCH_NO
;
7670 bool seen_attr
= false;
7671 gfc_interface
*intr
= NULL
, *head
;
7673 if (gfc_current_state () == COMP_DERIVED
)
7678 gfc_clear_attr (&attr
);
7683 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7684 if (is_type_attr_spec
== MATCH_ERROR
)
7686 if (is_type_attr_spec
== MATCH_YES
)
7688 } while (is_type_attr_spec
== MATCH_YES
);
7690 /* Deal with derived type extensions. The extension attribute has
7691 been added to 'attr' but now the parent type must be found and
7694 extended
= check_extended_derived_type (parent
);
7696 if (parent
[0] && !extended
)
7699 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7701 gfc_error ("Expected :: in TYPE definition at %C");
7705 m
= gfc_match (" %n%t", name
);
7709 /* Make sure the name is not the name of an intrinsic type. */
7710 if (gfc_is_intrinsic_typename (name
))
7712 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
7717 if (gfc_get_symbol (name
, NULL
, &gensym
))
7720 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7722 gfc_error ("Derived type name %qs at %C already has a basic type "
7723 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7727 if (!gensym
->attr
.generic
7728 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
7731 if (!gensym
->attr
.function
7732 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
7735 sym
= gfc_find_dt_in_generic (gensym
);
7737 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7739 gfc_error ("Derived type definition of %qs at %C has already been "
7740 "defined", sym
->name
);
7746 /* Use upper case to save the actual derived-type symbol. */
7747 gfc_get_symbol (gfc_get_string ("%c%s",
7748 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7749 &gensym
->name
[1]), NULL
, &sym
);
7750 sym
->name
= gfc_get_string (gensym
->name
);
7751 head
= gensym
->generic
;
7752 intr
= gfc_get_interface ();
7754 intr
->where
= gfc_current_locus
;
7755 intr
->sym
->declared_at
= gfc_current_locus
;
7757 gensym
->generic
= intr
;
7758 gensym
->attr
.if_source
= IFSRC_DECL
;
7761 /* The symbol may already have the derived attribute without the
7762 components. The ways this can happen is via a function
7763 definition, an INTRINSIC statement or a subtype in another
7764 derived type that is a pointer. The first part of the AND clause
7765 is true if the symbol is not the return value of a function. */
7766 if (sym
->attr
.flavor
!= FL_DERIVED
7767 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
7770 if (attr
.access
!= ACCESS_UNKNOWN
7771 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
7773 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7774 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7775 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
7779 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7780 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7781 gensym
->attr
.access
= sym
->attr
.access
;
7783 /* See if the derived type was labeled as bind(c). */
7784 if (attr
.is_bind_c
!= 0)
7785 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7787 /* Construct the f2k_derived namespace if it is not yet there. */
7788 if (!sym
->f2k_derived
)
7789 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7791 if (extended
&& !sym
->components
)
7795 /* Add the extended derived type as the first component. */
7796 gfc_add_component (sym
, parent
, &p
);
7798 gfc_set_sym_referenced (extended
);
7800 p
->ts
.type
= BT_DERIVED
;
7801 p
->ts
.u
.derived
= extended
;
7802 p
->initializer
= gfc_default_initializer (&p
->ts
);
7804 /* Set extension level. */
7805 if (extended
->attr
.extension
== 255)
7807 /* Since the extension field is 8 bit wide, we can only have
7808 up to 255 extension levels. */
7809 gfc_error ("Maximum extension level reached with type %qs at %L",
7810 extended
->name
, &extended
->declared_at
);
7813 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7815 /* Provide the links between the extended type and its extension. */
7816 if (!extended
->f2k_derived
)
7817 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7820 if (!sym
->hash_value
)
7821 /* Set the hash for the compound name for this type. */
7822 sym
->hash_value
= gfc_hash_value (sym
);
7824 /* Take over the ABSTRACT attribute. */
7825 sym
->attr
.abstract
= attr
.abstract
;
7827 gfc_new_block
= sym
;
7833 /* Cray Pointees can be declared as:
7834 pointer (ipt, a (n,m,...,*)) */
7837 gfc_mod_pointee_as (gfc_array_spec
*as
)
7839 as
->cray_pointee
= true; /* This will be useful to know later. */
7840 if (as
->type
== AS_ASSUMED_SIZE
)
7841 as
->cp_was_assumed
= true;
7842 else if (as
->type
== AS_ASSUMED_SHAPE
)
7844 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7851 /* Match the enum definition statement, here we are trying to match
7852 the first line of enum definition statement.
7853 Returns MATCH_YES if match is found. */
7856 gfc_match_enum (void)
7860 m
= gfc_match_eos ();
7864 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
7871 /* Returns an initializer whose value is one higher than the value of the
7872 LAST_INITIALIZER argument. If the argument is NULL, the
7873 initializers value will be set to zero. The initializer's kind
7874 will be set to gfc_c_int_kind.
7876 If -fshort-enums is given, the appropriate kind will be selected
7877 later after all enumerators have been parsed. A warning is issued
7878 here if an initializer exceeds gfc_c_int_kind. */
7881 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7884 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7886 mpz_init (result
->value
.integer
);
7888 if (last_initializer
!= NULL
)
7890 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7891 result
->where
= last_initializer
->where
;
7893 if (gfc_check_integer_range (result
->value
.integer
,
7894 gfc_c_int_kind
) != ARITH_OK
)
7896 gfc_error ("Enumerator exceeds the C integer type at %C");
7902 /* Control comes here, if it's the very first enumerator and no
7903 initializer has been given. It will be initialized to zero. */
7904 mpz_set_si (result
->value
.integer
, 0);
7911 /* Match a variable name with an optional initializer. When this
7912 subroutine is called, a variable is expected to be parsed next.
7913 Depending on what is happening at the moment, updates either the
7914 symbol table or the current interface. */
7917 enumerator_decl (void)
7919 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7920 gfc_expr
*initializer
;
7921 gfc_array_spec
*as
= NULL
;
7929 old_locus
= gfc_current_locus
;
7931 /* When we get here, we've just matched a list of attributes and
7932 maybe a type and a double colon. The next thing we expect to see
7933 is the name of the symbol. */
7934 m
= gfc_match_name (name
);
7938 var_locus
= gfc_current_locus
;
7940 /* OK, we've successfully matched the declaration. Now put the
7941 symbol in the current namespace. If we fail to create the symbol,
7943 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
7949 /* The double colon must be present in order to have initializers.
7950 Otherwise the statement is ambiguous with an assignment statement. */
7953 if (gfc_match_char ('=') == MATCH_YES
)
7955 m
= gfc_match_init_expr (&initializer
);
7958 gfc_error ("Expected an initialization expression at %C");
7967 /* If we do not have an initializer, the initialization value of the
7968 previous enumerator (stored in last_initializer) is incremented
7969 by 1 and is used to initialize the current enumerator. */
7970 if (initializer
== NULL
)
7971 initializer
= enum_initializer (last_initializer
, old_locus
);
7973 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7975 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7981 /* Store this current initializer, for the next enumerator variable
7982 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7983 use last_initializer below. */
7984 last_initializer
= initializer
;
7985 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7987 /* Maintain enumerator history. */
7988 gfc_find_symbol (name
, NULL
, 0, &sym
);
7989 create_enum_history (sym
, last_initializer
);
7991 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7994 /* Free stuff up and return. */
7995 gfc_free_expr (initializer
);
8001 /* Match the enumerator definition statement. */
8004 gfc_match_enumerator_def (void)
8009 gfc_clear_ts (¤t_ts
);
8011 m
= gfc_match (" enumerator");
8015 m
= gfc_match (" :: ");
8016 if (m
== MATCH_ERROR
)
8019 colon_seen
= (m
== MATCH_YES
);
8021 if (gfc_current_state () != COMP_ENUM
)
8023 gfc_error ("ENUM definition statement expected before %C");
8024 gfc_free_enum_history ();
8028 (¤t_ts
)->type
= BT_INTEGER
;
8029 (¤t_ts
)->kind
= gfc_c_int_kind
;
8031 gfc_clear_attr (¤t_attr
);
8032 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
8041 m
= enumerator_decl ();
8042 if (m
== MATCH_ERROR
)
8044 gfc_free_enum_history ();
8050 if (gfc_match_eos () == MATCH_YES
)
8052 if (gfc_match_char (',') != MATCH_YES
)
8056 if (gfc_current_state () == COMP_ENUM
)
8058 gfc_free_enum_history ();
8059 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8064 gfc_free_array_spec (current_as
);
8071 /* Match binding attributes. */
8074 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
8076 bool found_passing
= false;
8077 bool seen_ptr
= false;
8078 match m
= MATCH_YES
;
8080 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8081 this case the defaults are in there. */
8082 ba
->access
= ACCESS_UNKNOWN
;
8083 ba
->pass_arg
= NULL
;
8084 ba
->pass_arg_num
= 0;
8086 ba
->non_overridable
= 0;
8090 /* If we find a comma, we believe there are binding attributes. */
8091 m
= gfc_match_char (',');
8097 /* Access specifier. */
8099 m
= gfc_match (" public");
8100 if (m
== MATCH_ERROR
)
8104 if (ba
->access
!= ACCESS_UNKNOWN
)
8106 gfc_error ("Duplicate access-specifier at %C");
8110 ba
->access
= ACCESS_PUBLIC
;
8114 m
= gfc_match (" private");
8115 if (m
== MATCH_ERROR
)
8119 if (ba
->access
!= ACCESS_UNKNOWN
)
8121 gfc_error ("Duplicate access-specifier at %C");
8125 ba
->access
= ACCESS_PRIVATE
;
8129 /* If inside GENERIC, the following is not allowed. */
8134 m
= gfc_match (" nopass");
8135 if (m
== MATCH_ERROR
)
8141 gfc_error ("Binding attributes already specify passing,"
8142 " illegal NOPASS at %C");
8146 found_passing
= true;
8151 /* PASS possibly including argument. */
8152 m
= gfc_match (" pass");
8153 if (m
== MATCH_ERROR
)
8157 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8161 gfc_error ("Binding attributes already specify passing,"
8162 " illegal PASS at %C");
8166 m
= gfc_match (" ( %n )", arg
);
8167 if (m
== MATCH_ERROR
)
8170 ba
->pass_arg
= gfc_get_string (arg
);
8171 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8173 found_passing
= true;
8181 m
= gfc_match (" pointer");
8182 if (m
== MATCH_ERROR
)
8188 gfc_error ("Duplicate POINTER attribute at %C");
8198 /* NON_OVERRIDABLE flag. */
8199 m
= gfc_match (" non_overridable");
8200 if (m
== MATCH_ERROR
)
8204 if (ba
->non_overridable
)
8206 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8210 ba
->non_overridable
= 1;
8214 /* DEFERRED flag. */
8215 m
= gfc_match (" deferred");
8216 if (m
== MATCH_ERROR
)
8222 gfc_error ("Duplicate DEFERRED at %C");
8233 /* Nothing matching found. */
8235 gfc_error ("Expected access-specifier at %C");
8237 gfc_error ("Expected binding attribute at %C");
8240 while (gfc_match_char (',') == MATCH_YES
);
8242 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8243 if (ba
->non_overridable
&& ba
->deferred
)
8245 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8252 if (ba
->access
== ACCESS_UNKNOWN
)
8253 ba
->access
= gfc_typebound_default_access
;
8255 if (ppc
&& !seen_ptr
)
8257 gfc_error ("POINTER attribute is required for procedure pointer component"
8269 /* Match a PROCEDURE specific binding inside a derived type. */
8272 match_procedure_in_type (void)
8274 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8275 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8276 char* target
= NULL
, *ifc
= NULL
;
8277 gfc_typebound_proc tb
;
8286 /* Check current state. */
8287 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8288 block
= gfc_state_stack
->previous
->sym
;
8291 /* Try to match PROCEDURE(interface). */
8292 if (gfc_match (" (") == MATCH_YES
)
8294 m
= gfc_match_name (target_buf
);
8295 if (m
== MATCH_ERROR
)
8299 gfc_error ("Interface-name expected after %<(%> at %C");
8303 if (gfc_match (" )") != MATCH_YES
)
8305 gfc_error ("%<)%> expected at %C");
8312 /* Construct the data structure. */
8313 memset (&tb
, 0, sizeof (tb
));
8314 tb
.where
= gfc_current_locus
;
8316 /* Match binding attributes. */
8317 m
= match_binding_attributes (&tb
, false, false);
8318 if (m
== MATCH_ERROR
)
8320 seen_attrs
= (m
== MATCH_YES
);
8322 /* Check that attribute DEFERRED is given if an interface is specified. */
8323 if (tb
.deferred
&& !ifc
)
8325 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8328 if (ifc
&& !tb
.deferred
)
8330 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8334 /* Match the colons. */
8335 m
= gfc_match (" ::");
8336 if (m
== MATCH_ERROR
)
8338 seen_colons
= (m
== MATCH_YES
);
8339 if (seen_attrs
&& !seen_colons
)
8341 gfc_error ("Expected %<::%> after binding-attributes at %C");
8345 /* Match the binding names. */
8348 m
= gfc_match_name (name
);
8349 if (m
== MATCH_ERROR
)
8353 gfc_error ("Expected binding name at %C");
8357 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8360 /* Try to match the '=> target', if it's there. */
8362 m
= gfc_match (" =>");
8363 if (m
== MATCH_ERROR
)
8369 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8375 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8380 m
= gfc_match_name (target_buf
);
8381 if (m
== MATCH_ERROR
)
8385 gfc_error ("Expected binding target after %<=>%> at %C");
8388 target
= target_buf
;
8391 /* If no target was found, it has the same name as the binding. */
8395 /* Get the namespace to insert the symbols into. */
8396 ns
= block
->f2k_derived
;
8399 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8400 if (tb
.deferred
&& !block
->attr
.abstract
)
8402 gfc_error ("Type %qs containing DEFERRED binding at %C "
8403 "is not ABSTRACT", block
->name
);
8407 /* See if we already have a binding with this name in the symtree which
8408 would be an error. If a GENERIC already targeted this binding, it may
8409 be already there but then typebound is still NULL. */
8410 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8411 if (stree
&& stree
->n
.tb
)
8413 gfc_error ("There is already a procedure with binding name %qs for "
8414 "the derived type %qs at %C", name
, block
->name
);
8418 /* Insert it and set attributes. */
8422 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8425 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8427 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8430 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8432 if (gfc_match_eos () == MATCH_YES
)
8434 if (gfc_match_char (',') != MATCH_YES
)
8439 gfc_error ("Syntax error in PROCEDURE statement at %C");
8444 /* Match a GENERIC procedure binding inside a derived type. */
8447 gfc_match_generic (void)
8449 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8450 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8452 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8453 gfc_typebound_proc
* tb
;
8455 interface_type op_type
;
8456 gfc_intrinsic_op op
;
8459 /* Check current state. */
8460 if (gfc_current_state () == COMP_DERIVED
)
8462 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8465 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8467 block
= gfc_state_stack
->previous
->sym
;
8468 ns
= block
->f2k_derived
;
8469 gcc_assert (block
&& ns
);
8471 memset (&tbattr
, 0, sizeof (tbattr
));
8472 tbattr
.where
= gfc_current_locus
;
8474 /* See if we get an access-specifier. */
8475 m
= match_binding_attributes (&tbattr
, true, false);
8476 if (m
== MATCH_ERROR
)
8479 /* Now the colons, those are required. */
8480 if (gfc_match (" ::") != MATCH_YES
)
8482 gfc_error ("Expected %<::%> at %C");
8486 /* Match the binding name; depending on type (operator / generic) format
8487 it for future error messages into bind_name. */
8489 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8490 if (m
== MATCH_ERROR
)
8494 gfc_error ("Expected generic name or operator descriptor at %C");
8500 case INTERFACE_GENERIC
:
8501 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8504 case INTERFACE_USER_OP
:
8505 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8508 case INTERFACE_INTRINSIC_OP
:
8509 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8510 gfc_op2string (op
));
8517 /* Match the required =>. */
8518 if (gfc_match (" =>") != MATCH_YES
)
8520 gfc_error ("Expected %<=>%> at %C");
8524 /* Try to find existing GENERIC binding with this name / for this operator;
8525 if there is something, check that it is another GENERIC and then extend
8526 it rather than building a new node. Otherwise, create it and put it
8527 at the right position. */
8531 case INTERFACE_USER_OP
:
8532 case INTERFACE_GENERIC
:
8534 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8537 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8549 case INTERFACE_INTRINSIC_OP
:
8559 if (!tb
->is_generic
)
8561 gcc_assert (op_type
== INTERFACE_GENERIC
);
8562 gfc_error ("There's already a non-generic procedure with binding name"
8563 " %qs for the derived type %qs at %C",
8564 bind_name
, block
->name
);
8568 if (tb
->access
!= tbattr
.access
)
8570 gfc_error ("Binding at %C must have the same access as already"
8571 " defined binding %qs", bind_name
);
8577 tb
= gfc_get_typebound_proc (NULL
);
8578 tb
->where
= gfc_current_locus
;
8579 tb
->access
= tbattr
.access
;
8581 tb
->u
.generic
= NULL
;
8585 case INTERFACE_GENERIC
:
8586 case INTERFACE_USER_OP
:
8588 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8591 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8599 case INTERFACE_INTRINSIC_OP
:
8608 /* Now, match all following names as specific targets. */
8611 gfc_symtree
* target_st
;
8612 gfc_tbp_generic
* target
;
8614 m
= gfc_match_name (name
);
8615 if (m
== MATCH_ERROR
)
8619 gfc_error ("Expected specific binding name at %C");
8623 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8625 /* See if this is a duplicate specification. */
8626 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8627 if (target_st
== target
->specific_st
)
8629 gfc_error ("%qs already defined as specific binding for the"
8630 " generic %qs at %C", name
, bind_name
);
8634 target
= gfc_get_tbp_generic ();
8635 target
->specific_st
= target_st
;
8636 target
->specific
= NULL
;
8637 target
->next
= tb
->u
.generic
;
8638 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8639 || (op_type
== INTERFACE_INTRINSIC_OP
));
8640 tb
->u
.generic
= target
;
8642 while (gfc_match (" ,") == MATCH_YES
);
8644 /* Here should be the end. */
8645 if (gfc_match_eos () != MATCH_YES
)
8647 gfc_error ("Junk after GENERIC binding at %C");
8658 /* Match a FINAL declaration inside a derived type. */
8661 gfc_match_final_decl (void)
8663 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8666 gfc_namespace
* module_ns
;
8670 if (gfc_current_form
== FORM_FREE
)
8672 char c
= gfc_peek_ascii_char ();
8673 if (!gfc_is_whitespace (c
) && c
!= ':')
8677 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8679 if (gfc_current_form
== FORM_FIXED
)
8682 gfc_error ("FINAL declaration at %C must be inside a derived type "
8683 "CONTAINS section");
8687 block
= gfc_state_stack
->previous
->sym
;
8690 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8691 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8693 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8694 " specification part of a MODULE");
8698 module_ns
= gfc_current_ns
;
8699 gcc_assert (module_ns
);
8700 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8702 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8703 if (gfc_match (" ::") == MATCH_ERROR
)
8706 /* Match the sequence of procedure names. */
8713 if (first
&& gfc_match_eos () == MATCH_YES
)
8715 gfc_error ("Empty FINAL at %C");
8719 m
= gfc_match_name (name
);
8722 gfc_error ("Expected module procedure name at %C");
8725 else if (m
!= MATCH_YES
)
8728 if (gfc_match_eos () == MATCH_YES
)
8730 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8732 gfc_error ("Expected %<,%> at %C");
8736 if (gfc_get_symbol (name
, module_ns
, &sym
))
8738 gfc_error ("Unknown procedure name %qs at %C", name
);
8742 /* Mark the symbol as module procedure. */
8743 if (sym
->attr
.proc
!= PROC_MODULE
8744 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
8747 /* Check if we already have this symbol in the list, this is an error. */
8748 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8749 if (f
->proc_sym
== sym
)
8751 gfc_error ("%qs at %C is already defined as FINAL procedure!",
8756 /* Add this symbol to the list of finalizers. */
8757 gcc_assert (block
->f2k_derived
);
8759 f
= XCNEW (gfc_finalizer
);
8761 f
->proc_tree
= NULL
;
8762 f
->where
= gfc_current_locus
;
8763 f
->next
= block
->f2k_derived
->finalizers
;
8764 block
->f2k_derived
->finalizers
= f
;
8774 const ext_attr_t ext_attr_list
[] = {
8775 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8776 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8777 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8778 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8779 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8780 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
8781 { NULL
, EXT_ATTR_LAST
, NULL
}
8784 /* Match a !GCC$ ATTRIBUTES statement of the form:
8785 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8786 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8788 TODO: We should support all GCC attributes using the same syntax for
8789 the attribute list, i.e. the list in C
8790 __attributes(( attribute-list ))
8792 !GCC$ ATTRIBUTES attribute-list ::
8793 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8796 As there is absolutely no risk of confusion, we should never return
8799 gfc_match_gcc_attributes (void)
8801 symbol_attribute attr
;
8802 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8807 gfc_clear_attr (&attr
);
8812 if (gfc_match_name (name
) != MATCH_YES
)
8815 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8816 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8819 if (id
== EXT_ATTR_LAST
)
8821 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8825 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
8828 gfc_gobble_whitespace ();
8829 ch
= gfc_next_ascii_char ();
8832 /* This is the successful exit condition for the loop. */
8833 if (gfc_next_ascii_char () == ':')
8843 if (gfc_match_eos () == MATCH_YES
)
8848 m
= gfc_match_name (name
);
8852 if (find_special (name
, &sym
, true))
8855 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8857 if (gfc_match_eos () == MATCH_YES
)
8860 if (gfc_match_char (',') != MATCH_YES
)
8867 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");