1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static gfc_try
set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector
;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts
;
53 static symbol_attribute current_attr
;
54 static gfc_array_spec
*current_as
;
55 static int colon_seen
;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label
;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line
;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals
= 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr
*last_initializer
;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
77 gfc_expr
*initializer
;
78 struct enumerator_history
*next
;
82 /* Header of enum history chain. */
84 static enumerator_history
*enum_history
= NULL
;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history
*max_enum
= NULL
;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol
*gfc_new_block
;
94 bool gfc_matching_function
;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data
= false;
102 gfc_in_match_data (void)
104 return in_match_data
;
108 set_in_match_data (bool set_value
)
110 in_match_data
= set_value
;
113 /* Free a gfc_data_variable structure and everything beneath it. */
116 free_variable (gfc_data_variable
*p
)
118 gfc_data_variable
*q
;
123 gfc_free_expr (p
->expr
);
124 gfc_free_iterator (&p
->iter
, 0);
125 free_variable (p
->list
);
131 /* Free a gfc_data_value structure and everything beneath it. */
134 free_value (gfc_data_value
*p
)
141 mpz_clear (p
->repeat
);
142 gfc_free_expr (p
->expr
);
148 /* Free a list of gfc_data structures. */
151 gfc_free_data (gfc_data
*p
)
158 free_variable (p
->var
);
159 free_value (p
->value
);
165 /* Free all data in a namespace. */
168 gfc_free_data_all (gfc_namespace
*ns
)
181 static match
var_element (gfc_data_variable
*);
183 /* Match a list of variables terminated by an iterator and a right
187 var_list (gfc_data_variable
*parent
)
189 gfc_data_variable
*tail
, var
;
192 m
= var_element (&var
);
193 if (m
== MATCH_ERROR
)
198 tail
= gfc_get_data_variable ();
205 if (gfc_match_char (',') != MATCH_YES
)
208 m
= gfc_match_iterator (&parent
->iter
, 1);
211 if (m
== MATCH_ERROR
)
214 m
= var_element (&var
);
215 if (m
== MATCH_ERROR
)
220 tail
->next
= gfc_get_data_variable ();
226 if (gfc_match_char (')') != MATCH_YES
)
231 gfc_syntax_error (ST_DATA
);
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
240 var_element (gfc_data_variable
*new_var
)
245 memset (new_var
, 0, sizeof (gfc_data_variable
));
247 if (gfc_match_char ('(') == MATCH_YES
)
248 return var_list (new_var
);
250 m
= gfc_match_variable (&new_var
->expr
, 0);
254 sym
= new_var
->expr
->symtree
->n
.sym
;
256 /* Symbol should already have an associated type. */
257 if (gfc_check_symbol_typed (sym
, gfc_current_ns
,
258 false, gfc_current_locus
) == FAILURE
)
261 if (!sym
->attr
.function
&& gfc_current_ns
->parent
262 && gfc_current_ns
->parent
== sym
->ns
)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym
->name
);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym
->attr
.in_common
271 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym
->name
) == FAILURE
)
276 if (gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
) == FAILURE
)
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data
*d
)
288 gfc_data_variable var
, *tail
, *new_var
;
295 m
= var_element (&var
);
298 if (m
== MATCH_ERROR
)
301 new_var
= gfc_get_data_variable ();
307 tail
->next
= new_var
;
311 if (gfc_match_char ('/') == MATCH_YES
)
313 if (gfc_match_char (',') != MATCH_YES
)
320 gfc_syntax_error (ST_DATA
);
321 gfc_free_data_all (gfc_current_ns
);
327 match_data_constant (gfc_expr
**result
)
329 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
330 gfc_symbol
*sym
, *dt_sym
= NULL
;
335 m
= gfc_match_literal_constant (&expr
, 1);
342 if (m
== MATCH_ERROR
)
345 m
= gfc_match_null (result
);
349 old_loc
= gfc_current_locus
;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m
= gfc_match_rvalue (result
);
354 if (m
== MATCH_ERROR
)
357 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
359 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
364 gfc_current_locus
= old_loc
;
366 m
= gfc_match_name (name
);
370 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
373 if (sym
&& sym
->attr
.generic
)
374 dt_sym
= gfc_find_dt_in_generic (sym
);
377 || (sym
->attr
.flavor
!= FL_PARAMETER
378 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
380 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
384 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
385 return gfc_match_structure_constructor (dt_sym
, result
);
387 /* Check to see if the value is an initialization array expression. */
388 if (sym
->value
->expr_type
== EXPR_ARRAY
)
390 gfc_current_locus
= old_loc
;
392 m
= gfc_match_init_expr (result
);
393 if (m
== MATCH_ERROR
)
398 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
401 if ((*result
)->expr_type
== EXPR_CONSTANT
)
405 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
411 *result
= gfc_copy_expr (sym
->value
);
416 /* Match a list of values in a DATA statement. The leading '/' has
417 already been seen at this point. */
420 top_val_list (gfc_data
*data
)
422 gfc_data_value
*new_val
, *tail
;
430 m
= match_data_constant (&expr
);
433 if (m
== MATCH_ERROR
)
436 new_val
= gfc_get_data_value ();
437 mpz_init (new_val
->repeat
);
440 data
->value
= new_val
;
442 tail
->next
= new_val
;
446 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
449 mpz_set_ui (tail
->repeat
, 1);
453 if (expr
->ts
.type
== BT_INTEGER
)
454 mpz_set (tail
->repeat
, expr
->value
.integer
);
455 gfc_free_expr (expr
);
457 m
= match_data_constant (&tail
->expr
);
460 if (m
== MATCH_ERROR
)
464 if (gfc_match_char ('/') == MATCH_YES
)
466 if (gfc_match_char (',') == MATCH_NO
)
473 gfc_syntax_error (ST_DATA
);
474 gfc_free_data_all (gfc_current_ns
);
479 /* Matches an old style initialization. */
482 match_old_style_init (const char *name
)
489 /* Set up data structure to hold initializers. */
490 gfc_find_sym_tree (name
, NULL
, 0, &st
);
493 newdata
= gfc_get_data ();
494 newdata
->var
= gfc_get_data_variable ();
495 newdata
->var
->expr
= gfc_get_variable_expr (st
);
496 newdata
->where
= gfc_current_locus
;
498 /* Match initial value list. This also eats the terminal '/'. */
499 m
= top_val_list (newdata
);
508 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
513 if (gfc_implicit_pure (NULL
))
514 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
516 /* Mark the variable as having appeared in a data statement. */
517 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
523 /* Chain in namespace list of DATA initializers. */
524 newdata
->next
= gfc_current_ns
->data
;
525 gfc_current_ns
->data
= newdata
;
531 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
532 we are matching a DATA statement and are therefore issuing an error
533 if we encounter something unexpected, if not, we're trying to match
534 an old-style initialization expression of the form INTEGER I /2/. */
537 gfc_match_data (void)
542 set_in_match_data (true);
546 new_data
= gfc_get_data ();
547 new_data
->where
= gfc_current_locus
;
549 m
= top_var_list (new_data
);
553 m
= top_val_list (new_data
);
557 new_data
->next
= gfc_current_ns
->data
;
558 gfc_current_ns
->data
= new_data
;
560 if (gfc_match_eos () == MATCH_YES
)
563 gfc_match_char (','); /* Optional comma */
566 set_in_match_data (false);
570 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
574 if (gfc_implicit_pure (NULL
))
575 gfc_current_ns
->proc_name
->attr
.implicit_pure
= 0;
580 set_in_match_data (false);
581 gfc_free_data (new_data
);
586 /************************ Declaration statements *********************/
589 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
592 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
596 if (to
->rank
== 0 && from
->rank
> 0)
598 to
->rank
= from
->rank
;
599 to
->type
= from
->type
;
600 to
->cray_pointee
= from
->cray_pointee
;
601 to
->cp_was_assumed
= from
->cp_was_assumed
;
603 for (i
= 0; i
< to
->corank
; i
++)
605 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
606 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
608 for (i
= 0; i
< from
->rank
; i
++)
612 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
613 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
617 to
->lower
[i
] = from
->lower
[i
];
618 to
->upper
[i
] = from
->upper
[i
];
622 else if (to
->corank
== 0 && from
->corank
> 0)
624 to
->corank
= from
->corank
;
625 to
->cotype
= from
->cotype
;
627 for (i
= 0; i
< from
->corank
; i
++)
631 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
632 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
636 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
637 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
644 /* Match an intent specification. Since this can only happen after an
645 INTENT word, a legal intent-spec must follow. */
648 match_intent_spec (void)
651 if (gfc_match (" ( in out )") == MATCH_YES
)
653 if (gfc_match (" ( in )") == MATCH_YES
)
655 if (gfc_match (" ( out )") == MATCH_YES
)
658 gfc_error ("Bad INTENT specification at %C");
659 return INTENT_UNKNOWN
;
663 /* Matches a character length specification, which is either a
664 specification expression, '*', or ':'. */
667 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
674 if (gfc_match_char ('*') == MATCH_YES
)
677 if (gfc_match_char (':') == MATCH_YES
)
679 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: deferred type "
680 "parameter at %C") == FAILURE
)
688 m
= gfc_match_expr (expr
);
691 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
694 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
696 if ((*expr
)->value
.function
.actual
697 && (*expr
)->value
.function
.actual
->expr
->symtree
)
700 e
= (*expr
)->value
.function
.actual
->expr
;
701 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
702 && e
->expr_type
== EXPR_VARIABLE
)
704 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
706 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
707 && e
->symtree
->n
.sym
->ts
.u
.cl
708 && e
->symtree
->n
.sym
->ts
.u
.cl
->length
->ts
.type
== BT_UNKNOWN
)
716 gfc_error ("Conflict in attributes of function argument at %C");
721 /* A character length is a '*' followed by a literal integer or a
722 char_len_param_value in parenthesis. */
725 match_char_length (gfc_expr
**expr
, bool *deferred
)
731 m
= gfc_match_char ('*');
735 m
= gfc_match_small_literal_int (&length
, NULL
);
736 if (m
== MATCH_ERROR
)
741 if (gfc_notify_std (GFC_STD_F95_OBS
, "Obsolescent feature: "
742 "Old-style character length at %C") == FAILURE
)
744 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
748 if (gfc_match_char ('(') == MATCH_NO
)
751 m
= char_len_param_value (expr
, deferred
);
752 if (m
!= MATCH_YES
&& gfc_matching_function
)
758 if (m
== MATCH_ERROR
)
763 if (gfc_match_char (')') == MATCH_NO
)
765 gfc_free_expr (*expr
);
773 gfc_error ("Syntax error in character length specification at %C");
778 /* Special subroutine for finding a symbol. Check if the name is found
779 in the current name space. If not, and we're compiling a function or
780 subroutine and the parent compilation unit is an interface, then check
781 to see if the name we've been given is the name of the interface
782 (located in another namespace). */
785 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
791 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
794 *result
= st
? st
->n
.sym
: NULL
;
798 if (gfc_current_state () != COMP_SUBROUTINE
799 && gfc_current_state () != COMP_FUNCTION
)
802 s
= gfc_state_stack
->previous
;
806 if (s
->state
!= COMP_INTERFACE
)
809 goto end
; /* Nameless interface. */
811 if (strcmp (name
, s
->sym
->name
) == 0)
822 /* Special subroutine for getting a symbol node associated with a
823 procedure name, used in SUBROUTINE and FUNCTION statements. The
824 symbol is created in the parent using with symtree node in the
825 child unit pointing to the symbol. If the current namespace has no
826 parent, then the symbol is just created in the current unit. */
829 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
835 /* Module functions have to be left in their own namespace because
836 they have potentially (almost certainly!) already been referenced.
837 In this sense, they are rather like external functions. This is
838 fixed up in resolve.c(resolve_entries), where the symbol name-
839 space is set to point to the master function, so that the fake
840 result mechanism can work. */
841 if (module_fcn_entry
)
843 /* Present if entry is declared to be a module procedure. */
844 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
847 rc
= gfc_get_symbol (name
, NULL
, result
);
848 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
849 && (*result
)->ts
.type
== BT_UNKNOWN
850 && sym
->attr
.flavor
== FL_UNKNOWN
)
851 /* Pick up the typespec for the entry, if declared in the function
852 body. Note that this symbol is FL_UNKNOWN because it will
853 only have appeared in a type declaration. The local symtree
854 is set to point to the module symbol and a unique symtree
855 to the local version. This latter ensures a correct clearing
858 /* If the ENTRY proceeds its specification, we need to ensure
859 that this does not raise a "has no IMPLICIT type" error. */
860 if (sym
->ts
.type
== BT_UNKNOWN
)
861 sym
->attr
.untyped
= 1;
863 (*result
)->ts
= sym
->ts
;
865 /* Put the symbol in the procedure namespace so that, should
866 the ENTRY precede its specification, the specification
868 (*result
)->ns
= gfc_current_ns
;
870 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
872 st
= gfc_get_unique_symtree (gfc_current_ns
);
877 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
883 gfc_current_ns
->refs
++;
885 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
887 /* Trap another encompassed procedure with the same name. All
888 these conditions are necessary to avoid picking up an entry
889 whose name clashes with that of the encompassing procedure;
890 this is handled using gsymbols to register unique,globally
892 if (sym
->attr
.flavor
!= 0
893 && sym
->attr
.proc
!= 0
894 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
895 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
896 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
897 name
, &sym
->declared_at
);
899 /* Trap a procedure with a name the same as interface in the
900 encompassing scope. */
901 if (sym
->attr
.generic
!= 0
902 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
903 && !sym
->attr
.mod_proc
)
904 gfc_error_now ("Name '%s' at %C is already defined"
905 " as a generic interface at %L",
906 name
, &sym
->declared_at
);
908 /* Trap declarations of attributes in encompassing scope. The
909 signature for this is that ts.kind is set. Legitimate
910 references only set ts.type. */
911 if (sym
->ts
.kind
!= 0
912 && !sym
->attr
.implicit_type
913 && sym
->attr
.proc
== 0
914 && gfc_current_ns
->parent
!= NULL
915 && sym
->attr
.access
== 0
916 && !module_fcn_entry
)
917 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
918 "and must not have attributes declared at %L",
919 name
, &sym
->declared_at
);
922 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
925 /* Module function entries will already have a symtree in
926 the current namespace but will need one at module level. */
927 if (module_fcn_entry
)
929 /* Present if entry is declared to be a module procedure. */
930 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
932 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
935 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
940 /* See if the procedure should be a module procedure. */
942 if (((sym
->ns
->proc_name
!= NULL
943 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
944 && sym
->attr
.proc
!= PROC_MODULE
)
945 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
946 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
947 sym
->name
, NULL
) == FAILURE
)
954 /* Verify that the given symbol representing a parameter is C
955 interoperable, by checking to see if it was marked as such after
956 its declaration. If the given symbol is not interoperable, a
957 warning is reported, thus removing the need to return the status to
958 the calling function. The standard does not require the user use
959 one of the iso_c_binding named constants to declare an
960 interoperable parameter, but we can't be sure if the param is C
961 interop or not if the user doesn't. For example, integer(4) may be
962 legal Fortran, but doesn't have meaning in C. It may interop with
963 a number of the C types, which causes a problem because the
964 compiler can't know which one. This code is almost certainly not
965 portable, and the user will get what they deserve if the C type
966 across platforms isn't always interoperable with integer(4). If
967 the user had used something like integer(c_int) or integer(c_long),
968 the compiler could have automatically handled the varying sizes
972 gfc_verify_c_interop_param (gfc_symbol
*sym
)
974 int is_c_interop
= 0;
975 gfc_try retval
= SUCCESS
;
977 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
978 Don't repeat the checks here. */
979 if (sym
->attr
.implicit_type
)
982 /* For subroutines or functions that are passed to a BIND(C) procedure,
983 they're interoperable if they're BIND(C) and their params are all
985 if (sym
->attr
.flavor
== FL_PROCEDURE
)
987 if (sym
->attr
.is_bind_c
== 0)
989 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
990 "attribute to be C interoperable", sym
->name
,
991 &(sym
->declared_at
));
997 if (sym
->attr
.is_c_interop
== 1)
998 /* We've already checked this procedure; don't check it again. */
1001 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1006 /* See if we've stored a reference to a procedure that owns sym. */
1007 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1009 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1011 is_c_interop
= (gfc_verify_c_interop (&(sym
->ts
)) == SUCCESS
? 1 : 0);
1013 if (is_c_interop
!= 1)
1015 /* Make personalized messages to give better feedback. */
1016 if (sym
->ts
.type
== BT_DERIVED
)
1017 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1018 "BIND(C) procedure '%s' but is not C interoperable "
1019 "because derived type '%s' is not C interoperable",
1020 sym
->name
, &(sym
->declared_at
),
1021 sym
->ns
->proc_name
->name
,
1022 sym
->ts
.u
.derived
->name
);
1023 else if (sym
->ts
.type
== BT_CLASS
)
1024 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1025 "BIND(C) procedure '%s' but is not C interoperable "
1026 "because it is polymorphic",
1027 sym
->name
, &(sym
->declared_at
),
1028 sym
->ns
->proc_name
->name
);
1030 gfc_warning ("Variable '%s' at %L is a parameter to the "
1031 "BIND(C) procedure '%s' but may not be C "
1033 sym
->name
, &(sym
->declared_at
),
1034 sym
->ns
->proc_name
->name
);
1037 /* Character strings are only C interoperable if they have a
1039 if (sym
->ts
.type
== BT_CHARACTER
)
1041 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1042 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1043 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1045 gfc_error ("Character argument '%s' at %L "
1046 "must be length 1 because "
1047 "procedure '%s' is BIND(C)",
1048 sym
->name
, &sym
->declared_at
,
1049 sym
->ns
->proc_name
->name
);
1054 /* We have to make sure that any param to a bind(c) routine does
1055 not have the allocatable, pointer, or optional attributes,
1056 according to J3/04-007, section 5.1. */
1057 if (sym
->attr
.allocatable
== 1)
1059 gfc_error ("Variable '%s' at %L cannot have the "
1060 "ALLOCATABLE attribute because procedure '%s'"
1061 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1062 sym
->ns
->proc_name
->name
);
1066 if (sym
->attr
.pointer
== 1)
1068 gfc_error ("Variable '%s' at %L cannot have the "
1069 "POINTER attribute because procedure '%s'"
1070 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
1071 sym
->ns
->proc_name
->name
);
1075 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1077 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1078 "and the VALUE attribute because procedure '%s' "
1079 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1080 sym
->ns
->proc_name
->name
);
1083 else if (sym
->attr
.optional
== 1
1084 && gfc_notify_std (GFC_STD_F2008_TS
, "TS29113: Variable '%s' "
1085 "at %L with OPTIONAL attribute in "
1086 "procedure '%s' which is BIND(C)",
1087 sym
->name
, &(sym
->declared_at
),
1088 sym
->ns
->proc_name
->name
)
1092 /* Make sure that if it has the dimension attribute, that it is
1093 either assumed size or explicit shape. */
1094 if (sym
->as
!= NULL
)
1096 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
1098 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1099 "argument to the procedure '%s' at %L because "
1100 "the procedure is BIND(C)", sym
->name
,
1101 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1102 &(sym
->ns
->proc_name
->declared_at
));
1106 if (sym
->as
->type
== AS_DEFERRED
)
1108 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1109 "argument to the procedure '%s' at %L because "
1110 "the procedure is BIND(C)", sym
->name
,
1111 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1112 &(sym
->ns
->proc_name
->declared_at
));
1124 /* Function called by variable_decl() that adds a name to the symbol table. */
1127 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1128 gfc_array_spec
**as
, locus
*var_locus
)
1130 symbol_attribute attr
;
1133 if (gfc_get_symbol (name
, NULL
, &sym
))
1136 /* Start updating the symbol table. Add basic type attribute if present. */
1137 if (current_ts
.type
!= BT_UNKNOWN
1138 && (sym
->attr
.implicit_type
== 0
1139 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1140 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1143 if (sym
->ts
.type
== BT_CHARACTER
)
1146 sym
->ts
.deferred
= cl_deferred
;
1149 /* Add dimension attribute if present. */
1150 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1154 /* Add attribute to symbol. The copy is so that we can reset the
1155 dimension attribute. */
1156 attr
= current_attr
;
1158 attr
.codimension
= 0;
1160 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1163 /* Finish any work that may need to be done for the binding label,
1164 if it's a bind(c). The bind(c) attr is found before the symbol
1165 is made, and before the symbol name (for data decls), so the
1166 current_ts is holding the binding label, or nothing if the
1167 name= attr wasn't given. Therefore, test here if we're dealing
1168 with a bind(c) and make sure the binding label is set correctly. */
1169 if (sym
->attr
.is_bind_c
== 1)
1171 if (!sym
->binding_label
)
1173 /* Set the binding label and verify that if a NAME= was specified
1174 then only one identifier was in the entity-decl-list. */
1175 if (set_binding_label (&sym
->binding_label
, sym
->name
,
1176 num_idents_on_line
) == FAILURE
)
1181 /* See if we know we're in a common block, and if it's a bind(c)
1182 common then we need to make sure we're an interoperable type. */
1183 if (sym
->attr
.in_common
== 1)
1185 /* Test the common block object. */
1186 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1187 && sym
->ts
.is_c_interop
!= 1)
1189 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1190 "must be declared with a C interoperable "
1191 "kind since common block '%s' is BIND(C)",
1192 sym
->name
, sym
->common_block
->name
,
1193 sym
->common_block
->name
);
1198 sym
->attr
.implied_index
= 0;
1200 if (sym
->ts
.type
== BT_CLASS
)
1201 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false);
1207 /* Set character constant to the given length. The constant will be padded or
1208 truncated. If we're inside an array constructor without a typespec, we
1209 additionally check that all elements have the same length; check_len -1
1210 means no checking. */
1213 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1218 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1219 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1221 slen
= expr
->value
.character
.length
;
1224 s
= gfc_get_wide_string (len
+ 1);
1225 memcpy (s
, expr
->value
.character
.string
,
1226 MIN (len
, slen
) * sizeof (gfc_char_t
));
1228 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1230 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1231 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1232 "(%d/%d)", &expr
->where
, slen
, len
);
1234 /* Apply the standard by 'hand' otherwise it gets cleared for
1236 if (check_len
!= -1 && slen
!= check_len
1237 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1238 gfc_error_now ("The CHARACTER elements of the array constructor "
1239 "at %L must have the same length (%d/%d)",
1240 &expr
->where
, slen
, check_len
);
1243 free (expr
->value
.character
.string
);
1244 expr
->value
.character
.string
= s
;
1245 expr
->value
.character
.length
= len
;
1250 /* Function to create and update the enumerator history
1251 using the information passed as arguments.
1252 Pointer "max_enum" is also updated, to point to
1253 enum history node containing largest initializer.
1255 SYM points to the symbol node of enumerator.
1256 INIT points to its enumerator value. */
1259 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1261 enumerator_history
*new_enum_history
;
1262 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1264 new_enum_history
= XCNEW (enumerator_history
);
1266 new_enum_history
->sym
= sym
;
1267 new_enum_history
->initializer
= init
;
1268 new_enum_history
->next
= NULL
;
1270 if (enum_history
== NULL
)
1272 enum_history
= new_enum_history
;
1273 max_enum
= enum_history
;
1277 new_enum_history
->next
= enum_history
;
1278 enum_history
= new_enum_history
;
1280 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1281 new_enum_history
->initializer
->value
.integer
) < 0)
1282 max_enum
= new_enum_history
;
1287 /* Function to free enum kind history. */
1290 gfc_free_enum_history (void)
1292 enumerator_history
*current
= enum_history
;
1293 enumerator_history
*next
;
1295 while (current
!= NULL
)
1297 next
= current
->next
;
1302 enum_history
= NULL
;
1306 /* Function called by variable_decl() that adds an initialization
1307 expression to a symbol. */
1310 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1312 symbol_attribute attr
;
1317 if (find_special (name
, &sym
, false))
1322 /* If this symbol is confirming an implicit parameter type,
1323 then an initialization expression is not allowed. */
1324 if (attr
.flavor
== FL_PARAMETER
1325 && sym
->value
!= NULL
1328 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1335 /* An initializer is required for PARAMETER declarations. */
1336 if (attr
.flavor
== FL_PARAMETER
)
1338 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1344 /* If a variable appears in a DATA block, it cannot have an
1348 gfc_error ("Variable '%s' at %C with an initializer already "
1349 "appears in a DATA statement", sym
->name
);
1353 /* Check if the assignment can happen. This has to be put off
1354 until later for derived type variables and procedure pointers. */
1355 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1356 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1357 && !sym
->attr
.proc_pointer
1358 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1361 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1362 && init
->ts
.type
== BT_CHARACTER
)
1364 /* Update symbol character length according initializer. */
1365 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1368 if (sym
->ts
.u
.cl
->length
== NULL
)
1371 /* If there are multiple CHARACTER variables declared on the
1372 same line, we don't want them to share the same length. */
1373 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1375 if (sym
->attr
.flavor
== FL_PARAMETER
)
1377 if (init
->expr_type
== EXPR_CONSTANT
)
1379 clen
= init
->value
.character
.length
;
1380 sym
->ts
.u
.cl
->length
1381 = gfc_get_int_expr (gfc_default_integer_kind
,
1384 else if (init
->expr_type
== EXPR_ARRAY
)
1387 c
= gfc_constructor_first (init
->value
.constructor
);
1388 clen
= c
->expr
->value
.character
.length
;
1389 sym
->ts
.u
.cl
->length
1390 = gfc_get_int_expr (gfc_default_integer_kind
,
1393 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1394 sym
->ts
.u
.cl
->length
=
1395 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1398 /* Update initializer character length according symbol. */
1399 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1401 int len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1403 if (init
->expr_type
== EXPR_CONSTANT
)
1404 gfc_set_constant_character_len (len
, init
, -1);
1405 else if (init
->expr_type
== EXPR_ARRAY
)
1409 /* Build a new charlen to prevent simplification from
1410 deleting the length before it is resolved. */
1411 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1412 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1414 for (c
= gfc_constructor_first (init
->value
.constructor
);
1415 c
; c
= gfc_constructor_next (c
))
1416 gfc_set_constant_character_len (len
, c
->expr
, -1);
1421 /* If sym is implied-shape, set its upper bounds from init. */
1422 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1423 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1427 if (init
->rank
== 0)
1429 gfc_error ("Can't initialize implied-shape array at %L"
1430 " with scalar", &sym
->declared_at
);
1433 gcc_assert (sym
->as
->rank
== init
->rank
);
1435 /* Shape should be present, we get an initialization expression. */
1436 gcc_assert (init
->shape
);
1438 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1444 lower
= sym
->as
->lower
[dim
];
1445 if (lower
->expr_type
!= EXPR_CONSTANT
)
1447 gfc_error ("Non-constant lower bound in implied-shape"
1448 " declaration at %L", &lower
->where
);
1452 /* All dimensions must be without upper bound. */
1453 gcc_assert (!sym
->as
->upper
[dim
]);
1456 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1457 mpz_add (e
->value
.integer
,
1458 lower
->value
.integer
, init
->shape
[dim
]);
1459 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1460 sym
->as
->upper
[dim
] = e
;
1463 sym
->as
->type
= AS_EXPLICIT
;
1466 /* Need to check if the expression we initialized this
1467 to was one of the iso_c_binding named constants. If so,
1468 and we're a parameter (constant), let it be iso_c.
1470 integer(c_int), parameter :: my_int = c_int
1471 integer(my_int) :: my_int_2
1472 If we mark my_int as iso_c (since we can see it's value
1473 is equal to one of the named constants), then my_int_2
1474 will be considered C interoperable. */
1475 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1477 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1478 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1479 /* attr bits needed for module files. */
1480 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1481 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1482 if (init
->ts
.is_iso_c
)
1483 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1486 /* Add initializer. Make sure we keep the ranks sane. */
1487 if (sym
->attr
.dimension
&& init
->rank
== 0)
1492 if (sym
->attr
.flavor
== FL_PARAMETER
1493 && init
->expr_type
== EXPR_CONSTANT
1494 && spec_size (sym
->as
, &size
) == SUCCESS
1495 && mpz_cmp_si (size
, 0) > 0)
1497 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1499 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1500 gfc_constructor_append_expr (&array
->value
.constructor
,
1503 : gfc_copy_expr (init
),
1506 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1507 for (n
= 0; n
< sym
->as
->rank
; n
++)
1508 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1513 init
->rank
= sym
->as
->rank
;
1517 if (sym
->attr
.save
== SAVE_NONE
)
1518 sym
->attr
.save
= SAVE_IMPLICIT
;
1526 /* Function called by variable_decl() that adds a name to a structure
1530 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1531 gfc_array_spec
**as
)
1534 gfc_try t
= SUCCESS
;
1536 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1537 constructing, it must have the pointer attribute. */
1538 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1539 && current_ts
.u
.derived
== gfc_current_block ()
1540 && current_attr
.pointer
== 0)
1542 gfc_error ("Component at %C must have the POINTER attribute");
1546 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1548 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1550 gfc_error ("Array component of structure at %C must have explicit "
1551 "or deferred shape");
1556 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1560 if (c
->ts
.type
== BT_CHARACTER
)
1562 c
->attr
= current_attr
;
1564 c
->initializer
= *init
;
1571 c
->attr
.codimension
= 1;
1573 c
->attr
.dimension
= 1;
1577 /* Should this ever get more complicated, combine with similar section
1578 in add_init_expr_to_sym into a separate function. */
1579 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1581 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1585 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1586 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1587 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1589 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1591 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1592 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1593 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1594 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1596 gfc_constructor
*ctor
;
1597 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1602 bool has_ts
= (c
->initializer
->ts
.u
.cl
1603 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1605 /* Remember the length of the first element for checking
1606 that all elements *in the constructor* have the same
1607 length. This need not be the length of the LHS! */
1608 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1609 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1610 first_len
= ctor
->expr
->value
.character
.length
;
1612 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1613 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1615 gfc_set_constant_character_len (len
, ctor
->expr
,
1616 has_ts
? -1 : first_len
);
1617 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1623 /* Check array components. */
1624 if (!c
->attr
.dimension
)
1627 if (c
->attr
.pointer
)
1629 if (c
->as
->type
!= AS_DEFERRED
)
1631 gfc_error ("Pointer array component of structure at %C must have a "
1636 else if (c
->attr
.allocatable
)
1638 if (c
->as
->type
!= AS_DEFERRED
)
1640 gfc_error ("Allocatable component of structure at %C must have a "
1647 if (c
->as
->type
!= AS_EXPLICIT
)
1649 gfc_error ("Array component of structure at %C must have an "
1656 if (c
->ts
.type
== BT_CLASS
)
1658 bool delayed
= (gfc_state_stack
->sym
== c
->ts
.u
.derived
)
1659 || (!c
->ts
.u
.derived
->components
1660 && !c
->ts
.u
.derived
->attr
.zero_comp
);
1661 gfc_try t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
, delayed
);
1671 /* Match a 'NULL()', and possibly take care of some side effects. */
1674 gfc_match_null (gfc_expr
**result
)
1679 m
= gfc_match (" null ( )");
1683 /* The NULL symbol now has to be/become an intrinsic function. */
1684 if (gfc_get_symbol ("null", NULL
, &sym
))
1686 gfc_error ("NULL() initialization at %C is ambiguous");
1690 gfc_intrinsic_symbol (sym
);
1692 if (sym
->attr
.proc
!= PROC_INTRINSIC
1693 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1694 sym
->name
, NULL
) == FAILURE
1695 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1698 *result
= gfc_get_null_expr (&gfc_current_locus
);
1704 /* Match the initialization expr for a data pointer or procedure pointer. */
1707 match_pointer_init (gfc_expr
**init
, int procptr
)
1711 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1713 gfc_error ("Initialization of pointer at %C is not allowed in "
1714 "a PURE procedure");
1718 /* Match NULL() initialization. */
1719 m
= gfc_match_null (init
);
1723 /* Match non-NULL initialization. */
1724 gfc_matching_ptr_assignment
= !procptr
;
1725 gfc_matching_procptr_assignment
= procptr
;
1726 m
= gfc_match_rvalue (init
);
1727 gfc_matching_ptr_assignment
= 0;
1728 gfc_matching_procptr_assignment
= 0;
1729 if (m
== MATCH_ERROR
)
1731 else if (m
== MATCH_NO
)
1733 gfc_error ("Error in pointer initialization at %C");
1738 gfc_resolve_expr (*init
);
1740 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: non-NULL pointer "
1741 "initialization at %C") == FAILURE
)
1749 check_function_name (char *name
)
1751 /* In functions that have a RESULT variable defined, the function name always
1752 refers to function calls. Therefore, the name is not allowed to appear in
1753 specification statements. When checking this, be careful about
1754 'hidden' procedure pointer results ('ppr@'). */
1756 if (gfc_current_state () == COMP_FUNCTION
)
1758 gfc_symbol
*block
= gfc_current_block ();
1759 if (block
&& block
->result
&& block
->result
!= block
1760 && strcmp (block
->result
->name
, "ppr@") != 0
1761 && strcmp (block
->name
, name
) == 0)
1763 gfc_error ("Function name '%s' not allowed at %C", name
);
1772 /* Match a variable name with an optional initializer. When this
1773 subroutine is called, a variable is expected to be parsed next.
1774 Depending on what is happening at the moment, updates either the
1775 symbol table or the current interface. */
1778 variable_decl (int elem
)
1780 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1781 gfc_expr
*initializer
, *char_len
;
1783 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1795 /* When we get here, we've just matched a list of attributes and
1796 maybe a type and a double colon. The next thing we expect to see
1797 is the name of the symbol. */
1798 m
= gfc_match_name (name
);
1802 var_locus
= gfc_current_locus
;
1804 /* Now we could see the optional array spec. or character length. */
1805 m
= gfc_match_array_spec (&as
, true, true);
1806 if (m
== MATCH_ERROR
)
1810 as
= gfc_copy_array_spec (current_as
);
1811 else if (current_as
)
1812 merge_array_spec (current_as
, as
, true);
1814 if (gfc_option
.flag_cray_pointer
)
1815 cp_as
= gfc_copy_array_spec (as
);
1817 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1818 determine (and check) whether it can be implied-shape. If it
1819 was parsed as assumed-size, change it because PARAMETERs can not
1823 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1826 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1831 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1832 && current_attr
.flavor
== FL_PARAMETER
)
1833 as
->type
= AS_IMPLIED_SHAPE
;
1835 if (as
->type
== AS_IMPLIED_SHAPE
1836 && gfc_notify_std (GFC_STD_F2008
,
1837 "Fortran 2008: Implied-shape array at %L",
1838 &var_locus
) == FAILURE
)
1847 cl_deferred
= false;
1849 if (current_ts
.type
== BT_CHARACTER
)
1851 switch (match_char_length (&char_len
, &cl_deferred
))
1854 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1856 cl
->length
= char_len
;
1859 /* Non-constant lengths need to be copied after the first
1860 element. Also copy assumed lengths. */
1863 && (current_ts
.u
.cl
->length
== NULL
1864 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1866 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1867 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
1870 cl
= current_ts
.u
.cl
;
1872 cl_deferred
= current_ts
.deferred
;
1881 /* If this symbol has already shown up in a Cray Pointer declaration,
1882 then we want to set the type & bail out. */
1883 if (gfc_option
.flag_cray_pointer
)
1885 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1886 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1888 sym
->ts
.type
= current_ts
.type
;
1889 sym
->ts
.kind
= current_ts
.kind
;
1891 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
1892 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1893 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1896 /* Check to see if we have an array specification. */
1899 if (sym
->as
!= NULL
)
1901 gfc_error ("Duplicate array spec for Cray pointee at %C");
1902 gfc_free_array_spec (cp_as
);
1908 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1909 gfc_internal_error ("Couldn't set pointee array spec.");
1911 /* Fix the array spec. */
1912 m
= gfc_mod_pointee_as (sym
->as
);
1913 if (m
== MATCH_ERROR
)
1921 gfc_free_array_spec (cp_as
);
1925 /* Procedure pointer as function result. */
1926 if (gfc_current_state () == COMP_FUNCTION
1927 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1928 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1929 strcpy (name
, "ppr@");
1931 if (gfc_current_state () == COMP_FUNCTION
1932 && strcmp (name
, gfc_current_block ()->name
) == 0
1933 && gfc_current_block ()->result
1934 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1935 strcpy (name
, "ppr@");
1937 /* OK, we've successfully matched the declaration. Now put the
1938 symbol in the current namespace, because it might be used in the
1939 optional initialization expression for this symbol, e.g. this is
1942 integer, parameter :: i = huge(i)
1944 This is only true for parameters or variables of a basic type.
1945 For components of derived types, it is not true, so we don't
1946 create a symbol for those yet. If we fail to create the symbol,
1948 if (gfc_current_state () != COMP_DERIVED
1949 && build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
) == FAILURE
)
1955 /* An interface body specifies all of the procedure's
1956 characteristics and these shall be consistent with those
1957 specified in the procedure definition, except that the interface
1958 may specify a procedure that is not pure if the procedure is
1959 defined to be pure(12.3.2). */
1960 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1961 && gfc_current_ns
->proc_name
1962 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1963 && current_ts
.u
.derived
->ns
!= gfc_current_ns
)
1966 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.u
.derived
->name
);
1967 if (!(current_ts
.u
.derived
->attr
.imported
1969 && gfc_find_dt_in_generic (st
->n
.sym
) == current_ts
.u
.derived
)
1970 && !gfc_current_ns
->has_import_set
)
1972 gfc_error ("The type of '%s' at %C has not been declared within the "
1979 if (check_function_name (name
) == FAILURE
)
1985 /* We allow old-style initializations of the form
1986 integer i /2/, j(4) /3*3, 1/
1987 (if no colon has been seen). These are different from data
1988 statements in that initializers are only allowed to apply to the
1989 variable immediately preceding, i.e.
1991 is not allowed. Therefore we have to do some work manually, that
1992 could otherwise be left to the matchers for DATA statements. */
1994 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1996 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1997 "initialization at %C") == FAILURE
)
2000 return match_old_style_init (name
);
2003 /* The double colon must be present in order to have initializers.
2004 Otherwise the statement is ambiguous with an assignment statement. */
2007 if (gfc_match (" =>") == MATCH_YES
)
2009 if (!current_attr
.pointer
)
2011 gfc_error ("Initialization at %C isn't for a pointer variable");
2016 m
= match_pointer_init (&initializer
, 0);
2020 else if (gfc_match_char ('=') == MATCH_YES
)
2022 if (current_attr
.pointer
)
2024 gfc_error ("Pointer initialization at %C requires '=>', "
2030 m
= gfc_match_init_expr (&initializer
);
2033 gfc_error ("Expected an initialization expression at %C");
2037 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2038 && gfc_state_stack
->state
!= COMP_DERIVED
)
2040 gfc_error ("Initialization of variable at %C is not allowed in "
2041 "a PURE procedure");
2050 if (initializer
!= NULL
&& current_attr
.allocatable
2051 && gfc_current_state () == COMP_DERIVED
)
2053 gfc_error ("Initialization of allocatable component at %C is not "
2059 /* Add the initializer. Note that it is fine if initializer is
2060 NULL here, because we sometimes also need to check if a
2061 declaration *must* have an initialization expression. */
2062 if (gfc_current_state () != COMP_DERIVED
)
2063 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2066 if (current_ts
.type
== BT_DERIVED
2067 && !current_attr
.pointer
&& !initializer
)
2068 initializer
= gfc_default_initializer (¤t_ts
);
2069 t
= build_struct (name
, cl
, &initializer
, &as
);
2072 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
2075 /* Free stuff up and return. */
2076 gfc_free_expr (initializer
);
2077 gfc_free_array_spec (as
);
2083 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2084 This assumes that the byte size is equal to the kind number for
2085 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2088 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2093 if (gfc_match_char ('*') != MATCH_YES
)
2096 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2100 original_kind
= ts
->kind
;
2102 /* Massage the kind numbers for complex types. */
2103 if (ts
->type
== BT_COMPLEX
)
2107 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2108 gfc_basic_typename (ts
->type
), original_kind
);
2115 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2118 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2122 if (gfc_option
.flag_real4_kind
== 8)
2124 if (gfc_option
.flag_real4_kind
== 10)
2126 if (gfc_option
.flag_real4_kind
== 16)
2132 if (gfc_option
.flag_real8_kind
== 4)
2134 if (gfc_option
.flag_real8_kind
== 10)
2136 if (gfc_option
.flag_real8_kind
== 16)
2141 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2143 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2144 gfc_basic_typename (ts
->type
), original_kind
);
2148 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
2149 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
2156 /* Match a kind specification. Since kinds are generally optional, we
2157 usually return MATCH_NO if something goes wrong. If a "kind="
2158 string is found, then we know we have an error. */
2161 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2173 where
= loc
= gfc_current_locus
;
2178 if (gfc_match_char ('(') == MATCH_NO
)
2181 /* Also gobbles optional text. */
2182 if (gfc_match (" kind = ") == MATCH_YES
)
2185 loc
= gfc_current_locus
;
2188 n
= gfc_match_init_expr (&e
);
2192 if (gfc_matching_function
)
2194 /* The function kind expression might include use associated or
2195 imported parameters and try again after the specification
2197 if (gfc_match_char (')') != MATCH_YES
)
2199 gfc_error ("Missing right parenthesis at %C");
2205 gfc_undo_symbols ();
2210 /* ....or else, the match is real. */
2212 gfc_error ("Expected initialization expression at %C");
2220 gfc_error ("Expected scalar initialization expression at %C");
2225 msg
= gfc_extract_int (e
, &ts
->kind
);
2234 /* Before throwing away the expression, let's see if we had a
2235 C interoperable kind (and store the fact). */
2236 if (e
->ts
.is_c_interop
== 1)
2238 /* Mark this as C interoperable if being declared with one
2239 of the named constants from iso_c_binding. */
2240 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2241 ts
->f90_type
= e
->ts
.f90_type
;
2247 /* Ignore errors to this point, if we've gotten here. This means
2248 we ignore the m=MATCH_ERROR from above. */
2249 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2251 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2252 gfc_basic_typename (ts
->type
));
2253 gfc_current_locus
= where
;
2257 /* Warn if, e.g., c_int is used for a REAL variable, but not
2258 if, e.g., c_double is used for COMPLEX as the standard
2259 explicitly says that the kind type parameter for complex and real
2260 variable is the same, i.e. c_float == c_float_complex. */
2261 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2262 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2263 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2264 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2265 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2266 gfc_basic_typename (ts
->type
));
2268 gfc_gobble_whitespace ();
2269 if ((c
= gfc_next_ascii_char ()) != ')'
2270 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2272 if (ts
->type
== BT_CHARACTER
)
2273 gfc_error ("Missing right parenthesis or comma at %C");
2275 gfc_error ("Missing right parenthesis at %C");
2279 /* All tests passed. */
2282 if(m
== MATCH_ERROR
)
2283 gfc_current_locus
= where
;
2285 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && gfc_option
.flag_integer4_kind
== 8)
2288 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2292 if (gfc_option
.flag_real4_kind
== 8)
2294 if (gfc_option
.flag_real4_kind
== 10)
2296 if (gfc_option
.flag_real4_kind
== 16)
2302 if (gfc_option
.flag_real8_kind
== 4)
2304 if (gfc_option
.flag_real8_kind
== 10)
2306 if (gfc_option
.flag_real8_kind
== 16)
2311 /* Return what we know from the test(s). */
2316 gfc_current_locus
= where
;
2322 match_char_kind (int * kind
, int * is_iso_c
)
2331 where
= gfc_current_locus
;
2333 n
= gfc_match_init_expr (&e
);
2335 if (n
!= MATCH_YES
&& gfc_matching_function
)
2337 /* The expression might include use-associated or imported
2338 parameters and try again after the specification
2341 gfc_undo_symbols ();
2346 gfc_error ("Expected initialization expression at %C");
2352 gfc_error ("Expected scalar initialization expression at %C");
2357 msg
= gfc_extract_int (e
, kind
);
2358 *is_iso_c
= e
->ts
.is_iso_c
;
2368 /* Ignore errors to this point, if we've gotten here. This means
2369 we ignore the m=MATCH_ERROR from above. */
2370 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2372 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2376 /* All tests passed. */
2379 if (m
== MATCH_ERROR
)
2380 gfc_current_locus
= where
;
2382 /* Return what we know from the test(s). */
2387 gfc_current_locus
= where
;
2392 /* Match the various kind/length specifications in a CHARACTER
2393 declaration. We don't return MATCH_NO. */
2396 gfc_match_char_spec (gfc_typespec
*ts
)
2398 int kind
, seen_length
, is_iso_c
;
2410 /* Try the old-style specification first. */
2411 old_char_selector
= 0;
2413 m
= match_char_length (&len
, &deferred
);
2417 old_char_selector
= 1;
2422 m
= gfc_match_char ('(');
2425 m
= MATCH_YES
; /* Character without length is a single char. */
2429 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2430 if (gfc_match (" kind =") == MATCH_YES
)
2432 m
= match_char_kind (&kind
, &is_iso_c
);
2434 if (m
== MATCH_ERROR
)
2439 if (gfc_match (" , len =") == MATCH_NO
)
2442 m
= char_len_param_value (&len
, &deferred
);
2445 if (m
== MATCH_ERROR
)
2452 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2453 if (gfc_match (" len =") == MATCH_YES
)
2455 m
= char_len_param_value (&len
, &deferred
);
2458 if (m
== MATCH_ERROR
)
2462 if (gfc_match_char (')') == MATCH_YES
)
2465 if (gfc_match (" , kind =") != MATCH_YES
)
2468 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2474 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2475 m
= char_len_param_value (&len
, &deferred
);
2478 if (m
== MATCH_ERROR
)
2482 m
= gfc_match_char (')');
2486 if (gfc_match_char (',') != MATCH_YES
)
2489 gfc_match (" kind ="); /* Gobble optional text. */
2491 m
= match_char_kind (&kind
, &is_iso_c
);
2492 if (m
== MATCH_ERROR
)
2498 /* Require a right-paren at this point. */
2499 m
= gfc_match_char (')');
2504 gfc_error ("Syntax error in CHARACTER declaration at %C");
2506 gfc_free_expr (len
);
2510 /* Deal with character functions after USE and IMPORT statements. */
2511 if (gfc_matching_function
)
2513 gfc_free_expr (len
);
2514 gfc_undo_symbols ();
2520 gfc_free_expr (len
);
2524 /* Do some final massaging of the length values. */
2525 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2527 if (seen_length
== 0)
2528 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2533 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2534 ts
->deferred
= deferred
;
2536 /* We have to know if it was a C interoperable kind so we can
2537 do accurate type checking of bind(c) procs, etc. */
2539 /* Mark this as C interoperable if being declared with one
2540 of the named constants from iso_c_binding. */
2541 ts
->is_c_interop
= is_iso_c
;
2542 else if (len
!= NULL
)
2543 /* Here, we might have parsed something such as: character(c_char)
2544 In this case, the parsing code above grabs the c_char when
2545 looking for the length (line 1690, roughly). it's the last
2546 testcase for parsing the kind params of a character variable.
2547 However, it's not actually the length. this seems like it
2549 To see if the user used a C interop kind, test the expr
2550 of the so called length, and see if it's C interoperable. */
2551 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2557 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2558 structure to the matched specification. This is necessary for FUNCTION and
2559 IMPLICIT statements.
2561 If implicit_flag is nonzero, then we don't check for the optional
2562 kind specification. Not doing so is needed for matching an IMPLICIT
2563 statement correctly. */
2566 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2568 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2569 gfc_symbol
*sym
, *dt_sym
;
2572 bool seen_deferred_kind
, matched_type
;
2573 const char *dt_name
;
2575 /* A belt and braces check that the typespec is correctly being treated
2576 as a deferred characteristic association. */
2577 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2578 && (gfc_current_block ()->result
->ts
.kind
== -1)
2579 && (ts
->kind
== -1);
2581 if (seen_deferred_kind
)
2584 /* Clear the current binding label, in case one is given. */
2585 curr_binding_label
= NULL
;
2587 if (gfc_match (" byte") == MATCH_YES
)
2589 if (gfc_notify_std (GFC_STD_GNU
, "Extension: BYTE type at %C")
2593 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2595 gfc_error ("BYTE type used at %C "
2596 "is not available on the target machine");
2600 ts
->type
= BT_INTEGER
;
2606 m
= gfc_match (" type (");
2607 matched_type
= (m
== MATCH_YES
);
2610 gfc_gobble_whitespace ();
2611 if (gfc_peek_ascii_char () == '*')
2613 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2615 if (gfc_current_state () == COMP_DERIVED
)
2617 gfc_error ("Assumed type at %C is not allowed for components");
2620 if (gfc_notify_std (GFC_STD_F2008_TS
, "TS 29113: Assumed type "
2621 "at %C") == FAILURE
)
2623 ts
->type
= BT_ASSUMED
;
2627 m
= gfc_match ("%n", name
);
2628 matched_type
= (m
== MATCH_YES
);
2631 if ((matched_type
&& strcmp ("integer", name
) == 0)
2632 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2634 ts
->type
= BT_INTEGER
;
2635 ts
->kind
= gfc_default_integer_kind
;
2639 if ((matched_type
&& strcmp ("character", name
) == 0)
2640 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2643 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2644 "intrinsic-type-spec at %C") == FAILURE
)
2647 ts
->type
= BT_CHARACTER
;
2648 if (implicit_flag
== 0)
2649 m
= gfc_match_char_spec (ts
);
2653 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2659 if ((matched_type
&& strcmp ("real", name
) == 0)
2660 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2663 ts
->kind
= gfc_default_real_kind
;
2668 && (strcmp ("doubleprecision", name
) == 0
2669 || (strcmp ("double", name
) == 0
2670 && gfc_match (" precision") == MATCH_YES
)))
2671 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2674 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2675 "intrinsic-type-spec at %C") == FAILURE
)
2677 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2681 ts
->kind
= gfc_default_double_kind
;
2685 if ((matched_type
&& strcmp ("complex", name
) == 0)
2686 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2688 ts
->type
= BT_COMPLEX
;
2689 ts
->kind
= gfc_default_complex_kind
;
2694 && (strcmp ("doublecomplex", name
) == 0
2695 || (strcmp ("double", name
) == 0
2696 && gfc_match (" complex") == MATCH_YES
)))
2697 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2699 if (gfc_notify_std (GFC_STD_GNU
, "Extension: DOUBLE COMPLEX at %C")
2704 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2705 "intrinsic-type-spec at %C") == FAILURE
)
2708 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2711 ts
->type
= BT_COMPLEX
;
2712 ts
->kind
= gfc_default_double_kind
;
2716 if ((matched_type
&& strcmp ("logical", name
) == 0)
2717 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2719 ts
->type
= BT_LOGICAL
;
2720 ts
->kind
= gfc_default_logical_kind
;
2725 m
= gfc_match_char (')');
2728 ts
->type
= BT_DERIVED
;
2731 /* Match CLASS declarations. */
2732 m
= gfc_match (" class ( * )");
2733 if (m
== MATCH_ERROR
)
2735 else if (m
== MATCH_YES
)
2737 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2741 m
= gfc_match (" class ( %n )", name
);
2744 ts
->type
= BT_CLASS
;
2746 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: CLASS statement at %C")
2751 /* Defer association of the derived type until the end of the
2752 specification block. However, if the derived type can be
2753 found, add it to the typespec. */
2754 if (gfc_matching_function
)
2756 ts
->u
.derived
= NULL
;
2757 if (gfc_current_state () != COMP_INTERFACE
2758 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2760 sym
= gfc_find_dt_in_generic (sym
);
2761 ts
->u
.derived
= sym
;
2766 /* Search for the name but allow the components to be defined later. If
2767 type = -1, this typespec has been seen in a function declaration but
2768 the type could not be accessed at that point. The actual derived type is
2769 stored in a symtree with the first letter of the name capitalized; the
2770 symtree with the all lower-case name contains the associated
2771 generic function. */
2772 dt_name
= gfc_get_string ("%c%s",
2773 (char) TOUPPER ((unsigned char) name
[0]),
2774 (const char*)&name
[1]);
2779 gfc_get_ha_symbol (name
, &sym
);
2780 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2782 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2785 if (sym
->generic
&& !dt_sym
)
2786 dt_sym
= gfc_find_dt_in_generic (sym
);
2788 else if (ts
->kind
== -1)
2790 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2791 || gfc_current_ns
->has_import_set
;
2792 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2793 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2795 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2798 if (sym
&& sym
->generic
&& !dt_sym
)
2799 dt_sym
= gfc_find_dt_in_generic (sym
);
2806 if ((sym
->attr
.flavor
!= FL_UNKNOWN
2807 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
2808 || sym
->attr
.subroutine
)
2810 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2811 "entity at %L, which has the same name", name
,
2816 gfc_set_sym_referenced (sym
);
2817 if (!sym
->attr
.generic
2818 && gfc_add_generic (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2821 if (!sym
->attr
.function
2822 && gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
2827 gfc_interface
*intr
, *head
;
2829 /* Use upper case to save the actual derived-type symbol. */
2830 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
2831 dt_sym
->name
= gfc_get_string (sym
->name
);
2832 head
= sym
->generic
;
2833 intr
= gfc_get_interface ();
2835 intr
->where
= gfc_current_locus
;
2837 sym
->generic
= intr
;
2838 sym
->attr
.if_source
= IFSRC_DECL
;
2841 gfc_set_sym_referenced (dt_sym
);
2843 if (dt_sym
->attr
.flavor
!= FL_DERIVED
2844 && gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
)
2848 ts
->u
.derived
= dt_sym
;
2854 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: TYPE with "
2855 "intrinsic-type-spec at %C") == FAILURE
)
2858 /* For all types except double, derived and character, look for an
2859 optional kind specifier. MATCH_NO is actually OK at this point. */
2860 if (implicit_flag
== 1)
2862 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2868 if (gfc_current_form
== FORM_FREE
)
2870 c
= gfc_peek_ascii_char ();
2871 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
2872 && c
!= ':' && c
!= ',')
2874 if (matched_type
&& c
== ')')
2876 gfc_next_ascii_char ();
2883 m
= gfc_match_kind_spec (ts
, false);
2884 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2885 m
= gfc_match_old_kind_spec (ts
);
2887 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2890 /* Defer association of the KIND expression of function results
2891 until after USE and IMPORT statements. */
2892 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2893 || gfc_matching_function
)
2897 m
= MATCH_YES
; /* No kind specifier found. */
2903 /* Match an IMPLICIT NONE statement. Actually, this statement is
2904 already matched in parse.c, or we would not end up here in the
2905 first place. So the only thing we need to check, is if there is
2906 trailing garbage. If not, the match is successful. */
2909 gfc_match_implicit_none (void)
2911 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2915 /* Match the letter range(s) of an IMPLICIT statement. */
2918 match_implicit_range (void)
2924 cur_loc
= gfc_current_locus
;
2926 gfc_gobble_whitespace ();
2927 c
= gfc_next_ascii_char ();
2930 gfc_error ("Missing character range in IMPLICIT at %C");
2937 gfc_gobble_whitespace ();
2938 c1
= gfc_next_ascii_char ();
2942 gfc_gobble_whitespace ();
2943 c
= gfc_next_ascii_char ();
2948 inner
= 0; /* Fall through. */
2955 gfc_gobble_whitespace ();
2956 c2
= gfc_next_ascii_char ();
2960 gfc_gobble_whitespace ();
2961 c
= gfc_next_ascii_char ();
2963 if ((c
!= ',') && (c
!= ')'))
2976 gfc_error ("Letters must be in alphabetic order in "
2977 "IMPLICIT statement at %C");
2981 /* See if we can add the newly matched range to the pending
2982 implicits from this IMPLICIT statement. We do not check for
2983 conflicts with whatever earlier IMPLICIT statements may have
2984 set. This is done when we've successfully finished matching
2986 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2993 gfc_syntax_error (ST_IMPLICIT
);
2995 gfc_current_locus
= cur_loc
;
3000 /* Match an IMPLICIT statement, storing the types for
3001 gfc_set_implicit() if the statement is accepted by the parser.
3002 There is a strange looking, but legal syntactic construction
3003 possible. It looks like:
3005 IMPLICIT INTEGER (a-b) (c-d)
3007 This is legal if "a-b" is a constant expression that happens to
3008 equal one of the legal kinds for integers. The real problem
3009 happens with an implicit specification that looks like:
3011 IMPLICIT INTEGER (a-b)
3013 In this case, a typespec matcher that is "greedy" (as most of the
3014 matchers are) gobbles the character range as a kindspec, leaving
3015 nothing left. We therefore have to go a bit more slowly in the
3016 matching process by inhibiting the kindspec checking during
3017 typespec matching and checking for a kind later. */
3020 gfc_match_implicit (void)
3029 /* We don't allow empty implicit statements. */
3030 if (gfc_match_eos () == MATCH_YES
)
3032 gfc_error ("Empty IMPLICIT statement at %C");
3038 /* First cleanup. */
3039 gfc_clear_new_implicit ();
3041 /* A basic type is mandatory here. */
3042 m
= gfc_match_decl_type_spec (&ts
, 1);
3043 if (m
== MATCH_ERROR
)
3048 cur_loc
= gfc_current_locus
;
3049 m
= match_implicit_range ();
3053 /* We may have <TYPE> (<RANGE>). */
3054 gfc_gobble_whitespace ();
3055 c
= gfc_next_ascii_char ();
3056 if ((c
== '\n') || (c
== ','))
3058 /* Check for CHARACTER with no length parameter. */
3059 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3061 ts
.kind
= gfc_default_character_kind
;
3062 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3063 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3067 /* Record the Successful match. */
3068 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
3073 gfc_current_locus
= cur_loc
;
3076 /* Discard the (incorrectly) matched range. */
3077 gfc_clear_new_implicit ();
3079 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3080 if (ts
.type
== BT_CHARACTER
)
3081 m
= gfc_match_char_spec (&ts
);
3084 m
= gfc_match_kind_spec (&ts
, false);
3087 m
= gfc_match_old_kind_spec (&ts
);
3088 if (m
== MATCH_ERROR
)
3094 if (m
== MATCH_ERROR
)
3097 m
= match_implicit_range ();
3098 if (m
== MATCH_ERROR
)
3103 gfc_gobble_whitespace ();
3104 c
= gfc_next_ascii_char ();
3105 if ((c
!= '\n') && (c
!= ','))
3108 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
3116 gfc_syntax_error (ST_IMPLICIT
);
3124 gfc_match_import (void)
3126 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3131 if (gfc_current_ns
->proc_name
== NULL
3132 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3134 gfc_error ("IMPORT statement at %C only permitted in "
3135 "an INTERFACE body");
3139 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
3143 if (gfc_match_eos () == MATCH_YES
)
3145 /* All host variables should be imported. */
3146 gfc_current_ns
->has_import_set
= 1;
3150 if (gfc_match (" ::") == MATCH_YES
)
3152 if (gfc_match_eos () == MATCH_YES
)
3154 gfc_error ("Expecting list of named entities at %C");
3162 m
= gfc_match (" %n", name
);
3166 if (gfc_current_ns
->parent
!= NULL
3167 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3169 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3172 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3173 && gfc_find_symbol (name
,
3174 gfc_current_ns
->proc_name
->ns
->parent
,
3177 gfc_error ("Type name '%s' at %C is ambiguous", name
);
3183 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3184 "at %C - does not exist.", name
);
3188 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
3190 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3195 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, sym
->name
);
3198 sym
->attr
.imported
= 1;
3200 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3202 /* The actual derived type is stored in a symtree with the first
3203 letter of the name capitalized; the symtree with the all
3204 lower-case name contains the associated generic function. */
3205 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3206 gfc_get_string ("%c%s",
3207 (char) TOUPPER ((unsigned char) sym
->name
[0]),
3211 sym
->attr
.imported
= 1;
3224 if (gfc_match_eos () == MATCH_YES
)
3226 if (gfc_match_char (',') != MATCH_YES
)
3233 gfc_error ("Syntax error in IMPORT statement at %C");
3238 /* A minimal implementation of gfc_match without whitespace, escape
3239 characters or variable arguments. Returns true if the next
3240 characters match the TARGET template exactly. */
3243 match_string_p (const char *target
)
3247 for (p
= target
; *p
; p
++)
3248 if ((char) gfc_next_ascii_char () != *p
)
3253 /* Matches an attribute specification including array specs. If
3254 successful, leaves the variables current_attr and current_as
3255 holding the specification. Also sets the colon_seen variable for
3256 later use by matchers associated with initializations.
3258 This subroutine is a little tricky in the sense that we don't know
3259 if we really have an attr-spec until we hit the double colon.
3260 Until that time, we can only return MATCH_NO. This forces us to
3261 check for duplicate specification at this level. */
3264 match_attr_spec (void)
3266 /* Modifiers that can exist in a type statement. */
3268 { GFC_DECL_BEGIN
= 0,
3269 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3270 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3271 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3272 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3273 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3274 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3277 /* GFC_DECL_END is the sentinel, index starts at 0. */
3278 #define NUM_DECL GFC_DECL_END
3280 locus start
, seen_at
[NUM_DECL
];
3287 gfc_clear_attr (¤t_attr
);
3288 start
= gfc_current_locus
;
3293 /* See if we get all of the keywords up to the final double colon. */
3294 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3302 gfc_gobble_whitespace ();
3304 ch
= gfc_next_ascii_char ();
3307 /* This is the successful exit condition for the loop. */
3308 if (gfc_next_ascii_char () == ':')
3313 gfc_gobble_whitespace ();
3314 switch (gfc_peek_ascii_char ())
3317 gfc_next_ascii_char ();
3318 switch (gfc_next_ascii_char ())
3321 if (match_string_p ("locatable"))
3323 /* Matched "allocatable". */
3324 d
= DECL_ALLOCATABLE
;
3329 if (match_string_p ("ynchronous"))
3331 /* Matched "asynchronous". */
3332 d
= DECL_ASYNCHRONOUS
;
3339 /* Try and match the bind(c). */
3340 m
= gfc_match_bind_c (NULL
, true);
3343 else if (m
== MATCH_ERROR
)
3348 gfc_next_ascii_char ();
3349 if ('o' != gfc_next_ascii_char ())
3351 switch (gfc_next_ascii_char ())
3354 if (match_string_p ("imension"))
3356 d
= DECL_CODIMENSION
;
3360 if (match_string_p ("tiguous"))
3362 d
= DECL_CONTIGUOUS
;
3369 if (match_string_p ("dimension"))
3374 if (match_string_p ("external"))
3379 if (match_string_p ("int"))
3381 ch
= gfc_next_ascii_char ();
3384 if (match_string_p ("nt"))
3386 /* Matched "intent". */
3387 /* TODO: Call match_intent_spec from here. */
3388 if (gfc_match (" ( in out )") == MATCH_YES
)
3390 else if (gfc_match (" ( in )") == MATCH_YES
)
3392 else if (gfc_match (" ( out )") == MATCH_YES
)
3398 if (match_string_p ("insic"))
3400 /* Matched "intrinsic". */
3408 if (match_string_p ("optional"))
3413 gfc_next_ascii_char ();
3414 switch (gfc_next_ascii_char ())
3417 if (match_string_p ("rameter"))
3419 /* Matched "parameter". */
3425 if (match_string_p ("inter"))
3427 /* Matched "pointer". */
3433 ch
= gfc_next_ascii_char ();
3436 if (match_string_p ("vate"))
3438 /* Matched "private". */
3444 if (match_string_p ("tected"))
3446 /* Matched "protected". */
3453 if (match_string_p ("blic"))
3455 /* Matched "public". */
3463 if (match_string_p ("save"))
3468 if (match_string_p ("target"))
3473 gfc_next_ascii_char ();
3474 ch
= gfc_next_ascii_char ();
3477 if (match_string_p ("lue"))
3479 /* Matched "value". */
3485 if (match_string_p ("latile"))
3487 /* Matched "volatile". */
3495 /* No double colon and no recognizable decl_type, so assume that
3496 we've been looking at something else the whole time. */
3503 /* Check to make sure any parens are paired up correctly. */
3504 if (gfc_match_parens () == MATCH_ERROR
)
3511 seen_at
[d
] = gfc_current_locus
;
3513 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3515 gfc_array_spec
*as
= NULL
;
3517 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3518 d
== DECL_CODIMENSION
);
3520 if (current_as
== NULL
)
3522 else if (m
== MATCH_YES
)
3524 merge_array_spec (as
, current_as
, false);
3530 if (d
== DECL_CODIMENSION
)
3531 gfc_error ("Missing codimension specification at %C");
3533 gfc_error ("Missing dimension specification at %C");
3537 if (m
== MATCH_ERROR
)
3542 /* Since we've seen a double colon, we have to be looking at an
3543 attr-spec. This means that we can now issue errors. */
3544 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3549 case DECL_ALLOCATABLE
:
3550 attr
= "ALLOCATABLE";
3552 case DECL_ASYNCHRONOUS
:
3553 attr
= "ASYNCHRONOUS";
3555 case DECL_CODIMENSION
:
3556 attr
= "CODIMENSION";
3558 case DECL_CONTIGUOUS
:
3559 attr
= "CONTIGUOUS";
3561 case DECL_DIMENSION
:
3568 attr
= "INTENT (IN)";
3571 attr
= "INTENT (OUT)";
3574 attr
= "INTENT (IN OUT)";
3576 case DECL_INTRINSIC
:
3582 case DECL_PARAMETER
:
3588 case DECL_PROTECTED
:
3603 case DECL_IS_BIND_C
:
3613 attr
= NULL
; /* This shouldn't happen. */
3616 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3621 /* Now that we've dealt with duplicate attributes, add the attributes
3622 to the current attribute. */
3623 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3628 if (gfc_current_state () == COMP_DERIVED
3629 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3630 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3631 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3633 if (d
== DECL_ALLOCATABLE
)
3635 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3636 "attribute at %C in a TYPE definition")
3645 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3652 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3653 && gfc_current_state () != COMP_MODULE
)
3655 if (d
== DECL_PRIVATE
)
3659 if (gfc_current_state () == COMP_DERIVED
3660 && gfc_state_stack
->previous
3661 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3663 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3664 "at %L in a TYPE definition", attr
,
3674 gfc_error ("%s attribute at %L is not allowed outside of the "
3675 "specification part of a module", attr
, &seen_at
[d
]);
3683 case DECL_ALLOCATABLE
:
3684 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3687 case DECL_ASYNCHRONOUS
:
3688 if (gfc_notify_std (GFC_STD_F2003
,
3689 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3693 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3696 case DECL_CODIMENSION
:
3697 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3700 case DECL_CONTIGUOUS
:
3701 if (gfc_notify_std (GFC_STD_F2008
,
3702 "Fortran 2008: CONTIGUOUS attribute at %C")
3706 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3709 case DECL_DIMENSION
:
3710 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3714 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3718 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3722 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3726 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3729 case DECL_INTRINSIC
:
3730 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3734 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3737 case DECL_PARAMETER
:
3738 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3742 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3745 case DECL_PROTECTED
:
3746 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3748 gfc_error ("PROTECTED at %C only allowed in specification "
3749 "part of a module");
3754 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3759 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3763 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3768 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3773 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
3777 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3780 case DECL_IS_BIND_C
:
3781 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3785 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3790 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3794 if (gfc_notify_std (GFC_STD_F2003
,
3795 "Fortran 2003: VOLATILE attribute at %C")
3799 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3803 gfc_internal_error ("match_attr_spec(): Bad attribute");
3813 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3814 if (gfc_current_state () == COMP_MODULE
&& !current_attr
.save
3815 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
3816 current_attr
.save
= SAVE_IMPLICIT
;
3822 gfc_current_locus
= start
;
3823 gfc_free_array_spec (current_as
);
3829 /* Set the binding label, dest_label, either with the binding label
3830 stored in the given gfc_typespec, ts, or if none was provided, it
3831 will be the symbol name in all lower case, as required by the draft
3832 (J3/04-007, section 15.4.1). If a binding label was given and
3833 there is more than one argument (num_idents), it is an error. */
3836 set_binding_label (const char **dest_label
, const char *sym_name
,
3839 if (num_idents
> 1 && has_name_equals
)
3841 gfc_error ("Multiple identifiers provided with "
3842 "single NAME= specifier at %C");
3846 if (curr_binding_label
)
3847 /* Binding label given; store in temp holder till have sym. */
3848 *dest_label
= curr_binding_label
;
3851 /* No binding label given, and the NAME= specifier did not exist,
3852 which means there was no NAME="". */
3853 if (sym_name
!= NULL
&& has_name_equals
== 0)
3854 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
3861 /* Set the status of the given common block as being BIND(C) or not,
3862 depending on the given parameter, is_bind_c. */
3865 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3867 com_block
->is_bind_c
= is_bind_c
;
3872 /* Verify that the given gfc_typespec is for a C interoperable type. */
3875 gfc_verify_c_interop (gfc_typespec
*ts
)
3877 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
3878 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
3879 ? SUCCESS
: FAILURE
;
3880 else if (ts
->type
== BT_CLASS
)
3882 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
3889 /* Verify that the variables of a given common block, which has been
3890 defined with the attribute specifier bind(c), to be of a C
3891 interoperable type. Errors will be reported here, if
3895 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3897 gfc_symbol
*curr_sym
= NULL
;
3898 gfc_try retval
= SUCCESS
;
3900 curr_sym
= com_block
->head
;
3902 /* Make sure we have at least one symbol. */
3903 if (curr_sym
== NULL
)
3906 /* Here we know we have a symbol, so we'll execute this loop
3910 /* The second to last param, 1, says this is in a common block. */
3911 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3912 curr_sym
= curr_sym
->common_next
;
3913 } while (curr_sym
!= NULL
);
3919 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3920 an appropriate error message is reported. */
3923 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3924 int is_in_common
, gfc_common_head
*com_block
)
3926 bool bind_c_function
= false;
3927 gfc_try retval
= SUCCESS
;
3929 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3930 bind_c_function
= true;
3932 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3934 tmp_sym
= tmp_sym
->result
;
3935 /* Make sure it wasn't an implicitly typed result. */
3936 if (tmp_sym
->attr
.implicit_type
&& gfc_option
.warn_c_binding_type
)
3938 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3939 "%L may not be C interoperable", tmp_sym
->name
,
3940 &tmp_sym
->declared_at
);
3941 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3942 /* Mark it as C interoperable to prevent duplicate warnings. */
3943 tmp_sym
->ts
.is_c_interop
= 1;
3944 tmp_sym
->attr
.is_c_interop
= 1;
3948 /* Here, we know we have the bind(c) attribute, so if we have
3949 enough type info, then verify that it's a C interop kind.
3950 The info could be in the symbol already, or possibly still in
3951 the given ts (current_ts), so look in both. */
3952 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3954 if (gfc_verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3956 /* See if we're dealing with a sym in a common block or not. */
3957 if (is_in_common
== 1 && gfc_option
.warn_c_binding_type
)
3959 gfc_warning ("Variable '%s' in common block '%s' at %L "
3960 "may not be a C interoperable "
3961 "kind though common block '%s' is BIND(C)",
3962 tmp_sym
->name
, com_block
->name
,
3963 &(tmp_sym
->declared_at
), com_block
->name
);
3967 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3968 gfc_error ("Type declaration '%s' at %L is not C "
3969 "interoperable but it is BIND(C)",
3970 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3971 else if (gfc_option
.warn_c_binding_type
)
3972 gfc_warning ("Variable '%s' at %L "
3973 "may not be a C interoperable "
3974 "kind but it is bind(c)",
3975 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3979 /* Variables declared w/in a common block can't be bind(c)
3980 since there's no way for C to see these variables, so there's
3981 semantically no reason for the attribute. */
3982 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3984 gfc_error ("Variable '%s' in common block '%s' at "
3985 "%L cannot be declared with BIND(C) "
3986 "since it is not a global",
3987 tmp_sym
->name
, com_block
->name
,
3988 &(tmp_sym
->declared_at
));
3992 /* Scalar variables that are bind(c) can not have the pointer
3993 or allocatable attributes. */
3994 if (tmp_sym
->attr
.is_bind_c
== 1)
3996 if (tmp_sym
->attr
.pointer
== 1)
3998 gfc_error ("Variable '%s' at %L cannot have both the "
3999 "POINTER and BIND(C) attributes",
4000 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4004 if (tmp_sym
->attr
.allocatable
== 1)
4006 gfc_error ("Variable '%s' at %L cannot have both the "
4007 "ALLOCATABLE and BIND(C) attributes",
4008 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4014 /* If it is a BIND(C) function, make sure the return value is a
4015 scalar value. The previous tests in this function made sure
4016 the type is interoperable. */
4017 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4018 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4019 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4021 /* BIND(C) functions can not return a character string. */
4022 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4023 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4024 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4025 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4026 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4027 "be a character string", tmp_sym
->name
,
4028 &(tmp_sym
->declared_at
));
4031 /* See if the symbol has been marked as private. If it has, make sure
4032 there is no binding label and warn the user if there is one. */
4033 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4034 && tmp_sym
->binding_label
)
4035 /* Use gfc_warning_now because we won't say that the symbol fails
4036 just because of this. */
4037 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4038 "given the binding label '%s'", tmp_sym
->name
,
4039 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4045 /* Set the appropriate fields for a symbol that's been declared as
4046 BIND(C) (the is_bind_c flag and the binding label), and verify that
4047 the type is C interoperable. Errors are reported by the functions
4048 used to set/test these fields. */
4051 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4053 gfc_try retval
= SUCCESS
;
4055 /* TODO: Do we need to make sure the vars aren't marked private? */
4057 /* Set the is_bind_c bit in symbol_attribute. */
4058 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4060 if (set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
,
4061 num_idents
) != SUCCESS
)
4068 /* Set the fields marking the given common block as BIND(C), including
4069 a binding label, and report any errors encountered. */
4072 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4074 gfc_try retval
= SUCCESS
;
4076 /* destLabel, common name, typespec (which may have binding label). */
4077 if (set_binding_label (&com_block
->binding_label
, com_block
->name
,
4082 /* Set the given common block (com_block) to being bind(c) (1). */
4083 set_com_block_bind_c (com_block
, 1);
4089 /* Retrieve the list of one or more identifiers that the given bind(c)
4090 attribute applies to. */
4093 get_bind_c_idents (void)
4095 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4097 gfc_symbol
*tmp_sym
= NULL
;
4099 gfc_common_head
*com_block
= NULL
;
4101 if (gfc_match_name (name
) == MATCH_YES
)
4103 found_id
= MATCH_YES
;
4104 gfc_get_ha_symbol (name
, &tmp_sym
);
4106 else if (match_common_name (name
) == MATCH_YES
)
4108 found_id
= MATCH_YES
;
4109 com_block
= gfc_get_common (name
, 0);
4113 gfc_error ("Need either entity or common block name for "
4114 "attribute specification statement at %C");
4118 /* Save the current identifier and look for more. */
4121 /* Increment the number of identifiers found for this spec stmt. */
4124 /* Make sure we have a sym or com block, and verify that it can
4125 be bind(c). Set the appropriate field(s) and look for more
4127 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4129 if (tmp_sym
!= NULL
)
4131 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
4137 if (set_verify_bind_c_com_block(com_block
, num_idents
)
4142 /* Look to see if we have another identifier. */
4144 if (gfc_match_eos () == MATCH_YES
)
4145 found_id
= MATCH_NO
;
4146 else if (gfc_match_char (',') != MATCH_YES
)
4147 found_id
= MATCH_NO
;
4148 else if (gfc_match_name (name
) == MATCH_YES
)
4150 found_id
= MATCH_YES
;
4151 gfc_get_ha_symbol (name
, &tmp_sym
);
4153 else if (match_common_name (name
) == MATCH_YES
)
4155 found_id
= MATCH_YES
;
4156 com_block
= gfc_get_common (name
, 0);
4160 gfc_error ("Missing entity or common block name for "
4161 "attribute specification statement at %C");
4167 gfc_internal_error ("Missing symbol");
4169 } while (found_id
== MATCH_YES
);
4171 /* if we get here we were successful */
4176 /* Try and match a BIND(C) attribute specification statement. */
4179 gfc_match_bind_c_stmt (void)
4181 match found_match
= MATCH_NO
;
4186 /* This may not be necessary. */
4188 /* Clear the temporary binding label holder. */
4189 curr_binding_label
= NULL
;
4191 /* Look for the bind(c). */
4192 found_match
= gfc_match_bind_c (NULL
, true);
4194 if (found_match
== MATCH_YES
)
4196 /* Look for the :: now, but it is not required. */
4199 /* Get the identifier(s) that needs to be updated. This may need to
4200 change to hand the flag(s) for the attr specified so all identifiers
4201 found can have all appropriate parts updated (assuming that the same
4202 spec stmt can have multiple attrs, such as both bind(c) and
4204 if (get_bind_c_idents () != SUCCESS
)
4205 /* Error message should have printed already. */
4213 /* Match a data declaration statement. */
4216 gfc_match_data_decl (void)
4222 num_idents_on_line
= 0;
4224 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4228 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4229 && gfc_current_state () != COMP_DERIVED
)
4231 sym
= gfc_use_derived (current_ts
.u
.derived
);
4239 current_ts
.u
.derived
= sym
;
4242 m
= match_attr_spec ();
4243 if (m
== MATCH_ERROR
)
4249 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4250 && current_ts
.u
.derived
->components
== NULL
4251 && !current_ts
.u
.derived
->attr
.zero_comp
)
4254 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4257 gfc_find_symbol (current_ts
.u
.derived
->name
,
4258 current_ts
.u
.derived
->ns
->parent
, 1, &sym
);
4260 /* Any symbol that we find had better be a type definition
4261 which has its components defined. */
4262 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4263 && (current_ts
.u
.derived
->components
!= NULL
4264 || current_ts
.u
.derived
->attr
.zero_comp
))
4267 /* Now we have an error, which we signal, and then fix up
4268 because the knock-on is plain and simple confusing. */
4269 gfc_error_now ("Derived type at %C has not been previously defined "
4270 "and so cannot appear in a derived type definition");
4271 current_attr
.pointer
= 1;
4276 /* If we have an old-style character declaration, and no new-style
4277 attribute specifications, then there a comma is optional between
4278 the type specification and the variable list. */
4279 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4280 gfc_match_char (',');
4282 /* Give the types/attributes to symbols that follow. Give the element
4283 a number so that repeat character length expressions can be copied. */
4287 num_idents_on_line
++;
4288 m
= variable_decl (elem
++);
4289 if (m
== MATCH_ERROR
)
4294 if (gfc_match_eos () == MATCH_YES
)
4296 if (gfc_match_char (',') != MATCH_YES
)
4300 if (gfc_error_flag_test () == 0)
4301 gfc_error ("Syntax error in data declaration at %C");
4304 gfc_free_data_all (gfc_current_ns
);
4307 gfc_free_array_spec (current_as
);
4313 /* Match a prefix associated with a function or subroutine
4314 declaration. If the typespec pointer is nonnull, then a typespec
4315 can be matched. Note that if nothing matches, MATCH_YES is
4316 returned (the null string was matched). */
4319 gfc_match_prefix (gfc_typespec
*ts
)
4325 gfc_clear_attr (¤t_attr
);
4327 seen_impure
= false;
4329 gcc_assert (!gfc_matching_prefix
);
4330 gfc_matching_prefix
= true;
4334 found_prefix
= false;
4336 if (!seen_type
&& ts
!= NULL
4337 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4338 && gfc_match_space () == MATCH_YES
)
4342 found_prefix
= true;
4345 if (gfc_match ("elemental% ") == MATCH_YES
)
4347 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
4350 found_prefix
= true;
4353 if (gfc_match ("pure% ") == MATCH_YES
)
4355 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4358 found_prefix
= true;
4361 if (gfc_match ("recursive% ") == MATCH_YES
)
4363 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
4366 found_prefix
= true;
4369 /* IMPURE is a somewhat special case, as it needs not set an actual
4370 attribute but rather only prevents ELEMENTAL routines from being
4371 automatically PURE. */
4372 if (gfc_match ("impure% ") == MATCH_YES
)
4374 if (gfc_notify_std (GFC_STD_F2008
,
4375 "Fortran 2008: IMPURE procedure at %C")
4380 found_prefix
= true;
4383 while (found_prefix
);
4385 /* IMPURE and PURE must not both appear, of course. */
4386 if (seen_impure
&& current_attr
.pure
)
4388 gfc_error ("PURE and IMPURE must not appear both at %C");
4392 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4393 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4395 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
4399 /* At this point, the next item is not a prefix. */
4400 gcc_assert (gfc_matching_prefix
);
4401 gfc_matching_prefix
= false;
4405 gcc_assert (gfc_matching_prefix
);
4406 gfc_matching_prefix
= false;
4411 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4414 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4416 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
4419 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
4422 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
4429 /* Match a formal argument list. */
4432 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4434 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4435 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4441 if (gfc_match_char ('(') != MATCH_YES
)
4448 if (gfc_match_char (')') == MATCH_YES
)
4453 if (gfc_match_char ('*') == MATCH_YES
)
4457 m
= gfc_match_name (name
);
4461 if (gfc_get_symbol (name
, NULL
, &sym
))
4465 p
= gfc_get_formal_arglist ();
4477 /* We don't add the VARIABLE flavor because the name could be a
4478 dummy procedure. We don't apply these attributes to formal
4479 arguments of statement functions. */
4480 if (sym
!= NULL
&& !st_flag
4481 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
4482 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
4488 /* The name of a program unit can be in a different namespace,
4489 so check for it explicitly. After the statement is accepted,
4490 the name is checked for especially in gfc_get_symbol(). */
4491 if (gfc_new_block
!= NULL
&& sym
!= NULL
4492 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4494 gfc_error ("Name '%s' at %C is the name of the procedure",
4500 if (gfc_match_char (')') == MATCH_YES
)
4503 m
= gfc_match_char (',');
4506 gfc_error ("Unexpected junk in formal argument list at %C");
4512 /* Check for duplicate symbols in the formal argument list. */
4515 for (p
= head
; p
->next
; p
= p
->next
)
4520 for (q
= p
->next
; q
; q
= q
->next
)
4521 if (p
->sym
== q
->sym
)
4523 gfc_error ("Duplicate symbol '%s' in formal argument list "
4524 "at %C", p
->sym
->name
);
4532 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
4542 gfc_free_formal_arglist (head
);
4547 /* Match a RESULT specification following a function declaration or
4548 ENTRY statement. Also matches the end-of-statement. */
4551 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4553 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4557 if (gfc_match (" result (") != MATCH_YES
)
4560 m
= gfc_match_name (name
);
4564 /* Get the right paren, and that's it because there could be the
4565 bind(c) attribute after the result clause. */
4566 if (gfc_match_char(')') != MATCH_YES
)
4568 /* TODO: should report the missing right paren here. */
4572 if (strcmp (function
->name
, name
) == 0)
4574 gfc_error ("RESULT variable at %C must be different than function name");
4578 if (gfc_get_symbol (name
, NULL
, &r
))
4581 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
4590 /* Match a function suffix, which could be a combination of a result
4591 clause and BIND(C), either one, or neither. The draft does not
4592 require them to come in a specific order. */
4595 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4597 match is_bind_c
; /* Found bind(c). */
4598 match is_result
; /* Found result clause. */
4599 match found_match
; /* Status of whether we've found a good match. */
4600 char peek_char
; /* Character we're going to peek at. */
4601 bool allow_binding_name
;
4603 /* Initialize to having found nothing. */
4604 found_match
= MATCH_NO
;
4605 is_bind_c
= MATCH_NO
;
4606 is_result
= MATCH_NO
;
4608 /* Get the next char to narrow between result and bind(c). */
4609 gfc_gobble_whitespace ();
4610 peek_char
= gfc_peek_ascii_char ();
4612 /* C binding names are not allowed for internal procedures. */
4613 if (gfc_current_state () == COMP_CONTAINS
4614 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4615 allow_binding_name
= false;
4617 allow_binding_name
= true;
4622 /* Look for result clause. */
4623 is_result
= match_result (sym
, result
);
4624 if (is_result
== MATCH_YES
)
4626 /* Now see if there is a bind(c) after it. */
4627 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4628 /* We've found the result clause and possibly bind(c). */
4629 found_match
= MATCH_YES
;
4632 /* This should only be MATCH_ERROR. */
4633 found_match
= is_result
;
4636 /* Look for bind(c) first. */
4637 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4638 if (is_bind_c
== MATCH_YES
)
4640 /* Now see if a result clause followed it. */
4641 is_result
= match_result (sym
, result
);
4642 found_match
= MATCH_YES
;
4646 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4647 found_match
= MATCH_ERROR
;
4651 gfc_error ("Unexpected junk after function declaration at %C");
4652 found_match
= MATCH_ERROR
;
4656 if (is_bind_c
== MATCH_YES
)
4658 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4659 if (gfc_current_state () == COMP_CONTAINS
4660 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4661 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4662 "at %L may not be specified for an internal "
4663 "procedure", &gfc_current_locus
)
4667 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4676 /* Procedure pointer return value without RESULT statement:
4677 Add "hidden" result variable named "ppr@". */
4680 add_hidden_procptr_result (gfc_symbol
*sym
)
4684 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4687 /* First usage case: PROCEDURE and EXTERNAL statements. */
4688 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4689 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4690 && sym
->attr
.external
;
4691 /* Second usage case: INTERFACE statements. */
4692 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4693 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4694 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4700 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
4704 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
4705 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4706 st2
->n
.sym
= stree
->n
.sym
;
4708 sym
->result
= stree
->n
.sym
;
4710 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4711 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4712 sym
->result
->attr
.external
= sym
->attr
.external
;
4713 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4714 sym
->result
->ts
= sym
->ts
;
4715 sym
->attr
.proc_pointer
= 0;
4716 sym
->attr
.pointer
= 0;
4717 sym
->attr
.external
= 0;
4718 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4720 sym
->result
->attr
.pointer
= 0;
4721 sym
->result
->attr
.proc_pointer
= 1;
4724 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4726 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4727 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4728 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4729 && sym
== gfc_current_ns
->proc_name
4730 && sym
== sym
->result
->ns
->proc_name
4731 && strcmp ("ppr@", sym
->result
->name
) == 0)
4733 sym
->result
->attr
.proc_pointer
= 1;
4734 sym
->attr
.pointer
= 0;
4742 /* Match the interface for a PROCEDURE declaration,
4743 including brackets (R1212). */
4746 match_procedure_interface (gfc_symbol
**proc_if
)
4750 locus old_loc
, entry_loc
;
4751 gfc_namespace
*old_ns
= gfc_current_ns
;
4752 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4754 old_loc
= entry_loc
= gfc_current_locus
;
4755 gfc_clear_ts (¤t_ts
);
4757 if (gfc_match (" (") != MATCH_YES
)
4759 gfc_current_locus
= entry_loc
;
4763 /* Get the type spec. for the procedure interface. */
4764 old_loc
= gfc_current_locus
;
4765 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4766 gfc_gobble_whitespace ();
4767 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4770 if (m
== MATCH_ERROR
)
4773 /* Procedure interface is itself a procedure. */
4774 gfc_current_locus
= old_loc
;
4775 m
= gfc_match_name (name
);
4777 /* First look to see if it is already accessible in the current
4778 namespace because it is use associated or contained. */
4780 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
4783 /* If it is still not found, then try the parent namespace, if it
4784 exists and create the symbol there if it is still not found. */
4785 if (gfc_current_ns
->parent
)
4786 gfc_current_ns
= gfc_current_ns
->parent
;
4787 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
4790 gfc_current_ns
= old_ns
;
4791 *proc_if
= st
->n
.sym
;
4793 /* Various interface checks. */
4797 /* Resolve interface if possible. That way, attr.procedure is only set
4798 if it is declared by a later procedure-declaration-stmt, which is
4799 invalid per C1212. */
4800 while ((*proc_if
)->ts
.interface
)
4801 *proc_if
= (*proc_if
)->ts
.interface
;
4803 if ((*proc_if
)->generic
)
4805 gfc_error ("Interface '%s' at %C may not be generic",
4809 if ((*proc_if
)->attr
.proc
== PROC_ST_FUNCTION
)
4811 gfc_error ("Interface '%s' at %C may not be a statement function",
4815 /* Handle intrinsic procedures. */
4816 if (!((*proc_if
)->attr
.external
|| (*proc_if
)->attr
.use_assoc
4817 || (*proc_if
)->attr
.if_source
== IFSRC_IFBODY
)
4818 && (gfc_is_intrinsic ((*proc_if
), 0, gfc_current_locus
)
4819 || gfc_is_intrinsic ((*proc_if
), 1, gfc_current_locus
)))
4820 (*proc_if
)->attr
.intrinsic
= 1;
4821 if ((*proc_if
)->attr
.intrinsic
4822 && !gfc_intrinsic_actual_ok ((*proc_if
)->name
, 0))
4824 gfc_error ("Intrinsic procedure '%s' not allowed "
4825 "in PROCEDURE statement at %C", (*proc_if
)->name
);
4831 if (gfc_match (" )") != MATCH_YES
)
4833 gfc_current_locus
= entry_loc
;
4841 /* Match a PROCEDURE declaration (R1211). */
4844 match_procedure_decl (void)
4847 gfc_symbol
*sym
, *proc_if
= NULL
;
4849 gfc_expr
*initializer
= NULL
;
4851 /* Parse interface (with brackets). */
4852 m
= match_procedure_interface (&proc_if
);
4856 /* Parse attributes (with colons). */
4857 m
= match_attr_spec();
4858 if (m
== MATCH_ERROR
)
4861 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
4863 current_attr
.is_bind_c
= 1;
4864 has_name_equals
= 0;
4865 curr_binding_label
= NULL
;
4868 /* Get procedure symbols. */
4871 m
= gfc_match_symbol (&sym
, 0);
4874 else if (m
== MATCH_ERROR
)
4877 /* Add current_attr to the symbol attributes. */
4878 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4881 if (sym
->attr
.is_bind_c
)
4883 /* Check for C1218. */
4884 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4886 gfc_error ("BIND(C) attribute at %C requires "
4887 "an interface with BIND(C)");
4890 /* Check for C1217. */
4891 if (has_name_equals
&& sym
->attr
.pointer
)
4893 gfc_error ("BIND(C) procedure with NAME may not have "
4894 "POINTER attribute at %C");
4897 if (has_name_equals
&& sym
->attr
.dummy
)
4899 gfc_error ("Dummy procedure at %C may not have "
4900 "BIND(C) attribute with NAME");
4903 /* Set binding label for BIND(C). */
4904 if (set_binding_label (&sym
->binding_label
, sym
->name
, num
)
4909 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4912 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4915 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4918 /* Set interface. */
4919 if (proc_if
!= NULL
)
4921 if (sym
->ts
.type
!= BT_UNKNOWN
)
4923 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4924 sym
->name
, &gfc_current_locus
,
4925 gfc_basic_typename (sym
->ts
.type
));
4928 sym
->ts
.interface
= proc_if
;
4929 sym
->attr
.untyped
= 1;
4930 sym
->attr
.if_source
= IFSRC_IFBODY
;
4932 else if (current_ts
.type
!= BT_UNKNOWN
)
4934 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4936 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4937 sym
->ts
.interface
->ts
= current_ts
;
4938 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
4939 sym
->ts
.interface
->attr
.function
= 1;
4940 sym
->attr
.function
= 1;
4941 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4944 if (gfc_match (" =>") == MATCH_YES
)
4946 if (!current_attr
.pointer
)
4948 gfc_error ("Initialization at %C isn't for a pointer variable");
4953 m
= match_pointer_init (&initializer
, 1);
4957 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4963 gfc_set_sym_referenced (sym
);
4965 if (gfc_match_eos () == MATCH_YES
)
4967 if (gfc_match_char (',') != MATCH_YES
)
4972 gfc_error ("Syntax error in PROCEDURE statement at %C");
4976 /* Free stuff up and return. */
4977 gfc_free_expr (initializer
);
4983 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
4986 /* Match a procedure pointer component declaration (R445). */
4989 match_ppc_decl (void)
4992 gfc_symbol
*proc_if
= NULL
;
4996 gfc_expr
*initializer
= NULL
;
4997 gfc_typebound_proc
* tb
;
4998 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5000 /* Parse interface (with brackets). */
5001 m
= match_procedure_interface (&proc_if
);
5005 /* Parse attributes. */
5006 tb
= XCNEW (gfc_typebound_proc
);
5007 tb
->where
= gfc_current_locus
;
5008 m
= match_binding_attributes (tb
, false, true);
5009 if (m
== MATCH_ERROR
)
5012 gfc_clear_attr (¤t_attr
);
5013 current_attr
.procedure
= 1;
5014 current_attr
.proc_pointer
= 1;
5015 current_attr
.access
= tb
->access
;
5016 current_attr
.flavor
= FL_PROCEDURE
;
5018 /* Match the colons (required). */
5019 if (gfc_match (" ::") != MATCH_YES
)
5021 gfc_error ("Expected '::' after binding-attributes at %C");
5025 /* Check for C450. */
5026 if (!tb
->nopass
&& proc_if
== NULL
)
5028 gfc_error("NOPASS or explicit interface required at %C");
5032 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Procedure pointer "
5033 "component at %C") == FAILURE
)
5036 /* Match PPC names. */
5040 m
= gfc_match_name (name
);
5043 else if (m
== MATCH_ERROR
)
5046 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
5049 /* Add current_attr to the symbol attributes. */
5050 if (gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
) == FAILURE
)
5053 if (gfc_add_external (&c
->attr
, NULL
) == FAILURE
)
5056 if (gfc_add_proc (&c
->attr
, name
, NULL
) == FAILURE
)
5061 /* Set interface. */
5062 if (proc_if
!= NULL
)
5064 c
->ts
.interface
= proc_if
;
5065 c
->attr
.untyped
= 1;
5066 c
->attr
.if_source
= IFSRC_IFBODY
;
5068 else if (ts
.type
!= BT_UNKNOWN
)
5071 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5072 c
->ts
.interface
->ts
= ts
;
5073 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5074 c
->ts
.interface
->attr
.function
= 1;
5075 c
->attr
.function
= 1;
5076 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5079 if (gfc_match (" =>") == MATCH_YES
)
5081 m
= match_pointer_init (&initializer
, 1);
5084 gfc_free_expr (initializer
);
5087 c
->initializer
= initializer
;
5090 if (gfc_match_eos () == MATCH_YES
)
5092 if (gfc_match_char (',') != MATCH_YES
)
5097 gfc_error ("Syntax error in procedure pointer component at %C");
5102 /* Match a PROCEDURE declaration inside an interface (R1206). */
5105 match_procedure_in_interface (void)
5109 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5111 if (current_interface
.type
== INTERFACE_NAMELESS
5112 || current_interface
.type
== INTERFACE_ABSTRACT
)
5114 gfc_error ("PROCEDURE at %C must be in a generic interface");
5120 m
= gfc_match_name (name
);
5123 else if (m
== MATCH_ERROR
)
5125 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5128 if (gfc_add_interface (sym
) == FAILURE
)
5131 if (gfc_match_eos () == MATCH_YES
)
5133 if (gfc_match_char (',') != MATCH_YES
)
5140 gfc_error ("Syntax error in PROCEDURE statement at %C");
5145 /* General matcher for PROCEDURE declarations. */
5147 static match
match_procedure_in_type (void);
5150 gfc_match_procedure (void)
5154 switch (gfc_current_state ())
5159 case COMP_SUBROUTINE
:
5162 m
= match_procedure_decl ();
5164 case COMP_INTERFACE
:
5165 m
= match_procedure_in_interface ();
5168 m
= match_ppc_decl ();
5170 case COMP_DERIVED_CONTAINS
:
5171 m
= match_procedure_in_type ();
5180 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
5188 /* Warn if a matched procedure has the same name as an intrinsic; this is
5189 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5190 parser-state-stack to find out whether we're in a module. */
5193 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5197 in_module
= (gfc_state_stack
->previous
5198 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
5200 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5204 /* Match a function declaration. */
5207 gfc_match_function_decl (void)
5209 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5210 gfc_symbol
*sym
, *result
;
5214 match found_match
; /* Status returned by match func. */
5216 if (gfc_current_state () != COMP_NONE
5217 && gfc_current_state () != COMP_INTERFACE
5218 && gfc_current_state () != COMP_CONTAINS
)
5221 gfc_clear_ts (¤t_ts
);
5223 old_loc
= gfc_current_locus
;
5225 m
= gfc_match_prefix (¤t_ts
);
5228 gfc_current_locus
= old_loc
;
5232 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5234 gfc_current_locus
= old_loc
;
5237 if (get_proc_name (name
, &sym
, false))
5240 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5243 gfc_new_block
= sym
;
5245 m
= gfc_match_formal_arglist (sym
, 0, 0);
5248 gfc_error ("Expected formal argument list in function "
5249 "definition at %C");
5253 else if (m
== MATCH_ERROR
)
5258 /* According to the draft, the bind(c) and result clause can
5259 come in either order after the formal_arg_list (i.e., either
5260 can be first, both can exist together or by themselves or neither
5261 one). Therefore, the match_result can't match the end of the
5262 string, and check for the bind(c) or result clause in either order. */
5263 found_match
= gfc_match_eos ();
5265 /* Make sure that it isn't already declared as BIND(C). If it is, it
5266 must have been marked BIND(C) with a BIND(C) attribute and that is
5267 not allowed for procedures. */
5268 if (sym
->attr
.is_bind_c
== 1)
5270 sym
->attr
.is_bind_c
= 0;
5271 if (sym
->old_symbol
!= NULL
)
5272 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5273 "variables or common blocks",
5274 &(sym
->old_symbol
->declared_at
));
5276 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5277 "variables or common blocks", &gfc_current_locus
);
5280 if (found_match
!= MATCH_YES
)
5282 /* If we haven't found the end-of-statement, look for a suffix. */
5283 suffix_match
= gfc_match_suffix (sym
, &result
);
5284 if (suffix_match
== MATCH_YES
)
5285 /* Need to get the eos now. */
5286 found_match
= gfc_match_eos ();
5288 found_match
= suffix_match
;
5291 if(found_match
!= MATCH_YES
)
5295 /* Make changes to the symbol. */
5298 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5301 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
5302 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5305 /* Delay matching the function characteristics until after the
5306 specification block by signalling kind=-1. */
5307 sym
->declared_at
= old_loc
;
5308 if (current_ts
.type
!= BT_UNKNOWN
)
5309 current_ts
.kind
= -1;
5311 current_ts
.kind
= 0;
5315 if (current_ts
.type
!= BT_UNKNOWN
5316 && gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
5322 if (current_ts
.type
!= BT_UNKNOWN
5323 && gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
)
5326 sym
->result
= result
;
5329 /* Warn if this procedure has the same name as an intrinsic. */
5330 warn_intrinsic_shadow (sym
, true);
5336 gfc_current_locus
= old_loc
;
5341 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5342 pass the name of the entry, rather than the gfc_current_block name, and
5343 to return false upon finding an existing global entry. */
5346 add_global_entry (const char *name
, int sub
)
5349 enum gfc_symbol_type type
;
5351 s
= gfc_get_gsymbol(name
);
5352 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5355 || (s
->type
!= GSYM_UNKNOWN
5356 && s
->type
!= type
))
5357 gfc_global_used(s
, NULL
);
5361 s
->where
= gfc_current_locus
;
5363 s
->ns
= gfc_current_ns
;
5370 /* Match an ENTRY statement. */
5373 gfc_match_entry (void)
5378 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5379 gfc_compile_state state
;
5383 bool module_procedure
;
5387 m
= gfc_match_name (name
);
5391 if (gfc_notify_std (GFC_STD_F2008_OBS
, "Fortran 2008 obsolescent feature: "
5392 "ENTRY statement at %C") == FAILURE
)
5395 state
= gfc_current_state ();
5396 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5401 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5404 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5406 case COMP_BLOCK_DATA
:
5407 gfc_error ("ENTRY statement at %C cannot appear within "
5410 case COMP_INTERFACE
:
5411 gfc_error ("ENTRY statement at %C cannot appear within "
5415 gfc_error ("ENTRY statement at %C cannot appear within "
5416 "a DERIVED TYPE block");
5419 gfc_error ("ENTRY statement at %C cannot appear within "
5420 "an IF-THEN block");
5423 case COMP_DO_CONCURRENT
:
5424 gfc_error ("ENTRY statement at %C cannot appear within "
5428 gfc_error ("ENTRY statement at %C cannot appear within "
5432 gfc_error ("ENTRY statement at %C cannot appear within "
5436 gfc_error ("ENTRY statement at %C cannot appear within "
5440 gfc_error ("ENTRY statement at %C cannot appear within "
5441 "a contained subprogram");
5444 gfc_internal_error ("gfc_match_entry(): Bad state");
5449 module_procedure
= gfc_current_ns
->parent
!= NULL
5450 && gfc_current_ns
->parent
->proc_name
5451 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5454 if (gfc_current_ns
->parent
!= NULL
5455 && gfc_current_ns
->parent
->proc_name
5456 && !module_procedure
)
5458 gfc_error("ENTRY statement at %C cannot appear in a "
5459 "contained procedure");
5463 /* Module function entries need special care in get_proc_name
5464 because previous references within the function will have
5465 created symbols attached to the current namespace. */
5466 if (get_proc_name (name
, &entry
,
5467 gfc_current_ns
->parent
!= NULL
5468 && module_procedure
))
5471 proc
= gfc_current_block ();
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 (entry
->attr
.is_bind_c
== 1)
5478 entry
->attr
.is_bind_c
= 0;
5479 if (entry
->old_symbol
!= NULL
)
5480 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5481 "variables or common blocks",
5482 &(entry
->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 /* Check what next non-whitespace character is so we can tell if there
5489 is the required parens if we have a BIND(C). */
5490 gfc_gobble_whitespace ();
5491 peek_char
= gfc_peek_ascii_char ();
5493 if (state
== COMP_SUBROUTINE
)
5495 /* An entry in a subroutine. */
5496 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
5499 m
= gfc_match_formal_arglist (entry
, 0, 1);
5503 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5504 never be an internal procedure. */
5505 is_bind_c
= gfc_match_bind_c (entry
, true);
5506 if (is_bind_c
== MATCH_ERROR
)
5508 if (is_bind_c
== MATCH_YES
)
5510 if (peek_char
!= '(')
5512 gfc_error ("Missing required parentheses before BIND(C) at %C");
5515 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
5520 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5521 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5526 /* An entry in a function.
5527 We need to take special care because writing
5532 ENTRY f() RESULT (r)
5534 ENTRY f RESULT (r). */
5535 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
5538 old_loc
= gfc_current_locus
;
5539 if (gfc_match_eos () == MATCH_YES
)
5541 gfc_current_locus
= old_loc
;
5542 /* Match the empty argument list, and add the interface to
5544 m
= gfc_match_formal_arglist (entry
, 0, 1);
5547 m
= gfc_match_formal_arglist (entry
, 0, 0);
5554 if (gfc_match_eos () == MATCH_YES
)
5556 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5557 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5560 entry
->result
= entry
;
5564 m
= gfc_match_suffix (entry
, &result
);
5566 gfc_syntax_error (ST_ENTRY
);
5572 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
5573 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
5574 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
5577 entry
->result
= result
;
5581 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
5582 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
5584 entry
->result
= entry
;
5589 if (gfc_match_eos () != MATCH_YES
)
5591 gfc_syntax_error (ST_ENTRY
);
5595 entry
->attr
.recursive
= proc
->attr
.recursive
;
5596 entry
->attr
.elemental
= proc
->attr
.elemental
;
5597 entry
->attr
.pure
= proc
->attr
.pure
;
5599 el
= gfc_get_entry_list ();
5601 el
->next
= gfc_current_ns
->entries
;
5602 gfc_current_ns
->entries
= el
;
5604 el
->id
= el
->next
->id
+ 1;
5608 new_st
.op
= EXEC_ENTRY
;
5609 new_st
.ext
.entry
= el
;
5615 /* Match a subroutine statement, including optional prefixes. */
5618 gfc_match_subroutine (void)
5620 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5625 bool allow_binding_name
;
5627 if (gfc_current_state () != COMP_NONE
5628 && gfc_current_state () != COMP_INTERFACE
5629 && gfc_current_state () != COMP_CONTAINS
)
5632 m
= gfc_match_prefix (NULL
);
5636 m
= gfc_match ("subroutine% %n", name
);
5640 if (get_proc_name (name
, &sym
, false))
5643 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5644 the symbol existed before. */
5645 sym
->declared_at
= gfc_current_locus
;
5647 if (add_hidden_procptr_result (sym
) == SUCCESS
)
5650 gfc_new_block
= sym
;
5652 /* Check what next non-whitespace character is so we can tell if there
5653 is the required parens if we have a BIND(C). */
5654 gfc_gobble_whitespace ();
5655 peek_char
= gfc_peek_ascii_char ();
5657 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
5660 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
5663 /* Make sure that it isn't already declared as BIND(C). If it is, it
5664 must have been marked BIND(C) with a BIND(C) attribute and that is
5665 not allowed for procedures. */
5666 if (sym
->attr
.is_bind_c
== 1)
5668 sym
->attr
.is_bind_c
= 0;
5669 if (sym
->old_symbol
!= NULL
)
5670 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5671 "variables or common blocks",
5672 &(sym
->old_symbol
->declared_at
));
5674 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5675 "variables or common blocks", &gfc_current_locus
);
5678 /* C binding names are not allowed for internal procedures. */
5679 if (gfc_current_state () == COMP_CONTAINS
5680 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5681 allow_binding_name
= false;
5683 allow_binding_name
= true;
5685 /* Here, we are just checking if it has the bind(c) attribute, and if
5686 so, then we need to make sure it's all correct. If it doesn't,
5687 we still need to continue matching the rest of the subroutine line. */
5688 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
5689 if (is_bind_c
== MATCH_ERROR
)
5691 /* There was an attempt at the bind(c), but it was wrong. An
5692 error message should have been printed w/in the gfc_match_bind_c
5693 so here we'll just return the MATCH_ERROR. */
5697 if (is_bind_c
== MATCH_YES
)
5699 /* The following is allowed in the Fortran 2008 draft. */
5700 if (gfc_current_state () == COMP_CONTAINS
5701 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5702 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
5703 "at %L may not be specified for an internal "
5704 "procedure", &gfc_current_locus
)
5708 if (peek_char
!= '(')
5710 gfc_error ("Missing required parentheses before BIND(C) at %C");
5713 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
5718 if (gfc_match_eos () != MATCH_YES
)
5720 gfc_syntax_error (ST_SUBROUTINE
);
5724 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
5727 /* Warn if it has the same name as an intrinsic. */
5728 warn_intrinsic_shadow (sym
, false);
5734 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5735 given, and set the binding label in either the given symbol (if not
5736 NULL), or in the current_ts. The symbol may be NULL because we may
5737 encounter the BIND(C) before the declaration itself. Return
5738 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5739 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5740 or MATCH_YES if the specifier was correct and the binding label and
5741 bind(c) fields were set correctly for the given symbol or the
5742 current_ts. If allow_binding_name is false, no binding name may be
5746 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5748 /* binding label, if exists */
5749 const char* binding_label
= NULL
;
5753 /* Initialize the flag that specifies whether we encountered a NAME=
5754 specifier or not. */
5755 has_name_equals
= 0;
5757 /* This much we have to be able to match, in this order, if
5758 there is a bind(c) label. */
5759 if (gfc_match (" bind ( c ") != MATCH_YES
)
5762 /* Now see if there is a binding label, or if we've reached the
5763 end of the bind(c) attribute without one. */
5764 if (gfc_match_char (',') == MATCH_YES
)
5766 if (gfc_match (" name = ") != MATCH_YES
)
5768 gfc_error ("Syntax error in NAME= specifier for binding label "
5770 /* should give an error message here */
5774 has_name_equals
= 1;
5776 /* Get the opening quote. */
5777 double_quote
= MATCH_YES
;
5778 single_quote
= MATCH_YES
;
5779 double_quote
= gfc_match_char ('"');
5780 if (double_quote
!= MATCH_YES
)
5781 single_quote
= gfc_match_char ('\'');
5782 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5784 gfc_error ("Syntax error in NAME= specifier for binding label "
5789 /* Grab the binding label, using functions that will not lower
5790 case the names automatically. */
5791 if (gfc_match_name_C (&binding_label
) != MATCH_YES
)
5794 /* Get the closing quotation. */
5795 if (double_quote
== MATCH_YES
)
5797 if (gfc_match_char ('"') != MATCH_YES
)
5799 gfc_error ("Missing closing quote '\"' for binding label at %C");
5800 /* User started string with '"' so looked to match it. */
5806 if (gfc_match_char ('\'') != MATCH_YES
)
5808 gfc_error ("Missing closing quote '\'' for binding label at %C");
5809 /* User started string with "'" char. */
5815 /* Get the required right paren. */
5816 if (gfc_match_char (')') != MATCH_YES
)
5818 gfc_error ("Missing closing paren for binding label at %C");
5822 if (has_name_equals
&& !allow_binding_name
)
5824 gfc_error ("No binding name is allowed in BIND(C) at %C");
5828 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5830 gfc_error ("For dummy procedure %s, no binding name is "
5831 "allowed in BIND(C) at %C", sym
->name
);
5836 /* Save the binding label to the symbol. If sym is null, we're
5837 probably matching the typespec attributes of a declaration and
5838 haven't gotten the name yet, and therefore, no symbol yet. */
5842 sym
->binding_label
= binding_label
;
5844 curr_binding_label
= binding_label
;
5846 else if (allow_binding_name
)
5848 /* No binding label, but if symbol isn't null, we
5849 can set the label for it here.
5850 If name="" or allow_binding_name is false, no C binding name is
5852 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5853 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
5856 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5857 && current_interface
.type
== INTERFACE_ABSTRACT
)
5859 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5867 /* Return nonzero if we're currently compiling a contained procedure. */
5870 contained_procedure (void)
5872 gfc_state_data
*s
= gfc_state_stack
;
5874 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5875 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5881 /* Set the kind of each enumerator. The kind is selected such that it is
5882 interoperable with the corresponding C enumeration type, making
5883 sure that -fshort-enums is honored. */
5888 enumerator_history
*current_history
= NULL
;
5892 if (max_enum
== NULL
|| enum_history
== NULL
)
5895 if (!flag_short_enums
)
5901 kind
= gfc_integer_kinds
[i
++].kind
;
5903 while (kind
< gfc_c_int_kind
5904 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5907 current_history
= enum_history
;
5908 while (current_history
!= NULL
)
5910 current_history
->sym
->ts
.kind
= kind
;
5911 current_history
= current_history
->next
;
5916 /* Match any of the various end-block statements. Returns the type of
5917 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5918 and END BLOCK statements cannot be replaced by a single END statement. */
5921 gfc_match_end (gfc_statement
*st
)
5923 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5924 gfc_compile_state state
;
5926 const char *block_name
;
5931 old_loc
= gfc_current_locus
;
5932 if (gfc_match ("end") != MATCH_YES
)
5935 state
= gfc_current_state ();
5936 block_name
= gfc_current_block () == NULL
5937 ? NULL
: gfc_current_block ()->name
;
5941 case COMP_ASSOCIATE
:
5943 if (!strncmp (block_name
, "block@", strlen("block@")))
5948 case COMP_DERIVED_CONTAINS
:
5949 state
= gfc_state_stack
->previous
->state
;
5950 block_name
= gfc_state_stack
->previous
->sym
== NULL
5951 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5962 *st
= ST_END_PROGRAM
;
5963 target
= " program";
5967 case COMP_SUBROUTINE
:
5968 *st
= ST_END_SUBROUTINE
;
5969 target
= " subroutine";
5970 eos_ok
= !contained_procedure ();
5974 *st
= ST_END_FUNCTION
;
5975 target
= " function";
5976 eos_ok
= !contained_procedure ();
5979 case COMP_BLOCK_DATA
:
5980 *st
= ST_END_BLOCK_DATA
;
5981 target
= " block data";
5986 *st
= ST_END_MODULE
;
5991 case COMP_INTERFACE
:
5992 *st
= ST_END_INTERFACE
;
5993 target
= " interface";
5998 case COMP_DERIVED_CONTAINS
:
6004 case COMP_ASSOCIATE
:
6005 *st
= ST_END_ASSOCIATE
;
6006 target
= " associate";
6023 case COMP_DO_CONCURRENT
:
6030 *st
= ST_END_CRITICAL
;
6031 target
= " critical";
6036 case COMP_SELECT_TYPE
:
6037 *st
= ST_END_SELECT
;
6043 *st
= ST_END_FORALL
;
6058 last_initializer
= NULL
;
6060 gfc_free_enum_history ();
6064 gfc_error ("Unexpected END statement at %C");
6068 if (gfc_match_eos () == MATCH_YES
)
6070 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6072 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: END statement "
6073 "instead of %s statement at %L",
6074 gfc_ascii_statement (*st
), &old_loc
) == FAILURE
)
6079 /* We would have required END [something]. */
6080 gfc_error ("%s statement expected at %L",
6081 gfc_ascii_statement (*st
), &old_loc
);
6088 /* Verify that we've got the sort of end-block that we're expecting. */
6089 if (gfc_match (target
) != MATCH_YES
)
6091 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
6095 /* If we're at the end, make sure a block name wasn't required. */
6096 if (gfc_match_eos () == MATCH_YES
)
6099 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6100 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6101 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6107 gfc_error ("Expected block name of '%s' in %s statement at %C",
6108 block_name
, gfc_ascii_statement (*st
));
6113 /* END INTERFACE has a special handler for its several possible endings. */
6114 if (*st
== ST_END_INTERFACE
)
6115 return gfc_match_end_interface ();
6117 /* We haven't hit the end of statement, so what is left must be an
6119 m
= gfc_match_space ();
6121 m
= gfc_match_name (name
);
6124 gfc_error ("Expected terminating name at %C");
6128 if (block_name
== NULL
)
6131 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6133 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
6134 gfc_ascii_statement (*st
));
6137 /* Procedure pointer as function result. */
6138 else if (strcmp (block_name
, "ppr@") == 0
6139 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6141 gfc_error ("Expected label '%s' for %s statement at %C",
6142 gfc_current_block ()->ns
->proc_name
->name
,
6143 gfc_ascii_statement (*st
));
6147 if (gfc_match_eos () == MATCH_YES
)
6151 gfc_syntax_error (*st
);
6154 gfc_current_locus
= old_loc
;
6160 /***************** Attribute declaration statements ****************/
6162 /* Set the attribute of a single variable. */
6167 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6175 m
= gfc_match_name (name
);
6179 if (find_special (name
, &sym
, false))
6182 if (check_function_name (name
) == FAILURE
)
6188 var_locus
= gfc_current_locus
;
6190 /* Deal with possible array specification for certain attributes. */
6191 if (current_attr
.dimension
6192 || current_attr
.codimension
6193 || current_attr
.allocatable
6194 || current_attr
.pointer
6195 || current_attr
.target
)
6197 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6198 !current_attr
.dimension
6199 && !current_attr
.pointer
6200 && !current_attr
.target
);
6201 if (m
== MATCH_ERROR
)
6204 if (current_attr
.dimension
&& m
== MATCH_NO
)
6206 gfc_error ("Missing array specification at %L in DIMENSION "
6207 "statement", &var_locus
);
6212 if (current_attr
.dimension
&& sym
->value
)
6214 gfc_error ("Dimensions specified for %s at %L after its "
6215 "initialisation", sym
->name
, &var_locus
);
6220 if (current_attr
.codimension
&& m
== MATCH_NO
)
6222 gfc_error ("Missing array specification at %L in CODIMENSION "
6223 "statement", &var_locus
);
6228 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6229 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6231 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6237 /* Update symbol table. DIMENSION attribute is set in
6238 gfc_set_array_spec(). For CLASS variables, this must be applied
6239 to the first component, or '_data' field. */
6240 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6242 if (gfc_copy_attr (&CLASS_DATA (sym
)->attr
, ¤t_attr
, &var_locus
)
6251 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6252 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
6259 if (sym
->ts
.type
== BT_CLASS
6260 && gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
, false) == FAILURE
)
6266 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
6272 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6274 /* Fix the array spec. */
6275 m
= gfc_mod_pointee_as (sym
->as
);
6276 if (m
== MATCH_ERROR
)
6280 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
6286 if ((current_attr
.external
|| current_attr
.intrinsic
)
6287 && sym
->attr
.flavor
!= FL_PROCEDURE
6288 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
6294 add_hidden_procptr_result (sym
);
6299 gfc_free_array_spec (as
);
6304 /* Generic attribute declaration subroutine. Used for attributes that
6305 just have a list of names. */
6312 /* Gobble the optional double colon, by simply ignoring the result
6322 if (gfc_match_eos () == MATCH_YES
)
6328 if (gfc_match_char (',') != MATCH_YES
)
6330 gfc_error ("Unexpected character in variable list at %C");
6340 /* This routine matches Cray Pointer declarations of the form:
6341 pointer ( <pointer>, <pointee> )
6343 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6344 The pointer, if already declared, should be an integer. Otherwise, we
6345 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6346 be either a scalar, or an array declaration. No space is allocated for
6347 the pointee. For the statement
6348 pointer (ipt, ar(10))
6349 any subsequent uses of ar will be translated (in C-notation) as
6350 ar(i) => ((<type> *) ipt)(i)
6351 After gimplification, pointee variable will disappear in the code. */
6354 cray_pointer_decl (void)
6357 gfc_array_spec
*as
= NULL
;
6358 gfc_symbol
*cptr
; /* Pointer symbol. */
6359 gfc_symbol
*cpte
; /* Pointee symbol. */
6365 if (gfc_match_char ('(') != MATCH_YES
)
6367 gfc_error ("Expected '(' at %C");
6371 /* Match pointer. */
6372 var_locus
= gfc_current_locus
;
6373 gfc_clear_attr (¤t_attr
);
6374 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6375 current_ts
.type
= BT_INTEGER
;
6376 current_ts
.kind
= gfc_index_integer_kind
;
6378 m
= gfc_match_symbol (&cptr
, 0);
6381 gfc_error ("Expected variable name at %C");
6385 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
6388 gfc_set_sym_referenced (cptr
);
6390 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6392 cptr
->ts
.type
= BT_INTEGER
;
6393 cptr
->ts
.kind
= gfc_index_integer_kind
;
6395 else if (cptr
->ts
.type
!= BT_INTEGER
)
6397 gfc_error ("Cray pointer at %C must be an integer");
6400 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6401 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6402 " memory addresses require %d bytes",
6403 cptr
->ts
.kind
, gfc_index_integer_kind
);
6405 if (gfc_match_char (',') != MATCH_YES
)
6407 gfc_error ("Expected \",\" at %C");
6411 /* Match Pointee. */
6412 var_locus
= gfc_current_locus
;
6413 gfc_clear_attr (¤t_attr
);
6414 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6415 current_ts
.type
= BT_UNKNOWN
;
6416 current_ts
.kind
= 0;
6418 m
= gfc_match_symbol (&cpte
, 0);
6421 gfc_error ("Expected variable name at %C");
6425 /* Check for an optional array spec. */
6426 m
= gfc_match_array_spec (&as
, true, false);
6427 if (m
== MATCH_ERROR
)
6429 gfc_free_array_spec (as
);
6432 else if (m
== MATCH_NO
)
6434 gfc_free_array_spec (as
);
6438 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
6441 gfc_set_sym_referenced (cpte
);
6443 if (cpte
->as
== NULL
)
6445 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
6446 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6448 else if (as
!= NULL
)
6450 gfc_error ("Duplicate array spec for Cray pointee at %C");
6451 gfc_free_array_spec (as
);
6457 if (cpte
->as
!= NULL
)
6459 /* Fix array spec. */
6460 m
= gfc_mod_pointee_as (cpte
->as
);
6461 if (m
== MATCH_ERROR
)
6465 /* Point the Pointee at the Pointer. */
6466 cpte
->cp_pointer
= cptr
;
6468 if (gfc_match_char (')') != MATCH_YES
)
6470 gfc_error ("Expected \")\" at %C");
6473 m
= gfc_match_char (',');
6475 done
= true; /* Stop searching for more declarations. */
6479 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6480 || gfc_match_eos () != MATCH_YES
)
6482 gfc_error ("Expected \",\" or end of statement at %C");
6490 gfc_match_external (void)
6493 gfc_clear_attr (¤t_attr
);
6494 current_attr
.external
= 1;
6496 return attr_decl ();
6501 gfc_match_intent (void)
6505 /* This is not allowed within a BLOCK construct! */
6506 if (gfc_current_state () == COMP_BLOCK
)
6508 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6512 intent
= match_intent_spec ();
6513 if (intent
== INTENT_UNKNOWN
)
6516 gfc_clear_attr (¤t_attr
);
6517 current_attr
.intent
= intent
;
6519 return attr_decl ();
6524 gfc_match_intrinsic (void)
6527 gfc_clear_attr (¤t_attr
);
6528 current_attr
.intrinsic
= 1;
6530 return attr_decl ();
6535 gfc_match_optional (void)
6537 /* This is not allowed within a BLOCK construct! */
6538 if (gfc_current_state () == COMP_BLOCK
)
6540 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6544 gfc_clear_attr (¤t_attr
);
6545 current_attr
.optional
= 1;
6547 return attr_decl ();
6552 gfc_match_pointer (void)
6554 gfc_gobble_whitespace ();
6555 if (gfc_peek_ascii_char () == '(')
6557 if (!gfc_option
.flag_cray_pointer
)
6559 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6563 return cray_pointer_decl ();
6567 gfc_clear_attr (¤t_attr
);
6568 current_attr
.pointer
= 1;
6570 return attr_decl ();
6576 gfc_match_allocatable (void)
6578 gfc_clear_attr (¤t_attr
);
6579 current_attr
.allocatable
= 1;
6581 return attr_decl ();
6586 gfc_match_codimension (void)
6588 gfc_clear_attr (¤t_attr
);
6589 current_attr
.codimension
= 1;
6591 return attr_decl ();
6596 gfc_match_contiguous (void)
6598 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: CONTIGUOUS statement at %C")
6602 gfc_clear_attr (¤t_attr
);
6603 current_attr
.contiguous
= 1;
6605 return attr_decl ();
6610 gfc_match_dimension (void)
6612 gfc_clear_attr (¤t_attr
);
6613 current_attr
.dimension
= 1;
6615 return attr_decl ();
6620 gfc_match_target (void)
6622 gfc_clear_attr (¤t_attr
);
6623 current_attr
.target
= 1;
6625 return attr_decl ();
6629 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6633 access_attr_decl (gfc_statement st
)
6635 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6636 interface_type type
;
6638 gfc_symbol
*sym
, *dt_sym
;
6639 gfc_intrinsic_op op
;
6642 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6647 m
= gfc_match_generic_spec (&type
, name
, &op
);
6650 if (m
== MATCH_ERROR
)
6655 case INTERFACE_NAMELESS
:
6656 case INTERFACE_ABSTRACT
:
6659 case INTERFACE_GENERIC
:
6660 if (gfc_get_symbol (name
, NULL
, &sym
))
6663 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
6664 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
6665 sym
->name
, NULL
) == FAILURE
)
6668 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
6669 && gfc_add_access (&dt_sym
->attr
,
6670 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
6672 sym
->name
, NULL
) == FAILURE
)
6677 case INTERFACE_INTRINSIC_OP
:
6678 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
6680 gfc_intrinsic_op other_op
;
6682 gfc_current_ns
->operator_access
[op
] =
6683 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6685 /* Handle the case if there is another op with the same
6686 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6687 other_op
= gfc_equivalent_op (op
);
6689 if (other_op
!= INTRINSIC_NONE
)
6690 gfc_current_ns
->operator_access
[other_op
] =
6691 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6696 gfc_error ("Access specification of the %s operator at %C has "
6697 "already been specified", gfc_op2string (op
));
6703 case INTERFACE_USER_OP
:
6704 uop
= gfc_get_uop (name
);
6706 if (uop
->access
== ACCESS_UNKNOWN
)
6708 uop
->access
= (st
== ST_PUBLIC
)
6709 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
6713 gfc_error ("Access specification of the .%s. operator at %C "
6714 "has already been specified", sym
->name
);
6721 if (gfc_match_char (',') == MATCH_NO
)
6725 if (gfc_match_eos () != MATCH_YES
)
6730 gfc_syntax_error (st
);
6738 gfc_match_protected (void)
6743 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6745 gfc_error ("PROTECTED at %C only allowed in specification "
6746 "part of a module");
6751 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
6755 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6760 if (gfc_match_eos () == MATCH_YES
)
6765 m
= gfc_match_symbol (&sym
, 0);
6769 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6782 if (gfc_match_eos () == MATCH_YES
)
6784 if (gfc_match_char (',') != MATCH_YES
)
6791 gfc_error ("Syntax error in PROTECTED statement at %C");
6796 /* The PRIVATE statement is a bit weird in that it can be an attribute
6797 declaration, but also works as a standalone statement inside of a
6798 type declaration or a module. */
6801 gfc_match_private (gfc_statement
*st
)
6804 if (gfc_match ("private") != MATCH_YES
)
6807 if (gfc_current_state () != COMP_MODULE
6808 && !(gfc_current_state () == COMP_DERIVED
6809 && gfc_state_stack
->previous
6810 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
6811 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6812 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
6813 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
6815 gfc_error ("PRIVATE statement at %C is only allowed in the "
6816 "specification part of a module");
6820 if (gfc_current_state () == COMP_DERIVED
)
6822 if (gfc_match_eos () == MATCH_YES
)
6828 gfc_syntax_error (ST_PRIVATE
);
6832 if (gfc_match_eos () == MATCH_YES
)
6839 return access_attr_decl (ST_PRIVATE
);
6844 gfc_match_public (gfc_statement
*st
)
6847 if (gfc_match ("public") != MATCH_YES
)
6850 if (gfc_current_state () != COMP_MODULE
)
6852 gfc_error ("PUBLIC statement at %C is only allowed in the "
6853 "specification part of a module");
6857 if (gfc_match_eos () == MATCH_YES
)
6864 return access_attr_decl (ST_PUBLIC
);
6868 /* Workhorse for gfc_match_parameter. */
6878 m
= gfc_match_symbol (&sym
, 0);
6880 gfc_error ("Expected variable name at %C in PARAMETER statement");
6885 if (gfc_match_char ('=') == MATCH_NO
)
6887 gfc_error ("Expected = sign in PARAMETER statement at %C");
6891 m
= gfc_match_init_expr (&init
);
6893 gfc_error ("Expected expression at %C in PARAMETER statement");
6897 if (sym
->ts
.type
== BT_UNKNOWN
6898 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6904 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6905 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6913 gfc_error ("Initializing already initialized variable at %C");
6918 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
6919 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6922 gfc_free_expr (init
);
6927 /* Match a parameter statement, with the weird syntax that these have. */
6930 gfc_match_parameter (void)
6934 if (gfc_match_char ('(') == MATCH_NO
)
6943 if (gfc_match (" )%t") == MATCH_YES
)
6946 if (gfc_match_char (',') != MATCH_YES
)
6948 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6958 /* Save statements have a special syntax. */
6961 gfc_match_save (void)
6963 char n
[GFC_MAX_SYMBOL_LEN
+1];
6968 if (gfc_match_eos () == MATCH_YES
)
6970 if (gfc_current_ns
->seen_save
)
6972 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6973 "follows previous SAVE statement")
6978 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6982 if (gfc_current_ns
->save_all
)
6984 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6985 "blanket SAVE statement")
6994 m
= gfc_match_symbol (&sym
, 0);
6998 if (gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
6999 &gfc_current_locus
) == FAILURE
)
7010 m
= gfc_match (" / %n /", &n
);
7011 if (m
== MATCH_ERROR
)
7016 c
= gfc_get_common (n
, 0);
7019 gfc_current_ns
->seen_save
= 1;
7022 if (gfc_match_eos () == MATCH_YES
)
7024 if (gfc_match_char (',') != MATCH_YES
)
7031 gfc_error ("Syntax error in SAVE statement at %C");
7037 gfc_match_value (void)
7042 /* This is not allowed within a BLOCK construct! */
7043 if (gfc_current_state () == COMP_BLOCK
)
7045 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7049 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
7053 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7058 if (gfc_match_eos () == MATCH_YES
)
7063 m
= gfc_match_symbol (&sym
, 0);
7067 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
7080 if (gfc_match_eos () == MATCH_YES
)
7082 if (gfc_match_char (',') != MATCH_YES
)
7089 gfc_error ("Syntax error in VALUE statement at %C");
7095 gfc_match_volatile (void)
7100 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
7104 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7109 if (gfc_match_eos () == MATCH_YES
)
7114 /* VOLATILE is special because it can be added to host-associated
7115 symbols locally. Except for coarrays. */
7116 m
= gfc_match_symbol (&sym
, 1);
7120 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7121 for variable in a BLOCK which is defined outside of the BLOCK. */
7122 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7124 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7125 "%C, which is use-/host-associated", sym
->name
);
7128 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
7141 if (gfc_match_eos () == MATCH_YES
)
7143 if (gfc_match_char (',') != MATCH_YES
)
7150 gfc_error ("Syntax error in VOLATILE statement at %C");
7156 gfc_match_asynchronous (void)
7161 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ASYNCHRONOUS statement at %C")
7165 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7170 if (gfc_match_eos () == MATCH_YES
)
7175 /* ASYNCHRONOUS is special because it can be added to host-associated
7177 m
= gfc_match_symbol (&sym
, 1);
7181 if (gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
)
7194 if (gfc_match_eos () == MATCH_YES
)
7196 if (gfc_match_char (',') != MATCH_YES
)
7203 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7208 /* Match a module procedure statement. Note that we have to modify
7209 symbols in the parent's namespace because the current one was there
7210 to receive symbols that are in an interface's formal argument list. */
7213 gfc_match_modproc (void)
7215 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7219 gfc_namespace
*module_ns
;
7220 gfc_interface
*old_interface_head
, *interface
;
7222 if (gfc_state_stack
->state
!= COMP_INTERFACE
7223 || gfc_state_stack
->previous
== NULL
7224 || current_interface
.type
== INTERFACE_NAMELESS
7225 || current_interface
.type
== INTERFACE_ABSTRACT
)
7227 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7232 module_ns
= gfc_current_ns
->parent
;
7233 for (; module_ns
; module_ns
= module_ns
->parent
)
7234 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7235 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7236 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7237 && !module_ns
->proc_name
->attr
.contained
))
7240 if (module_ns
== NULL
)
7243 /* Store the current state of the interface. We will need it if we
7244 end up with a syntax error and need to recover. */
7245 old_interface_head
= gfc_current_interface_head ();
7247 /* Check if the F2008 optional double colon appears. */
7248 gfc_gobble_whitespace ();
7249 old_locus
= gfc_current_locus
;
7250 if (gfc_match ("::") == MATCH_YES
)
7252 if (gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: double colon in "
7253 "MODULE PROCEDURE statement at %L", &old_locus
)
7258 gfc_current_locus
= old_locus
;
7263 old_locus
= gfc_current_locus
;
7265 m
= gfc_match_name (name
);
7271 /* Check for syntax error before starting to add symbols to the
7272 current namespace. */
7273 if (gfc_match_eos () == MATCH_YES
)
7276 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7279 /* Now we're sure the syntax is valid, we process this item
7281 if (gfc_get_symbol (name
, module_ns
, &sym
))
7284 if (sym
->attr
.intrinsic
)
7286 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7287 "PROCEDURE", &old_locus
);
7291 if (sym
->attr
.proc
!= PROC_MODULE
7292 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7293 sym
->name
, NULL
) == FAILURE
)
7296 if (gfc_add_interface (sym
) == FAILURE
)
7299 sym
->attr
.mod_proc
= 1;
7300 sym
->declared_at
= old_locus
;
7309 /* Restore the previous state of the interface. */
7310 interface
= gfc_current_interface_head ();
7311 gfc_set_current_interface_head (old_interface_head
);
7313 /* Free the new interfaces. */
7314 while (interface
!= old_interface_head
)
7316 gfc_interface
*i
= interface
->next
;
7321 /* And issue a syntax error. */
7322 gfc_syntax_error (ST_MODULE_PROC
);
7327 /* Check a derived type that is being extended. */
7329 check_extended_derived_type (char *name
)
7331 gfc_symbol
*extended
;
7333 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7335 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7341 gfc_error ("No such symbol in TYPE definition at %C");
7345 extended
= gfc_find_dt_in_generic (extended
);
7347 if (extended
->attr
.flavor
!= FL_DERIVED
)
7349 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7350 "derived type", name
);
7354 if (extended
->attr
.is_bind_c
)
7356 gfc_error ("'%s' cannot be extended at %C because it "
7357 "is BIND(C)", extended
->name
);
7361 if (extended
->attr
.sequence
)
7363 gfc_error ("'%s' cannot be extended at %C because it "
7364 "is a SEQUENCE type", extended
->name
);
7372 /* Match the optional attribute specifiers for a type declaration.
7373 Return MATCH_ERROR if an error is encountered in one of the handled
7374 attributes (public, private, bind(c)), MATCH_NO if what's found is
7375 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7376 checking on attribute conflicts needs to be done. */
7379 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7381 /* See if the derived type is marked as private. */
7382 if (gfc_match (" , private") == MATCH_YES
)
7384 if (gfc_current_state () != COMP_MODULE
)
7386 gfc_error ("Derived type at %C can only be PRIVATE in the "
7387 "specification part of a module");
7391 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
7394 else if (gfc_match (" , public") == MATCH_YES
)
7396 if (gfc_current_state () != COMP_MODULE
)
7398 gfc_error ("Derived type at %C can only be PUBLIC in the "
7399 "specification part of a module");
7403 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
7406 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7408 /* If the type is defined to be bind(c) it then needs to make
7409 sure that all fields are interoperable. This will
7410 need to be a semantic check on the finished derived type.
7411 See 15.2.3 (lines 9-12) of F2003 draft. */
7412 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
7415 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7417 else if (gfc_match (" , abstract") == MATCH_YES
)
7419 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
7423 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
7426 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
7428 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
7434 /* If we get here, something matched. */
7439 /* Match the beginning of a derived type declaration. If a type name
7440 was the result of a function, then it is possible to have a symbol
7441 already to be known as a derived type yet have no components. */
7444 gfc_match_derived_decl (void)
7446 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7447 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
7448 symbol_attribute attr
;
7449 gfc_symbol
*sym
, *gensym
;
7450 gfc_symbol
*extended
;
7452 match is_type_attr_spec
= MATCH_NO
;
7453 bool seen_attr
= false;
7454 gfc_interface
*intr
= NULL
, *head
;
7456 if (gfc_current_state () == COMP_DERIVED
)
7461 gfc_clear_attr (&attr
);
7466 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
7467 if (is_type_attr_spec
== MATCH_ERROR
)
7469 if (is_type_attr_spec
== MATCH_YES
)
7471 } while (is_type_attr_spec
== MATCH_YES
);
7473 /* Deal with derived type extensions. The extension attribute has
7474 been added to 'attr' but now the parent type must be found and
7477 extended
= check_extended_derived_type (parent
);
7479 if (parent
[0] && !extended
)
7482 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
7484 gfc_error ("Expected :: in TYPE definition at %C");
7488 m
= gfc_match (" %n%t", name
);
7492 /* Make sure the name is not the name of an intrinsic type. */
7493 if (gfc_is_intrinsic_typename (name
))
7495 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7500 if (gfc_get_symbol (name
, NULL
, &gensym
))
7503 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
7505 gfc_error ("Derived type name '%s' at %C already has a basic type "
7506 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
7510 if (!gensym
->attr
.generic
7511 && gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
) == FAILURE
)
7514 if (!gensym
->attr
.function
7515 && gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
) == FAILURE
)
7518 sym
= gfc_find_dt_in_generic (gensym
);
7520 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
7522 gfc_error ("Derived type definition of '%s' at %C has already been "
7523 "defined", sym
->name
);
7529 /* Use upper case to save the actual derived-type symbol. */
7530 gfc_get_symbol (gfc_get_string ("%c%s",
7531 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
7532 &gensym
->name
[1]), NULL
, &sym
);
7533 sym
->name
= gfc_get_string (gensym
->name
);
7534 head
= gensym
->generic
;
7535 intr
= gfc_get_interface ();
7537 intr
->where
= gfc_current_locus
;
7538 intr
->sym
->declared_at
= gfc_current_locus
;
7540 gensym
->generic
= intr
;
7541 gensym
->attr
.if_source
= IFSRC_DECL
;
7544 /* The symbol may already have the derived attribute without the
7545 components. The ways this can happen is via a function
7546 definition, an INTRINSIC statement or a subtype in another
7547 derived type that is a pointer. The first part of the AND clause
7548 is true if the symbol is not the return value of a function. */
7549 if (sym
->attr
.flavor
!= FL_DERIVED
7550 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
7553 if (attr
.access
!= ACCESS_UNKNOWN
7554 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
7556 else if (sym
->attr
.access
== ACCESS_UNKNOWN
7557 && gensym
->attr
.access
!= ACCESS_UNKNOWN
7558 && gfc_add_access (&sym
->attr
, gensym
->attr
.access
, sym
->name
, NULL
)
7562 if (sym
->attr
.access
!= ACCESS_UNKNOWN
7563 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
7564 gensym
->attr
.access
= sym
->attr
.access
;
7566 /* See if the derived type was labeled as bind(c). */
7567 if (attr
.is_bind_c
!= 0)
7568 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
7570 /* Construct the f2k_derived namespace if it is not yet there. */
7571 if (!sym
->f2k_derived
)
7572 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7574 if (extended
&& !sym
->components
)
7579 /* Add the extended derived type as the first component. */
7580 gfc_add_component (sym
, parent
, &p
);
7582 gfc_set_sym_referenced (extended
);
7584 p
->ts
.type
= BT_DERIVED
;
7585 p
->ts
.u
.derived
= extended
;
7586 p
->initializer
= gfc_default_initializer (&p
->ts
);
7588 /* Set extension level. */
7589 if (extended
->attr
.extension
== 255)
7591 /* Since the extension field is 8 bit wide, we can only have
7592 up to 255 extension levels. */
7593 gfc_error ("Maximum extension level reached with type '%s' at %L",
7594 extended
->name
, &extended
->declared_at
);
7597 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
7599 /* Provide the links between the extended type and its extension. */
7600 if (!extended
->f2k_derived
)
7601 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
7602 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
7606 if (!sym
->hash_value
)
7607 /* Set the hash for the compound name for this type. */
7608 sym
->hash_value
= gfc_hash_value (sym
);
7610 /* Take over the ABSTRACT attribute. */
7611 sym
->attr
.abstract
= attr
.abstract
;
7613 gfc_new_block
= sym
;
7619 /* Cray Pointees can be declared as:
7620 pointer (ipt, a (n,m,...,*)) */
7623 gfc_mod_pointee_as (gfc_array_spec
*as
)
7625 as
->cray_pointee
= true; /* This will be useful to know later. */
7626 if (as
->type
== AS_ASSUMED_SIZE
)
7627 as
->cp_was_assumed
= true;
7628 else if (as
->type
== AS_ASSUMED_SHAPE
)
7630 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7637 /* Match the enum definition statement, here we are trying to match
7638 the first line of enum definition statement.
7639 Returns MATCH_YES if match is found. */
7642 gfc_match_enum (void)
7646 m
= gfc_match_eos ();
7650 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
7658 /* Returns an initializer whose value is one higher than the value of the
7659 LAST_INITIALIZER argument. If the argument is NULL, the
7660 initializers value will be set to zero. The initializer's kind
7661 will be set to gfc_c_int_kind.
7663 If -fshort-enums is given, the appropriate kind will be selected
7664 later after all enumerators have been parsed. A warning is issued
7665 here if an initializer exceeds gfc_c_int_kind. */
7668 enum_initializer (gfc_expr
*last_initializer
, locus where
)
7671 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
7673 mpz_init (result
->value
.integer
);
7675 if (last_initializer
!= NULL
)
7677 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
7678 result
->where
= last_initializer
->where
;
7680 if (gfc_check_integer_range (result
->value
.integer
,
7681 gfc_c_int_kind
) != ARITH_OK
)
7683 gfc_error ("Enumerator exceeds the C integer type at %C");
7689 /* Control comes here, if it's the very first enumerator and no
7690 initializer has been given. It will be initialized to zero. */
7691 mpz_set_si (result
->value
.integer
, 0);
7698 /* Match a variable name with an optional initializer. When this
7699 subroutine is called, a variable is expected to be parsed next.
7700 Depending on what is happening at the moment, updates either the
7701 symbol table or the current interface. */
7704 enumerator_decl (void)
7706 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7707 gfc_expr
*initializer
;
7708 gfc_array_spec
*as
= NULL
;
7716 old_locus
= gfc_current_locus
;
7718 /* When we get here, we've just matched a list of attributes and
7719 maybe a type and a double colon. The next thing we expect to see
7720 is the name of the symbol. */
7721 m
= gfc_match_name (name
);
7725 var_locus
= gfc_current_locus
;
7727 /* OK, we've successfully matched the declaration. Now put the
7728 symbol in the current namespace. If we fail to create the symbol,
7730 if (build_sym (name
, NULL
, false, &as
, &var_locus
) == FAILURE
)
7736 /* The double colon must be present in order to have initializers.
7737 Otherwise the statement is ambiguous with an assignment statement. */
7740 if (gfc_match_char ('=') == MATCH_YES
)
7742 m
= gfc_match_init_expr (&initializer
);
7745 gfc_error ("Expected an initialization expression at %C");
7754 /* If we do not have an initializer, the initialization value of the
7755 previous enumerator (stored in last_initializer) is incremented
7756 by 1 and is used to initialize the current enumerator. */
7757 if (initializer
== NULL
)
7758 initializer
= enum_initializer (last_initializer
, old_locus
);
7760 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
7762 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7768 /* Store this current initializer, for the next enumerator variable
7769 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7770 use last_initializer below. */
7771 last_initializer
= initializer
;
7772 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
7774 /* Maintain enumerator history. */
7775 gfc_find_symbol (name
, NULL
, 0, &sym
);
7776 create_enum_history (sym
, last_initializer
);
7778 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
7781 /* Free stuff up and return. */
7782 gfc_free_expr (initializer
);
7788 /* Match the enumerator definition statement. */
7791 gfc_match_enumerator_def (void)
7796 gfc_clear_ts (¤t_ts
);
7798 m
= gfc_match (" enumerator");
7802 m
= gfc_match (" :: ");
7803 if (m
== MATCH_ERROR
)
7806 colon_seen
= (m
== MATCH_YES
);
7808 if (gfc_current_state () != COMP_ENUM
)
7810 gfc_error ("ENUM definition statement expected before %C");
7811 gfc_free_enum_history ();
7815 (¤t_ts
)->type
= BT_INTEGER
;
7816 (¤t_ts
)->kind
= gfc_c_int_kind
;
7818 gfc_clear_attr (¤t_attr
);
7819 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
7828 m
= enumerator_decl ();
7829 if (m
== MATCH_ERROR
)
7831 gfc_free_enum_history ();
7837 if (gfc_match_eos () == MATCH_YES
)
7839 if (gfc_match_char (',') != MATCH_YES
)
7843 if (gfc_current_state () == COMP_ENUM
)
7845 gfc_free_enum_history ();
7846 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7851 gfc_free_array_spec (current_as
);
7858 /* Match binding attributes. */
7861 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
7863 bool found_passing
= false;
7864 bool seen_ptr
= false;
7865 match m
= MATCH_YES
;
7867 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7868 this case the defaults are in there. */
7869 ba
->access
= ACCESS_UNKNOWN
;
7870 ba
->pass_arg
= NULL
;
7871 ba
->pass_arg_num
= 0;
7873 ba
->non_overridable
= 0;
7877 /* If we find a comma, we believe there are binding attributes. */
7878 m
= gfc_match_char (',');
7884 /* Access specifier. */
7886 m
= gfc_match (" public");
7887 if (m
== MATCH_ERROR
)
7891 if (ba
->access
!= ACCESS_UNKNOWN
)
7893 gfc_error ("Duplicate access-specifier at %C");
7897 ba
->access
= ACCESS_PUBLIC
;
7901 m
= gfc_match (" private");
7902 if (m
== MATCH_ERROR
)
7906 if (ba
->access
!= ACCESS_UNKNOWN
)
7908 gfc_error ("Duplicate access-specifier at %C");
7912 ba
->access
= ACCESS_PRIVATE
;
7916 /* If inside GENERIC, the following is not allowed. */
7921 m
= gfc_match (" nopass");
7922 if (m
== MATCH_ERROR
)
7928 gfc_error ("Binding attributes already specify passing,"
7929 " illegal NOPASS at %C");
7933 found_passing
= true;
7938 /* PASS possibly including argument. */
7939 m
= gfc_match (" pass");
7940 if (m
== MATCH_ERROR
)
7944 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
7948 gfc_error ("Binding attributes already specify passing,"
7949 " illegal PASS at %C");
7953 m
= gfc_match (" ( %n )", arg
);
7954 if (m
== MATCH_ERROR
)
7957 ba
->pass_arg
= gfc_get_string (arg
);
7958 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
7960 found_passing
= true;
7968 m
= gfc_match (" pointer");
7969 if (m
== MATCH_ERROR
)
7975 gfc_error ("Duplicate POINTER attribute at %C");
7985 /* NON_OVERRIDABLE flag. */
7986 m
= gfc_match (" non_overridable");
7987 if (m
== MATCH_ERROR
)
7991 if (ba
->non_overridable
)
7993 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7997 ba
->non_overridable
= 1;
8001 /* DEFERRED flag. */
8002 m
= gfc_match (" deferred");
8003 if (m
== MATCH_ERROR
)
8009 gfc_error ("Duplicate DEFERRED at %C");
8020 /* Nothing matching found. */
8022 gfc_error ("Expected access-specifier at %C");
8024 gfc_error ("Expected binding attribute at %C");
8027 while (gfc_match_char (',') == MATCH_YES
);
8029 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8030 if (ba
->non_overridable
&& ba
->deferred
)
8032 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8039 if (ba
->access
== ACCESS_UNKNOWN
)
8040 ba
->access
= gfc_typebound_default_access
;
8042 if (ppc
&& !seen_ptr
)
8044 gfc_error ("POINTER attribute is required for procedure pointer component"
8056 /* Match a PROCEDURE specific binding inside a derived type. */
8059 match_procedure_in_type (void)
8061 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8062 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8063 char* target
= NULL
, *ifc
= NULL
;
8064 gfc_typebound_proc tb
;
8073 /* Check current state. */
8074 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8075 block
= gfc_state_stack
->previous
->sym
;
8078 /* Try to match PROCEDURE(interface). */
8079 if (gfc_match (" (") == MATCH_YES
)
8081 m
= gfc_match_name (target_buf
);
8082 if (m
== MATCH_ERROR
)
8086 gfc_error ("Interface-name expected after '(' at %C");
8090 if (gfc_match (" )") != MATCH_YES
)
8092 gfc_error ("')' expected at %C");
8099 /* Construct the data structure. */
8100 memset (&tb
, 0, sizeof (tb
));
8101 tb
.where
= gfc_current_locus
;
8103 /* Match binding attributes. */
8104 m
= match_binding_attributes (&tb
, false, false);
8105 if (m
== MATCH_ERROR
)
8107 seen_attrs
= (m
== MATCH_YES
);
8109 /* Check that attribute DEFERRED is given if an interface is specified. */
8110 if (tb
.deferred
&& !ifc
)
8112 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8115 if (ifc
&& !tb
.deferred
)
8117 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8121 /* Match the colons. */
8122 m
= gfc_match (" ::");
8123 if (m
== MATCH_ERROR
)
8125 seen_colons
= (m
== MATCH_YES
);
8126 if (seen_attrs
&& !seen_colons
)
8128 gfc_error ("Expected '::' after binding-attributes at %C");
8132 /* Match the binding names. */
8135 m
= gfc_match_name (name
);
8136 if (m
== MATCH_ERROR
)
8140 gfc_error ("Expected binding name at %C");
8144 if (num
>1 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: PROCEDURE list"
8145 " at %C") == FAILURE
)
8148 /* Try to match the '=> target', if it's there. */
8150 m
= gfc_match (" =>");
8151 if (m
== MATCH_ERROR
)
8157 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8163 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8168 m
= gfc_match_name (target_buf
);
8169 if (m
== MATCH_ERROR
)
8173 gfc_error ("Expected binding target after '=>' at %C");
8176 target
= target_buf
;
8179 /* If no target was found, it has the same name as the binding. */
8183 /* Get the namespace to insert the symbols into. */
8184 ns
= block
->f2k_derived
;
8187 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8188 if (tb
.deferred
&& !block
->attr
.abstract
)
8190 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8191 "is not ABSTRACT", block
->name
);
8195 /* See if we already have a binding with this name in the symtree which
8196 would be an error. If a GENERIC already targetted this binding, it may
8197 be already there but then typebound is still NULL. */
8198 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8199 if (stree
&& stree
->n
.tb
)
8201 gfc_error ("There is already a procedure with binding name '%s' for "
8202 "the derived type '%s' at %C", name
, block
->name
);
8206 /* Insert it and set attributes. */
8210 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8213 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8215 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8218 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8220 if (gfc_match_eos () == MATCH_YES
)
8222 if (gfc_match_char (',') != MATCH_YES
)
8227 gfc_error ("Syntax error in PROCEDURE statement at %C");
8232 /* Match a GENERIC procedure binding inside a derived type. */
8235 gfc_match_generic (void)
8237 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8238 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8240 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8241 gfc_typebound_proc
* tb
;
8243 interface_type op_type
;
8244 gfc_intrinsic_op op
;
8247 /* Check current state. */
8248 if (gfc_current_state () == COMP_DERIVED
)
8250 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8253 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8255 block
= gfc_state_stack
->previous
->sym
;
8256 ns
= block
->f2k_derived
;
8257 gcc_assert (block
&& ns
);
8259 memset (&tbattr
, 0, sizeof (tbattr
));
8260 tbattr
.where
= gfc_current_locus
;
8262 /* See if we get an access-specifier. */
8263 m
= match_binding_attributes (&tbattr
, true, false);
8264 if (m
== MATCH_ERROR
)
8267 /* Now the colons, those are required. */
8268 if (gfc_match (" ::") != MATCH_YES
)
8270 gfc_error ("Expected '::' at %C");
8274 /* Match the binding name; depending on type (operator / generic) format
8275 it for future error messages into bind_name. */
8277 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8278 if (m
== MATCH_ERROR
)
8282 gfc_error ("Expected generic name or operator descriptor at %C");
8288 case INTERFACE_GENERIC
:
8289 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8292 case INTERFACE_USER_OP
:
8293 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8296 case INTERFACE_INTRINSIC_OP
:
8297 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8298 gfc_op2string (op
));
8305 /* Match the required =>. */
8306 if (gfc_match (" =>") != MATCH_YES
)
8308 gfc_error ("Expected '=>' at %C");
8312 /* Try to find existing GENERIC binding with this name / for this operator;
8313 if there is something, check that it is another GENERIC and then extend
8314 it rather than building a new node. Otherwise, create it and put it
8315 at the right position. */
8319 case INTERFACE_USER_OP
:
8320 case INTERFACE_GENERIC
:
8322 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8325 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8337 case INTERFACE_INTRINSIC_OP
:
8347 if (!tb
->is_generic
)
8349 gcc_assert (op_type
== INTERFACE_GENERIC
);
8350 gfc_error ("There's already a non-generic procedure with binding name"
8351 " '%s' for the derived type '%s' at %C",
8352 bind_name
, block
->name
);
8356 if (tb
->access
!= tbattr
.access
)
8358 gfc_error ("Binding at %C must have the same access as already"
8359 " defined binding '%s'", bind_name
);
8365 tb
= gfc_get_typebound_proc (NULL
);
8366 tb
->where
= gfc_current_locus
;
8367 tb
->access
= tbattr
.access
;
8369 tb
->u
.generic
= NULL
;
8373 case INTERFACE_GENERIC
:
8374 case INTERFACE_USER_OP
:
8376 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8379 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8387 case INTERFACE_INTRINSIC_OP
:
8396 /* Now, match all following names as specific targets. */
8399 gfc_symtree
* target_st
;
8400 gfc_tbp_generic
* target
;
8402 m
= gfc_match_name (name
);
8403 if (m
== MATCH_ERROR
)
8407 gfc_error ("Expected specific binding name at %C");
8411 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8413 /* See if this is a duplicate specification. */
8414 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8415 if (target_st
== target
->specific_st
)
8417 gfc_error ("'%s' already defined as specific binding for the"
8418 " generic '%s' at %C", name
, bind_name
);
8422 target
= gfc_get_tbp_generic ();
8423 target
->specific_st
= target_st
;
8424 target
->specific
= NULL
;
8425 target
->next
= tb
->u
.generic
;
8426 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
8427 || (op_type
== INTERFACE_INTRINSIC_OP
));
8428 tb
->u
.generic
= target
;
8430 while (gfc_match (" ,") == MATCH_YES
);
8432 /* Here should be the end. */
8433 if (gfc_match_eos () != MATCH_YES
)
8435 gfc_error ("Junk after GENERIC binding at %C");
8446 /* Match a FINAL declaration inside a derived type. */
8449 gfc_match_final_decl (void)
8451 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8454 gfc_namespace
* module_ns
;
8458 if (gfc_current_form
== FORM_FREE
)
8460 char c
= gfc_peek_ascii_char ();
8461 if (!gfc_is_whitespace (c
) && c
!= ':')
8465 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
8467 if (gfc_current_form
== FORM_FIXED
)
8470 gfc_error ("FINAL declaration at %C must be inside a derived type "
8471 "CONTAINS section");
8475 block
= gfc_state_stack
->previous
->sym
;
8478 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
8479 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
8481 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8482 " specification part of a MODULE");
8486 module_ns
= gfc_current_ns
;
8487 gcc_assert (module_ns
);
8488 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
8490 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8491 if (gfc_match (" ::") == MATCH_ERROR
)
8494 /* Match the sequence of procedure names. */
8501 if (first
&& gfc_match_eos () == MATCH_YES
)
8503 gfc_error ("Empty FINAL at %C");
8507 m
= gfc_match_name (name
);
8510 gfc_error ("Expected module procedure name at %C");
8513 else if (m
!= MATCH_YES
)
8516 if (gfc_match_eos () == MATCH_YES
)
8518 if (!last
&& gfc_match_char (',') != MATCH_YES
)
8520 gfc_error ("Expected ',' at %C");
8524 if (gfc_get_symbol (name
, module_ns
, &sym
))
8526 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
8530 /* Mark the symbol as module procedure. */
8531 if (sym
->attr
.proc
!= PROC_MODULE
8532 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
8533 sym
->name
, NULL
) == FAILURE
)
8536 /* Check if we already have this symbol in the list, this is an error. */
8537 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
8538 if (f
->proc_sym
== sym
)
8540 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8545 /* Add this symbol to the list of finalizers. */
8546 gcc_assert (block
->f2k_derived
);
8548 f
= XCNEW (gfc_finalizer
);
8550 f
->proc_tree
= NULL
;
8551 f
->where
= gfc_current_locus
;
8552 f
->next
= block
->f2k_derived
->finalizers
;
8553 block
->f2k_derived
->finalizers
= f
;
8563 const ext_attr_t ext_attr_list
[] = {
8564 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
8565 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
8566 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
8567 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
8568 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
8569 { NULL
, EXT_ATTR_LAST
, NULL
}
8572 /* Match a !GCC$ ATTRIBUTES statement of the form:
8573 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8574 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8576 TODO: We should support all GCC attributes using the same syntax for
8577 the attribute list, i.e. the list in C
8578 __attributes(( attribute-list ))
8580 !GCC$ ATTRIBUTES attribute-list ::
8581 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8584 As there is absolutely no risk of confusion, we should never return
8587 gfc_match_gcc_attributes (void)
8589 symbol_attribute attr
;
8590 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8595 gfc_clear_attr (&attr
);
8600 if (gfc_match_name (name
) != MATCH_YES
)
8603 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
8604 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
8607 if (id
== EXT_ATTR_LAST
)
8609 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8613 if (gfc_add_ext_attribute (&attr
, (ext_attr_id_t
) id
, &gfc_current_locus
)
8617 gfc_gobble_whitespace ();
8618 ch
= gfc_next_ascii_char ();
8621 /* This is the successful exit condition for the loop. */
8622 if (gfc_next_ascii_char () == ':')
8632 if (gfc_match_eos () == MATCH_YES
)
8637 m
= gfc_match_name (name
);
8641 if (find_special (name
, &sym
, true))
8644 sym
->attr
.ext_attr
|= attr
.ext_attr
;
8646 if (gfc_match_eos () == MATCH_YES
)
8649 if (gfc_match_char (',') != MATCH_YES
)
8656 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");