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