]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/decl.c
arith.c: Change copyright header to refer to version 3 of the GNU General Public...
[thirdparty/gcc.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
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
11 version.
12
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
16 for more details.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27
28 /* This flag is set if an old-style length selector is matched
29 during a type-declaration statement. */
30
31 static int old_char_selector;
32
33 /* When variables acquire types and attributes from a declaration
34 statement, they get them from the following static variables. The
35 first part of a declaration sets these variables and the second
36 part copies these into symbol structures. */
37
38 static gfc_typespec current_ts;
39
40 static symbol_attribute current_attr;
41 static gfc_array_spec *current_as;
42 static int colon_seen;
43
44 /* The current binding label (if any). */
45 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
46 /* Need to know how many identifiers are on the current data declaration
47 line in case we're given the BIND(C) attribute with a NAME= specifier. */
48 static int num_idents_on_line;
49 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
50 can supply a name if the curr_binding_label is nil and NAME= was not. */
51 static int has_name_equals = 0;
52
53 /* Initializer of the previous enumerator. */
54
55 static gfc_expr *last_initializer;
56
57 /* History of all the enumerators is maintained, so that
58 kind values of all the enumerators could be updated depending
59 upon the maximum initialized value. */
60
61 typedef struct enumerator_history
62 {
63 gfc_symbol *sym;
64 gfc_expr *initializer;
65 struct enumerator_history *next;
66 }
67 enumerator_history;
68
69 /* Header of enum history chain. */
70
71 static enumerator_history *enum_history = NULL;
72
73 /* Pointer of enum history node containing largest initializer. */
74
75 static enumerator_history *max_enum = NULL;
76
77 /* gfc_new_block points to the symbol of a newly matched block. */
78
79 gfc_symbol *gfc_new_block;
80
81
82 /********************* DATA statement subroutines *********************/
83
84 static bool in_match_data = false;
85
86 bool
87 gfc_in_match_data (void)
88 {
89 return in_match_data;
90 }
91
92 void
93 gfc_set_in_match_data (bool set_value)
94 {
95 in_match_data = set_value;
96 }
97
98 /* Free a gfc_data_variable structure and everything beneath it. */
99
100 static void
101 free_variable (gfc_data_variable *p)
102 {
103 gfc_data_variable *q;
104
105 for (; p; p = q)
106 {
107 q = p->next;
108 gfc_free_expr (p->expr);
109 gfc_free_iterator (&p->iter, 0);
110 free_variable (p->list);
111 gfc_free (p);
112 }
113 }
114
115
116 /* Free a gfc_data_value structure and everything beneath it. */
117
118 static void
119 free_value (gfc_data_value *p)
120 {
121 gfc_data_value *q;
122
123 for (; p; p = q)
124 {
125 q = p->next;
126 gfc_free_expr (p->expr);
127 gfc_free (p);
128 }
129 }
130
131
132 /* Free a list of gfc_data structures. */
133
134 void
135 gfc_free_data (gfc_data *p)
136 {
137 gfc_data *q;
138
139 for (; p; p = q)
140 {
141 q = p->next;
142 free_variable (p->var);
143 free_value (p->value);
144 gfc_free (p);
145 }
146 }
147
148
149 /* Free all data in a namespace. */
150
151 static void
152 gfc_free_data_all (gfc_namespace *ns)
153 {
154 gfc_data *d;
155
156 for (;ns->data;)
157 {
158 d = ns->data->next;
159 gfc_free (ns->data);
160 ns->data = d;
161 }
162 }
163
164
165 static match var_element (gfc_data_variable *);
166
167 /* Match a list of variables terminated by an iterator and a right
168 parenthesis. */
169
170 static match
171 var_list (gfc_data_variable *parent)
172 {
173 gfc_data_variable *tail, var;
174 match m;
175
176 m = var_element (&var);
177 if (m == MATCH_ERROR)
178 return MATCH_ERROR;
179 if (m == MATCH_NO)
180 goto syntax;
181
182 tail = gfc_get_data_variable ();
183 *tail = var;
184
185 parent->list = tail;
186
187 for (;;)
188 {
189 if (gfc_match_char (',') != MATCH_YES)
190 goto syntax;
191
192 m = gfc_match_iterator (&parent->iter, 1);
193 if (m == MATCH_YES)
194 break;
195 if (m == MATCH_ERROR)
196 return MATCH_ERROR;
197
198 m = var_element (&var);
199 if (m == MATCH_ERROR)
200 return MATCH_ERROR;
201 if (m == MATCH_NO)
202 goto syntax;
203
204 tail->next = gfc_get_data_variable ();
205 tail = tail->next;
206
207 *tail = var;
208 }
209
210 if (gfc_match_char (')') != MATCH_YES)
211 goto syntax;
212 return MATCH_YES;
213
214 syntax:
215 gfc_syntax_error (ST_DATA);
216 return MATCH_ERROR;
217 }
218
219
220 /* Match a single element in a data variable list, which can be a
221 variable-iterator list. */
222
223 static match
224 var_element (gfc_data_variable *new)
225 {
226 match m;
227 gfc_symbol *sym;
228
229 memset (new, 0, sizeof (gfc_data_variable));
230
231 if (gfc_match_char ('(') == MATCH_YES)
232 return var_list (new);
233
234 m = gfc_match_variable (&new->expr, 0);
235 if (m != MATCH_YES)
236 return m;
237
238 sym = new->expr->symtree->n.sym;
239
240 if (!sym->attr.function && gfc_current_ns->parent
241 && gfc_current_ns->parent == sym->ns)
242 {
243 gfc_error ("Host associated variable '%s' may not be in the DATA "
244 "statement at %C", sym->name);
245 return MATCH_ERROR;
246 }
247
248 if (gfc_current_state () != COMP_BLOCK_DATA
249 && sym->attr.in_common
250 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
251 "common block variable '%s' in DATA statement at %C",
252 sym->name) == FAILURE)
253 return MATCH_ERROR;
254
255 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
256 return MATCH_ERROR;
257
258 return MATCH_YES;
259 }
260
261
262 /* Match the top-level list of data variables. */
263
264 static match
265 top_var_list (gfc_data *d)
266 {
267 gfc_data_variable var, *tail, *new;
268 match m;
269
270 tail = NULL;
271
272 for (;;)
273 {
274 m = var_element (&var);
275 if (m == MATCH_NO)
276 goto syntax;
277 if (m == MATCH_ERROR)
278 return MATCH_ERROR;
279
280 new = gfc_get_data_variable ();
281 *new = var;
282
283 if (tail == NULL)
284 d->var = new;
285 else
286 tail->next = new;
287
288 tail = new;
289
290 if (gfc_match_char ('/') == MATCH_YES)
291 break;
292 if (gfc_match_char (',') != MATCH_YES)
293 goto syntax;
294 }
295
296 return MATCH_YES;
297
298 syntax:
299 gfc_syntax_error (ST_DATA);
300 gfc_free_data_all (gfc_current_ns);
301 return MATCH_ERROR;
302 }
303
304
305 static match
306 match_data_constant (gfc_expr **result)
307 {
308 char name[GFC_MAX_SYMBOL_LEN + 1];
309 gfc_symbol *sym;
310 gfc_expr *expr;
311 match m;
312 locus old_loc;
313
314 m = gfc_match_literal_constant (&expr, 1);
315 if (m == MATCH_YES)
316 {
317 *result = expr;
318 return MATCH_YES;
319 }
320
321 if (m == MATCH_ERROR)
322 return MATCH_ERROR;
323
324 m = gfc_match_null (result);
325 if (m != MATCH_NO)
326 return m;
327
328 old_loc = gfc_current_locus;
329
330 /* Should this be a structure component, try to match it
331 before matching a name. */
332 m = gfc_match_rvalue (result);
333 if (m == MATCH_ERROR)
334 return m;
335
336 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
337 {
338 if (gfc_simplify_expr (*result, 0) == FAILURE)
339 m = MATCH_ERROR;
340 return m;
341 }
342
343 gfc_current_locus = old_loc;
344
345 m = gfc_match_name (name);
346 if (m != MATCH_YES)
347 return m;
348
349 if (gfc_find_symbol (name, NULL, 1, &sym))
350 return MATCH_ERROR;
351
352 if (sym == NULL
353 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
354 {
355 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
356 name);
357 return MATCH_ERROR;
358 }
359 else if (sym->attr.flavor == FL_DERIVED)
360 return gfc_match_structure_constructor (sym, result);
361
362 *result = gfc_copy_expr (sym->value);
363 return MATCH_YES;
364 }
365
366
367 /* Match a list of values in a DATA statement. The leading '/' has
368 already been seen at this point. */
369
370 static match
371 top_val_list (gfc_data *data)
372 {
373 gfc_data_value *new, *tail;
374 gfc_expr *expr;
375 const char *msg;
376 match m;
377
378 tail = NULL;
379
380 for (;;)
381 {
382 m = match_data_constant (&expr);
383 if (m == MATCH_NO)
384 goto syntax;
385 if (m == MATCH_ERROR)
386 return MATCH_ERROR;
387
388 new = gfc_get_data_value ();
389
390 if (tail == NULL)
391 data->value = new;
392 else
393 tail->next = new;
394
395 tail = new;
396
397 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
398 {
399 tail->expr = expr;
400 tail->repeat = 1;
401 }
402 else
403 {
404 signed int tmp;
405 msg = gfc_extract_int (expr, &tmp);
406 gfc_free_expr (expr);
407 if (msg != NULL)
408 {
409 gfc_error (msg);
410 return MATCH_ERROR;
411 }
412 tail->repeat = tmp;
413
414 m = match_data_constant (&tail->expr);
415 if (m == MATCH_NO)
416 goto syntax;
417 if (m == MATCH_ERROR)
418 return MATCH_ERROR;
419 }
420
421 if (gfc_match_char ('/') == MATCH_YES)
422 break;
423 if (gfc_match_char (',') == MATCH_NO)
424 goto syntax;
425 }
426
427 return MATCH_YES;
428
429 syntax:
430 gfc_syntax_error (ST_DATA);
431 gfc_free_data_all (gfc_current_ns);
432 return MATCH_ERROR;
433 }
434
435
436 /* Matches an old style initialization. */
437
438 static match
439 match_old_style_init (const char *name)
440 {
441 match m;
442 gfc_symtree *st;
443 gfc_symbol *sym;
444 gfc_data *newdata;
445
446 /* Set up data structure to hold initializers. */
447 gfc_find_sym_tree (name, NULL, 0, &st);
448 sym = st->n.sym;
449
450 newdata = gfc_get_data ();
451 newdata->var = gfc_get_data_variable ();
452 newdata->var->expr = gfc_get_variable_expr (st);
453 newdata->where = gfc_current_locus;
454
455 /* Match initial value list. This also eats the terminal '/'. */
456 m = top_val_list (newdata);
457 if (m != MATCH_YES)
458 {
459 gfc_free (newdata);
460 return m;
461 }
462
463 if (gfc_pure (NULL))
464 {
465 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
466 gfc_free (newdata);
467 return MATCH_ERROR;
468 }
469
470 /* Mark the variable as having appeared in a data statement. */
471 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
472 {
473 gfc_free (newdata);
474 return MATCH_ERROR;
475 }
476
477 /* Chain in namespace list of DATA initializers. */
478 newdata->next = gfc_current_ns->data;
479 gfc_current_ns->data = newdata;
480
481 return m;
482 }
483
484
485 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
486 we are matching a DATA statement and are therefore issuing an error
487 if we encounter something unexpected, if not, we're trying to match
488 an old-style initialization expression of the form INTEGER I /2/. */
489
490 match
491 gfc_match_data (void)
492 {
493 gfc_data *new;
494 match m;
495
496 gfc_set_in_match_data (true);
497
498 for (;;)
499 {
500 new = gfc_get_data ();
501 new->where = gfc_current_locus;
502
503 m = top_var_list (new);
504 if (m != MATCH_YES)
505 goto cleanup;
506
507 m = top_val_list (new);
508 if (m != MATCH_YES)
509 goto cleanup;
510
511 new->next = gfc_current_ns->data;
512 gfc_current_ns->data = new;
513
514 if (gfc_match_eos () == MATCH_YES)
515 break;
516
517 gfc_match_char (','); /* Optional comma */
518 }
519
520 gfc_set_in_match_data (false);
521
522 if (gfc_pure (NULL))
523 {
524 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
525 return MATCH_ERROR;
526 }
527
528 return MATCH_YES;
529
530 cleanup:
531 gfc_set_in_match_data (false);
532 gfc_free_data (new);
533 return MATCH_ERROR;
534 }
535
536
537 /************************ Declaration statements *********************/
538
539 /* Match an intent specification. Since this can only happen after an
540 INTENT word, a legal intent-spec must follow. */
541
542 static sym_intent
543 match_intent_spec (void)
544 {
545
546 if (gfc_match (" ( in out )") == MATCH_YES)
547 return INTENT_INOUT;
548 if (gfc_match (" ( in )") == MATCH_YES)
549 return INTENT_IN;
550 if (gfc_match (" ( out )") == MATCH_YES)
551 return INTENT_OUT;
552
553 gfc_error ("Bad INTENT specification at %C");
554 return INTENT_UNKNOWN;
555 }
556
557
558 /* Matches a character length specification, which is either a
559 specification expression or a '*'. */
560
561 static match
562 char_len_param_value (gfc_expr **expr)
563 {
564 if (gfc_match_char ('*') == MATCH_YES)
565 {
566 *expr = NULL;
567 return MATCH_YES;
568 }
569
570 return gfc_match_expr (expr);
571 }
572
573
574 /* A character length is a '*' followed by a literal integer or a
575 char_len_param_value in parenthesis. */
576
577 static match
578 match_char_length (gfc_expr **expr)
579 {
580 int length;
581 match m;
582
583 m = gfc_match_char ('*');
584 if (m != MATCH_YES)
585 return m;
586
587 m = gfc_match_small_literal_int (&length, NULL);
588 if (m == MATCH_ERROR)
589 return m;
590
591 if (m == MATCH_YES)
592 {
593 *expr = gfc_int_expr (length);
594 return m;
595 }
596
597 if (gfc_match_char ('(') == MATCH_NO)
598 goto syntax;
599
600 m = char_len_param_value (expr);
601 if (m == MATCH_ERROR)
602 return m;
603 if (m == MATCH_NO)
604 goto syntax;
605
606 if (gfc_match_char (')') == MATCH_NO)
607 {
608 gfc_free_expr (*expr);
609 *expr = NULL;
610 goto syntax;
611 }
612
613 return MATCH_YES;
614
615 syntax:
616 gfc_error ("Syntax error in character length specification at %C");
617 return MATCH_ERROR;
618 }
619
620
621 /* Special subroutine for finding a symbol. Check if the name is found
622 in the current name space. If not, and we're compiling a function or
623 subroutine and the parent compilation unit is an interface, then check
624 to see if the name we've been given is the name of the interface
625 (located in another namespace). */
626
627 static int
628 find_special (const char *name, gfc_symbol **result)
629 {
630 gfc_state_data *s;
631 int i;
632
633 i = gfc_get_symbol (name, NULL, result);
634 if (i == 0)
635 goto end;
636
637 if (gfc_current_state () != COMP_SUBROUTINE
638 && gfc_current_state () != COMP_FUNCTION)
639 goto end;
640
641 s = gfc_state_stack->previous;
642 if (s == NULL)
643 goto end;
644
645 if (s->state != COMP_INTERFACE)
646 goto end;
647 if (s->sym == NULL)
648 goto end; /* Nameless interface. */
649
650 if (strcmp (name, s->sym->name) == 0)
651 {
652 *result = s->sym;
653 return 0;
654 }
655
656 end:
657 return i;
658 }
659
660
661 /* Special subroutine for getting a symbol node associated with a
662 procedure name, used in SUBROUTINE and FUNCTION statements. The
663 symbol is created in the parent using with symtree node in the
664 child unit pointing to the symbol. If the current namespace has no
665 parent, then the symbol is just created in the current unit. */
666
667 static int
668 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
669 {
670 gfc_symtree *st;
671 gfc_symbol *sym;
672 int rc;
673
674 /* Module functions have to be left in their own namespace because
675 they have potentially (almost certainly!) already been referenced.
676 In this sense, they are rather like external functions. This is
677 fixed up in resolve.c(resolve_entries), where the symbol name-
678 space is set to point to the master function, so that the fake
679 result mechanism can work. */
680 if (module_fcn_entry)
681 {
682 /* Present if entry is declared to be a module procedure. */
683 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
684 if (*result == NULL)
685 rc = gfc_get_symbol (name, NULL, result);
686 }
687 else
688 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
689
690 sym = *result;
691 gfc_current_ns->refs++;
692
693 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
694 {
695 /* Trap another encompassed procedure with the same name. All
696 these conditions are necessary to avoid picking up an entry
697 whose name clashes with that of the encompassing procedure;
698 this is handled using gsymbols to register unique,globally
699 accessible names. */
700 if (sym->attr.flavor != 0
701 && sym->attr.proc != 0
702 && (sym->attr.subroutine || sym->attr.function)
703 && sym->attr.if_source != IFSRC_UNKNOWN)
704 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
705 name, &sym->declared_at);
706
707 /* Trap a procedure with a name the same as interface in the
708 encompassing scope. */
709 if (sym->attr.generic != 0
710 && (sym->attr.subroutine || sym->attr.function)
711 && !sym->attr.mod_proc)
712 gfc_error_now ("Name '%s' at %C is already defined"
713 " as a generic interface at %L",
714 name, &sym->declared_at);
715
716 /* Trap declarations of attributes in encompassing scope. The
717 signature for this is that ts.kind is set. Legitimate
718 references only set ts.type. */
719 if (sym->ts.kind != 0
720 && !sym->attr.implicit_type
721 && sym->attr.proc == 0
722 && gfc_current_ns->parent != NULL
723 && sym->attr.access == 0
724 && !module_fcn_entry)
725 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
726 "and must not have attributes declared at %L",
727 name, &sym->declared_at);
728 }
729
730 if (gfc_current_ns->parent == NULL || *result == NULL)
731 return rc;
732
733 /* Module function entries will already have a symtree in
734 the current namespace but will need one at module level. */
735 if (module_fcn_entry)
736 {
737 /* Present if entry is declared to be a module procedure. */
738 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
739 if (st == NULL)
740 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
741 }
742 else
743 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
744
745 st->n.sym = sym;
746 sym->refs++;
747
748 /* See if the procedure should be a module procedure. */
749
750 if (((sym->ns->proc_name != NULL
751 && sym->ns->proc_name->attr.flavor == FL_MODULE
752 && sym->attr.proc != PROC_MODULE)
753 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
754 && gfc_add_procedure (&sym->attr, PROC_MODULE,
755 sym->name, NULL) == FAILURE)
756 rc = 2;
757
758 return rc;
759 }
760
761
762 /* Verify that the given symbol representing a parameter is C
763 interoperable, by checking to see if it was marked as such after
764 its declaration. If the given symbol is not interoperable, a
765 warning is reported, thus removing the need to return the status to
766 the calling function. The standard does not require the user use
767 one of the iso_c_binding named constants to declare an
768 interoperable parameter, but we can't be sure if the param is C
769 interop or not if the user doesn't. For example, integer(4) may be
770 legal Fortran, but doesn't have meaning in C. It may interop with
771 a number of the C types, which causes a problem because the
772 compiler can't know which one. This code is almost certainly not
773 portable, and the user will get what they deserve if the C type
774 across platforms isn't always interoperable with integer(4). If
775 the user had used something like integer(c_int) or integer(c_long),
776 the compiler could have automatically handled the varying sizes
777 across platforms. */
778
779 try
780 verify_c_interop_param (gfc_symbol *sym)
781 {
782 int is_c_interop = 0;
783 try retval = SUCCESS;
784
785 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
786 Don't repeat the checks here. */
787 if (sym->attr.implicit_type)
788 return SUCCESS;
789
790 /* For subroutines or functions that are passed to a BIND(C) procedure,
791 they're interoperable if they're BIND(C) and their params are all
792 interoperable. */
793 if (sym->attr.flavor == FL_PROCEDURE)
794 {
795 if (sym->attr.is_bind_c == 0)
796 {
797 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
798 "attribute to be C interoperable", sym->name,
799 &(sym->declared_at));
800
801 return FAILURE;
802 }
803 else
804 {
805 if (sym->attr.is_c_interop == 1)
806 /* We've already checked this procedure; don't check it again. */
807 return SUCCESS;
808 else
809 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
810 sym->common_block);
811 }
812 }
813
814 /* See if we've stored a reference to a procedure that owns sym. */
815 if (sym->ns != NULL && sym->ns->proc_name != NULL)
816 {
817 if (sym->ns->proc_name->attr.is_bind_c == 1)
818 {
819 is_c_interop =
820 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
821 == SUCCESS ? 1 : 0);
822
823 if (is_c_interop != 1)
824 {
825 /* Make personalized messages to give better feedback. */
826 if (sym->ts.type == BT_DERIVED)
827 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
828 " procedure '%s' but is not C interoperable "
829 "because derived type '%s' is not C interoperable",
830 sym->name, &(sym->declared_at),
831 sym->ns->proc_name->name,
832 sym->ts.derived->name);
833 else
834 gfc_warning ("Variable '%s' at %L is a parameter to the "
835 "BIND(C) procedure '%s' but may not be C "
836 "interoperable",
837 sym->name, &(sym->declared_at),
838 sym->ns->proc_name->name);
839 }
840
841 /* Character strings are only C interoperable if they have a
842 length of 1. */
843 if (sym->ts.type == BT_CHARACTER)
844 {
845 gfc_charlen *cl = sym->ts.cl;
846 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
847 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
848 {
849 gfc_error ("Character argument '%s' at %L "
850 "must be length 1 because "
851 "procedure '%s' is BIND(C)",
852 sym->name, &sym->declared_at,
853 sym->ns->proc_name->name);
854 retval = FAILURE;
855 }
856 }
857
858 /* We have to make sure that any param to a bind(c) routine does
859 not have the allocatable, pointer, or optional attributes,
860 according to J3/04-007, section 5.1. */
861 if (sym->attr.allocatable == 1)
862 {
863 gfc_error ("Variable '%s' at %L cannot have the "
864 "ALLOCATABLE attribute because procedure '%s'"
865 " is BIND(C)", sym->name, &(sym->declared_at),
866 sym->ns->proc_name->name);
867 retval = FAILURE;
868 }
869
870 if (sym->attr.pointer == 1)
871 {
872 gfc_error ("Variable '%s' at %L cannot have the "
873 "POINTER attribute because procedure '%s'"
874 " is BIND(C)", sym->name, &(sym->declared_at),
875 sym->ns->proc_name->name);
876 retval = FAILURE;
877 }
878
879 if (sym->attr.optional == 1)
880 {
881 gfc_error ("Variable '%s' at %L cannot have the "
882 "OPTIONAL attribute because procedure '%s'"
883 " is BIND(C)", sym->name, &(sym->declared_at),
884 sym->ns->proc_name->name);
885 retval = FAILURE;
886 }
887
888 /* Make sure that if it has the dimension attribute, that it is
889 either assumed size or explicit shape. */
890 if (sym->as != NULL)
891 {
892 if (sym->as->type == AS_ASSUMED_SHAPE)
893 {
894 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
895 "argument to the procedure '%s' at %L because "
896 "the procedure is BIND(C)", sym->name,
897 &(sym->declared_at), sym->ns->proc_name->name,
898 &(sym->ns->proc_name->declared_at));
899 retval = FAILURE;
900 }
901
902 if (sym->as->type == AS_DEFERRED)
903 {
904 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
905 "argument to the procedure '%s' at %L because "
906 "the procedure is BIND(C)", sym->name,
907 &(sym->declared_at), sym->ns->proc_name->name,
908 &(sym->ns->proc_name->declared_at));
909 retval = FAILURE;
910 }
911 }
912 }
913 }
914
915 return retval;
916 }
917
918
919 /* Function called by variable_decl() that adds a name to the symbol table. */
920
921 static try
922 build_sym (const char *name, gfc_charlen *cl,
923 gfc_array_spec **as, locus *var_locus)
924 {
925 symbol_attribute attr;
926 gfc_symbol *sym;
927
928 if (gfc_get_symbol (name, NULL, &sym))
929 return FAILURE;
930
931 /* Start updating the symbol table. Add basic type attribute if present. */
932 if (current_ts.type != BT_UNKNOWN
933 && (sym->attr.implicit_type == 0
934 || !gfc_compare_types (&sym->ts, &current_ts))
935 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
936 return FAILURE;
937
938 if (sym->ts.type == BT_CHARACTER)
939 sym->ts.cl = cl;
940
941 /* Add dimension attribute if present. */
942 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
943 return FAILURE;
944 *as = NULL;
945
946 /* Add attribute to symbol. The copy is so that we can reset the
947 dimension attribute. */
948 attr = current_attr;
949 attr.dimension = 0;
950
951 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
952 return FAILURE;
953
954 /* Finish any work that may need to be done for the binding label,
955 if it's a bind(c). The bind(c) attr is found before the symbol
956 is made, and before the symbol name (for data decls), so the
957 current_ts is holding the binding label, or nothing if the
958 name= attr wasn't given. Therefore, test here if we're dealing
959 with a bind(c) and make sure the binding label is set correctly. */
960 if (sym->attr.is_bind_c == 1)
961 {
962 if (sym->binding_label[0] == '\0')
963 {
964 /* Here, we're not checking the numIdents (the last param).
965 This could be an error we're letting slip through! */
966 if (set_binding_label (sym->binding_label, sym->name, 1) == FAILURE)
967 return FAILURE;
968 }
969 }
970
971 /* See if we know we're in a common block, and if it's a bind(c)
972 common then we need to make sure we're an interoperable type. */
973 if (sym->attr.in_common == 1)
974 {
975 /* Test the common block object. */
976 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
977 && sym->ts.is_c_interop != 1)
978 {
979 gfc_error_now ("Variable '%s' in common block '%s' at %C "
980 "must be declared with a C interoperable "
981 "kind since common block '%s' is BIND(C)",
982 sym->name, sym->common_block->name,
983 sym->common_block->name);
984 gfc_clear_error ();
985 }
986 }
987
988 sym->attr.implied_index = 0;
989
990 return SUCCESS;
991 }
992
993
994 /* Set character constant to the given length. The constant will be padded or
995 truncated. */
996
997 void
998 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
999 {
1000 char *s;
1001 int slen;
1002
1003 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1004 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1005
1006 slen = expr->value.character.length;
1007 if (len != slen)
1008 {
1009 s = gfc_getmem (len + 1);
1010 memcpy (s, expr->value.character.string, MIN (len, slen));
1011 if (len > slen)
1012 memset (&s[slen], ' ', len - slen);
1013
1014 if (gfc_option.warn_character_truncation && slen > len)
1015 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1016 "(%d/%d)", &expr->where, slen, len);
1017
1018 /* Apply the standard by 'hand' otherwise it gets cleared for
1019 initializers. */
1020 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1021 gfc_error_now ("The CHARACTER elements of the array constructor "
1022 "at %L must have the same length (%d/%d)",
1023 &expr->where, slen, len);
1024
1025 s[len] = '\0';
1026 gfc_free (expr->value.character.string);
1027 expr->value.character.string = s;
1028 expr->value.character.length = len;
1029 }
1030 }
1031
1032
1033 /* Function to create and update the enumerator history
1034 using the information passed as arguments.
1035 Pointer "max_enum" is also updated, to point to
1036 enum history node containing largest initializer.
1037
1038 SYM points to the symbol node of enumerator.
1039 INIT points to its enumerator value. */
1040
1041 static void
1042 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1043 {
1044 enumerator_history *new_enum_history;
1045 gcc_assert (sym != NULL && init != NULL);
1046
1047 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1048
1049 new_enum_history->sym = sym;
1050 new_enum_history->initializer = init;
1051 new_enum_history->next = NULL;
1052
1053 if (enum_history == NULL)
1054 {
1055 enum_history = new_enum_history;
1056 max_enum = enum_history;
1057 }
1058 else
1059 {
1060 new_enum_history->next = enum_history;
1061 enum_history = new_enum_history;
1062
1063 if (mpz_cmp (max_enum->initializer->value.integer,
1064 new_enum_history->initializer->value.integer) < 0)
1065 max_enum = new_enum_history;
1066 }
1067 }
1068
1069
1070 /* Function to free enum kind history. */
1071
1072 void
1073 gfc_free_enum_history (void)
1074 {
1075 enumerator_history *current = enum_history;
1076 enumerator_history *next;
1077
1078 while (current != NULL)
1079 {
1080 next = current->next;
1081 gfc_free (current);
1082 current = next;
1083 }
1084 max_enum = NULL;
1085 enum_history = NULL;
1086 }
1087
1088
1089 /* Function called by variable_decl() that adds an initialization
1090 expression to a symbol. */
1091
1092 static try
1093 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1094 {
1095 symbol_attribute attr;
1096 gfc_symbol *sym;
1097 gfc_expr *init;
1098
1099 init = *initp;
1100 if (find_special (name, &sym))
1101 return FAILURE;
1102
1103 attr = sym->attr;
1104
1105 /* If this symbol is confirming an implicit parameter type,
1106 then an initialization expression is not allowed. */
1107 if (attr.flavor == FL_PARAMETER
1108 && sym->value != NULL
1109 && *initp != NULL)
1110 {
1111 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1112 sym->name);
1113 return FAILURE;
1114 }
1115
1116 if (attr.in_common
1117 && !attr.data
1118 && *initp != NULL)
1119 {
1120 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1121 sym->name);
1122 return FAILURE;
1123 }
1124
1125 if (init == NULL)
1126 {
1127 /* An initializer is required for PARAMETER declarations. */
1128 if (attr.flavor == FL_PARAMETER)
1129 {
1130 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1131 return FAILURE;
1132 }
1133 }
1134 else
1135 {
1136 /* If a variable appears in a DATA block, it cannot have an
1137 initializer. */
1138 if (sym->attr.data)
1139 {
1140 gfc_error ("Variable '%s' at %C with an initializer already "
1141 "appears in a DATA statement", sym->name);
1142 return FAILURE;
1143 }
1144
1145 /* Check if the assignment can happen. This has to be put off
1146 until later for a derived type variable. */
1147 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1148 && gfc_check_assign_symbol (sym, init) == FAILURE)
1149 return FAILURE;
1150
1151 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1152 {
1153 /* Update symbol character length according initializer. */
1154 if (sym->ts.cl->length == NULL)
1155 {
1156 /* If there are multiple CHARACTER variables declared on the
1157 same line, we don't want them to share the same length. */
1158 sym->ts.cl = gfc_get_charlen ();
1159 sym->ts.cl->next = gfc_current_ns->cl_list;
1160 gfc_current_ns->cl_list = sym->ts.cl;
1161
1162 if (sym->attr.flavor == FL_PARAMETER
1163 && init->expr_type == EXPR_ARRAY)
1164 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
1165 }
1166 /* Update initializer character length according symbol. */
1167 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1168 {
1169 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1170 gfc_constructor * p;
1171
1172 if (init->expr_type == EXPR_CONSTANT)
1173 gfc_set_constant_character_len (len, init, false);
1174 else if (init->expr_type == EXPR_ARRAY)
1175 {
1176 /* Build a new charlen to prevent simplification from
1177 deleting the length before it is resolved. */
1178 init->ts.cl = gfc_get_charlen ();
1179 init->ts.cl->next = gfc_current_ns->cl_list;
1180 gfc_current_ns->cl_list = sym->ts.cl;
1181 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1182
1183 for (p = init->value.constructor; p; p = p->next)
1184 gfc_set_constant_character_len (len, p->expr, false);
1185 }
1186 }
1187 }
1188
1189 /* Need to check if the expression we initialized this
1190 to was one of the iso_c_binding named constants. If so,
1191 and we're a parameter (constant), let it be iso_c.
1192 For example:
1193 integer(c_int), parameter :: my_int = c_int
1194 integer(my_int) :: my_int_2
1195 If we mark my_int as iso_c (since we can see it's value
1196 is equal to one of the named constants), then my_int_2
1197 will be considered C interoperable. */
1198 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1199 {
1200 sym->ts.is_iso_c |= init->ts.is_iso_c;
1201 sym->ts.is_c_interop |= init->ts.is_c_interop;
1202 /* attr bits needed for module files. */
1203 sym->attr.is_iso_c |= init->ts.is_iso_c;
1204 sym->attr.is_c_interop |= init->ts.is_c_interop;
1205 if (init->ts.is_iso_c)
1206 sym->ts.f90_type = init->ts.f90_type;
1207 }
1208
1209 /* Add initializer. Make sure we keep the ranks sane. */
1210 if (sym->attr.dimension && init->rank == 0)
1211 {
1212 mpz_t size;
1213 gfc_expr *array;
1214 gfc_constructor *c;
1215 int n;
1216 if (sym->attr.flavor == FL_PARAMETER
1217 && init->expr_type == EXPR_CONSTANT
1218 && spec_size (sym->as, &size) == SUCCESS
1219 && mpz_cmp_si (size, 0) > 0)
1220 {
1221 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1222 &init->where);
1223
1224 array->value.constructor = c = NULL;
1225 for (n = 0; n < (int)mpz_get_si (size); n++)
1226 {
1227 if (array->value.constructor == NULL)
1228 {
1229 array->value.constructor = c = gfc_get_constructor ();
1230 c->expr = init;
1231 }
1232 else
1233 {
1234 c->next = gfc_get_constructor ();
1235 c = c->next;
1236 c->expr = gfc_copy_expr (init);
1237 }
1238 }
1239
1240 array->shape = gfc_get_shape (sym->as->rank);
1241 for (n = 0; n < sym->as->rank; n++)
1242 spec_dimen_size (sym->as, n, &array->shape[n]);
1243
1244 init = array;
1245 mpz_clear (size);
1246 }
1247 init->rank = sym->as->rank;
1248 }
1249
1250 sym->value = init;
1251 if (sym->attr.save == SAVE_NONE)
1252 sym->attr.save = SAVE_IMPLICIT;
1253 *initp = NULL;
1254 }
1255
1256 return SUCCESS;
1257 }
1258
1259
1260 /* Function called by variable_decl() that adds a name to a structure
1261 being built. */
1262
1263 static try
1264 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1265 gfc_array_spec **as)
1266 {
1267 gfc_component *c;
1268
1269 /* If the current symbol is of the same derived type that we're
1270 constructing, it must have the pointer attribute. */
1271 if (current_ts.type == BT_DERIVED
1272 && current_ts.derived == gfc_current_block ()
1273 && current_attr.pointer == 0)
1274 {
1275 gfc_error ("Component at %C must have the POINTER attribute");
1276 return FAILURE;
1277 }
1278
1279 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1280 {
1281 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1282 {
1283 gfc_error ("Array component of structure at %C must have explicit "
1284 "or deferred shape");
1285 return FAILURE;
1286 }
1287 }
1288
1289 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1290 return FAILURE;
1291
1292 c->ts = current_ts;
1293 c->ts.cl = cl;
1294 gfc_set_component_attr (c, &current_attr);
1295
1296 c->initializer = *init;
1297 *init = NULL;
1298
1299 c->as = *as;
1300 if (c->as != NULL)
1301 c->dimension = 1;
1302 *as = NULL;
1303
1304 /* Check array components. */
1305 if (!c->dimension)
1306 {
1307 if (c->allocatable)
1308 {
1309 gfc_error ("Allocatable component at %C must be an array");
1310 return FAILURE;
1311 }
1312 else
1313 return SUCCESS;
1314 }
1315
1316 if (c->pointer)
1317 {
1318 if (c->as->type != AS_DEFERRED)
1319 {
1320 gfc_error ("Pointer array component of structure at %C must have a "
1321 "deferred shape");
1322 return FAILURE;
1323 }
1324 }
1325 else if (c->allocatable)
1326 {
1327 if (c->as->type != AS_DEFERRED)
1328 {
1329 gfc_error ("Allocatable component of structure at %C must have a "
1330 "deferred shape");
1331 return FAILURE;
1332 }
1333 }
1334 else
1335 {
1336 if (c->as->type != AS_EXPLICIT)
1337 {
1338 gfc_error ("Array component of structure at %C must have an "
1339 "explicit shape");
1340 return FAILURE;
1341 }
1342 }
1343
1344 return SUCCESS;
1345 }
1346
1347
1348 /* Match a 'NULL()', and possibly take care of some side effects. */
1349
1350 match
1351 gfc_match_null (gfc_expr **result)
1352 {
1353 gfc_symbol *sym;
1354 gfc_expr *e;
1355 match m;
1356
1357 m = gfc_match (" null ( )");
1358 if (m != MATCH_YES)
1359 return m;
1360
1361 /* The NULL symbol now has to be/become an intrinsic function. */
1362 if (gfc_get_symbol ("null", NULL, &sym))
1363 {
1364 gfc_error ("NULL() initialization at %C is ambiguous");
1365 return MATCH_ERROR;
1366 }
1367
1368 gfc_intrinsic_symbol (sym);
1369
1370 if (sym->attr.proc != PROC_INTRINSIC
1371 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1372 sym->name, NULL) == FAILURE
1373 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1374 return MATCH_ERROR;
1375
1376 e = gfc_get_expr ();
1377 e->where = gfc_current_locus;
1378 e->expr_type = EXPR_NULL;
1379 e->ts.type = BT_UNKNOWN;
1380
1381 *result = e;
1382
1383 return MATCH_YES;
1384 }
1385
1386
1387 /* Match a variable name with an optional initializer. When this
1388 subroutine is called, a variable is expected to be parsed next.
1389 Depending on what is happening at the moment, updates either the
1390 symbol table or the current interface. */
1391
1392 static match
1393 variable_decl (int elem)
1394 {
1395 char name[GFC_MAX_SYMBOL_LEN + 1];
1396 gfc_expr *initializer, *char_len;
1397 gfc_array_spec *as;
1398 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1399 gfc_charlen *cl;
1400 locus var_locus;
1401 match m;
1402 try t;
1403 gfc_symbol *sym;
1404 locus old_locus;
1405
1406 initializer = NULL;
1407 as = NULL;
1408 cp_as = NULL;
1409 old_locus = gfc_current_locus;
1410
1411 /* When we get here, we've just matched a list of attributes and
1412 maybe a type and a double colon. The next thing we expect to see
1413 is the name of the symbol. */
1414 m = gfc_match_name (name);
1415 if (m != MATCH_YES)
1416 goto cleanup;
1417
1418 var_locus = gfc_current_locus;
1419
1420 /* Now we could see the optional array spec. or character length. */
1421 m = gfc_match_array_spec (&as);
1422 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1423 cp_as = gfc_copy_array_spec (as);
1424 else if (m == MATCH_ERROR)
1425 goto cleanup;
1426
1427 if (m == MATCH_NO)
1428 as = gfc_copy_array_spec (current_as);
1429
1430 char_len = NULL;
1431 cl = NULL;
1432
1433 if (current_ts.type == BT_CHARACTER)
1434 {
1435 switch (match_char_length (&char_len))
1436 {
1437 case MATCH_YES:
1438 cl = gfc_get_charlen ();
1439 cl->next = gfc_current_ns->cl_list;
1440 gfc_current_ns->cl_list = cl;
1441
1442 cl->length = char_len;
1443 break;
1444
1445 /* Non-constant lengths need to be copied after the first
1446 element. */
1447 case MATCH_NO:
1448 if (elem > 1 && current_ts.cl->length
1449 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1450 {
1451 cl = gfc_get_charlen ();
1452 cl->next = gfc_current_ns->cl_list;
1453 gfc_current_ns->cl_list = cl;
1454 cl->length = gfc_copy_expr (current_ts.cl->length);
1455 }
1456 else
1457 cl = current_ts.cl;
1458
1459 break;
1460
1461 case MATCH_ERROR:
1462 goto cleanup;
1463 }
1464 }
1465
1466 /* If this symbol has already shown up in a Cray Pointer declaration,
1467 then we want to set the type & bail out. */
1468 if (gfc_option.flag_cray_pointer)
1469 {
1470 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1471 if (sym != NULL && sym->attr.cray_pointee)
1472 {
1473 sym->ts.type = current_ts.type;
1474 sym->ts.kind = current_ts.kind;
1475 sym->ts.cl = cl;
1476 sym->ts.derived = current_ts.derived;
1477 sym->ts.is_c_interop = current_ts.is_c_interop;
1478 sym->ts.is_iso_c = current_ts.is_iso_c;
1479 m = MATCH_YES;
1480
1481 /* Check to see if we have an array specification. */
1482 if (cp_as != NULL)
1483 {
1484 if (sym->as != NULL)
1485 {
1486 gfc_error ("Duplicate array spec for Cray pointee at %C");
1487 gfc_free_array_spec (cp_as);
1488 m = MATCH_ERROR;
1489 goto cleanup;
1490 }
1491 else
1492 {
1493 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1494 gfc_internal_error ("Couldn't set pointee array spec.");
1495
1496 /* Fix the array spec. */
1497 m = gfc_mod_pointee_as (sym->as);
1498 if (m == MATCH_ERROR)
1499 goto cleanup;
1500 }
1501 }
1502 goto cleanup;
1503 }
1504 else
1505 {
1506 gfc_free_array_spec (cp_as);
1507 }
1508 }
1509
1510
1511 /* OK, we've successfully matched the declaration. Now put the
1512 symbol in the current namespace, because it might be used in the
1513 optional initialization expression for this symbol, e.g. this is
1514 perfectly legal:
1515
1516 integer, parameter :: i = huge(i)
1517
1518 This is only true for parameters or variables of a basic type.
1519 For components of derived types, it is not true, so we don't
1520 create a symbol for those yet. If we fail to create the symbol,
1521 bail out. */
1522 if (gfc_current_state () != COMP_DERIVED
1523 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1524 {
1525 m = MATCH_ERROR;
1526 goto cleanup;
1527 }
1528
1529 /* An interface body specifies all of the procedure's
1530 characteristics and these shall be consistent with those
1531 specified in the procedure definition, except that the interface
1532 may specify a procedure that is not pure if the procedure is
1533 defined to be pure(12.3.2). */
1534 if (current_ts.type == BT_DERIVED
1535 && gfc_current_ns->proc_name
1536 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1537 && current_ts.derived->ns != gfc_current_ns
1538 && !gfc_current_ns->has_import_set)
1539 {
1540 gfc_error ("the type of '%s' at %C has not been declared within the "
1541 "interface", name);
1542 m = MATCH_ERROR;
1543 goto cleanup;
1544 }
1545
1546 /* In functions that have a RESULT variable defined, the function
1547 name always refers to function calls. Therefore, the name is
1548 not allowed to appear in specification statements. */
1549 if (gfc_current_state () == COMP_FUNCTION
1550 && gfc_current_block () != NULL
1551 && gfc_current_block ()->result != NULL
1552 && gfc_current_block ()->result != gfc_current_block ()
1553 && strcmp (gfc_current_block ()->name, name) == 0)
1554 {
1555 gfc_error ("Function name '%s' not allowed at %C", name);
1556 m = MATCH_ERROR;
1557 goto cleanup;
1558 }
1559
1560 /* We allow old-style initializations of the form
1561 integer i /2/, j(4) /3*3, 1/
1562 (if no colon has been seen). These are different from data
1563 statements in that initializers are only allowed to apply to the
1564 variable immediately preceding, i.e.
1565 integer i, j /1, 2/
1566 is not allowed. Therefore we have to do some work manually, that
1567 could otherwise be left to the matchers for DATA statements. */
1568
1569 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1570 {
1571 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1572 "initialization at %C") == FAILURE)
1573 return MATCH_ERROR;
1574
1575 return match_old_style_init (name);
1576 }
1577
1578 /* The double colon must be present in order to have initializers.
1579 Otherwise the statement is ambiguous with an assignment statement. */
1580 if (colon_seen)
1581 {
1582 if (gfc_match (" =>") == MATCH_YES)
1583 {
1584 if (!current_attr.pointer)
1585 {
1586 gfc_error ("Initialization at %C isn't for a pointer variable");
1587 m = MATCH_ERROR;
1588 goto cleanup;
1589 }
1590
1591 m = gfc_match_null (&initializer);
1592 if (m == MATCH_NO)
1593 {
1594 gfc_error ("Pointer initialization requires a NULL() at %C");
1595 m = MATCH_ERROR;
1596 }
1597
1598 if (gfc_pure (NULL))
1599 {
1600 gfc_error ("Initialization of pointer at %C is not allowed in "
1601 "a PURE procedure");
1602 m = MATCH_ERROR;
1603 }
1604
1605 if (m != MATCH_YES)
1606 goto cleanup;
1607
1608 }
1609 else if (gfc_match_char ('=') == MATCH_YES)
1610 {
1611 if (current_attr.pointer)
1612 {
1613 gfc_error ("Pointer initialization at %C requires '=>', "
1614 "not '='");
1615 m = MATCH_ERROR;
1616 goto cleanup;
1617 }
1618
1619 m = gfc_match_init_expr (&initializer);
1620 if (m == MATCH_NO)
1621 {
1622 gfc_error ("Expected an initialization expression at %C");
1623 m = MATCH_ERROR;
1624 }
1625
1626 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1627 {
1628 gfc_error ("Initialization of variable at %C is not allowed in "
1629 "a PURE procedure");
1630 m = MATCH_ERROR;
1631 }
1632
1633 if (m != MATCH_YES)
1634 goto cleanup;
1635 }
1636 }
1637
1638 if (initializer != NULL && current_attr.allocatable
1639 && gfc_current_state () == COMP_DERIVED)
1640 {
1641 gfc_error ("Initialization of allocatable component at %C is not "
1642 "allowed");
1643 m = MATCH_ERROR;
1644 goto cleanup;
1645 }
1646
1647 /* Add the initializer. Note that it is fine if initializer is
1648 NULL here, because we sometimes also need to check if a
1649 declaration *must* have an initialization expression. */
1650 if (gfc_current_state () != COMP_DERIVED)
1651 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1652 else
1653 {
1654 if (current_ts.type == BT_DERIVED
1655 && !current_attr.pointer && !initializer)
1656 initializer = gfc_default_initializer (&current_ts);
1657 t = build_struct (name, cl, &initializer, &as);
1658 }
1659
1660 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1661
1662 cleanup:
1663 /* Free stuff up and return. */
1664 gfc_free_expr (initializer);
1665 gfc_free_array_spec (as);
1666
1667 return m;
1668 }
1669
1670
1671 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1672 This assumes that the byte size is equal to the kind number for
1673 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1674
1675 match
1676 gfc_match_old_kind_spec (gfc_typespec *ts)
1677 {
1678 match m;
1679 int original_kind;
1680
1681 if (gfc_match_char ('*') != MATCH_YES)
1682 return MATCH_NO;
1683
1684 m = gfc_match_small_literal_int (&ts->kind, NULL);
1685 if (m != MATCH_YES)
1686 return MATCH_ERROR;
1687
1688 original_kind = ts->kind;
1689
1690 /* Massage the kind numbers for complex types. */
1691 if (ts->type == BT_COMPLEX)
1692 {
1693 if (ts->kind % 2)
1694 {
1695 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1696 gfc_basic_typename (ts->type), original_kind);
1697 return MATCH_ERROR;
1698 }
1699 ts->kind /= 2;
1700 }
1701
1702 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1703 {
1704 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1705 gfc_basic_typename (ts->type), original_kind);
1706 return MATCH_ERROR;
1707 }
1708
1709 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1710 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1711 return MATCH_ERROR;
1712
1713 return MATCH_YES;
1714 }
1715
1716
1717 /* Match a kind specification. Since kinds are generally optional, we
1718 usually return MATCH_NO if something goes wrong. If a "kind="
1719 string is found, then we know we have an error. */
1720
1721 match
1722 gfc_match_kind_spec (gfc_typespec *ts)
1723 {
1724 locus where;
1725 gfc_expr *e;
1726 match m, n;
1727 const char *msg;
1728
1729 m = MATCH_NO;
1730 e = NULL;
1731
1732 where = gfc_current_locus;
1733
1734 if (gfc_match_char ('(') == MATCH_NO)
1735 return MATCH_NO;
1736
1737 /* Also gobbles optional text. */
1738 if (gfc_match (" kind = ") == MATCH_YES)
1739 m = MATCH_ERROR;
1740
1741 n = gfc_match_init_expr (&e);
1742 if (n == MATCH_NO)
1743 gfc_error ("Expected initialization expression at %C");
1744 if (n != MATCH_YES)
1745 return MATCH_ERROR;
1746
1747 if (e->rank != 0)
1748 {
1749 gfc_error ("Expected scalar initialization expression at %C");
1750 m = MATCH_ERROR;
1751 goto no_match;
1752 }
1753
1754 msg = gfc_extract_int (e, &ts->kind);
1755 if (msg != NULL)
1756 {
1757 gfc_error (msg);
1758 m = MATCH_ERROR;
1759 goto no_match;
1760 }
1761
1762 /* Before throwing away the expression, let's see if we had a
1763 C interoperable kind (and store the fact). */
1764 if (e->ts.is_c_interop == 1)
1765 {
1766 /* Mark this as c interoperable if being declared with one
1767 of the named constants from iso_c_binding. */
1768 ts->is_c_interop = e->ts.is_iso_c;
1769 ts->f90_type = e->ts.f90_type;
1770 }
1771
1772 gfc_free_expr (e);
1773 e = NULL;
1774
1775 /* Ignore errors to this point, if we've gotten here. This means
1776 we ignore the m=MATCH_ERROR from above. */
1777 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1778 {
1779 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1780 gfc_basic_typename (ts->type));
1781 m = MATCH_ERROR;
1782 }
1783 else if (gfc_match_char (')') != MATCH_YES)
1784 {
1785 gfc_error ("Missing right parenthesis at %C");
1786 m = MATCH_ERROR;
1787 }
1788 else
1789 /* All tests passed. */
1790 m = MATCH_YES;
1791
1792 if(m == MATCH_ERROR)
1793 gfc_current_locus = where;
1794
1795 /* Return what we know from the test(s). */
1796 return m;
1797
1798 no_match:
1799 gfc_free_expr (e);
1800 gfc_current_locus = where;
1801 return m;
1802 }
1803
1804
1805 /* Match the various kind/length specifications in a CHARACTER
1806 declaration. We don't return MATCH_NO. */
1807
1808 static match
1809 match_char_spec (gfc_typespec *ts)
1810 {
1811 int kind, seen_length;
1812 gfc_charlen *cl;
1813 gfc_expr *len;
1814 match m;
1815 gfc_expr *kind_expr = NULL;
1816 kind = gfc_default_character_kind;
1817 len = NULL;
1818 seen_length = 0;
1819
1820 /* Try the old-style specification first. */
1821 old_char_selector = 0;
1822
1823 m = match_char_length (&len);
1824 if (m != MATCH_NO)
1825 {
1826 if (m == MATCH_YES)
1827 old_char_selector = 1;
1828 seen_length = 1;
1829 goto done;
1830 }
1831
1832 m = gfc_match_char ('(');
1833 if (m != MATCH_YES)
1834 {
1835 m = MATCH_YES; /* Character without length is a single char. */
1836 goto done;
1837 }
1838
1839 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1840 if (gfc_match (" kind =") == MATCH_YES)
1841 {
1842 m = gfc_match_small_int_expr(&kind, &kind_expr);
1843
1844 if (m == MATCH_ERROR)
1845 goto done;
1846 if (m == MATCH_NO)
1847 goto syntax;
1848
1849 if (gfc_match (" , len =") == MATCH_NO)
1850 goto rparen;
1851
1852 m = char_len_param_value (&len);
1853 if (m == MATCH_NO)
1854 goto syntax;
1855 if (m == MATCH_ERROR)
1856 goto done;
1857 seen_length = 1;
1858
1859 goto rparen;
1860 }
1861
1862 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1863 if (gfc_match (" len =") == MATCH_YES)
1864 {
1865 m = char_len_param_value (&len);
1866 if (m == MATCH_NO)
1867 goto syntax;
1868 if (m == MATCH_ERROR)
1869 goto done;
1870 seen_length = 1;
1871
1872 if (gfc_match_char (')') == MATCH_YES)
1873 goto done;
1874
1875 if (gfc_match (" , kind =") != MATCH_YES)
1876 goto syntax;
1877
1878 gfc_match_small_int_expr(&kind, &kind_expr);
1879
1880 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1881 {
1882 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1883 return MATCH_YES;
1884 }
1885
1886 goto rparen;
1887 }
1888
1889 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1890 m = char_len_param_value (&len);
1891 if (m == MATCH_NO)
1892 goto syntax;
1893 if (m == MATCH_ERROR)
1894 goto done;
1895 seen_length = 1;
1896
1897 m = gfc_match_char (')');
1898 if (m == MATCH_YES)
1899 goto done;
1900
1901 if (gfc_match_char (',') != MATCH_YES)
1902 goto syntax;
1903
1904 gfc_match (" kind ="); /* Gobble optional text. */
1905
1906 m = gfc_match_small_int_expr(&kind, &kind_expr);
1907 if (m == MATCH_ERROR)
1908 goto done;
1909 if (m == MATCH_NO)
1910 goto syntax;
1911
1912 rparen:
1913 /* Require a right-paren at this point. */
1914 m = gfc_match_char (')');
1915 if (m == MATCH_YES)
1916 goto done;
1917
1918 syntax:
1919 gfc_error ("Syntax error in CHARACTER declaration at %C");
1920 m = MATCH_ERROR;
1921 gfc_free_expr (len);
1922 return m;
1923
1924 done:
1925 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1926 {
1927 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1928 m = MATCH_ERROR;
1929 }
1930
1931 if (seen_length == 1 && len != NULL
1932 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1933 {
1934 gfc_error ("Expression at %C must be of INTEGER type");
1935 m = MATCH_ERROR;
1936 }
1937
1938 if (m != MATCH_YES)
1939 {
1940 gfc_free_expr (len);
1941 gfc_free_expr (kind_expr);
1942 return m;
1943 }
1944
1945 /* Do some final massaging of the length values. */
1946 cl = gfc_get_charlen ();
1947 cl->next = gfc_current_ns->cl_list;
1948 gfc_current_ns->cl_list = cl;
1949
1950 if (seen_length == 0)
1951 cl->length = gfc_int_expr (1);
1952 else
1953 cl->length = len;
1954
1955 ts->cl = cl;
1956 ts->kind = kind;
1957
1958 /* We have to know if it was a c interoperable kind so we can
1959 do accurate type checking of bind(c) procs, etc. */
1960 if (kind_expr != NULL)
1961 {
1962 /* Mark this as c interoperable if being declared with one
1963 of the named constants from iso_c_binding. */
1964 ts->is_c_interop = kind_expr->ts.is_iso_c;
1965 gfc_free_expr (kind_expr);
1966 }
1967 else if (len != NULL)
1968 {
1969 /* Here, we might have parsed something such as:
1970 character(c_char)
1971 In this case, the parsing code above grabs the c_char when
1972 looking for the length (line 1690, roughly). it's the last
1973 testcase for parsing the kind params of a character variable.
1974 However, it's not actually the length. this seems like it
1975 could be an error.
1976 To see if the user used a C interop kind, test the expr
1977 of the so called length, and see if it's C interoperable. */
1978 ts->is_c_interop = len->ts.is_iso_c;
1979 }
1980
1981 return MATCH_YES;
1982 }
1983
1984
1985 /* Matches a type specification. If successful, sets the ts structure
1986 to the matched specification. This is necessary for FUNCTION and
1987 IMPLICIT statements.
1988
1989 If implicit_flag is nonzero, then we don't check for the optional
1990 kind specification. Not doing so is needed for matching an IMPLICIT
1991 statement correctly. */
1992
1993 static match
1994 match_type_spec (gfc_typespec *ts, int implicit_flag)
1995 {
1996 char name[GFC_MAX_SYMBOL_LEN + 1];
1997 gfc_symbol *sym;
1998 match m;
1999 int c;
2000
2001 gfc_clear_ts (ts);
2002
2003 /* Clear the current binding label, in case one is given. */
2004 curr_binding_label[0] = '\0';
2005
2006 if (gfc_match (" byte") == MATCH_YES)
2007 {
2008 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2009 == FAILURE)
2010 return MATCH_ERROR;
2011
2012 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2013 {
2014 gfc_error ("BYTE type used at %C "
2015 "is not available on the target machine");
2016 return MATCH_ERROR;
2017 }
2018
2019 ts->type = BT_INTEGER;
2020 ts->kind = 1;
2021 return MATCH_YES;
2022 }
2023
2024 if (gfc_match (" integer") == MATCH_YES)
2025 {
2026 ts->type = BT_INTEGER;
2027 ts->kind = gfc_default_integer_kind;
2028 goto get_kind;
2029 }
2030
2031 if (gfc_match (" character") == MATCH_YES)
2032 {
2033 ts->type = BT_CHARACTER;
2034 if (implicit_flag == 0)
2035 return match_char_spec (ts);
2036 else
2037 return MATCH_YES;
2038 }
2039
2040 if (gfc_match (" real") == MATCH_YES)
2041 {
2042 ts->type = BT_REAL;
2043 ts->kind = gfc_default_real_kind;
2044 goto get_kind;
2045 }
2046
2047 if (gfc_match (" double precision") == MATCH_YES)
2048 {
2049 ts->type = BT_REAL;
2050 ts->kind = gfc_default_double_kind;
2051 return MATCH_YES;
2052 }
2053
2054 if (gfc_match (" complex") == MATCH_YES)
2055 {
2056 ts->type = BT_COMPLEX;
2057 ts->kind = gfc_default_complex_kind;
2058 goto get_kind;
2059 }
2060
2061 if (gfc_match (" double complex") == MATCH_YES)
2062 {
2063 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2064 "conform to the Fortran 95 standard") == FAILURE)
2065 return MATCH_ERROR;
2066
2067 ts->type = BT_COMPLEX;
2068 ts->kind = gfc_default_double_kind;
2069 return MATCH_YES;
2070 }
2071
2072 if (gfc_match (" logical") == MATCH_YES)
2073 {
2074 ts->type = BT_LOGICAL;
2075 ts->kind = gfc_default_logical_kind;
2076 goto get_kind;
2077 }
2078
2079 m = gfc_match (" type ( %n )", name);
2080 if (m != MATCH_YES)
2081 return m;
2082
2083 /* Search for the name but allow the components to be defined later. */
2084 if (gfc_get_ha_symbol (name, &sym))
2085 {
2086 gfc_error ("Type name '%s' at %C is ambiguous", name);
2087 return MATCH_ERROR;
2088 }
2089
2090 if (sym->attr.flavor != FL_DERIVED
2091 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2092 return MATCH_ERROR;
2093
2094 ts->type = BT_DERIVED;
2095 ts->kind = 0;
2096 ts->derived = sym;
2097
2098 return MATCH_YES;
2099
2100 get_kind:
2101 /* For all types except double, derived and character, look for an
2102 optional kind specifier. MATCH_NO is actually OK at this point. */
2103 if (implicit_flag == 1)
2104 return MATCH_YES;
2105
2106 if (gfc_current_form == FORM_FREE)
2107 {
2108 c = gfc_peek_char();
2109 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2110 && c != ':' && c != ',')
2111 return MATCH_NO;
2112 }
2113
2114 m = gfc_match_kind_spec (ts);
2115 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2116 m = gfc_match_old_kind_spec (ts);
2117
2118 if (m == MATCH_NO)
2119 m = MATCH_YES; /* No kind specifier found. */
2120
2121 return m;
2122 }
2123
2124
2125 /* Match an IMPLICIT NONE statement. Actually, this statement is
2126 already matched in parse.c, or we would not end up here in the
2127 first place. So the only thing we need to check, is if there is
2128 trailing garbage. If not, the match is successful. */
2129
2130 match
2131 gfc_match_implicit_none (void)
2132 {
2133 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2134 }
2135
2136
2137 /* Match the letter range(s) of an IMPLICIT statement. */
2138
2139 static match
2140 match_implicit_range (void)
2141 {
2142 int c, c1, c2, inner;
2143 locus cur_loc;
2144
2145 cur_loc = gfc_current_locus;
2146
2147 gfc_gobble_whitespace ();
2148 c = gfc_next_char ();
2149 if (c != '(')
2150 {
2151 gfc_error ("Missing character range in IMPLICIT at %C");
2152 goto bad;
2153 }
2154
2155 inner = 1;
2156 while (inner)
2157 {
2158 gfc_gobble_whitespace ();
2159 c1 = gfc_next_char ();
2160 if (!ISALPHA (c1))
2161 goto bad;
2162
2163 gfc_gobble_whitespace ();
2164 c = gfc_next_char ();
2165
2166 switch (c)
2167 {
2168 case ')':
2169 inner = 0; /* Fall through. */
2170
2171 case ',':
2172 c2 = c1;
2173 break;
2174
2175 case '-':
2176 gfc_gobble_whitespace ();
2177 c2 = gfc_next_char ();
2178 if (!ISALPHA (c2))
2179 goto bad;
2180
2181 gfc_gobble_whitespace ();
2182 c = gfc_next_char ();
2183
2184 if ((c != ',') && (c != ')'))
2185 goto bad;
2186 if (c == ')')
2187 inner = 0;
2188
2189 break;
2190
2191 default:
2192 goto bad;
2193 }
2194
2195 if (c1 > c2)
2196 {
2197 gfc_error ("Letters must be in alphabetic order in "
2198 "IMPLICIT statement at %C");
2199 goto bad;
2200 }
2201
2202 /* See if we can add the newly matched range to the pending
2203 implicits from this IMPLICIT statement. We do not check for
2204 conflicts with whatever earlier IMPLICIT statements may have
2205 set. This is done when we've successfully finished matching
2206 the current one. */
2207 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2208 goto bad;
2209 }
2210
2211 return MATCH_YES;
2212
2213 bad:
2214 gfc_syntax_error (ST_IMPLICIT);
2215
2216 gfc_current_locus = cur_loc;
2217 return MATCH_ERROR;
2218 }
2219
2220
2221 /* Match an IMPLICIT statement, storing the types for
2222 gfc_set_implicit() if the statement is accepted by the parser.
2223 There is a strange looking, but legal syntactic construction
2224 possible. It looks like:
2225
2226 IMPLICIT INTEGER (a-b) (c-d)
2227
2228 This is legal if "a-b" is a constant expression that happens to
2229 equal one of the legal kinds for integers. The real problem
2230 happens with an implicit specification that looks like:
2231
2232 IMPLICIT INTEGER (a-b)
2233
2234 In this case, a typespec matcher that is "greedy" (as most of the
2235 matchers are) gobbles the character range as a kindspec, leaving
2236 nothing left. We therefore have to go a bit more slowly in the
2237 matching process by inhibiting the kindspec checking during
2238 typespec matching and checking for a kind later. */
2239
2240 match
2241 gfc_match_implicit (void)
2242 {
2243 gfc_typespec ts;
2244 locus cur_loc;
2245 int c;
2246 match m;
2247
2248 /* We don't allow empty implicit statements. */
2249 if (gfc_match_eos () == MATCH_YES)
2250 {
2251 gfc_error ("Empty IMPLICIT statement at %C");
2252 return MATCH_ERROR;
2253 }
2254
2255 do
2256 {
2257 /* First cleanup. */
2258 gfc_clear_new_implicit ();
2259
2260 /* A basic type is mandatory here. */
2261 m = match_type_spec (&ts, 1);
2262 if (m == MATCH_ERROR)
2263 goto error;
2264 if (m == MATCH_NO)
2265 goto syntax;
2266
2267 cur_loc = gfc_current_locus;
2268 m = match_implicit_range ();
2269
2270 if (m == MATCH_YES)
2271 {
2272 /* We may have <TYPE> (<RANGE>). */
2273 gfc_gobble_whitespace ();
2274 c = gfc_next_char ();
2275 if ((c == '\n') || (c == ','))
2276 {
2277 /* Check for CHARACTER with no length parameter. */
2278 if (ts.type == BT_CHARACTER && !ts.cl)
2279 {
2280 ts.kind = gfc_default_character_kind;
2281 ts.cl = gfc_get_charlen ();
2282 ts.cl->next = gfc_current_ns->cl_list;
2283 gfc_current_ns->cl_list = ts.cl;
2284 ts.cl->length = gfc_int_expr (1);
2285 }
2286
2287 /* Record the Successful match. */
2288 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2289 return MATCH_ERROR;
2290 continue;
2291 }
2292
2293 gfc_current_locus = cur_loc;
2294 }
2295
2296 /* Discard the (incorrectly) matched range. */
2297 gfc_clear_new_implicit ();
2298
2299 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2300 if (ts.type == BT_CHARACTER)
2301 m = match_char_spec (&ts);
2302 else
2303 {
2304 m = gfc_match_kind_spec (&ts);
2305 if (m == MATCH_NO)
2306 {
2307 m = gfc_match_old_kind_spec (&ts);
2308 if (m == MATCH_ERROR)
2309 goto error;
2310 if (m == MATCH_NO)
2311 goto syntax;
2312 }
2313 }
2314 if (m == MATCH_ERROR)
2315 goto error;
2316
2317 m = match_implicit_range ();
2318 if (m == MATCH_ERROR)
2319 goto error;
2320 if (m == MATCH_NO)
2321 goto syntax;
2322
2323 gfc_gobble_whitespace ();
2324 c = gfc_next_char ();
2325 if ((c != '\n') && (c != ','))
2326 goto syntax;
2327
2328 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2329 return MATCH_ERROR;
2330 }
2331 while (c == ',');
2332
2333 return MATCH_YES;
2334
2335 syntax:
2336 gfc_syntax_error (ST_IMPLICIT);
2337
2338 error:
2339 return MATCH_ERROR;
2340 }
2341
2342
2343 match
2344 gfc_match_import (void)
2345 {
2346 char name[GFC_MAX_SYMBOL_LEN + 1];
2347 match m;
2348 gfc_symbol *sym;
2349 gfc_symtree *st;
2350
2351 if (gfc_current_ns->proc_name == NULL
2352 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2353 {
2354 gfc_error ("IMPORT statement at %C only permitted in "
2355 "an INTERFACE body");
2356 return MATCH_ERROR;
2357 }
2358
2359 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2360 == FAILURE)
2361 return MATCH_ERROR;
2362
2363 if (gfc_match_eos () == MATCH_YES)
2364 {
2365 /* All host variables should be imported. */
2366 gfc_current_ns->has_import_set = 1;
2367 return MATCH_YES;
2368 }
2369
2370 if (gfc_match (" ::") == MATCH_YES)
2371 {
2372 if (gfc_match_eos () == MATCH_YES)
2373 {
2374 gfc_error ("Expecting list of named entities at %C");
2375 return MATCH_ERROR;
2376 }
2377 }
2378
2379 for(;;)
2380 {
2381 m = gfc_match (" %n", name);
2382 switch (m)
2383 {
2384 case MATCH_YES:
2385 if (gfc_current_ns->parent != NULL
2386 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2387 {
2388 gfc_error ("Type name '%s' at %C is ambiguous", name);
2389 return MATCH_ERROR;
2390 }
2391 else if (gfc_current_ns->proc_name->ns->parent != NULL
2392 && gfc_find_symbol (name,
2393 gfc_current_ns->proc_name->ns->parent,
2394 1, &sym))
2395 {
2396 gfc_error ("Type name '%s' at %C is ambiguous", name);
2397 return MATCH_ERROR;
2398 }
2399
2400 if (sym == NULL)
2401 {
2402 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2403 "at %C - does not exist.", name);
2404 return MATCH_ERROR;
2405 }
2406
2407 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2408 {
2409 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2410 "at %C.", name);
2411 goto next_item;
2412 }
2413
2414 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2415 st->n.sym = sym;
2416 sym->refs++;
2417 sym->ns = gfc_current_ns;
2418
2419 goto next_item;
2420
2421 case MATCH_NO:
2422 break;
2423
2424 case MATCH_ERROR:
2425 return MATCH_ERROR;
2426 }
2427
2428 next_item:
2429 if (gfc_match_eos () == MATCH_YES)
2430 break;
2431 if (gfc_match_char (',') != MATCH_YES)
2432 goto syntax;
2433 }
2434
2435 return MATCH_YES;
2436
2437 syntax:
2438 gfc_error ("Syntax error in IMPORT statement at %C");
2439 return MATCH_ERROR;
2440 }
2441
2442
2443 /* Matches an attribute specification including array specs. If
2444 successful, leaves the variables current_attr and current_as
2445 holding the specification. Also sets the colon_seen variable for
2446 later use by matchers associated with initializations.
2447
2448 This subroutine is a little tricky in the sense that we don't know
2449 if we really have an attr-spec until we hit the double colon.
2450 Until that time, we can only return MATCH_NO. This forces us to
2451 check for duplicate specification at this level. */
2452
2453 static match
2454 match_attr_spec (void)
2455 {
2456 /* Modifiers that can exist in a type statement. */
2457 typedef enum
2458 { GFC_DECL_BEGIN = 0,
2459 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2460 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2461 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2462 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2463 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
2464 GFC_DECL_END /* Sentinel */
2465 }
2466 decl_types;
2467
2468 /* GFC_DECL_END is the sentinel, index starts at 0. */
2469 #define NUM_DECL GFC_DECL_END
2470
2471 static mstring decls[] = {
2472 minit (", allocatable", DECL_ALLOCATABLE),
2473 minit (", dimension", DECL_DIMENSION),
2474 minit (", external", DECL_EXTERNAL),
2475 minit (", intent ( in )", DECL_IN),
2476 minit (", intent ( out )", DECL_OUT),
2477 minit (", intent ( in out )", DECL_INOUT),
2478 minit (", intrinsic", DECL_INTRINSIC),
2479 minit (", optional", DECL_OPTIONAL),
2480 minit (", parameter", DECL_PARAMETER),
2481 minit (", pointer", DECL_POINTER),
2482 minit (", protected", DECL_PROTECTED),
2483 minit (", private", DECL_PRIVATE),
2484 minit (", public", DECL_PUBLIC),
2485 minit (", save", DECL_SAVE),
2486 minit (", target", DECL_TARGET),
2487 minit (", value", DECL_VALUE),
2488 minit (", volatile", DECL_VOLATILE),
2489 minit ("::", DECL_COLON),
2490 minit (NULL, DECL_NONE)
2491 };
2492
2493 locus start, seen_at[NUM_DECL];
2494 int seen[NUM_DECL];
2495 decl_types d;
2496 const char *attr;
2497 match m;
2498 try t;
2499 char peek_char;
2500
2501 gfc_clear_attr (&current_attr);
2502 start = gfc_current_locus;
2503
2504 current_as = NULL;
2505 colon_seen = 0;
2506
2507 /* See if we get all of the keywords up to the final double colon. */
2508 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2509 seen[d] = 0;
2510
2511 for (;;)
2512 {
2513 d = (decl_types) gfc_match_strings (decls);
2514
2515 if (d == DECL_NONE)
2516 {
2517 /* See if we can find the bind(c) since all else failed.
2518 We need to skip over any whitespace and stop on the ','. */
2519 gfc_gobble_whitespace ();
2520 peek_char = gfc_peek_char ();
2521 if (peek_char == ',')
2522 {
2523 /* Chomp the comma. */
2524 peek_char = gfc_next_char ();
2525 /* Try and match the bind(c). */
2526 if (gfc_match_bind_c (NULL) == MATCH_YES)
2527 d = DECL_IS_BIND_C;
2528 }
2529 }
2530
2531 if (d == DECL_NONE || d == DECL_COLON)
2532 break;
2533
2534 seen[d]++;
2535 seen_at[d] = gfc_current_locus;
2536
2537 if (d == DECL_DIMENSION)
2538 {
2539 m = gfc_match_array_spec (&current_as);
2540
2541 if (m == MATCH_NO)
2542 {
2543 gfc_error ("Missing dimension specification at %C");
2544 m = MATCH_ERROR;
2545 }
2546
2547 if (m == MATCH_ERROR)
2548 goto cleanup;
2549 }
2550 }
2551
2552 /* No double colon, so assume that we've been looking at something
2553 else the whole time. */
2554 if (d == DECL_NONE)
2555 {
2556 m = MATCH_NO;
2557 goto cleanup;
2558 }
2559
2560 /* Since we've seen a double colon, we have to be looking at an
2561 attr-spec. This means that we can now issue errors. */
2562 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2563 if (seen[d] > 1)
2564 {
2565 switch (d)
2566 {
2567 case DECL_ALLOCATABLE:
2568 attr = "ALLOCATABLE";
2569 break;
2570 case DECL_DIMENSION:
2571 attr = "DIMENSION";
2572 break;
2573 case DECL_EXTERNAL:
2574 attr = "EXTERNAL";
2575 break;
2576 case DECL_IN:
2577 attr = "INTENT (IN)";
2578 break;
2579 case DECL_OUT:
2580 attr = "INTENT (OUT)";
2581 break;
2582 case DECL_INOUT:
2583 attr = "INTENT (IN OUT)";
2584 break;
2585 case DECL_INTRINSIC:
2586 attr = "INTRINSIC";
2587 break;
2588 case DECL_OPTIONAL:
2589 attr = "OPTIONAL";
2590 break;
2591 case DECL_PARAMETER:
2592 attr = "PARAMETER";
2593 break;
2594 case DECL_POINTER:
2595 attr = "POINTER";
2596 break;
2597 case DECL_PROTECTED:
2598 attr = "PROTECTED";
2599 break;
2600 case DECL_PRIVATE:
2601 attr = "PRIVATE";
2602 break;
2603 case DECL_PUBLIC:
2604 attr = "PUBLIC";
2605 break;
2606 case DECL_SAVE:
2607 attr = "SAVE";
2608 break;
2609 case DECL_TARGET:
2610 attr = "TARGET";
2611 break;
2612 case DECL_IS_BIND_C:
2613 attr = "IS_BIND_C";
2614 break;
2615 case DECL_VALUE:
2616 attr = "VALUE";
2617 break;
2618 case DECL_VOLATILE:
2619 attr = "VOLATILE";
2620 break;
2621 default:
2622 attr = NULL; /* This shouldn't happen. */
2623 }
2624
2625 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2626 m = MATCH_ERROR;
2627 goto cleanup;
2628 }
2629
2630 /* Now that we've dealt with duplicate attributes, add the attributes
2631 to the current attribute. */
2632 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2633 {
2634 if (seen[d] == 0)
2635 continue;
2636
2637 if (gfc_current_state () == COMP_DERIVED
2638 && d != DECL_DIMENSION && d != DECL_POINTER
2639 && d != DECL_COLON && d != DECL_PRIVATE
2640 && d != DECL_PUBLIC && d != DECL_NONE)
2641 {
2642 if (d == DECL_ALLOCATABLE)
2643 {
2644 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2645 "attribute at %C in a TYPE definition")
2646 == FAILURE)
2647 {
2648 m = MATCH_ERROR;
2649 goto cleanup;
2650 }
2651 }
2652 else
2653 {
2654 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2655 &seen_at[d]);
2656 m = MATCH_ERROR;
2657 goto cleanup;
2658 }
2659 }
2660
2661 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2662 && gfc_current_state () != COMP_MODULE)
2663 {
2664 if (d == DECL_PRIVATE)
2665 attr = "PRIVATE";
2666 else
2667 attr = "PUBLIC";
2668 if (gfc_current_state () == COMP_DERIVED
2669 && gfc_state_stack->previous
2670 && gfc_state_stack->previous->state == COMP_MODULE)
2671 {
2672 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2673 "at %L in a TYPE definition", attr,
2674 &seen_at[d])
2675 == FAILURE)
2676 {
2677 m = MATCH_ERROR;
2678 goto cleanup;
2679 }
2680 }
2681 else
2682 {
2683 gfc_error ("%s attribute at %L is not allowed outside of the "
2684 "specification part of a module", attr, &seen_at[d]);
2685 m = MATCH_ERROR;
2686 goto cleanup;
2687 }
2688 }
2689
2690 switch (d)
2691 {
2692 case DECL_ALLOCATABLE:
2693 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2694 break;
2695
2696 case DECL_DIMENSION:
2697 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2698 break;
2699
2700 case DECL_EXTERNAL:
2701 t = gfc_add_external (&current_attr, &seen_at[d]);
2702 break;
2703
2704 case DECL_IN:
2705 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2706 break;
2707
2708 case DECL_OUT:
2709 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2710 break;
2711
2712 case DECL_INOUT:
2713 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2714 break;
2715
2716 case DECL_INTRINSIC:
2717 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2718 break;
2719
2720 case DECL_OPTIONAL:
2721 t = gfc_add_optional (&current_attr, &seen_at[d]);
2722 break;
2723
2724 case DECL_PARAMETER:
2725 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2726 break;
2727
2728 case DECL_POINTER:
2729 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2730 break;
2731
2732 case DECL_PROTECTED:
2733 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2734 {
2735 gfc_error ("PROTECTED at %C only allowed in specification "
2736 "part of a module");
2737 t = FAILURE;
2738 break;
2739 }
2740
2741 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2742 "attribute at %C")
2743 == FAILURE)
2744 t = FAILURE;
2745 else
2746 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2747 break;
2748
2749 case DECL_PRIVATE:
2750 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2751 &seen_at[d]);
2752 break;
2753
2754 case DECL_PUBLIC:
2755 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2756 &seen_at[d]);
2757 break;
2758
2759 case DECL_SAVE:
2760 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2761 break;
2762
2763 case DECL_TARGET:
2764 t = gfc_add_target (&current_attr, &seen_at[d]);
2765 break;
2766
2767 case DECL_IS_BIND_C:
2768 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
2769 break;
2770
2771 case DECL_VALUE:
2772 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2773 "at %C")
2774 == FAILURE)
2775 t = FAILURE;
2776 else
2777 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
2778 break;
2779
2780 case DECL_VOLATILE:
2781 if (gfc_notify_std (GFC_STD_F2003,
2782 "Fortran 2003: VOLATILE attribute at %C")
2783 == FAILURE)
2784 t = FAILURE;
2785 else
2786 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
2787 break;
2788
2789 default:
2790 gfc_internal_error ("match_attr_spec(): Bad attribute");
2791 }
2792
2793 if (t == FAILURE)
2794 {
2795 m = MATCH_ERROR;
2796 goto cleanup;
2797 }
2798 }
2799
2800 colon_seen = 1;
2801 return MATCH_YES;
2802
2803 cleanup:
2804 gfc_current_locus = start;
2805 gfc_free_array_spec (current_as);
2806 current_as = NULL;
2807 return m;
2808 }
2809
2810
2811 /* Set the binding label, dest_label, either with the binding label
2812 stored in the given gfc_typespec, ts, or if none was provided, it
2813 will be the symbol name in all lower case, as required by the draft
2814 (J3/04-007, section 15.4.1). If a binding label was given and
2815 there is more than one argument (num_idents), it is an error. */
2816
2817 try
2818 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2819 {
2820 if (curr_binding_label[0] != '\0')
2821 {
2822 if (num_idents > 1 || num_idents_on_line > 1)
2823 {
2824 gfc_error ("Multiple identifiers provided with "
2825 "single NAME= specifier at %C");
2826 return FAILURE;
2827 }
2828
2829 /* Binding label given; store in temp holder til have sym. */
2830 strncpy (dest_label, curr_binding_label,
2831 strlen (curr_binding_label) + 1);
2832 }
2833 else
2834 {
2835 /* No binding label given, and the NAME= specifier did not exist,
2836 which means there was no NAME="". */
2837 if (sym_name != NULL && has_name_equals == 0)
2838 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2839 }
2840
2841 return SUCCESS;
2842 }
2843
2844
2845 /* Set the status of the given common block as being BIND(C) or not,
2846 depending on the given parameter, is_bind_c. */
2847
2848 void
2849 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2850 {
2851 com_block->is_bind_c = is_bind_c;
2852 return;
2853 }
2854
2855
2856 /* Verify that the given gfc_typespec is for a C interoperable type. */
2857
2858 try
2859 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2860 {
2861 try t;
2862
2863 /* Make sure the kind used is appropriate for the type.
2864 The f90_type is unknown if an integer constant was
2865 used (e.g., real(4), bind(c) :: myFloat). */
2866 if (ts->f90_type != BT_UNKNOWN)
2867 {
2868 t = gfc_validate_c_kind (ts);
2869 if (t != SUCCESS)
2870 {
2871 /* Print an error, but continue parsing line. */
2872 gfc_error_now ("C kind parameter is for type %s but "
2873 "symbol '%s' at %L is of type %s",
2874 gfc_basic_typename (ts->f90_type),
2875 name, where,
2876 gfc_basic_typename (ts->type));
2877 }
2878 }
2879
2880 /* Make sure the kind is C interoperable. This does not care about the
2881 possible error above. */
2882 if (ts->type == BT_DERIVED && ts->derived != NULL)
2883 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2884 else if (ts->is_c_interop != 1)
2885 return FAILURE;
2886
2887 return SUCCESS;
2888 }
2889
2890
2891 /* Verify that the variables of a given common block, which has been
2892 defined with the attribute specifier bind(c), to be of a C
2893 interoperable type. Errors will be reported here, if
2894 encountered. */
2895
2896 try
2897 verify_com_block_vars_c_interop (gfc_common_head *com_block)
2898 {
2899 gfc_symbol *curr_sym = NULL;
2900 try retval = SUCCESS;
2901
2902 curr_sym = com_block->head;
2903
2904 /* Make sure we have at least one symbol. */
2905 if (curr_sym == NULL)
2906 return retval;
2907
2908 /* Here we know we have a symbol, so we'll execute this loop
2909 at least once. */
2910 do
2911 {
2912 /* The second to last param, 1, says this is in a common block. */
2913 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2914 curr_sym = curr_sym->common_next;
2915 } while (curr_sym != NULL);
2916
2917 return retval;
2918 }
2919
2920
2921 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2922 an appropriate error message is reported. */
2923
2924 try
2925 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2926 int is_in_common, gfc_common_head *com_block)
2927 {
2928 try retval = SUCCESS;
2929
2930 if (tmp_sym->attr.function && tmp_sym->result != NULL)
2931 {
2932 tmp_sym = tmp_sym->result;
2933 /* Make sure it wasn't an implicitly typed result. */
2934 if (tmp_sym->attr.implicit_type)
2935 {
2936 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
2937 "%L may not be C interoperable", tmp_sym->name,
2938 &tmp_sym->declared_at);
2939 tmp_sym->ts.f90_type = tmp_sym->ts.type;
2940 /* Mark it as C interoperable to prevent duplicate warnings. */
2941 tmp_sym->ts.is_c_interop = 1;
2942 tmp_sym->attr.is_c_interop = 1;
2943 }
2944 }
2945
2946 /* Here, we know we have the bind(c) attribute, so if we have
2947 enough type info, then verify that it's a C interop kind.
2948 The info could be in the symbol already, or possibly still in
2949 the given ts (current_ts), so look in both. */
2950 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2951 {
2952 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2953 &(tmp_sym->declared_at)) != SUCCESS)
2954 {
2955 /* See if we're dealing with a sym in a common block or not. */
2956 if (is_in_common == 1)
2957 {
2958 gfc_warning ("Variable '%s' in common block '%s' at %L "
2959 "may not be a C interoperable "
2960 "kind though common block '%s' is BIND(C)",
2961 tmp_sym->name, com_block->name,
2962 &(tmp_sym->declared_at), com_block->name);
2963 }
2964 else
2965 {
2966 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2967 gfc_error ("Type declaration '%s' at %L is not C "
2968 "interoperable but it is BIND(C)",
2969 tmp_sym->name, &(tmp_sym->declared_at));
2970 else
2971 gfc_warning ("Variable '%s' at %L "
2972 "may not be a C interoperable "
2973 "kind but it is bind(c)",
2974 tmp_sym->name, &(tmp_sym->declared_at));
2975 }
2976 }
2977
2978 /* Variables declared w/in a common block can't be bind(c)
2979 since there's no way for C to see these variables, so there's
2980 semantically no reason for the attribute. */
2981 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
2982 {
2983 gfc_error ("Variable '%s' in common block '%s' at "
2984 "%L cannot be declared with BIND(C) "
2985 "since it is not a global",
2986 tmp_sym->name, com_block->name,
2987 &(tmp_sym->declared_at));
2988 retval = FAILURE;
2989 }
2990
2991 /* Scalar variables that are bind(c) can not have the pointer
2992 or allocatable attributes. */
2993 if (tmp_sym->attr.is_bind_c == 1)
2994 {
2995 if (tmp_sym->attr.pointer == 1)
2996 {
2997 gfc_error ("Variable '%s' at %L cannot have both the "
2998 "POINTER and BIND(C) attributes",
2999 tmp_sym->name, &(tmp_sym->declared_at));
3000 retval = FAILURE;
3001 }
3002
3003 if (tmp_sym->attr.allocatable == 1)
3004 {
3005 gfc_error ("Variable '%s' at %L cannot have both the "
3006 "ALLOCATABLE and BIND(C) attributes",
3007 tmp_sym->name, &(tmp_sym->declared_at));
3008 retval = FAILURE;
3009 }
3010
3011 /* If it is a BIND(C) function, make sure the return value is a
3012 scalar value. The previous tests in this function made sure
3013 the type is interoperable. */
3014 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3015 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3016 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3017
3018 /* BIND(C) functions can not return a character string. */
3019 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3020 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3021 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3022 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3023 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3024 "be a character string", tmp_sym->name,
3025 &(tmp_sym->declared_at));
3026 }
3027 }
3028
3029 /* See if the symbol has been marked as private. If it has, make sure
3030 there is no binding label and warn the user if there is one. */
3031 if (tmp_sym->attr.access == ACCESS_PRIVATE
3032 && tmp_sym->binding_label[0] != '\0')
3033 /* Use gfc_warning_now because we won't say that the symbol fails
3034 just because of this. */
3035 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3036 "given the binding label '%s'", tmp_sym->name,
3037 &(tmp_sym->declared_at), tmp_sym->binding_label);
3038
3039 return retval;
3040 }
3041
3042
3043 /* Set the appropriate fields for a symbol that's been declared as
3044 BIND(C) (the is_bind_c flag and the binding label), and verify that
3045 the type is C interoperable. Errors are reported by the functions
3046 used to set/test these fields. */
3047
3048 try
3049 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3050 {
3051 try retval = SUCCESS;
3052
3053 /* TODO: Do we need to make sure the vars aren't marked private? */
3054
3055 /* Set the is_bind_c bit in symbol_attribute. */
3056 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3057
3058 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3059 num_idents) != SUCCESS)
3060 return FAILURE;
3061
3062 return retval;
3063 }
3064
3065
3066 /* Set the fields marking the given common block as BIND(C), including
3067 a binding label, and report any errors encountered. */
3068
3069 try
3070 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3071 {
3072 try retval = SUCCESS;
3073
3074 /* destLabel, common name, typespec (which may have binding label). */
3075 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3076 != SUCCESS)
3077 return FAILURE;
3078
3079 /* Set the given common block (com_block) to being bind(c) (1). */
3080 set_com_block_bind_c (com_block, 1);
3081
3082 return retval;
3083 }
3084
3085
3086 /* Retrieve the list of one or more identifiers that the given bind(c)
3087 attribute applies to. */
3088
3089 try
3090 get_bind_c_idents (void)
3091 {
3092 char name[GFC_MAX_SYMBOL_LEN + 1];
3093 int num_idents = 0;
3094 gfc_symbol *tmp_sym = NULL;
3095 match found_id;
3096 gfc_common_head *com_block = NULL;
3097
3098 if (gfc_match_name (name) == MATCH_YES)
3099 {
3100 found_id = MATCH_YES;
3101 gfc_get_ha_symbol (name, &tmp_sym);
3102 }
3103 else if (match_common_name (name) == MATCH_YES)
3104 {
3105 found_id = MATCH_YES;
3106 com_block = gfc_get_common (name, 0);
3107 }
3108 else
3109 {
3110 gfc_error ("Need either entity or common block name for "
3111 "attribute specification statement at %C");
3112 return FAILURE;
3113 }
3114
3115 /* Save the current identifier and look for more. */
3116 do
3117 {
3118 /* Increment the number of identifiers found for this spec stmt. */
3119 num_idents++;
3120
3121 /* Make sure we have a sym or com block, and verify that it can
3122 be bind(c). Set the appropriate field(s) and look for more
3123 identifiers. */
3124 if (tmp_sym != NULL || com_block != NULL)
3125 {
3126 if (tmp_sym != NULL)
3127 {
3128 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3129 != SUCCESS)
3130 return FAILURE;
3131 }
3132 else
3133 {
3134 if (set_verify_bind_c_com_block(com_block, num_idents)
3135 != SUCCESS)
3136 return FAILURE;
3137 }
3138
3139 /* Look to see if we have another identifier. */
3140 tmp_sym = NULL;
3141 if (gfc_match_eos () == MATCH_YES)
3142 found_id = MATCH_NO;
3143 else if (gfc_match_char (',') != MATCH_YES)
3144 found_id = MATCH_NO;
3145 else if (gfc_match_name (name) == MATCH_YES)
3146 {
3147 found_id = MATCH_YES;
3148 gfc_get_ha_symbol (name, &tmp_sym);
3149 }
3150 else if (match_common_name (name) == MATCH_YES)
3151 {
3152 found_id = MATCH_YES;
3153 com_block = gfc_get_common (name, 0);
3154 }
3155 else
3156 {
3157 gfc_error ("Missing entity or common block name for "
3158 "attribute specification statement at %C");
3159 return FAILURE;
3160 }
3161 }
3162 else
3163 {
3164 gfc_internal_error ("Missing symbol");
3165 }
3166 } while (found_id == MATCH_YES);
3167
3168 /* if we get here we were successful */
3169 return SUCCESS;
3170 }
3171
3172
3173 /* Try and match a BIND(C) attribute specification statement. */
3174
3175 match
3176 gfc_match_bind_c_stmt (void)
3177 {
3178 match found_match = MATCH_NO;
3179 gfc_typespec *ts;
3180
3181 ts = &current_ts;
3182
3183 /* This may not be necessary. */
3184 gfc_clear_ts (ts);
3185 /* Clear the temporary binding label holder. */
3186 curr_binding_label[0] = '\0';
3187
3188 /* Look for the bind(c). */
3189 found_match = gfc_match_bind_c (NULL);
3190
3191 if (found_match == MATCH_YES)
3192 {
3193 /* Look for the :: now, but it is not required. */
3194 gfc_match (" :: ");
3195
3196 /* Get the identifier(s) that needs to be updated. This may need to
3197 change to hand the flag(s) for the attr specified so all identifiers
3198 found can have all appropriate parts updated (assuming that the same
3199 spec stmt can have multiple attrs, such as both bind(c) and
3200 allocatable...). */
3201 if (get_bind_c_idents () != SUCCESS)
3202 /* Error message should have printed already. */
3203 return MATCH_ERROR;
3204 }
3205
3206 return found_match;
3207 }
3208
3209
3210 /* Match a data declaration statement. */
3211
3212 match
3213 gfc_match_data_decl (void)
3214 {
3215 gfc_symbol *sym;
3216 match m;
3217 int elem;
3218
3219 num_idents_on_line = 0;
3220
3221 m = match_type_spec (&current_ts, 0);
3222 if (m != MATCH_YES)
3223 return m;
3224
3225 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3226 {
3227 sym = gfc_use_derived (current_ts.derived);
3228
3229 if (sym == NULL)
3230 {
3231 m = MATCH_ERROR;
3232 goto cleanup;
3233 }
3234
3235 current_ts.derived = sym;
3236 }
3237
3238 m = match_attr_spec ();
3239 if (m == MATCH_ERROR)
3240 {
3241 m = MATCH_NO;
3242 goto cleanup;
3243 }
3244
3245 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3246 {
3247
3248 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3249 goto ok;
3250
3251 gfc_find_symbol (current_ts.derived->name,
3252 current_ts.derived->ns->parent, 1, &sym);
3253
3254 /* Any symbol that we find had better be a type definition
3255 which has its components defined. */
3256 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3257 && current_ts.derived->components != NULL)
3258 goto ok;
3259
3260 /* Now we have an error, which we signal, and then fix up
3261 because the knock-on is plain and simple confusing. */
3262 gfc_error_now ("Derived type at %C has not been previously defined "
3263 "and so cannot appear in a derived type definition");
3264 current_attr.pointer = 1;
3265 goto ok;
3266 }
3267
3268 ok:
3269 /* If we have an old-style character declaration, and no new-style
3270 attribute specifications, then there a comma is optional between
3271 the type specification and the variable list. */
3272 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3273 gfc_match_char (',');
3274
3275 /* Give the types/attributes to symbols that follow. Give the element
3276 a number so that repeat character length expressions can be copied. */
3277 elem = 1;
3278 for (;;)
3279 {
3280 num_idents_on_line++;
3281 m = variable_decl (elem++);
3282 if (m == MATCH_ERROR)
3283 goto cleanup;
3284 if (m == MATCH_NO)
3285 break;
3286
3287 if (gfc_match_eos () == MATCH_YES)
3288 goto cleanup;
3289 if (gfc_match_char (',') != MATCH_YES)
3290 break;
3291 }
3292
3293 if (gfc_error_flag_test () == 0)
3294 gfc_error ("Syntax error in data declaration at %C");
3295 m = MATCH_ERROR;
3296
3297 gfc_free_data_all (gfc_current_ns);
3298
3299 cleanup:
3300 gfc_free_array_spec (current_as);
3301 current_as = NULL;
3302 return m;
3303 }
3304
3305
3306 /* Match a prefix associated with a function or subroutine
3307 declaration. If the typespec pointer is nonnull, then a typespec
3308 can be matched. Note that if nothing matches, MATCH_YES is
3309 returned (the null string was matched). */
3310
3311 static match
3312 match_prefix (gfc_typespec *ts)
3313 {
3314 int seen_type;
3315
3316 gfc_clear_attr (&current_attr);
3317 seen_type = 0;
3318
3319 loop:
3320 if (!seen_type && ts != NULL
3321 && match_type_spec (ts, 0) == MATCH_YES
3322 && gfc_match_space () == MATCH_YES)
3323 {
3324
3325 seen_type = 1;
3326 goto loop;
3327 }
3328
3329 if (gfc_match ("elemental% ") == MATCH_YES)
3330 {
3331 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3332 return MATCH_ERROR;
3333
3334 goto loop;
3335 }
3336
3337 if (gfc_match ("pure% ") == MATCH_YES)
3338 {
3339 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3340 return MATCH_ERROR;
3341
3342 goto loop;
3343 }
3344
3345 if (gfc_match ("recursive% ") == MATCH_YES)
3346 {
3347 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3348 return MATCH_ERROR;
3349
3350 goto loop;
3351 }
3352
3353 /* At this point, the next item is not a prefix. */
3354 return MATCH_YES;
3355 }
3356
3357
3358 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3359
3360 static try
3361 copy_prefix (symbol_attribute *dest, locus *where)
3362 {
3363 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3364 return FAILURE;
3365
3366 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3367 return FAILURE;
3368
3369 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3370 return FAILURE;
3371
3372 return SUCCESS;
3373 }
3374
3375
3376 /* Match a formal argument list. */
3377
3378 match
3379 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3380 {
3381 gfc_formal_arglist *head, *tail, *p, *q;
3382 char name[GFC_MAX_SYMBOL_LEN + 1];
3383 gfc_symbol *sym;
3384 match m;
3385
3386 head = tail = NULL;
3387
3388 if (gfc_match_char ('(') != MATCH_YES)
3389 {
3390 if (null_flag)
3391 goto ok;
3392 return MATCH_NO;
3393 }
3394
3395 if (gfc_match_char (')') == MATCH_YES)
3396 goto ok;
3397
3398 for (;;)
3399 {
3400 if (gfc_match_char ('*') == MATCH_YES)
3401 sym = NULL;
3402 else
3403 {
3404 m = gfc_match_name (name);
3405 if (m != MATCH_YES)
3406 goto cleanup;
3407
3408 if (gfc_get_symbol (name, NULL, &sym))
3409 goto cleanup;
3410 }
3411
3412 p = gfc_get_formal_arglist ();
3413
3414 if (head == NULL)
3415 head = tail = p;
3416 else
3417 {
3418 tail->next = p;
3419 tail = p;
3420 }
3421
3422 tail->sym = sym;
3423
3424 /* We don't add the VARIABLE flavor because the name could be a
3425 dummy procedure. We don't apply these attributes to formal
3426 arguments of statement functions. */
3427 if (sym != NULL && !st_flag
3428 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3429 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3430 {
3431 m = MATCH_ERROR;
3432 goto cleanup;
3433 }
3434
3435 /* The name of a program unit can be in a different namespace,
3436 so check for it explicitly. After the statement is accepted,
3437 the name is checked for especially in gfc_get_symbol(). */
3438 if (gfc_new_block != NULL && sym != NULL
3439 && strcmp (sym->name, gfc_new_block->name) == 0)
3440 {
3441 gfc_error ("Name '%s' at %C is the name of the procedure",
3442 sym->name);
3443 m = MATCH_ERROR;
3444 goto cleanup;
3445 }
3446
3447 if (gfc_match_char (')') == MATCH_YES)
3448 goto ok;
3449
3450 m = gfc_match_char (',');
3451 if (m != MATCH_YES)
3452 {
3453 gfc_error ("Unexpected junk in formal argument list at %C");
3454 goto cleanup;
3455 }
3456 }
3457
3458 ok:
3459 /* Check for duplicate symbols in the formal argument list. */
3460 if (head != NULL)
3461 {
3462 for (p = head; p->next; p = p->next)
3463 {
3464 if (p->sym == NULL)
3465 continue;
3466
3467 for (q = p->next; q; q = q->next)
3468 if (p->sym == q->sym)
3469 {
3470 gfc_error ("Duplicate symbol '%s' in formal argument list "
3471 "at %C", p->sym->name);
3472
3473 m = MATCH_ERROR;
3474 goto cleanup;
3475 }
3476 }
3477 }
3478
3479 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3480 == FAILURE)
3481 {
3482 m = MATCH_ERROR;
3483 goto cleanup;
3484 }
3485
3486 return MATCH_YES;
3487
3488 cleanup:
3489 gfc_free_formal_arglist (head);
3490 return m;
3491 }
3492
3493
3494 /* Match a RESULT specification following a function declaration or
3495 ENTRY statement. Also matches the end-of-statement. */
3496
3497 static match
3498 match_result (gfc_symbol *function, gfc_symbol **result)
3499 {
3500 char name[GFC_MAX_SYMBOL_LEN + 1];
3501 gfc_symbol *r;
3502 match m;
3503
3504 if (gfc_match (" result (") != MATCH_YES)
3505 return MATCH_NO;
3506
3507 m = gfc_match_name (name);
3508 if (m != MATCH_YES)
3509 return m;
3510
3511 /* Get the right paren, and that's it because there could be the
3512 bind(c) attribute after the result clause. */
3513 if (gfc_match_char(')') != MATCH_YES)
3514 {
3515 /* TODO: should report the missing right paren here. */
3516 return MATCH_ERROR;
3517 }
3518
3519 if (strcmp (function->name, name) == 0)
3520 {
3521 gfc_error ("RESULT variable at %C must be different than function name");
3522 return MATCH_ERROR;
3523 }
3524
3525 if (gfc_get_symbol (name, NULL, &r))
3526 return MATCH_ERROR;
3527
3528 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3529 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3530 return MATCH_ERROR;
3531
3532 *result = r;
3533
3534 return MATCH_YES;
3535 }
3536
3537
3538 /* Match a function suffix, which could be a combination of a result
3539 clause and BIND(C), either one, or neither. The draft does not
3540 require them to come in a specific order. */
3541
3542 match
3543 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3544 {
3545 match is_bind_c; /* Found bind(c). */
3546 match is_result; /* Found result clause. */
3547 match found_match; /* Status of whether we've found a good match. */
3548 int peek_char; /* Character we're going to peek at. */
3549
3550 /* Initialize to having found nothing. */
3551 found_match = MATCH_NO;
3552 is_bind_c = MATCH_NO;
3553 is_result = MATCH_NO;
3554
3555 /* Get the next char to narrow between result and bind(c). */
3556 gfc_gobble_whitespace ();
3557 peek_char = gfc_peek_char ();
3558
3559 switch (peek_char)
3560 {
3561 case 'r':
3562 /* Look for result clause. */
3563 is_result = match_result (sym, result);
3564 if (is_result == MATCH_YES)
3565 {
3566 /* Now see if there is a bind(c) after it. */
3567 is_bind_c = gfc_match_bind_c (sym);
3568 /* We've found the result clause and possibly bind(c). */
3569 found_match = MATCH_YES;
3570 }
3571 else
3572 /* This should only be MATCH_ERROR. */
3573 found_match = is_result;
3574 break;
3575 case 'b':
3576 /* Look for bind(c) first. */
3577 is_bind_c = gfc_match_bind_c (sym);
3578 if (is_bind_c == MATCH_YES)
3579 {
3580 /* Now see if a result clause followed it. */
3581 is_result = match_result (sym, result);
3582 found_match = MATCH_YES;
3583 }
3584 else
3585 {
3586 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3587 found_match = MATCH_ERROR;
3588 }
3589 break;
3590 default:
3591 gfc_error ("Unexpected junk after function declaration at %C");
3592 found_match = MATCH_ERROR;
3593 break;
3594 }
3595
3596 if (is_bind_c == MATCH_YES)
3597 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3598 == FAILURE)
3599 return MATCH_ERROR;
3600
3601 return found_match;
3602 }
3603
3604
3605 /* Match a function declaration. */
3606
3607 match
3608 gfc_match_function_decl (void)
3609 {
3610 char name[GFC_MAX_SYMBOL_LEN + 1];
3611 gfc_symbol *sym, *result;
3612 locus old_loc;
3613 match m;
3614 match suffix_match;
3615 match found_match; /* Status returned by match func. */
3616
3617 if (gfc_current_state () != COMP_NONE
3618 && gfc_current_state () != COMP_INTERFACE
3619 && gfc_current_state () != COMP_CONTAINS)
3620 return MATCH_NO;
3621
3622 gfc_clear_ts (&current_ts);
3623
3624 old_loc = gfc_current_locus;
3625
3626 m = match_prefix (&current_ts);
3627 if (m != MATCH_YES)
3628 {
3629 gfc_current_locus = old_loc;
3630 return m;
3631 }
3632
3633 if (gfc_match ("function% %n", name) != MATCH_YES)
3634 {
3635 gfc_current_locus = old_loc;
3636 return MATCH_NO;
3637 }
3638 if (get_proc_name (name, &sym, false))
3639 return MATCH_ERROR;
3640 gfc_new_block = sym;
3641
3642 m = gfc_match_formal_arglist (sym, 0, 0);
3643 if (m == MATCH_NO)
3644 {
3645 gfc_error ("Expected formal argument list in function "
3646 "definition at %C");
3647 m = MATCH_ERROR;
3648 goto cleanup;
3649 }
3650 else if (m == MATCH_ERROR)
3651 goto cleanup;
3652
3653 result = NULL;
3654
3655 /* According to the draft, the bind(c) and result clause can
3656 come in either order after the formal_arg_list (i.e., either
3657 can be first, both can exist together or by themselves or neither
3658 one). Therefore, the match_result can't match the end of the
3659 string, and check for the bind(c) or result clause in either order. */
3660 found_match = gfc_match_eos ();
3661
3662 /* Make sure that it isn't already declared as BIND(C). If it is, it
3663 must have been marked BIND(C) with a BIND(C) attribute and that is
3664 not allowed for procedures. */
3665 if (sym->attr.is_bind_c == 1)
3666 {
3667 sym->attr.is_bind_c = 0;
3668 if (sym->old_symbol != NULL)
3669 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3670 "variables or common blocks",
3671 &(sym->old_symbol->declared_at));
3672 else
3673 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3674 "variables or common blocks", &gfc_current_locus);
3675 }
3676
3677 if (found_match != MATCH_YES)
3678 {
3679 /* If we haven't found the end-of-statement, look for a suffix. */
3680 suffix_match = gfc_match_suffix (sym, &result);
3681 if (suffix_match == MATCH_YES)
3682 /* Need to get the eos now. */
3683 found_match = gfc_match_eos ();
3684 else
3685 found_match = suffix_match;
3686 }
3687
3688 if(found_match != MATCH_YES)
3689 m = MATCH_ERROR;
3690 else
3691 {
3692 /* Make changes to the symbol. */
3693 m = MATCH_ERROR;
3694
3695 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3696 goto cleanup;
3697
3698 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3699 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3700 goto cleanup;
3701
3702 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3703 && !sym->attr.implicit_type)
3704 {
3705 gfc_error ("Function '%s' at %C already has a type of %s", name,
3706 gfc_basic_typename (sym->ts.type));
3707 goto cleanup;
3708 }
3709
3710 if (result == NULL)
3711 {
3712 sym->ts = current_ts;
3713 sym->result = sym;
3714 }
3715 else
3716 {
3717 result->ts = current_ts;
3718 sym->result = result;
3719 }
3720
3721 return MATCH_YES;
3722 }
3723
3724 cleanup:
3725 gfc_current_locus = old_loc;
3726 return m;
3727 }
3728
3729
3730 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3731 pass the name of the entry, rather than the gfc_current_block name, and
3732 to return false upon finding an existing global entry. */
3733
3734 static bool
3735 add_global_entry (const char *name, int sub)
3736 {
3737 gfc_gsymbol *s;
3738
3739 s = gfc_get_gsymbol(name);
3740
3741 if (s->defined
3742 || (s->type != GSYM_UNKNOWN
3743 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3744 global_used(s, NULL);
3745 else
3746 {
3747 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3748 s->where = gfc_current_locus;
3749 s->defined = 1;
3750 return true;
3751 }
3752 return false;
3753 }
3754
3755
3756 /* Match an ENTRY statement. */
3757
3758 match
3759 gfc_match_entry (void)
3760 {
3761 gfc_symbol *proc;
3762 gfc_symbol *result;
3763 gfc_symbol *entry;
3764 char name[GFC_MAX_SYMBOL_LEN + 1];
3765 gfc_compile_state state;
3766 match m;
3767 gfc_entry_list *el;
3768 locus old_loc;
3769 bool module_procedure;
3770
3771 m = gfc_match_name (name);
3772 if (m != MATCH_YES)
3773 return m;
3774
3775 state = gfc_current_state ();
3776 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3777 {
3778 switch (state)
3779 {
3780 case COMP_PROGRAM:
3781 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3782 break;
3783 case COMP_MODULE:
3784 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3785 break;
3786 case COMP_BLOCK_DATA:
3787 gfc_error ("ENTRY statement at %C cannot appear within "
3788 "a BLOCK DATA");
3789 break;
3790 case COMP_INTERFACE:
3791 gfc_error ("ENTRY statement at %C cannot appear within "
3792 "an INTERFACE");
3793 break;
3794 case COMP_DERIVED:
3795 gfc_error ("ENTRY statement at %C cannot appear within "
3796 "a DERIVED TYPE block");
3797 break;
3798 case COMP_IF:
3799 gfc_error ("ENTRY statement at %C cannot appear within "
3800 "an IF-THEN block");
3801 break;
3802 case COMP_DO:
3803 gfc_error ("ENTRY statement at %C cannot appear within "
3804 "a DO block");
3805 break;
3806 case COMP_SELECT:
3807 gfc_error ("ENTRY statement at %C cannot appear within "
3808 "a SELECT block");
3809 break;
3810 case COMP_FORALL:
3811 gfc_error ("ENTRY statement at %C cannot appear within "
3812 "a FORALL block");
3813 break;
3814 case COMP_WHERE:
3815 gfc_error ("ENTRY statement at %C cannot appear within "
3816 "a WHERE block");
3817 break;
3818 case COMP_CONTAINS:
3819 gfc_error ("ENTRY statement at %C cannot appear within "
3820 "a contained subprogram");
3821 break;
3822 default:
3823 gfc_internal_error ("gfc_match_entry(): Bad state");
3824 }
3825 return MATCH_ERROR;
3826 }
3827
3828 module_procedure = gfc_current_ns->parent != NULL
3829 && gfc_current_ns->parent->proc_name
3830 && gfc_current_ns->parent->proc_name->attr.flavor
3831 == FL_MODULE;
3832
3833 if (gfc_current_ns->parent != NULL
3834 && gfc_current_ns->parent->proc_name
3835 && !module_procedure)
3836 {
3837 gfc_error("ENTRY statement at %C cannot appear in a "
3838 "contained procedure");
3839 return MATCH_ERROR;
3840 }
3841
3842 /* Module function entries need special care in get_proc_name
3843 because previous references within the function will have
3844 created symbols attached to the current namespace. */
3845 if (get_proc_name (name, &entry,
3846 gfc_current_ns->parent != NULL
3847 && module_procedure
3848 && gfc_current_ns->proc_name->attr.function))
3849 return MATCH_ERROR;
3850
3851 proc = gfc_current_block ();
3852
3853 if (state == COMP_SUBROUTINE)
3854 {
3855 /* An entry in a subroutine. */
3856 if (!add_global_entry (name, 1))
3857 return MATCH_ERROR;
3858
3859 m = gfc_match_formal_arglist (entry, 0, 1);
3860 if (m != MATCH_YES)
3861 return MATCH_ERROR;
3862
3863 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3864 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3865 return MATCH_ERROR;
3866 }
3867 else
3868 {
3869 /* An entry in a function.
3870 We need to take special care because writing
3871 ENTRY f()
3872 as
3873 ENTRY f
3874 is allowed, whereas
3875 ENTRY f() RESULT (r)
3876 can't be written as
3877 ENTRY f RESULT (r). */
3878 if (!add_global_entry (name, 0))
3879 return MATCH_ERROR;
3880
3881 old_loc = gfc_current_locus;
3882 if (gfc_match_eos () == MATCH_YES)
3883 {
3884 gfc_current_locus = old_loc;
3885 /* Match the empty argument list, and add the interface to
3886 the symbol. */
3887 m = gfc_match_formal_arglist (entry, 0, 1);
3888 }
3889 else
3890 m = gfc_match_formal_arglist (entry, 0, 0);
3891
3892 if (m != MATCH_YES)
3893 return MATCH_ERROR;
3894
3895 result = NULL;
3896
3897 if (gfc_match_eos () == MATCH_YES)
3898 {
3899 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3900 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3901 return MATCH_ERROR;
3902
3903 entry->result = entry;
3904 }
3905 else
3906 {
3907 m = match_result (proc, &result);
3908 if (m == MATCH_NO)
3909 gfc_syntax_error (ST_ENTRY);
3910 if (m != MATCH_YES)
3911 return MATCH_ERROR;
3912
3913 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3914 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3915 || gfc_add_function (&entry->attr, result->name, NULL)
3916 == FAILURE)
3917 return MATCH_ERROR;
3918
3919 entry->result = result;
3920 }
3921 }
3922
3923 if (gfc_match_eos () != MATCH_YES)
3924 {
3925 gfc_syntax_error (ST_ENTRY);
3926 return MATCH_ERROR;
3927 }
3928
3929 entry->attr.recursive = proc->attr.recursive;
3930 entry->attr.elemental = proc->attr.elemental;
3931 entry->attr.pure = proc->attr.pure;
3932
3933 el = gfc_get_entry_list ();
3934 el->sym = entry;
3935 el->next = gfc_current_ns->entries;
3936 gfc_current_ns->entries = el;
3937 if (el->next)
3938 el->id = el->next->id + 1;
3939 else
3940 el->id = 1;
3941
3942 new_st.op = EXEC_ENTRY;
3943 new_st.ext.entry = el;
3944
3945 return MATCH_YES;
3946 }
3947
3948
3949 /* Match a subroutine statement, including optional prefixes. */
3950
3951 match
3952 gfc_match_subroutine (void)
3953 {
3954 char name[GFC_MAX_SYMBOL_LEN + 1];
3955 gfc_symbol *sym;
3956 match m;
3957 match is_bind_c;
3958 char peek_char;
3959
3960 if (gfc_current_state () != COMP_NONE
3961 && gfc_current_state () != COMP_INTERFACE
3962 && gfc_current_state () != COMP_CONTAINS)
3963 return MATCH_NO;
3964
3965 m = match_prefix (NULL);
3966 if (m != MATCH_YES)
3967 return m;
3968
3969 m = gfc_match ("subroutine% %n", name);
3970 if (m != MATCH_YES)
3971 return m;
3972
3973 if (get_proc_name (name, &sym, false))
3974 return MATCH_ERROR;
3975 gfc_new_block = sym;
3976
3977 /* Check what next non-whitespace character is so we can tell if there
3978 where the required parens if we have a BIND(C). */
3979 gfc_gobble_whitespace ();
3980 peek_char = gfc_peek_char ();
3981
3982 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3983 return MATCH_ERROR;
3984
3985 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3986 return MATCH_ERROR;
3987
3988 /* Make sure that it isn't already declared as BIND(C). If it is, it
3989 must have been marked BIND(C) with a BIND(C) attribute and that is
3990 not allowed for procedures. */
3991 if (sym->attr.is_bind_c == 1)
3992 {
3993 sym->attr.is_bind_c = 0;
3994 if (sym->old_symbol != NULL)
3995 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3996 "variables or common blocks",
3997 &(sym->old_symbol->declared_at));
3998 else
3999 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4000 "variables or common blocks", &gfc_current_locus);
4001 }
4002
4003 /* Here, we are just checking if it has the bind(c) attribute, and if
4004 so, then we need to make sure it's all correct. If it doesn't,
4005 we still need to continue matching the rest of the subroutine line. */
4006 is_bind_c = gfc_match_bind_c (sym);
4007 if (is_bind_c == MATCH_ERROR)
4008 {
4009 /* There was an attempt at the bind(c), but it was wrong. An
4010 error message should have been printed w/in the gfc_match_bind_c
4011 so here we'll just return the MATCH_ERROR. */
4012 return MATCH_ERROR;
4013 }
4014
4015 if (is_bind_c == MATCH_YES)
4016 {
4017 if (peek_char != '(')
4018 {
4019 gfc_error ("Missing required parentheses before BIND(C) at %C");
4020 return MATCH_ERROR;
4021 }
4022 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4023 == FAILURE)
4024 return MATCH_ERROR;
4025 }
4026
4027 if (gfc_match_eos () != MATCH_YES)
4028 {
4029 gfc_syntax_error (ST_SUBROUTINE);
4030 return MATCH_ERROR;
4031 }
4032
4033 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4034 return MATCH_ERROR;
4035
4036 return MATCH_YES;
4037 }
4038
4039
4040 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4041 given, and set the binding label in either the given symbol (if not
4042 NULL), or in the current_ts. The symbol may be NULL because we may
4043 encounter the BIND(C) before the declaration itself. Return
4044 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4045 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4046 or MATCH_YES if the specifier was correct and the binding label and
4047 bind(c) fields were set correctly for the given symbol or the
4048 current_ts. */
4049
4050 match
4051 gfc_match_bind_c (gfc_symbol *sym)
4052 {
4053 /* binding label, if exists */
4054 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4055 match double_quote;
4056 match single_quote;
4057 int has_name_equals = 0;
4058
4059 /* Initialize the flag that specifies whether we encountered a NAME=
4060 specifier or not. */
4061 has_name_equals = 0;
4062
4063 /* Init the first char to nil so we can catch if we don't have
4064 the label (name attr) or the symbol name yet. */
4065 binding_label[0] = '\0';
4066
4067 /* This much we have to be able to match, in this order, if
4068 there is a bind(c) label. */
4069 if (gfc_match (" bind ( c ") != MATCH_YES)
4070 return MATCH_NO;
4071
4072 /* Now see if there is a binding label, or if we've reached the
4073 end of the bind(c) attribute without one. */
4074 if (gfc_match_char (',') == MATCH_YES)
4075 {
4076 if (gfc_match (" name = ") != MATCH_YES)
4077 {
4078 gfc_error ("Syntax error in NAME= specifier for binding label "
4079 "at %C");
4080 /* should give an error message here */
4081 return MATCH_ERROR;
4082 }
4083
4084 has_name_equals = 1;
4085
4086 /* Get the opening quote. */
4087 double_quote = MATCH_YES;
4088 single_quote = MATCH_YES;
4089 double_quote = gfc_match_char ('"');
4090 if (double_quote != MATCH_YES)
4091 single_quote = gfc_match_char ('\'');
4092 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4093 {
4094 gfc_error ("Syntax error in NAME= specifier for binding label "
4095 "at %C");
4096 return MATCH_ERROR;
4097 }
4098
4099 /* Grab the binding label, using functions that will not lower
4100 case the names automatically. */
4101 if (gfc_match_name_C (binding_label) != MATCH_YES)
4102 return MATCH_ERROR;
4103
4104 /* Get the closing quotation. */
4105 if (double_quote == MATCH_YES)
4106 {
4107 if (gfc_match_char ('"') != MATCH_YES)
4108 {
4109 gfc_error ("Missing closing quote '\"' for binding label at %C");
4110 /* User started string with '"' so looked to match it. */
4111 return MATCH_ERROR;
4112 }
4113 }
4114 else
4115 {
4116 if (gfc_match_char ('\'') != MATCH_YES)
4117 {
4118 gfc_error ("Missing closing quote '\'' for binding label at %C");
4119 /* User started string with "'" char. */
4120 return MATCH_ERROR;
4121 }
4122 }
4123 }
4124
4125 /* Get the required right paren. */
4126 if (gfc_match_char (')') != MATCH_YES)
4127 {
4128 gfc_error ("Missing closing paren for binding label at %C");
4129 return MATCH_ERROR;
4130 }
4131
4132 /* Save the binding label to the symbol. If sym is null, we're
4133 probably matching the typespec attributes of a declaration and
4134 haven't gotten the name yet, and therefore, no symbol yet. */
4135 if (binding_label[0] != '\0')
4136 {
4137 if (sym != NULL)
4138 {
4139 strncpy (sym->binding_label, binding_label,
4140 strlen (binding_label)+1);
4141 }
4142 else
4143 strncpy (curr_binding_label, binding_label,
4144 strlen (binding_label) + 1);
4145 }
4146 else
4147 {
4148 /* No binding label, but if symbol isn't null, we
4149 can set the label for it here. */
4150 /* TODO: If the name= was given and no binding label (name=""), we simply
4151 will let fortran mangle the symbol name as it usually would.
4152 However, this could still let C call it if the user looked up the
4153 symbol in the object file. Should the name set during mangling in
4154 trans-decl.c be marked with characters that are invalid for C to
4155 prevent this? */
4156 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4157 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4158 }
4159
4160 return MATCH_YES;
4161 }
4162
4163
4164 /* Return nonzero if we're currently compiling a contained procedure. */
4165
4166 static int
4167 contained_procedure (void)
4168 {
4169 gfc_state_data *s;
4170
4171 for (s=gfc_state_stack; s; s=s->previous)
4172 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4173 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4174 return 1;
4175
4176 return 0;
4177 }
4178
4179 /* Set the kind of each enumerator. The kind is selected such that it is
4180 interoperable with the corresponding C enumeration type, making
4181 sure that -fshort-enums is honored. */
4182
4183 static void
4184 set_enum_kind(void)
4185 {
4186 enumerator_history *current_history = NULL;
4187 int kind;
4188 int i;
4189
4190 if (max_enum == NULL || enum_history == NULL)
4191 return;
4192
4193 if (!gfc_option.fshort_enums)
4194 return;
4195
4196 i = 0;
4197 do
4198 {
4199 kind = gfc_integer_kinds[i++].kind;
4200 }
4201 while (kind < gfc_c_int_kind
4202 && gfc_check_integer_range (max_enum->initializer->value.integer,
4203 kind) != ARITH_OK);
4204
4205 current_history = enum_history;
4206 while (current_history != NULL)
4207 {
4208 current_history->sym->ts.kind = kind;
4209 current_history = current_history->next;
4210 }
4211 }
4212
4213
4214 /* Match any of the various end-block statements. Returns the type of
4215 END to the caller. The END INTERFACE, END IF, END DO and END
4216 SELECT statements cannot be replaced by a single END statement. */
4217
4218 match
4219 gfc_match_end (gfc_statement *st)
4220 {
4221 char name[GFC_MAX_SYMBOL_LEN + 1];
4222 gfc_compile_state state;
4223 locus old_loc;
4224 const char *block_name;
4225 const char *target;
4226 int eos_ok;
4227 match m;
4228
4229 old_loc = gfc_current_locus;
4230 if (gfc_match ("end") != MATCH_YES)
4231 return MATCH_NO;
4232
4233 state = gfc_current_state ();
4234 block_name = gfc_current_block () == NULL
4235 ? NULL : gfc_current_block ()->name;
4236
4237 if (state == COMP_CONTAINS)
4238 {
4239 state = gfc_state_stack->previous->state;
4240 block_name = gfc_state_stack->previous->sym == NULL
4241 ? NULL : gfc_state_stack->previous->sym->name;
4242 }
4243
4244 switch (state)
4245 {
4246 case COMP_NONE:
4247 case COMP_PROGRAM:
4248 *st = ST_END_PROGRAM;
4249 target = " program";
4250 eos_ok = 1;
4251 break;
4252
4253 case COMP_SUBROUTINE:
4254 *st = ST_END_SUBROUTINE;
4255 target = " subroutine";
4256 eos_ok = !contained_procedure ();
4257 break;
4258
4259 case COMP_FUNCTION:
4260 *st = ST_END_FUNCTION;
4261 target = " function";
4262 eos_ok = !contained_procedure ();
4263 break;
4264
4265 case COMP_BLOCK_DATA:
4266 *st = ST_END_BLOCK_DATA;
4267 target = " block data";
4268 eos_ok = 1;
4269 break;
4270
4271 case COMP_MODULE:
4272 *st = ST_END_MODULE;
4273 target = " module";
4274 eos_ok = 1;
4275 break;
4276
4277 case COMP_INTERFACE:
4278 *st = ST_END_INTERFACE;
4279 target = " interface";
4280 eos_ok = 0;
4281 break;
4282
4283 case COMP_DERIVED:
4284 *st = ST_END_TYPE;
4285 target = " type";
4286 eos_ok = 0;
4287 break;
4288
4289 case COMP_IF:
4290 *st = ST_ENDIF;
4291 target = " if";
4292 eos_ok = 0;
4293 break;
4294
4295 case COMP_DO:
4296 *st = ST_ENDDO;
4297 target = " do";
4298 eos_ok = 0;
4299 break;
4300
4301 case COMP_SELECT:
4302 *st = ST_END_SELECT;
4303 target = " select";
4304 eos_ok = 0;
4305 break;
4306
4307 case COMP_FORALL:
4308 *st = ST_END_FORALL;
4309 target = " forall";
4310 eos_ok = 0;
4311 break;
4312
4313 case COMP_WHERE:
4314 *st = ST_END_WHERE;
4315 target = " where";
4316 eos_ok = 0;
4317 break;
4318
4319 case COMP_ENUM:
4320 *st = ST_END_ENUM;
4321 target = " enum";
4322 eos_ok = 0;
4323 last_initializer = NULL;
4324 set_enum_kind ();
4325 gfc_free_enum_history ();
4326 break;
4327
4328 default:
4329 gfc_error ("Unexpected END statement at %C");
4330 goto cleanup;
4331 }
4332
4333 if (gfc_match_eos () == MATCH_YES)
4334 {
4335 if (!eos_ok)
4336 {
4337 /* We would have required END [something]. */
4338 gfc_error ("%s statement expected at %L",
4339 gfc_ascii_statement (*st), &old_loc);
4340 goto cleanup;
4341 }
4342
4343 return MATCH_YES;
4344 }
4345
4346 /* Verify that we've got the sort of end-block that we're expecting. */
4347 if (gfc_match (target) != MATCH_YES)
4348 {
4349 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4350 goto cleanup;
4351 }
4352
4353 /* If we're at the end, make sure a block name wasn't required. */
4354 if (gfc_match_eos () == MATCH_YES)
4355 {
4356
4357 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4358 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4359 return MATCH_YES;
4360
4361 if (gfc_current_block () == NULL)
4362 return MATCH_YES;
4363
4364 gfc_error ("Expected block name of '%s' in %s statement at %C",
4365 block_name, gfc_ascii_statement (*st));
4366
4367 return MATCH_ERROR;
4368 }
4369
4370 /* END INTERFACE has a special handler for its several possible endings. */
4371 if (*st == ST_END_INTERFACE)
4372 return gfc_match_end_interface ();
4373
4374 /* We haven't hit the end of statement, so what is left must be an
4375 end-name. */
4376 m = gfc_match_space ();
4377 if (m == MATCH_YES)
4378 m = gfc_match_name (name);
4379
4380 if (m == MATCH_NO)
4381 gfc_error ("Expected terminating name at %C");
4382 if (m != MATCH_YES)
4383 goto cleanup;
4384
4385 if (block_name == NULL)
4386 goto syntax;
4387
4388 if (strcmp (name, block_name) != 0)
4389 {
4390 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4391 gfc_ascii_statement (*st));
4392 goto cleanup;
4393 }
4394
4395 if (gfc_match_eos () == MATCH_YES)
4396 return MATCH_YES;
4397
4398 syntax:
4399 gfc_syntax_error (*st);
4400
4401 cleanup:
4402 gfc_current_locus = old_loc;
4403 return MATCH_ERROR;
4404 }
4405
4406
4407
4408 /***************** Attribute declaration statements ****************/
4409
4410 /* Set the attribute of a single variable. */
4411
4412 static match
4413 attr_decl1 (void)
4414 {
4415 char name[GFC_MAX_SYMBOL_LEN + 1];
4416 gfc_array_spec *as;
4417 gfc_symbol *sym;
4418 locus var_locus;
4419 match m;
4420
4421 as = NULL;
4422
4423 m = gfc_match_name (name);
4424 if (m != MATCH_YES)
4425 goto cleanup;
4426
4427 if (find_special (name, &sym))
4428 return MATCH_ERROR;
4429
4430 var_locus = gfc_current_locus;
4431
4432 /* Deal with possible array specification for certain attributes. */
4433 if (current_attr.dimension
4434 || current_attr.allocatable
4435 || current_attr.pointer
4436 || current_attr.target)
4437 {
4438 m = gfc_match_array_spec (&as);
4439 if (m == MATCH_ERROR)
4440 goto cleanup;
4441
4442 if (current_attr.dimension && m == MATCH_NO)
4443 {
4444 gfc_error ("Missing array specification at %L in DIMENSION "
4445 "statement", &var_locus);
4446 m = MATCH_ERROR;
4447 goto cleanup;
4448 }
4449
4450 if ((current_attr.allocatable || current_attr.pointer)
4451 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4452 {
4453 gfc_error ("Array specification must be deferred at %L", &var_locus);
4454 m = MATCH_ERROR;
4455 goto cleanup;
4456 }
4457 }
4458
4459 /* Update symbol table. DIMENSION attribute is set
4460 in gfc_set_array_spec(). */
4461 if (current_attr.dimension == 0
4462 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4463 {
4464 m = MATCH_ERROR;
4465 goto cleanup;
4466 }
4467
4468 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4469 {
4470 m = MATCH_ERROR;
4471 goto cleanup;
4472 }
4473
4474 if (sym->attr.cray_pointee && sym->as != NULL)
4475 {
4476 /* Fix the array spec. */
4477 m = gfc_mod_pointee_as (sym->as);
4478 if (m == MATCH_ERROR)
4479 goto cleanup;
4480 }
4481
4482 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
4483 {
4484 m = MATCH_ERROR;
4485 goto cleanup;
4486 }
4487
4488 if ((current_attr.external || current_attr.intrinsic)
4489 && sym->attr.flavor != FL_PROCEDURE
4490 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4491 {
4492 m = MATCH_ERROR;
4493 goto cleanup;
4494 }
4495
4496 return MATCH_YES;
4497
4498 cleanup:
4499 gfc_free_array_spec (as);
4500 return m;
4501 }
4502
4503
4504 /* Generic attribute declaration subroutine. Used for attributes that
4505 just have a list of names. */
4506
4507 static match
4508 attr_decl (void)
4509 {
4510 match m;
4511
4512 /* Gobble the optional double colon, by simply ignoring the result
4513 of gfc_match(). */
4514 gfc_match (" ::");
4515
4516 for (;;)
4517 {
4518 m = attr_decl1 ();
4519 if (m != MATCH_YES)
4520 break;
4521
4522 if (gfc_match_eos () == MATCH_YES)
4523 {
4524 m = MATCH_YES;
4525 break;
4526 }
4527
4528 if (gfc_match_char (',') != MATCH_YES)
4529 {
4530 gfc_error ("Unexpected character in variable list at %C");
4531 m = MATCH_ERROR;
4532 break;
4533 }
4534 }
4535
4536 return m;
4537 }
4538
4539
4540 /* This routine matches Cray Pointer declarations of the form:
4541 pointer ( <pointer>, <pointee> )
4542 or
4543 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4544 The pointer, if already declared, should be an integer. Otherwise, we
4545 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4546 be either a scalar, or an array declaration. No space is allocated for
4547 the pointee. For the statement
4548 pointer (ipt, ar(10))
4549 any subsequent uses of ar will be translated (in C-notation) as
4550 ar(i) => ((<type> *) ipt)(i)
4551 After gimplification, pointee variable will disappear in the code. */
4552
4553 static match
4554 cray_pointer_decl (void)
4555 {
4556 match m;
4557 gfc_array_spec *as;
4558 gfc_symbol *cptr; /* Pointer symbol. */
4559 gfc_symbol *cpte; /* Pointee symbol. */
4560 locus var_locus;
4561 bool done = false;
4562
4563 while (!done)
4564 {
4565 if (gfc_match_char ('(') != MATCH_YES)
4566 {
4567 gfc_error ("Expected '(' at %C");
4568 return MATCH_ERROR;
4569 }
4570
4571 /* Match pointer. */
4572 var_locus = gfc_current_locus;
4573 gfc_clear_attr (&current_attr);
4574 gfc_add_cray_pointer (&current_attr, &var_locus);
4575 current_ts.type = BT_INTEGER;
4576 current_ts.kind = gfc_index_integer_kind;
4577
4578 m = gfc_match_symbol (&cptr, 0);
4579 if (m != MATCH_YES)
4580 {
4581 gfc_error ("Expected variable name at %C");
4582 return m;
4583 }
4584
4585 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
4586 return MATCH_ERROR;
4587
4588 gfc_set_sym_referenced (cptr);
4589
4590 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
4591 {
4592 cptr->ts.type = BT_INTEGER;
4593 cptr->ts.kind = gfc_index_integer_kind;
4594 }
4595 else if (cptr->ts.type != BT_INTEGER)
4596 {
4597 gfc_error ("Cray pointer at %C must be an integer");
4598 return MATCH_ERROR;
4599 }
4600 else if (cptr->ts.kind < gfc_index_integer_kind)
4601 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
4602 " memory addresses require %d bytes",
4603 cptr->ts.kind, gfc_index_integer_kind);
4604
4605 if (gfc_match_char (',') != MATCH_YES)
4606 {
4607 gfc_error ("Expected \",\" at %C");
4608 return MATCH_ERROR;
4609 }
4610
4611 /* Match Pointee. */
4612 var_locus = gfc_current_locus;
4613 gfc_clear_attr (&current_attr);
4614 gfc_add_cray_pointee (&current_attr, &var_locus);
4615 current_ts.type = BT_UNKNOWN;
4616 current_ts.kind = 0;
4617
4618 m = gfc_match_symbol (&cpte, 0);
4619 if (m != MATCH_YES)
4620 {
4621 gfc_error ("Expected variable name at %C");
4622 return m;
4623 }
4624
4625 /* Check for an optional array spec. */
4626 m = gfc_match_array_spec (&as);
4627 if (m == MATCH_ERROR)
4628 {
4629 gfc_free_array_spec (as);
4630 return m;
4631 }
4632 else if (m == MATCH_NO)
4633 {
4634 gfc_free_array_spec (as);
4635 as = NULL;
4636 }
4637
4638 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
4639 return MATCH_ERROR;
4640
4641 gfc_set_sym_referenced (cpte);
4642
4643 if (cpte->as == NULL)
4644 {
4645 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
4646 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4647 }
4648 else if (as != NULL)
4649 {
4650 gfc_error ("Duplicate array spec for Cray pointee at %C");
4651 gfc_free_array_spec (as);
4652 return MATCH_ERROR;
4653 }
4654
4655 as = NULL;
4656
4657 if (cpte->as != NULL)
4658 {
4659 /* Fix array spec. */
4660 m = gfc_mod_pointee_as (cpte->as);
4661 if (m == MATCH_ERROR)
4662 return m;
4663 }
4664
4665 /* Point the Pointee at the Pointer. */
4666 cpte->cp_pointer = cptr;
4667
4668 if (gfc_match_char (')') != MATCH_YES)
4669 {
4670 gfc_error ("Expected \")\" at %C");
4671 return MATCH_ERROR;
4672 }
4673 m = gfc_match_char (',');
4674 if (m != MATCH_YES)
4675 done = true; /* Stop searching for more declarations. */
4676
4677 }
4678
4679 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
4680 || gfc_match_eos () != MATCH_YES)
4681 {
4682 gfc_error ("Expected \",\" or end of statement at %C");
4683 return MATCH_ERROR;
4684 }
4685 return MATCH_YES;
4686 }
4687
4688
4689 match
4690 gfc_match_external (void)
4691 {
4692
4693 gfc_clear_attr (&current_attr);
4694 current_attr.external = 1;
4695
4696 return attr_decl ();
4697 }
4698
4699
4700 match
4701 gfc_match_intent (void)
4702 {
4703 sym_intent intent;
4704
4705 intent = match_intent_spec ();
4706 if (intent == INTENT_UNKNOWN)
4707 return MATCH_ERROR;
4708
4709 gfc_clear_attr (&current_attr);
4710 current_attr.intent = intent;
4711
4712 return attr_decl ();
4713 }
4714
4715
4716 match
4717 gfc_match_intrinsic (void)
4718 {
4719
4720 gfc_clear_attr (&current_attr);
4721 current_attr.intrinsic = 1;
4722
4723 return attr_decl ();
4724 }
4725
4726
4727 match
4728 gfc_match_optional (void)
4729 {
4730
4731 gfc_clear_attr (&current_attr);
4732 current_attr.optional = 1;
4733
4734 return attr_decl ();
4735 }
4736
4737
4738 match
4739 gfc_match_pointer (void)
4740 {
4741 gfc_gobble_whitespace ();
4742 if (gfc_peek_char () == '(')
4743 {
4744 if (!gfc_option.flag_cray_pointer)
4745 {
4746 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4747 "flag");
4748 return MATCH_ERROR;
4749 }
4750 return cray_pointer_decl ();
4751 }
4752 else
4753 {
4754 gfc_clear_attr (&current_attr);
4755 current_attr.pointer = 1;
4756
4757 return attr_decl ();
4758 }
4759 }
4760
4761
4762 match
4763 gfc_match_allocatable (void)
4764 {
4765 gfc_clear_attr (&current_attr);
4766 current_attr.allocatable = 1;
4767
4768 return attr_decl ();
4769 }
4770
4771
4772 match
4773 gfc_match_dimension (void)
4774 {
4775 gfc_clear_attr (&current_attr);
4776 current_attr.dimension = 1;
4777
4778 return attr_decl ();
4779 }
4780
4781
4782 match
4783 gfc_match_target (void)
4784 {
4785 gfc_clear_attr (&current_attr);
4786 current_attr.target = 1;
4787
4788 return attr_decl ();
4789 }
4790
4791
4792 /* Match the list of entities being specified in a PUBLIC or PRIVATE
4793 statement. */
4794
4795 static match
4796 access_attr_decl (gfc_statement st)
4797 {
4798 char name[GFC_MAX_SYMBOL_LEN + 1];
4799 interface_type type;
4800 gfc_user_op *uop;
4801 gfc_symbol *sym;
4802 gfc_intrinsic_op operator;
4803 match m;
4804
4805 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4806 goto done;
4807
4808 for (;;)
4809 {
4810 m = gfc_match_generic_spec (&type, name, &operator);
4811 if (m == MATCH_NO)
4812 goto syntax;
4813 if (m == MATCH_ERROR)
4814 return MATCH_ERROR;
4815
4816 switch (type)
4817 {
4818 case INTERFACE_NAMELESS:
4819 goto syntax;
4820
4821 case INTERFACE_GENERIC:
4822 if (gfc_get_symbol (name, NULL, &sym))
4823 goto done;
4824
4825 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
4826 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
4827 sym->name, NULL) == FAILURE)
4828 return MATCH_ERROR;
4829
4830 break;
4831
4832 case INTERFACE_INTRINSIC_OP:
4833 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
4834 {
4835 gfc_current_ns->operator_access[operator] =
4836 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4837 }
4838 else
4839 {
4840 gfc_error ("Access specification of the %s operator at %C has "
4841 "already been specified", gfc_op2string (operator));
4842 goto done;
4843 }
4844
4845 break;
4846
4847 case INTERFACE_USER_OP:
4848 uop = gfc_get_uop (name);
4849
4850 if (uop->access == ACCESS_UNKNOWN)
4851 {
4852 uop->access = (st == ST_PUBLIC)
4853 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4854 }
4855 else
4856 {
4857 gfc_error ("Access specification of the .%s. operator at %C "
4858 "has already been specified", sym->name);
4859 goto done;
4860 }
4861
4862 break;
4863 }
4864
4865 if (gfc_match_char (',') == MATCH_NO)
4866 break;
4867 }
4868
4869 if (gfc_match_eos () != MATCH_YES)
4870 goto syntax;
4871 return MATCH_YES;
4872
4873 syntax:
4874 gfc_syntax_error (st);
4875
4876 done:
4877 return MATCH_ERROR;
4878 }
4879
4880
4881 match
4882 gfc_match_protected (void)
4883 {
4884 gfc_symbol *sym;
4885 match m;
4886
4887 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4888 {
4889 gfc_error ("PROTECTED at %C only allowed in specification "
4890 "part of a module");
4891 return MATCH_ERROR;
4892
4893 }
4894
4895 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
4896 == FAILURE)
4897 return MATCH_ERROR;
4898
4899 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4900 {
4901 return MATCH_ERROR;
4902 }
4903
4904 if (gfc_match_eos () == MATCH_YES)
4905 goto syntax;
4906
4907 for(;;)
4908 {
4909 m = gfc_match_symbol (&sym, 0);
4910 switch (m)
4911 {
4912 case MATCH_YES:
4913 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
4914 == FAILURE)
4915 return MATCH_ERROR;
4916 goto next_item;
4917
4918 case MATCH_NO:
4919 break;
4920
4921 case MATCH_ERROR:
4922 return MATCH_ERROR;
4923 }
4924
4925 next_item:
4926 if (gfc_match_eos () == MATCH_YES)
4927 break;
4928 if (gfc_match_char (',') != MATCH_YES)
4929 goto syntax;
4930 }
4931
4932 return MATCH_YES;
4933
4934 syntax:
4935 gfc_error ("Syntax error in PROTECTED statement at %C");
4936 return MATCH_ERROR;
4937 }
4938
4939
4940 /* The PRIVATE statement is a bit weird in that it can be an attribute
4941 declaration, but also works as a standlone statement inside of a
4942 type declaration or a module. */
4943
4944 match
4945 gfc_match_private (gfc_statement *st)
4946 {
4947
4948 if (gfc_match ("private") != MATCH_YES)
4949 return MATCH_NO;
4950
4951 if (gfc_current_state () != COMP_MODULE
4952 && (gfc_current_state () != COMP_DERIVED
4953 || !gfc_state_stack->previous
4954 || gfc_state_stack->previous->state != COMP_MODULE))
4955 {
4956 gfc_error ("PRIVATE statement at %C is only allowed in the "
4957 "specification part of a module");
4958 return MATCH_ERROR;
4959 }
4960
4961 if (gfc_current_state () == COMP_DERIVED)
4962 {
4963 if (gfc_match_eos () == MATCH_YES)
4964 {
4965 *st = ST_PRIVATE;
4966 return MATCH_YES;
4967 }
4968
4969 gfc_syntax_error (ST_PRIVATE);
4970 return MATCH_ERROR;
4971 }
4972
4973 if (gfc_match_eos () == MATCH_YES)
4974 {
4975 *st = ST_PRIVATE;
4976 return MATCH_YES;
4977 }
4978
4979 *st = ST_ATTR_DECL;
4980 return access_attr_decl (ST_PRIVATE);
4981 }
4982
4983
4984 match
4985 gfc_match_public (gfc_statement *st)
4986 {
4987
4988 if (gfc_match ("public") != MATCH_YES)
4989 return MATCH_NO;
4990
4991 if (gfc_current_state () != COMP_MODULE)
4992 {
4993 gfc_error ("PUBLIC statement at %C is only allowed in the "
4994 "specification part of a module");
4995 return MATCH_ERROR;
4996 }
4997
4998 if (gfc_match_eos () == MATCH_YES)
4999 {
5000 *st = ST_PUBLIC;
5001 return MATCH_YES;
5002 }
5003
5004 *st = ST_ATTR_DECL;
5005 return access_attr_decl (ST_PUBLIC);
5006 }
5007
5008
5009 /* Workhorse for gfc_match_parameter. */
5010
5011 static match
5012 do_parm (void)
5013 {
5014 gfc_symbol *sym;
5015 gfc_expr *init;
5016 match m;
5017
5018 m = gfc_match_symbol (&sym, 0);
5019 if (m == MATCH_NO)
5020 gfc_error ("Expected variable name at %C in PARAMETER statement");
5021
5022 if (m != MATCH_YES)
5023 return m;
5024
5025 if (gfc_match_char ('=') == MATCH_NO)
5026 {
5027 gfc_error ("Expected = sign in PARAMETER statement at %C");
5028 return MATCH_ERROR;
5029 }
5030
5031 m = gfc_match_init_expr (&init);
5032 if (m == MATCH_NO)
5033 gfc_error ("Expected expression at %C in PARAMETER statement");
5034 if (m != MATCH_YES)
5035 return m;
5036
5037 if (sym->ts.type == BT_UNKNOWN
5038 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5039 {
5040 m = MATCH_ERROR;
5041 goto cleanup;
5042 }
5043
5044 if (gfc_check_assign_symbol (sym, init) == FAILURE
5045 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5046 {
5047 m = MATCH_ERROR;
5048 goto cleanup;
5049 }
5050
5051 if (sym->ts.type == BT_CHARACTER
5052 && sym->ts.cl != NULL
5053 && sym->ts.cl->length != NULL
5054 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5055 && init->expr_type == EXPR_CONSTANT
5056 && init->ts.type == BT_CHARACTER
5057 && init->ts.kind == 1)
5058 gfc_set_constant_character_len (
5059 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5060
5061 sym->value = init;
5062 return MATCH_YES;
5063
5064 cleanup:
5065 gfc_free_expr (init);
5066 return m;
5067 }
5068
5069
5070 /* Match a parameter statement, with the weird syntax that these have. */
5071
5072 match
5073 gfc_match_parameter (void)
5074 {
5075 match m;
5076
5077 if (gfc_match_char ('(') == MATCH_NO)
5078 return MATCH_NO;
5079
5080 for (;;)
5081 {
5082 m = do_parm ();
5083 if (m != MATCH_YES)
5084 break;
5085
5086 if (gfc_match (" )%t") == MATCH_YES)
5087 break;
5088
5089 if (gfc_match_char (',') != MATCH_YES)
5090 {
5091 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5092 m = MATCH_ERROR;
5093 break;
5094 }
5095 }
5096
5097 return m;
5098 }
5099
5100
5101 /* Save statements have a special syntax. */
5102
5103 match
5104 gfc_match_save (void)
5105 {
5106 char n[GFC_MAX_SYMBOL_LEN+1];
5107 gfc_common_head *c;
5108 gfc_symbol *sym;
5109 match m;
5110
5111 if (gfc_match_eos () == MATCH_YES)
5112 {
5113 if (gfc_current_ns->seen_save)
5114 {
5115 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5116 "follows previous SAVE statement")
5117 == FAILURE)
5118 return MATCH_ERROR;
5119 }
5120
5121 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5122 return MATCH_YES;
5123 }
5124
5125 if (gfc_current_ns->save_all)
5126 {
5127 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5128 "blanket SAVE statement")
5129 == FAILURE)
5130 return MATCH_ERROR;
5131 }
5132
5133 gfc_match (" ::");
5134
5135 for (;;)
5136 {
5137 m = gfc_match_symbol (&sym, 0);
5138 switch (m)
5139 {
5140 case MATCH_YES:
5141 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5142 == FAILURE)
5143 return MATCH_ERROR;
5144 goto next_item;
5145
5146 case MATCH_NO:
5147 break;
5148
5149 case MATCH_ERROR:
5150 return MATCH_ERROR;
5151 }
5152
5153 m = gfc_match (" / %n /", &n);
5154 if (m == MATCH_ERROR)
5155 return MATCH_ERROR;
5156 if (m == MATCH_NO)
5157 goto syntax;
5158
5159 c = gfc_get_common (n, 0);
5160 c->saved = 1;
5161
5162 gfc_current_ns->seen_save = 1;
5163
5164 next_item:
5165 if (gfc_match_eos () == MATCH_YES)
5166 break;
5167 if (gfc_match_char (',') != MATCH_YES)
5168 goto syntax;
5169 }
5170
5171 return MATCH_YES;
5172
5173 syntax:
5174 gfc_error ("Syntax error in SAVE statement at %C");
5175 return MATCH_ERROR;
5176 }
5177
5178
5179 match
5180 gfc_match_value (void)
5181 {
5182 gfc_symbol *sym;
5183 match m;
5184
5185 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5186 == FAILURE)
5187 return MATCH_ERROR;
5188
5189 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5190 {
5191 return MATCH_ERROR;
5192 }
5193
5194 if (gfc_match_eos () == MATCH_YES)
5195 goto syntax;
5196
5197 for(;;)
5198 {
5199 m = gfc_match_symbol (&sym, 0);
5200 switch (m)
5201 {
5202 case MATCH_YES:
5203 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5204 == FAILURE)
5205 return MATCH_ERROR;
5206 goto next_item;
5207
5208 case MATCH_NO:
5209 break;
5210
5211 case MATCH_ERROR:
5212 return MATCH_ERROR;
5213 }
5214
5215 next_item:
5216 if (gfc_match_eos () == MATCH_YES)
5217 break;
5218 if (gfc_match_char (',') != MATCH_YES)
5219 goto syntax;
5220 }
5221
5222 return MATCH_YES;
5223
5224 syntax:
5225 gfc_error ("Syntax error in VALUE statement at %C");
5226 return MATCH_ERROR;
5227 }
5228
5229
5230 match
5231 gfc_match_volatile (void)
5232 {
5233 gfc_symbol *sym;
5234 match m;
5235
5236 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5237 == FAILURE)
5238 return MATCH_ERROR;
5239
5240 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5241 {
5242 return MATCH_ERROR;
5243 }
5244
5245 if (gfc_match_eos () == MATCH_YES)
5246 goto syntax;
5247
5248 for(;;)
5249 {
5250 /* VOLATILE is special because it can be added to host-associated
5251 symbols locally. */
5252 m = gfc_match_symbol (&sym, 1);
5253 switch (m)
5254 {
5255 case MATCH_YES:
5256 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5257 == FAILURE)
5258 return MATCH_ERROR;
5259 goto next_item;
5260
5261 case MATCH_NO:
5262 break;
5263
5264 case MATCH_ERROR:
5265 return MATCH_ERROR;
5266 }
5267
5268 next_item:
5269 if (gfc_match_eos () == MATCH_YES)
5270 break;
5271 if (gfc_match_char (',') != MATCH_YES)
5272 goto syntax;
5273 }
5274
5275 return MATCH_YES;
5276
5277 syntax:
5278 gfc_error ("Syntax error in VOLATILE statement at %C");
5279 return MATCH_ERROR;
5280 }
5281
5282
5283 /* Match a module procedure statement. Note that we have to modify
5284 symbols in the parent's namespace because the current one was there
5285 to receive symbols that are in an interface's formal argument list. */
5286
5287 match
5288 gfc_match_modproc (void)
5289 {
5290 char name[GFC_MAX_SYMBOL_LEN + 1];
5291 gfc_symbol *sym;
5292 match m;
5293 gfc_namespace *module_ns;
5294
5295 if (gfc_state_stack->state != COMP_INTERFACE
5296 || gfc_state_stack->previous == NULL
5297 || current_interface.type == INTERFACE_NAMELESS)
5298 {
5299 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5300 "interface");
5301 return MATCH_ERROR;
5302 }
5303
5304 module_ns = gfc_current_ns->parent;
5305 for (; module_ns; module_ns = module_ns->parent)
5306 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5307 break;
5308
5309 if (module_ns == NULL)
5310 return MATCH_ERROR;
5311
5312 for (;;)
5313 {
5314 m = gfc_match_name (name);
5315 if (m == MATCH_NO)
5316 goto syntax;
5317 if (m != MATCH_YES)
5318 return MATCH_ERROR;
5319
5320 if (gfc_get_symbol (name, module_ns, &sym))
5321 return MATCH_ERROR;
5322
5323 if (sym->attr.proc != PROC_MODULE
5324 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5325 sym->name, NULL) == FAILURE)
5326 return MATCH_ERROR;
5327
5328 if (gfc_add_interface (sym) == FAILURE)
5329 return MATCH_ERROR;
5330
5331 sym->attr.mod_proc = 1;
5332
5333 if (gfc_match_eos () == MATCH_YES)
5334 break;
5335 if (gfc_match_char (',') != MATCH_YES)
5336 goto syntax;
5337 }
5338
5339 return MATCH_YES;
5340
5341 syntax:
5342 gfc_syntax_error (ST_MODULE_PROC);
5343 return MATCH_ERROR;
5344 }
5345
5346
5347 /* Match the optional attribute specifiers for a type declaration.
5348 Return MATCH_ERROR if an error is encountered in one of the handled
5349 attributes (public, private, bind(c)), MATCH_NO if what's found is
5350 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5351 checking on attribute conflicts needs to be done. */
5352
5353 match
5354 gfc_get_type_attr_spec (symbol_attribute *attr)
5355 {
5356 /* See if the derived type is marked as private. */
5357 if (gfc_match (" , private") == MATCH_YES)
5358 {
5359 if (gfc_current_state () != COMP_MODULE)
5360 {
5361 gfc_error ("Derived type at %C can only be PRIVATE in the "
5362 "specification part of a module");
5363 return MATCH_ERROR;
5364 }
5365
5366 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5367 return MATCH_ERROR;
5368 }
5369 else if (gfc_match (" , public") == MATCH_YES)
5370 {
5371 if (gfc_current_state () != COMP_MODULE)
5372 {
5373 gfc_error ("Derived type at %C can only be PUBLIC in the "
5374 "specification part of a module");
5375 return MATCH_ERROR;
5376 }
5377
5378 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5379 return MATCH_ERROR;
5380 }
5381 else if(gfc_match(" , bind ( c )") == MATCH_YES)
5382 {
5383 /* If the type is defined to be bind(c) it then needs to make
5384 sure that all fields are interoperable. This will
5385 need to be a semantic check on the finished derived type.
5386 See 15.2.3 (lines 9-12) of F2003 draft. */
5387 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5388 return MATCH_ERROR;
5389
5390 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5391 }
5392 else
5393 return MATCH_NO;
5394
5395 /* If we get here, something matched. */
5396 return MATCH_YES;
5397 }
5398
5399
5400 /* Match the beginning of a derived type declaration. If a type name
5401 was the result of a function, then it is possible to have a symbol
5402 already to be known as a derived type yet have no components. */
5403
5404 match
5405 gfc_match_derived_decl (void)
5406 {
5407 char name[GFC_MAX_SYMBOL_LEN + 1];
5408 symbol_attribute attr;
5409 gfc_symbol *sym;
5410 match m;
5411 match is_type_attr_spec = MATCH_NO;
5412
5413 if (gfc_current_state () == COMP_DERIVED)
5414 return MATCH_NO;
5415
5416 gfc_clear_attr (&attr);
5417
5418 do
5419 {
5420 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5421 if (is_type_attr_spec == MATCH_ERROR)
5422 return MATCH_ERROR;
5423 } while (is_type_attr_spec == MATCH_YES);
5424
5425 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
5426 {
5427 gfc_error ("Expected :: in TYPE definition at %C");
5428 return MATCH_ERROR;
5429 }
5430
5431 m = gfc_match (" %n%t", name);
5432 if (m != MATCH_YES)
5433 return m;
5434
5435 /* Make sure the name isn't the name of an intrinsic type. The
5436 'double {precision,complex}' types don't get past the name
5437 matcher, unless they're written as a single word or in fixed
5438 form. */
5439 if (strcmp (name, "integer") == 0
5440 || strcmp (name, "real") == 0
5441 || strcmp (name, "character") == 0
5442 || strcmp (name, "logical") == 0
5443 || strcmp (name, "complex") == 0
5444 || strcmp (name, "doubleprecision") == 0
5445 || strcmp (name, "doublecomplex") == 0)
5446 {
5447 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5448 "type", name);
5449 return MATCH_ERROR;
5450 }
5451
5452 if (gfc_get_symbol (name, NULL, &sym))
5453 return MATCH_ERROR;
5454
5455 if (sym->ts.type != BT_UNKNOWN)
5456 {
5457 gfc_error ("Derived type name '%s' at %C already has a basic type "
5458 "of %s", sym->name, gfc_typename (&sym->ts));
5459 return MATCH_ERROR;
5460 }
5461
5462 /* The symbol may already have the derived attribute without the
5463 components. The ways this can happen is via a function
5464 definition, an INTRINSIC statement or a subtype in another
5465 derived type that is a pointer. The first part of the AND clause
5466 is true if a the symbol is not the return value of a function. */
5467 if (sym->attr.flavor != FL_DERIVED
5468 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
5469 return MATCH_ERROR;
5470
5471 if (sym->components != NULL)
5472 {
5473 gfc_error ("Derived type definition of '%s' at %C has already been "
5474 "defined", sym->name);
5475 return MATCH_ERROR;
5476 }
5477
5478 if (attr.access != ACCESS_UNKNOWN
5479 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
5480 return MATCH_ERROR;
5481
5482 /* See if the derived type was labeled as bind(c). */
5483 if (attr.is_bind_c != 0)
5484 sym->attr.is_bind_c = attr.is_bind_c;
5485
5486 gfc_new_block = sym;
5487
5488 return MATCH_YES;
5489 }
5490
5491
5492 /* Cray Pointees can be declared as:
5493 pointer (ipt, a (n,m,...,*))
5494 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5495 cheat and set a constant bound of 1 for the last dimension, if this
5496 is the case. Since there is no bounds-checking for Cray Pointees,
5497 this will be okay. */
5498
5499 try
5500 gfc_mod_pointee_as (gfc_array_spec *as)
5501 {
5502 as->cray_pointee = true; /* This will be useful to know later. */
5503 if (as->type == AS_ASSUMED_SIZE)
5504 {
5505 as->type = AS_EXPLICIT;
5506 as->upper[as->rank - 1] = gfc_int_expr (1);
5507 as->cp_was_assumed = true;
5508 }
5509 else if (as->type == AS_ASSUMED_SHAPE)
5510 {
5511 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5512 return MATCH_ERROR;
5513 }
5514 return MATCH_YES;
5515 }
5516
5517
5518 /* Match the enum definition statement, here we are trying to match
5519 the first line of enum definition statement.
5520 Returns MATCH_YES if match is found. */
5521
5522 match
5523 gfc_match_enum (void)
5524 {
5525 match m;
5526
5527 m = gfc_match_eos ();
5528 if (m != MATCH_YES)
5529 return m;
5530
5531 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
5532 == FAILURE)
5533 return MATCH_ERROR;
5534
5535 return MATCH_YES;
5536 }
5537
5538
5539 /* Match a variable name with an optional initializer. When this
5540 subroutine is called, a variable is expected to be parsed next.
5541 Depending on what is happening at the moment, updates either the
5542 symbol table or the current interface. */
5543
5544 static match
5545 enumerator_decl (void)
5546 {
5547 char name[GFC_MAX_SYMBOL_LEN + 1];
5548 gfc_expr *initializer;
5549 gfc_array_spec *as = NULL;
5550 gfc_symbol *sym;
5551 locus var_locus;
5552 match m;
5553 try t;
5554 locus old_locus;
5555
5556 initializer = NULL;
5557 old_locus = gfc_current_locus;
5558
5559 /* When we get here, we've just matched a list of attributes and
5560 maybe a type and a double colon. The next thing we expect to see
5561 is the name of the symbol. */
5562 m = gfc_match_name (name);
5563 if (m != MATCH_YES)
5564 goto cleanup;
5565
5566 var_locus = gfc_current_locus;
5567
5568 /* OK, we've successfully matched the declaration. Now put the
5569 symbol in the current namespace. If we fail to create the symbol,
5570 bail out. */
5571 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5572 {
5573 m = MATCH_ERROR;
5574 goto cleanup;
5575 }
5576
5577 /* The double colon must be present in order to have initializers.
5578 Otherwise the statement is ambiguous with an assignment statement. */
5579 if (colon_seen)
5580 {
5581 if (gfc_match_char ('=') == MATCH_YES)
5582 {
5583 m = gfc_match_init_expr (&initializer);
5584 if (m == MATCH_NO)
5585 {
5586 gfc_error ("Expected an initialization expression at %C");
5587 m = MATCH_ERROR;
5588 }
5589
5590 if (m != MATCH_YES)
5591 goto cleanup;
5592 }
5593 }
5594
5595 /* If we do not have an initializer, the initialization value of the
5596 previous enumerator (stored in last_initializer) is incremented
5597 by 1 and is used to initialize the current enumerator. */
5598 if (initializer == NULL)
5599 initializer = gfc_enum_initializer (last_initializer, old_locus);
5600
5601 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
5602 {
5603 gfc_error("ENUMERATOR %L not initialized with integer expression",
5604 &var_locus);
5605 m = MATCH_ERROR;
5606 gfc_free_enum_history ();
5607 goto cleanup;
5608 }
5609
5610 /* Store this current initializer, for the next enumerator variable
5611 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5612 use last_initializer below. */
5613 last_initializer = initializer;
5614 t = add_init_expr_to_sym (name, &initializer, &var_locus);
5615
5616 /* Maintain enumerator history. */
5617 gfc_find_symbol (name, NULL, 0, &sym);
5618 create_enum_history (sym, last_initializer);
5619
5620 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
5621
5622 cleanup:
5623 /* Free stuff up and return. */
5624 gfc_free_expr (initializer);
5625
5626 return m;
5627 }
5628
5629
5630 /* Match the enumerator definition statement. */
5631
5632 match
5633 gfc_match_enumerator_def (void)
5634 {
5635 match m;
5636 try t;
5637
5638 gfc_clear_ts (&current_ts);
5639
5640 m = gfc_match (" enumerator");
5641 if (m != MATCH_YES)
5642 return m;
5643
5644 m = gfc_match (" :: ");
5645 if (m == MATCH_ERROR)
5646 return m;
5647
5648 colon_seen = (m == MATCH_YES);
5649
5650 if (gfc_current_state () != COMP_ENUM)
5651 {
5652 gfc_error ("ENUM definition statement expected before %C");
5653 gfc_free_enum_history ();
5654 return MATCH_ERROR;
5655 }
5656
5657 (&current_ts)->type = BT_INTEGER;
5658 (&current_ts)->kind = gfc_c_int_kind;
5659
5660 gfc_clear_attr (&current_attr);
5661 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
5662 if (t == FAILURE)
5663 {
5664 m = MATCH_ERROR;
5665 goto cleanup;
5666 }
5667
5668 for (;;)
5669 {
5670 m = enumerator_decl ();
5671 if (m == MATCH_ERROR)
5672 goto cleanup;
5673 if (m == MATCH_NO)
5674 break;
5675
5676 if (gfc_match_eos () == MATCH_YES)
5677 goto cleanup;
5678 if (gfc_match_char (',') != MATCH_YES)
5679 break;
5680 }
5681
5682 if (gfc_current_state () == COMP_ENUM)
5683 {
5684 gfc_free_enum_history ();
5685 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5686 m = MATCH_ERROR;
5687 }
5688
5689 cleanup:
5690 gfc_free_array_spec (current_as);
5691 current_as = NULL;
5692 return m;
5693
5694 }
5695