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