1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "constructor.h"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector
;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts
;
49 static symbol_attribute current_attr
;
50 static gfc_array_spec
*current_as
;
51 static int colon_seen
;
53 /* The current binding label (if any). */
54 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line
;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals
= 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr
*last_initializer
;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr
*initializer
;
74 struct enumerator_history
*next
;
78 /* Header of enum history chain. */
80 static enumerator_history
*enum_history
= NULL
;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history
*max_enum
= NULL
;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol
*gfc_new_block
;
90 bool gfc_matching_function
;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data
= false;
98 gfc_in_match_data (void)
100 return in_match_data
;
104 set_in_match_data (bool set_value
)
106 in_match_data
= set_value
;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable
*p
)
114 gfc_data_variable
*q
;
119 gfc_free_expr (p
->expr
);
120 gfc_free_iterator (&p
->iter
, 0);
121 free_variable (p
->list
);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value
*p
)
137 mpz_clear (p
->repeat
);
138 gfc_free_expr (p
->expr
);
144 /* Free a list of gfc_data structures. */
147 gfc_free_data (gfc_data
*p
)
154 free_variable (p
->var
);
155 free_value (p
->value
);
161 /* Free all data in a namespace. */
164 gfc_free_data_all (gfc_namespace
*ns
)
177 static match
var_element (gfc_data_variable
*);
179 /* Match a list of variables terminated by an iterator and a right
183 var_list (gfc_data_variable
*parent
)
185 gfc_data_variable
*tail
, var
;
188 m
= var_element (&var
);
189 if (m
== MATCH_ERROR
)
194 tail
= gfc_get_data_variable ();
201 if (gfc_match_char (',') != MATCH_YES
)
204 m
= gfc_match_iterator (&parent
->iter
, 1);
207 if (m
== MATCH_ERROR
)
210 m
= var_element (&var
);
211 if (m
== MATCH_ERROR
)
216 tail
->next
= gfc_get_data_variable ();
222 if (gfc_match_char (')') != MATCH_YES
)
227 gfc_syntax_error (ST_DATA
);
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
236 var_element (gfc_data_variable
*new_var
)
241 memset (new_var
, 0, sizeof (gfc_data_variable
));
243 if (gfc_match_char ('(') == MATCH_YES
)
244 return var_list (new_var
);
246 m
= gfc_match_variable (&new_var
->expr
, 0);
250 sym
= new_var
->expr
->symtree
->n
.sym
;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym
, gfc_current_ns
,
254 false, gfc_current_locus
) == FAILURE
)
257 if (!sym
->attr
.function
&& gfc_current_ns
->parent
258 && gfc_current_ns
->parent
== sym
->ns
)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym
->name
);
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym
->attr
.in_common
267 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym
->name
) == FAILURE
)
272 if (gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
) == FAILURE
)
279 /* Match the top-level list of data variables. */
282 top_var_list (gfc_data
*d
)
284 gfc_data_variable var
, *tail
, *new_var
;
291 m
= var_element (&var
);
294 if (m
== MATCH_ERROR
)
297 new_var
= gfc_get_data_variable ();
303 tail
->next
= new_var
;
307 if (gfc_match_char ('/') == MATCH_YES
)
309 if (gfc_match_char (',') != MATCH_YES
)
316 gfc_syntax_error (ST_DATA
);
317 gfc_free_data_all (gfc_current_ns
);
323 match_data_constant (gfc_expr
**result
)
325 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
331 m
= gfc_match_literal_constant (&expr
, 1);
338 if (m
== MATCH_ERROR
)
341 m
= gfc_match_null (result
);
345 old_loc
= gfc_current_locus
;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m
= gfc_match_rvalue (result
);
350 if (m
== MATCH_ERROR
)
353 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
355 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
360 gfc_current_locus
= old_loc
;
362 m
= gfc_match_name (name
);
366 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
370 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
376 else if (sym
->attr
.flavor
== FL_DERIVED
)
377 return gfc_match_structure_constructor (sym
, result
, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym
->value
->expr_type
== EXPR_ARRAY
)
382 gfc_current_locus
= old_loc
;
384 m
= gfc_match_init_expr (result
);
385 if (m
== MATCH_ERROR
)
390 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
393 if ((*result
)->expr_type
== EXPR_CONSTANT
)
397 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
403 *result
= gfc_copy_expr (sym
->value
);
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
412 top_val_list (gfc_data
*data
)
414 gfc_data_value
*new_val
, *tail
;
422 m
= match_data_constant (&expr
);
425 if (m
== MATCH_ERROR
)
428 new_val
= gfc_get_data_value ();
429 mpz_init (new_val
->repeat
);
432 data
->value
= new_val
;
434 tail
->next
= new_val
;
438 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
441 mpz_set_ui (tail
->repeat
, 1);
445 if (expr
->ts
.type
== BT_INTEGER
)
446 mpz_set (tail
->repeat
, expr
->value
.integer
);
447 gfc_free_expr (expr
);
449 m
= match_data_constant (&tail
->expr
);
452 if (m
== MATCH_ERROR
)
456 if (gfc_match_char ('/') == MATCH_YES
)
458 if (gfc_match_char (',') == MATCH_NO
)
465 gfc_syntax_error (ST_DATA
);
466 gfc_free_data_all (gfc_current_ns
);
471 /* Matches an old style initialization. */
474 match_old_style_init (const char *name
)
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name
, NULL
, 0, &st
);
485 newdata
= gfc_get_data ();
486 newdata
->var
= gfc_get_data_variable ();
487 newdata
->var
->expr
= gfc_get_variable_expr (st
);
488 newdata
->where
= gfc_current_locus
;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m
= top_val_list (newdata
);
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
505 if (gfc_implicit_pure (NULL
))
506 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
508 /* Mark the variable as having appeared in a data statement. */
509 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
515 /* Chain in namespace list of DATA initializers. */
516 newdata
->next
= gfc_current_ns
->data
;
517 gfc_current_ns
->data
= newdata
;
523 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
524 we are matching a DATA statement and are therefore issuing an error
525 if we encounter something unexpected, if not, we're trying to match
526 an old-style initialization expression of the form INTEGER I /2/. */
529 gfc_match_data (void)
534 set_in_match_data (true);
538 new_data
= gfc_get_data ();
539 new_data
->where
= gfc_current_locus
;
541 m
= top_var_list (new_data
);
545 m
= top_val_list (new_data
);
549 new_data
->next
= gfc_current_ns
->data
;
550 gfc_current_ns
->data
= new_data
;
552 if (gfc_match_eos () == MATCH_YES
)
555 gfc_match_char (','); /* Optional comma */
558 set_in_match_data (false);
562 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
566 if (gfc_implicit_pure (NULL
))
567 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
572 set_in_match_data (false);
573 gfc_free_data (new_data
);
578 /************************ Declaration statements *********************/
581 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
584 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
588 if (to
->rank
== 0 && from
->rank
> 0)
590 to
->rank
= from
->rank
;
591 to
->type
= from
->type
;
592 to
->cray_pointee
= from
->cray_pointee
;
593 to
->cp_was_assumed
= from
->cp_was_assumed
;
595 for (i
= 0; i
< to
->corank
; i
++)
597 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
598 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
600 for (i
= 0; i
< from
->rank
; i
++)
604 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
605 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
609 to
->lower
[i
] = from
->lower
[i
];
610 to
->upper
[i
] = from
->upper
[i
];
614 else if (to
->corank
== 0 && from
->corank
> 0)
616 to
->corank
= from
->corank
;
617 to
->cotype
= from
->cotype
;
619 for (i
= 0; i
< from
->corank
; i
++)
623 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
624 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
628 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
629 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
636 /* Match an intent specification. Since this can only happen after an
637 INTENT word, a legal intent-spec must follow. */
640 match_intent_spec (void)
643 if (gfc_match (" ( in out )") == MATCH_YES
)
645 if (gfc_match (" ( in )") == MATCH_YES
)
647 if (gfc_match (" ( out )") == MATCH_YES
)
650 gfc_error ("Bad INTENT specification at %C");
651 return INTENT_UNKNOWN
;
655 /* Matches a character length specification, which is either a
656 specification expression, '*', or ':'. */
659 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
666 if (gfc_match_char ('*') == MATCH_YES
)
669 if (gfc_match_char (':') == MATCH_YES
)
671 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: deferred type "
672 "parameter at %C") == FAILURE
)
680 m
= gfc_match_expr (expr
);
683 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
686 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
688 if ((*expr
)->value
.function
.actual
689 && (*expr
)->value
.function
.actual
->expr
->symtree
)
692 e
= (*expr
)->value
.function
.actual
->expr
;
693 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
694 && e
->expr_type
== EXPR_VARIABLE
)
696 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
698 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
699 && e
->symtree
->n
.sym
->ts
.u
.cl
700 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
708 gfc_error ("Conflict in attributes of function argument at %C");
713 /* A character length is a '*' followed by a literal integer or a
714 char_len_param_value in parenthesis. */
717 match_char_length (gfc_expr
**expr
, bool *deferred
)
723 m
= gfc_match_char ('*');
727 m
= gfc_match_small_literal_int (&length
, NULL
);
728 if (m
== MATCH_ERROR
)
733 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
734 "Old-style character length at %C") == FAILURE
)
736 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
740 if (gfc_match_char ('(') == MATCH_NO
)
743 m
= char_len_param_value (expr
, deferred
);
744 if (m
!= MATCH_YES
&& gfc_matching_function
)
750 if (m
== MATCH_ERROR
)
755 if (gfc_match_char (')') == MATCH_NO
)
757 gfc_free_expr (*expr
);
765 gfc_error ("Syntax error in character length specification at %C");
770 /* Special subroutine for finding a symbol. Check if the name is found
771 in the current name space. If not, and we're compiling a function or
772 subroutine and the parent compilation unit is an interface, then check
773 to see if the name we've been given is the name of the interface
774 (located in another namespace). */
777 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
783 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
786 *result
= st
? st
->n
.sym
: NULL
;
790 if (gfc_current_state () != COMP_SUBROUTINE
791 && gfc_current_state () != COMP_FUNCTION
)
794 s
= gfc_state_stack
->previous
;
798 if (s
->state
!= COMP_INTERFACE
)
801 goto end
; /* Nameless interface. */
803 if (strcmp (name
, s
->sym
->name
) == 0)
814 /* Special subroutine for getting a symbol node associated with a
815 procedure name, used in SUBROUTINE and FUNCTION statements. The
816 symbol is created in the parent using with symtree node in the
817 child unit pointing to the symbol. If the current namespace has no
818 parent, then the symbol is just created in the current unit. */
821 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
827 /* Module functions have to be left in their own namespace because
828 they have potentially (almost certainly!) already been referenced.
829 In this sense, they are rather like external functions. This is
830 fixed up in resolve.c(resolve_entries), where the symbol name-
831 space is set to point to the master function, so that the fake
832 result mechanism can work. */
833 if (module_fcn_entry
)
835 /* Present if entry is declared to be a module procedure. */
836 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
839 rc
= gfc_get_symbol (name
, NULL
, result
);
840 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
841 && (*result
)->ts
.type
== BT_UNKNOWN
842 && sym
->attr
.flavor
== FL_UNKNOWN
)
843 /* Pick up the typespec for the entry, if declared in the function
844 body. Note that this symbol is FL_UNKNOWN because it will
845 only have appeared in a type declaration. The local symtree
846 is set to point to the module symbol and a unique symtree
847 to the local version. This latter ensures a correct clearing
850 /* If the ENTRY proceeds its specification, we need to ensure
851 that this does not raise a "has no IMPLICIT type" error. */
852 if (sym
->ts
.type
== BT_UNKNOWN
)
853 sym
->attr
.untyped
= 1;
855 (*result
)->ts
= sym
->ts
;
857 /* Put the symbol in the procedure namespace so that, should
858 the ENTRY precede its specification, the specification
860 (*result
)->ns
= gfc_current_ns
;
862 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
864 st
= gfc_get_unique_symtree (gfc_current_ns
);
869 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
875 gfc_current_ns
->refs
++;
877 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
879 /* Trap another encompassed procedure with the same name. All
880 these conditions are necessary to avoid picking up an entry
881 whose name clashes with that of the encompassing procedure;
882 this is handled using gsymbols to register unique,globally
884 if (sym
->attr
.flavor
!= 0
885 && sym
->attr
.proc
!= 0
886 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
887 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
888 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
889 name
, &sym
->declared_at
);
891 /* Trap a procedure with a name the same as interface in the
892 encompassing scope. */
893 if (sym
->attr
.generic
!= 0
894 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
895 && !sym
->attr
.mod_proc
)
896 gfc_error_now ("Name '%s' at %C is already defined"
897 " as a generic interface at %L",
898 name
, &sym
->declared_at
);
900 /* Trap declarations of attributes in encompassing scope. The
901 signature for this is that ts.kind is set. Legitimate
902 references only set ts.type. */
903 if (sym
->ts
.kind
!= 0
904 && !sym
->attr
.implicit_type
905 && sym
->attr
.proc
== 0
906 && gfc_current_ns
->parent
!= NULL
907 && sym
->attr
.access
== 0
908 && !module_fcn_entry
)
909 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
910 "and must not have attributes declared at %L",
911 name
, &sym
->declared_at
);
914 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
917 /* Module function entries will already have a symtree in
918 the current namespace but will need one at module level. */
919 if (module_fcn_entry
)
921 /* Present if entry is declared to be a module procedure. */
922 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
924 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
927 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
932 /* See if the procedure should be a module procedure. */
934 if (((sym
->ns
->proc_name
!= NULL
935 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
936 && sym
->attr
.proc
!= PROC_MODULE
)
937 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
938 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
939 sym
->name
, NULL
) == FAILURE
)
946 /* Verify that the given symbol representing a parameter is C
947 interoperable, by checking to see if it was marked as such after
948 its declaration. If the given symbol is not interoperable, a
949 warning is reported, thus removing the need to return the status to
950 the calling function. The standard does not require the user use
951 one of the iso_c_binding named constants to declare an
952 interoperable parameter, but we can't be sure if the param is C
953 interop or not if the user doesn't. For example, integer(4) may be
954 legal Fortran, but doesn't have meaning in C. It may interop with
955 a number of the C types, which causes a problem because the
956 compiler can't know which one. This code is almost certainly not
957 portable, and the user will get what they deserve if the C type
958 across platforms isn't always interoperable with integer(4). If
959 the user had used something like integer(c_int) or integer(c_long),
960 the compiler could have automatically handled the varying sizes
964 verify_c_interop_param (gfc_symbol
*sym
)
966 int is_c_interop
= 0;
967 gfc_try retval
= SUCCESS
;
969 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
970 Don't repeat the checks here. */
971 if (sym
->attr
.implicit_type
)
974 /* For subroutines or functions that are passed to a BIND(C) procedure,
975 they're interoperable if they're BIND(C) and their params are all
977 if (sym
->attr
.flavor
== FL_PROCEDURE
)
979 if (sym
->attr
.is_bind_c
== 0)
981 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
982 "attribute to be C interoperable", sym
->name
,
983 &(sym
->declared_at
));
989 if (sym
->attr
.is_c_interop
== 1)
990 /* We've already checked this procedure; don't check it again. */
993 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
998 /* See if we've stored a reference to a procedure that owns sym. */
999 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1001 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1004 (verify_c_interop (&(sym
->ts
))
1005 == SUCCESS
? 1 : 0);
1007 if (is_c_interop
!= 1)
1009 /* Make personalized messages to give better feedback. */
1010 if (sym
->ts
.type
== BT_DERIVED
)
1011 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1012 "procedure '%s' but is not C interoperable "
1013 "because derived type '%s' is not C interoperable",
1014 sym
->name
, &(sym
->declared_at
),
1015 sym
->ns
->proc_name
->name
,
1016 sym
->ts
.u
.derived
->name
);
1018 gfc_warning ("Variable '%s' at %L is a parameter to the "
1019 "BIND(C) procedure '%s' but may not be C "
1021 sym
->name
, &(sym
->declared_at
),
1022 sym
->ns
->proc_name
->name
);
1025 /* Character strings are only C interoperable if they have a
1027 if (sym
->ts
.type
== BT_CHARACTER
)
1029 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1030 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1031 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1033 gfc_error ("Character argument '%s' at %L "
1034 "must be length 1 because "
1035 "procedure '%s' is BIND(C)",
1036 sym
->name
, &sym
->declared_at
,
1037 sym
->ns
->proc_name
->name
);
1042 /* We have to make sure that any param to a bind(c) routine does
1043 not have the allocatable, pointer, or optional attributes,
1044 according to J3/04-007, section 5.1. */
1045 if (sym
->attr
.allocatable
== 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "ALLOCATABLE attribute because procedure '%s'"
1049 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1050 sym
->ns
->proc_name
->name
);
1054 if (sym
->attr
.pointer
== 1)
1056 gfc_error ("Variable '%s' at %L cannot have the "
1057 "POINTER attribute because procedure '%s'"
1058 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1059 sym
->ns
->proc_name
->name
);
1063 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1065 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1066 "and the VALUE attribute because procedure '%s' "
1067 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1068 sym
->ns
->proc_name
->name
);
1071 else if (sym
->attr
.optional
== 1
1072 && gfc_notify_std (GFC_STD_F2008_TR
, "TR29113: Variable '%s' "
1073 "at %L with OPTIONAL attribute in "
1074 "procedure '%s' which is BIND(C)",
1075 sym
->name
, &(sym
->declared_at
),
1076 sym
->ns
->proc_name
->name
)
1080 /* Make sure that if it has the dimension attribute, that it is
1081 either assumed size or explicit shape. */
1082 if (sym
->as
!= NULL
)
1084 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
1086 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1087 "argument to the procedure '%s' at %L because "
1088 "the procedure is BIND(C)", sym
->name
,
1089 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1090 &(sym
->ns
->proc_name
->declared_at
));
1094 if (sym
->as
->type
== AS_DEFERRED
)
1096 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1097 "argument to the procedure '%s' at %L because "
1098 "the procedure is BIND(C)", sym
->name
,
1099 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1100 &(sym
->ns
->proc_name
->declared_at
));
1112 /* Function called by variable_decl() that adds a name to the symbol table. */
1115 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1116 gfc_array_spec
**as
, locus
*var_locus
)
1118 symbol_attribute attr
;
1121 if (gfc_get_symbol (name
, NULL
, &sym
))
1124 /* Start updating the symbol table. Add basic type attribute if present. */
1125 if (current_ts
.type
!= BT_UNKNOWN
1126 && (sym
->attr
.implicit_type
== 0
1127 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1128 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1131 if (sym
->ts
.type
== BT_CHARACTER
)
1134 sym
->ts
.deferred
= cl_deferred
;
1137 /* Add dimension attribute if present. */
1138 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1142 /* Add attribute to symbol. The copy is so that we can reset the
1143 dimension attribute. */
1144 attr
= current_attr
;
1146 attr
.codimension
= 0;
1148 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1151 /* Finish any work that may need to be done for the binding label,
1152 if it's a bind(c). The bind(c) attr is found before the symbol
1153 is made, and before the symbol name (for data decls), so the
1154 current_ts is holding the binding label, or nothing if the
1155 name= attr wasn't given. Therefore, test here if we're dealing
1156 with a bind(c) and make sure the binding label is set correctly. */
1157 if (sym
->attr
.is_bind_c
== 1)
1159 if (sym
->binding_label
[0] == '\0')
1161 /* Set the binding label and verify that if a NAME= was specified
1162 then only one identifier was in the entity-decl-list. */
1163 if (set_binding_label (sym
->binding_label
, sym
->name
,
1164 num_idents_on_line
) == FAILURE
)
1169 /* See if we know we're in a common block, and if it's a bind(c)
1170 common then we need to make sure we're an interoperable type. */
1171 if (sym
->attr
.in_common
== 1)
1173 /* Test the common block object. */
1174 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1175 && sym
->ts
.is_c_interop
!= 1)
1177 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1178 "must be declared with a C interoperable "
1179 "kind since common block '%s' is BIND(C)",
1180 sym
->name
, sym
->common_block
->name
,
1181 sym
->common_block
->name
);
1186 sym
->attr
.implied_index
= 0;
1188 if (sym
->ts
.type
== BT_CLASS
)
1189 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1195 /* Set character constant to the given length. The constant will be padded or
1196 truncated. If we're inside an array constructor without a typespec, we
1197 additionally check that all elements have the same length; check_len -1
1198 means no checking. */
1201 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1206 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1207 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1209 slen
= expr
->value
.character
.length
;
1212 s
= gfc_get_wide_string (len
+ 1);
1213 memcpy (s
, expr
->value
.character
.string
,
1214 MIN (len
, slen
) * sizeof (gfc_char_t
));
1216 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1218 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1219 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1220 "(%d/%d)", &expr
->where
, slen
, len
);
1222 /* Apply the standard by 'hand' otherwise it gets cleared for
1224 if (check_len
!= -1 && slen
!= check_len
1225 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1226 gfc_error_now ("The CHARACTER elements of the array constructor "
1227 "at %L must have the same length (%d/%d)",
1228 &expr
->where
, slen
, check_len
);
1231 free (expr
->value
.character
.string
);
1232 expr
->value
.character
.string
= s
;
1233 expr
->value
.character
.length
= len
;
1238 /* Function to create and update the enumerator history
1239 using the information passed as arguments.
1240 Pointer "max_enum" is also updated, to point to
1241 enum history node containing largest initializer.
1243 SYM points to the symbol node of enumerator.
1244 INIT points to its enumerator value. */
1247 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1249 enumerator_history
*new_enum_history
;
1250 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1252 new_enum_history
= XCNEW (enumerator_history
);
1254 new_enum_history
->sym
= sym
;
1255 new_enum_history
->initializer
= init
;
1256 new_enum_history
->next
= NULL
;
1258 if (enum_history
== NULL
)
1260 enum_history
= new_enum_history
;
1261 max_enum
= enum_history
;
1265 new_enum_history
->next
= enum_history
;
1266 enum_history
= new_enum_history
;
1268 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1269 new_enum_history
->initializer
->value
.integer
) < 0)
1270 max_enum
= new_enum_history
;
1275 /* Function to free enum kind history. */
1278 gfc_free_enum_history (void)
1280 enumerator_history
*current
= enum_history
;
1281 enumerator_history
*next
;
1283 while (current
!= NULL
)
1285 next
= current
->next
;
1290 enum_history
= NULL
;
1294 /* Function called by variable_decl() that adds an initialization
1295 expression to a symbol. */
1298 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1300 symbol_attribute attr
;
1305 if (find_special (name
, &sym
, false))
1310 /* If this symbol is confirming an implicit parameter type,
1311 then an initialization expression is not allowed. */
1312 if (attr
.flavor
== FL_PARAMETER
1313 && sym
->value
!= NULL
1316 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1323 /* An initializer is required for PARAMETER declarations. */
1324 if (attr
.flavor
== FL_PARAMETER
)
1326 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1332 /* If a variable appears in a DATA block, it cannot have an
1336 gfc_error ("Variable '%s' at %C with an initializer already "
1337 "appears in a DATA statement", sym
->name
);
1341 /* Check if the assignment can happen. This has to be put off
1342 until later for derived type variables and procedure pointers. */
1343 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1344 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1345 && !sym
->attr
.proc_pointer
1346 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1349 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1350 && init
->ts
.type
== BT_CHARACTER
)
1352 /* Update symbol character length according initializer. */
1353 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1356 if (sym
->ts
.u
.cl
->length
== NULL
)
1359 /* If there are multiple CHARACTER variables declared on the
1360 same line, we don't want them to share the same length. */
1361 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1363 if (sym
->attr
.flavor
== FL_PARAMETER
)
1365 if (init
->expr_type
== EXPR_CONSTANT
)
1367 clen
= init
->value
.character
.length
;
1368 sym
->ts
.u
.cl
->length
1369 = gfc_get_int_expr (gfc_default_integer_kind
,
1372 else if (init
->expr_type
== EXPR_ARRAY
)
1375 c
= gfc_constructor_first (init
->value
.constructor
);
1376 clen
= c
->expr
->value
.character
.length
;
1377 sym
->ts
.u
.cl
->length
1378 = gfc_get_int_expr (gfc_default_integer_kind
,
1381 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1382 sym
->ts
.u
.cl
->length
=
1383 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1386 /* Update initializer character length according symbol. */
1387 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1389 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1391 if (init
->expr_type
== EXPR_CONSTANT
)
1392 gfc_set_constant_character_len (len
, init
, -1);
1393 else if (init
->expr_type
== EXPR_ARRAY
)
1397 /* Build a new charlen to prevent simplification from
1398 deleting the length before it is resolved. */
1399 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1400 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1402 for (c
= gfc_constructor_first (init
->value
.constructor
);
1403 c
; c
= gfc_constructor_next (c
))
1404 gfc_set_constant_character_len (len
, c
->expr
, -1);
1409 /* If sym is implied-shape, set its upper bounds from init. */
1410 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1411 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1415 if (init
->rank
== 0)
1417 gfc_error ("Can't initialize implied-shape array at %L"
1418 " with scalar", &sym
->declared_at
);
1421 gcc_assert (sym
->as
->rank
== init
->rank
);
1423 /* Shape should be present, we get an initialization expression. */
1424 gcc_assert (init
->shape
);
1426 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1432 lower
= sym
->as
->lower
[dim
];
1433 if (lower
->expr_type
!= EXPR_CONSTANT
)
1435 gfc_error ("Non-constant lower bound in implied-shape"
1436 " declaration at %L", &lower
->where
);
1440 /* All dimensions must be without upper bound. */
1441 gcc_assert (!sym
->as
->upper
[dim
]);
1444 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1445 mpz_add (e
->value
.integer
,
1446 lower
->value
.integer
, init
->shape
[dim
]);
1447 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1448 sym
->as
->upper
[dim
] = e
;
1451 sym
->as
->type
= AS_EXPLICIT
;
1454 /* Need to check if the expression we initialized this
1455 to was one of the iso_c_binding named constants. If so,
1456 and we're a parameter (constant), let it be iso_c.
1458 integer(c_int), parameter :: my_int = c_int
1459 integer(my_int) :: my_int_2
1460 If we mark my_int as iso_c (since we can see it's value
1461 is equal to one of the named constants), then my_int_2
1462 will be considered C interoperable. */
1463 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1465 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1466 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1467 /* attr bits needed for module files. */
1468 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1469 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1470 if (init
->ts
.is_iso_c
)
1471 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1474 /* Add initializer. Make sure we keep the ranks sane. */
1475 if (sym
->attr
.dimension
&& init
->rank
== 0)
1480 if (sym
->attr
.flavor
== FL_PARAMETER
1481 && init
->expr_type
== EXPR_CONSTANT
1482 && spec_size (sym
->as
, &size
) == SUCCESS
1483 && mpz_cmp_si (size
, 0) > 0)
1485 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1487 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1488 gfc_constructor_append_expr (&array
->value
.constructor
,
1491 : gfc_copy_expr (init
),
1494 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1495 for (n
= 0; n
< sym
->as
->rank
; n
++)
1496 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1501 init
->rank
= sym
->as
->rank
;
1505 if (sym
->attr
.save
== SAVE_NONE
)
1506 sym
->attr
.save
= SAVE_IMPLICIT
;
1514 /* Function called by variable_decl() that adds a name to a structure
1518 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1519 gfc_array_spec
**as
)
1522 gfc_try t
= SUCCESS
;
1524 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1525 constructing, it must have the pointer attribute. */
1526 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1527 && current_ts
.u
.derived
== gfc_current_block ()
1528 && current_attr
.pointer
== 0)
1530 gfc_error ("Component at %C must have the POINTER attribute");
1534 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1536 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1538 gfc_error ("Array component of structure at %C must have explicit "
1539 "or deferred shape");
1544 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1548 if (c
->ts
.type
== BT_CHARACTER
)
1550 c
->attr
= current_attr
;
1552 c
->initializer
= *init
;
1559 c
->attr
.codimension
= 1;
1561 c
->attr
.dimension
= 1;
1565 /* Should this ever get more complicated, combine with similar section
1566 in add_init_expr_to_sym into a separate function. */
1567 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
&& c
->ts
.u
.cl
1568 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1572 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1573 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1574 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1576 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1578 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1579 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1580 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1581 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1583 gfc_constructor
*ctor
;
1584 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1589 bool has_ts
= (c
->initializer
->ts
.u
.cl
1590 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1592 /* Remember the length of the first element for checking
1593 that all elements *in the constructor* have the same
1594 length. This need not be the length of the LHS! */
1595 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1596 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1597 first_len
= ctor
->expr
->value
.character
.length
;
1599 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1600 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1602 gfc_set_constant_character_len (len
, ctor
->expr
,
1603 has_ts
? -1 : first_len
);
1604 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1610 /* Check array components. */
1611 if (!c
->attr
.dimension
)
1614 if (c
->attr
.pointer
)
1616 if (c
->as
->type
!= AS_DEFERRED
)
1618 gfc_error ("Pointer array component of structure at %C must have a "
1623 else if (c
->attr
.allocatable
)
1625 if (c
->as
->type
!= AS_DEFERRED
)
1627 gfc_error ("Allocatable component of structure at %C must have a "
1634 if (c
->as
->type
!= AS_EXPLICIT
)
1636 gfc_error ("Array component of structure at %C must have an "
1643 if (c
->ts
.type
== BT_CLASS
)
1645 bool delayed
= (gfc_state_stack
->sym
== c
->ts
.u
.derived
)
1646 || (!c
->ts
.u
.derived
->components
1647 && !c
->ts
.u
.derived
->attr
.zero_comp
);
1648 return gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, delayed
);
1655 /* Match a 'NULL()', and possibly take care of some side effects. */
1658 gfc_match_null (gfc_expr
**result
)
1663 m
= gfc_match (" null ( )");
1667 /* The NULL symbol now has to be/become an intrinsic function. */
1668 if (gfc_get_symbol ("null", NULL
, &sym
))
1670 gfc_error ("NULL() initialization at %C is ambiguous");
1674 gfc_intrinsic_symbol (sym
);
1676 if (sym
->attr
.proc
!= PROC_INTRINSIC
1677 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1678 sym
->name
, NULL
) == FAILURE
1679 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1682 *result
= gfc_get_null_expr (&gfc_current_locus
);
1688 /* Match the initialization expr for a data pointer or procedure pointer. */
1691 match_pointer_init (gfc_expr
**init
, int procptr
)
1695 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1697 gfc_error ("Initialization of pointer at %C is not allowed in "
1698 "a PURE procedure");
1702 /* Match NULL() initilization. */
1703 m
= gfc_match_null (init
);
1707 /* Match non-NULL initialization. */
1708 gfc_matching_ptr_assignment
= !procptr
;
1709 gfc_matching_procptr_assignment
= procptr
;
1710 m
= gfc_match_rvalue (init
);
1711 gfc_matching_ptr_assignment
= 0;
1712 gfc_matching_procptr_assignment
= 0;
1713 if (m
== MATCH_ERROR
)
1715 else if (m
== MATCH_NO
)
1717 gfc_error ("Error in pointer initialization at %C");
1722 gfc_resolve_expr (*init
);
1724 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: non-NULL pointer "
1725 "initialization at %C") == FAILURE
)
1732 /* Match a variable name with an optional initializer. When this
1733 subroutine is called, a variable is expected to be parsed next.
1734 Depending on what is happening at the moment, updates either the
1735 symbol table or the current interface. */
1738 variable_decl (int elem
)
1740 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1741 gfc_expr
*initializer
, *char_len
;
1743 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1755 /* When we get here, we've just matched a list of attributes and
1756 maybe a type and a double colon. The next thing we expect to see
1757 is the name of the symbol. */
1758 m
= gfc_match_name (name
);
1762 var_locus
= gfc_current_locus
;
1764 /* Now we could see the optional array spec. or character length. */
1765 m
= gfc_match_array_spec (&as
, true, true);
1766 if (m
== MATCH_ERROR
)
1770 as
= gfc_copy_array_spec (current_as
);
1771 else if (current_as
)
1772 merge_array_spec (current_as
, as
, true);
1774 if (gfc_option
.flag_cray_pointer
)
1775 cp_as
= gfc_copy_array_spec (as
);
1777 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1778 determine (and check) whether it can be implied-shape. If it
1779 was parsed as assumed-size, change it because PARAMETERs can not
1783 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1786 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1791 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1792 && current_attr
.flavor
== FL_PARAMETER
)
1793 as
->type
= AS_IMPLIED_SHAPE
;
1795 if (as
->type
== AS_IMPLIED_SHAPE
1796 && gfc_notify_std (GFC_STD_F2008
,
1797 "Fortran 2008: Implied-shape array at %L",
1798 &var_locus
) == FAILURE
)
1807 cl_deferred
= false;
1809 if (current_ts
.type
== BT_CHARACTER
)
1811 switch (match_char_length (&char_len
, &cl_deferred
))
1814 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1816 cl
->length
= char_len
;
1819 /* Non-constant lengths need to be copied after the first
1820 element. Also copy assumed lengths. */
1823 && (current_ts
.u
.cl
->length
== NULL
1824 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1826 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1827 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1830 cl
= current_ts
.u
.cl
;
1832 cl_deferred
= current_ts
.deferred
;
1841 /* If this symbol has already shown up in a Cray Pointer declaration,
1842 then we want to set the type & bail out. */
1843 if (gfc_option
.flag_cray_pointer
)
1845 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1846 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1848 sym
->ts
.type
= current_ts
.type
;
1849 sym
->ts
.kind
= current_ts
.kind
;
1851 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1852 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1853 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1856 /* Check to see if we have an array specification. */
1859 if (sym
->as
!= NULL
)
1861 gfc_error ("Duplicate array spec for Cray pointee at %C");
1862 gfc_free_array_spec (cp_as
);
1868 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1869 gfc_internal_error ("Couldn't set pointee array spec.");
1871 /* Fix the array spec. */
1872 m
= gfc_mod_pointee_as (sym
->as
);
1873 if (m
== MATCH_ERROR
)
1881 gfc_free_array_spec (cp_as
);
1885 /* Procedure pointer as function result. */
1886 if (gfc_current_state () == COMP_FUNCTION
1887 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1888 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1889 strcpy (name
, "ppr@");
1891 if (gfc_current_state () == COMP_FUNCTION
1892 && strcmp (name
, gfc_current_block ()->name
) == 0
1893 && gfc_current_block ()->result
1894 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1895 strcpy (name
, "ppr@");
1897 /* OK, we've successfully matched the declaration. Now put the
1898 symbol in the current namespace, because it might be used in the
1899 optional initialization expression for this symbol, e.g. this is
1902 integer, parameter :: i = huge(i)
1904 This is only true for parameters or variables of a basic type.
1905 For components of derived types, it is not true, so we don't
1906 create a symbol for those yet. If we fail to create the symbol,
1908 if (gfc_current_state () != COMP_DERIVED
1909 && build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
) == FAILURE
)
1915 /* An interface body specifies all of the procedure's
1916 characteristics and these shall be consistent with those
1917 specified in the procedure definition, except that the interface
1918 may specify a procedure that is not pure if the procedure is
1919 defined to be pure(12.3.2). */
1920 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1921 && gfc_current_ns
->proc_name
1922 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1923 && current_ts
.u
.derived
->ns
!= gfc_current_ns
)
1926 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.u
.derived
->name
);
1927 if (!(current_ts
.u
.derived
->attr
.imported
1929 && st
->n
.sym
== current_ts
.u
.derived
)
1930 && !gfc_current_ns
->has_import_set
)
1932 gfc_error ("the type of '%s' at %C has not been declared within the "
1939 /* In functions that have a RESULT variable defined, the function
1940 name always refers to function calls. Therefore, the name is
1941 not allowed to appear in specification statements. */
1942 if (gfc_current_state () == COMP_FUNCTION
1943 && gfc_current_block () != NULL
1944 && gfc_current_block ()->result
!= NULL
1945 && gfc_current_block ()->result
!= gfc_current_block ()
1946 && strcmp (gfc_current_block ()->name
, name
) == 0)
1948 gfc_error ("Function name '%s' not allowed at %C", name
);
1953 /* We allow old-style initializations of the form
1954 integer i /2/, j(4) /3*3, 1/
1955 (if no colon has been seen). These are different from data
1956 statements in that initializers are only allowed to apply to the
1957 variable immediately preceding, i.e.
1959 is not allowed. Therefore we have to do some work manually, that
1960 could otherwise be left to the matchers for DATA statements. */
1962 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1964 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1965 "initialization at %C") == FAILURE
)
1968 return match_old_style_init (name
);
1971 /* The double colon must be present in order to have initializers.
1972 Otherwise the statement is ambiguous with an assignment statement. */
1975 if (gfc_match (" =>") == MATCH_YES
)
1977 if (!current_attr
.pointer
)
1979 gfc_error ("Initialization at %C isn't for a pointer variable");
1984 m
= match_pointer_init (&initializer
, 0);
1988 else if (gfc_match_char ('=') == MATCH_YES
)
1990 if (current_attr
.pointer
)
1992 gfc_error ("Pointer initialization at %C requires '=>', "
1998 m
= gfc_match_init_expr (&initializer
);
2001 gfc_error ("Expected an initialization expression at %C");
2005 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2006 && gfc_state_stack
->state
!= COMP_DERIVED
)
2008 gfc_error ("Initialization of variable at %C is not allowed in "
2009 "a PURE procedure");
2018 if (initializer
!= NULL
&& current_attr
.allocatable
2019 && gfc_current_state () == COMP_DERIVED
)
2021 gfc_error ("Initialization of allocatable component at %C is not "
2027 /* Add the initializer. Note that it is fine if initializer is
2028 NULL here, because we sometimes also need to check if a
2029 declaration *must* have an initialization expression. */
2030 if (gfc_current_state () != COMP_DERIVED
)
2031 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2034 if (current_ts
.type
== BT_DERIVED
2035 && !current_attr
.pointer
&& !initializer
)
2036 initializer
= gfc_default_initializer (¤t_ts
);
2037 t
= build_struct (name
, cl
, &initializer
, &as
);
2040 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
2043 /* Free stuff up and return. */
2044 gfc_free_expr (initializer
);
2045 gfc_free_array_spec (as
);
2051 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2052 This assumes that the byte size is equal to the kind number for
2053 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2056 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2061 if (gfc_match_char ('*') != MATCH_YES
)
2064 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2068 original_kind
= ts
->kind
;
2070 /* Massage the kind numbers for complex types. */
2071 if (ts
->type
== BT_COMPLEX
)
2075 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2076 gfc_basic_typename (ts
->type
), original_kind
);
2082 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2084 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2085 gfc_basic_typename (ts
->type
), original_kind
);
2089 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
2090 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
2097 /* Match a kind specification. Since kinds are generally optional, we
2098 usually return MATCH_NO if something goes wrong. If a "kind="
2099 string is found, then we know we have an error. */
2102 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2114 where
= loc
= gfc_current_locus
;
2119 if (gfc_match_char ('(') == MATCH_NO
)
2122 /* Also gobbles optional text. */
2123 if (gfc_match (" kind = ") == MATCH_YES
)
2126 loc
= gfc_current_locus
;
2129 n
= gfc_match_init_expr (&e
);
2133 if (gfc_matching_function
)
2135 /* The function kind expression might include use associated or
2136 imported parameters and try again after the specification
2138 if (gfc_match_char (')') != MATCH_YES
)
2140 gfc_error ("Missing right parenthesis at %C");
2146 gfc_undo_symbols ();
2151 /* ....or else, the match is real. */
2153 gfc_error ("Expected initialization expression at %C");
2161 gfc_error ("Expected scalar initialization expression at %C");
2166 msg
= gfc_extract_int (e
, &ts
->kind
);
2175 /* Before throwing away the expression, let's see if we had a
2176 C interoperable kind (and store the fact). */
2177 if (e
->ts
.is_c_interop
== 1)
2179 /* Mark this as c interoperable if being declared with one
2180 of the named constants from iso_c_binding. */
2181 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2182 ts
->f90_type
= e
->ts
.f90_type
;
2188 /* Ignore errors to this point, if we've gotten here. This means
2189 we ignore the m=MATCH_ERROR from above. */
2190 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2192 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2193 gfc_basic_typename (ts
->type
));
2194 gfc_current_locus
= where
;
2198 /* Warn if, e.g., c_int is used for a REAL variable, but not
2199 if, e.g., c_double is used for COMPLEX as the standard
2200 explicitly says that the kind type parameter for complex and real
2201 variable is the same, i.e. c_float == c_float_complex. */
2202 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2203 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2204 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2205 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2206 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2207 gfc_basic_typename (ts
->type
));
2209 gfc_gobble_whitespace ();
2210 if ((c
= gfc_next_ascii_char ()) != ')'
2211 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2213 if (ts
->type
== BT_CHARACTER
)
2214 gfc_error ("Missing right parenthesis or comma at %C");
2216 gfc_error ("Missing right parenthesis at %C");
2220 /* All tests passed. */
2223 if(m
== MATCH_ERROR
)
2224 gfc_current_locus
= where
;
2226 /* Return what we know from the test(s). */
2231 gfc_current_locus
= where
;
2237 match_char_kind (int * kind
, int * is_iso_c
)
2246 where
= gfc_current_locus
;
2248 n
= gfc_match_init_expr (&e
);
2250 if (n
!= MATCH_YES
&& gfc_matching_function
)
2252 /* The expression might include use-associated or imported
2253 parameters and try again after the specification
2256 gfc_undo_symbols ();
2261 gfc_error ("Expected initialization expression at %C");
2267 gfc_error ("Expected scalar initialization expression at %C");
2272 msg
= gfc_extract_int (e
, kind
);
2273 *is_iso_c
= e
->ts
.is_iso_c
;
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 (BT_CHARACTER
, *kind
, true) < 0)
2287 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2291 /* All tests passed. */
2294 if (m
== MATCH_ERROR
)
2295 gfc_current_locus
= where
;
2297 /* Return what we know from the test(s). */
2302 gfc_current_locus
= where
;
2307 /* Match the various kind/length specifications in a CHARACTER
2308 declaration. We don't return MATCH_NO. */
2311 gfc_match_char_spec (gfc_typespec
*ts
)
2313 int kind
, seen_length
, is_iso_c
;
2325 /* Try the old-style specification first. */
2326 old_char_selector
= 0;
2328 m
= match_char_length (&len
, &deferred
);
2332 old_char_selector
= 1;
2337 m
= gfc_match_char ('(');
2340 m
= MATCH_YES
; /* Character without length is a single char. */
2344 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2345 if (gfc_match (" kind =") == MATCH_YES
)
2347 m
= match_char_kind (&kind
, &is_iso_c
);
2349 if (m
== MATCH_ERROR
)
2354 if (gfc_match (" , len =") == MATCH_NO
)
2357 m
= char_len_param_value (&len
, &deferred
);
2360 if (m
== MATCH_ERROR
)
2367 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2368 if (gfc_match (" len =") == MATCH_YES
)
2370 m
= char_len_param_value (&len
, &deferred
);
2373 if (m
== MATCH_ERROR
)
2377 if (gfc_match_char (')') == MATCH_YES
)
2380 if (gfc_match (" , kind =") != MATCH_YES
)
2383 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2389 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2390 m
= char_len_param_value (&len
, &deferred
);
2393 if (m
== MATCH_ERROR
)
2397 m
= gfc_match_char (')');
2401 if (gfc_match_char (',') != MATCH_YES
)
2404 gfc_match (" kind ="); /* Gobble optional text. */
2406 m
= match_char_kind (&kind
, &is_iso_c
);
2407 if (m
== MATCH_ERROR
)
2413 /* Require a right-paren at this point. */
2414 m
= gfc_match_char (')');
2419 gfc_error ("Syntax error in CHARACTER declaration at %C");
2421 gfc_free_expr (len
);
2425 /* Deal with character functions after USE and IMPORT statements. */
2426 if (gfc_matching_function
)
2428 gfc_free_expr (len
);
2429 gfc_undo_symbols ();
2435 gfc_free_expr (len
);
2439 /* Do some final massaging of the length values. */
2440 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2442 if (seen_length
== 0)
2443 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2448 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2449 ts
->deferred
= deferred
;
2451 /* We have to know if it was a c interoperable kind so we can
2452 do accurate type checking of bind(c) procs, etc. */
2454 /* Mark this as c interoperable if being declared with one
2455 of the named constants from iso_c_binding. */
2456 ts
->is_c_interop
= is_iso_c
;
2457 else if (len
!= NULL
)
2458 /* Here, we might have parsed something such as: character(c_char)
2459 In this case, the parsing code above grabs the c_char when
2460 looking for the length (line 1690, roughly). it's the last
2461 testcase for parsing the kind params of a character variable.
2462 However, it's not actually the length. this seems like it
2464 To see if the user used a C interop kind, test the expr
2465 of the so called length, and see if it's C interoperable. */
2466 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2472 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2473 structure to the matched specification. This is necessary for FUNCTION and
2474 IMPLICIT statements.
2476 If implicit_flag is nonzero, then we don't check for the optional
2477 kind specification. Not doing so is needed for matching an IMPLICIT
2478 statement correctly. */
2481 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2483 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2487 bool seen_deferred_kind
, matched_type
;
2489 /* A belt and braces check that the typespec is correctly being treated
2490 as a deferred characteristic association. */
2491 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2492 && (gfc_current_block ()->result
->ts
.kind
== -1)
2493 && (ts
->kind
== -1);
2495 if (seen_deferred_kind
)
2498 /* Clear the current binding label, in case one is given. */
2499 curr_binding_label
[0] = '\0';
2501 if (gfc_match (" byte") == MATCH_YES
)
2503 if (gfc_notify_std (GFC_STD_GNU
, "Extension: BYTE type at %C")
2507 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2509 gfc_error ("BYTE type used at %C "
2510 "is not available on the target machine");
2514 ts
->type
= BT_INTEGER
;
2520 m
= gfc_match (" type ( %n", name
);
2521 matched_type
= (m
== MATCH_YES
);
2523 if ((matched_type
&& strcmp ("integer", name
) == 0)
2524 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2526 ts
->type
= BT_INTEGER
;
2527 ts
->kind
= gfc_default_integer_kind
;
2531 if ((matched_type
&& strcmp ("character", name
) == 0)
2532 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2535 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2536 "intrinsic-type-spec at %C") == FAILURE
)
2539 ts
->type
= BT_CHARACTER
;
2540 if (implicit_flag
== 0)
2541 m
= gfc_match_char_spec (ts
);
2545 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2551 if ((matched_type
&& strcmp ("real", name
) == 0)
2552 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2555 ts
->kind
= gfc_default_real_kind
;
2560 && (strcmp ("doubleprecision", name
) == 0
2561 || (strcmp ("double", name
) == 0
2562 && gfc_match (" precision") == MATCH_YES
)))
2563 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2566 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2567 "intrinsic-type-spec at %C") == FAILURE
)
2569 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2573 ts
->kind
= gfc_default_double_kind
;
2577 if ((matched_type
&& strcmp ("complex", name
) == 0)
2578 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2580 ts
->type
= BT_COMPLEX
;
2581 ts
->kind
= gfc_default_complex_kind
;
2586 && (strcmp ("doublecomplex", name
) == 0
2587 || (strcmp ("double", name
) == 0
2588 && gfc_match (" complex") == MATCH_YES
)))
2589 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2591 if (gfc_notify_std (GFC_STD_GNU
, "Extension: DOUBLE COMPLEX at %C")
2596 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2597 "intrinsic-type-spec at %C") == FAILURE
)
2600 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2603 ts
->type
= BT_COMPLEX
;
2604 ts
->kind
= gfc_default_double_kind
;
2608 if ((matched_type
&& strcmp ("logical", name
) == 0)
2609 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2611 ts
->type
= BT_LOGICAL
;
2612 ts
->kind
= gfc_default_logical_kind
;
2617 m
= gfc_match_char (')');
2620 ts
->type
= BT_DERIVED
;
2623 /* Match CLASS declarations. */
2624 m
= gfc_match (" class ( * )");
2625 if (m
== MATCH_ERROR
)
2627 else if (m
== MATCH_YES
)
2629 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2633 m
= gfc_match (" class ( %n )", name
);
2636 ts
->type
= BT_CLASS
;
2638 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: CLASS statement at %C")
2643 /* Defer association of the derived type until the end of the
2644 specification block. However, if the derived type can be
2645 found, add it to the typespec. */
2646 if (gfc_matching_function
)
2648 ts
->u
.derived
= NULL
;
2649 if (gfc_current_state () != COMP_INTERFACE
2650 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2651 ts
->u
.derived
= sym
;
2655 /* Search for the name but allow the components to be defined later. If
2656 type = -1, this typespec has been seen in a function declaration but
2657 the type could not be accessed at that point. */
2659 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2661 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2664 else if (ts
->kind
== -1)
2666 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2667 || gfc_current_ns
->has_import_set
;
2668 if (gfc_find_symbol (name
, NULL
, iface
, &sym
))
2670 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2679 if (sym
->attr
.flavor
!= FL_DERIVED
2680 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2683 gfc_set_sym_referenced (sym
);
2684 ts
->u
.derived
= sym
;
2690 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2691 "intrinsic-type-spec at %C") == FAILURE
)
2694 /* For all types except double, derived and character, look for an
2695 optional kind specifier. MATCH_NO is actually OK at this point. */
2696 if (implicit_flag
== 1)
2698 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2704 if (gfc_current_form
== FORM_FREE
)
2706 c
= gfc_peek_ascii_char ();
2707 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2708 && c
!= ':' && c
!= ',')
2710 if (matched_type
&& c
== ')')
2712 gfc_next_ascii_char ();
2719 m
= gfc_match_kind_spec (ts
, false);
2720 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2721 m
= gfc_match_old_kind_spec (ts
);
2723 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2726 /* Defer association of the KIND expression of function results
2727 until after USE and IMPORT statements. */
2728 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2729 || gfc_matching_function
)
2733 m
= MATCH_YES
; /* No kind specifier found. */
2739 /* Match an IMPLICIT NONE statement. Actually, this statement is
2740 already matched in parse.c, or we would not end up here in the
2741 first place. So the only thing we need to check, is if there is
2742 trailing garbage. If not, the match is successful. */
2745 gfc_match_implicit_none (void)
2747 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2751 /* Match the letter range(s) of an IMPLICIT statement. */
2754 match_implicit_range (void)
2760 cur_loc
= gfc_current_locus
;
2762 gfc_gobble_whitespace ();
2763 c
= gfc_next_ascii_char ();
2766 gfc_error ("Missing character range in IMPLICIT at %C");
2773 gfc_gobble_whitespace ();
2774 c1
= gfc_next_ascii_char ();
2778 gfc_gobble_whitespace ();
2779 c
= gfc_next_ascii_char ();
2784 inner
= 0; /* Fall through. */
2791 gfc_gobble_whitespace ();
2792 c2
= gfc_next_ascii_char ();
2796 gfc_gobble_whitespace ();
2797 c
= gfc_next_ascii_char ();
2799 if ((c
!= ',') && (c
!= ')'))
2812 gfc_error ("Letters must be in alphabetic order in "
2813 "IMPLICIT statement at %C");
2817 /* See if we can add the newly matched range to the pending
2818 implicits from this IMPLICIT statement. We do not check for
2819 conflicts with whatever earlier IMPLICIT statements may have
2820 set. This is done when we've successfully finished matching
2822 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2829 gfc_syntax_error (ST_IMPLICIT
);
2831 gfc_current_locus
= cur_loc
;
2836 /* Match an IMPLICIT statement, storing the types for
2837 gfc_set_implicit() if the statement is accepted by the parser.
2838 There is a strange looking, but legal syntactic construction
2839 possible. It looks like:
2841 IMPLICIT INTEGER (a-b) (c-d)
2843 This is legal if "a-b" is a constant expression that happens to
2844 equal one of the legal kinds for integers. The real problem
2845 happens with an implicit specification that looks like:
2847 IMPLICIT INTEGER (a-b)
2849 In this case, a typespec matcher that is "greedy" (as most of the
2850 matchers are) gobbles the character range as a kindspec, leaving
2851 nothing left. We therefore have to go a bit more slowly in the
2852 matching process by inhibiting the kindspec checking during
2853 typespec matching and checking for a kind later. */
2856 gfc_match_implicit (void)
2865 /* We don't allow empty implicit statements. */
2866 if (gfc_match_eos () == MATCH_YES
)
2868 gfc_error ("Empty IMPLICIT statement at %C");
2874 /* First cleanup. */
2875 gfc_clear_new_implicit ();
2877 /* A basic type is mandatory here. */
2878 m
= gfc_match_decl_type_spec (&ts
, 1);
2879 if (m
== MATCH_ERROR
)
2884 cur_loc
= gfc_current_locus
;
2885 m
= match_implicit_range ();
2889 /* We may have <TYPE> (<RANGE>). */
2890 gfc_gobble_whitespace ();
2891 c
= gfc_next_ascii_char ();
2892 if ((c
== '\n') || (c
== ','))
2894 /* Check for CHARACTER with no length parameter. */
2895 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
2897 ts
.kind
= gfc_default_character_kind
;
2898 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2899 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
2903 /* Record the Successful match. */
2904 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2909 gfc_current_locus
= cur_loc
;
2912 /* Discard the (incorrectly) matched range. */
2913 gfc_clear_new_implicit ();
2915 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2916 if (ts
.type
== BT_CHARACTER
)
2917 m
= gfc_match_char_spec (&ts
);
2920 m
= gfc_match_kind_spec (&ts
, false);
2923 m
= gfc_match_old_kind_spec (&ts
);
2924 if (m
== MATCH_ERROR
)
2930 if (m
== MATCH_ERROR
)
2933 m
= match_implicit_range ();
2934 if (m
== MATCH_ERROR
)
2939 gfc_gobble_whitespace ();
2940 c
= gfc_next_ascii_char ();
2941 if ((c
!= '\n') && (c
!= ','))
2944 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2952 gfc_syntax_error (ST_IMPLICIT
);
2960 gfc_match_import (void)
2962 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2967 if (gfc_current_ns
->proc_name
== NULL
2968 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2970 gfc_error ("IMPORT statement at %C only permitted in "
2971 "an INTERFACE body");
2975 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2979 if (gfc_match_eos () == MATCH_YES
)
2981 /* All host variables should be imported. */
2982 gfc_current_ns
->has_import_set
= 1;
2986 if (gfc_match (" ::") == MATCH_YES
)
2988 if (gfc_match_eos () == MATCH_YES
)
2990 gfc_error ("Expecting list of named entities at %C");
2998 m
= gfc_match (" %n", name
);
3002 if (gfc_current_ns
->parent
!= NULL
3003 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3005 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3008 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3009 && gfc_find_symbol (name
,
3010 gfc_current_ns
->proc_name
->ns
->parent
,
3013 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3019 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3020 "at %C - does not exist.", name
);
3024 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
3026 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3031 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, sym
->name
);
3034 sym
->attr
.imported
= 1;
3046 if (gfc_match_eos () == MATCH_YES
)
3048 if (gfc_match_char (',') != MATCH_YES
)
3055 gfc_error ("Syntax error in IMPORT statement at %C");
3060 /* A minimal implementation of gfc_match without whitespace, escape
3061 characters or variable arguments. Returns true if the next
3062 characters match the TARGET template exactly. */
3065 match_string_p (const char *target
)
3069 for (p
= target
; *p
; p
++)
3070 if ((char) gfc_next_ascii_char () != *p
)
3075 /* Matches an attribute specification including array specs. If
3076 successful, leaves the variables current_attr and current_as
3077 holding the specification. Also sets the colon_seen variable for
3078 later use by matchers associated with initializations.
3080 This subroutine is a little tricky in the sense that we don't know
3081 if we really have an attr-spec until we hit the double colon.
3082 Until that time, we can only return MATCH_NO. This forces us to
3083 check for duplicate specification at this level. */
3086 match_attr_spec (void)
3088 /* Modifiers that can exist in a type statement. */
3090 { GFC_DECL_BEGIN
= 0,
3091 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3092 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3093 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3094 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3095 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3096 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3100 /* GFC_DECL_END is the sentinel, index starts at 0. */
3101 #define NUM_DECL GFC_DECL_END
3103 locus start
, seen_at
[NUM_DECL
];
3110 gfc_clear_attr (¤t_attr
);
3111 start
= gfc_current_locus
;
3116 /* See if we get all of the keywords up to the final double colon. */
3117 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3125 gfc_gobble_whitespace ();
3127 ch
= gfc_next_ascii_char ();
3130 /* This is the successful exit condition for the loop. */
3131 if (gfc_next_ascii_char () == ':')
3136 gfc_gobble_whitespace ();
3137 switch (gfc_peek_ascii_char ())
3140 gfc_next_ascii_char ();
3141 switch (gfc_next_ascii_char ())
3144 if (match_string_p ("locatable"))
3146 /* Matched "allocatable". */
3147 d
= DECL_ALLOCATABLE
;
3152 if (match_string_p ("ynchronous"))
3154 /* Matched "asynchronous". */
3155 d
= DECL_ASYNCHRONOUS
;
3162 /* Try and match the bind(c). */
3163 m
= gfc_match_bind_c (NULL
, true);
3166 else if (m
== MATCH_ERROR
)
3171 gfc_next_ascii_char ();
3172 if ('o' != gfc_next_ascii_char ())
3174 switch (gfc_next_ascii_char ())
3177 if (match_string_p ("imension"))
3179 d
= DECL_CODIMENSION
;
3183 if (match_string_p ("tiguous"))
3185 d
= DECL_CONTIGUOUS
;
3192 if (match_string_p ("dimension"))
3197 if (match_string_p ("external"))
3202 if (match_string_p ("int"))
3204 ch
= gfc_next_ascii_char ();
3207 if (match_string_p ("nt"))
3209 /* Matched "intent". */
3210 /* TODO: Call match_intent_spec from here. */
3211 if (gfc_match (" ( in out )") == MATCH_YES
)
3213 else if (gfc_match (" ( in )") == MATCH_YES
)
3215 else if (gfc_match (" ( out )") == MATCH_YES
)
3221 if (match_string_p ("insic"))
3223 /* Matched "intrinsic". */
3231 if (match_string_p ("optional"))
3236 gfc_next_ascii_char ();
3237 switch (gfc_next_ascii_char ())
3240 if (match_string_p ("rameter"))
3242 /* Matched "parameter". */
3248 if (match_string_p ("inter"))
3250 /* Matched "pointer". */
3256 ch
= gfc_next_ascii_char ();
3259 if (match_string_p ("vate"))
3261 /* Matched "private". */
3267 if (match_string_p ("tected"))
3269 /* Matched "protected". */
3276 if (match_string_p ("blic"))
3278 /* Matched "public". */
3286 if (match_string_p ("save"))
3291 if (match_string_p ("target"))
3296 gfc_next_ascii_char ();
3297 ch
= gfc_next_ascii_char ();
3300 if (match_string_p ("lue"))
3302 /* Matched "value". */
3308 if (match_string_p ("latile"))
3310 /* Matched "volatile". */
3318 /* No double colon and no recognizable decl_type, so assume that
3319 we've been looking at something else the whole time. */
3326 /* Check to make sure any parens are paired up correctly. */
3327 if (gfc_match_parens () == MATCH_ERROR
)
3334 seen_at
[d
] = gfc_current_locus
;
3336 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3338 gfc_array_spec
*as
= NULL
;
3340 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3341 d
== DECL_CODIMENSION
);
3343 if (current_as
== NULL
)
3345 else if (m
== MATCH_YES
)
3347 merge_array_spec (as
, current_as
, false);
3353 if (d
== DECL_CODIMENSION
)
3354 gfc_error ("Missing codimension specification at %C");
3356 gfc_error ("Missing dimension specification at %C");
3360 if (m
== MATCH_ERROR
)
3365 /* Since we've seen a double colon, we have to be looking at an
3366 attr-spec. This means that we can now issue errors. */
3367 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3372 case DECL_ALLOCATABLE
:
3373 attr
= "ALLOCATABLE";
3375 case DECL_ASYNCHRONOUS
:
3376 attr
= "ASYNCHRONOUS";
3378 case DECL_CODIMENSION
:
3379 attr
= "CODIMENSION";
3381 case DECL_CONTIGUOUS
:
3382 attr
= "CONTIGUOUS";
3384 case DECL_DIMENSION
:
3391 attr
= "INTENT (IN)";
3394 attr
= "INTENT (OUT)";
3397 attr
= "INTENT (IN OUT)";
3399 case DECL_INTRINSIC
:
3405 case DECL_PARAMETER
:
3411 case DECL_PROTECTED
:
3426 case DECL_IS_BIND_C
:
3436 attr
= NULL
; /* This shouldn't happen. */
3439 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3444 /* Now that we've dealt with duplicate attributes, add the attributes
3445 to the current attribute. */
3446 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3451 if (gfc_current_state () == COMP_DERIVED
3452 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3453 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3454 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3456 if (d
== DECL_ALLOCATABLE
)
3458 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3459 "attribute at %C in a TYPE definition")
3468 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3475 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3476 && gfc_current_state () != COMP_MODULE
)
3478 if (d
== DECL_PRIVATE
)
3482 if (gfc_current_state () == COMP_DERIVED
3483 && gfc_state_stack
->previous
3484 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3486 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3487 "at %L in a TYPE definition", attr
,
3497 gfc_error ("%s attribute at %L is not allowed outside of the "
3498 "specification part of a module", attr
, &seen_at
[d
]);
3506 case DECL_ALLOCATABLE
:
3507 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3510 case DECL_ASYNCHRONOUS
:
3511 if (gfc_notify_std (GFC_STD_F2003
,
3512 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3516 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3519 case DECL_CODIMENSION
:
3520 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3523 case DECL_CONTIGUOUS
:
3524 if (gfc_notify_std (GFC_STD_F2008
,
3525 "Fortran 2008: CONTIGUOUS attribute at %C")
3529 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3532 case DECL_DIMENSION
:
3533 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3537 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3541 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3545 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3549 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3552 case DECL_INTRINSIC
:
3553 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3557 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3560 case DECL_PARAMETER
:
3561 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3565 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3568 case DECL_PROTECTED
:
3569 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3571 gfc_error ("PROTECTED at %C only allowed in specification "
3572 "part of a module");
3577 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3582 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3586 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3591 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3596 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3600 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3603 case DECL_IS_BIND_C
:
3604 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3608 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3613 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3617 if (gfc_notify_std (GFC_STD_F2003
,
3618 "Fortran 2003: VOLATILE attribute at %C")
3622 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3626 gfc_internal_error ("match_attr_spec(): Bad attribute");
3636 /* Module variables implicitly have the SAVE attribute. */
3637 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
)
3638 current_attr
.save
= SAVE_IMPLICIT
;
3644 gfc_current_locus
= start
;
3645 gfc_free_array_spec (current_as
);
3651 /* Set the binding label, dest_label, either with the binding label
3652 stored in the given gfc_typespec, ts, or if none was provided, it
3653 will be the symbol name in all lower case, as required by the draft
3654 (J3/04-007, section 15.4.1). If a binding label was given and
3655 there is more than one argument (num_idents), it is an error. */
3658 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3660 if (num_idents
> 1 && has_name_equals
)
3662 gfc_error ("Multiple identifiers provided with "
3663 "single NAME= specifier at %C");
3667 if (curr_binding_label
[0] != '\0')
3669 /* Binding label given; store in temp holder til have sym. */
3670 strcpy (dest_label
, curr_binding_label
);
3674 /* No binding label given, and the NAME= specifier did not exist,
3675 which means there was no NAME="". */
3676 if (sym_name
!= NULL
&& has_name_equals
== 0)
3677 strcpy (dest_label
, sym_name
);
3684 /* Set the status of the given common block as being BIND(C) or not,
3685 depending on the given parameter, is_bind_c. */
3688 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3690 com_block
->is_bind_c
= is_bind_c
;
3695 /* Verify that the given gfc_typespec is for a C interoperable type. */
3698 verify_c_interop (gfc_typespec
*ts
)
3700 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3701 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3702 ? SUCCESS
: FAILURE
;
3703 else if (ts
->is_c_interop
!= 1)
3710 /* Verify that the variables of a given common block, which has been
3711 defined with the attribute specifier bind(c), to be of a C
3712 interoperable type. Errors will be reported here, if
3716 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3718 gfc_symbol
*curr_sym
= NULL
;
3719 gfc_try retval
= SUCCESS
;
3721 curr_sym
= com_block
->head
;
3723 /* Make sure we have at least one symbol. */
3724 if (curr_sym
== NULL
)
3727 /* Here we know we have a symbol, so we'll execute this loop
3731 /* The second to last param, 1, says this is in a common block. */
3732 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3733 curr_sym
= curr_sym
->common_next
;
3734 } while (curr_sym
!= NULL
);
3740 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3741 an appropriate error message is reported. */
3744 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3745 int is_in_common
, gfc_common_head
*com_block
)
3747 bool bind_c_function
= false;
3748 gfc_try retval
= SUCCESS
;
3750 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3751 bind_c_function
= true;
3753 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3755 tmp_sym
= tmp_sym
->result
;
3756 /* Make sure it wasn't an implicitly typed result. */
3757 if (tmp_sym
->attr
.implicit_type
)
3759 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3760 "%L may not be C interoperable", tmp_sym
->name
,
3761 &tmp_sym
->declared_at
);
3762 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3763 /* Mark it as C interoperable to prevent duplicate warnings. */
3764 tmp_sym
->ts
.is_c_interop
= 1;
3765 tmp_sym
->attr
.is_c_interop
= 1;
3769 /* Here, we know we have the bind(c) attribute, so if we have
3770 enough type info, then verify that it's a C interop kind.
3771 The info could be in the symbol already, or possibly still in
3772 the given ts (current_ts), so look in both. */
3773 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3775 if (verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3777 /* See if we're dealing with a sym in a common block or not. */
3778 if (is_in_common
== 1)
3780 gfc_warning ("Variable '%s' in common block '%s' at %L "
3781 "may not be a C interoperable "
3782 "kind though common block '%s' is BIND(C)",
3783 tmp_sym
->name
, com_block
->name
,
3784 &(tmp_sym
->declared_at
), com_block
->name
);
3788 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3789 gfc_error ("Type declaration '%s' at %L is not C "
3790 "interoperable but it is BIND(C)",
3791 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3793 gfc_warning ("Variable '%s' at %L "
3794 "may not be a C interoperable "
3795 "kind but it is bind(c)",
3796 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3800 /* Variables declared w/in a common block can't be bind(c)
3801 since there's no way for C to see these variables, so there's
3802 semantically no reason for the attribute. */
3803 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3805 gfc_error ("Variable '%s' in common block '%s' at "
3806 "%L cannot be declared with BIND(C) "
3807 "since it is not a global",
3808 tmp_sym
->name
, com_block
->name
,
3809 &(tmp_sym
->declared_at
));
3813 /* Scalar variables that are bind(c) can not have the pointer
3814 or allocatable attributes. */
3815 if (tmp_sym
->attr
.is_bind_c
== 1)
3817 if (tmp_sym
->attr
.pointer
== 1)
3819 gfc_error ("Variable '%s' at %L cannot have both the "
3820 "POINTER and BIND(C) attributes",
3821 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3825 if (tmp_sym
->attr
.allocatable
== 1)
3827 gfc_error ("Variable '%s' at %L cannot have both the "
3828 "ALLOCATABLE and BIND(C) attributes",
3829 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3835 /* If it is a BIND(C) function, make sure the return value is a
3836 scalar value. The previous tests in this function made sure
3837 the type is interoperable. */
3838 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
3839 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3840 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3842 /* BIND(C) functions can not return a character string. */
3843 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
3844 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
3845 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
3846 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
3847 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3848 "be a character string", tmp_sym
->name
,
3849 &(tmp_sym
->declared_at
));
3852 /* See if the symbol has been marked as private. If it has, make sure
3853 there is no binding label and warn the user if there is one. */
3854 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3855 && tmp_sym
->binding_label
[0] != '\0')
3856 /* Use gfc_warning_now because we won't say that the symbol fails
3857 just because of this. */
3858 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3859 "given the binding label '%s'", tmp_sym
->name
,
3860 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3866 /* Set the appropriate fields for a symbol that's been declared as
3867 BIND(C) (the is_bind_c flag and the binding label), and verify that
3868 the type is C interoperable. Errors are reported by the functions
3869 used to set/test these fields. */
3872 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3874 gfc_try retval
= SUCCESS
;
3876 /* TODO: Do we need to make sure the vars aren't marked private? */
3878 /* Set the is_bind_c bit in symbol_attribute. */
3879 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3881 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3882 num_idents
) != SUCCESS
)
3889 /* Set the fields marking the given common block as BIND(C), including
3890 a binding label, and report any errors encountered. */
3893 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3895 gfc_try retval
= SUCCESS
;
3897 /* destLabel, common name, typespec (which may have binding label). */
3898 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3902 /* Set the given common block (com_block) to being bind(c) (1). */
3903 set_com_block_bind_c (com_block
, 1);
3909 /* Retrieve the list of one or more identifiers that the given bind(c)
3910 attribute applies to. */
3913 get_bind_c_idents (void)
3915 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3917 gfc_symbol
*tmp_sym
= NULL
;
3919 gfc_common_head
*com_block
= NULL
;
3921 if (gfc_match_name (name
) == MATCH_YES
)
3923 found_id
= MATCH_YES
;
3924 gfc_get_ha_symbol (name
, &tmp_sym
);
3926 else if (match_common_name (name
) == MATCH_YES
)
3928 found_id
= MATCH_YES
;
3929 com_block
= gfc_get_common (name
, 0);
3933 gfc_error ("Need either entity or common block name for "
3934 "attribute specification statement at %C");
3938 /* Save the current identifier and look for more. */
3941 /* Increment the number of identifiers found for this spec stmt. */
3944 /* Make sure we have a sym or com block, and verify that it can
3945 be bind(c). Set the appropriate field(s) and look for more
3947 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3949 if (tmp_sym
!= NULL
)
3951 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3957 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3962 /* Look to see if we have another identifier. */
3964 if (gfc_match_eos () == MATCH_YES
)
3965 found_id
= MATCH_NO
;
3966 else if (gfc_match_char (',') != MATCH_YES
)
3967 found_id
= MATCH_NO
;
3968 else if (gfc_match_name (name
) == MATCH_YES
)
3970 found_id
= MATCH_YES
;
3971 gfc_get_ha_symbol (name
, &tmp_sym
);
3973 else if (match_common_name (name
) == MATCH_YES
)
3975 found_id
= MATCH_YES
;
3976 com_block
= gfc_get_common (name
, 0);
3980 gfc_error ("Missing entity or common block name for "
3981 "attribute specification statement at %C");
3987 gfc_internal_error ("Missing symbol");
3989 } while (found_id
== MATCH_YES
);
3991 /* if we get here we were successful */
3996 /* Try and match a BIND(C) attribute specification statement. */
3999 gfc_match_bind_c_stmt (void)
4001 match found_match
= MATCH_NO
;
4006 /* This may not be necessary. */
4008 /* Clear the temporary binding label holder. */
4009 curr_binding_label
[0] = '\0';
4011 /* Look for the bind(c). */
4012 found_match
= gfc_match_bind_c (NULL
, true);
4014 if (found_match
== MATCH_YES
)
4016 /* Look for the :: now, but it is not required. */
4019 /* Get the identifier(s) that needs to be updated. This may need to
4020 change to hand the flag(s) for the attr specified so all identifiers
4021 found can have all appropriate parts updated (assuming that the same
4022 spec stmt can have multiple attrs, such as both bind(c) and
4024 if (get_bind_c_idents () != SUCCESS
)
4025 /* Error message should have printed already. */
4033 /* Match a data declaration statement. */
4036 gfc_match_data_decl (void)
4042 num_idents_on_line
= 0;
4044 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4048 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4049 && gfc_current_state () != COMP_DERIVED
)
4051 sym
= gfc_use_derived (current_ts
.u
.derived
);
4059 current_ts
.u
.derived
= sym
;
4062 m
= match_attr_spec ();
4063 if (m
== MATCH_ERROR
)
4069 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4070 && current_ts
.u
.derived
->components
== NULL
4071 && !current_ts
.u
.derived
->attr
.zero_comp
)
4074 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4077 gfc_find_symbol (current_ts
.u
.derived
->name
,
4078 current_ts
.u
.derived
->ns
->parent
, 1, &sym
);
4080 /* Any symbol that we find had better be a type definition
4081 which has its components defined. */
4082 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4083 && (current_ts
.u
.derived
->components
!= NULL
4084 || current_ts
.u
.derived
->attr
.zero_comp
))
4087 /* Now we have an error, which we signal, and then fix up
4088 because the knock-on is plain and simple confusing. */
4089 gfc_error_now ("Derived type at %C has not been previously defined "
4090 "and so cannot appear in a derived type definition");
4091 current_attr
.pointer
= 1;
4096 /* If we have an old-style character declaration, and no new-style
4097 attribute specifications, then there a comma is optional between
4098 the type specification and the variable list. */
4099 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4100 gfc_match_char (',');
4102 /* Give the types/attributes to symbols that follow. Give the element
4103 a number so that repeat character length expressions can be copied. */
4107 num_idents_on_line
++;
4108 m
= variable_decl (elem
++);
4109 if (m
== MATCH_ERROR
)
4114 if (gfc_match_eos () == MATCH_YES
)
4116 if (gfc_match_char (',') != MATCH_YES
)
4120 if (gfc_error_flag_test () == 0)
4121 gfc_error ("Syntax error in data declaration at %C");
4124 gfc_free_data_all (gfc_current_ns
);
4127 gfc_free_array_spec (current_as
);
4133 /* Match a prefix associated with a function or subroutine
4134 declaration. If the typespec pointer is nonnull, then a typespec
4135 can be matched. Note that if nothing matches, MATCH_YES is
4136 returned (the null string was matched). */
4139 gfc_match_prefix (gfc_typespec
*ts
)
4145 gfc_clear_attr (¤t_attr
);
4147 seen_impure
= false;
4149 gcc_assert (!gfc_matching_prefix
);
4150 gfc_matching_prefix
= true;
4154 found_prefix
= false;
4156 if (!seen_type
&& ts
!= NULL
4157 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4158 && gfc_match_space () == MATCH_YES
)
4162 found_prefix
= true;
4165 if (gfc_match ("elemental% ") == MATCH_YES
)
4167 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
4170 found_prefix
= true;
4173 if (gfc_match ("pure% ") == MATCH_YES
)
4175 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4178 found_prefix
= true;
4181 if (gfc_match ("recursive% ") == MATCH_YES
)
4183 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
4186 found_prefix
= true;
4189 /* IMPURE is a somewhat special case, as it needs not set an actual
4190 attribute but rather only prevents ELEMENTAL routines from being
4191 automatically PURE. */
4192 if (gfc_match ("impure% ") == MATCH_YES
)
4194 if (gfc_notify_std (GFC_STD_F2008
,
4195 "Fortran 2008: IMPURE procedure at %C")
4200 found_prefix
= true;
4203 while (found_prefix
);
4205 /* IMPURE and PURE must not both appear, of course. */
4206 if (seen_impure
&& current_attr
.pure
)
4208 gfc_error ("PURE and IMPURE must not appear both at %C");
4212 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4213 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4215 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4219 /* At this point, the next item is not a prefix. */
4220 gcc_assert (gfc_matching_prefix
);
4221 gfc_matching_prefix
= false;
4225 gcc_assert (gfc_matching_prefix
);
4226 gfc_matching_prefix
= false;
4231 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4234 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4236 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
4239 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
4242 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
4249 /* Match a formal argument list. */
4252 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4254 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4255 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4261 if (gfc_match_char ('(') != MATCH_YES
)
4268 if (gfc_match_char (')') == MATCH_YES
)
4273 if (gfc_match_char ('*') == MATCH_YES
)
4277 m
= gfc_match_name (name
);
4281 if (gfc_get_symbol (name
, NULL
, &sym
))
4285 p
= gfc_get_formal_arglist ();
4297 /* We don't add the VARIABLE flavor because the name could be a
4298 dummy procedure. We don't apply these attributes to formal
4299 arguments of statement functions. */
4300 if (sym
!= NULL
&& !st_flag
4301 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
4302 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
4308 /* The name of a program unit can be in a different namespace,
4309 so check for it explicitly. After the statement is accepted,
4310 the name is checked for especially in gfc_get_symbol(). */
4311 if (gfc_new_block
!= NULL
&& sym
!= NULL
4312 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4314 gfc_error ("Name '%s' at %C is the name of the procedure",
4320 if (gfc_match_char (')') == MATCH_YES
)
4323 m
= gfc_match_char (',');
4326 gfc_error ("Unexpected junk in formal argument list at %C");
4332 /* Check for duplicate symbols in the formal argument list. */
4335 for (p
= head
; p
->next
; p
= p
->next
)
4340 for (q
= p
->next
; q
; q
= q
->next
)
4341 if (p
->sym
== q
->sym
)
4343 gfc_error ("Duplicate symbol '%s' in formal argument list "
4344 "at %C", p
->sym
->name
);
4352 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
4362 gfc_free_formal_arglist (head
);
4367 /* Match a RESULT specification following a function declaration or
4368 ENTRY statement. Also matches the end-of-statement. */
4371 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4373 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4377 if (gfc_match (" result (") != MATCH_YES
)
4380 m
= gfc_match_name (name
);
4384 /* Get the right paren, and that's it because there could be the
4385 bind(c) attribute after the result clause. */
4386 if (gfc_match_char(')') != MATCH_YES
)
4388 /* TODO: should report the missing right paren here. */
4392 if (strcmp (function
->name
, name
) == 0)
4394 gfc_error ("RESULT variable at %C must be different than function name");
4398 if (gfc_get_symbol (name
, NULL
, &r
))
4401 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
4410 /* Match a function suffix, which could be a combination of a result
4411 clause and BIND(C), either one, or neither. The draft does not
4412 require them to come in a specific order. */
4415 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4417 match is_bind_c
; /* Found bind(c). */
4418 match is_result
; /* Found result clause. */
4419 match found_match
; /* Status of whether we've found a good match. */
4420 char peek_char
; /* Character we're going to peek at. */
4421 bool allow_binding_name
;
4423 /* Initialize to having found nothing. */
4424 found_match
= MATCH_NO
;
4425 is_bind_c
= MATCH_NO
;
4426 is_result
= MATCH_NO
;
4428 /* Get the next char to narrow between result and bind(c). */
4429 gfc_gobble_whitespace ();
4430 peek_char
= gfc_peek_ascii_char ();
4432 /* C binding names are not allowed for internal procedures. */
4433 if (gfc_current_state () == COMP_CONTAINS
4434 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4435 allow_binding_name
= false;
4437 allow_binding_name
= true;
4442 /* Look for result clause. */
4443 is_result
= match_result (sym
, result
);
4444 if (is_result
== MATCH_YES
)
4446 /* Now see if there is a bind(c) after it. */
4447 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4448 /* We've found the result clause and possibly bind(c). */
4449 found_match
= MATCH_YES
;
4452 /* This should only be MATCH_ERROR. */
4453 found_match
= is_result
;
4456 /* Look for bind(c) first. */
4457 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4458 if (is_bind_c
== MATCH_YES
)
4460 /* Now see if a result clause followed it. */
4461 is_result
= match_result (sym
, result
);
4462 found_match
= MATCH_YES
;
4466 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4467 found_match
= MATCH_ERROR
;
4471 gfc_error ("Unexpected junk after function declaration at %C");
4472 found_match
= MATCH_ERROR
;
4476 if (is_bind_c
== MATCH_YES
)
4478 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4479 if (gfc_current_state () == COMP_CONTAINS
4480 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4481 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4482 "at %L may not be specified for an internal "
4483 "procedure", &gfc_current_locus
)
4487 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4496 /* Procedure pointer return value without RESULT statement:
4497 Add "hidden" result variable named "ppr@". */
4500 add_hidden_procptr_result (gfc_symbol
*sym
)
4504 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4507 /* First usage case: PROCEDURE and EXTERNAL statements. */
4508 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4509 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4510 && sym
->attr
.external
;
4511 /* Second usage case: INTERFACE statements. */
4512 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4513 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4514 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4520 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4524 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4525 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4526 st2
->n
.sym
= stree
->n
.sym
;
4528 sym
->result
= stree
->n
.sym
;
4530 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4531 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4532 sym
->result
->attr
.external
= sym
->attr
.external
;
4533 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4534 sym
->result
->ts
= sym
->ts
;
4535 sym
->attr
.proc_pointer
= 0;
4536 sym
->attr
.pointer
= 0;
4537 sym
->attr
.external
= 0;
4538 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4540 sym
->result
->attr
.pointer
= 0;
4541 sym
->result
->attr
.proc_pointer
= 1;
4544 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4546 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4547 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4548 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4549 && sym
== gfc_current_ns
->proc_name
4550 && sym
== sym
->result
->ns
->proc_name
4551 && strcmp ("ppr@", sym
->result
->name
) == 0)
4553 sym
->result
->attr
.proc_pointer
= 1;
4554 sym
->attr
.pointer
= 0;
4562 /* Match the interface for a PROCEDURE declaration,
4563 including brackets (R1212). */
4566 match_procedure_interface (gfc_symbol
**proc_if
)
4570 locus old_loc
, entry_loc
;
4571 gfc_namespace
*old_ns
= gfc_current_ns
;
4572 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4574 old_loc
= entry_loc
= gfc_current_locus
;
4575 gfc_clear_ts (¤t_ts
);
4577 if (gfc_match (" (") != MATCH_YES
)
4579 gfc_current_locus
= entry_loc
;
4583 /* Get the type spec. for the procedure interface. */
4584 old_loc
= gfc_current_locus
;
4585 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4586 gfc_gobble_whitespace ();
4587 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4590 if (m
== MATCH_ERROR
)
4593 /* Procedure interface is itself a procedure. */
4594 gfc_current_locus
= old_loc
;
4595 m
= gfc_match_name (name
);
4597 /* First look to see if it is already accessible in the current
4598 namespace because it is use associated or contained. */
4600 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4603 /* If it is still not found, then try the parent namespace, if it
4604 exists and create the symbol there if it is still not found. */
4605 if (gfc_current_ns
->parent
)
4606 gfc_current_ns
= gfc_current_ns
->parent
;
4607 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4610 gfc_current_ns
= old_ns
;
4611 *proc_if
= st
->n
.sym
;
4613 /* Various interface checks. */
4617 /* Resolve interface if possible. That way, attr.procedure is only set
4618 if it is declared by a later procedure-declaration-stmt, which is
4619 invalid per C1212. */
4620 while ((*proc_if
)->ts
.interface
)
4621 *proc_if
= (*proc_if
)->ts
.interface
;
4623 if ((*proc_if
)->generic
)
4625 gfc_error ("Interface '%s' at %C may not be generic",
4629 if ((*proc_if
)->attr
.proc
== PROC_ST_FUNCTION
)
4631 gfc_error ("Interface '%s' at %C may not be a statement function",
4635 /* Handle intrinsic procedures. */
4636 if (!((*proc_if
)->attr
.external
|| (*proc_if
)->attr
.use_assoc
4637 || (*proc_if
)->attr
.if_source
== IFSRC_IFBODY
)
4638 && (gfc_is_intrinsic ((*proc_if
), 0, gfc_current_locus
)
4639 || gfc_is_intrinsic ((*proc_if
), 1, gfc_current_locus
)))
4640 (*proc_if
)->attr
.intrinsic
= 1;
4641 if ((*proc_if
)->attr
.intrinsic
4642 && !gfc_intrinsic_actual_ok ((*proc_if
)->name
, 0))
4644 gfc_error ("Intrinsic procedure '%s' not allowed "
4645 "in PROCEDURE statement at %C", (*proc_if
)->name
);
4651 if (gfc_match (" )") != MATCH_YES
)
4653 gfc_current_locus
= entry_loc
;
4661 /* Match a PROCEDURE declaration (R1211). */
4664 match_procedure_decl (void)
4667 gfc_symbol
*sym
, *proc_if
= NULL
;
4669 gfc_expr
*initializer
= NULL
;
4671 /* Parse interface (with brackets). */
4672 m
= match_procedure_interface (&proc_if
);
4676 /* Parse attributes (with colons). */
4677 m
= match_attr_spec();
4678 if (m
== MATCH_ERROR
)
4681 /* Get procedure symbols. */
4684 m
= gfc_match_symbol (&sym
, 0);
4687 else if (m
== MATCH_ERROR
)
4690 /* Add current_attr to the symbol attributes. */
4691 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4694 if (sym
->attr
.is_bind_c
)
4696 /* Check for C1218. */
4697 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4699 gfc_error ("BIND(C) attribute at %C requires "
4700 "an interface with BIND(C)");
4703 /* Check for C1217. */
4704 if (has_name_equals
&& sym
->attr
.pointer
)
4706 gfc_error ("BIND(C) procedure with NAME may not have "
4707 "POINTER attribute at %C");
4710 if (has_name_equals
&& sym
->attr
.dummy
)
4712 gfc_error ("Dummy procedure at %C may not have "
4713 "BIND(C) attribute with NAME");
4716 /* Set binding label for BIND(C). */
4717 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4721 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4724 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4727 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4730 /* Set interface. */
4731 if (proc_if
!= NULL
)
4733 if (sym
->ts
.type
!= BT_UNKNOWN
)
4735 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4736 sym
->name
, &gfc_current_locus
,
4737 gfc_basic_typename (sym
->ts
.type
));
4740 sym
->ts
.interface
= proc_if
;
4741 sym
->attr
.untyped
= 1;
4742 sym
->attr
.if_source
= IFSRC_IFBODY
;
4744 else if (current_ts
.type
!= BT_UNKNOWN
)
4746 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4748 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4749 sym
->ts
.interface
->ts
= current_ts
;
4750 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4751 sym
->ts
.interface
->attr
.function
= 1;
4752 sym
->attr
.function
= 1;
4753 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4756 if (gfc_match (" =>") == MATCH_YES
)
4758 if (!current_attr
.pointer
)
4760 gfc_error ("Initialization at %C isn't for a pointer variable");
4765 m
= match_pointer_init (&initializer
, 1);
4769 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4775 gfc_set_sym_referenced (sym
);
4777 if (gfc_match_eos () == MATCH_YES
)
4779 if (gfc_match_char (',') != MATCH_YES
)
4784 gfc_error ("Syntax error in PROCEDURE statement at %C");
4788 /* Free stuff up and return. */
4789 gfc_free_expr (initializer
);
4795 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4798 /* Match a procedure pointer component declaration (R445). */
4801 match_ppc_decl (void)
4804 gfc_symbol
*proc_if
= NULL
;
4808 gfc_expr
*initializer
= NULL
;
4809 gfc_typebound_proc
* tb
;
4810 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4812 /* Parse interface (with brackets). */
4813 m
= match_procedure_interface (&proc_if
);
4817 /* Parse attributes. */
4818 tb
= XCNEW (gfc_typebound_proc
);
4819 tb
->where
= gfc_current_locus
;
4820 m
= match_binding_attributes (tb
, false, true);
4821 if (m
== MATCH_ERROR
)
4824 gfc_clear_attr (¤t_attr
);
4825 current_attr
.procedure
= 1;
4826 current_attr
.proc_pointer
= 1;
4827 current_attr
.access
= tb
->access
;
4828 current_attr
.flavor
= FL_PROCEDURE
;
4830 /* Match the colons (required). */
4831 if (gfc_match (" ::") != MATCH_YES
)
4833 gfc_error ("Expected '::' after binding-attributes at %C");
4837 /* Check for C450. */
4838 if (!tb
->nopass
&& proc_if
== NULL
)
4840 gfc_error("NOPASS or explicit interface required at %C");
4844 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure pointer "
4845 "component at %C") == FAILURE
)
4848 /* Match PPC names. */
4852 m
= gfc_match_name (name
);
4855 else if (m
== MATCH_ERROR
)
4858 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
4861 /* Add current_attr to the symbol attributes. */
4862 if (gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4865 if (gfc_add_external (&c
->attr
, NULL
) == FAILURE
)
4868 if (gfc_add_proc (&c
->attr
, name
, NULL
) == FAILURE
)
4873 /* Set interface. */
4874 if (proc_if
!= NULL
)
4876 c
->ts
.interface
= proc_if
;
4877 c
->attr
.untyped
= 1;
4878 c
->attr
.if_source
= IFSRC_IFBODY
;
4880 else if (ts
.type
!= BT_UNKNOWN
)
4883 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4884 c
->ts
.interface
->ts
= ts
;
4885 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4886 c
->ts
.interface
->attr
.function
= 1;
4887 c
->attr
.function
= 1;
4888 c
->attr
.if_source
= IFSRC_UNKNOWN
;
4891 if (gfc_match (" =>") == MATCH_YES
)
4893 m
= match_pointer_init (&initializer
, 1);
4896 gfc_free_expr (initializer
);
4899 c
->initializer
= initializer
;
4902 if (gfc_match_eos () == MATCH_YES
)
4904 if (gfc_match_char (',') != MATCH_YES
)
4909 gfc_error ("Syntax error in procedure pointer component at %C");
4914 /* Match a PROCEDURE declaration inside an interface (R1206). */
4917 match_procedure_in_interface (void)
4921 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4923 if (current_interface
.type
== INTERFACE_NAMELESS
4924 || current_interface
.type
== INTERFACE_ABSTRACT
)
4926 gfc_error ("PROCEDURE at %C must be in a generic interface");
4932 m
= gfc_match_name (name
);
4935 else if (m
== MATCH_ERROR
)
4937 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4940 if (gfc_add_interface (sym
) == FAILURE
)
4943 if (gfc_match_eos () == MATCH_YES
)
4945 if (gfc_match_char (',') != MATCH_YES
)
4952 gfc_error ("Syntax error in PROCEDURE statement at %C");
4957 /* General matcher for PROCEDURE declarations. */
4959 static match
match_procedure_in_type (void);
4962 gfc_match_procedure (void)
4966 switch (gfc_current_state ())
4971 case COMP_SUBROUTINE
:
4973 m
= match_procedure_decl ();
4975 case COMP_INTERFACE
:
4976 m
= match_procedure_in_interface ();
4979 m
= match_ppc_decl ();
4981 case COMP_DERIVED_CONTAINS
:
4982 m
= match_procedure_in_type ();
4991 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4999 /* Warn if a matched procedure has the same name as an intrinsic; this is
5000 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5001 parser-state-stack to find out whether we're in a module. */
5004 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5008 in_module
= (gfc_state_stack
->previous
5009 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5011 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5015 /* Match a function declaration. */
5018 gfc_match_function_decl (void)
5020 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5021 gfc_symbol
*sym
, *result
;
5025 match found_match
; /* Status returned by match func. */
5027 if (gfc_current_state () != COMP_NONE
5028 && gfc_current_state () != COMP_INTERFACE
5029 && gfc_current_state () != COMP_CONTAINS
)
5032 gfc_clear_ts (¤t_ts
);
5034 old_loc
= gfc_current_locus
;
5036 m
= gfc_match_prefix (¤t_ts
);
5039 gfc_current_locus
= old_loc
;
5043 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5045 gfc_current_locus
= old_loc
;
5048 if (get_proc_name (name
, &sym
, false))
5051 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5054 gfc_new_block
= sym
;
5056 m
= gfc_match_formal_arglist (sym
, 0, 0);
5059 gfc_error ("Expected formal argument list in function "
5060 "definition at %C");
5064 else if (m
== MATCH_ERROR
)
5069 /* According to the draft, the bind(c) and result clause can
5070 come in either order after the formal_arg_list (i.e., either
5071 can be first, both can exist together or by themselves or neither
5072 one). Therefore, the match_result can't match the end of the
5073 string, and check for the bind(c) or result clause in either order. */
5074 found_match
= gfc_match_eos ();
5076 /* Make sure that it isn't already declared as BIND(C). If it is, it
5077 must have been marked BIND(C) with a BIND(C) attribute and that is
5078 not allowed for procedures. */
5079 if (sym
->attr
.is_bind_c
== 1)
5081 sym
->attr
.is_bind_c
= 0;
5082 if (sym
->old_symbol
!= NULL
)
5083 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5084 "variables or common blocks",
5085 &(sym
->old_symbol
->declared_at
));
5087 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5088 "variables or common blocks", &gfc_current_locus
);
5091 if (found_match
!= MATCH_YES
)
5093 /* If we haven't found the end-of-statement, look for a suffix. */
5094 suffix_match
= gfc_match_suffix (sym
, &result
);
5095 if (suffix_match
== MATCH_YES
)
5096 /* Need to get the eos now. */
5097 found_match
= gfc_match_eos ();
5099 found_match
= suffix_match
;
5102 if(found_match
!= MATCH_YES
)
5106 /* Make changes to the symbol. */
5109 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5112 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
5113 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5116 /* Delay matching the function characteristics until after the
5117 specification block by signalling kind=-1. */
5118 sym
->declared_at
= old_loc
;
5119 if (current_ts
.type
!= BT_UNKNOWN
)
5120 current_ts
.kind
= -1;
5122 current_ts
.kind
= 0;
5126 if (current_ts
.type
!= BT_UNKNOWN
5127 && gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
5133 if (current_ts
.type
!= BT_UNKNOWN
5134 && gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
)
5137 sym
->result
= result
;
5140 /* Warn if this procedure has the same name as an intrinsic. */
5141 warn_intrinsic_shadow (sym
, true);
5147 gfc_current_locus
= old_loc
;
5152 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5153 pass the name of the entry, rather than the gfc_current_block name, and
5154 to return false upon finding an existing global entry. */
5157 add_global_entry (const char *name
, int sub
)
5160 enum gfc_symbol_type type
;
5162 s
= gfc_get_gsymbol(name
);
5163 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5166 || (s
->type
!= GSYM_UNKNOWN
5167 && s
->type
!= type
))
5168 gfc_global_used(s
, NULL
);
5172 s
->where
= gfc_current_locus
;
5174 s
->ns
= gfc_current_ns
;
5181 /* Match an ENTRY statement. */
5184 gfc_match_entry (void)
5189 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5190 gfc_compile_state state
;
5194 bool module_procedure
;
5198 m
= gfc_match_name (name
);
5202 if (gfc_notify_std (GFC_STD_F2008_OBS
, "Fortran 2008 obsolescent feature: "
5203 "ENTRY statement at %C") == FAILURE
)
5206 state
= gfc_current_state ();
5207 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5212 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5215 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5217 case COMP_BLOCK_DATA
:
5218 gfc_error ("ENTRY statement at %C cannot appear within "
5221 case COMP_INTERFACE
:
5222 gfc_error ("ENTRY statement at %C cannot appear within "
5226 gfc_error ("ENTRY statement at %C cannot appear within "
5227 "a DERIVED TYPE block");
5230 gfc_error ("ENTRY statement at %C cannot appear within "
5231 "an IF-THEN block");
5234 gfc_error ("ENTRY statement at %C cannot appear within "
5238 gfc_error ("ENTRY statement at %C cannot appear within "
5242 gfc_error ("ENTRY statement at %C cannot appear within "
5246 gfc_error ("ENTRY statement at %C cannot appear within "
5250 gfc_error ("ENTRY statement at %C cannot appear within "
5251 "a contained subprogram");
5254 gfc_internal_error ("gfc_match_entry(): Bad state");
5259 module_procedure
= gfc_current_ns
->parent
!= NULL
5260 && gfc_current_ns
->parent
->proc_name
5261 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5264 if (gfc_current_ns
->parent
!= NULL
5265 && gfc_current_ns
->parent
->proc_name
5266 && !module_procedure
)
5268 gfc_error("ENTRY statement at %C cannot appear in a "
5269 "contained procedure");
5273 /* Module function entries need special care in get_proc_name
5274 because previous references within the function will have
5275 created symbols attached to the current namespace. */
5276 if (get_proc_name (name
, &entry
,
5277 gfc_current_ns
->parent
!= NULL
5278 && module_procedure
))
5281 proc
= gfc_current_block ();
5283 /* Make sure that it isn't already declared as BIND(C). If it is, it
5284 must have been marked BIND(C) with a BIND(C) attribute and that is
5285 not allowed for procedures. */
5286 if (entry
->attr
.is_bind_c
== 1)
5288 entry
->attr
.is_bind_c
= 0;
5289 if (entry
->old_symbol
!= NULL
)
5290 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5291 "variables or common blocks",
5292 &(entry
->old_symbol
->declared_at
));
5294 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5295 "variables or common blocks", &gfc_current_locus
);
5298 /* Check what next non-whitespace character is so we can tell if there
5299 is the required parens if we have a BIND(C). */
5300 gfc_gobble_whitespace ();
5301 peek_char
= gfc_peek_ascii_char ();
5303 if (state
== COMP_SUBROUTINE
)
5305 /* An entry in a subroutine. */
5306 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
5309 m
= gfc_match_formal_arglist (entry
, 0, 1);
5313 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5314 never be an internal procedure. */
5315 is_bind_c
= gfc_match_bind_c (entry
, true);
5316 if (is_bind_c
== MATCH_ERROR
)
5318 if (is_bind_c
== MATCH_YES
)
5320 if (peek_char
!= '(')
5322 gfc_error ("Missing required parentheses before BIND(C) at %C");
5325 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
5330 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5331 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5336 /* An entry in a function.
5337 We need to take special care because writing
5342 ENTRY f() RESULT (r)
5344 ENTRY f RESULT (r). */
5345 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
5348 old_loc
= gfc_current_locus
;
5349 if (gfc_match_eos () == MATCH_YES
)
5351 gfc_current_locus
= old_loc
;
5352 /* Match the empty argument list, and add the interface to
5354 m
= gfc_match_formal_arglist (entry
, 0, 1);
5357 m
= gfc_match_formal_arglist (entry
, 0, 0);
5364 if (gfc_match_eos () == MATCH_YES
)
5366 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5367 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5370 entry
->result
= entry
;
5374 m
= gfc_match_suffix (entry
, &result
);
5376 gfc_syntax_error (ST_ENTRY
);
5382 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
5383 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
5384 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
5387 entry
->result
= result
;
5391 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5392 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5394 entry
->result
= entry
;
5399 if (gfc_match_eos () != MATCH_YES
)
5401 gfc_syntax_error (ST_ENTRY
);
5405 entry
->attr
.recursive
= proc
->attr
.recursive
;
5406 entry
->attr
.elemental
= proc
->attr
.elemental
;
5407 entry
->attr
.pure
= proc
->attr
.pure
;
5409 el
= gfc_get_entry_list ();
5411 el
->next
= gfc_current_ns
->entries
;
5412 gfc_current_ns
->entries
= el
;
5414 el
->id
= el
->next
->id
+ 1;
5418 new_st
.op
= EXEC_ENTRY
;
5419 new_st
.ext
.entry
= el
;
5425 /* Match a subroutine statement, including optional prefixes. */
5428 gfc_match_subroutine (void)
5430 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5435 bool allow_binding_name
;
5437 if (gfc_current_state () != COMP_NONE
5438 && gfc_current_state () != COMP_INTERFACE
5439 && gfc_current_state () != COMP_CONTAINS
)
5442 m
= gfc_match_prefix (NULL
);
5446 m
= gfc_match ("subroutine% %n", name
);
5450 if (get_proc_name (name
, &sym
, false))
5453 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5454 the symbol existed before. */
5455 sym
->declared_at
= gfc_current_locus
;
5457 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5460 gfc_new_block
= sym
;
5462 /* Check what next non-whitespace character is so we can tell if there
5463 is the required parens if we have a BIND(C). */
5464 gfc_gobble_whitespace ();
5465 peek_char
= gfc_peek_ascii_char ();
5467 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5470 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5473 /* Make sure that it isn't already declared as BIND(C). If it is, it
5474 must have been marked BIND(C) with a BIND(C) attribute and that is
5475 not allowed for procedures. */
5476 if (sym
->attr
.is_bind_c
== 1)
5478 sym
->attr
.is_bind_c
= 0;
5479 if (sym
->old_symbol
!= NULL
)
5480 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5481 "variables or common blocks",
5482 &(sym
->old_symbol
->declared_at
));
5484 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5485 "variables or common blocks", &gfc_current_locus
);
5488 /* C binding names are not allowed for internal procedures. */
5489 if (gfc_current_state () == COMP_CONTAINS
5490 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5491 allow_binding_name
= false;
5493 allow_binding_name
= true;
5495 /* Here, we are just checking if it has the bind(c) attribute, and if
5496 so, then we need to make sure it's all correct. If it doesn't,
5497 we still need to continue matching the rest of the subroutine line. */
5498 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5499 if (is_bind_c
== MATCH_ERROR
)
5501 /* There was an attempt at the bind(c), but it was wrong. An
5502 error message should have been printed w/in the gfc_match_bind_c
5503 so here we'll just return the MATCH_ERROR. */
5507 if (is_bind_c
== MATCH_YES
)
5509 /* The following is allowed in the Fortran 2008 draft. */
5510 if (gfc_current_state () == COMP_CONTAINS
5511 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5512 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
5513 "at %L may not be specified for an internal "
5514 "procedure", &gfc_current_locus
)
5518 if (peek_char
!= '(')
5520 gfc_error ("Missing required parentheses before BIND(C) at %C");
5523 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
5528 if (gfc_match_eos () != MATCH_YES
)
5530 gfc_syntax_error (ST_SUBROUTINE
);
5534 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5537 /* Warn if it has the same name as an intrinsic. */
5538 warn_intrinsic_shadow (sym
, false);
5544 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5545 given, and set the binding label in either the given symbol (if not
5546 NULL), or in the current_ts. The symbol may be NULL because we may
5547 encounter the BIND(C) before the declaration itself. Return
5548 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5549 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5550 or MATCH_YES if the specifier was correct and the binding label and
5551 bind(c) fields were set correctly for the given symbol or the
5552 current_ts. If allow_binding_name is false, no binding name may be
5556 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5558 /* binding label, if exists */
5559 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
5563 /* Initialize the flag that specifies whether we encountered a NAME=
5564 specifier or not. */
5565 has_name_equals
= 0;
5567 /* Init the first char to nil so we can catch if we don't have
5568 the label (name attr) or the symbol name yet. */
5569 binding_label
[0] = '\0';
5571 /* This much we have to be able to match, in this order, if
5572 there is a bind(c) label. */
5573 if (gfc_match (" bind ( c ") != MATCH_YES
)
5576 /* Now see if there is a binding label, or if we've reached the
5577 end of the bind(c) attribute without one. */
5578 if (gfc_match_char (',') == MATCH_YES
)
5580 if (gfc_match (" name = ") != MATCH_YES
)
5582 gfc_error ("Syntax error in NAME= specifier for binding label "
5584 /* should give an error message here */
5588 has_name_equals
= 1;
5590 /* Get the opening quote. */
5591 double_quote
= MATCH_YES
;
5592 single_quote
= MATCH_YES
;
5593 double_quote
= gfc_match_char ('"');
5594 if (double_quote
!= MATCH_YES
)
5595 single_quote
= gfc_match_char ('\'');
5596 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5598 gfc_error ("Syntax error in NAME= specifier for binding label "
5603 /* Grab the binding label, using functions that will not lower
5604 case the names automatically. */
5605 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
5608 /* Get the closing quotation. */
5609 if (double_quote
== MATCH_YES
)
5611 if (gfc_match_char ('"') != MATCH_YES
)
5613 gfc_error ("Missing closing quote '\"' for binding label at %C");
5614 /* User started string with '"' so looked to match it. */
5620 if (gfc_match_char ('\'') != MATCH_YES
)
5622 gfc_error ("Missing closing quote '\'' for binding label at %C");
5623 /* User started string with "'" char. */
5629 /* Get the required right paren. */
5630 if (gfc_match_char (')') != MATCH_YES
)
5632 gfc_error ("Missing closing paren for binding label at %C");
5636 if (has_name_equals
&& !allow_binding_name
)
5638 gfc_error ("No binding name is allowed in BIND(C) at %C");
5642 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5644 gfc_error ("For dummy procedure %s, no binding name is "
5645 "allowed in BIND(C) at %C", sym
->name
);
5650 /* Save the binding label to the symbol. If sym is null, we're
5651 probably matching the typespec attributes of a declaration and
5652 haven't gotten the name yet, and therefore, no symbol yet. */
5653 if (binding_label
[0] != '\0')
5657 strcpy (sym
->binding_label
, binding_label
);
5660 strcpy (curr_binding_label
, binding_label
);
5662 else if (allow_binding_name
)
5664 /* No binding label, but if symbol isn't null, we
5665 can set the label for it here.
5666 If name="" or allow_binding_name is false, no C binding name is
5668 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5669 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
5672 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5673 && current_interface
.type
== INTERFACE_ABSTRACT
)
5675 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5683 /* Return nonzero if we're currently compiling a contained procedure. */
5686 contained_procedure (void)
5688 gfc_state_data
*s
= gfc_state_stack
;
5690 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5691 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5697 /* Set the kind of each enumerator. The kind is selected such that it is
5698 interoperable with the corresponding C enumeration type, making
5699 sure that -fshort-enums is honored. */
5704 enumerator_history
*current_history
= NULL
;
5708 if (max_enum
== NULL
|| enum_history
== NULL
)
5711 if (!flag_short_enums
)
5717 kind
= gfc_integer_kinds
[i
++].kind
;
5719 while (kind
< gfc_c_int_kind
5720 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5723 current_history
= enum_history
;
5724 while (current_history
!= NULL
)
5726 current_history
->sym
->ts
.kind
= kind
;
5727 current_history
= current_history
->next
;
5732 /* Match any of the various end-block statements. Returns the type of
5733 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5734 and END BLOCK statements cannot be replaced by a single END statement. */
5737 gfc_match_end (gfc_statement
*st
)
5739 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5740 gfc_compile_state state
;
5742 const char *block_name
;
5747 old_loc
= gfc_current_locus
;
5748 if (gfc_match ("end") != MATCH_YES
)
5751 state
= gfc_current_state ();
5752 block_name
= gfc_current_block () == NULL
5753 ? NULL
: gfc_current_block ()->name
;
5757 case COMP_ASSOCIATE
:
5759 if (!strncmp (block_name
, "block@", strlen("block@")))
5764 case COMP_DERIVED_CONTAINS
:
5765 state
= gfc_state_stack
->previous
->state
;
5766 block_name
= gfc_state_stack
->previous
->sym
== NULL
5767 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5778 *st
= ST_END_PROGRAM
;
5779 target
= " program";
5783 case COMP_SUBROUTINE
:
5784 *st
= ST_END_SUBROUTINE
;
5785 target
= " subroutine";
5786 eos_ok
= !contained_procedure ();
5790 *st
= ST_END_FUNCTION
;
5791 target
= " function";
5792 eos_ok
= !contained_procedure ();
5795 case COMP_BLOCK_DATA
:
5796 *st
= ST_END_BLOCK_DATA
;
5797 target
= " block data";
5802 *st
= ST_END_MODULE
;
5807 case COMP_INTERFACE
:
5808 *st
= ST_END_INTERFACE
;
5809 target
= " interface";
5814 case COMP_DERIVED_CONTAINS
:
5820 case COMP_ASSOCIATE
:
5821 *st
= ST_END_ASSOCIATE
;
5822 target
= " associate";
5845 *st
= ST_END_CRITICAL
;
5846 target
= " critical";
5851 case COMP_SELECT_TYPE
:
5852 *st
= ST_END_SELECT
;
5858 *st
= ST_END_FORALL
;
5873 last_initializer
= NULL
;
5875 gfc_free_enum_history ();
5879 gfc_error ("Unexpected END statement at %C");
5883 if (gfc_match_eos () == MATCH_YES
)
5885 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
5887 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: END statement "
5888 "instead of %s statement at %L",
5889 gfc_ascii_statement (*st
), &old_loc
) == FAILURE
)
5894 /* We would have required END [something]. */
5895 gfc_error ("%s statement expected at %L",
5896 gfc_ascii_statement (*st
), &old_loc
);
5903 /* Verify that we've got the sort of end-block that we're expecting. */
5904 if (gfc_match (target
) != MATCH_YES
)
5906 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
5910 /* If we're at the end, make sure a block name wasn't required. */
5911 if (gfc_match_eos () == MATCH_YES
)
5914 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
5915 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
5916 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
5922 gfc_error ("Expected block name of '%s' in %s statement at %C",
5923 block_name
, gfc_ascii_statement (*st
));
5928 /* END INTERFACE has a special handler for its several possible endings. */
5929 if (*st
== ST_END_INTERFACE
)
5930 return gfc_match_end_interface ();
5932 /* We haven't hit the end of statement, so what is left must be an
5934 m
= gfc_match_space ();
5936 m
= gfc_match_name (name
);
5939 gfc_error ("Expected terminating name at %C");
5943 if (block_name
== NULL
)
5946 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
5948 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
5949 gfc_ascii_statement (*st
));
5952 /* Procedure pointer as function result. */
5953 else if (strcmp (block_name
, "ppr@") == 0
5954 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
5956 gfc_error ("Expected label '%s' for %s statement at %C",
5957 gfc_current_block ()->ns
->proc_name
->name
,
5958 gfc_ascii_statement (*st
));
5962 if (gfc_match_eos () == MATCH_YES
)
5966 gfc_syntax_error (*st
);
5969 gfc_current_locus
= old_loc
;
5975 /***************** Attribute declaration statements ****************/
5977 /* Set the attribute of a single variable. */
5982 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5990 m
= gfc_match_name (name
);
5994 if (find_special (name
, &sym
, false))
5997 var_locus
= gfc_current_locus
;
5999 /* Deal with possible array specification for certain attributes. */
6000 if (current_attr
.dimension
6001 || current_attr
.codimension
6002 || current_attr
.allocatable
6003 || current_attr
.pointer
6004 || current_attr
.target
)
6006 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6007 !current_attr
.dimension
6008 && !current_attr
.pointer
6009 && !current_attr
.target
);
6010 if (m
== MATCH_ERROR
)
6013 if (current_attr
.dimension
&& m
== MATCH_NO
)
6015 gfc_error ("Missing array specification at %L in DIMENSION "
6016 "statement", &var_locus
);
6021 if (current_attr
.dimension
&& sym
->value
)
6023 gfc_error ("Dimensions specified for %s at %L after its "
6024 "initialisation", sym
->name
, &var_locus
);
6029 if (current_attr
.codimension
&& m
== MATCH_NO
)
6031 gfc_error ("Missing array specification at %L in CODIMENSION "
6032 "statement", &var_locus
);
6037 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6038 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6040 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6046 /* Update symbol table. DIMENSION attribute is set in
6047 gfc_set_array_spec(). For CLASS variables, this must be applied
6048 to the first component, or '_data' field. */
6049 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6051 if (gfc_copy_attr (&CLASS_DATA (sym
)->attr
, ¤t_attr
, &var_locus
)
6060 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6061 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
6068 if (sym
->ts
.type
== BT_CLASS
6069 && gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false) == FAILURE
)
6075 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
6081 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6083 /* Fix the array spec. */
6084 m
= gfc_mod_pointee_as (sym
->as
);
6085 if (m
== MATCH_ERROR
)
6089 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
6095 if ((current_attr
.external
|| current_attr
.intrinsic
)
6096 && sym
->attr
.flavor
!= FL_PROCEDURE
6097 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
6103 add_hidden_procptr_result (sym
);
6108 gfc_free_array_spec (as
);
6113 /* Generic attribute declaration subroutine. Used for attributes that
6114 just have a list of names. */
6121 /* Gobble the optional double colon, by simply ignoring the result
6131 if (gfc_match_eos () == MATCH_YES
)
6137 if (gfc_match_char (',') != MATCH_YES
)
6139 gfc_error ("Unexpected character in variable list at %C");
6149 /* This routine matches Cray Pointer declarations of the form:
6150 pointer ( <pointer>, <pointee> )
6152 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6153 The pointer, if already declared, should be an integer. Otherwise, we
6154 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6155 be either a scalar, or an array declaration. No space is allocated for
6156 the pointee. For the statement
6157 pointer (ipt, ar(10))
6158 any subsequent uses of ar will be translated (in C-notation) as
6159 ar(i) => ((<type> *) ipt)(i)
6160 After gimplification, pointee variable will disappear in the code. */
6163 cray_pointer_decl (void)
6166 gfc_array_spec
*as
= NULL
;
6167 gfc_symbol
*cptr
; /* Pointer symbol. */
6168 gfc_symbol
*cpte
; /* Pointee symbol. */
6174 if (gfc_match_char ('(') != MATCH_YES
)
6176 gfc_error ("Expected '(' at %C");
6180 /* Match pointer. */
6181 var_locus
= gfc_current_locus
;
6182 gfc_clear_attr (¤t_attr
);
6183 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6184 current_ts
.type
= BT_INTEGER
;
6185 current_ts
.kind
= gfc_index_integer_kind
;
6187 m
= gfc_match_symbol (&cptr
, 0);
6190 gfc_error ("Expected variable name at %C");
6194 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
6197 gfc_set_sym_referenced (cptr
);
6199 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6201 cptr
->ts
.type
= BT_INTEGER
;
6202 cptr
->ts
.kind
= gfc_index_integer_kind
;
6204 else if (cptr
->ts
.type
!= BT_INTEGER
)
6206 gfc_error ("Cray pointer at %C must be an integer");
6209 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6210 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6211 " memory addresses require %d bytes",
6212 cptr
->ts
.kind
, gfc_index_integer_kind
);
6214 if (gfc_match_char (',') != MATCH_YES
)
6216 gfc_error ("Expected \",\" at %C");
6220 /* Match Pointee. */
6221 var_locus
= gfc_current_locus
;
6222 gfc_clear_attr (¤t_attr
);
6223 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6224 current_ts
.type
= BT_UNKNOWN
;
6225 current_ts
.kind
= 0;
6227 m
= gfc_match_symbol (&cpte
, 0);
6230 gfc_error ("Expected variable name at %C");
6234 /* Check for an optional array spec. */
6235 m
= gfc_match_array_spec (&as
, true, false);
6236 if (m
== MATCH_ERROR
)
6238 gfc_free_array_spec (as
);
6241 else if (m
== MATCH_NO
)
6243 gfc_free_array_spec (as
);
6247 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
6250 gfc_set_sym_referenced (cpte
);
6252 if (cpte
->as
== NULL
)
6254 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
6255 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6257 else if (as
!= NULL
)
6259 gfc_error ("Duplicate array spec for Cray pointee at %C");
6260 gfc_free_array_spec (as
);
6266 if (cpte
->as
!= NULL
)
6268 /* Fix array spec. */
6269 m
= gfc_mod_pointee_as (cpte
->as
);
6270 if (m
== MATCH_ERROR
)
6274 /* Point the Pointee at the Pointer. */
6275 cpte
->cp_pointer
= cptr
;
6277 if (gfc_match_char (')') != MATCH_YES
)
6279 gfc_error ("Expected \")\" at %C");
6282 m
= gfc_match_char (',');
6284 done
= true; /* Stop searching for more declarations. */
6288 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6289 || gfc_match_eos () != MATCH_YES
)
6291 gfc_error ("Expected \",\" or end of statement at %C");
6299 gfc_match_external (void)
6302 gfc_clear_attr (¤t_attr
);
6303 current_attr
.external
= 1;
6305 return attr_decl ();
6310 gfc_match_intent (void)
6314 /* This is not allowed within a BLOCK construct! */
6315 if (gfc_current_state () == COMP_BLOCK
)
6317 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6321 intent
= match_intent_spec ();
6322 if (intent
== INTENT_UNKNOWN
)
6325 gfc_clear_attr (¤t_attr
);
6326 current_attr
.intent
= intent
;
6328 return attr_decl ();
6333 gfc_match_intrinsic (void)
6336 gfc_clear_attr (¤t_attr
);
6337 current_attr
.intrinsic
= 1;
6339 return attr_decl ();
6344 gfc_match_optional (void)
6346 /* This is not allowed within a BLOCK construct! */
6347 if (gfc_current_state () == COMP_BLOCK
)
6349 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6353 gfc_clear_attr (¤t_attr
);
6354 current_attr
.optional
= 1;
6356 return attr_decl ();
6361 gfc_match_pointer (void)
6363 gfc_gobble_whitespace ();
6364 if (gfc_peek_ascii_char () == '(')
6366 if (!gfc_option
.flag_cray_pointer
)
6368 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6372 return cray_pointer_decl ();
6376 gfc_clear_attr (¤t_attr
);
6377 current_attr
.pointer
= 1;
6379 return attr_decl ();
6385 gfc_match_allocatable (void)
6387 gfc_clear_attr (¤t_attr
);
6388 current_attr
.allocatable
= 1;
6390 return attr_decl ();
6395 gfc_match_codimension (void)
6397 gfc_clear_attr (¤t_attr
);
6398 current_attr
.codimension
= 1;
6400 return attr_decl ();
6405 gfc_match_contiguous (void)
6407 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTIGUOUS statement at %C")
6411 gfc_clear_attr (¤t_attr
);
6412 current_attr
.contiguous
= 1;
6414 return attr_decl ();
6419 gfc_match_dimension (void)
6421 gfc_clear_attr (¤t_attr
);
6422 current_attr
.dimension
= 1;
6424 return attr_decl ();
6429 gfc_match_target (void)
6431 gfc_clear_attr (¤t_attr
);
6432 current_attr
.target
= 1;
6434 return attr_decl ();
6438 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6442 access_attr_decl (gfc_statement st
)
6444 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6445 interface_type type
;
6448 gfc_intrinsic_op op
;
6451 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6456 m
= gfc_match_generic_spec (&type
, name
, &op
);
6459 if (m
== MATCH_ERROR
)
6464 case INTERFACE_NAMELESS
:
6465 case INTERFACE_ABSTRACT
:
6468 case INTERFACE_GENERIC
:
6469 if (gfc_get_symbol (name
, NULL
, &sym
))
6472 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
6473 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6474 sym
->name
, NULL
) == FAILURE
)
6479 case INTERFACE_INTRINSIC_OP
:
6480 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6482 gfc_intrinsic_op other_op
;
6484 gfc_current_ns
->operator_access
[op
] =
6485 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6487 /* Handle the case if there is another op with the same
6488 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6489 other_op
= gfc_equivalent_op (op
);
6491 if (other_op
!= INTRINSIC_NONE
)
6492 gfc_current_ns
->operator_access
[other_op
] =
6493 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6498 gfc_error ("Access specification of the %s operator at %C has "
6499 "already been specified", gfc_op2string (op
));
6505 case INTERFACE_USER_OP
:
6506 uop
= gfc_get_uop (name
);
6508 if (uop
->access
== ACCESS_UNKNOWN
)
6510 uop
->access
= (st
== ST_PUBLIC
)
6511 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6515 gfc_error ("Access specification of the .%s. operator at %C "
6516 "has already been specified", sym
->name
);
6523 if (gfc_match_char (',') == MATCH_NO
)
6527 if (gfc_match_eos () != MATCH_YES
)
6532 gfc_syntax_error (st
);
6540 gfc_match_protected (void)
6545 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6547 gfc_error ("PROTECTED at %C only allowed in specification "
6548 "part of a module");
6553 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
6557 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6562 if (gfc_match_eos () == MATCH_YES
)
6567 m
= gfc_match_symbol (&sym
, 0);
6571 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6584 if (gfc_match_eos () == MATCH_YES
)
6586 if (gfc_match_char (',') != MATCH_YES
)
6593 gfc_error ("Syntax error in PROTECTED statement at %C");
6598 /* The PRIVATE statement is a bit weird in that it can be an attribute
6599 declaration, but also works as a standalone statement inside of a
6600 type declaration or a module. */
6603 gfc_match_private (gfc_statement
*st
)
6606 if (gfc_match ("private") != MATCH_YES
)
6609 if (gfc_current_state () != COMP_MODULE
6610 && !(gfc_current_state () == COMP_DERIVED
6611 && gfc_state_stack
->previous
6612 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6613 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6614 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6615 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6617 gfc_error ("PRIVATE statement at %C is only allowed in the "
6618 "specification part of a module");
6622 if (gfc_current_state () == COMP_DERIVED
)
6624 if (gfc_match_eos () == MATCH_YES
)
6630 gfc_syntax_error (ST_PRIVATE
);
6634 if (gfc_match_eos () == MATCH_YES
)
6641 return access_attr_decl (ST_PRIVATE
);
6646 gfc_match_public (gfc_statement
*st
)
6649 if (gfc_match ("public") != MATCH_YES
)
6652 if (gfc_current_state () != COMP_MODULE
)
6654 gfc_error ("PUBLIC statement at %C is only allowed in the "
6655 "specification part of a module");
6659 if (gfc_match_eos () == MATCH_YES
)
6666 return access_attr_decl (ST_PUBLIC
);
6670 /* Workhorse for gfc_match_parameter. */
6680 m
= gfc_match_symbol (&sym
, 0);
6682 gfc_error ("Expected variable name at %C in PARAMETER statement");
6687 if (gfc_match_char ('=') == MATCH_NO
)
6689 gfc_error ("Expected = sign in PARAMETER statement at %C");
6693 m
= gfc_match_init_expr (&init
);
6695 gfc_error ("Expected expression at %C in PARAMETER statement");
6699 if (sym
->ts
.type
== BT_UNKNOWN
6700 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6706 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6707 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6715 gfc_error ("Initializing already initialized variable at %C");
6720 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6721 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6724 gfc_free_expr (init
);
6729 /* Match a parameter statement, with the weird syntax that these have. */
6732 gfc_match_parameter (void)
6736 if (gfc_match_char ('(') == MATCH_NO
)
6745 if (gfc_match (" )%t") == MATCH_YES
)
6748 if (gfc_match_char (',') != MATCH_YES
)
6750 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6760 /* Save statements have a special syntax. */
6763 gfc_match_save (void)
6765 char n
[GFC_MAX_SYMBOL_LEN
+1];
6770 if (gfc_match_eos () == MATCH_YES
)
6772 if (gfc_current_ns
->seen_save
)
6774 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6775 "follows previous SAVE statement")
6780 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6784 if (gfc_current_ns
->save_all
)
6786 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6787 "blanket SAVE statement")
6796 m
= gfc_match_symbol (&sym
, 0);
6800 if (gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
6801 &gfc_current_locus
) == FAILURE
)
6812 m
= gfc_match (" / %n /", &n
);
6813 if (m
== MATCH_ERROR
)
6818 c
= gfc_get_common (n
, 0);
6821 gfc_current_ns
->seen_save
= 1;
6824 if (gfc_match_eos () == MATCH_YES
)
6826 if (gfc_match_char (',') != MATCH_YES
)
6833 gfc_error ("Syntax error in SAVE statement at %C");
6839 gfc_match_value (void)
6844 /* This is not allowed within a BLOCK construct! */
6845 if (gfc_current_state () == COMP_BLOCK
)
6847 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6851 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
6855 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6860 if (gfc_match_eos () == MATCH_YES
)
6865 m
= gfc_match_symbol (&sym
, 0);
6869 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6882 if (gfc_match_eos () == MATCH_YES
)
6884 if (gfc_match_char (',') != MATCH_YES
)
6891 gfc_error ("Syntax error in VALUE statement at %C");
6897 gfc_match_volatile (void)
6902 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
6906 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6911 if (gfc_match_eos () == MATCH_YES
)
6916 /* VOLATILE is special because it can be added to host-associated
6917 symbols locally. Except for coarrays. */
6918 m
= gfc_match_symbol (&sym
, 1);
6922 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6923 for variable in a BLOCK which is defined outside of the BLOCK. */
6924 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
6926 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6927 "%C, which is use-/host-associated", sym
->name
);
6930 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6943 if (gfc_match_eos () == MATCH_YES
)
6945 if (gfc_match_char (',') != MATCH_YES
)
6952 gfc_error ("Syntax error in VOLATILE statement at %C");
6958 gfc_match_asynchronous (void)
6963 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASYNCHRONOUS statement at %C")
6967 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6972 if (gfc_match_eos () == MATCH_YES
)
6977 /* ASYNCHRONOUS is special because it can be added to host-associated
6979 m
= gfc_match_symbol (&sym
, 1);
6983 if (gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6996 if (gfc_match_eos () == MATCH_YES
)
6998 if (gfc_match_char (',') != MATCH_YES
)
7005 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7010 /* Match a module procedure statement. Note that we have to modify
7011 symbols in the parent's namespace because the current one was there
7012 to receive symbols that are in an interface's formal argument list. */
7015 gfc_match_modproc (void)
7017 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7021 gfc_namespace
*module_ns
;
7022 gfc_interface
*old_interface_head
, *interface
;
7024 if (gfc_state_stack
->state
!= COMP_INTERFACE
7025 || gfc_state_stack
->previous
== NULL
7026 || current_interface
.type
== INTERFACE_NAMELESS
7027 || current_interface
.type
== INTERFACE_ABSTRACT
)
7029 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7034 module_ns
= gfc_current_ns
->parent
;
7035 for (; module_ns
; module_ns
= module_ns
->parent
)
7036 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7037 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7038 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7039 && !module_ns
->proc_name
->attr
.contained
))
7042 if (module_ns
== NULL
)
7045 /* Store the current state of the interface. We will need it if we
7046 end up with a syntax error and need to recover. */
7047 old_interface_head
= gfc_current_interface_head ();
7049 /* Check if the F2008 optional double colon appears. */
7050 gfc_gobble_whitespace ();
7051 old_locus
= gfc_current_locus
;
7052 if (gfc_match ("::") == MATCH_YES
)
7054 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: double colon in "
7055 "MODULE PROCEDURE statement at %L", &old_locus
)
7060 gfc_current_locus
= old_locus
;
7065 old_locus
= gfc_current_locus
;
7067 m
= gfc_match_name (name
);
7073 /* Check for syntax error before starting to add symbols to the
7074 current namespace. */
7075 if (gfc_match_eos () == MATCH_YES
)
7078 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7081 /* Now we're sure the syntax is valid, we process this item
7083 if (gfc_get_symbol (name
, module_ns
, &sym
))
7086 if (sym
->attr
.intrinsic
)
7088 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7089 "PROCEDURE", &old_locus
);
7093 if (sym
->attr
.proc
!= PROC_MODULE
7094 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7095 sym
->name
, NULL
) == FAILURE
)
7098 if (gfc_add_interface (sym
) == FAILURE
)
7101 sym
->attr
.mod_proc
= 1;
7102 sym
->declared_at
= old_locus
;
7111 /* Restore the previous state of the interface. */
7112 interface
= gfc_current_interface_head ();
7113 gfc_set_current_interface_head (old_interface_head
);
7115 /* Free the new interfaces. */
7116 while (interface
!= old_interface_head
)
7118 gfc_interface
*i
= interface
->next
;
7123 /* And issue a syntax error. */
7124 gfc_syntax_error (ST_MODULE_PROC
);
7129 /* Check a derived type that is being extended. */
7131 check_extended_derived_type (char *name
)
7133 gfc_symbol
*extended
;
7135 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7137 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7143 gfc_error ("No such symbol in TYPE definition at %C");
7147 if (extended
->attr
.flavor
!= FL_DERIVED
)
7149 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7150 "derived type", name
);
7154 if (extended
->attr
.is_bind_c
)
7156 gfc_error ("'%s' cannot be extended at %C because it "
7157 "is BIND(C)", extended
->name
);
7161 if (extended
->attr
.sequence
)
7163 gfc_error ("'%s' cannot be extended at %C because it "
7164 "is a SEQUENCE type", extended
->name
);
7172 /* Match the optional attribute specifiers for a type declaration.
7173 Return MATCH_ERROR if an error is encountered in one of the handled
7174 attributes (public, private, bind(c)), MATCH_NO if what's found is
7175 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7176 checking on attribute conflicts needs to be done. */
7179 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7181 /* See if the derived type is marked as private. */
7182 if (gfc_match (" , private") == MATCH_YES
)
7184 if (gfc_current_state () != COMP_MODULE
)
7186 gfc_error ("Derived type at %C can only be PRIVATE in the "
7187 "specification part of a module");
7191 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
7194 else if (gfc_match (" , public") == MATCH_YES
)
7196 if (gfc_current_state () != COMP_MODULE
)
7198 gfc_error ("Derived type at %C can only be PUBLIC in the "
7199 "specification part of a module");
7203 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
7206 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7208 /* If the type is defined to be bind(c) it then needs to make
7209 sure that all fields are interoperable. This will
7210 need to be a semantic check on the finished derived type.
7211 See 15.2.3 (lines 9-12) of F2003 draft. */
7212 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
7215 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7217 else if (gfc_match (" , abstract") == MATCH_YES
)
7219 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
7223 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
7226 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
7228 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
7234 /* If we get here, something matched. */
7239 /* Match the beginning of a derived type declaration. If a type name
7240 was the result of a function, then it is possible to have a symbol
7241 already to be known as a derived type yet have no components. */
7244 gfc_match_derived_decl (void)
7246 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7247 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7248 symbol_attribute attr
;
7250 gfc_symbol
*extended
;
7252 match is_type_attr_spec
= MATCH_NO
;
7253 bool seen_attr
= false;
7255 if (gfc_current_state () == COMP_DERIVED
)
7260 gfc_clear_attr (&attr
);
7265 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7266 if (is_type_attr_spec
== MATCH_ERROR
)
7268 if (is_type_attr_spec
== MATCH_YES
)
7270 } while (is_type_attr_spec
== MATCH_YES
);
7272 /* Deal with derived type extensions. The extension attribute has
7273 been added to 'attr' but now the parent type must be found and
7276 extended
= check_extended_derived_type (parent
);
7278 if (parent
[0] && !extended
)
7281 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7283 gfc_error ("Expected :: in TYPE definition at %C");
7287 m
= gfc_match (" %n%t", name
);
7291 /* Make sure the name is not the name of an intrinsic type. */
7292 if (gfc_is_intrinsic_typename (name
))
7294 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7299 if (gfc_get_symbol (name
, NULL
, &sym
))
7302 if (sym
->ts
.type
!= BT_UNKNOWN
)
7304 gfc_error ("Derived type name '%s' at %C already has a basic type "
7305 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
7309 /* The symbol may already have the derived attribute without the
7310 components. The ways this can happen is via a function
7311 definition, an INTRINSIC statement or a subtype in another
7312 derived type that is a pointer. The first part of the AND clause
7313 is true if the symbol is not the return value of a function. */
7314 if (sym
->attr
.flavor
!= FL_DERIVED
7315 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
7318 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
7320 gfc_error ("Derived type definition of '%s' at %C has already been "
7321 "defined", sym
->name
);
7325 if (attr
.access
!= ACCESS_UNKNOWN
7326 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
7329 /* See if the derived type was labeled as bind(c). */
7330 if (attr
.is_bind_c
!= 0)
7331 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7333 /* Construct the f2k_derived namespace if it is not yet there. */
7334 if (!sym
->f2k_derived
)
7335 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7337 if (extended
&& !sym
->components
)
7342 /* Add the extended derived type as the first component. */
7343 gfc_add_component (sym
, parent
, &p
);
7345 gfc_set_sym_referenced (extended
);
7347 p
->ts
.type
= BT_DERIVED
;
7348 p
->ts
.u
.derived
= extended
;
7349 p
->initializer
= gfc_default_initializer (&p
->ts
);
7351 /* Set extension level. */
7352 if (extended
->attr
.extension
== 255)
7354 /* Since the extension field is 8 bit wide, we can only have
7355 up to 255 extension levels. */
7356 gfc_error ("Maximum extension level reached with type '%s' at %L",
7357 extended
->name
, &extended
->declared_at
);
7360 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7362 /* Provide the links between the extended type and its extension. */
7363 if (!extended
->f2k_derived
)
7364 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7365 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7369 if (!sym
->hash_value
)
7370 /* Set the hash for the compound name for this type. */
7371 sym
->hash_value
= gfc_hash_value (sym
);
7373 /* Take over the ABSTRACT attribute. */
7374 sym
->attr
.abstract
= attr
.abstract
;
7376 gfc_new_block
= sym
;
7382 /* Cray Pointees can be declared as:
7383 pointer (ipt, a (n,m,...,*)) */
7386 gfc_mod_pointee_as (gfc_array_spec
*as
)
7388 as
->cray_pointee
= true; /* This will be useful to know later. */
7389 if (as
->type
== AS_ASSUMED_SIZE
)
7390 as
->cp_was_assumed
= true;
7391 else if (as
->type
== AS_ASSUMED_SHAPE
)
7393 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7400 /* Match the enum definition statement, here we are trying to match
7401 the first line of enum definition statement.
7402 Returns MATCH_YES if match is found. */
7405 gfc_match_enum (void)
7409 m
= gfc_match_eos ();
7413 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
7421 /* Returns an initializer whose value is one higher than the value of the
7422 LAST_INITIALIZER argument. If the argument is NULL, the
7423 initializers value will be set to zero. The initializer's kind
7424 will be set to gfc_c_int_kind.
7426 If -fshort-enums is given, the appropriate kind will be selected
7427 later after all enumerators have been parsed. A warning is issued
7428 here if an initializer exceeds gfc_c_int_kind. */
7431 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7434 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7436 mpz_init (result
->value
.integer
);
7438 if (last_initializer
!= NULL
)
7440 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7441 result
->where
= last_initializer
->where
;
7443 if (gfc_check_integer_range (result
->value
.integer
,
7444 gfc_c_int_kind
) != ARITH_OK
)
7446 gfc_error ("Enumerator exceeds the C integer type at %C");
7452 /* Control comes here, if it's the very first enumerator and no
7453 initializer has been given. It will be initialized to zero. */
7454 mpz_set_si (result
->value
.integer
, 0);
7461 /* Match a variable name with an optional initializer. When this
7462 subroutine is called, a variable is expected to be parsed next.
7463 Depending on what is happening at the moment, updates either the
7464 symbol table or the current interface. */
7467 enumerator_decl (void)
7469 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7470 gfc_expr
*initializer
;
7471 gfc_array_spec
*as
= NULL
;
7479 old_locus
= gfc_current_locus
;
7481 /* When we get here, we've just matched a list of attributes and
7482 maybe a type and a double colon. The next thing we expect to see
7483 is the name of the symbol. */
7484 m
= gfc_match_name (name
);
7488 var_locus
= gfc_current_locus
;
7490 /* OK, we've successfully matched the declaration. Now put the
7491 symbol in the current namespace. If we fail to create the symbol,
7493 if (build_sym (name
, NULL
, false, &as
, &var_locus
) == FAILURE
)
7499 /* The double colon must be present in order to have initializers.
7500 Otherwise the statement is ambiguous with an assignment statement. */
7503 if (gfc_match_char ('=') == MATCH_YES
)
7505 m
= gfc_match_init_expr (&initializer
);
7508 gfc_error ("Expected an initialization expression at %C");
7517 /* If we do not have an initializer, the initialization value of the
7518 previous enumerator (stored in last_initializer) is incremented
7519 by 1 and is used to initialize the current enumerator. */
7520 if (initializer
== NULL
)
7521 initializer
= enum_initializer (last_initializer
, old_locus
);
7523 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7525 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7531 /* Store this current initializer, for the next enumerator variable
7532 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7533 use last_initializer below. */
7534 last_initializer
= initializer
;
7535 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7537 /* Maintain enumerator history. */
7538 gfc_find_symbol (name
, NULL
, 0, &sym
);
7539 create_enum_history (sym
, last_initializer
);
7541 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
7544 /* Free stuff up and return. */
7545 gfc_free_expr (initializer
);
7551 /* Match the enumerator definition statement. */
7554 gfc_match_enumerator_def (void)
7559 gfc_clear_ts (¤t_ts
);
7561 m
= gfc_match (" enumerator");
7565 m
= gfc_match (" :: ");
7566 if (m
== MATCH_ERROR
)
7569 colon_seen
= (m
== MATCH_YES
);
7571 if (gfc_current_state () != COMP_ENUM
)
7573 gfc_error ("ENUM definition statement expected before %C");
7574 gfc_free_enum_history ();
7578 (¤t_ts
)->type
= BT_INTEGER
;
7579 (¤t_ts
)->kind
= gfc_c_int_kind
;
7581 gfc_clear_attr (¤t_attr
);
7582 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7591 m
= enumerator_decl ();
7592 if (m
== MATCH_ERROR
)
7594 gfc_free_enum_history ();
7600 if (gfc_match_eos () == MATCH_YES
)
7602 if (gfc_match_char (',') != MATCH_YES
)
7606 if (gfc_current_state () == COMP_ENUM
)
7608 gfc_free_enum_history ();
7609 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7614 gfc_free_array_spec (current_as
);
7621 /* Match binding attributes. */
7624 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7626 bool found_passing
= false;
7627 bool seen_ptr
= false;
7628 match m
= MATCH_YES
;
7630 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7631 this case the defaults are in there. */
7632 ba
->access
= ACCESS_UNKNOWN
;
7633 ba
->pass_arg
= NULL
;
7634 ba
->pass_arg_num
= 0;
7636 ba
->non_overridable
= 0;
7640 /* If we find a comma, we believe there are binding attributes. */
7641 m
= gfc_match_char (',');
7647 /* Access specifier. */
7649 m
= gfc_match (" public");
7650 if (m
== MATCH_ERROR
)
7654 if (ba
->access
!= ACCESS_UNKNOWN
)
7656 gfc_error ("Duplicate access-specifier at %C");
7660 ba
->access
= ACCESS_PUBLIC
;
7664 m
= gfc_match (" private");
7665 if (m
== MATCH_ERROR
)
7669 if (ba
->access
!= ACCESS_UNKNOWN
)
7671 gfc_error ("Duplicate access-specifier at %C");
7675 ba
->access
= ACCESS_PRIVATE
;
7679 /* If inside GENERIC, the following is not allowed. */
7684 m
= gfc_match (" nopass");
7685 if (m
== MATCH_ERROR
)
7691 gfc_error ("Binding attributes already specify passing,"
7692 " illegal NOPASS at %C");
7696 found_passing
= true;
7701 /* PASS possibly including argument. */
7702 m
= gfc_match (" pass");
7703 if (m
== MATCH_ERROR
)
7707 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
7711 gfc_error ("Binding attributes already specify passing,"
7712 " illegal PASS at %C");
7716 m
= gfc_match (" ( %n )", arg
);
7717 if (m
== MATCH_ERROR
)
7720 ba
->pass_arg
= gfc_get_string (arg
);
7721 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
7723 found_passing
= true;
7731 m
= gfc_match (" pointer");
7732 if (m
== MATCH_ERROR
)
7738 gfc_error ("Duplicate POINTER attribute at %C");
7748 /* NON_OVERRIDABLE flag. */
7749 m
= gfc_match (" non_overridable");
7750 if (m
== MATCH_ERROR
)
7754 if (ba
->non_overridable
)
7756 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7760 ba
->non_overridable
= 1;
7764 /* DEFERRED flag. */
7765 m
= gfc_match (" deferred");
7766 if (m
== MATCH_ERROR
)
7772 gfc_error ("Duplicate DEFERRED at %C");
7783 /* Nothing matching found. */
7785 gfc_error ("Expected access-specifier at %C");
7787 gfc_error ("Expected binding attribute at %C");
7790 while (gfc_match_char (',') == MATCH_YES
);
7792 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7793 if (ba
->non_overridable
&& ba
->deferred
)
7795 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7802 if (ba
->access
== ACCESS_UNKNOWN
)
7803 ba
->access
= gfc_typebound_default_access
;
7805 if (ppc
&& !seen_ptr
)
7807 gfc_error ("POINTER attribute is required for procedure pointer component"
7819 /* Match a PROCEDURE specific binding inside a derived type. */
7822 match_procedure_in_type (void)
7824 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7825 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
7826 char* target
= NULL
, *ifc
= NULL
;
7827 gfc_typebound_proc tb
;
7836 /* Check current state. */
7837 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
7838 block
= gfc_state_stack
->previous
->sym
;
7841 /* Try to match PROCEDURE(interface). */
7842 if (gfc_match (" (") == MATCH_YES
)
7844 m
= gfc_match_name (target_buf
);
7845 if (m
== MATCH_ERROR
)
7849 gfc_error ("Interface-name expected after '(' at %C");
7853 if (gfc_match (" )") != MATCH_YES
)
7855 gfc_error ("')' expected at %C");
7862 /* Construct the data structure. */
7863 memset (&tb
, 0, sizeof (tb
));
7864 tb
.where
= gfc_current_locus
;
7866 /* Match binding attributes. */
7867 m
= match_binding_attributes (&tb
, false, false);
7868 if (m
== MATCH_ERROR
)
7870 seen_attrs
= (m
== MATCH_YES
);
7872 /* Check that attribute DEFERRED is given if an interface is specified. */
7873 if (tb
.deferred
&& !ifc
)
7875 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7878 if (ifc
&& !tb
.deferred
)
7880 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7884 /* Match the colons. */
7885 m
= gfc_match (" ::");
7886 if (m
== MATCH_ERROR
)
7888 seen_colons
= (m
== MATCH_YES
);
7889 if (seen_attrs
&& !seen_colons
)
7891 gfc_error ("Expected '::' after binding-attributes at %C");
7895 /* Match the binding names. */
7898 m
= gfc_match_name (name
);
7899 if (m
== MATCH_ERROR
)
7903 gfc_error ("Expected binding name at %C");
7907 if (num
>1 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: PROCEDURE list"
7908 " at %C") == FAILURE
)
7911 /* Try to match the '=> target', if it's there. */
7913 m
= gfc_match (" =>");
7914 if (m
== MATCH_ERROR
)
7920 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7926 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7931 m
= gfc_match_name (target_buf
);
7932 if (m
== MATCH_ERROR
)
7936 gfc_error ("Expected binding target after '=>' at %C");
7939 target
= target_buf
;
7942 /* If no target was found, it has the same name as the binding. */
7946 /* Get the namespace to insert the symbols into. */
7947 ns
= block
->f2k_derived
;
7950 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7951 if (tb
.deferred
&& !block
->attr
.abstract
)
7953 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7954 "is not ABSTRACT", block
->name
);
7958 /* See if we already have a binding with this name in the symtree which
7959 would be an error. If a GENERIC already targetted this binding, it may
7960 be already there but then typebound is still NULL. */
7961 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7962 if (stree
&& stree
->n
.tb
)
7964 gfc_error ("There is already a procedure with binding name '%s' for "
7965 "the derived type '%s' at %C", name
, block
->name
);
7969 /* Insert it and set attributes. */
7973 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7976 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
7978 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
7981 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
7983 if (gfc_match_eos () == MATCH_YES
)
7985 if (gfc_match_char (',') != MATCH_YES
)
7990 gfc_error ("Syntax error in PROCEDURE statement at %C");
7995 /* Match a GENERIC procedure binding inside a derived type. */
7998 gfc_match_generic (void)
8000 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8001 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8003 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8004 gfc_typebound_proc
* tb
;
8006 interface_type op_type
;
8007 gfc_intrinsic_op op
;
8010 /* Check current state. */
8011 if (gfc_current_state () == COMP_DERIVED
)
8013 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8016 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8018 block
= gfc_state_stack
->previous
->sym
;
8019 ns
= block
->f2k_derived
;
8020 gcc_assert (block
&& ns
);
8022 memset (&tbattr
, 0, sizeof (tbattr
));
8023 tbattr
.where
= gfc_current_locus
;
8025 /* See if we get an access-specifier. */
8026 m
= match_binding_attributes (&tbattr
, true, false);
8027 if (m
== MATCH_ERROR
)
8030 /* Now the colons, those are required. */
8031 if (gfc_match (" ::") != MATCH_YES
)
8033 gfc_error ("Expected '::' at %C");
8037 /* Match the binding name; depending on type (operator / generic) format
8038 it for future error messages into bind_name. */
8040 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8041 if (m
== MATCH_ERROR
)
8045 gfc_error ("Expected generic name or operator descriptor at %C");
8051 case INTERFACE_GENERIC
:
8052 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8055 case INTERFACE_USER_OP
:
8056 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8059 case INTERFACE_INTRINSIC_OP
:
8060 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8061 gfc_op2string (op
));
8068 /* Match the required =>. */
8069 if (gfc_match (" =>") != MATCH_YES
)
8071 gfc_error ("Expected '=>' at %C");
8075 /* Try to find existing GENERIC binding with this name / for this operator;
8076 if there is something, check that it is another GENERIC and then extend
8077 it rather than building a new node. Otherwise, create it and put it
8078 at the right position. */
8082 case INTERFACE_USER_OP
:
8083 case INTERFACE_GENERIC
:
8085 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8088 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8100 case INTERFACE_INTRINSIC_OP
:
8110 if (!tb
->is_generic
)
8112 gcc_assert (op_type
== INTERFACE_GENERIC
);
8113 gfc_error ("There's already a non-generic procedure with binding name"
8114 " '%s' for the derived type '%s' at %C",
8115 bind_name
, block
->name
);
8119 if (tb
->access
!= tbattr
.access
)
8121 gfc_error ("Binding at %C must have the same access as already"
8122 " defined binding '%s'", bind_name
);
8128 tb
= gfc_get_typebound_proc (NULL
);
8129 tb
->where
= gfc_current_locus
;
8130 tb
->access
= tbattr
.access
;
8132 tb
->u
.generic
= NULL
;
8136 case INTERFACE_GENERIC
:
8137 case INTERFACE_USER_OP
:
8139 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8142 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8150 case INTERFACE_INTRINSIC_OP
:
8159 /* Now, match all following names as specific targets. */
8162 gfc_symtree
* target_st
;
8163 gfc_tbp_generic
* target
;
8165 m
= gfc_match_name (name
);
8166 if (m
== MATCH_ERROR
)
8170 gfc_error ("Expected specific binding name at %C");
8174 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8176 /* See if this is a duplicate specification. */
8177 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8178 if (target_st
== target
->specific_st
)
8180 gfc_error ("'%s' already defined as specific binding for the"
8181 " generic '%s' at %C", name
, bind_name
);
8185 target
= gfc_get_tbp_generic ();
8186 target
->specific_st
= target_st
;
8187 target
->specific
= NULL
;
8188 target
->next
= tb
->u
.generic
;
8189 tb
->u
.generic
= target
;
8191 while (gfc_match (" ,") == MATCH_YES
);
8193 /* Here should be the end. */
8194 if (gfc_match_eos () != MATCH_YES
)
8196 gfc_error ("Junk after GENERIC binding at %C");
8207 /* Match a FINAL declaration inside a derived type. */
8210 gfc_match_final_decl (void)
8212 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8215 gfc_namespace
* module_ns
;
8219 if (gfc_current_form
== FORM_FREE
)
8221 char c
= gfc_peek_ascii_char ();
8222 if (!gfc_is_whitespace (c
) && c
!= ':')
8226 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8228 if (gfc_current_form
== FORM_FIXED
)
8231 gfc_error ("FINAL declaration at %C must be inside a derived type "
8232 "CONTAINS section");
8236 block
= gfc_state_stack
->previous
->sym
;
8239 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8240 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8242 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8243 " specification part of a MODULE");
8247 module_ns
= gfc_current_ns
;
8248 gcc_assert (module_ns
);
8249 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8251 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8252 if (gfc_match (" ::") == MATCH_ERROR
)
8255 /* Match the sequence of procedure names. */
8262 if (first
&& gfc_match_eos () == MATCH_YES
)
8264 gfc_error ("Empty FINAL at %C");
8268 m
= gfc_match_name (name
);
8271 gfc_error ("Expected module procedure name at %C");
8274 else if (m
!= MATCH_YES
)
8277 if (gfc_match_eos () == MATCH_YES
)
8279 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8281 gfc_error ("Expected ',' at %C");
8285 if (gfc_get_symbol (name
, module_ns
, &sym
))
8287 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8291 /* Mark the symbol as module procedure. */
8292 if (sym
->attr
.proc
!= PROC_MODULE
8293 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
8294 sym
->name
, NULL
) == FAILURE
)
8297 /* Check if we already have this symbol in the list, this is an error. */
8298 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8299 if (f
->proc_sym
== sym
)
8301 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8306 /* Add this symbol to the list of finalizers. */
8307 gcc_assert (block
->f2k_derived
);
8309 f
= XCNEW (gfc_finalizer
);
8311 f
->proc_tree
= NULL
;
8312 f
->where
= gfc_current_locus
;
8313 f
->next
= block
->f2k_derived
->finalizers
;
8314 block
->f2k_derived
->finalizers
= f
;
8324 const ext_attr_t ext_attr_list
[] = {
8325 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8326 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8327 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8328 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8329 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8330 { NULL
, EXT_ATTR_LAST
, NULL
}
8333 /* Match a !GCC$ ATTRIBUTES statement of the form:
8334 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8335 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8337 TODO: We should support all GCC attributes using the same syntax for
8338 the attribute list, i.e. the list in C
8339 __attributes(( attribute-list ))
8341 !GCC$ ATTRIBUTES attribute-list ::
8342 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8345 As there is absolutely no risk of confusion, we should never return
8348 gfc_match_gcc_attributes (void)
8350 symbol_attribute attr
;
8351 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8356 gfc_clear_attr (&attr
);
8361 if (gfc_match_name (name
) != MATCH_YES
)
8364 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8365 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8368 if (id
== EXT_ATTR_LAST
)
8370 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8374 if (gfc_add_ext_attribute (&attr
, (ext_attr_id_t
) id
, &gfc_current_locus
)
8378 gfc_gobble_whitespace ();
8379 ch
= gfc_next_ascii_char ();
8382 /* This is the successful exit condition for the loop. */
8383 if (gfc_next_ascii_char () == ':')
8393 if (gfc_match_eos () == MATCH_YES
)
8398 m
= gfc_match_name (name
);
8402 if (find_special (name
, &sym
, true))
8405 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8407 if (gfc_match_eos () == MATCH_YES
)
8410 if (gfc_match_char (',') != MATCH_YES
)
8417 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");