OSDN Git Service

2010-04-06 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "flags.h"
28
29
30 /* Macros to access allocate memory for gfc_data_variable,
31    gfc_data_value and gfc_data.  */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
35
36
37 /* This flag is set if an old-style length selector is matched
38    during a type-declaration statement.  */
39
40 static int old_char_selector;
41
42 /* When variables acquire types and attributes from a declaration
43    statement, they get them from the following static variables.  The
44    first part of a declaration sets these variables and the second
45    part copies these into symbol structures.  */
46
47 static gfc_typespec current_ts;
48
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
52
53 /* The current binding label (if any).  */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56    line in case we're given the BIND(C) attribute with a NAME= specifier.  */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59    can supply a name if the curr_binding_label is nil and NAME= was not.  */
60 static int has_name_equals = 0;
61
62 /* Initializer of the previous enumerator.  */
63
64 static gfc_expr *last_initializer;
65
66 /* History of all the enumerators is maintained, so that
67    kind values of all the enumerators could be updated depending
68    upon the maximum initialized value.  */
69
70 typedef struct enumerator_history
71 {
72   gfc_symbol *sym;
73   gfc_expr *initializer;
74   struct enumerator_history *next;
75 }
76 enumerator_history;
77
78 /* Header of enum history chain.  */
79
80 static enumerator_history *enum_history = NULL;
81
82 /* Pointer of enum history node containing largest initializer.  */
83
84 static enumerator_history *max_enum = NULL;
85
86 /* gfc_new_block points to the symbol of a newly matched block.  */
87
88 gfc_symbol *gfc_new_block;
89
90 bool gfc_matching_function;
91
92
93 /********************* DATA statement subroutines *********************/
94
95 static bool in_match_data = false;
96
97 bool
98 gfc_in_match_data (void)
99 {
100   return in_match_data;
101 }
102
103 static void
104 set_in_match_data (bool set_value)
105 {
106   in_match_data = set_value;
107 }
108
109 /* Free a gfc_data_variable structure and everything beneath it.  */
110
111 static void
112 free_variable (gfc_data_variable *p)
113 {
114   gfc_data_variable *q;
115
116   for (; p; p = q)
117     {
118       q = p->next;
119       gfc_free_expr (p->expr);
120       gfc_free_iterator (&p->iter, 0);
121       free_variable (p->list);
122       gfc_free (p);
123     }
124 }
125
126
127 /* Free a gfc_data_value structure and everything beneath it.  */
128
129 static void
130 free_value (gfc_data_value *p)
131 {
132   gfc_data_value *q;
133
134   for (; p; p = q)
135     {
136       q = p->next;
137       gfc_free_expr (p->expr);
138       gfc_free (p);
139     }
140 }
141
142
143 /* Free a list of gfc_data structures.  */
144
145 void
146 gfc_free_data (gfc_data *p)
147 {
148   gfc_data *q;
149
150   for (; p; p = q)
151     {
152       q = p->next;
153       free_variable (p->var);
154       free_value (p->value);
155       gfc_free (p);
156     }
157 }
158
159
160 /* Free all data in a namespace.  */
161
162 static void
163 gfc_free_data_all (gfc_namespace *ns)
164 {
165   gfc_data *d;
166
167   for (;ns->data;)
168     {
169       d = ns->data->next;
170       gfc_free (ns->data);
171       ns->data = d;
172     }
173 }
174
175
176 static match var_element (gfc_data_variable *);
177
178 /* Match a list of variables terminated by an iterator and a right
179    parenthesis.  */
180
181 static match
182 var_list (gfc_data_variable *parent)
183 {
184   gfc_data_variable *tail, var;
185   match m;
186
187   m = var_element (&var);
188   if (m == MATCH_ERROR)
189     return MATCH_ERROR;
190   if (m == MATCH_NO)
191     goto syntax;
192
193   tail = gfc_get_data_variable ();
194   *tail = var;
195
196   parent->list = tail;
197
198   for (;;)
199     {
200       if (gfc_match_char (',') != MATCH_YES)
201         goto syntax;
202
203       m = gfc_match_iterator (&parent->iter, 1);
204       if (m == MATCH_YES)
205         break;
206       if (m == MATCH_ERROR)
207         return MATCH_ERROR;
208
209       m = var_element (&var);
210       if (m == MATCH_ERROR)
211         return MATCH_ERROR;
212       if (m == MATCH_NO)
213         goto syntax;
214
215       tail->next = gfc_get_data_variable ();
216       tail = tail->next;
217
218       *tail = var;
219     }
220
221   if (gfc_match_char (')') != MATCH_YES)
222     goto syntax;
223   return MATCH_YES;
224
225 syntax:
226   gfc_syntax_error (ST_DATA);
227   return MATCH_ERROR;
228 }
229
230
231 /* Match a single element in a data variable list, which can be a
232    variable-iterator list.  */
233
234 static match
235 var_element (gfc_data_variable *new_var)
236 {
237   match m;
238   gfc_symbol *sym;
239
240   memset (new_var, 0, sizeof (gfc_data_variable));
241
242   if (gfc_match_char ('(') == MATCH_YES)
243     return var_list (new_var);
244
245   m = gfc_match_variable (&new_var->expr, 0);
246   if (m != MATCH_YES)
247     return m;
248
249   sym = new_var->expr->symtree->n.sym;
250
251   /* Symbol should already have an associated type.  */
252   if (gfc_check_symbol_typed (sym, gfc_current_ns,
253                               false, gfc_current_locus) == FAILURE)
254     return MATCH_ERROR;
255
256   if (!sym->attr.function && gfc_current_ns->parent
257       && gfc_current_ns->parent == sym->ns)
258     {
259       gfc_error ("Host associated variable '%s' may not be in the DATA "
260                  "statement at %C", sym->name);
261       return MATCH_ERROR;
262     }
263
264   if (gfc_current_state () != COMP_BLOCK_DATA
265       && sym->attr.in_common
266       && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
267                          "common block variable '%s' in DATA statement at %C",
268                          sym->name) == FAILURE)
269     return MATCH_ERROR;
270
271   if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
272     return MATCH_ERROR;
273
274   return MATCH_YES;
275 }
276
277
278 /* Match the top-level list of data variables.  */
279
280 static match
281 top_var_list (gfc_data *d)
282 {
283   gfc_data_variable var, *tail, *new_var;
284   match m;
285
286   tail = NULL;
287
288   for (;;)
289     {
290       m = var_element (&var);
291       if (m == MATCH_NO)
292         goto syntax;
293       if (m == MATCH_ERROR)
294         return MATCH_ERROR;
295
296       new_var = gfc_get_data_variable ();
297       *new_var = var;
298
299       if (tail == NULL)
300         d->var = new_var;
301       else
302         tail->next = new_var;
303
304       tail = new_var;
305
306       if (gfc_match_char ('/') == MATCH_YES)
307         break;
308       if (gfc_match_char (',') != MATCH_YES)
309         goto syntax;
310     }
311
312   return MATCH_YES;
313
314 syntax:
315   gfc_syntax_error (ST_DATA);
316   gfc_free_data_all (gfc_current_ns);
317   return MATCH_ERROR;
318 }
319
320
321 static match
322 match_data_constant (gfc_expr **result)
323 {
324   char name[GFC_MAX_SYMBOL_LEN + 1];
325   gfc_symbol *sym;
326   gfc_expr *expr;
327   match m;
328   locus old_loc;
329
330   m = gfc_match_literal_constant (&expr, 1);
331   if (m == MATCH_YES)
332     {
333       *result = expr;
334       return MATCH_YES;
335     }
336
337   if (m == MATCH_ERROR)
338     return MATCH_ERROR;
339
340   m = gfc_match_null (result);
341   if (m != MATCH_NO)
342     return m;
343
344   old_loc = gfc_current_locus;
345
346   /* Should this be a structure component, try to match it
347      before matching a name.  */
348   m = gfc_match_rvalue (result);
349   if (m == MATCH_ERROR)
350     return m;
351
352   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
353     {
354       if (gfc_simplify_expr (*result, 0) == FAILURE)
355         m = MATCH_ERROR;
356       return m;
357     }
358
359   gfc_current_locus = old_loc;
360
361   m = gfc_match_name (name);
362   if (m != MATCH_YES)
363     return m;
364
365   if (gfc_find_symbol (name, NULL, 1, &sym))
366     return MATCH_ERROR;
367
368   if (sym == NULL
369       || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
370     {
371       gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
372                  name);
373       return MATCH_ERROR;
374     }
375   else if (sym->attr.flavor == FL_DERIVED)
376     return gfc_match_structure_constructor (sym, result, false);
377
378   /* Check to see if the value is an initialization array expression.  */
379   if (sym->value->expr_type == EXPR_ARRAY)
380     {
381       gfc_current_locus = old_loc;
382
383       m = gfc_match_init_expr (result);
384       if (m == MATCH_ERROR)
385         return m;
386
387       if (m == MATCH_YES)
388         {
389           if (gfc_simplify_expr (*result, 0) == FAILURE)
390             m = MATCH_ERROR;
391
392           if ((*result)->expr_type == EXPR_CONSTANT)
393             return m;
394           else
395             {
396               gfc_error ("Invalid initializer %s in Data statement at %C", name);
397               return MATCH_ERROR;
398             }
399         }
400     }
401
402   *result = gfc_copy_expr (sym->value);
403   return MATCH_YES;
404 }
405
406
407 /* Match a list of values in a DATA statement.  The leading '/' has
408    already been seen at this point.  */
409
410 static match
411 top_val_list (gfc_data *data)
412 {
413   gfc_data_value *new_val, *tail;
414   gfc_expr *expr;
415   match m;
416
417   tail = NULL;
418
419   for (;;)
420     {
421       m = match_data_constant (&expr);
422       if (m == MATCH_NO)
423         goto syntax;
424       if (m == MATCH_ERROR)
425         return MATCH_ERROR;
426
427       new_val = gfc_get_data_value ();
428       mpz_init (new_val->repeat);
429
430       if (tail == NULL)
431         data->value = new_val;
432       else
433         tail->next = new_val;
434
435       tail = new_val;
436
437       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
438         {
439           tail->expr = expr;
440           mpz_set_ui (tail->repeat, 1);
441         }
442       else
443         {
444           if (expr->ts.type == BT_INTEGER)
445             mpz_set (tail->repeat, expr->value.integer);
446           gfc_free_expr (expr);
447
448           m = match_data_constant (&tail->expr);
449           if (m == MATCH_NO)
450             goto syntax;
451           if (m == MATCH_ERROR)
452             return MATCH_ERROR;
453         }
454
455       if (gfc_match_char ('/') == MATCH_YES)
456         break;
457       if (gfc_match_char (',') == MATCH_NO)
458         goto syntax;
459     }
460
461   return MATCH_YES;
462
463 syntax:
464   gfc_syntax_error (ST_DATA);
465   gfc_free_data_all (gfc_current_ns);
466   return MATCH_ERROR;
467 }
468
469
470 /* Matches an old style initialization.  */
471
472 static match
473 match_old_style_init (const char *name)
474 {
475   match m;
476   gfc_symtree *st;
477   gfc_symbol *sym;
478   gfc_data *newdata;
479
480   /* Set up data structure to hold initializers.  */
481   gfc_find_sym_tree (name, NULL, 0, &st);
482   sym = st->n.sym;
483
484   newdata = gfc_get_data ();
485   newdata->var = gfc_get_data_variable ();
486   newdata->var->expr = gfc_get_variable_expr (st);
487   newdata->where = gfc_current_locus;
488
489   /* Match initial value list. This also eats the terminal '/'.  */
490   m = top_val_list (newdata);
491   if (m != MATCH_YES)
492     {
493       gfc_free (newdata);
494       return m;
495     }
496
497   if (gfc_pure (NULL))
498     {
499       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
500       gfc_free (newdata);
501       return MATCH_ERROR;
502     }
503
504   /* Mark the variable as having appeared in a data statement.  */
505   if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
506     {
507       gfc_free (newdata);
508       return MATCH_ERROR;
509     }
510
511   /* Chain in namespace list of DATA initializers.  */
512   newdata->next = gfc_current_ns->data;
513   gfc_current_ns->data = newdata;
514
515   return m;
516 }
517
518
519 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
520    we are matching a DATA statement and are therefore issuing an error
521    if we encounter something unexpected, if not, we're trying to match
522    an old-style initialization expression of the form INTEGER I /2/.  */
523
524 match
525 gfc_match_data (void)
526 {
527   gfc_data *new_data;
528   match m;
529
530   set_in_match_data (true);
531
532   for (;;)
533     {
534       new_data = gfc_get_data ();
535       new_data->where = gfc_current_locus;
536
537       m = top_var_list (new_data);
538       if (m != MATCH_YES)
539         goto cleanup;
540
541       m = top_val_list (new_data);
542       if (m != MATCH_YES)
543         goto cleanup;
544
545       new_data->next = gfc_current_ns->data;
546       gfc_current_ns->data = new_data;
547
548       if (gfc_match_eos () == MATCH_YES)
549         break;
550
551       gfc_match_char (',');     /* Optional comma */
552     }
553
554   set_in_match_data (false);
555
556   if (gfc_pure (NULL))
557     {
558       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
559       return MATCH_ERROR;
560     }
561
562   return MATCH_YES;
563
564 cleanup:
565   set_in_match_data (false);
566   gfc_free_data (new_data);
567   return MATCH_ERROR;
568 }
569
570
571 /************************ Declaration statements *********************/
572
573 /* Match an intent specification.  Since this can only happen after an
574    INTENT word, a legal intent-spec must follow.  */
575
576 static sym_intent
577 match_intent_spec (void)
578 {
579
580   if (gfc_match (" ( in out )") == MATCH_YES)
581     return INTENT_INOUT;
582   if (gfc_match (" ( in )") == MATCH_YES)
583     return INTENT_IN;
584   if (gfc_match (" ( out )") == MATCH_YES)
585     return INTENT_OUT;
586
587   gfc_error ("Bad INTENT specification at %C");
588   return INTENT_UNKNOWN;
589 }
590
591
592 /* Matches a character length specification, which is either a
593    specification expression or a '*'.  */
594
595 static match
596 char_len_param_value (gfc_expr **expr)
597 {
598   match m;
599
600   if (gfc_match_char ('*') == MATCH_YES)
601     {
602       *expr = NULL;
603       return MATCH_YES;
604     }
605
606   m = gfc_match_expr (expr);
607
608   if (m == MATCH_YES
609       && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
610     return MATCH_ERROR;
611
612   if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
613     {
614       if ((*expr)->value.function.actual
615           && (*expr)->value.function.actual->expr->symtree)
616         {
617           gfc_expr *e;
618           e = (*expr)->value.function.actual->expr;
619           if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
620               && e->expr_type == EXPR_VARIABLE)
621             {
622               if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
623                 goto syntax;
624               if (e->symtree->n.sym->ts.type == BT_CHARACTER
625                   && e->symtree->n.sym->ts.u.cl
626                   && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
627                 goto syntax;
628             }
629         }
630     }
631   return m;
632
633 syntax:
634   gfc_error ("Conflict in attributes of function argument at %C");
635   return MATCH_ERROR;
636 }
637
638
639 /* A character length is a '*' followed by a literal integer or a
640    char_len_param_value in parenthesis.  */
641
642 static match
643 match_char_length (gfc_expr **expr)
644 {
645   int length;
646   match m;
647
648   m = gfc_match_char ('*');
649   if (m != MATCH_YES)
650     return m;
651
652   m = gfc_match_small_literal_int (&length, NULL);
653   if (m == MATCH_ERROR)
654     return m;
655
656   if (m == MATCH_YES)
657     {
658       if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
659                           "Old-style character length at %C") == FAILURE)
660         return MATCH_ERROR;
661       *expr = gfc_int_expr (length);
662       return m;
663     }
664
665   if (gfc_match_char ('(') == MATCH_NO)
666     goto syntax;
667
668   m = char_len_param_value (expr);
669   if (m != MATCH_YES && gfc_matching_function)
670     {
671       gfc_undo_symbols ();
672       m = MATCH_YES;
673     }
674
675   if (m == MATCH_ERROR)
676     return m;
677   if (m == MATCH_NO)
678     goto syntax;
679
680   if (gfc_match_char (')') == MATCH_NO)
681     {
682       gfc_free_expr (*expr);
683       *expr = NULL;
684       goto syntax;
685     }
686
687   return MATCH_YES;
688
689 syntax:
690   gfc_error ("Syntax error in character length specification at %C");
691   return MATCH_ERROR;
692 }
693
694
695 /* Special subroutine for finding a symbol.  Check if the name is found
696    in the current name space.  If not, and we're compiling a function or
697    subroutine and the parent compilation unit is an interface, then check
698    to see if the name we've been given is the name of the interface
699    (located in another namespace).  */
700
701 static int
702 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
703 {
704   gfc_state_data *s;
705   gfc_symtree *st;
706   int i;
707
708   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
709   if (i == 0)
710     {
711       *result = st ? st->n.sym : NULL;
712       goto end;
713     }
714
715   if (gfc_current_state () != COMP_SUBROUTINE
716       && gfc_current_state () != COMP_FUNCTION)
717     goto end;
718
719   s = gfc_state_stack->previous;
720   if (s == NULL)
721     goto end;
722
723   if (s->state != COMP_INTERFACE)
724     goto end;
725   if (s->sym == NULL)
726     goto end;             /* Nameless interface.  */
727
728   if (strcmp (name, s->sym->name) == 0)
729     {
730       *result = s->sym;
731       return 0;
732     }
733
734 end:
735   return i;
736 }
737
738
739 /* Special subroutine for getting a symbol node associated with a
740    procedure name, used in SUBROUTINE and FUNCTION statements.  The
741    symbol is created in the parent using with symtree node in the
742    child unit pointing to the symbol.  If the current namespace has no
743    parent, then the symbol is just created in the current unit.  */
744
745 static int
746 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
747 {
748   gfc_symtree *st;
749   gfc_symbol *sym;
750   int rc = 0;
751
752   /* Module functions have to be left in their own namespace because
753      they have potentially (almost certainly!) already been referenced.
754      In this sense, they are rather like external functions.  This is
755      fixed up in resolve.c(resolve_entries), where the symbol name-
756      space is set to point to the master function, so that the fake
757      result mechanism can work.  */
758   if (module_fcn_entry)
759     {
760       /* Present if entry is declared to be a module procedure.  */
761       rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
762
763       if (*result == NULL)
764         rc = gfc_get_symbol (name, NULL, result);
765       else if (!gfc_get_symbol (name, NULL, &sym) && sym
766                  && (*result)->ts.type == BT_UNKNOWN
767                  && sym->attr.flavor == FL_UNKNOWN)
768         /* Pick up the typespec for the entry, if declared in the function
769            body.  Note that this symbol is FL_UNKNOWN because it will
770            only have appeared in a type declaration.  The local symtree
771            is set to point to the module symbol and a unique symtree
772            to the local version.  This latter ensures a correct clearing
773            of the symbols.  */
774         {
775           /* If the ENTRY proceeds its specification, we need to ensure
776              that this does not raise a "has no IMPLICIT type" error.  */
777           if (sym->ts.type == BT_UNKNOWN)
778             sym->attr.untyped = 1;
779
780           (*result)->ts = sym->ts;
781
782           /* Put the symbol in the procedure namespace so that, should
783              the ENTRY precede its specification, the specification
784              can be applied.  */
785           (*result)->ns = gfc_current_ns;
786
787           gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
788           st->n.sym = *result;
789           st = gfc_get_unique_symtree (gfc_current_ns);
790           st->n.sym = sym;
791         }
792     }
793   else
794     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
795
796   if (rc)
797     return rc;
798
799   sym = *result;
800   gfc_current_ns->refs++;
801
802   if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
803     {
804       /* Trap another encompassed procedure with the same name.  All
805          these conditions are necessary to avoid picking up an entry
806          whose name clashes with that of the encompassing procedure;
807          this is handled using gsymbols to register unique,globally
808          accessible names.  */
809       if (sym->attr.flavor != 0
810           && sym->attr.proc != 0
811           && (sym->attr.subroutine || sym->attr.function)
812           && sym->attr.if_source != IFSRC_UNKNOWN)
813         gfc_error_now ("Procedure '%s' at %C is already defined at %L",
814                        name, &sym->declared_at);
815
816       /* Trap a procedure with a name the same as interface in the
817          encompassing scope.  */
818       if (sym->attr.generic != 0
819           && (sym->attr.subroutine || sym->attr.function)
820           && !sym->attr.mod_proc)
821         gfc_error_now ("Name '%s' at %C is already defined"
822                        " as a generic interface at %L",
823                        name, &sym->declared_at);
824
825       /* Trap declarations of attributes in encompassing scope.  The
826          signature for this is that ts.kind is set.  Legitimate
827          references only set ts.type.  */
828       if (sym->ts.kind != 0
829           && !sym->attr.implicit_type
830           && sym->attr.proc == 0
831           && gfc_current_ns->parent != NULL
832           && sym->attr.access == 0
833           && !module_fcn_entry)
834         gfc_error_now ("Procedure '%s' at %C has an explicit interface "
835                        "and must not have attributes declared at %L",
836                        name, &sym->declared_at);
837     }
838
839   if (gfc_current_ns->parent == NULL || *result == NULL)
840     return rc;
841
842   /* Module function entries will already have a symtree in
843      the current namespace but will need one at module level.  */
844   if (module_fcn_entry)
845     {
846       /* Present if entry is declared to be a module procedure.  */
847       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
848       if (st == NULL)
849         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
850     }
851   else
852     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
853
854   st->n.sym = sym;
855   sym->refs++;
856
857   /* See if the procedure should be a module procedure.  */
858
859   if (((sym->ns->proc_name != NULL
860                 && sym->ns->proc_name->attr.flavor == FL_MODULE
861                 && sym->attr.proc != PROC_MODULE)
862             || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
863         && gfc_add_procedure (&sym->attr, PROC_MODULE,
864                               sym->name, NULL) == FAILURE)
865     rc = 2;
866
867   return rc;
868 }
869
870
871 /* Verify that the given symbol representing a parameter is C
872    interoperable, by checking to see if it was marked as such after
873    its declaration.  If the given symbol is not interoperable, a
874    warning is reported, thus removing the need to return the status to
875    the calling function.  The standard does not require the user use
876    one of the iso_c_binding named constants to declare an
877    interoperable parameter, but we can't be sure if the param is C
878    interop or not if the user doesn't.  For example, integer(4) may be
879    legal Fortran, but doesn't have meaning in C.  It may interop with
880    a number of the C types, which causes a problem because the
881    compiler can't know which one.  This code is almost certainly not
882    portable, and the user will get what they deserve if the C type
883    across platforms isn't always interoperable with integer(4).  If
884    the user had used something like integer(c_int) or integer(c_long),
885    the compiler could have automatically handled the varying sizes
886    across platforms.  */
887
888 gfc_try
889 verify_c_interop_param (gfc_symbol *sym)
890 {
891   int is_c_interop = 0;
892   gfc_try retval = SUCCESS;
893
894   /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
895      Don't repeat the checks here.  */
896   if (sym->attr.implicit_type)
897     return SUCCESS;
898   
899   /* For subroutines or functions that are passed to a BIND(C) procedure,
900      they're interoperable if they're BIND(C) and their params are all
901      interoperable.  */
902   if (sym->attr.flavor == FL_PROCEDURE)
903     {
904       if (sym->attr.is_bind_c == 0)
905         {
906           gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
907                          "attribute to be C interoperable", sym->name,
908                          &(sym->declared_at));
909                          
910           return FAILURE;
911         }
912       else
913         {
914           if (sym->attr.is_c_interop == 1)
915             /* We've already checked this procedure; don't check it again.  */
916             return SUCCESS;
917           else
918             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
919                                       sym->common_block);
920         }
921     }
922   
923   /* See if we've stored a reference to a procedure that owns sym.  */
924   if (sym->ns != NULL && sym->ns->proc_name != NULL)
925     {
926       if (sym->ns->proc_name->attr.is_bind_c == 1)
927         {
928           is_c_interop =
929             (verify_c_interop (&(sym->ts))
930              == SUCCESS ? 1 : 0);
931
932           if (is_c_interop != 1)
933             {
934               /* Make personalized messages to give better feedback.  */
935               if (sym->ts.type == BT_DERIVED)
936                 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
937                            " procedure '%s' but is not C interoperable "
938                            "because derived type '%s' is not C interoperable",
939                            sym->name, &(sym->declared_at),
940                            sym->ns->proc_name->name, 
941                            sym->ts.u.derived->name);
942               else
943                 gfc_warning ("Variable '%s' at %L is a parameter to the "
944                              "BIND(C) procedure '%s' but may not be C "
945                              "interoperable",
946                              sym->name, &(sym->declared_at),
947                              sym->ns->proc_name->name);
948             }
949
950           /* Character strings are only C interoperable if they have a
951              length of 1.  */
952           if (sym->ts.type == BT_CHARACTER)
953             {
954               gfc_charlen *cl = sym->ts.u.cl;
955               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
956                   || mpz_cmp_si (cl->length->value.integer, 1) != 0)
957                 {
958                   gfc_error ("Character argument '%s' at %L "
959                              "must be length 1 because "
960                              "procedure '%s' is BIND(C)",
961                              sym->name, &sym->declared_at,
962                              sym->ns->proc_name->name);
963                   retval = FAILURE;
964                 }
965             }
966
967           /* We have to make sure that any param to a bind(c) routine does
968              not have the allocatable, pointer, or optional attributes,
969              according to J3/04-007, section 5.1.  */
970           if (sym->attr.allocatable == 1)
971             {
972               gfc_error ("Variable '%s' at %L cannot have the "
973                          "ALLOCATABLE attribute because procedure '%s'"
974                          " is BIND(C)", sym->name, &(sym->declared_at),
975                          sym->ns->proc_name->name);
976               retval = FAILURE;
977             }
978
979           if (sym->attr.pointer == 1)
980             {
981               gfc_error ("Variable '%s' at %L cannot have the "
982                          "POINTER attribute because procedure '%s'"
983                          " is BIND(C)", sym->name, &(sym->declared_at),
984                          sym->ns->proc_name->name);
985               retval = FAILURE;
986             }
987
988           if (sym->attr.optional == 1)
989             {
990               gfc_error ("Variable '%s' at %L cannot have the "
991                          "OPTIONAL attribute because procedure '%s'"
992                          " is BIND(C)", sym->name, &(sym->declared_at),
993                          sym->ns->proc_name->name);
994               retval = FAILURE;
995             }
996
997           /* Make sure that if it has the dimension attribute, that it is
998              either assumed size or explicit shape.  */
999           if (sym->as != NULL)
1000             {
1001               if (sym->as->type == AS_ASSUMED_SHAPE)
1002                 {
1003                   gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1004                              "argument to the procedure '%s' at %L because "
1005                              "the procedure is BIND(C)", sym->name,
1006                              &(sym->declared_at), sym->ns->proc_name->name,
1007                              &(sym->ns->proc_name->declared_at));
1008                   retval = FAILURE;
1009                 }
1010
1011               if (sym->as->type == AS_DEFERRED)
1012                 {
1013                   gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1014                              "argument to the procedure '%s' at %L because "
1015                              "the procedure is BIND(C)", sym->name,
1016                              &(sym->declared_at), sym->ns->proc_name->name,
1017                              &(sym->ns->proc_name->declared_at));
1018                   retval = FAILURE;
1019                 }
1020           }
1021         }
1022     }
1023
1024   return retval;
1025 }
1026
1027
1028
1029 /* Function called by variable_decl() that adds a name to the symbol table.  */
1030
1031 static gfc_try
1032 build_sym (const char *name, gfc_charlen *cl,
1033            gfc_array_spec **as, locus *var_locus)
1034 {
1035   symbol_attribute attr;
1036   gfc_symbol *sym;
1037
1038   if (gfc_get_symbol (name, NULL, &sym))
1039     return FAILURE;
1040
1041   /* Start updating the symbol table.  Add basic type attribute if present.  */
1042   if (current_ts.type != BT_UNKNOWN
1043       && (sym->attr.implicit_type == 0
1044           || !gfc_compare_types (&sym->ts, &current_ts))
1045       && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1046     return FAILURE;
1047
1048   if (sym->ts.type == BT_CHARACTER)
1049     sym->ts.u.cl = cl;
1050
1051   /* Add dimension attribute if present.  */
1052   if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1053     return FAILURE;
1054   *as = NULL;
1055
1056   /* Add attribute to symbol.  The copy is so that we can reset the
1057      dimension attribute.  */
1058   attr = current_attr;
1059   attr.dimension = 0;
1060
1061   if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1062     return FAILURE;
1063
1064   /* Finish any work that may need to be done for the binding label,
1065      if it's a bind(c).  The bind(c) attr is found before the symbol
1066      is made, and before the symbol name (for data decls), so the
1067      current_ts is holding the binding label, or nothing if the
1068      name= attr wasn't given.  Therefore, test here if we're dealing
1069      with a bind(c) and make sure the binding label is set correctly.  */
1070   if (sym->attr.is_bind_c == 1)
1071     {
1072       if (sym->binding_label[0] == '\0')
1073         {
1074           /* Set the binding label and verify that if a NAME= was specified
1075              then only one identifier was in the entity-decl-list.  */
1076           if (set_binding_label (sym->binding_label, sym->name,
1077                                  num_idents_on_line) == FAILURE)
1078             return FAILURE;
1079         }
1080     }
1081
1082   /* See if we know we're in a common block, and if it's a bind(c)
1083      common then we need to make sure we're an interoperable type.  */
1084   if (sym->attr.in_common == 1)
1085     {
1086       /* Test the common block object.  */
1087       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1088           && sym->ts.is_c_interop != 1)
1089         {
1090           gfc_error_now ("Variable '%s' in common block '%s' at %C "
1091                          "must be declared with a C interoperable "
1092                          "kind since common block '%s' is BIND(C)",
1093                          sym->name, sym->common_block->name,
1094                          sym->common_block->name);
1095           gfc_clear_error ();
1096         }
1097     }
1098
1099   sym->attr.implied_index = 0;
1100
1101   if (sym->ts.type == BT_CLASS)
1102     {
1103       sym->attr.class_ok = (sym->attr.dummy
1104                               || sym->attr.pointer
1105                               || sym->attr.allocatable) ? 1 : 0;
1106       gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1107     }
1108
1109   return SUCCESS;
1110 }
1111
1112
1113 /* Set character constant to the given length. The constant will be padded or
1114    truncated.  If we're inside an array constructor without a typespec, we
1115    additionally check that all elements have the same length; check_len -1
1116    means no checking.  */
1117
1118 void
1119 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1120 {
1121   gfc_char_t *s;
1122   int slen;
1123
1124   gcc_assert (expr->expr_type == EXPR_CONSTANT);
1125   gcc_assert (expr->ts.type == BT_CHARACTER);
1126
1127   slen = expr->value.character.length;
1128   if (len != slen)
1129     {
1130       s = gfc_get_wide_string (len + 1);
1131       memcpy (s, expr->value.character.string,
1132               MIN (len, slen) * sizeof (gfc_char_t));
1133       if (len > slen)
1134         gfc_wide_memset (&s[slen], ' ', len - slen);
1135
1136       if (gfc_option.warn_character_truncation && slen > len)
1137         gfc_warning_now ("CHARACTER expression at %L is being truncated "
1138                          "(%d/%d)", &expr->where, slen, len);
1139
1140       /* Apply the standard by 'hand' otherwise it gets cleared for
1141          initializers.  */
1142       if (check_len != -1 && slen != check_len
1143           && !(gfc_option.allow_std & GFC_STD_GNU))
1144         gfc_error_now ("The CHARACTER elements of the array constructor "
1145                        "at %L must have the same length (%d/%d)",
1146                         &expr->where, slen, check_len);
1147
1148       s[len] = '\0';
1149       gfc_free (expr->value.character.string);
1150       expr->value.character.string = s;
1151       expr->value.character.length = len;
1152     }
1153 }
1154
1155
1156 /* Function to create and update the enumerator history
1157    using the information passed as arguments.
1158    Pointer "max_enum" is also updated, to point to
1159    enum history node containing largest initializer.
1160
1161    SYM points to the symbol node of enumerator.
1162    INIT points to its enumerator value.  */
1163
1164 static void
1165 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1166 {
1167   enumerator_history *new_enum_history;
1168   gcc_assert (sym != NULL && init != NULL);
1169
1170   new_enum_history = XCNEW (enumerator_history);
1171
1172   new_enum_history->sym = sym;
1173   new_enum_history->initializer = init;
1174   new_enum_history->next = NULL;
1175
1176   if (enum_history == NULL)
1177     {
1178       enum_history = new_enum_history;
1179       max_enum = enum_history;
1180     }
1181   else
1182     {
1183       new_enum_history->next = enum_history;
1184       enum_history = new_enum_history;
1185
1186       if (mpz_cmp (max_enum->initializer->value.integer,
1187                    new_enum_history->initializer->value.integer) < 0)
1188         max_enum = new_enum_history;
1189     }
1190 }
1191
1192
1193 /* Function to free enum kind history.  */
1194
1195 void
1196 gfc_free_enum_history (void)
1197 {
1198   enumerator_history *current = enum_history;
1199   enumerator_history *next;
1200
1201   while (current != NULL)
1202     {
1203       next = current->next;
1204       gfc_free (current);
1205       current = next;
1206     }
1207   max_enum = NULL;
1208   enum_history = NULL;
1209 }
1210
1211
1212 /* Function called by variable_decl() that adds an initialization
1213    expression to a symbol.  */
1214
1215 static gfc_try
1216 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1217 {
1218   symbol_attribute attr;
1219   gfc_symbol *sym;
1220   gfc_expr *init;
1221
1222   init = *initp;
1223   if (find_special (name, &sym, false))
1224     return FAILURE;
1225
1226   attr = sym->attr;
1227
1228   /* If this symbol is confirming an implicit parameter type,
1229      then an initialization expression is not allowed.  */
1230   if (attr.flavor == FL_PARAMETER
1231       && sym->value != NULL
1232       && *initp != NULL)
1233     {
1234       gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1235                  sym->name);
1236       return FAILURE;
1237     }
1238
1239   if (init == NULL)
1240     {
1241       /* An initializer is required for PARAMETER declarations.  */
1242       if (attr.flavor == FL_PARAMETER)
1243         {
1244           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1245           return FAILURE;
1246         }
1247     }
1248   else
1249     {
1250       /* If a variable appears in a DATA block, it cannot have an
1251          initializer.  */
1252       if (sym->attr.data)
1253         {
1254           gfc_error ("Variable '%s' at %C with an initializer already "
1255                      "appears in a DATA statement", sym->name);
1256           return FAILURE;
1257         }
1258
1259       /* Check if the assignment can happen. This has to be put off
1260          until later for a derived type variable.  */
1261       if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1262           && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1263           && gfc_check_assign_symbol (sym, init) == FAILURE)
1264         return FAILURE;
1265
1266       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1267             && init->ts.type == BT_CHARACTER)
1268         {
1269           /* Update symbol character length according initializer.  */
1270           if (gfc_check_assign_symbol (sym, init) == FAILURE)
1271             return FAILURE;
1272
1273           if (sym->ts.u.cl->length == NULL)
1274             {
1275               int clen;
1276               /* If there are multiple CHARACTER variables declared on the
1277                  same line, we don't want them to share the same length.  */
1278               sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1279
1280               if (sym->attr.flavor == FL_PARAMETER)
1281                 {
1282                   if (init->expr_type == EXPR_CONSTANT)
1283                     {
1284                       clen = init->value.character.length;
1285                       sym->ts.u.cl->length = gfc_int_expr (clen);
1286                     }
1287                   else if (init->expr_type == EXPR_ARRAY)
1288                     {
1289                       gfc_expr *p = init->value.constructor->expr;
1290                       clen = p->value.character.length;
1291                       sym->ts.u.cl->length = gfc_int_expr (clen);
1292                     }
1293                   else if (init->ts.u.cl && init->ts.u.cl->length)
1294                     sym->ts.u.cl->length =
1295                                 gfc_copy_expr (sym->value->ts.u.cl->length);
1296                 }
1297             }
1298           /* Update initializer character length according symbol.  */
1299           else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1300             {
1301               int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1302               gfc_constructor * p;
1303
1304               if (init->expr_type == EXPR_CONSTANT)
1305                 gfc_set_constant_character_len (len, init, -1);
1306               else if (init->expr_type == EXPR_ARRAY)
1307                 {
1308                   /* Build a new charlen to prevent simplification from
1309                      deleting the length before it is resolved.  */
1310                   init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1311                   init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1312
1313                   for (p = init->value.constructor; p; p = p->next)
1314                     gfc_set_constant_character_len (len, p->expr, -1);
1315                 }
1316             }
1317         }
1318
1319       /* Need to check if the expression we initialized this
1320          to was one of the iso_c_binding named constants.  If so,
1321          and we're a parameter (constant), let it be iso_c.
1322          For example:
1323          integer(c_int), parameter :: my_int = c_int
1324          integer(my_int) :: my_int_2
1325          If we mark my_int as iso_c (since we can see it's value
1326          is equal to one of the named constants), then my_int_2
1327          will be considered C interoperable.  */
1328       if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1329         {
1330           sym->ts.is_iso_c |= init->ts.is_iso_c;
1331           sym->ts.is_c_interop |= init->ts.is_c_interop;
1332           /* attr bits needed for module files.  */
1333           sym->attr.is_iso_c |= init->ts.is_iso_c;
1334           sym->attr.is_c_interop |= init->ts.is_c_interop;
1335           if (init->ts.is_iso_c)
1336             sym->ts.f90_type = init->ts.f90_type;
1337         }
1338       
1339       /* Add initializer.  Make sure we keep the ranks sane.  */
1340       if (sym->attr.dimension && init->rank == 0)
1341         {
1342           mpz_t size;
1343           gfc_expr *array;
1344           gfc_constructor *c;
1345           int n;
1346           if (sym->attr.flavor == FL_PARAMETER
1347                 && init->expr_type == EXPR_CONSTANT
1348                 && spec_size (sym->as, &size) == SUCCESS
1349                 && mpz_cmp_si (size, 0) > 0)
1350             {
1351               array = gfc_start_constructor (init->ts.type, init->ts.kind,
1352                                              &init->where);
1353
1354               array->value.constructor = c = NULL;
1355               for (n = 0; n < (int)mpz_get_si (size); n++)
1356                 {
1357                   if (array->value.constructor == NULL)
1358                     {
1359                       array->value.constructor = c = gfc_get_constructor ();
1360                       c->expr = init;
1361                     }
1362                   else
1363                     {
1364                       c->next = gfc_get_constructor ();
1365                       c = c->next;
1366                       c->expr = gfc_copy_expr (init);
1367                     }
1368                 }
1369
1370               array->shape = gfc_get_shape (sym->as->rank);
1371               for (n = 0; n < sym->as->rank; n++)
1372                 spec_dimen_size (sym->as, n, &array->shape[n]);
1373
1374               init = array;
1375               mpz_clear (size);
1376             }
1377           init->rank = sym->as->rank;
1378         }
1379
1380       sym->value = init;
1381       if (sym->attr.save == SAVE_NONE)
1382         sym->attr.save = SAVE_IMPLICIT;
1383       *initp = NULL;
1384     }
1385
1386   return SUCCESS;
1387 }
1388
1389
1390 /* Function called by variable_decl() that adds a name to a structure
1391    being built.  */
1392
1393 static gfc_try
1394 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1395               gfc_array_spec **as)
1396 {
1397   gfc_component *c;
1398   gfc_try t = SUCCESS;
1399
1400   /* F03:C438/C439. If the current symbol is of the same derived type that we're
1401      constructing, it must have the pointer attribute.  */
1402   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1403       && current_ts.u.derived == gfc_current_block ()
1404       && current_attr.pointer == 0)
1405     {
1406       gfc_error ("Component at %C must have the POINTER attribute");
1407       return FAILURE;
1408     }
1409
1410   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1411     {
1412       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1413         {
1414           gfc_error ("Array component of structure at %C must have explicit "
1415                      "or deferred shape");
1416           return FAILURE;
1417         }
1418     }
1419
1420   if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1421     return FAILURE;
1422
1423   c->ts = current_ts;
1424   if (c->ts.type == BT_CHARACTER)
1425     c->ts.u.cl = cl;
1426   c->attr = current_attr;
1427
1428   c->initializer = *init;
1429   *init = NULL;
1430
1431   c->as = *as;
1432   if (c->as != NULL)
1433     c->attr.dimension = 1;
1434   *as = NULL;
1435
1436   /* Should this ever get more complicated, combine with similar section
1437      in add_init_expr_to_sym into a separate function.  */
1438   if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1439       && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1440     {
1441       int len;
1442
1443       gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1444       gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1445       gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1446
1447       len = mpz_get_si (c->ts.u.cl->length->value.integer);
1448
1449       if (c->initializer->expr_type == EXPR_CONSTANT)
1450         gfc_set_constant_character_len (len, c->initializer, -1);
1451       else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1452                         c->initializer->ts.u.cl->length->value.integer))
1453         {
1454           bool has_ts;
1455           gfc_constructor *ctor = c->initializer->value.constructor;
1456
1457           has_ts = (c->initializer->ts.u.cl
1458                     && c->initializer->ts.u.cl->length_from_typespec);
1459
1460           if (ctor)
1461             {
1462               int first_len;
1463
1464               /* Remember the length of the first element for checking
1465                  that all elements *in the constructor* have the same
1466                  length.  This need not be the length of the LHS!  */
1467               gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1468               gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1469               first_len = ctor->expr->value.character.length;
1470
1471               for (; ctor; ctor = ctor->next)
1472                 {
1473                   if (ctor->expr->expr_type == EXPR_CONSTANT)
1474                     gfc_set_constant_character_len (len, ctor->expr,
1475                                                     has_ts ? -1 : first_len);
1476                 }
1477             }
1478         }
1479     }
1480
1481   /* Check array components.  */
1482   if (!c->attr.dimension)
1483     goto scalar;
1484
1485   if (c->attr.pointer)
1486     {
1487       if (c->as->type != AS_DEFERRED)
1488         {
1489           gfc_error ("Pointer array component of structure at %C must have a "
1490                      "deferred shape");
1491           t = FAILURE;
1492         }
1493     }
1494   else if (c->attr.allocatable)
1495     {
1496       if (c->as->type != AS_DEFERRED)
1497         {
1498           gfc_error ("Allocatable component of structure at %C must have a "
1499                      "deferred shape");
1500           t = FAILURE;
1501         }
1502     }
1503   else
1504     {
1505       if (c->as->type != AS_EXPLICIT)
1506         {
1507           gfc_error ("Array component of structure at %C must have an "
1508                      "explicit shape");
1509           t = FAILURE;
1510         }
1511     }
1512
1513 scalar:
1514   if (c->ts.type == BT_CLASS)
1515     gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1516
1517   return t;
1518 }
1519
1520
1521 /* Match a 'NULL()', and possibly take care of some side effects.  */
1522
1523 match
1524 gfc_match_null (gfc_expr **result)
1525 {
1526   gfc_symbol *sym;
1527   gfc_expr *e;
1528   match m;
1529
1530   m = gfc_match (" null ( )");
1531   if (m != MATCH_YES)
1532     return m;
1533
1534   /* The NULL symbol now has to be/become an intrinsic function.  */
1535   if (gfc_get_symbol ("null", NULL, &sym))
1536     {
1537       gfc_error ("NULL() initialization at %C is ambiguous");
1538       return MATCH_ERROR;
1539     }
1540
1541   gfc_intrinsic_symbol (sym);
1542
1543   if (sym->attr.proc != PROC_INTRINSIC
1544       && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1545                              sym->name, NULL) == FAILURE
1546           || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1547     return MATCH_ERROR;
1548
1549   e = gfc_get_expr ();
1550   e->where = gfc_current_locus;
1551   e->expr_type = EXPR_NULL;
1552   e->ts.type = BT_UNKNOWN;
1553
1554   *result = e;
1555
1556   return MATCH_YES;
1557 }
1558
1559
1560 /* Match a variable name with an optional initializer.  When this
1561    subroutine is called, a variable is expected to be parsed next.
1562    Depending on what is happening at the moment, updates either the
1563    symbol table or the current interface.  */
1564
1565 static match
1566 variable_decl (int elem)
1567 {
1568   char name[GFC_MAX_SYMBOL_LEN + 1];
1569   gfc_expr *initializer, *char_len;
1570   gfc_array_spec *as;
1571   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1572   gfc_charlen *cl;
1573   locus var_locus;
1574   match m;
1575   gfc_try t;
1576   gfc_symbol *sym;
1577
1578   initializer = NULL;
1579   as = NULL;
1580   cp_as = NULL;
1581
1582   /* When we get here, we've just matched a list of attributes and
1583      maybe a type and a double colon.  The next thing we expect to see
1584      is the name of the symbol.  */
1585   m = gfc_match_name (name);
1586   if (m != MATCH_YES)
1587     goto cleanup;
1588
1589   var_locus = gfc_current_locus;
1590
1591   /* Now we could see the optional array spec. or character length.  */
1592   m = gfc_match_array_spec (&as);
1593   if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1594     cp_as = gfc_copy_array_spec (as);
1595   else if (m == MATCH_ERROR)
1596     goto cleanup;
1597
1598   if (m == MATCH_NO)
1599     as = gfc_copy_array_spec (current_as);
1600
1601   char_len = NULL;
1602   cl = NULL;
1603
1604   if (current_ts.type == BT_CHARACTER)
1605     {
1606       switch (match_char_length (&char_len))
1607         {
1608         case MATCH_YES:
1609           cl = gfc_new_charlen (gfc_current_ns, NULL);
1610
1611           cl->length = char_len;
1612           break;
1613
1614         /* Non-constant lengths need to be copied after the first
1615            element.  Also copy assumed lengths.  */
1616         case MATCH_NO:
1617           if (elem > 1
1618               && (current_ts.u.cl->length == NULL
1619                   || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1620             {
1621               cl = gfc_new_charlen (gfc_current_ns, NULL);
1622               cl->length = gfc_copy_expr (current_ts.u.cl->length);
1623             }
1624           else
1625             cl = current_ts.u.cl;
1626
1627           break;
1628
1629         case MATCH_ERROR:
1630           goto cleanup;
1631         }
1632     }
1633
1634   /*  If this symbol has already shown up in a Cray Pointer declaration,
1635       then we want to set the type & bail out.  */
1636   if (gfc_option.flag_cray_pointer)
1637     {
1638       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1639       if (sym != NULL && sym->attr.cray_pointee)
1640         {
1641           sym->ts.type = current_ts.type;
1642           sym->ts.kind = current_ts.kind;
1643           sym->ts.u.cl = cl;
1644           sym->ts.u.derived = current_ts.u.derived;
1645           sym->ts.is_c_interop = current_ts.is_c_interop;
1646           sym->ts.is_iso_c = current_ts.is_iso_c;
1647           m = MATCH_YES;
1648         
1649           /* Check to see if we have an array specification.  */
1650           if (cp_as != NULL)
1651             {
1652               if (sym->as != NULL)
1653                 {
1654                   gfc_error ("Duplicate array spec for Cray pointee at %C");
1655                   gfc_free_array_spec (cp_as);
1656                   m = MATCH_ERROR;
1657                   goto cleanup;
1658                 }
1659               else
1660                 {
1661                   if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1662                     gfc_internal_error ("Couldn't set pointee array spec.");
1663
1664                   /* Fix the array spec.  */
1665                   m = gfc_mod_pointee_as (sym->as);
1666                   if (m == MATCH_ERROR)
1667                     goto cleanup;
1668                 }
1669             }
1670           goto cleanup;
1671         }
1672       else
1673         {
1674           gfc_free_array_spec (cp_as);
1675         }
1676     }
1677
1678   /* Procedure pointer as function result.  */
1679   if (gfc_current_state () == COMP_FUNCTION
1680       && strcmp ("ppr@", gfc_current_block ()->name) == 0
1681       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1682     strcpy (name, "ppr@");
1683
1684   if (gfc_current_state () == COMP_FUNCTION
1685       && strcmp (name, gfc_current_block ()->name) == 0
1686       && gfc_current_block ()->result
1687       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1688     strcpy (name, "ppr@");
1689
1690   /* OK, we've successfully matched the declaration.  Now put the
1691      symbol in the current namespace, because it might be used in the
1692      optional initialization expression for this symbol, e.g. this is
1693      perfectly legal:
1694
1695      integer, parameter :: i = huge(i)
1696
1697      This is only true for parameters or variables of a basic type.
1698      For components of derived types, it is not true, so we don't
1699      create a symbol for those yet.  If we fail to create the symbol,
1700      bail out.  */
1701   if (gfc_current_state () != COMP_DERIVED
1702       && build_sym (name, cl, &as, &var_locus) == FAILURE)
1703     {
1704       m = MATCH_ERROR;
1705       goto cleanup;
1706     }
1707
1708   /* An interface body specifies all of the procedure's
1709      characteristics and these shall be consistent with those
1710      specified in the procedure definition, except that the interface
1711      may specify a procedure that is not pure if the procedure is
1712      defined to be pure(12.3.2).  */
1713   if (current_ts.type == BT_DERIVED
1714       && gfc_current_ns->proc_name
1715       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1716       && current_ts.u.derived->ns != gfc_current_ns)
1717     {
1718       gfc_symtree *st;
1719       st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1720       if (!(current_ts.u.derived->attr.imported
1721                 && st != NULL
1722                 && st->n.sym == current_ts.u.derived)
1723             && !gfc_current_ns->has_import_set)
1724         {
1725             gfc_error ("the type of '%s' at %C has not been declared within the "
1726                        "interface", name);
1727             m = MATCH_ERROR;
1728             goto cleanup;
1729         }
1730     }
1731
1732   /* In functions that have a RESULT variable defined, the function
1733      name always refers to function calls.  Therefore, the name is
1734      not allowed to appear in specification statements.  */
1735   if (gfc_current_state () == COMP_FUNCTION
1736       && gfc_current_block () != NULL
1737       && gfc_current_block ()->result != NULL
1738       && gfc_current_block ()->result != gfc_current_block ()
1739       && strcmp (gfc_current_block ()->name, name) == 0)
1740     {
1741       gfc_error ("Function name '%s' not allowed at %C", name);
1742       m = MATCH_ERROR;
1743       goto cleanup;
1744     }
1745
1746   /* We allow old-style initializations of the form
1747        integer i /2/, j(4) /3*3, 1/
1748      (if no colon has been seen). These are different from data
1749      statements in that initializers are only allowed to apply to the
1750      variable immediately preceding, i.e.
1751        integer i, j /1, 2/
1752      is not allowed. Therefore we have to do some work manually, that
1753      could otherwise be left to the matchers for DATA statements.  */
1754
1755   if (!colon_seen && gfc_match (" /") == MATCH_YES)
1756     {
1757       if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1758                           "initialization at %C") == FAILURE)
1759         return MATCH_ERROR;
1760  
1761       return match_old_style_init (name);
1762     }
1763
1764   /* The double colon must be present in order to have initializers.
1765      Otherwise the statement is ambiguous with an assignment statement.  */
1766   if (colon_seen)
1767     {
1768       if (gfc_match (" =>") == MATCH_YES)
1769         {
1770           if (!current_attr.pointer)
1771             {
1772               gfc_error ("Initialization at %C isn't for a pointer variable");
1773               m = MATCH_ERROR;
1774               goto cleanup;
1775             }
1776
1777           m = gfc_match_null (&initializer);
1778           if (m == MATCH_NO)
1779             {
1780               gfc_error ("Pointer initialization requires a NULL() at %C");
1781               m = MATCH_ERROR;
1782             }
1783
1784           if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1785             {
1786               gfc_error ("Initialization of pointer at %C is not allowed in "
1787                          "a PURE procedure");
1788               m = MATCH_ERROR;
1789             }
1790
1791           if (m != MATCH_YES)
1792             goto cleanup;
1793
1794         }
1795       else if (gfc_match_char ('=') == MATCH_YES)
1796         {
1797           if (current_attr.pointer)
1798             {
1799               gfc_error ("Pointer initialization at %C requires '=>', "
1800                          "not '='");
1801               m = MATCH_ERROR;
1802               goto cleanup;
1803             }
1804
1805           m = gfc_match_init_expr (&initializer);
1806           if (m == MATCH_NO)
1807             {
1808               gfc_error ("Expected an initialization expression at %C");
1809               m = MATCH_ERROR;
1810             }
1811
1812           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
1813               && gfc_state_stack->state != COMP_DERIVED)
1814             {
1815               gfc_error ("Initialization of variable at %C is not allowed in "
1816                          "a PURE procedure");
1817               m = MATCH_ERROR;
1818             }
1819
1820           if (m != MATCH_YES)
1821             goto cleanup;
1822         }
1823     }
1824
1825   if (initializer != NULL && current_attr.allocatable
1826         && gfc_current_state () == COMP_DERIVED)
1827     {
1828       gfc_error ("Initialization of allocatable component at %C is not "
1829                  "allowed");
1830       m = MATCH_ERROR;
1831       goto cleanup;
1832     }
1833
1834   /* Add the initializer.  Note that it is fine if initializer is
1835      NULL here, because we sometimes also need to check if a
1836      declaration *must* have an initialization expression.  */
1837   if (gfc_current_state () != COMP_DERIVED)
1838     t = add_init_expr_to_sym (name, &initializer, &var_locus);
1839   else
1840     {
1841       if (current_ts.type == BT_DERIVED
1842           && !current_attr.pointer && !initializer)
1843         initializer = gfc_default_initializer (&current_ts);
1844       t = build_struct (name, cl, &initializer, &as);
1845     }
1846
1847   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1848
1849 cleanup:
1850   /* Free stuff up and return.  */
1851   gfc_free_expr (initializer);
1852   gfc_free_array_spec (as);
1853
1854   return m;
1855 }
1856
1857
1858 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1859    This assumes that the byte size is equal to the kind number for
1860    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
1861
1862 match
1863 gfc_match_old_kind_spec (gfc_typespec *ts)
1864 {
1865   match m;
1866   int original_kind;
1867
1868   if (gfc_match_char ('*') != MATCH_YES)
1869     return MATCH_NO;
1870
1871   m = gfc_match_small_literal_int (&ts->kind, NULL);
1872   if (m != MATCH_YES)
1873     return MATCH_ERROR;
1874
1875   original_kind = ts->kind;
1876
1877   /* Massage the kind numbers for complex types.  */
1878   if (ts->type == BT_COMPLEX)
1879     {
1880       if (ts->kind % 2)
1881         {
1882           gfc_error ("Old-style type declaration %s*%d not supported at %C",
1883                      gfc_basic_typename (ts->type), original_kind);
1884           return MATCH_ERROR;
1885         }
1886       ts->kind /= 2;
1887     }
1888
1889   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1890     {
1891       gfc_error ("Old-style type declaration %s*%d not supported at %C",
1892                  gfc_basic_typename (ts->type), original_kind);
1893       return MATCH_ERROR;
1894     }
1895
1896   if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1897                       gfc_basic_typename (ts->type), original_kind) == FAILURE)
1898     return MATCH_ERROR;
1899
1900   return MATCH_YES;
1901 }
1902
1903
1904 /* Match a kind specification.  Since kinds are generally optional, we
1905    usually return MATCH_NO if something goes wrong.  If a "kind="
1906    string is found, then we know we have an error.  */
1907
1908 match
1909 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1910 {
1911   locus where, loc;
1912   gfc_expr *e;
1913   match m, n;
1914   char c;
1915   const char *msg;
1916
1917   m = MATCH_NO;
1918   n = MATCH_YES;
1919   e = NULL;
1920
1921   where = loc = gfc_current_locus;
1922
1923   if (kind_expr_only)
1924     goto kind_expr;
1925
1926   if (gfc_match_char ('(') == MATCH_NO)
1927     return MATCH_NO;
1928
1929   /* Also gobbles optional text.  */
1930   if (gfc_match (" kind = ") == MATCH_YES)
1931     m = MATCH_ERROR;
1932
1933   loc = gfc_current_locus;
1934
1935 kind_expr:
1936   n = gfc_match_init_expr (&e);
1937
1938   if (n != MATCH_YES)
1939     {
1940       if (gfc_matching_function)
1941         {
1942           /* The function kind expression might include use associated or 
1943              imported parameters and try again after the specification
1944              expressions.....  */
1945           if (gfc_match_char (')') != MATCH_YES)
1946             {
1947               gfc_error ("Missing right parenthesis at %C");
1948               m = MATCH_ERROR;
1949               goto no_match;
1950             }
1951
1952           gfc_free_expr (e);
1953           gfc_undo_symbols ();
1954           return MATCH_YES;
1955         }
1956       else
1957         {
1958           /* ....or else, the match is real.  */
1959           if (n == MATCH_NO)
1960             gfc_error ("Expected initialization expression at %C");
1961           if (n != MATCH_YES)
1962             return MATCH_ERROR;
1963         }
1964     }
1965
1966   if (e->rank != 0)
1967     {
1968       gfc_error ("Expected scalar initialization expression at %C");
1969       m = MATCH_ERROR;
1970       goto no_match;
1971     }
1972
1973   msg = gfc_extract_int (e, &ts->kind);
1974
1975   if (msg != NULL)
1976     {
1977       gfc_error (msg);
1978       m = MATCH_ERROR;
1979       goto no_match;
1980     }
1981
1982   /* Before throwing away the expression, let's see if we had a
1983      C interoperable kind (and store the fact).  */
1984   if (e->ts.is_c_interop == 1)
1985     {
1986       /* Mark this as c interoperable if being declared with one
1987          of the named constants from iso_c_binding.  */
1988       ts->is_c_interop = e->ts.is_iso_c;
1989       ts->f90_type = e->ts.f90_type;
1990     }
1991   
1992   gfc_free_expr (e);
1993   e = NULL;
1994
1995   /* Ignore errors to this point, if we've gotten here.  This means
1996      we ignore the m=MATCH_ERROR from above.  */
1997   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1998     {
1999       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2000                  gfc_basic_typename (ts->type));
2001       gfc_current_locus = where;
2002       return MATCH_ERROR;
2003     }
2004
2005   /* Warn if, e.g., c_int is used for a REAL variable, but not
2006      if, e.g., c_double is used for COMPLEX as the standard
2007      explicitly says that the kind type parameter for complex and real
2008      variable is the same, i.e. c_float == c_float_complex.  */
2009   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2010       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2011            || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2012     gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2013                      "is %s", gfc_basic_typename (ts->f90_type), &where,
2014                      gfc_basic_typename (ts->type));
2015
2016   gfc_gobble_whitespace ();
2017   if ((c = gfc_next_ascii_char ()) != ')'
2018       && (ts->type != BT_CHARACTER || c != ','))
2019     {
2020       if (ts->type == BT_CHARACTER)
2021         gfc_error ("Missing right parenthesis or comma at %C");
2022       else
2023         gfc_error ("Missing right parenthesis at %C");
2024       m = MATCH_ERROR;
2025     }
2026   else
2027      /* All tests passed.  */
2028      m = MATCH_YES;
2029
2030   if(m == MATCH_ERROR)
2031      gfc_current_locus = where;
2032   
2033   /* Return what we know from the test(s).  */
2034   return m;
2035
2036 no_match:
2037   gfc_free_expr (e);
2038   gfc_current_locus = where;
2039   return m;
2040 }
2041
2042
2043 static match
2044 match_char_kind (int * kind, int * is_iso_c)
2045 {
2046   locus where;
2047   gfc_expr *e;
2048   match m, n;
2049   const char *msg;
2050
2051   m = MATCH_NO;
2052   e = NULL;
2053   where = gfc_current_locus;
2054
2055   n = gfc_match_init_expr (&e);
2056
2057   if (n != MATCH_YES && gfc_matching_function)
2058     {
2059       /* The expression might include use-associated or imported
2060          parameters and try again after the specification 
2061          expressions.  */
2062       gfc_free_expr (e);
2063       gfc_undo_symbols ();
2064       return MATCH_YES;
2065     }
2066
2067   if (n == MATCH_NO)
2068     gfc_error ("Expected initialization expression at %C");
2069   if (n != MATCH_YES)
2070     return MATCH_ERROR;
2071
2072   if (e->rank != 0)
2073     {
2074       gfc_error ("Expected scalar initialization expression at %C");
2075       m = MATCH_ERROR;
2076       goto no_match;
2077     }
2078
2079   msg = gfc_extract_int (e, kind);
2080   *is_iso_c = e->ts.is_iso_c;
2081   if (msg != NULL)
2082     {
2083       gfc_error (msg);
2084       m = MATCH_ERROR;
2085       goto no_match;
2086     }
2087
2088   gfc_free_expr (e);
2089
2090   /* Ignore errors to this point, if we've gotten here.  This means
2091      we ignore the m=MATCH_ERROR from above.  */
2092   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2093     {
2094       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2095       m = MATCH_ERROR;
2096     }
2097   else
2098      /* All tests passed.  */
2099      m = MATCH_YES;
2100
2101   if (m == MATCH_ERROR)
2102      gfc_current_locus = where;
2103   
2104   /* Return what we know from the test(s).  */
2105   return m;
2106
2107 no_match:
2108   gfc_free_expr (e);
2109   gfc_current_locus = where;
2110   return m;
2111 }
2112
2113
2114 /* Match the various kind/length specifications in a CHARACTER
2115    declaration.  We don't return MATCH_NO.  */
2116
2117 match
2118 gfc_match_char_spec (gfc_typespec *ts)
2119 {
2120   int kind, seen_length, is_iso_c;
2121   gfc_charlen *cl;
2122   gfc_expr *len;
2123   match m;
2124
2125   len = NULL;
2126   seen_length = 0;
2127   kind = 0;
2128   is_iso_c = 0;
2129
2130   /* Try the old-style specification first.  */
2131   old_char_selector = 0;
2132
2133   m = match_char_length (&len);
2134   if (m != MATCH_NO)
2135     {
2136       if (m == MATCH_YES)
2137         old_char_selector = 1;
2138       seen_length = 1;
2139       goto done;
2140     }
2141
2142   m = gfc_match_char ('(');
2143   if (m != MATCH_YES)
2144     {
2145       m = MATCH_YES;    /* Character without length is a single char.  */
2146       goto done;
2147     }
2148
2149   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
2150   if (gfc_match (" kind =") == MATCH_YES)
2151     {
2152       m = match_char_kind (&kind, &is_iso_c);
2153        
2154       if (m == MATCH_ERROR)
2155         goto done;
2156       if (m == MATCH_NO)
2157         goto syntax;
2158
2159       if (gfc_match (" , len =") == MATCH_NO)
2160         goto rparen;
2161
2162       m = char_len_param_value (&len);
2163       if (m == MATCH_NO)
2164         goto syntax;
2165       if (m == MATCH_ERROR)
2166         goto done;
2167       seen_length = 1;
2168
2169       goto rparen;
2170     }
2171
2172   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
2173   if (gfc_match (" len =") == MATCH_YES)
2174     {
2175       m = char_len_param_value (&len);
2176       if (m == MATCH_NO)
2177         goto syntax;
2178       if (m == MATCH_ERROR)
2179         goto done;
2180       seen_length = 1;
2181
2182       if (gfc_match_char (')') == MATCH_YES)
2183         goto done;
2184
2185       if (gfc_match (" , kind =") != MATCH_YES)
2186         goto syntax;
2187
2188       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2189         goto done;
2190
2191       goto rparen;
2192     }
2193
2194   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
2195   m = char_len_param_value (&len);
2196   if (m == MATCH_NO)
2197     goto syntax;
2198   if (m == MATCH_ERROR)
2199     goto done;
2200   seen_length = 1;
2201
2202   m = gfc_match_char (')');
2203   if (m == MATCH_YES)
2204     goto done;
2205
2206   if (gfc_match_char (',') != MATCH_YES)
2207     goto syntax;
2208
2209   gfc_match (" kind =");        /* Gobble optional text.  */
2210
2211   m = match_char_kind (&kind, &is_iso_c);
2212   if (m == MATCH_ERROR)
2213     goto done;
2214   if (m == MATCH_NO)
2215     goto syntax;
2216
2217 rparen:
2218   /* Require a right-paren at this point.  */
2219   m = gfc_match_char (')');
2220   if (m == MATCH_YES)
2221     goto done;
2222
2223 syntax:
2224   gfc_error ("Syntax error in CHARACTER declaration at %C");
2225   m = MATCH_ERROR;
2226   gfc_free_expr (len);
2227   return m;
2228
2229 done:
2230   /* Deal with character functions after USE and IMPORT statements.  */
2231   if (gfc_matching_function)
2232     {
2233       gfc_free_expr (len);
2234       gfc_undo_symbols ();
2235       return MATCH_YES;
2236     }
2237
2238   if (m != MATCH_YES)
2239     {
2240       gfc_free_expr (len);
2241       return m;
2242     }
2243
2244   /* Do some final massaging of the length values.  */
2245   cl = gfc_new_charlen (gfc_current_ns, NULL);
2246
2247   if (seen_length == 0)
2248     cl->length = gfc_int_expr (1);
2249   else
2250     cl->length = len;
2251
2252   ts->u.cl = cl;
2253   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2254
2255   /* We have to know if it was a c interoperable kind so we can
2256      do accurate type checking of bind(c) procs, etc.  */
2257   if (kind != 0)
2258     /* Mark this as c interoperable if being declared with one
2259        of the named constants from iso_c_binding.  */
2260     ts->is_c_interop = is_iso_c;
2261   else if (len != NULL)
2262     /* Here, we might have parsed something such as: character(c_char)
2263        In this case, the parsing code above grabs the c_char when
2264        looking for the length (line 1690, roughly).  it's the last
2265        testcase for parsing the kind params of a character variable.
2266        However, it's not actually the length.    this seems like it
2267        could be an error.  
2268        To see if the user used a C interop kind, test the expr
2269        of the so called length, and see if it's C interoperable.  */
2270     ts->is_c_interop = len->ts.is_iso_c;
2271   
2272   return MATCH_YES;
2273 }
2274
2275
2276 /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
2277    structure to the matched specification.  This is necessary for FUNCTION and
2278    IMPLICIT statements.
2279
2280    If implicit_flag is nonzero, then we don't check for the optional
2281    kind specification.  Not doing so is needed for matching an IMPLICIT
2282    statement correctly.  */
2283
2284 match
2285 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2286 {
2287   char name[GFC_MAX_SYMBOL_LEN + 1];
2288   gfc_symbol *sym;
2289   match m;
2290   char c;
2291   bool seen_deferred_kind;
2292
2293   /* A belt and braces check that the typespec is correctly being treated
2294      as a deferred characteristic association.  */
2295   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2296                           && (gfc_current_block ()->result->ts.kind == -1)
2297                           && (ts->kind == -1);
2298   gfc_clear_ts (ts);
2299   if (seen_deferred_kind)
2300     ts->kind = -1;
2301
2302   /* Clear the current binding label, in case one is given.  */
2303   curr_binding_label[0] = '\0';
2304
2305   if (gfc_match (" byte") == MATCH_YES)
2306     {
2307       if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2308           == FAILURE)
2309         return MATCH_ERROR;
2310
2311       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2312         {
2313           gfc_error ("BYTE type used at %C "
2314                      "is not available on the target machine");
2315           return MATCH_ERROR;
2316         }
2317
2318       ts->type = BT_INTEGER;
2319       ts->kind = 1;
2320       return MATCH_YES;
2321     }
2322
2323   if (gfc_match (" integer") == MATCH_YES)
2324     {
2325       ts->type = BT_INTEGER;
2326       ts->kind = gfc_default_integer_kind;
2327       goto get_kind;
2328     }
2329
2330   if (gfc_match (" character") == MATCH_YES)
2331     {
2332       ts->type = BT_CHARACTER;
2333       if (implicit_flag == 0)
2334         return gfc_match_char_spec (ts);
2335       else
2336         return MATCH_YES;
2337     }
2338
2339   if (gfc_match (" real") == MATCH_YES)
2340     {
2341       ts->type = BT_REAL;
2342       ts->kind = gfc_default_real_kind;
2343       goto get_kind;
2344     }
2345
2346   if (gfc_match (" double precision") == MATCH_YES)
2347     {
2348       ts->type = BT_REAL;
2349       ts->kind = gfc_default_double_kind;
2350       return MATCH_YES;
2351     }
2352
2353   if (gfc_match (" complex") == MATCH_YES)
2354     {
2355       ts->type = BT_COMPLEX;
2356       ts->kind = gfc_default_complex_kind;
2357       goto get_kind;
2358     }
2359
2360   if (gfc_match (" double complex") == MATCH_YES)
2361     {
2362       if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2363                           "conform to the Fortran 95 standard") == FAILURE)
2364         return MATCH_ERROR;
2365
2366       ts->type = BT_COMPLEX;
2367       ts->kind = gfc_default_double_kind;
2368       return MATCH_YES;
2369     }
2370
2371   if (gfc_match (" logical") == MATCH_YES)
2372     {
2373       ts->type = BT_LOGICAL;
2374       ts->kind = gfc_default_logical_kind;
2375       goto get_kind;
2376     }
2377
2378   m = gfc_match (" type ( %n )", name);
2379   if (m == MATCH_YES)
2380     ts->type = BT_DERIVED;
2381   else
2382     {
2383       m = gfc_match (" class ( %n )", name);
2384       if (m != MATCH_YES)
2385         return m;
2386       ts->type = BT_CLASS;
2387
2388       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2389                           == FAILURE)
2390         return MATCH_ERROR;
2391     }
2392
2393   /* Defer association of the derived type until the end of the
2394      specification block.  However, if the derived type can be
2395      found, add it to the typespec.  */  
2396   if (gfc_matching_function)
2397     {
2398       ts->u.derived = NULL;
2399       if (gfc_current_state () != COMP_INTERFACE
2400             && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2401         ts->u.derived = sym;
2402       return MATCH_YES;
2403     }
2404
2405   /* Search for the name but allow the components to be defined later.  If
2406      type = -1, this typespec has been seen in a function declaration but
2407      the type could not be accessed at that point.  */
2408   sym = NULL;
2409   if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2410     {
2411       gfc_error ("Type name '%s' at %C is ambiguous", name);
2412       return MATCH_ERROR;
2413     }
2414   else if (ts->kind == -1)
2415     {
2416       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2417                     || gfc_current_ns->has_import_set;
2418       if (gfc_find_symbol (name, NULL, iface, &sym))
2419         {       
2420           gfc_error ("Type name '%s' at %C is ambiguous", name);
2421           return MATCH_ERROR;
2422         }
2423
2424       ts->kind = 0;
2425       if (sym == NULL)
2426         return MATCH_NO;
2427     }
2428
2429   if (sym->attr.flavor != FL_DERIVED
2430       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2431     return MATCH_ERROR;
2432
2433   gfc_set_sym_referenced (sym);
2434   ts->u.derived = sym;
2435
2436   return MATCH_YES;
2437
2438 get_kind:
2439   /* For all types except double, derived and character, look for an
2440      optional kind specifier.  MATCH_NO is actually OK at this point.  */
2441   if (implicit_flag == 1)
2442     return MATCH_YES;
2443
2444   if (gfc_current_form == FORM_FREE)
2445     {
2446       c = gfc_peek_ascii_char ();
2447       if (!gfc_is_whitespace (c) && c != '*' && c != '('
2448           && c != ':' && c != ',')
2449        return MATCH_NO;
2450     }
2451
2452   m = gfc_match_kind_spec (ts, false);
2453   if (m == MATCH_NO && ts->type != BT_CHARACTER)
2454     m = gfc_match_old_kind_spec (ts);
2455
2456   /* Defer association of the KIND expression of function results
2457      until after USE and IMPORT statements.  */
2458   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2459          || gfc_matching_function)
2460     return MATCH_YES;
2461
2462   if (m == MATCH_NO)
2463     m = MATCH_YES;              /* No kind specifier found.  */
2464
2465   return m;
2466 }
2467
2468
2469 /* Match an IMPLICIT NONE statement.  Actually, this statement is
2470    already matched in parse.c, or we would not end up here in the
2471    first place.  So the only thing we need to check, is if there is
2472    trailing garbage.  If not, the match is successful.  */
2473
2474 match
2475 gfc_match_implicit_none (void)
2476 {
2477   return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2478 }
2479
2480
2481 /* Match the letter range(s) of an IMPLICIT statement.  */
2482
2483 static match
2484 match_implicit_range (void)
2485 {
2486   char c, c1, c2;
2487   int inner;
2488   locus cur_loc;
2489
2490   cur_loc = gfc_current_locus;
2491
2492   gfc_gobble_whitespace ();
2493   c = gfc_next_ascii_char ();
2494   if (c != '(')
2495     {
2496       gfc_error ("Missing character range in IMPLICIT at %C");
2497       goto bad;
2498     }
2499
2500   inner = 1;
2501   while (inner)
2502     {
2503       gfc_gobble_whitespace ();
2504       c1 = gfc_next_ascii_char ();
2505       if (!ISALPHA (c1))
2506         goto bad;
2507
2508       gfc_gobble_whitespace ();
2509       c = gfc_next_ascii_char ();
2510
2511       switch (c)
2512         {
2513         case ')':
2514           inner = 0;            /* Fall through.  */
2515
2516         case ',':
2517           c2 = c1;
2518           break;
2519
2520         case '-':
2521           gfc_gobble_whitespace ();
2522           c2 = gfc_next_ascii_char ();
2523           if (!ISALPHA (c2))
2524             goto bad;
2525
2526           gfc_gobble_whitespace ();
2527           c = gfc_next_ascii_char ();
2528
2529           if ((c != ',') && (c != ')'))
2530             goto bad;
2531           if (c == ')')
2532             inner = 0;
2533
2534           break;
2535
2536         default:
2537           goto bad;
2538         }
2539
2540       if (c1 > c2)
2541         {
2542           gfc_error ("Letters must be in alphabetic order in "
2543                      "IMPLICIT statement at %C");
2544           goto bad;
2545         }
2546
2547       /* See if we can add the newly matched range to the pending
2548          implicits from this IMPLICIT statement.  We do not check for
2549          conflicts with whatever earlier IMPLICIT statements may have
2550          set.  This is done when we've successfully finished matching
2551          the current one.  */
2552       if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2553         goto bad;
2554     }
2555
2556   return MATCH_YES;
2557
2558 bad:
2559   gfc_syntax_error (ST_IMPLICIT);
2560
2561   gfc_current_locus = cur_loc;
2562   return MATCH_ERROR;
2563 }
2564
2565
2566 /* Match an IMPLICIT statement, storing the types for
2567    gfc_set_implicit() if the statement is accepted by the parser.
2568    There is a strange looking, but legal syntactic construction
2569    possible.  It looks like:
2570
2571      IMPLICIT INTEGER (a-b) (c-d)
2572
2573    This is legal if "a-b" is a constant expression that happens to
2574    equal one of the legal kinds for integers.  The real problem
2575    happens with an implicit specification that looks like:
2576
2577      IMPLICIT INTEGER (a-b)
2578
2579    In this case, a typespec matcher that is "greedy" (as most of the
2580    matchers are) gobbles the character range as a kindspec, leaving
2581    nothing left.  We therefore have to go a bit more slowly in the
2582    matching process by inhibiting the kindspec checking during
2583    typespec matching and checking for a kind later.  */
2584
2585 match
2586 gfc_match_implicit (void)
2587 {
2588   gfc_typespec ts;
2589   locus cur_loc;
2590   char c;
2591   match m;
2592
2593   gfc_clear_ts (&ts);
2594
2595   /* We don't allow empty implicit statements.  */
2596   if (gfc_match_eos () == MATCH_YES)
2597     {
2598       gfc_error ("Empty IMPLICIT statement at %C");
2599       return MATCH_ERROR;
2600     }
2601
2602   do
2603     {
2604       /* First cleanup.  */
2605       gfc_clear_new_implicit ();
2606
2607       /* A basic type is mandatory here.  */
2608       m = gfc_match_decl_type_spec (&ts, 1);
2609       if (m == MATCH_ERROR)
2610         goto error;
2611       if (m == MATCH_NO)
2612         goto syntax;
2613
2614       cur_loc = gfc_current_locus;
2615       m = match_implicit_range ();
2616
2617       if (m == MATCH_YES)
2618         {
2619           /* We may have <TYPE> (<RANGE>).  */
2620           gfc_gobble_whitespace ();
2621           c = gfc_next_ascii_char ();
2622           if ((c == '\n') || (c == ','))
2623             {
2624               /* Check for CHARACTER with no length parameter.  */
2625               if (ts.type == BT_CHARACTER && !ts.u.cl)
2626                 {
2627                   ts.kind = gfc_default_character_kind;
2628                   ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2629                   ts.u.cl->length = gfc_int_expr (1);
2630                 }
2631
2632               /* Record the Successful match.  */
2633               if (gfc_merge_new_implicit (&ts) != SUCCESS)
2634                 return MATCH_ERROR;
2635               continue;
2636             }
2637
2638           gfc_current_locus = cur_loc;
2639         }
2640
2641       /* Discard the (incorrectly) matched range.  */
2642       gfc_clear_new_implicit ();
2643
2644       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
2645       if (ts.type == BT_CHARACTER)
2646         m = gfc_match_char_spec (&ts);
2647       else
2648         {
2649           m = gfc_match_kind_spec (&ts, false);
2650           if (m == MATCH_NO)
2651             {
2652               m = gfc_match_old_kind_spec (&ts);
2653               if (m == MATCH_ERROR)
2654                 goto error;
2655               if (m == MATCH_NO)
2656                 goto syntax;
2657             }
2658         }
2659       if (m == MATCH_ERROR)
2660         goto error;
2661
2662       m = match_implicit_range ();
2663       if (m == MATCH_ERROR)
2664         goto error;
2665       if (m == MATCH_NO)
2666         goto syntax;
2667
2668       gfc_gobble_whitespace ();
2669       c = gfc_next_ascii_char ();
2670       if ((c != '\n') && (c != ','))
2671         goto syntax;
2672
2673       if (gfc_merge_new_implicit (&ts) != SUCCESS)
2674         return MATCH_ERROR;
2675     }
2676   while (c == ',');
2677
2678   return MATCH_YES;
2679
2680 syntax:
2681   gfc_syntax_error (ST_IMPLICIT);
2682
2683 error:
2684   return MATCH_ERROR;
2685 }
2686
2687
2688 match
2689 gfc_match_import (void)
2690 {
2691   char name[GFC_MAX_SYMBOL_LEN + 1];
2692   match m;
2693   gfc_symbol *sym;
2694   gfc_symtree *st;
2695
2696   if (gfc_current_ns->proc_name == NULL
2697       || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2698     {
2699       gfc_error ("IMPORT statement at %C only permitted in "
2700                  "an INTERFACE body");
2701       return MATCH_ERROR;
2702     }
2703
2704   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2705       == FAILURE)
2706     return MATCH_ERROR;
2707
2708   if (gfc_match_eos () == MATCH_YES)
2709     {
2710       /* All host variables should be imported.  */
2711       gfc_current_ns->has_import_set = 1;
2712       return MATCH_YES;
2713     }
2714
2715   if (gfc_match (" ::") == MATCH_YES)
2716     {
2717       if (gfc_match_eos () == MATCH_YES)
2718         {
2719            gfc_error ("Expecting list of named entities at %C");
2720            return MATCH_ERROR;
2721         }
2722     }
2723
2724   for(;;)
2725     {
2726       m = gfc_match (" %n", name);
2727       switch (m)
2728         {
2729         case MATCH_YES:
2730           if (gfc_current_ns->parent !=  NULL
2731               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2732             {
2733                gfc_error ("Type name '%s' at %C is ambiguous", name);
2734                return MATCH_ERROR;
2735             }
2736           else if (gfc_current_ns->proc_name->ns->parent !=  NULL
2737                    && gfc_find_symbol (name,
2738                                        gfc_current_ns->proc_name->ns->parent,
2739                                        1, &sym))
2740             {
2741                gfc_error ("Type name '%s' at %C is ambiguous", name);
2742                return MATCH_ERROR;
2743             }
2744
2745           if (sym == NULL)
2746             {
2747               gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2748                          "at %C - does not exist.", name);
2749               return MATCH_ERROR;
2750             }
2751
2752           if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2753             {
2754               gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2755                            "at %C.", name);
2756               goto next_item;
2757             }
2758
2759           st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2760           st->n.sym = sym;
2761           sym->refs++;
2762           sym->attr.imported = 1;
2763
2764           goto next_item;
2765
2766         case MATCH_NO:
2767           break;
2768
2769         case MATCH_ERROR:
2770           return MATCH_ERROR;
2771         }
2772
2773     next_item:
2774       if (gfc_match_eos () == MATCH_YES)
2775         break;
2776       if (gfc_match_char (',') != MATCH_YES)
2777         goto syntax;
2778     }
2779
2780   return MATCH_YES;
2781
2782 syntax:
2783   gfc_error ("Syntax error in IMPORT statement at %C");
2784   return MATCH_ERROR;
2785 }
2786
2787
2788 /* A minimal implementation of gfc_match without whitespace, escape
2789    characters or variable arguments.  Returns true if the next
2790    characters match the TARGET template exactly.  */
2791
2792 static bool
2793 match_string_p (const char *target)
2794 {
2795   const char *p;
2796
2797   for (p = target; *p; p++)
2798     if ((char) gfc_next_ascii_char () != *p)
2799       return false;
2800   return true;
2801 }
2802
2803 /* Matches an attribute specification including array specs.  If
2804    successful, leaves the variables current_attr and current_as
2805    holding the specification.  Also sets the colon_seen variable for
2806    later use by matchers associated with initializations.
2807
2808    This subroutine is a little tricky in the sense that we don't know
2809    if we really have an attr-spec until we hit the double colon.
2810    Until that time, we can only return MATCH_NO.  This forces us to
2811    check for duplicate specification at this level.  */
2812
2813 static match
2814 match_attr_spec (void)
2815 {
2816   /* Modifiers that can exist in a type statement.  */
2817   typedef enum
2818   { GFC_DECL_BEGIN = 0,
2819     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2820     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2821     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2822     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2823     DECL_IS_BIND_C, DECL_ASYNCHRONOUS, DECL_NONE,
2824     GFC_DECL_END /* Sentinel */
2825   }
2826   decl_types;
2827
2828 /* GFC_DECL_END is the sentinel, index starts at 0.  */
2829 #define NUM_DECL GFC_DECL_END
2830
2831   locus start, seen_at[NUM_DECL];
2832   int seen[NUM_DECL];
2833   unsigned int d;
2834   const char *attr;
2835   match m;
2836   gfc_try t;
2837
2838   gfc_clear_attr (&current_attr);
2839   start = gfc_current_locus;
2840
2841   current_as = NULL;
2842   colon_seen = 0;
2843
2844   /* See if we get all of the keywords up to the final double colon.  */
2845   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2846     seen[d] = 0;
2847
2848   for (;;)
2849     {
2850       char ch;
2851
2852       d = DECL_NONE;
2853       gfc_gobble_whitespace ();
2854
2855       ch = gfc_next_ascii_char ();
2856       if (ch == ':')
2857         {
2858           /* This is the successful exit condition for the loop.  */
2859           if (gfc_next_ascii_char () == ':')
2860             break;
2861         }
2862       else if (ch == ',')
2863         {
2864           gfc_gobble_whitespace ();
2865           switch (gfc_peek_ascii_char ())
2866             {
2867             case 'a':
2868               gfc_next_ascii_char ();
2869               switch (gfc_next_ascii_char ())
2870                 {
2871                 case 'l':
2872                   if (match_string_p ("locatable"))
2873                     {
2874                       /* Matched "allocatable".  */
2875                       d = DECL_ALLOCATABLE;
2876                     }
2877                   break;
2878
2879                 case 's':
2880                   if (match_string_p ("ynchronous"))
2881                     {
2882                       /* Matched "asynchronous".  */
2883                       d = DECL_ASYNCHRONOUS;
2884                     }
2885                   break;
2886                 }
2887
2888             case 'b':
2889               /* Try and match the bind(c).  */
2890               m = gfc_match_bind_c (NULL, true);
2891               if (m == MATCH_YES)
2892                 d = DECL_IS_BIND_C;
2893               else if (m == MATCH_ERROR)
2894                 goto cleanup;
2895               break;
2896
2897             case 'd':
2898               if (match_string_p ("dimension"))
2899                 d = DECL_DIMENSION;
2900               break;
2901
2902             case 'e':
2903               if (match_string_p ("external"))
2904                 d = DECL_EXTERNAL;
2905               break;
2906
2907             case 'i':
2908               if (match_string_p ("int"))
2909                 {
2910                   ch = gfc_next_ascii_char ();
2911                   if (ch == 'e')
2912                     {
2913                       if (match_string_p ("nt"))
2914                         {
2915                           /* Matched "intent".  */
2916                           /* TODO: Call match_intent_spec from here.  */
2917                           if (gfc_match (" ( in out )") == MATCH_YES)
2918                             d = DECL_INOUT;
2919                           else if (gfc_match (" ( in )") == MATCH_YES)
2920                             d = DECL_IN;
2921                           else if (gfc_match (" ( out )") == MATCH_YES)
2922                             d = DECL_OUT;
2923                         }
2924                     }
2925                   else if (ch == 'r')
2926                     {
2927                       if (match_string_p ("insic"))
2928                         {
2929                           /* Matched "intrinsic".  */
2930                           d = DECL_INTRINSIC;
2931                         }
2932                     }
2933                 }
2934               break;
2935
2936             case 'o':
2937               if (match_string_p ("optional"))
2938                 d = DECL_OPTIONAL;
2939               break;
2940
2941             case 'p':
2942               gfc_next_ascii_char ();
2943               switch (gfc_next_ascii_char ())
2944                 {
2945                 case 'a':
2946                   if (match_string_p ("rameter"))
2947                     {
2948                       /* Matched "parameter".  */
2949                       d = DECL_PARAMETER;
2950                     }
2951                   break;
2952
2953                 case 'o':
2954                   if (match_string_p ("inter"))
2955                     {
2956                       /* Matched "pointer".  */
2957                       d = DECL_POINTER;
2958                     }
2959                   break;
2960
2961                 case 'r':
2962                   ch = gfc_next_ascii_char ();
2963                   if (ch == 'i')
2964                     {
2965                       if (match_string_p ("vate"))
2966                         {
2967                           /* Matched "private".  */
2968                           d = DECL_PRIVATE;
2969                         }
2970                     }
2971                   else if (ch == 'o')
2972                     {
2973                       if (match_string_p ("tected"))
2974                         {
2975                           /* Matched "protected".  */
2976                           d = DECL_PROTECTED;
2977                         }
2978                     }
2979                   break;
2980
2981                 case 'u':
2982                   if (match_string_p ("blic"))
2983                     {
2984                       /* Matched "public".  */
2985                       d = DECL_PUBLIC;
2986                     }
2987                   break;
2988                 }
2989               break;
2990
2991             case 's':
2992               if (match_string_p ("save"))
2993                 d = DECL_SAVE;
2994               break;
2995
2996             case 't':
2997               if (match_string_p ("target"))
2998                 d = DECL_TARGET;
2999               break;
3000
3001             case 'v':
3002               gfc_next_ascii_char ();
3003               ch = gfc_next_ascii_char ();
3004               if (ch == 'a')
3005                 {
3006                   if (match_string_p ("lue"))
3007                     {
3008                       /* Matched "value".  */
3009                       d = DECL_VALUE;
3010                     }
3011                 }
3012               else if (ch == 'o')
3013                 {
3014                   if (match_string_p ("latile"))
3015                     {
3016                       /* Matched "volatile".  */
3017                       d = DECL_VOLATILE;
3018                     }
3019                 }
3020               break;
3021             }
3022         }
3023
3024       /* No double colon and no recognizable decl_type, so assume that
3025          we've been looking at something else the whole time.  */
3026       if (d == DECL_NONE)
3027         {
3028           m = MATCH_NO;
3029           goto cleanup;
3030         }
3031
3032       /* Check to make sure any parens are paired up correctly.  */
3033       if (gfc_match_parens () == MATCH_ERROR)
3034         {
3035           m = MATCH_ERROR;
3036           goto cleanup;
3037         }
3038
3039       seen[d]++;
3040       seen_at[d] = gfc_current_locus;
3041
3042       if (d == DECL_DIMENSION)
3043         {
3044           m = gfc_match_array_spec (&current_as);
3045
3046           if (m == MATCH_NO)
3047             {
3048               gfc_error ("Missing dimension specification at %C");
3049               m = MATCH_ERROR;
3050             }
3051
3052           if (m == MATCH_ERROR)
3053             goto cleanup;
3054         }
3055     }
3056
3057   /* Since we've seen a double colon, we have to be looking at an
3058      attr-spec.  This means that we can now issue errors.  */
3059   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3060     if (seen[d] > 1)
3061       {
3062         switch (d)
3063           {
3064           case DECL_ALLOCATABLE:
3065             attr = "ALLOCATABLE";
3066             break;
3067           case DECL_ASYNCHRONOUS:
3068             attr = "ASYNCHRONOUS";
3069             break;
3070           case DECL_DIMENSION:
3071             attr = "DIMENSION";
3072             break;
3073           case DECL_EXTERNAL:
3074             attr = "EXTERNAL";
3075             break;
3076           case DECL_IN:
3077             attr = "INTENT (IN)";
3078             break;
3079           case DECL_OUT:
3080             attr = "INTENT (OUT)";
3081             break;
3082           case DECL_INOUT:
3083             attr = "INTENT (IN OUT)";
3084             break;
3085           case DECL_INTRINSIC:
3086             attr = "INTRINSIC";
3087             break;
3088           case DECL_OPTIONAL:
3089             attr = "OPTIONAL";
3090             break;
3091           case DECL_PARAMETER:
3092             attr = "PARAMETER";
3093             break;
3094           case DECL_POINTER:
3095             attr = "POINTER";
3096             break;
3097           case DECL_PROTECTED:
3098             attr = "PROTECTED";
3099             break;
3100           case DECL_PRIVATE:
3101             attr = "PRIVATE";
3102             break;
3103           case DECL_PUBLIC:
3104             attr = "PUBLIC";
3105             break;
3106           case DECL_SAVE:
3107             attr = "SAVE";
3108             break;
3109           case DECL_TARGET:
3110             attr = "TARGET";
3111             break;
3112           case DECL_IS_BIND_C:
3113             attr = "IS_BIND_C";
3114             break;
3115           case DECL_VALUE:
3116             attr = "VALUE";
3117             break;
3118           case DECL_VOLATILE:
3119             attr = "VOLATILE";
3120             break;
3121           default:
3122             attr = NULL;        /* This shouldn't happen.  */
3123           }
3124
3125         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3126         m = MATCH_ERROR;
3127         goto cleanup;
3128       }
3129
3130   /* Now that we've dealt with duplicate attributes, add the attributes
3131      to the current attribute.  */
3132   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3133     {
3134       if (seen[d] == 0)
3135         continue;
3136
3137       if (gfc_current_state () == COMP_DERIVED
3138           && d != DECL_DIMENSION && d != DECL_POINTER
3139           && d != DECL_PRIVATE   && d != DECL_PUBLIC
3140           && d != DECL_NONE)
3141         {
3142           if (d == DECL_ALLOCATABLE)
3143             {
3144               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3145                                   "attribute at %C in a TYPE definition")
3146                   == FAILURE)
3147                 {
3148                   m = MATCH_ERROR;
3149                   goto cleanup;
3150                 }
3151             }
3152           else
3153             {
3154               gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3155                          &seen_at[d]);
3156               m = MATCH_ERROR;
3157               goto cleanup;
3158             }
3159         }
3160
3161       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3162           && gfc_current_state () != COMP_MODULE)
3163         {
3164           if (d == DECL_PRIVATE)
3165             attr = "PRIVATE";
3166           else
3167             attr = "PUBLIC";
3168           if (gfc_current_state () == COMP_DERIVED
3169               && gfc_state_stack->previous
3170               && gfc_state_stack->previous->state == COMP_MODULE)
3171             {
3172               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3173                                   "at %L in a TYPE definition", attr,
3174                                   &seen_at[d])
3175                   == FAILURE)
3176                 {
3177                   m = MATCH_ERROR;
3178                   goto cleanup;
3179                 }
3180             }
3181           else
3182             {
3183               gfc_error ("%s attribute at %L is not allowed outside of the "
3184                          "specification part of a module", attr, &seen_at[d]);
3185               m = MATCH_ERROR;
3186               goto cleanup;
3187             }
3188         }
3189
3190       switch (d)
3191         {
3192         case DECL_ALLOCATABLE:
3193           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3194           break;
3195
3196         case DECL_ASYNCHRONOUS:
3197           if (gfc_notify_std (GFC_STD_F2003,
3198                               "Fortran 2003: ASYNCHRONOUS attribute at %C")
3199               == FAILURE)
3200             t = FAILURE;
3201           else
3202             t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3203           break;
3204
3205         case DECL_DIMENSION:
3206           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3207           break;
3208
3209         case DECL_EXTERNAL:
3210           t = gfc_add_external (&current_attr, &seen_at[d]);
3211           break;
3212
3213         case DECL_IN:
3214           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3215           break;
3216
3217         case DECL_OUT:
3218           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3219           break;
3220
3221         case DECL_INOUT:
3222           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3223           break;
3224
3225         case DECL_INTRINSIC:
3226           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3227           break;
3228
3229         case DECL_OPTIONAL:
3230           t = gfc_add_optional (&current_attr, &seen_at[d]);
3231           break;
3232
3233         case DECL_PARAMETER:
3234           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3235           break;
3236
3237         case DECL_POINTER:
3238           t = gfc_add_pointer (&current_attr, &seen_at[d]);
3239           break;
3240
3241         case DECL_PROTECTED:
3242           if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3243             {
3244                gfc_error ("PROTECTED at %C only allowed in specification "
3245                           "part of a module");
3246                t = FAILURE;
3247                break;
3248             }
3249
3250           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3251                               "attribute at %C")
3252               == FAILURE)
3253             t = FAILURE;
3254           else
3255             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3256           break;
3257
3258         case DECL_PRIVATE:
3259           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3260                               &seen_at[d]);
3261           break;
3262
3263         case DECL_PUBLIC:
3264           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3265                               &seen_at[d]);
3266           break;
3267
3268         case DECL_SAVE:
3269           t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
3270           break;
3271
3272         case DECL_TARGET:
3273           t = gfc_add_target (&current_attr, &seen_at[d]);
3274           break;
3275
3276         case DECL_IS_BIND_C:
3277            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3278            break;
3279            
3280         case DECL_VALUE:
3281           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3282                               "at %C")
3283               == FAILURE)
3284             t = FAILURE;
3285           else
3286             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3287           break;
3288
3289         case DECL_VOLATILE:
3290           if (gfc_notify_std (GFC_STD_F2003,
3291                               "Fortran 2003: VOLATILE attribute at %C")
3292               == FAILURE)
3293             t = FAILURE;
3294           else
3295             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3296           break;
3297
3298         default:
3299           gfc_internal_error ("match_attr_spec(): Bad attribute");
3300         }
3301
3302       if (t == FAILURE)
3303         {
3304           m = MATCH_ERROR;
3305           goto cleanup;
3306         }
3307     }
3308
3309   colon_seen = 1;
3310   return MATCH_YES;
3311
3312 cleanup:
3313   gfc_current_locus = start;
3314   gfc_free_array_spec (current_as);
3315   current_as = NULL;
3316   return m;
3317 }
3318
3319
3320 /* Set the binding label, dest_label, either with the binding label
3321    stored in the given gfc_typespec, ts, or if none was provided, it
3322    will be the symbol name in all lower case, as required by the draft
3323    (J3/04-007, section 15.4.1).  If a binding label was given and
3324    there is more than one argument (num_idents), it is an error.  */
3325
3326 gfc_try
3327 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3328 {
3329   if (num_idents > 1 && has_name_equals)
3330     {
3331       gfc_error ("Multiple identifiers provided with "
3332                  "single NAME= specifier at %C");
3333       return FAILURE;
3334     }
3335
3336   if (curr_binding_label[0] != '\0')
3337     {
3338       /* Binding label given; store in temp holder til have sym.  */
3339       strcpy (dest_label, curr_binding_label);
3340     }
3341   else
3342     {
3343       /* No binding label given, and the NAME= specifier did not exist,
3344          which means there was no NAME="".  */
3345       if (sym_name != NULL && has_name_equals == 0)
3346         strcpy (dest_label, sym_name);
3347     }
3348    
3349   return SUCCESS;
3350 }
3351
3352
3353 /* Set the status of the given common block as being BIND(C) or not,
3354    depending on the given parameter, is_bind_c.  */
3355
3356 void
3357 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3358 {
3359   com_block->is_bind_c = is_bind_c;
3360   return;
3361 }
3362
3363
3364 /* Verify that the given gfc_typespec is for a C interoperable type.  */
3365
3366 gfc_try
3367 verify_c_interop (gfc_typespec *ts)
3368 {
3369   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3370     return (ts->u.derived->ts.is_c_interop ? SUCCESS : FAILURE);
3371   else if (ts->is_c_interop != 1)
3372     return FAILURE;
3373   
3374   return SUCCESS;
3375 }
3376
3377
3378 /* Verify that the variables of a given common block, which has been
3379    defined with the attribute specifier bind(c), to be of a C
3380    interoperable type.  Errors will be reported here, if
3381    encountered.  */
3382
3383 gfc_try
3384 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3385 {
3386   gfc_symbol *curr_sym = NULL;
3387   gfc_try retval = SUCCESS;
3388
3389   curr_sym = com_block->head;
3390   
3391   /* Make sure we have at least one symbol.  */
3392   if (curr_sym == NULL)
3393     return retval;
3394
3395   /* Here we know we have a symbol, so we'll execute this loop
3396      at least once.  */
3397   do
3398     {
3399       /* The second to last param, 1, says this is in a common block.  */
3400       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3401       curr_sym = curr_sym->common_next;
3402     } while (curr_sym != NULL); 
3403
3404   return retval;
3405 }
3406
3407
3408 /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
3409    an appropriate error message is reported.  */
3410
3411 gfc_try
3412 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3413                    int is_in_common, gfc_common_head *com_block)
3414 {
3415   bool bind_c_function = false;
3416   gfc_try retval = SUCCESS;
3417
3418   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3419     bind_c_function = true;
3420
3421   if (tmp_sym->attr.function && tmp_sym->result != NULL)
3422     {
3423       tmp_sym = tmp_sym->result;
3424       /* Make sure it wasn't an implicitly typed result.  */
3425       if (tmp_sym->attr.implicit_type)
3426         {
3427           gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3428                        "%L may not be C interoperable", tmp_sym->name,
3429                        &tmp_sym->declared_at);
3430           tmp_sym->ts.f90_type = tmp_sym->ts.type;
3431           /* Mark it as C interoperable to prevent duplicate warnings.  */
3432           tmp_sym->ts.is_c_interop = 1;
3433           tmp_sym->attr.is_c_interop = 1;
3434         }
3435     }
3436
3437   /* Here, we know we have the bind(c) attribute, so if we have
3438      enough type info, then verify that it's a C interop kind.
3439      The info could be in the symbol already, or possibly still in
3440      the given ts (current_ts), so look in both.  */
3441   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN) 
3442     {
3443       if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3444         {
3445           /* See if we're dealing with a sym in a common block or not.  */
3446           if (is_in_common == 1)
3447             {
3448               gfc_warning ("Variable '%s' in common block '%s' at %L "
3449                            "may not be a C interoperable "
3450                            "kind though common block '%s' is BIND(C)",
3451                            tmp_sym->name, com_block->name,
3452                            &(tmp_sym->declared_at), com_block->name);
3453             }
3454           else
3455             {
3456               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3457                 gfc_error ("Type declaration '%s' at %L is not C "
3458                            "interoperable but it is BIND(C)",
3459                            tmp_sym->name, &(tmp_sym->declared_at));
3460               else
3461                 gfc_warning ("Variable '%s' at %L "
3462                              "may not be a C interoperable "
3463                              "kind but it is bind(c)",
3464                              tmp_sym->name, &(tmp_sym->declared_at));
3465             }
3466         }
3467       
3468       /* Variables declared w/in a common block can't be bind(c)
3469          since there's no way for C to see these variables, so there's
3470          semantically no reason for the attribute.  */
3471       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3472         {
3473           gfc_error ("Variable '%s' in common block '%s' at "
3474                      "%L cannot be declared with BIND(C) "
3475                      "since it is not a global",
3476                      tmp_sym->name, com_block->name,
3477                      &(tmp_sym->declared_at));
3478           retval = FAILURE;
3479         }
3480       
3481       /* Scalar variables that are bind(c) can not have the pointer
3482          or allocatable attributes.  */
3483       if (tmp_sym->attr.is_bind_c == 1)
3484         {
3485           if (tmp_sym->attr.pointer == 1)
3486             {
3487               gfc_error ("Variable '%s' at %L cannot have both the "
3488                          "POINTER and BIND(C) attributes",
3489                          tmp_sym->name, &(tmp_sym->declared_at));
3490               retval = FAILURE;
3491             }
3492
3493           if (tmp_sym->attr.allocatable == 1)
3494             {
3495               gfc_error ("Variable '%s' at %L cannot have both the "
3496                          "ALLOCATABLE and BIND(C) attributes",
3497                          tmp_sym->name, &(tmp_sym->declared_at));
3498               retval = FAILURE;
3499             }
3500
3501         }
3502
3503       /* If it is a BIND(C) function, make sure the return value is a
3504          scalar value.  The previous tests in this function made sure
3505          the type is interoperable.  */
3506       if (bind_c_function && tmp_sym->as != NULL)
3507         gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3508                    "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3509
3510       /* BIND(C) functions can not return a character string.  */
3511       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3512         if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3513             || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3514             || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3515           gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3516                          "be a character string", tmp_sym->name,
3517                          &(tmp_sym->declared_at));
3518     }
3519
3520   /* See if the symbol has been marked as private.  If it has, make sure
3521      there is no binding label and warn the user if there is one.  */
3522   if (tmp_sym->attr.access == ACCESS_PRIVATE
3523       && tmp_sym->binding_label[0] != '\0')
3524       /* Use gfc_warning_now because we won't say that the symbol fails
3525          just because of this.  */
3526       gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3527                        "given the binding label '%s'", tmp_sym->name,
3528                        &(tmp_sym->declared_at), tmp_sym->binding_label);
3529
3530   return retval;
3531 }
3532
3533
3534 /* Set the appropriate fields for a symbol that's been declared as
3535    BIND(C) (the is_bind_c flag and the binding label), and verify that
3536    the type is C interoperable.  Errors are reported by the functions
3537    used to set/test these fields.  */
3538
3539 gfc_try
3540 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3541 {
3542   gfc_try retval = SUCCESS;
3543   
3544   /* TODO: Do we need to make sure the vars aren't marked private?  */
3545
3546   /* Set the is_bind_c bit in symbol_attribute.  */
3547   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3548
3549   if (set_binding_label (tmp_sym->binding_label, tmp_sym->name, 
3550                          num_idents) != SUCCESS)
3551     return FAILURE;
3552
3553   return retval;
3554 }
3555
3556
3557 /* Set the fields marking the given common block as BIND(C), including
3558    a binding label, and report any errors encountered.  */
3559
3560 gfc_try
3561 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3562 {
3563   gfc_try retval = SUCCESS;
3564   
3565   /* destLabel, common name, typespec (which may have binding label).  */
3566   if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3567       != SUCCESS)
3568     return FAILURE;
3569
3570   /* Set the given common block (com_block) to being bind(c) (1).  */
3571   set_com_block_bind_c (com_block, 1);
3572
3573   return retval;
3574 }
3575
3576
3577 /* Retrieve the list of one or more identifiers that the given bind(c)
3578    attribute applies to.  */
3579
3580 gfc_try
3581 get_bind_c_idents (void)
3582 {
3583   char name[GFC_MAX_SYMBOL_LEN + 1];
3584   int num_idents = 0;
3585   gfc_symbol *tmp_sym = NULL;
3586   match found_id;
3587   gfc_common_head *com_block = NULL;
3588   
3589   if (gfc_match_name (name) == MATCH_YES)
3590     {
3591       found_id = MATCH_YES;
3592       gfc_get_ha_symbol (name, &tmp_sym);
3593     }
3594   else if (match_common_name (name) == MATCH_YES)
3595     {
3596       found_id = MATCH_YES;
3597       com_block = gfc_get_common (name, 0);
3598     }
3599   else
3600     {
3601       gfc_error ("Need either entity or common block name for "
3602                  "attribute specification statement at %C");
3603       return FAILURE;
3604     }
3605    
3606   /* Save the current identifier and look for more.  */
3607   do
3608     {
3609       /* Increment the number of identifiers found for this spec stmt.  */
3610       num_idents++;
3611
3612       /* Make sure we have a sym or com block, and verify that it can
3613          be bind(c).  Set the appropriate field(s) and look for more
3614          identifiers.  */
3615       if (tmp_sym != NULL || com_block != NULL)         
3616         {
3617           if (tmp_sym != NULL)
3618             {
3619               if (set_verify_bind_c_sym (tmp_sym, num_idents)
3620                   != SUCCESS)
3621                 return FAILURE;
3622             }
3623           else
3624             {
3625               if (set_verify_bind_c_com_block(com_block, num_idents)
3626                   != SUCCESS)
3627                 return FAILURE;
3628             }
3629          
3630           /* Look to see if we have another identifier.  */
3631           tmp_sym = NULL;
3632           if (gfc_match_eos () == MATCH_YES)
3633             found_id = MATCH_NO;
3634           else if (gfc_match_char (',') != MATCH_YES)
3635             found_id = MATCH_NO;
3636           else if (gfc_match_name (name) == MATCH_YES)
3637             {
3638               found_id = MATCH_YES;
3639               gfc_get_ha_symbol (name, &tmp_sym);
3640             }
3641           else if (match_common_name (name) == MATCH_YES)
3642             {
3643               found_id = MATCH_YES;
3644               com_block = gfc_get_common (name, 0);
3645             }
3646           else
3647             {
3648               gfc_error ("Missing entity or common block name for "
3649                          "attribute specification statement at %C");
3650               return FAILURE;
3651             }
3652         }
3653       else
3654         {
3655           gfc_internal_error ("Missing symbol");
3656         }
3657     } while (found_id == MATCH_YES);
3658
3659   /* if we get here we were successful */
3660   return SUCCESS;
3661 }
3662
3663
3664 /* Try and match a BIND(C) attribute specification statement.  */
3665    
3666 match
3667 gfc_match_bind_c_stmt (void)
3668 {
3669   match found_match = MATCH_NO;
3670   gfc_typespec *ts;
3671
3672   ts = &current_ts;
3673   
3674   /* This may not be necessary.  */
3675   gfc_clear_ts (ts);
3676   /* Clear the temporary binding label holder.  */
3677   curr_binding_label[0] = '\0';
3678
3679   /* Look for the bind(c).  */
3680   found_match = gfc_match_bind_c (NULL, true);
3681
3682   if (found_match == MATCH_YES)
3683     {
3684       /* Look for the :: now, but it is not required.  */
3685       gfc_match (" :: ");
3686
3687       /* Get the identifier(s) that needs to be updated.  This may need to
3688          change to hand the flag(s) for the attr specified so all identifiers
3689          found can have all appropriate parts updated (assuming that the same
3690          spec stmt can have multiple attrs, such as both bind(c) and
3691          allocatable...).  */
3692       if (get_bind_c_idents () != SUCCESS)
3693         /* Error message should have printed already.  */
3694         return MATCH_ERROR;
3695     }
3696
3697   return found_match;
3698 }
3699
3700
3701 /* Match a data declaration statement.  */
3702
3703 match
3704 gfc_match_data_decl (void)
3705 {
3706   gfc_symbol *sym;
3707   match m;
3708   int elem;
3709
3710   num_idents_on_line = 0;
3711   
3712   m = gfc_match_decl_type_spec (&current_ts, 0);
3713   if (m != MATCH_YES)
3714     return m;
3715
3716   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3717         && gfc_current_state () != COMP_DERIVED)
3718     {
3719       sym = gfc_use_derived (current_ts.u.derived);
3720
3721       if (sym == NULL)
3722         {
3723           m = MATCH_ERROR;
3724           goto cleanup;
3725         }
3726
3727       current_ts.u.derived = sym;
3728     }
3729
3730   m = match_attr_spec ();
3731   if (m == MATCH_ERROR)
3732     {
3733       m = MATCH_NO;
3734       goto cleanup;
3735     }
3736
3737   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3738       && current_ts.u.derived->components == NULL
3739       && !current_ts.u.derived->attr.zero_comp)
3740     {
3741
3742       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3743         goto ok;
3744
3745       gfc_find_symbol (current_ts.u.derived->name,
3746                        current_ts.u.derived->ns->parent, 1, &sym);
3747
3748       /* Any symbol that we find had better be a type definition
3749          which has its components defined.  */
3750       if (sym != NULL && sym->attr.flavor == FL_DERIVED
3751           && (current_ts.u.derived->components != NULL
3752               || current_ts.u.derived->attr.zero_comp))
3753         goto ok;
3754
3755       /* Now we have an error, which we signal, and then fix up
3756          because the knock-on is plain and simple confusing.  */
3757       gfc_error_now ("Derived type at %C has not been previously defined "
3758                      "and so cannot appear in a derived type definition");
3759       current_attr.pointer = 1;
3760       goto ok;
3761     }
3762
3763 ok:
3764   /* If we have an old-style character declaration, and no new-style
3765      attribute specifications, then there a comma is optional between
3766      the type specification and the variable list.  */
3767   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3768     gfc_match_char (',');
3769
3770   /* Give the types/attributes to symbols that follow. Give the element
3771      a number so that repeat character length expressions can be copied.  */
3772   elem = 1;
3773   for (;;)
3774     {
3775       num_idents_on_line++;
3776       m = variable_decl (elem++);
3777       if (m == MATCH_ERROR)
3778         goto cleanup;
3779       if (m == MATCH_NO)
3780         break;
3781
3782       if (gfc_match_eos () == MATCH_YES)
3783         goto cleanup;
3784       if (gfc_match_char (',') != MATCH_YES)
3785         break;
3786     }
3787
3788   if (gfc_error_flag_test () == 0)
3789     gfc_error ("Syntax error in data declaration at %C");
3790   m = MATCH_ERROR;
3791
3792   gfc_free_data_all (gfc_current_ns);
3793
3794 cleanup:
3795   gfc_free_array_spec (current_as);
3796   current_as = NULL;
3797   return m;
3798 }
3799
3800
3801 /* Match a prefix associated with a function or subroutine
3802    declaration.  If the typespec pointer is nonnull, then a typespec
3803    can be matched.  Note that if nothing matches, MATCH_YES is
3804    returned (the null string was matched).  */
3805
3806 match
3807 gfc_match_prefix (gfc_typespec *ts)
3808 {
3809   bool seen_type;
3810
3811   gfc_clear_attr (&current_attr);
3812   seen_type = 0;
3813
3814   gcc_assert (!gfc_matching_prefix);
3815   gfc_matching_prefix = true;
3816
3817 loop:
3818   if (!seen_type && ts != NULL
3819       && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
3820       && gfc_match_space () == MATCH_YES)
3821     {
3822
3823       seen_type = 1;
3824       goto loop;
3825     }
3826
3827   if (gfc_match ("elemental% ") == MATCH_YES)
3828     {
3829       if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3830         goto error;
3831
3832       goto loop;
3833     }
3834
3835   if (gfc_match ("pure% ") == MATCH_YES)
3836     {
3837       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3838         goto error;
3839
3840       goto loop;
3841     }
3842
3843   if (gfc_match ("recursive% ") == MATCH_YES)
3844     {
3845       if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3846         goto error;
3847
3848       goto loop;
3849     }
3850
3851   /* At this point, the next item is not a prefix.  */
3852   gcc_assert (gfc_matching_prefix);
3853   gfc_matching_prefix = false;
3854   return MATCH_YES;
3855
3856 error:
3857   gcc_assert (gfc_matching_prefix);
3858   gfc_matching_prefix = false;
3859   return MATCH_ERROR;
3860 }
3861
3862
3863 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
3864
3865 static gfc_try
3866 copy_prefix (symbol_attribute *dest, locus *where)
3867 {
3868   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3869     return FAILURE;
3870
3871   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3872     return FAILURE;
3873
3874   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3875     return FAILURE;
3876
3877   return SUCCESS;
3878 }
3879
3880
3881 /* Match a formal argument list.  */
3882
3883 match
3884 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3885 {
3886   gfc_formal_arglist *head, *tail, *p, *q;
3887   char name[GFC_MAX_SYMBOL_LEN + 1];
3888   gfc_symbol *sym;
3889   match m;
3890
3891   head = tail = NULL;
3892
3893   if (gfc_match_char ('(') != MATCH_YES)
3894     {
3895       if (null_flag)
3896         goto ok;
3897       return MATCH_NO;
3898     }
3899
3900   if (gfc_match_char (')') == MATCH_YES)
3901     goto ok;
3902
3903   for (;;)
3904     {
3905       if (gfc_match_char ('*') == MATCH_YES)
3906         sym = NULL;
3907       else
3908         {
3909           m = gfc_match_name (name);
3910           if (m != MATCH_YES)
3911             goto cleanup;
3912
3913           if (gfc_get_symbol (name, NULL, &sym))
3914             goto cleanup;
3915         }
3916
3917       p = gfc_get_formal_arglist ();
3918
3919       if (head == NULL)
3920         head = tail = p;
3921       else
3922         {
3923           tail->next = p;
3924           tail = p;
3925         }
3926
3927       tail->sym = sym;
3928
3929       /* We don't add the VARIABLE flavor because the name could be a
3930          dummy procedure.  We don't apply these attributes to formal
3931          arguments of statement functions.  */
3932       if (sym != NULL && !st_flag
3933           && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3934               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3935         {
3936           m = MATCH_ERROR;
3937           goto cleanup;
3938         }
3939
3940       /* The name of a program unit can be in a different namespace,
3941          so check for it explicitly.  After the statement is accepted,
3942          the name is checked for especially in gfc_get_symbol().  */
3943       if (gfc_new_block != NULL && sym != NULL
3944           && strcmp (sym->name, gfc_new_block->name) == 0)
3945         {
3946           gfc_error ("Name '%s' at %C is the name of the procedure",
3947                      sym->name);
3948           m = MATCH_ERROR;
3949           goto cleanup;
3950         }
3951
3952       if (gfc_match_char (')') == MATCH_YES)
3953         goto ok;
3954
3955       m = gfc_match_char (',');
3956       if (m != MATCH_YES)
3957         {
3958           gfc_error ("Unexpected junk in formal argument list at %C");
3959           goto cleanup;
3960         }
3961     }
3962
3963 ok:
3964   /* Check for duplicate symbols in the formal argument list.  */
3965   if (head != NULL)
3966     {
3967       for (p = head; p->next; p = p->next)
3968         {
3969           if (p->sym == NULL)
3970             continue;
3971
3972           for (q = p->next; q; q = q->next)
3973             if (p->sym == q->sym)
3974               {
3975                 gfc_error ("Duplicate symbol '%s' in formal argument list "
3976                            "at %C", p->sym->name);
3977
3978                 m = MATCH_ERROR;
3979                 goto cleanup;
3980               }
3981         }
3982     }
3983
3984   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3985       == FAILURE)
3986     {
3987       m = MATCH_ERROR;
3988       goto cleanup;
3989     }
3990
3991   return MATCH_YES;
3992
3993 cleanup:
3994   gfc_free_formal_arglist (head);
3995   return m;
3996 }
3997
3998
3999 /* Match a RESULT specification following a function declaration or
4000    ENTRY statement.  Also matches the end-of-statement.  */
4001
4002 static match
4003 match_result (gfc_symbol *function, gfc_symbol **result)
4004 {
4005   char name[GFC_MAX_SYMBOL_LEN + 1];
4006   gfc_symbol *r;
4007   match m;
4008
4009   if (gfc_match (" result (") != MATCH_YES)
4010     return MATCH_NO;
4011
4012   m = gfc_match_name (name);
4013   if (m != MATCH_YES)
4014     return m;
4015
4016   /* Get the right paren, and that's it because there could be the
4017      bind(c) attribute after the result clause.  */
4018   if (gfc_match_char(')') != MATCH_YES)
4019     {
4020      /* TODO: should report the missing right paren here.  */
4021       return MATCH_ERROR;
4022     }
4023
4024   if (strcmp (function->name, name) == 0)
4025     {
4026       gfc_error ("RESULT variable at %C must be different than function name");
4027       return MATCH_ERROR;
4028     }
4029
4030   if (gfc_get_symbol (name, NULL, &r))
4031     return MATCH_ERROR;
4032
4033   if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4034     return MATCH_ERROR;
4035
4036   *result = r;
4037
4038   return MATCH_YES;
4039 }
4040
4041
4042 /* Match a function suffix, which could be a combination of a result
4043    clause and BIND(C), either one, or neither.  The draft does not
4044    require them to come in a specific order.  */
4045
4046 match
4047 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4048 {
4049   match is_bind_c;   /* Found bind(c).  */
4050   match is_result;   /* Found result clause.  */
4051   match found_match; /* Status of whether we've found a good match.  */
4052   char peek_char;    /* Character we're going to peek at.  */
4053   bool allow_binding_name;
4054
4055   /* Initialize to having found nothing.  */
4056   found_match = MATCH_NO;
4057   is_bind_c = MATCH_NO; 
4058   is_result = MATCH_NO;
4059
4060   /* Get the next char to narrow between result and bind(c).  */
4061   gfc_gobble_whitespace ();
4062   peek_char = gfc_peek_ascii_char ();
4063
4064   /* C binding names are not allowed for internal procedures.  */
4065   if (gfc_current_state () == COMP_CONTAINS
4066       && sym->ns->proc_name->attr.flavor != FL_MODULE)
4067     allow_binding_name = false;
4068   else
4069     allow_binding_name = true;
4070
4071   switch (peek_char)
4072     {
4073     case 'r':
4074       /* Look for result clause.  */
4075       is_result = match_result (sym, result);
4076       if (is_result == MATCH_YES)
4077         {
4078           /* Now see if there is a bind(c) after it.  */
4079           is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4080           /* We've found the result clause and possibly bind(c).  */
4081           found_match = MATCH_YES;
4082         }
4083       else
4084         /* This should only be MATCH_ERROR.  */
4085         found_match = is_result; 
4086       break;
4087     case 'b':
4088       /* Look for bind(c) first.  */
4089       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4090       if (is_bind_c == MATCH_YES)
4091         {
4092           /* Now see if a result clause followed it.  */
4093           is_result = match_result (sym, result);
4094           found_match = MATCH_YES;
4095         }
4096       else
4097         {
4098           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
4099           found_match = MATCH_ERROR;
4100         }
4101       break;
4102     default:
4103       gfc_error ("Unexpected junk after function declaration at %C");
4104       found_match = MATCH_ERROR;
4105       break;
4106     }
4107
4108   if (is_bind_c == MATCH_YES)
4109     {
4110       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
4111       if (gfc_current_state () == COMP_CONTAINS
4112           && sym->ns->proc_name->attr.flavor != FL_MODULE
4113           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4114                              "at %L may not be specified for an internal "
4115                              "procedure", &gfc_current_locus)
4116              == FAILURE)
4117         return MATCH_ERROR;
4118
4119       if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4120           == FAILURE)
4121         return MATCH_ERROR;
4122     }
4123   
4124   return found_match;
4125 }
4126
4127
4128 /* Procedure pointer return value without RESULT statement:
4129    Add "hidden" result variable named "ppr@".  */
4130
4131 static gfc_try
4132 add_hidden_procptr_result (gfc_symbol *sym)
4133 {
4134   bool case1,case2;
4135
4136   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4137     return FAILURE;
4138
4139   /* First usage case: PROCEDURE and EXTERNAL statements.  */
4140   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4141           && strcmp (gfc_current_block ()->name, sym->name) == 0
4142           && sym->attr.external;
4143   /* Second usage case: INTERFACE statements.  */
4144   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4145           && gfc_state_stack->previous->state == COMP_FUNCTION
4146           && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4147
4148   if (case1 || case2)
4149     {
4150       gfc_symtree *stree;
4151       if (case1)
4152         gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4153       else if (case2)
4154         {
4155           gfc_symtree *st2;
4156           gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4157           st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4158           st2->n.sym = stree->n.sym;
4159         }
4160       sym->result = stree->n.sym;
4161
4162       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4163       sym->result->attr.pointer = sym->attr.pointer;
4164       sym->result->attr.external = sym->attr.external;
4165       sym->result->attr.referenced = sym->attr.referenced;
4166       sym->result->ts = sym->ts;
4167       sym->attr.proc_pointer = 0;
4168       sym->attr.pointer = 0;
4169       sym->attr.external = 0;
4170       if (sym->result->attr.external && sym->result->attr.pointer)
4171         {
4172           sym->result->attr.pointer = 0;
4173           sym->result->attr.proc_pointer = 1;
4174         }
4175
4176       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4177     }
4178   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
4179   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4180            && sym->result && sym->result != sym && sym->result->attr.external
4181            && sym == gfc_current_ns->proc_name
4182            && sym == sym->result->ns->proc_name
4183            && strcmp ("ppr@", sym->result->name) == 0)
4184     {
4185       sym->result->attr.proc_pointer = 1;
4186       sym->attr.pointer = 0;
4187       return SUCCESS;
4188     }
4189   else
4190     return FAILURE;
4191 }
4192
4193
4194 /* Match the interface for a PROCEDURE declaration,
4195    including brackets (R1212).  */
4196
4197 static match
4198 match_procedure_interface (gfc_symbol **proc_if)
4199 {
4200   match m;
4201   gfc_symtree *st;
4202   locus old_loc, entry_loc;
4203   gfc_namespace *old_ns = gfc_current_ns;
4204   char name[GFC_MAX_SYMBOL_LEN + 1];
4205
4206   old_loc = entry_loc = gfc_current_locus;
4207   gfc_clear_ts (&current_ts);
4208
4209   if (gfc_match (" (") != MATCH_YES)
4210     {
4211       gfc_current_locus = entry_loc;
4212       return MATCH_NO;
4213     }
4214
4215   /* Get the type spec. for the procedure interface.  */
4216   old_loc = gfc_current_locus;
4217   m = gfc_match_decl_type_spec (&current_ts, 0);
4218   gfc_gobble_whitespace ();
4219   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4220     goto got_ts;
4221
4222   if (m == MATCH_ERROR)
4223     return m;
4224
4225   /* Procedure interface is itself a procedure.  */
4226   gfc_current_locus = old_loc;
4227   m = gfc_match_name (name);
4228
4229   /* First look to see if it is already accessible in the current
4230      namespace because it is use associated or contained.  */
4231   st = NULL;
4232   if (gfc_find_sym_tree (name, NULL, 0, &st))
4233     return MATCH_ERROR;
4234
4235   /* If it is still not found, then try the parent namespace, if it
4236      exists and create the symbol there if it is still not found.  */
4237   if (gfc_current_ns->parent)
4238     gfc_current_ns = gfc_current_ns->parent;
4239   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4240     return MATCH_ERROR;
4241
4242   gfc_current_ns = old_ns;
4243   *proc_if = st->n.sym;
4244
4245   /* Various interface checks.  */
4246   if (*proc_if)
4247     {
4248       (*proc_if)->refs++;
4249       /* Resolve interface if possible. That way, attr.procedure is only set
4250          if it is declared by a later procedure-declaration-stmt, which is
4251          invalid per C1212.  */
4252       while ((*proc_if)->ts.interface)
4253         *proc_if = (*proc_if)->ts.interface;
4254
4255       if ((*proc_if)->generic)
4256         {
4257           gfc_error ("Interface '%s' at %C may not be generic",
4258                      (*proc_if)->name);
4259           return MATCH_ERROR;
4260         }
4261       if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4262         {
4263           gfc_error ("Interface '%s' at %C may not be a statement function",
4264                      (*proc_if)->name);
4265           return MATCH_ERROR;
4266         }
4267       /* Handle intrinsic procedures.  */
4268       if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4269             || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4270           && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4271               || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4272         (*proc_if)->attr.intrinsic = 1;
4273       if ((*proc_if)->attr.intrinsic
4274           && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4275         {
4276           gfc_error ("Intrinsic procedure '%s' not allowed "
4277                     "in PROCEDURE statement at %C", (*proc_if)->name);
4278           return MATCH_ERROR;
4279         }
4280     }
4281
4282 got_ts:
4283   if (gfc_match (" )") != MATCH_YES)
4284     {
4285       gfc_current_locus = entry_loc;
4286       return MATCH_NO;
4287     }
4288
4289   return MATCH_YES;
4290 }
4291
4292
4293 /* Match a PROCEDURE declaration (R1211).  */
4294
4295 static match
4296 match_procedure_decl (void)
4297 {
4298   match m;
4299   gfc_symbol *sym, *proc_if = NULL;
4300   int num;
4301   gfc_expr *initializer = NULL;
4302
4303   /* Parse interface (with brackets). */
4304   m = match_procedure_interface (&proc_if);
4305   if (m != MATCH_YES)
4306     return m;
4307
4308   /* Parse attributes (with colons).  */
4309   m = match_attr_spec();
4310   if (m == MATCH_ERROR)
4311     return MATCH_ERROR;
4312
4313   /* Get procedure symbols.  */
4314   for(num=1;;num++)
4315     {
4316       m = gfc_match_symbol (&sym, 0);
4317       if (m == MATCH_NO)
4318         goto syntax;
4319       else if (m == MATCH_ERROR)
4320         return m;
4321
4322       /* Add current_attr to the symbol attributes.  */
4323       if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4324         return MATCH_ERROR;
4325
4326       if (sym->attr.is_bind_c)
4327         {
4328           /* Check for C1218.  */
4329           if (!proc_if || !proc_if->attr.is_bind_c)
4330             {
4331               gfc_error ("BIND(C) attribute at %C requires "
4332                         "an interface with BIND(C)");
4333               return MATCH_ERROR;
4334             }
4335           /* Check for C1217.  */
4336           if (has_name_equals && sym->attr.pointer)
4337             {
4338               gfc_error ("BIND(C) procedure with NAME may not have "
4339                         "POINTER attribute at %C");
4340               return MATCH_ERROR;
4341             }
4342           if (has_name_equals && sym->attr.dummy)
4343             {
4344               gfc_error ("Dummy procedure at %C may not have "
4345                         "BIND(C) attribute with NAME");
4346               return MATCH_ERROR;
4347             }
4348           /* Set binding label for BIND(C).  */
4349           if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4350             return MATCH_ERROR;
4351         }
4352
4353       if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4354         return MATCH_ERROR;
4355
4356       if (add_hidden_procptr_result (sym) == SUCCESS)
4357         sym = sym->result;
4358
4359       if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4360         return MATCH_ERROR;
4361
4362       /* Set interface.  */
4363       if (proc_if != NULL)
4364         {
4365           if (sym->ts.type != BT_UNKNOWN)
4366             {
4367               gfc_error ("Procedure '%s' at %L already has basic type of %s",
4368                          sym->name, &gfc_current_locus,
4369                          gfc_basic_typename (sym->ts.type));
4370               return MATCH_ERROR;
4371             }
4372           sym->ts.interface = proc_if;
4373           sym->attr.untyped = 1;
4374           sym->attr.if_source = IFSRC_IFBODY;
4375         }
4376       else if (current_ts.type != BT_UNKNOWN)
4377         {
4378           if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4379             return MATCH_ERROR;
4380           sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4381           sym->ts.interface->ts = current_ts;
4382           sym->ts.interface->attr.function = 1;
4383           sym->attr.function = sym->ts.interface->attr.function;
4384           sym->attr.if_source = IFSRC_UNKNOWN;
4385         }
4386
4387       if (gfc_match (" =>") == MATCH_YES)
4388         {
4389           if (!current_attr.pointer)
4390             {
4391               gfc_error ("Initialization at %C isn't for a pointer variable");
4392               m = MATCH_ERROR;
4393               goto cleanup;
4394             }
4395
4396           m = gfc_match_null (&initializer);
4397           if (m == MATCH_NO)
4398             {
4399               gfc_error ("Pointer initialization requires a NULL() at %C");
4400               m = MATCH_ERROR;
4401             }
4402
4403           if (gfc_pure (NULL))
4404             {
4405               gfc_error ("Initialization of pointer at %C is not allowed in "
4406                          "a PURE procedure");
4407               m = MATCH_ERROR;
4408             }
4409
4410           if (m != MATCH_YES)
4411             goto cleanup;
4412
4413           if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4414               != SUCCESS)
4415             goto cleanup;
4416
4417         }
4418
4419       gfc_set_sym_referenced (sym);
4420
4421       if (gfc_match_eos () == MATCH_YES)
4422         return MATCH_YES;
4423       if (gfc_match_char (',') != MATCH_YES)
4424         goto syntax;
4425     }
4426
4427 syntax:
4428   gfc_error ("Syntax error in PROCEDURE statement at %C");
4429   return MATCH_ERROR;
4430
4431 cleanup:
4432   /* Free stuff up and return.  */
4433   gfc_free_expr (initializer);
4434   return m;
4435 }
4436
4437
4438 static match
4439 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4440
4441
4442 /* Match a procedure pointer component declaration (R445).  */
4443
4444 static match
4445 match_ppc_decl (void)
4446 {
4447   match m;
4448   gfc_symbol *proc_if = NULL;
4449   gfc_typespec ts;
4450   int num;
4451   gfc_component *c;
4452   gfc_expr *initializer = NULL;
4453   gfc_typebound_proc* tb;
4454   char name[GFC_MAX_SYMBOL_LEN + 1];
4455
4456   /* Parse interface (with brackets).  */
4457   m = match_procedure_interface (&proc_if);
4458   if (m != MATCH_YES)
4459     goto syntax;
4460
4461   /* Parse attributes.  */
4462   tb = XCNEW (gfc_typebound_proc);
4463   tb->where = gfc_current_locus;
4464   m = match_binding_attributes (tb, false, true);
4465   if (m == MATCH_ERROR)
4466     return m;
4467
4468   gfc_clear_attr (&current_attr);
4469   current_attr.procedure = 1;
4470   current_attr.proc_pointer = 1;
4471   current_attr.access = tb->access;
4472   current_attr.flavor = FL_PROCEDURE;
4473
4474   /* Match the colons (required).  */
4475   if (gfc_match (" ::") != MATCH_YES)
4476     {
4477       gfc_error ("Expected '::' after binding-attributes at %C");
4478       return MATCH_ERROR;
4479     }
4480
4481   /* Check for C450.  */
4482   if (!tb->nopass && proc_if == NULL)
4483     {
4484       gfc_error("NOPASS or explicit interface required at %C");
4485       return MATCH_ERROR;
4486     }
4487
4488   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4489                      "component at %C") == FAILURE)
4490     return MATCH_ERROR;
4491
4492   /* Match PPC names.  */
4493   ts = current_ts;
4494   for(num=1;;num++)
4495     {
4496       m = gfc_match_name (name);
4497       if (m == MATCH_NO)
4498         goto syntax;
4499       else if (m == MATCH_ERROR)
4500         return m;
4501
4502       if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4503         return MATCH_ERROR;
4504
4505       /* Add current_attr to the symbol attributes.  */
4506       if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4507         return MATCH_ERROR;
4508
4509       if (gfc_add_external (&c->attr, NULL) == FAILURE)
4510         return MATCH_ERROR;
4511
4512       if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4513         return MATCH_ERROR;
4514
4515       c->tb = tb;
4516
4517       /* Set interface.  */
4518       if (proc_if != NULL)
4519         {
4520           c->ts.interface = proc_if;
4521           c->attr.untyped = 1;
4522           c->attr.if_source = IFSRC_IFBODY;
4523         }
4524       else if (ts.type != BT_UNKNOWN)
4525         {
4526           c->ts = ts;
4527           c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4528           c->ts.interface->ts = ts;
4529           c->ts.interface->attr.function = 1;
4530           c->attr.function = c->ts.interface->attr.function;
4531           c->attr.if_source = IFSRC_UNKNOWN;
4532         }
4533
4534       if (gfc_match (" =>") == MATCH_YES)
4535         {
4536           m = gfc_match_null (&initializer);
4537           if (m == MATCH_NO)
4538             {
4539               gfc_error ("Pointer initialization requires a NULL() at %C");
4540               m = MATCH_ERROR;
4541             }
4542           if (gfc_pure (NULL))
4543             {
4544               gfc_error ("Initialization of pointer at %C is not allowed in "
4545                          "a PURE procedure");
4546               m = MATCH_ERROR;
4547             }
4548           if (m != MATCH_YES)
4549             {
4550               gfc_free_expr (initializer);
4551               return m;
4552             }
4553           c->initializer = initializer;
4554         }
4555
4556       if (gfc_match_eos () == MATCH_YES)
4557         return MATCH_YES;
4558       if (gfc_match_char (',') != MATCH_YES)
4559         goto syntax;
4560     }
4561
4562 syntax:
4563   gfc_error ("Syntax error in procedure pointer component at %C");
4564   return MATCH_ERROR;
4565 }
4566
4567
4568 /* Match a PROCEDURE declaration inside an interface (R1206).  */
4569
4570 static match
4571 match_procedure_in_interface (void)
4572 {
4573   match m;
4574   gfc_symbol *sym;
4575   char name[GFC_MAX_SYMBOL_LEN + 1];
4576
4577   if (current_interface.type == INTERFACE_NAMELESS
4578       || current_interface.type == INTERFACE_ABSTRACT)
4579     {
4580       gfc_error ("PROCEDURE at %C must be in a generic interface");
4581       return MATCH_ERROR;
4582     }
4583
4584   for(;;)
4585     {
4586       m = gfc_match_name (name);
4587       if (m == MATCH_NO)
4588         goto syntax;
4589       else if (m == MATCH_ERROR)
4590         return m;
4591       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4592         return MATCH_ERROR;
4593
4594       if (gfc_add_interface (sym) == FAILURE)
4595         return MATCH_ERROR;
4596
4597       if (gfc_match_eos () == MATCH_YES)
4598         break;
4599       if (gfc_match_char (',') != MATCH_YES)
4600         goto syntax;
4601     }
4602
4603   return MATCH_YES;
4604
4605 syntax:
4606   gfc_error ("Syntax error in PROCEDURE statement at %C");
4607   return MATCH_ERROR;
4608 }
4609
4610
4611 /* General matcher for PROCEDURE declarations.  */
4612
4613 static match match_procedure_in_type (void);
4614
4615 match
4616 gfc_match_procedure (void)
4617 {
4618   match m;
4619
4620   switch (gfc_current_state ())
4621     {
4622     case COMP_NONE:
4623     case COMP_PROGRAM:
4624     case COMP_MODULE:
4625     case COMP_SUBROUTINE:
4626     case COMP_FUNCTION:
4627       m = match_procedure_decl ();
4628       break;
4629     case COMP_INTERFACE:
4630       m = match_procedure_in_interface ();
4631       break;
4632     case COMP_DERIVED:
4633       m = match_ppc_decl ();
4634       break;
4635     case COMP_DERIVED_CONTAINS:
4636       m = match_procedure_in_type ();
4637       break;
4638     default:
4639       return MATCH_NO;
4640     }
4641
4642   if (m != MATCH_YES)
4643     return m;
4644
4645   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4646       == FAILURE)
4647     return MATCH_ERROR;
4648
4649   return m;
4650 }
4651
4652
4653 /* Warn if a matched procedure has the same name as an intrinsic; this is
4654    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4655    parser-state-stack to find out whether we're in a module.  */
4656
4657 static void
4658 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4659 {
4660   bool in_module;
4661
4662   in_module = (gfc_state_stack->previous
4663                && gfc_state_stack->previous->state == COMP_MODULE);
4664
4665   gfc_warn_intrinsic_shadow (sym, in_module, func);
4666 }
4667
4668
4669 /* Match a function declaration.  */
4670
4671 match
4672 gfc_match_function_decl (void)
4673 {
4674   char name[GFC_MAX_SYMBOL_LEN + 1];
4675   gfc_symbol *sym, *result;
4676   locus old_loc;
4677   match m;
4678   match suffix_match;
4679   match found_match; /* Status returned by match func.  */  
4680
4681   if (gfc_current_state () != COMP_NONE
4682       && gfc_current_state () != COMP_INTERFACE
4683       && gfc_current_state () != COMP_CONTAINS)
4684     return MATCH_NO;
4685
4686   gfc_clear_ts (&current_ts);
4687
4688   old_loc = gfc_current_locus;
4689
4690   m = gfc_match_prefix (&current_ts);
4691   if (m != MATCH_YES)
4692     {
4693       gfc_current_locus = old_loc;
4694       return m;
4695     }
4696
4697   if (gfc_match ("function% %n", name) != MATCH_YES)
4698     {
4699       gfc_current_locus = old_loc;
4700       return MATCH_NO;
4701     }
4702   if (get_proc_name (name, &sym, false))
4703     return MATCH_ERROR;
4704
4705   if (add_hidden_procptr_result (sym) == SUCCESS)
4706     sym = sym->result;
4707
4708   gfc_new_block = sym;
4709
4710   m = gfc_match_formal_arglist (sym, 0, 0);
4711   if (m == MATCH_NO)
4712     {
4713       gfc_error ("Expected formal argument list in function "
4714                  "definition at %C");
4715       m = MATCH_ERROR;
4716       goto cleanup;
4717     }
4718   else if (m == MATCH_ERROR)
4719     goto cleanup;
4720
4721   result = NULL;
4722
4723   /* According to the draft, the bind(c) and result clause can
4724      come in either order after the formal_arg_list (i.e., either
4725      can be first, both can exist together or by themselves or neither
4726      one).  Therefore, the match_result can't match the end of the
4727      string, and check for the bind(c) or result clause in either order.  */
4728   found_match = gfc_match_eos ();
4729
4730   /* Make sure that it isn't already declared as BIND(C).  If it is, it
4731      must have been marked BIND(C) with a BIND(C) attribute and that is
4732      not allowed for procedures.  */
4733   if (sym->attr.is_bind_c == 1)
4734     {
4735       sym->attr.is_bind_c = 0;
4736       if (sym->old_symbol != NULL)
4737         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4738                        "variables or common blocks",
4739                        &(sym->old_symbol->declared_at));
4740       else
4741         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4742                        "variables or common blocks", &gfc_current_locus);
4743     }
4744
4745   if (found_match != MATCH_YES)
4746     {
4747       /* If we haven't found the end-of-statement, look for a suffix.  */
4748       suffix_match = gfc_match_suffix (sym, &result);
4749       if (suffix_match == MATCH_YES)
4750         /* Need to get the eos now.  */
4751         found_match = gfc_match_eos ();
4752       else
4753         found_match = suffix_match;
4754     }
4755
4756   if(found_match != MATCH_YES)
4757     m = MATCH_ERROR;
4758   else
4759     {
4760       /* Make changes to the symbol.  */
4761       m = MATCH_ERROR;
4762       
4763       if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4764         goto cleanup;
4765       
4766       if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4767           || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4768         goto cleanup;
4769
4770       /* Delay matching the function characteristics until after the
4771          specification block by signalling kind=-1.  */
4772       sym->declared_at = old_loc;
4773       if (current_ts.type != BT_UNKNOWN)
4774         current_ts.kind = -1;
4775       else
4776         current_ts.kind = 0;
4777
4778       if (result == NULL)
4779         {
4780           if (current_ts.type != BT_UNKNOWN
4781               && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4782             goto cleanup;
4783           sym->result = sym;
4784         }
4785       else
4786         {
4787           if (current_ts.type != BT_UNKNOWN
4788               && gfc_add_type (result, &current_ts, &gfc_current_locus)
4789                  == FAILURE)
4790             goto cleanup;
4791           sym->result = result;
4792         }
4793
4794       /* Warn if this procedure has the same name as an intrinsic.  */
4795       warn_intrinsic_shadow (sym, true);
4796
4797       return MATCH_YES;
4798     }
4799
4800 cleanup:
4801   gfc_current_locus = old_loc;
4802   return m;
4803 }
4804
4805
4806 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4807    pass the name of the entry, rather than the gfc_current_block name, and
4808    to return false upon finding an existing global entry.  */
4809
4810 static bool
4811 add_global_entry (const char *name, int sub)
4812 {
4813   gfc_gsymbol *s;
4814   enum gfc_symbol_type type;
4815
4816   s = gfc_get_gsymbol(name);
4817   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4818
4819   if (s->defined
4820       || (s->type != GSYM_UNKNOWN
4821           && s->type != type))
4822     gfc_global_used(s, NULL);
4823   else
4824     {
4825       s->type = type;
4826       s->where = gfc_current_locus;
4827       s->defined = 1;
4828       s->ns = gfc_current_ns;
4829       return true;
4830     }
4831   return false;
4832 }
4833
4834
4835 /* Match an ENTRY statement.  */
4836
4837 match
4838 gfc_match_entry (void)
4839 {
4840   gfc_symbol *proc;
4841   gfc_symbol *result;
4842   gfc_symbol *entry;
4843   char name[GFC_MAX_SYMBOL_LEN + 1];
4844   gfc_compile_state state;
4845   match m;
4846   gfc_entry_list *el;
4847   locus old_loc;
4848   bool module_procedure;
4849   char peek_char;
4850   match is_bind_c;
4851
4852   m = gfc_match_name (name);
4853   if (m != MATCH_YES)
4854     return m;
4855
4856   state = gfc_current_state ();
4857   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4858     {
4859       switch (state)
4860         {
4861           case COMP_PROGRAM:
4862             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4863             break;
4864           case COMP_MODULE:
4865             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4866             break;
4867           case COMP_BLOCK_DATA:
4868             gfc_error ("ENTRY statement at %C cannot appear within "
4869                        "a BLOCK DATA");
4870             break;
4871           case COMP_INTERFACE:
4872             gfc_error ("ENTRY statement at %C cannot appear within "
4873                        "an INTERFACE");
4874             break;
4875           case COMP_DERIVED:
4876             gfc_error ("ENTRY statement at %C cannot appear within "
4877                        "a DERIVED TYPE block");
4878             break;
4879           case COMP_IF:
4880             gfc_error ("ENTRY statement at %C cannot appear within "
4881                        "an IF-THEN block");
4882             break;
4883           case COMP_DO:
4884             gfc_error ("ENTRY statement at %C cannot appear within "
4885                        "a DO block");
4886             break;
4887           case COMP_SELECT:
4888             gfc_error ("ENTRY statement at %C cannot appear within "
4889                        "a SELECT block");
4890             break;
4891           case COMP_FORALL:
4892             gfc_error ("ENTRY statement at %C cannot appear within "
4893                        "a FORALL block");
4894             break;
4895           case COMP_WHERE:
4896             gfc_error ("ENTRY statement at %C cannot appear within "
4897                        "a WHERE block");
4898             break;
4899           case COMP_CONTAINS:
4900             gfc_error ("ENTRY statement at %C cannot appear within "
4901                        "a contained subprogram");
4902             break;
4903           default:
4904             gfc_internal_error ("gfc_match_entry(): Bad state");
4905         }
4906       return MATCH_ERROR;
4907     }
4908
4909   module_procedure = gfc_current_ns->parent != NULL
4910                    && gfc_current_ns->parent->proc_name
4911                    && gfc_current_ns->parent->proc_name->attr.flavor
4912                       == FL_MODULE;
4913
4914   if (gfc_current_ns->parent != NULL
4915       && gfc_current_ns->parent->proc_name
4916       && !module_procedure)
4917     {
4918       gfc_error("ENTRY statement at %C cannot appear in a "
4919                 "contained procedure");
4920       return MATCH_ERROR;
4921     }
4922
4923   /* Module function entries need special care in get_proc_name
4924      because previous references within the function will have
4925      created symbols attached to the current namespace.  */
4926   if (get_proc_name (name, &entry,
4927                      gfc_current_ns->parent != NULL
4928                      && module_procedure))
4929     return MATCH_ERROR;
4930
4931   proc = gfc_current_block ();
4932
4933   /* Make sure that it isn't already declared as BIND(C).  If it is, it
4934      must have been marked BIND(C) with a BIND(C) attribute and that is
4935      not allowed for procedures.  */
4936   if (entry->attr.is_bind_c == 1)
4937     {
4938       entry->attr.is_bind_c = 0;
4939       if (entry->old_symbol != NULL)
4940         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4941                        "variables or common blocks",
4942                        &(entry->old_symbol->declared_at));
4943       else
4944         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4945                        "variables or common blocks", &gfc_current_locus);
4946     }
4947   
4948   /* Check what next non-whitespace character is so we can tell if there
4949      is the required parens if we have a BIND(C).  */
4950   gfc_gobble_whitespace ();
4951   peek_char = gfc_peek_ascii_char ();
4952
4953   if (state == COMP_SUBROUTINE)
4954     {
4955       /* An entry in a subroutine.  */
4956       if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4957         return MATCH_ERROR;
4958
4959       m = gfc_match_formal_arglist (entry, 0, 1);
4960       if (m != MATCH_YES)
4961         return MATCH_ERROR;
4962
4963       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4964          never be an internal procedure.  */
4965       is_bind_c = gfc_match_bind_c (entry, true);
4966       if (is_bind_c == MATCH_ERROR)
4967         return MATCH_ERROR;
4968       if (is_bind_c == MATCH_YES)
4969         {
4970           if (peek_char != '(')
4971             {
4972               gfc_error ("Missing required parentheses before BIND(C) at %C");
4973               return MATCH_ERROR;
4974             }
4975             if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4976                 == FAILURE)
4977               return MATCH_ERROR;
4978         }
4979
4980       if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4981           || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4982         return MATCH_ERROR;
4983     }
4984   else
4985     {
4986       /* An entry in a function.
4987          We need to take special care because writing
4988             ENTRY f()
4989          as
4990             ENTRY f
4991          is allowed, whereas
4992             ENTRY f() RESULT (r)
4993          can't be written as
4994             ENTRY f RESULT (r).  */
4995       if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4996         return MATCH_ERROR;
4997
4998       old_loc = gfc_current_locus;
4999       if (gfc_match_eos () == MATCH_YES)
5000         {
5001           gfc_current_locus = old_loc;
5002           /* Match the empty argument list, and add the interface to
5003              the symbol.  */
5004           m = gfc_match_formal_arglist (entry, 0, 1);
5005         }
5006       else
5007         m = gfc_match_formal_arglist (entry, 0, 0);
5008
5009       if (m != MATCH_YES)
5010         return MATCH_ERROR;
5011
5012       result = NULL;
5013
5014       if (gfc_match_eos () == MATCH_YES)
5015         {
5016           if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5017               || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5018             return MATCH_ERROR;
5019
5020           entry->result = entry;
5021         }
5022       else
5023         {
5024           m = gfc_match_suffix (entry, &result);
5025           if (m == MATCH_NO)
5026             gfc_syntax_error (ST_ENTRY);
5027           if (m != MATCH_YES)
5028             return MATCH_ERROR;
5029
5030           if (result)
5031             {
5032               if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5033                   || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5034                   || gfc_add_function (&entry->attr, result->name, NULL)
5035                   == FAILURE)
5036                 return MATCH_ERROR;
5037               entry->result = result;
5038             }
5039           else
5040             {
5041               if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5042                   || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5043                 return MATCH_ERROR;
5044               entry->result = entry;
5045             }
5046         }
5047     }
5048
5049   if (gfc_match_eos () != MATCH_YES)
5050     {
5051       gfc_syntax_error (ST_ENTRY);
5052       return MATCH_ERROR;
5053     }
5054
5055   entry->attr.recursive = proc->attr.recursive;
5056   entry->attr.elemental = proc->attr.elemental;
5057   entry->attr.pure = proc->attr.pure;
5058
5059   el = gfc_get_entry_list ();
5060   el->sym = entry;
5061   el->next = gfc_current_ns->entries;
5062   gfc_current_ns->entries = el;
5063   if (el->next)
5064     el->id = el->next->id + 1;
5065   else
5066     el->id = 1;
5067
5068   new_st.op = EXEC_ENTRY;
5069   new_st.ext.entry = el;
5070
5071   return MATCH_YES;
5072 }
5073
5074
5075 /* Match a subroutine statement, including optional prefixes.  */
5076
5077 match
5078 gfc_match_subroutine (void)
5079 {
5080   char name[GFC_MAX_SYMBOL_LEN + 1];
5081   gfc_symbol *sym;
5082   match m;
5083   match is_bind_c;
5084   char peek_char;
5085   bool allow_binding_name;
5086
5087   if (gfc_current_state () != COMP_NONE
5088       && gfc_current_state () != COMP_INTERFACE
5089       && gfc_current_state () != COMP_CONTAINS)
5090     return MATCH_NO;
5091
5092   m = gfc_match_prefix (NULL);
5093   if (m != MATCH_YES)
5094     return m;
5095
5096   m = gfc_match ("subroutine% %n", name);
5097   if (m != MATCH_YES)
5098     return m;
5099
5100   if (get_proc_name (name, &sym, false))
5101     return MATCH_ERROR;
5102
5103   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5104      the symbol existed before. */
5105   sym->declared_at = gfc_current_locus;
5106
5107   if (add_hidden_procptr_result (sym) == SUCCESS)
5108     sym = sym->result;
5109
5110   gfc_new_block = sym;
5111
5112   /* Check what next non-whitespace character is so we can tell if there
5113      is the required parens if we have a BIND(C).  */
5114   gfc_gobble_whitespace ();
5115   peek_char = gfc_peek_ascii_char ();
5116   
5117   if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5118     return MATCH_ERROR;
5119
5120   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5121     return MATCH_ERROR;
5122
5123   /* Make sure that it isn't already declared as BIND(C).  If it is, it
5124      must have been marked BIND(C) with a BIND(C) attribute and that is
5125      not allowed for procedures.  */
5126   if (sym->attr.is_bind_c == 1)
5127     {
5128       sym->attr.is_bind_c = 0;
5129       if (sym->old_symbol != NULL)
5130         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5131                        "variables or common blocks",
5132                        &(sym->old_symbol->declared_at));
5133       else
5134         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5135                        "variables or common blocks", &gfc_current_locus);
5136     }
5137
5138   /* C binding names are not allowed for internal procedures.  */
5139   if (gfc_current_state () == COMP_CONTAINS
5140       && sym->ns->proc_name->attr.flavor != FL_MODULE)
5141     allow_binding_name = false;
5142   else
5143     allow_binding_name = true;
5144
5145   /* Here, we are just checking if it has the bind(c) attribute, and if
5146      so, then we need to make sure it's all correct.  If it doesn't,
5147      we still need to continue matching the rest of the subroutine line.  */
5148   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5149   if (is_bind_c == MATCH_ERROR)
5150     {
5151       /* There was an attempt at the bind(c), but it was wrong.  An
5152          error message should have been printed w/in the gfc_match_bind_c
5153          so here we'll just return the MATCH_ERROR.  */
5154       return MATCH_ERROR;
5155     }
5156
5157   if (is_bind_c == MATCH_YES)
5158     {
5159       /* The following is allowed in the Fortran 2008 draft.  */
5160       if (gfc_current_state () == COMP_CONTAINS
5161           && sym->ns->proc_name->attr.flavor != FL_MODULE
5162           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5163                              "at %L may not be specified for an internal "
5164                              "procedure", &gfc_current_locus)
5165              == FAILURE)
5166         return MATCH_ERROR;
5167
5168       if (peek_char != '(')
5169         {
5170           gfc_error ("Missing required parentheses before BIND(C) at %C");
5171           return MATCH_ERROR;
5172         }
5173       if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5174           == FAILURE)
5175         return MATCH_ERROR;
5176     }
5177   
5178   if (gfc_match_eos () != MATCH_YES)
5179     {
5180       gfc_syntax_error (ST_SUBROUTINE);
5181       return MATCH_ERROR;
5182     }
5183
5184   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5185     return MATCH_ERROR;
5186
5187   /* Warn if it has the same name as an intrinsic.  */
5188   warn_intrinsic_shadow (sym, false);
5189
5190   return MATCH_YES;
5191 }
5192
5193
5194 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5195    given, and set the binding label in either the given symbol (if not
5196    NULL), or in the current_ts.  The symbol may be NULL because we may
5197    encounter the BIND(C) before the declaration itself.  Return
5198    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5199    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5200    or MATCH_YES if the specifier was correct and the binding label and
5201    bind(c) fields were set correctly for the given symbol or the
5202    current_ts. If allow_binding_name is false, no binding name may be
5203    given.  */
5204
5205 match
5206 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5207 {
5208   /* binding label, if exists */   
5209   char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5210   match double_quote;
5211   match single_quote;
5212
5213   /* Initialize the flag that specifies whether we encountered a NAME= 
5214      specifier or not.  */
5215   has_name_equals = 0;
5216
5217   /* Init the first char to nil so we can catch if we don't have
5218      the label (name attr) or the symbol name yet.  */
5219   binding_label[0] = '\0';
5220    
5221   /* This much we have to be able to match, in this order, if
5222      there is a bind(c) label.  */
5223   if (gfc_match (" bind ( c ") != MATCH_YES)
5224     return MATCH_NO;
5225
5226   /* Now see if there is a binding label, or if we've reached the
5227      end of the bind(c) attribute without one.  */
5228   if (gfc_match_char (',') == MATCH_YES)
5229     {
5230       if (gfc_match (" name = ") != MATCH_YES)
5231         {
5232           gfc_error ("Syntax error in NAME= specifier for binding label "
5233                      "at %C");
5234           /* should give an error message here */
5235           return MATCH_ERROR;
5236         }
5237
5238       has_name_equals = 1;
5239
5240       /* Get the opening quote.  */
5241       double_quote = MATCH_YES;
5242       single_quote = MATCH_YES;
5243       double_quote = gfc_match_char ('"');
5244       if (double_quote != MATCH_YES)
5245         single_quote = gfc_match_char ('\'');
5246       if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5247         {
5248           gfc_error ("Syntax error in NAME= specifier for binding label "
5249                      "at %C");
5250           return MATCH_ERROR;
5251         }
5252       
5253       /* Grab the binding label, using functions that will not lower
5254          case the names automatically.  */
5255       if (gfc_match_name_C (binding_label) != MATCH_YES)
5256          return MATCH_ERROR;
5257       
5258       /* Get the closing quotation.  */
5259       if (double_quote == MATCH_YES)
5260         {
5261           if (gfc_match_char ('"') != MATCH_YES)
5262             {
5263               gfc_error ("Missing closing quote '\"' for binding label at %C");
5264               /* User started string with '"' so looked to match it.  */
5265               return MATCH_ERROR;
5266             }
5267         }
5268       else
5269         {
5270           if (gfc_match_char ('\'') != MATCH_YES)
5271             {
5272               gfc_error ("Missing closing quote '\'' for binding label at %C");
5273               /* User started string with "'" char.  */
5274               return MATCH_ERROR;
5275             }
5276         }
5277    }
5278
5279   /* Get the required right paren.  */
5280   if (gfc_match_char (')') != MATCH_YES)
5281     {
5282       gfc_error ("Missing closing paren for binding label at %C");
5283       return MATCH_ERROR;
5284     }
5285
5286   if (has_name_equals && !allow_binding_name)
5287     {
5288       gfc_error ("No binding name is allowed in BIND(C) at %C");
5289       return MATCH_ERROR;
5290     }
5291
5292   if (has_name_equals && sym != NULL && sym->attr.dummy)
5293     {
5294       gfc_error ("For dummy procedure %s, no binding name is "
5295                  "allowed in BIND(C) at %C", sym->name);
5296       return MATCH_ERROR;
5297     }
5298
5299
5300   /* Save the binding label to the symbol.  If sym is null, we're
5301      probably matching the typespec attributes of a declaration and
5302      haven't gotten the name yet, and therefore, no symbol yet.  */
5303   if (binding_label[0] != '\0')
5304     {
5305       if (sym != NULL)
5306       {
5307         strcpy (sym->binding_label, binding_label);
5308       }
5309       else
5310         strcpy (curr_binding_label, binding_label);
5311     }
5312   else if (allow_binding_name)
5313     {
5314       /* No binding label, but if symbol isn't null, we
5315          can set the label for it here.
5316          If name="" or allow_binding_name is false, no C binding name is
5317          created. */
5318       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5319         strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5320     }
5321
5322   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5323       && current_interface.type == INTERFACE_ABSTRACT)
5324     {
5325       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5326       return MATCH_ERROR;
5327     }
5328
5329   return MATCH_YES;
5330 }
5331
5332
5333 /* Return nonzero if we're currently compiling a contained procedure.  */
5334
5335 static int
5336 contained_procedure (void)
5337 {
5338   gfc_state_data *s = gfc_state_stack;
5339
5340   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5341       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5342     return 1;
5343
5344   return 0;
5345 }
5346
5347 /* Set the kind of each enumerator.  The kind is selected such that it is
5348    interoperable with the corresponding C enumeration type, making
5349    sure that -fshort-enums is honored.  */
5350
5351 static void
5352 set_enum_kind(void)
5353 {
5354   enumerator_history *current_history = NULL;
5355   int kind;
5356   int i;
5357
5358   if (max_enum == NULL || enum_history == NULL)
5359     return;
5360
5361   if (!flag_short_enums)
5362     return;
5363
5364   i = 0;
5365   do
5366     {
5367       kind = gfc_integer_kinds[i++].kind;
5368     }
5369   while (kind < gfc_c_int_kind
5370          && gfc_check_integer_range (max_enum->initializer->value.integer,
5371                                      kind) != ARITH_OK);
5372
5373   current_history = enum_history;
5374   while (current_history != NULL)
5375     {
5376       current_history->sym->ts.kind = kind;
5377       current_history = current_history->next;
5378     }
5379 }
5380
5381
5382 /* Match any of the various end-block statements.  Returns the type of
5383    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
5384    and END BLOCK statements cannot be replaced by a single END statement.  */
5385
5386 match
5387 gfc_match_end (gfc_statement *st)
5388 {
5389   char name[GFC_MAX_SYMBOL_LEN + 1];
5390   gfc_compile_state state;
5391   locus old_loc;
5392   const char *block_name;
5393   const char *target;
5394   int eos_ok;
5395   match m;
5396
5397   old_loc = gfc_current_locus;
5398   if (gfc_match ("end") != MATCH_YES)
5399     return MATCH_NO;
5400
5401   state = gfc_current_state ();
5402   block_name = gfc_current_block () == NULL
5403              ? NULL : gfc_current_block ()->name;
5404
5405   if (state == COMP_BLOCK && !strcmp (block_name, "block@"))
5406     block_name = NULL;
5407
5408   if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
5409     {
5410       state = gfc_state_stack->previous->state;
5411       block_name = gfc_state_stack->previous->sym == NULL
5412                  ? NULL : gfc_state_stack->previous->sym->name;
5413     }
5414
5415   switch (state)
5416     {
5417     case COMP_NONE:
5418     case COMP_PROGRAM:
5419       *st = ST_END_PROGRAM;
5420       target = " program";
5421       eos_ok = 1;
5422       break;
5423
5424     case COMP_SUBROUTINE:
5425       *st = ST_END_SUBROUTINE;
5426       target = " subroutine";
5427       eos_ok = !contained_procedure ();
5428       break;
5429
5430     case COMP_FUNCTION:
5431       *st = ST_END_FUNCTION;
5432       target = " function";
5433       eos_ok = !contained_procedure ();
5434       break;
5435
5436     case COMP_BLOCK_DATA:
5437       *st = ST_END_BLOCK_DATA;
5438       target = " block data";
5439       eos_ok = 1;
5440       break;
5441
5442     case COMP_MODULE:
5443       *st = ST_END_MODULE;
5444       target = " module";
5445       eos_ok = 1;
5446       break;
5447
5448     case COMP_INTERFACE:
5449       *st = ST_END_INTERFACE;
5450       target = " interface";
5451       eos_ok = 0;
5452       break;
5453
5454     case COMP_DERIVED:
5455     case COMP_DERIVED_CONTAINS:
5456       *st = ST_END_TYPE;
5457       target = " type";
5458       eos_ok = 0;
5459       break;
5460
5461     case COMP_BLOCK:
5462       *st = ST_END_BLOCK;
5463       target = " block";
5464       eos_ok = 0;
5465       break;
5466
5467     case COMP_IF:
5468       *st = ST_ENDIF;
5469       target = " if";
5470       eos_ok = 0;
5471       break;
5472
5473     case COMP_DO:
5474       *st = ST_ENDDO;
5475       target = " do";
5476       eos_ok = 0;
5477       break;
5478
5479     case COMP_CRITICAL:
5480       *st = ST_END_CRITICAL;
5481       target = " critical";
5482       eos_ok = 0;
5483       break;
5484
5485     case COMP_SELECT:
5486     case COMP_SELECT_TYPE:
5487       *st = ST_END_SELECT;
5488       target = " select";
5489       eos_ok = 0;
5490       break;
5491
5492     case COMP_FORALL:
5493       *st = ST_END_FORALL;
5494       target = " forall";
5495       eos_ok = 0;
5496       break;
5497
5498     case COMP_WHERE:
5499       *st = ST_END_WHERE;
5500       target = " where";
5501       eos_ok = 0;
5502       break;
5503
5504     case COMP_ENUM:
5505       *st = ST_END_ENUM;
5506       target = " enum";
5507       eos_ok = 0;
5508       last_initializer = NULL;
5509       set_enum_kind ();
5510       gfc_free_enum_history ();
5511       break;
5512
5513     default:
5514       gfc_error ("Unexpected END statement at %C");
5515       goto cleanup;
5516     }
5517
5518   if (gfc_match_eos () == MATCH_YES)
5519     {
5520       if (!eos_ok)
5521         {
5522           /* We would have required END [something].  */
5523           gfc_error ("%s statement expected at %L",
5524                      gfc_ascii_statement (*st), &old_loc);
5525           goto cleanup;
5526         }
5527
5528       return MATCH_YES;
5529     }
5530
5531   /* Verify that we've got the sort of end-block that we're expecting.  */
5532   if (gfc_match (target) != MATCH_YES)
5533     {
5534       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5535       goto cleanup;
5536     }
5537
5538   /* If we're at the end, make sure a block name wasn't required.  */
5539   if (gfc_match_eos () == MATCH_YES)
5540     {
5541
5542       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5543           && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5544           && *st != ST_END_CRITICAL)
5545         return MATCH_YES;
5546
5547       if (!block_name)
5548         return MATCH_YES;
5549
5550       gfc_error ("Expected block name of '%s' in %s statement at %C",
5551                  block_name, gfc_ascii_statement (*st));
5552
5553       return MATCH_ERROR;
5554     }
5555
5556   /* END INTERFACE has a special handler for its several possible endings.  */
5557   if (*st == ST_END_INTERFACE)
5558     return gfc_match_end_interface ();
5559
5560   /* We haven't hit the end of statement, so what is left must be an
5561      end-name.  */
5562   m = gfc_match_space ();
5563   if (m == MATCH_YES)
5564     m = gfc_match_name (name);
5565
5566   if (m == MATCH_NO)
5567     gfc_error ("Expected terminating name at %C");
5568   if (m != MATCH_YES)
5569     goto cleanup;
5570
5571   if (block_name == NULL)
5572     goto syntax;
5573
5574   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5575     {
5576       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5577                  gfc_ascii_statement (*st));
5578       goto cleanup;
5579     }
5580   /* Procedure pointer as function result.  */
5581   else if (strcmp (block_name, "ppr@") == 0
5582            && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5583     {
5584       gfc_error ("Expected label '%s' for %s statement at %C",
5585                  gfc_current_block ()->ns->proc_name->name,
5586                  gfc_ascii_statement (*st));
5587       goto cleanup;
5588     }
5589
5590   if (gfc_match_eos () == MATCH_YES)
5591     return MATCH_YES;
5592
5593 syntax:
5594   gfc_syntax_error (*st);
5595
5596 cleanup:
5597   gfc_current_locus = old_loc;
5598   return MATCH_ERROR;
5599 }
5600
5601
5602
5603 /***************** Attribute declaration statements ****************/
5604
5605 /* Set the attribute of a single variable.  */
5606
5607 static match
5608 attr_decl1 (void)
5609 {
5610   char name[GFC_MAX_SYMBOL_LEN + 1];
5611   gfc_array_spec *as;
5612   gfc_symbol *sym;
5613   locus var_locus;
5614   match m;
5615
5616   as = NULL;
5617
5618   m = gfc_match_name (name);
5619   if (m != MATCH_YES)
5620     goto cleanup;
5621
5622   if (find_special (name, &sym, false))
5623     return MATCH_ERROR;
5624
5625   var_locus = gfc_current_locus;
5626
5627   /* Deal with possible array specification for certain attributes.  */
5628   if (current_attr.dimension
5629       || current_attr.allocatable
5630       || current_attr.pointer
5631       || current_attr.target)
5632     {
5633       m = gfc_match_array_spec (&as);
5634       if (m == MATCH_ERROR)
5635         goto cleanup;
5636
5637       if (current_attr.dimension && m == MATCH_NO)
5638         {
5639           gfc_error ("Missing array specification at %L in DIMENSION "
5640                      "statement", &var_locus);
5641           m = MATCH_ERROR;
5642           goto cleanup;
5643         }
5644
5645       if (current_attr.dimension && sym->value)
5646         {
5647           gfc_error ("Dimensions specified for %s at %L after its "
5648                      "initialisation", sym->name, &var_locus);
5649           m = MATCH_ERROR;
5650           goto cleanup;
5651         }
5652
5653       if ((current_attr.allocatable || current_attr.pointer)
5654           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5655         {
5656           gfc_error ("Array specification must be deferred at %L", &var_locus);
5657           m = MATCH_ERROR;
5658           goto cleanup;
5659         }
5660     }
5661
5662   /* Update symbol table.  DIMENSION attribute is set in
5663      gfc_set_array_spec().  For CLASS variables, this must be applied
5664      to the first component, or '$data' field.  */
5665   if (sym->ts.type == BT_CLASS && sym->ts.u.derived)
5666     {
5667       gfc_component *comp;
5668       comp = gfc_find_component (sym->ts.u.derived, "$data", true, true);
5669       if (comp == NULL || gfc_copy_attr (&comp->attr, &current_attr,
5670                                          &var_locus) == FAILURE)
5671         {
5672           m = MATCH_ERROR;
5673           goto cleanup;
5674         }
5675       sym->attr.class_ok = (sym->attr.class_ok
5676                               || current_attr.allocatable
5677                               || current_attr.pointer);
5678     }
5679   else
5680     {
5681       if (current_attr.dimension == 0
5682             && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
5683         {
5684           m = MATCH_ERROR;
5685           goto cleanup;
5686         }
5687     }
5688
5689   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5690     {
5691       m = MATCH_ERROR;
5692       goto cleanup;
5693     }
5694
5695   if (sym->attr.cray_pointee && sym->as != NULL)
5696     {
5697       /* Fix the array spec.  */
5698       m = gfc_mod_pointee_as (sym->as);         
5699       if (m == MATCH_ERROR)
5700         goto cleanup;
5701     }
5702
5703   if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5704     {
5705       m = MATCH_ERROR;
5706       goto cleanup;
5707     }
5708
5709   if ((current_attr.external || current_attr.intrinsic)
5710       && sym->attr.flavor != FL_PROCEDURE
5711       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5712     {
5713       m = MATCH_ERROR;
5714       goto cleanup;
5715     }
5716
5717   add_hidden_procptr_result (sym);
5718
5719   return MATCH_YES;
5720
5721 cleanup:
5722   gfc_free_array_spec (as);
5723   return m;
5724 }
5725
5726
5727 /* Generic attribute declaration subroutine.  Used for attributes that
5728    just have a list of names.  */
5729
5730 static match
5731 attr_decl (void)
5732 {
5733   match m;
5734
5735   /* Gobble the optional double colon, by simply ignoring the result
5736      of gfc_match().  */
5737   gfc_match (" ::");
5738
5739   for (;;)
5740     {
5741       m = attr_decl1 ();
5742       if (m != MATCH_YES)
5743         break;
5744
5745       if (gfc_match_eos () == MATCH_YES)
5746         {
5747           m = MATCH_YES;
5748           break;
5749         }
5750
5751       if (gfc_match_char (',') != MATCH_YES)
5752         {
5753           gfc_error ("Unexpected character in variable list at %C");
5754           m = MATCH_ERROR;
5755           break;
5756         }
5757     }
5758
5759   return m;
5760 }
5761
5762
5763 /* This routine matches Cray Pointer declarations of the form:
5764    pointer ( <pointer>, <pointee> )
5765    or
5766    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5767    The pointer, if already declared, should be an integer.  Otherwise, we
5768    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
5769    be either a scalar, or an array declaration.  No space is allocated for
5770    the pointee.  For the statement
5771    pointer (ipt, ar(10))
5772    any subsequent uses of ar will be translated (in C-notation) as
5773    ar(i) => ((<type> *) ipt)(i)
5774    After gimplification, pointee variable will disappear in the code.  */
5775
5776 static match
5777 cray_pointer_decl (void)
5778 {
5779   match m;
5780   gfc_array_spec *as;
5781   gfc_symbol *cptr; /* Pointer symbol.  */
5782   gfc_symbol *cpte; /* Pointee symbol.  */
5783   locus var_locus;
5784   bool done = false;
5785
5786   while (!done)
5787     {
5788       if (gfc_match_char ('(') != MATCH_YES)
5789         {
5790           gfc_error ("Expected '(' at %C");
5791           return MATCH_ERROR;
5792         }
5793
5794       /* Match pointer.  */
5795       var_locus = gfc_current_locus;
5796       gfc_clear_attr (&current_attr);
5797       gfc_add_cray_pointer (&current_attr, &var_locus);
5798       current_ts.type = BT_INTEGER;
5799       current_ts.kind = gfc_index_integer_kind;
5800
5801       m = gfc_match_symbol (&cptr, 0);
5802       if (m != MATCH_YES)
5803         {
5804           gfc_error ("Expected variable name at %C");
5805           return m;
5806         }
5807
5808       if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5809         return MATCH_ERROR;
5810
5811       gfc_set_sym_referenced (cptr);
5812
5813       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
5814         {
5815           cptr->ts.type = BT_INTEGER;
5816           cptr->ts.kind = gfc_index_integer_kind;
5817         }
5818       else if (cptr->ts.type != BT_INTEGER)
5819         {
5820           gfc_error ("Cray pointer at %C must be an integer");
5821           return MATCH_ERROR;
5822         }
5823       else if (cptr->ts.kind < gfc_index_integer_kind)
5824         gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5825                      " memory addresses require %d bytes",
5826                      cptr->ts.kind, gfc_index_integer_kind);
5827
5828       if (gfc_match_char (',') != MATCH_YES)
5829         {
5830           gfc_error ("Expected \",\" at %C");
5831           return MATCH_ERROR;
5832         }
5833
5834       /* Match Pointee.  */
5835       var_locus = gfc_current_locus;
5836       gfc_clear_attr (&current_attr);
5837       gfc_add_cray_pointee (&current_attr, &var_locus);
5838       current_ts.type = BT_UNKNOWN;
5839       current_ts.kind = 0;
5840
5841       m = gfc_match_symbol (&cpte, 0);
5842       if (m != MATCH_YES)
5843         {
5844           gfc_error ("Expected variable name at %C");
5845           return m;
5846         }
5847
5848       /* Check for an optional array spec.  */
5849       m = gfc_match_array_spec (&as);
5850       if (m == MATCH_ERROR)
5851         {
5852           gfc_free_array_spec (as);
5853           return m;
5854         }
5855       else if (m == MATCH_NO)
5856         {
5857           gfc_free_array_spec (as);
5858           as = NULL;
5859         }   
5860
5861       if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5862         return MATCH_ERROR;
5863
5864       gfc_set_sym_referenced (cpte);
5865
5866       if (cpte->as == NULL)
5867         {
5868           if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5869             gfc_internal_error ("Couldn't set Cray pointee array spec.");
5870         }
5871       else if (as != NULL)
5872         {
5873           gfc_error ("Duplicate array spec for Cray pointee at %C");
5874           gfc_free_array_spec (as);
5875           return MATCH_ERROR;
5876         }
5877       
5878       as = NULL;
5879     
5880       if (cpte->as != NULL)
5881         {
5882           /* Fix array spec.  */
5883           m = gfc_mod_pointee_as (cpte->as);
5884           if (m == MATCH_ERROR)
5885             return m;
5886         } 
5887    
5888       /* Point the Pointee at the Pointer.  */
5889       cpte->cp_pointer = cptr;
5890
5891       if (gfc_match_char (')') != MATCH_YES)
5892         {
5893           gfc_error ("Expected \")\" at %C");
5894           return MATCH_ERROR;    
5895         }
5896       m = gfc_match_char (',');
5897       if (m != MATCH_YES)
5898         done = true; /* Stop searching for more declarations.  */
5899
5900     }
5901   
5902   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
5903       || gfc_match_eos () != MATCH_YES)
5904     {
5905       gfc_error ("Expected \",\" or end of statement at %C");
5906       return MATCH_ERROR;
5907     }
5908   return MATCH_YES;
5909 }
5910
5911
5912 match
5913 gfc_match_external (void)
5914 {
5915
5916   gfc_clear_attr (&current_attr);
5917   current_attr.external = 1;
5918
5919   return attr_decl ();
5920 }
5921
5922
5923 match
5924 gfc_match_intent (void)
5925 {
5926   sym_intent intent;
5927
5928   /* This is not allowed within a BLOCK construct!  */
5929   if (gfc_current_state () == COMP_BLOCK)
5930     {
5931       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
5932       return MATCH_ERROR;
5933     }
5934
5935   intent = match_intent_spec ();
5936   if (intent == INTENT_UNKNOWN)
5937     return MATCH_ERROR;
5938
5939   gfc_clear_attr (&current_attr);
5940   current_attr.intent = intent;
5941
5942   return attr_decl ();
5943 }
5944
5945
5946 match
5947 gfc_match_intrinsic (void)
5948 {
5949
5950   gfc_clear_attr (&current_attr);
5951   current_attr.intrinsic = 1;
5952
5953   return attr_decl ();
5954 }
5955
5956
5957 match
5958 gfc_match_optional (void)
5959 {
5960   /* This is not allowed within a BLOCK construct!  */
5961   if (gfc_current_state () == COMP_BLOCK)
5962     {
5963       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
5964       return MATCH_ERROR;
5965     }
5966
5967   gfc_clear_attr (&current_attr);
5968   current_attr.optional = 1;
5969
5970   return attr_decl ();
5971 }
5972
5973
5974 match
5975 gfc_match_pointer (void)
5976 {
5977   gfc_gobble_whitespace ();
5978   if (gfc_peek_ascii_char () == '(')
5979     {
5980       if (!gfc_option.flag_cray_pointer)
5981         {
5982           gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5983                      "flag");
5984           return MATCH_ERROR;
5985         }
5986       return cray_pointer_decl ();
5987     }
5988   else
5989     {
5990       gfc_clear_attr (&current_attr);
5991       current_attr.pointer = 1;
5992     
5993       return attr_decl ();
5994     }
5995 }
5996
5997
5998 match
5999 gfc_match_allocatable (void)
6000 {
6001   gfc_clear_attr (&current_attr);
6002   current_attr.allocatable = 1;
6003
6004   return attr_decl ();
6005 }
6006
6007
6008 match
6009 gfc_match_dimension (void)
6010 {
6011   gfc_clear_attr (&current_attr);
6012   current_attr.dimension = 1;
6013
6014   return attr_decl ();
6015 }
6016
6017
6018 match
6019 gfc_match_target (void)
6020 {
6021   gfc_clear_attr (&current_attr);
6022   current_attr.target = 1;
6023
6024   return attr_decl ();
6025 }
6026
6027
6028 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6029    statement.  */
6030
6031 static match
6032 access_attr_decl (gfc_statement st)
6033 {
6034   char name[GFC_MAX_SYMBOL_LEN + 1];
6035   interface_type type;
6036   gfc_user_op *uop;
6037   gfc_symbol *sym;
6038   gfc_intrinsic_op op;
6039   match m;
6040
6041   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6042     goto done;
6043
6044   for (;;)
6045     {
6046       m = gfc_match_generic_spec (&type, name, &op);
6047       if (m == MATCH_NO)
6048         goto syntax;
6049       if (m == MATCH_ERROR)
6050         return MATCH_ERROR;
6051
6052       switch (type)
6053         {
6054         case INTERFACE_NAMELESS:
6055         case INTERFACE_ABSTRACT:
6056           goto syntax;
6057
6058         case INTERFACE_GENERIC:
6059           if (gfc_get_symbol (name, NULL, &sym))
6060             goto done;
6061
6062           if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6063                                           ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6064                               sym->name, NULL) == FAILURE)
6065             return MATCH_ERROR;
6066
6067           break;
6068
6069         case INTERFACE_INTRINSIC_OP:
6070           if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6071             {
6072               gfc_current_ns->operator_access[op] =
6073                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6074             }
6075           else
6076             {
6077               gfc_error ("Access specification of the %s operator at %C has "
6078                          "already been specified", gfc_op2string (op));
6079               goto done;
6080             }
6081
6082           break;
6083
6084         case INTERFACE_USER_OP:
6085           uop = gfc_get_uop (name);
6086
6087           if (uop->access == ACCESS_UNKNOWN)
6088             {
6089               uop->access = (st == ST_PUBLIC)
6090                           ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6091             }
6092           else
6093             {
6094               gfc_error ("Access specification of the .%s. operator at %C "
6095                          "has already been specified", sym->name);
6096               goto done;
6097             }
6098
6099           break;
6100         }
6101
6102       if (gfc_match_char (',') == MATCH_NO)
6103         break;
6104     }
6105
6106   if (gfc_match_eos () != MATCH_YES)
6107     goto syntax;
6108   return MATCH_YES;
6109
6110 syntax:
6111   gfc_syntax_error (st);
6112
6113 done:
6114   return MATCH_ERROR;
6115 }
6116
6117
6118 match
6119 gfc_match_protected (void)
6120 {
6121   gfc_symbol *sym;
6122   match m;
6123
6124   if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6125     {
6126        gfc_error ("PROTECTED at %C only allowed in specification "
6127                   "part of a module");
6128        return MATCH_ERROR;
6129
6130     }
6131
6132   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6133       == FAILURE)
6134     return MATCH_ERROR;
6135
6136   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6137     {
6138       return MATCH_ERROR;
6139     }
6140
6141   if (gfc_match_eos () == MATCH_YES)
6142     goto syntax;
6143
6144   for(;;)
6145     {
6146       m = gfc_match_symbol (&sym, 0);
6147       switch (m)
6148         {
6149         case MATCH_YES:
6150           if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6151               == FAILURE)
6152             return MATCH_ERROR;
6153           goto next_item;
6154
6155         case MATCH_NO:
6156           break;
6157
6158         case MATCH_ERROR:
6159           return MATCH_ERROR;
6160         }
6161
6162     next_item:
6163       if (gfc_match_eos () == MATCH_YES)
6164         break;
6165       if (gfc_match_char (',') != MATCH_YES)
6166         goto syntax;
6167     }
6168
6169   return MATCH_YES;
6170
6171 syntax:
6172   gfc_error ("Syntax error in PROTECTED statement at %C");
6173   return MATCH_ERROR;
6174 }
6175
6176
6177 /* The PRIVATE statement is a bit weird in that it can be an attribute
6178    declaration, but also works as a standalone statement inside of a
6179    type declaration or a module.  */
6180
6181 match
6182 gfc_match_private (gfc_statement *st)
6183 {
6184
6185   if (gfc_match ("private") != MATCH_YES)
6186     return MATCH_NO;
6187
6188   if (gfc_current_state () != COMP_MODULE
6189       && !(gfc_current_state () == COMP_DERIVED
6190            && gfc_state_stack->previous
6191            && gfc_state_stack->previous->state == COMP_MODULE)
6192       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6193            && gfc_state_stack->previous && gfc_state_stack->previous->previous
6194            && gfc_state_stack->previous->previous->state == COMP_MODULE))
6195     {
6196       gfc_error ("PRIVATE statement at %C is only allowed in the "
6197                  "specification part of a module");
6198       return MATCH_ERROR;
6199     }
6200
6201   if (gfc_current_state () == COMP_DERIVED)
6202     {
6203       if (gfc_match_eos () == MATCH_YES)
6204         {
6205           *st = ST_PRIVATE;
6206           return MATCH_YES;
6207         }
6208
6209       gfc_syntax_error (ST_PRIVATE);
6210       return MATCH_ERROR;
6211     }
6212
6213   if (gfc_match_eos () == MATCH_YES)
6214     {
6215       *st = ST_PRIVATE;
6216       return MATCH_YES;
6217     }
6218
6219   *st = ST_ATTR_DECL;
6220   return access_attr_decl (ST_PRIVATE);
6221 }
6222
6223
6224 match
6225 gfc_match_public (gfc_statement *st)
6226 {
6227
6228   if (gfc_match ("public") != MATCH_YES)
6229     return MATCH_NO;
6230
6231   if (gfc_current_state () != COMP_MODULE)
6232     {
6233       gfc_error ("PUBLIC statement at %C is only allowed in the "
6234                  "specification part of a module");
6235       return MATCH_ERROR;
6236     }
6237
6238   if (gfc_match_eos () == MATCH_YES)
6239     {
6240       *st = ST_PUBLIC;
6241       return MATCH_YES;
6242     }
6243
6244   *st = ST_ATTR_DECL;
6245   return access_attr_decl (ST_PUBLIC);
6246 }
6247
6248
6249 /* Workhorse for gfc_match_parameter.  */
6250
6251 static match
6252 do_parm (void)
6253 {
6254   gfc_symbol *sym;
6255   gfc_expr *init;
6256   match m;
6257   gfc_try t;
6258
6259   m = gfc_match_symbol (&sym, 0);
6260   if (m == MATCH_NO)
6261     gfc_error ("Expected variable name at %C in PARAMETER statement");
6262
6263   if (m != MATCH_YES)
6264     return m;
6265
6266   if (gfc_match_char ('=') == MATCH_NO)
6267     {
6268       gfc_error ("Expected = sign in PARAMETER statement at %C");
6269       return MATCH_ERROR;
6270     }
6271
6272   m = gfc_match_init_expr (&init);
6273   if (m == MATCH_NO)
6274     gfc_error ("Expected expression at %C in PARAMETER statement");
6275   if (m != MATCH_YES)
6276     return m;
6277
6278   if (sym->ts.type == BT_UNKNOWN
6279       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6280     {
6281       m = MATCH_ERROR;
6282       goto cleanup;
6283     }
6284
6285   if (gfc_check_assign_symbol (sym, init) == FAILURE
6286       || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6287     {
6288       m = MATCH_ERROR;
6289       goto cleanup;
6290     }
6291
6292   if (sym->value)
6293     {
6294       gfc_error ("Initializing already initialized variable at %C");
6295       m = MATCH_ERROR;
6296       goto cleanup;
6297     }
6298
6299   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6300   return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6301
6302 cleanup:
6303   gfc_free_expr (init);
6304   return m;
6305 }
6306
6307
6308 /* Match a parameter statement, with the weird syntax that these have.  */
6309
6310 match
6311 gfc_match_parameter (void)
6312 {
6313   match m;
6314
6315   if (gfc_match_char ('(') == MATCH_NO)
6316     return MATCH_NO;
6317
6318   for (;;)
6319     {
6320       m = do_parm ();
6321       if (m != MATCH_YES)
6322         break;
6323
6324       if (gfc_match (" )%t") == MATCH_YES)
6325         break;
6326
6327       if (gfc_match_char (',') != MATCH_YES)
6328         {
6329           gfc_error ("Unexpected characters in PARAMETER statement at %C");
6330           m = MATCH_ERROR;
6331           break;
6332         }
6333     }
6334
6335   return m;
6336 }
6337
6338
6339 /* Save statements have a special syntax.  */
6340
6341 match
6342 gfc_match_save (void)
6343 {
6344   char n[GFC_MAX_SYMBOL_LEN+1];
6345   gfc_common_head *c;
6346   gfc_symbol *sym;
6347   match m;
6348
6349   if (gfc_match_eos () == MATCH_YES)
6350     {
6351       if (gfc_current_ns->seen_save)
6352         {
6353           if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6354                               "follows previous SAVE statement")
6355               == FAILURE)
6356             return MATCH_ERROR;
6357         }
6358
6359       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6360       return MATCH_YES;
6361     }
6362
6363   if (gfc_current_ns->save_all)
6364     {
6365       if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6366                           "blanket SAVE statement")
6367           == FAILURE)
6368         return MATCH_ERROR;
6369     }
6370
6371   gfc_match (" ::");
6372
6373   for (;;)
6374     {
6375       m = gfc_match_symbol (&sym, 0);
6376       switch (m)
6377         {
6378         case MATCH_YES:
6379           if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6380               == FAILURE)
6381             return MATCH_ERROR;
6382           goto next_item;
6383
6384         case MATCH_NO:
6385           break;
6386
6387         case MATCH_ERROR:
6388           return MATCH_ERROR;
6389         }
6390
6391       m = gfc_match (" / %n /", &n);
6392       if (m == MATCH_ERROR)
6393         return MATCH_ERROR;
6394       if (m == MATCH_NO)
6395         goto syntax;
6396
6397       c = gfc_get_common (n, 0);
6398       c->saved = 1;
6399
6400       gfc_current_ns->seen_save = 1;
6401
6402     next_item:
6403       if (gfc_match_eos () == MATCH_YES)
6404         break;
6405       if (gfc_match_char (',') != MATCH_YES)
6406         goto syntax;
6407     }
6408
6409   return MATCH_YES;
6410
6411 syntax:
6412   gfc_error ("Syntax error in SAVE statement at %C");
6413   return MATCH_ERROR;
6414 }
6415
6416
6417 match
6418 gfc_match_value (void)
6419 {
6420   gfc_symbol *sym;
6421   match m;
6422
6423   /* This is not allowed within a BLOCK construct!  */
6424   if (gfc_current_state () == COMP_BLOCK)
6425     {
6426       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6427       return MATCH_ERROR;
6428     }
6429
6430   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6431       == FAILURE)
6432     return MATCH_ERROR;
6433
6434   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6435     {
6436       return MATCH_ERROR;
6437     }
6438
6439   if (gfc_match_eos () == MATCH_YES)
6440     goto syntax;
6441
6442   for(;;)
6443     {
6444       m = gfc_match_symbol (&sym, 0);
6445       switch (m)
6446         {
6447         case MATCH_YES:
6448           if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6449               == FAILURE)
6450             return MATCH_ERROR;
6451           goto next_item;
6452
6453         case MATCH_NO:
6454           break;
6455
6456         case MATCH_ERROR:
6457           return MATCH_ERROR;
6458         }
6459
6460     next_item:
6461       if (gfc_match_eos () == MATCH_YES)
6462         break;
6463       if (gfc_match_char (',') != MATCH_YES)
6464         goto syntax;
6465     }
6466
6467   return MATCH_YES;
6468
6469 syntax:
6470   gfc_error ("Syntax error in VALUE statement at %C");
6471   return MATCH_ERROR;
6472 }
6473
6474
6475 match
6476 gfc_match_volatile (void)
6477 {
6478   gfc_symbol *sym;
6479   match m;
6480
6481   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6482       == FAILURE)
6483     return MATCH_ERROR;
6484
6485   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6486     {
6487       return MATCH_ERROR;
6488     }
6489
6490   if (gfc_match_eos () == MATCH_YES)
6491     goto syntax;
6492
6493   for(;;)
6494     {
6495       /* VOLATILE is special because it can be added to host-associated 
6496          symbols locally.  */
6497       m = gfc_match_symbol (&sym, 1);
6498       switch (m)
6499         {
6500         case MATCH_YES:
6501           if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6502               == FAILURE)
6503             return MATCH_ERROR;
6504           goto next_item;
6505
6506         case MATCH_NO:
6507           break;
6508
6509         case MATCH_ERROR:
6510           return MATCH_ERROR;
6511         }
6512
6513     next_item:
6514       if (gfc_match_eos () == MATCH_YES)
6515         break;
6516       if (gfc_match_char (',') != MATCH_YES)
6517         goto syntax;
6518     }
6519
6520   return MATCH_YES;
6521
6522 syntax:
6523   gfc_error ("Syntax error in VOLATILE statement at %C");
6524   return MATCH_ERROR;
6525 }
6526
6527
6528 match
6529 gfc_match_asynchronous (void)
6530 {
6531   gfc_symbol *sym;
6532   match m;
6533
6534   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6535       == FAILURE)
6536     return MATCH_ERROR;
6537
6538   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6539     {
6540       return MATCH_ERROR;
6541     }
6542
6543   if (gfc_match_eos () == MATCH_YES)
6544     goto syntax;
6545
6546   for(;;)
6547     {
6548       /* ASYNCHRONOUS is special because it can be added to host-associated 
6549          symbols locally.  */
6550       m = gfc_match_symbol (&sym, 1);
6551       switch (m)
6552         {
6553         case MATCH_YES:
6554           if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6555               == FAILURE)
6556             return MATCH_ERROR;
6557           goto next_item;
6558
6559         case MATCH_NO:
6560           break;
6561
6562         case MATCH_ERROR:
6563           return MATCH_ERROR;
6564         }
6565
6566     next_item:
6567       if (gfc_match_eos () == MATCH_YES)
6568         break;
6569       if (gfc_match_char (',') != MATCH_YES)
6570         goto syntax;
6571     }
6572
6573   return MATCH_YES;
6574
6575 syntax:
6576   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6577   return MATCH_ERROR;
6578 }
6579
6580
6581 /* Match a module procedure statement.  Note that we have to modify
6582    symbols in the parent's namespace because the current one was there
6583    to receive symbols that are in an interface's formal argument list.  */
6584
6585 match
6586 gfc_match_modproc (void)
6587 {
6588   char name[GFC_MAX_SYMBOL_LEN + 1];
6589   gfc_symbol *sym;
6590   match m;
6591   gfc_namespace *module_ns;
6592   gfc_interface *old_interface_head, *interface;
6593
6594   if (gfc_state_stack->state != COMP_INTERFACE
6595       || gfc_state_stack->previous == NULL
6596       || current_interface.type == INTERFACE_NAMELESS
6597       || current_interface.type == INTERFACE_ABSTRACT)
6598     {
6599       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6600                  "interface");
6601       return MATCH_ERROR;
6602     }
6603
6604   module_ns = gfc_current_ns->parent;
6605   for (; module_ns; module_ns = module_ns->parent)
6606     if (module_ns->proc_name->attr.flavor == FL_MODULE
6607         || module_ns->proc_name->attr.flavor == FL_PROGRAM
6608         || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
6609             && !module_ns->proc_name->attr.contained))
6610       break;
6611
6612   if (module_ns == NULL)
6613     return MATCH_ERROR;
6614
6615   /* Store the current state of the interface. We will need it if we
6616      end up with a syntax error and need to recover.  */
6617   old_interface_head = gfc_current_interface_head ();
6618
6619   for (;;)
6620     {
6621       locus old_locus = gfc_current_locus;
6622       bool last = false;
6623
6624       m = gfc_match_name (name);
6625       if (m == MATCH_NO)
6626         goto syntax;
6627       if (m != MATCH_YES)
6628         return MATCH_ERROR;
6629
6630       /* Check for syntax error before starting to add symbols to the
6631          current namespace.  */
6632       if (gfc_match_eos () == MATCH_YES)
6633         last = true;
6634       if (!last && gfc_match_char (',') != MATCH_YES)
6635         goto syntax;
6636
6637       /* Now we're sure the syntax is valid, we process this item
6638          further.  */
6639       if (gfc_get_symbol (name, module_ns, &sym))
6640         return MATCH_ERROR;
6641
6642       if (sym->attr.intrinsic)
6643         {
6644           gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
6645                      "PROCEDURE", &old_locus);
6646           return MATCH_ERROR;
6647         }
6648
6649       if (sym->attr.proc != PROC_MODULE
6650           && gfc_add_procedure (&sym->attr, PROC_MODULE,
6651                                 sym->name, NULL) == FAILURE)
6652         return MATCH_ERROR;
6653
6654       if (gfc_add_interface (sym) == FAILURE)
6655         return MATCH_ERROR;
6656
6657       sym->attr.mod_proc = 1;
6658       sym->declared_at = old_locus;
6659
6660       if (last)
6661         break;
6662     }
6663
6664   return MATCH_YES;
6665
6666 syntax:
6667   /* Restore the previous state of the interface.  */
6668   interface = gfc_current_interface_head ();
6669   gfc_set_current_interface_head (old_interface_head);
6670
6671   /* Free the new interfaces.  */
6672   while (interface != old_interface_head)
6673   {
6674     gfc_interface *i = interface->next;
6675     gfc_free (interface);
6676     interface = i;
6677   }
6678
6679   /* And issue a syntax error.  */
6680   gfc_syntax_error (ST_MODULE_PROC);
6681   return MATCH_ERROR;
6682 }
6683
6684
6685 /* Check a derived type that is being extended.  */
6686 static gfc_symbol*
6687 check_extended_derived_type (char *name)
6688 {
6689   gfc_symbol *extended;
6690
6691   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6692     {
6693       gfc_error ("Ambiguous symbol in TYPE definition at %C");
6694       return NULL;
6695     }
6696
6697   if (!extended)
6698     {
6699       gfc_error ("No such symbol in TYPE definition at %C");
6700       return NULL;
6701     }
6702
6703   if (extended->attr.flavor != FL_DERIVED)
6704     {
6705       gfc_error ("'%s' in EXTENDS expression at %C is not a "
6706                  "derived type", name);
6707       return NULL;
6708     }
6709
6710   if (extended->attr.is_bind_c)
6711     {
6712       gfc_error ("'%s' cannot be extended at %C because it "
6713                  "is BIND(C)", extended->name);
6714       return NULL;
6715     }
6716
6717   if (extended->attr.sequence)
6718     {
6719       gfc_error ("'%s' cannot be extended at %C because it "
6720                  "is a SEQUENCE type", extended->name);
6721       return NULL;
6722     }
6723
6724   return extended;
6725 }
6726
6727
6728 /* Match the optional attribute specifiers for a type declaration.
6729    Return MATCH_ERROR if an error is encountered in one of the handled
6730    attributes (public, private, bind(c)), MATCH_NO if what's found is
6731    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
6732    checking on attribute conflicts needs to be done.  */
6733
6734 match
6735 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6736 {
6737   /* See if the derived type is marked as private.  */
6738   if (gfc_match (" , private") == MATCH_YES)
6739     {
6740       if (gfc_current_state () != COMP_MODULE)
6741         {
6742           gfc_error ("Derived type at %C can only be PRIVATE in the "
6743                      "specification part of a module");
6744           return MATCH_ERROR;
6745         }
6746
6747       if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6748         return MATCH_ERROR;
6749     }
6750   else if (gfc_match (" , public") == MATCH_YES)
6751     {
6752       if (gfc_current_state () != COMP_MODULE)
6753         {
6754           gfc_error ("Derived type at %C can only be PUBLIC in the "
6755                      "specification part of a module");
6756           return MATCH_ERROR;
6757         }
6758
6759       if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6760         return MATCH_ERROR;
6761     }
6762   else if (gfc_match (" , bind ( c )") == MATCH_YES)
6763     {
6764       /* If the type is defined to be bind(c) it then needs to make
6765          sure that all fields are interoperable.  This will
6766          need to be a semantic check on the finished derived type.
6767          See 15.2.3 (lines 9-12) of F2003 draft.  */
6768       if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6769         return MATCH_ERROR;
6770
6771       /* TODO: attr conflicts need to be checked, probably in symbol.c.  */
6772     }
6773   else if (gfc_match (" , abstract") == MATCH_YES)
6774     {
6775       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6776             == FAILURE)
6777         return MATCH_ERROR;
6778
6779       if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6780         return MATCH_ERROR;
6781     }
6782   else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6783     {
6784       if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
6785         return MATCH_ERROR;
6786     }
6787   else
6788     return MATCH_NO;
6789
6790   /* If we get here, something matched.  */
6791   return MATCH_YES;
6792 }
6793
6794
6795 /* Assign a hash value for a derived type. The algorithm is that of
6796    SDBM. The hashed string is '[module_name #] derived_name'.  */
6797 static unsigned int
6798 hash_value (gfc_symbol *sym)
6799 {
6800   unsigned int hash = 0;
6801   const char *c;
6802   int i, len;
6803
6804   /* Hash of the module or procedure name.  */
6805   if (sym->module != NULL)
6806     c = sym->module;
6807   else if (sym->ns && sym->ns->proc_name
6808              && sym->ns->proc_name->attr.flavor == FL_MODULE)
6809     c = sym->ns->proc_name->name;
6810   else
6811     c = NULL;
6812
6813   if (c)
6814     { 
6815       len = strlen (c);
6816       for (i = 0; i < len; i++, c++)
6817         hash =  (hash << 6) + (hash << 16) - hash + (*c);
6818
6819       /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'.  */ 
6820       hash =  (hash << 6) + (hash << 16) - hash + '#';
6821     }
6822
6823   /* Hash of the derived type name.  */
6824   len = strlen (sym->name);
6825   c = sym->name;
6826   for (i = 0; i < len; i++, c++)
6827     hash = (hash << 6) + (hash << 16) - hash + (*c);
6828
6829   /* Return the hash but take the modulus for the sake of module read,
6830      even though this slightly increases the chance of collision.  */
6831   return (hash % 100000000);
6832 }
6833
6834
6835 /* Match the beginning of a derived type declaration.  If a type name
6836    was the result of a function, then it is possible to have a symbol
6837    already to be known as a derived type yet have no components.  */
6838
6839 match
6840 gfc_match_derived_decl (void)
6841 {
6842   char name[GFC_MAX_SYMBOL_LEN + 1];
6843   char parent[GFC_MAX_SYMBOL_LEN + 1];
6844   symbol_attribute attr;
6845   gfc_symbol *sym;
6846   gfc_symbol *extended;
6847   match m;
6848   match is_type_attr_spec = MATCH_NO;
6849   bool seen_attr = false;
6850
6851   if (gfc_current_state () == COMP_DERIVED)
6852     return MATCH_NO;
6853
6854   name[0] = '\0';
6855   parent[0] = '\0';
6856   gfc_clear_attr (&attr);
6857   extended = NULL;
6858
6859   do
6860     {
6861       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
6862       if (is_type_attr_spec == MATCH_ERROR)
6863         return MATCH_ERROR;
6864       if (is_type_attr_spec == MATCH_YES)
6865         seen_attr = true;
6866     } while (is_type_attr_spec == MATCH_YES);
6867
6868   /* Deal with derived type extensions.  The extension attribute has
6869      been added to 'attr' but now the parent type must be found and
6870      checked.  */
6871   if (parent[0])
6872     extended = check_extended_derived_type (parent);
6873
6874   if (parent[0] && !extended)
6875     return MATCH_ERROR;
6876
6877   if (gfc_match (" ::") != MATCH_YES && seen_attr)
6878     {
6879       gfc_error ("Expected :: in TYPE definition at %C");
6880       return MATCH_ERROR;
6881     }
6882
6883   m = gfc_match (" %n%t", name);
6884   if (m != MATCH_YES)
6885     return m;
6886
6887   /* Make sure the name is not the name of an intrinsic type.  */
6888   if (gfc_is_intrinsic_typename (name))
6889     {
6890       gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6891                  "type", name);
6892       return MATCH_ERROR;
6893     }
6894
6895   if (gfc_get_symbol (name, NULL, &sym))
6896     return MATCH_ERROR;
6897
6898   if (sym->ts.type != BT_UNKNOWN)
6899     {
6900       gfc_error ("Derived type name '%s' at %C already has a basic type "
6901                  "of %s", sym->name, gfc_typename (&sym->ts));
6902       return MATCH_ERROR;
6903     }
6904
6905   /* The symbol may already have the derived attribute without the
6906      components.  The ways this can happen is via a function
6907      definition, an INTRINSIC statement or a subtype in another
6908      derived type that is a pointer.  The first part of the AND clause
6909      is true if the symbol is not the return value of a function.  */
6910   if (sym->attr.flavor != FL_DERIVED
6911       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6912     return MATCH_ERROR;
6913
6914   if (sym->components != NULL || sym->attr.zero_comp)
6915     {
6916       gfc_error ("Derived type definition of '%s' at %C has already been "
6917                  "defined", sym->name);
6918       return MATCH_ERROR;
6919     }
6920
6921   if (attr.access != ACCESS_UNKNOWN
6922       && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6923     return MATCH_ERROR;
6924
6925   /* See if the derived type was labeled as bind(c).  */
6926   if (attr.is_bind_c != 0)
6927     sym->attr.is_bind_c = attr.is_bind_c;
6928
6929   /* Construct the f2k_derived namespace if it is not yet there.  */
6930   if (!sym->f2k_derived)
6931     sym->f2k_derived = gfc_get_namespace (NULL, 0);
6932   
6933   if (extended && !sym->components)
6934     {
6935       gfc_component *p;
6936       gfc_symtree *st;
6937
6938       /* Add the extended derived type as the first component.  */
6939       gfc_add_component (sym, parent, &p);
6940       extended->refs++;
6941       gfc_set_sym_referenced (extended);
6942
6943       p->ts.type = BT_DERIVED;
6944       p->ts.u.derived = extended;
6945       p->initializer = gfc_default_initializer (&p->ts);
6946       
6947       /* Set extension level.  */
6948       if (extended->attr.extension == 255)
6949         {
6950           /* Since the extension field is 8 bit wide, we can only have
6951              up to 255 extension levels.  */
6952           gfc_error ("Maximum extension level reached with type '%s' at %L",
6953                      extended->name, &extended->declared_at);
6954           return MATCH_ERROR;
6955         }
6956       sym->attr.extension = extended->attr.extension + 1;
6957
6958       /* Provide the links between the extended type and its extension.  */
6959       if (!extended->f2k_derived)
6960         extended->f2k_derived = gfc_get_namespace (NULL, 0);
6961       st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6962       st->n.sym = sym;
6963     }
6964
6965   if (!sym->hash_value)
6966     /* Set the hash for the compound name for this type.  */
6967     sym->hash_value = hash_value (sym);
6968
6969   /* Take over the ABSTRACT attribute.  */
6970   sym->attr.abstract = attr.abstract;
6971
6972   gfc_new_block = sym;
6973
6974   return MATCH_YES;
6975 }
6976
6977
6978 /* Cray Pointees can be declared as: 
6979       pointer (ipt, a (n,m,...,*))  */
6980
6981 match
6982 gfc_mod_pointee_as (gfc_array_spec *as)
6983 {
6984   as->cray_pointee = true; /* This will be useful to know later.  */
6985   if (as->type == AS_ASSUMED_SIZE)
6986     as->cp_was_assumed = true;
6987   else if (as->type == AS_ASSUMED_SHAPE)
6988     {
6989       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6990       return MATCH_ERROR;
6991     }
6992   return MATCH_YES;
6993 }
6994
6995
6996 /* Match the enum definition statement, here we are trying to match 
6997    the first line of enum definition statement.  
6998    Returns MATCH_YES if match is found.  */
6999
7000 match
7001 gfc_match_enum (void)
7002 {
7003   match m;
7004   
7005   m = gfc_match_eos ();
7006   if (m != MATCH_YES)
7007     return m;
7008
7009   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7010       == FAILURE)
7011     return MATCH_ERROR;
7012
7013   return MATCH_YES;
7014 }
7015
7016
7017 /* Returns an initializer whose value is one higher than the value of the
7018    LAST_INITIALIZER argument.  If the argument is NULL, the
7019    initializers value will be set to zero.  The initializer's kind
7020    will be set to gfc_c_int_kind.
7021
7022    If -fshort-enums is given, the appropriate kind will be selected
7023    later after all enumerators have been parsed.  A warning is issued
7024    here if an initializer exceeds gfc_c_int_kind.  */
7025
7026 static gfc_expr *
7027 enum_initializer (gfc_expr *last_initializer, locus where)
7028 {
7029   gfc_expr *result;
7030
7031   result = gfc_get_expr ();
7032   result->expr_type = EXPR_CONSTANT;
7033   result->ts.type = BT_INTEGER;
7034   result->ts.kind = gfc_c_int_kind;
7035   result->where = where;
7036
7037   mpz_init (result->value.integer);
7038
7039   if (last_initializer != NULL)
7040     {
7041       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7042       result->where = last_initializer->where;
7043
7044       if (gfc_check_integer_range (result->value.integer,
7045              gfc_c_int_kind) != ARITH_OK)
7046         {
7047           gfc_error ("Enumerator exceeds the C integer type at %C");
7048           return NULL;
7049         }
7050     }
7051   else
7052     {
7053       /* Control comes here, if it's the very first enumerator and no
7054          initializer has been given.  It will be initialized to zero.  */
7055       mpz_set_si (result->value.integer, 0);
7056     }
7057
7058   return result;
7059 }
7060
7061
7062 /* Match a variable name with an optional initializer.  When this
7063    subroutine is called, a variable is expected to be parsed next.
7064    Depending on what is happening at the moment, updates either the
7065    symbol table or the current interface.  */
7066
7067 static match
7068 enumerator_decl (void)
7069 {
7070   char name[GFC_MAX_SYMBOL_LEN + 1];
7071   gfc_expr *initializer;
7072   gfc_array_spec *as = NULL;
7073   gfc_symbol *sym;
7074   locus var_locus;
7075   match m;
7076   gfc_try t;
7077   locus old_locus;
7078
7079   initializer = NULL;
7080   old_locus = gfc_current_locus;
7081
7082   /* When we get here, we've just matched a list of attributes and
7083      maybe a type and a double colon.  The next thing we expect to see
7084      is the name of the symbol.  */
7085   m = gfc_match_name (name);
7086   if (m != MATCH_YES)
7087     goto cleanup;
7088
7089   var_locus = gfc_current_locus;
7090
7091   /* OK, we've successfully matched the declaration.  Now put the
7092      symbol in the current namespace. If we fail to create the symbol,
7093      bail out.  */
7094   if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
7095     {
7096       m = MATCH_ERROR;
7097       goto cleanup;
7098     }
7099
7100   /* The double colon must be present in order to have initializers.
7101      Otherwise the statement is ambiguous with an assignment statement.  */
7102   if (colon_seen)
7103     {
7104       if (gfc_match_char ('=') == MATCH_YES)
7105         {
7106           m = gfc_match_init_expr (&initializer);
7107           if (m == MATCH_NO)
7108             {
7109               gfc_error ("Expected an initialization expression at %C");
7110               m = MATCH_ERROR;
7111             }
7112
7113           if (m != MATCH_YES)
7114             goto cleanup;
7115         }
7116     }
7117
7118   /* If we do not have an initializer, the initialization value of the
7119      previous enumerator (stored in last_initializer) is incremented
7120      by 1 and is used to initialize the current enumerator.  */
7121   if (initializer == NULL)
7122     initializer = enum_initializer (last_initializer, old_locus);
7123
7124   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7125     {
7126       gfc_error ("ENUMERATOR %L not initialized with integer expression",
7127                  &var_locus);
7128       m = MATCH_ERROR;
7129       goto cleanup;
7130     }
7131
7132   /* Store this current initializer, for the next enumerator variable
7133      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
7134      use last_initializer below.  */
7135   last_initializer = initializer;
7136   t = add_init_expr_to_sym (name, &initializer, &var_locus);
7137
7138   /* Maintain enumerator history.  */
7139   gfc_find_symbol (name, NULL, 0, &sym);
7140   create_enum_history (sym, last_initializer);
7141
7142   return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7143
7144 cleanup:
7145   /* Free stuff up and return.  */
7146   gfc_free_expr (initializer);
7147
7148   return m;
7149 }
7150
7151
7152 /* Match the enumerator definition statement.  */
7153
7154 match
7155 gfc_match_enumerator_def (void)
7156 {
7157   match m;
7158   gfc_try t;
7159
7160   gfc_clear_ts (&current_ts);
7161
7162   m = gfc_match (" enumerator");
7163   if (m != MATCH_YES)
7164     return m;
7165
7166   m = gfc_match (" :: ");
7167   if (m == MATCH_ERROR)
7168     return m;
7169
7170   colon_seen = (m == MATCH_YES);
7171
7172   if (gfc_current_state () != COMP_ENUM)
7173     {
7174       gfc_error ("ENUM definition statement expected before %C");
7175       gfc_free_enum_history ();
7176       return MATCH_ERROR;
7177     }
7178
7179   (&current_ts)->type = BT_INTEGER;
7180   (&current_ts)->kind = gfc_c_int_kind;
7181
7182   gfc_clear_attr (&current_attr);
7183   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7184   if (t == FAILURE)
7185     {
7186       m = MATCH_ERROR;
7187       goto cleanup;
7188     }
7189
7190   for (;;)
7191     {
7192       m = enumerator_decl ();
7193       if (m == MATCH_ERROR)
7194         {
7195           gfc_free_enum_history ();
7196           goto cleanup;
7197         }
7198       if (m == MATCH_NO)
7199         break;
7200
7201       if (gfc_match_eos () == MATCH_YES)
7202         goto cleanup;
7203       if (gfc_match_char (',') != MATCH_YES)
7204         break;
7205     }
7206
7207   if (gfc_current_state () == COMP_ENUM)
7208     {
7209       gfc_free_enum_history ();
7210       gfc_error ("Syntax error in ENUMERATOR definition at %C");
7211       m = MATCH_ERROR;
7212     }
7213
7214 cleanup:
7215   gfc_free_array_spec (current_as);
7216   current_as = NULL;
7217   return m;
7218
7219 }
7220
7221
7222 /* Match binding attributes.  */
7223
7224 static match
7225 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7226 {
7227   bool found_passing = false;
7228   bool seen_ptr = false;
7229   match m = MATCH_YES;
7230
7231   /* Intialize to defaults.  Do so even before the MATCH_NO check so that in
7232      this case the defaults are in there.  */
7233   ba->access = ACCESS_UNKNOWN;
7234   ba->pass_arg = NULL;
7235   ba->pass_arg_num = 0;
7236   ba->nopass = 0;
7237   ba->non_overridable = 0;
7238   ba->deferred = 0;
7239   ba->ppc = ppc;
7240
7241   /* If we find a comma, we believe there are binding attributes.  */
7242   m = gfc_match_char (',');
7243   if (m == MATCH_NO)
7244     goto done;
7245
7246   do
7247     {
7248       /* Access specifier.  */
7249
7250       m = gfc_match (" public");
7251       if (m == MATCH_ERROR)
7252         goto error;
7253       if (m == MATCH_YES)
7254         {
7255           if (ba->access != ACCESS_UNKNOWN)
7256             {
7257               gfc_error ("Duplicate access-specifier at %C");
7258               goto error;
7259             }
7260
7261           ba->access = ACCESS_PUBLIC;
7262           continue;
7263         }
7264
7265       m = gfc_match (" private");
7266       if (m == MATCH_ERROR)
7267         goto error;
7268       if (m == MATCH_YES)
7269         {
7270           if (ba->access != ACCESS_UNKNOWN)
7271             {
7272               gfc_error ("Duplicate access-specifier at %C");
7273               goto error;
7274             }
7275
7276           ba->access = ACCESS_PRIVATE;
7277           continue;
7278         }
7279
7280       /* If inside GENERIC, the following is not allowed.  */
7281       if (!generic)
7282         {
7283
7284           /* NOPASS flag.  */
7285           m = gfc_match (" nopass");
7286           if (m == MATCH_ERROR)
7287             goto error;
7288           if (m == MATCH_YES)
7289             {
7290               if (found_passing)
7291                 {
7292                   gfc_error ("Binding attributes already specify passing,"
7293                              " illegal NOPASS at %C");
7294                   goto error;
7295                 }
7296
7297               found_passing = true;
7298               ba->nopass = 1;
7299               continue;
7300             }
7301
7302           /* PASS possibly including argument.  */
7303           m = gfc_match (" pass");
7304           if (m == MATCH_ERROR)
7305             goto error;
7306           if (m == MATCH_YES)
7307             {
7308               char arg[GFC_MAX_SYMBOL_LEN + 1];
7309
7310               if (found_passing)
7311                 {
7312                   gfc_error ("Binding attributes already specify passing,"
7313                              " illegal PASS at %C");
7314                   goto error;
7315                 }
7316
7317               m = gfc_match (" ( %n )", arg);
7318               if (m == MATCH_ERROR)
7319                 goto error;
7320               if (m == MATCH_YES)
7321                 ba->pass_arg = gfc_get_string (arg);
7322               gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7323
7324               found_passing = true;
7325               ba->nopass = 0;
7326               continue;
7327             }
7328
7329           if (ppc)
7330             {
7331               /* POINTER flag.  */
7332               m = gfc_match (" pointer");
7333               if (m == MATCH_ERROR)
7334                 goto error;
7335               if (m == MATCH_YES)
7336                 {
7337                   if (seen_ptr)
7338                     {
7339                       gfc_error ("Duplicate POINTER attribute at %C");
7340                       goto error;
7341                     }
7342
7343                   seen_ptr = true;
7344                   continue;
7345                 }
7346             }
7347           else
7348             {
7349               /* NON_OVERRIDABLE flag.  */
7350               m = gfc_match (" non_overridable");
7351               if (m == MATCH_ERROR)
7352                 goto error;
7353               if (m == MATCH_YES)
7354                 {
7355                   if (ba->non_overridable)
7356                     {
7357                       gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7358                       goto error;
7359                     }
7360
7361                   ba->non_overridable = 1;
7362                   continue;
7363                 }
7364
7365               /* DEFERRED flag.  */
7366               m = gfc_match (" deferred");
7367               if (m == MATCH_ERROR)
7368                 goto error;
7369               if (m == MATCH_YES)
7370                 {
7371                   if (ba->deferred)
7372                     {
7373                       gfc_error ("Duplicate DEFERRED at %C");
7374                       goto error;
7375                     }
7376
7377                   ba->deferred = 1;
7378                   continue;
7379                 }
7380             }
7381
7382         }
7383
7384       /* Nothing matching found.  */
7385       if (generic)
7386         gfc_error ("Expected access-specifier at %C");
7387       else
7388         gfc_error ("Expected binding attribute at %C");
7389       goto error;
7390     }
7391   while (gfc_match_char (',') == MATCH_YES);
7392
7393   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
7394   if (ba->non_overridable && ba->deferred)
7395     {
7396       gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7397       goto error;
7398     }
7399
7400   m = MATCH_YES;
7401
7402 done:
7403   if (ba->access == ACCESS_UNKNOWN)
7404     ba->access = gfc_typebound_default_access;
7405
7406   if (ppc && !seen_ptr)
7407     {
7408       gfc_error ("POINTER attribute is required for procedure pointer component"
7409                  " at %C");
7410       goto error;
7411     }
7412
7413   return m;
7414
7415 error:
7416   return MATCH_ERROR;
7417 }
7418
7419
7420 /* Match a PROCEDURE specific binding inside a derived type.  */
7421
7422 static match
7423 match_procedure_in_type (void)
7424 {
7425   char name[GFC_MAX_SYMBOL_LEN + 1];
7426   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7427   char* target = NULL;
7428   gfc_typebound_proc* tb;
7429   bool seen_colons;
7430   bool seen_attrs;
7431   match m;
7432   gfc_symtree* stree;
7433   gfc_namespace* ns;
7434   gfc_symbol* block;
7435
7436   /* Check current state.  */
7437   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7438   block = gfc_state_stack->previous->sym;
7439   gcc_assert (block);
7440
7441   /* Try to match PROCEDURE(interface).  */
7442   if (gfc_match (" (") == MATCH_YES)
7443     {
7444       m = gfc_match_name (target_buf);
7445       if (m == MATCH_ERROR)
7446         return m;
7447       if (m != MATCH_YES)
7448         {
7449           gfc_error ("Interface-name expected after '(' at %C");
7450           return MATCH_ERROR;
7451         }
7452
7453       if (gfc_match (" )") != MATCH_YES)
7454         {
7455           gfc_error ("')' expected at %C");
7456           return MATCH_ERROR;
7457         }
7458
7459       target = target_buf;
7460     }
7461
7462   /* Construct the data structure.  */
7463   tb = gfc_get_typebound_proc ();
7464   tb->where = gfc_current_locus;
7465   tb->is_generic = 0;
7466
7467   /* Match binding attributes.  */
7468   m = match_binding_attributes (tb, false, false);
7469   if (m == MATCH_ERROR)
7470     return m;
7471   seen_attrs = (m == MATCH_YES);
7472
7473   /* Check that attribute DEFERRED is given iff an interface is specified, which
7474      means target != NULL.  */
7475   if (tb->deferred && !target)
7476     {
7477       gfc_error ("Interface must be specified for DEFERRED binding at %C");
7478       return MATCH_ERROR;
7479     }
7480   if (target && !tb->deferred)
7481     {
7482       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7483       return MATCH_ERROR;
7484     }
7485
7486   /* Match the colons.  */
7487   m = gfc_match (" ::");
7488   if (m == MATCH_ERROR)
7489     return m;
7490   seen_colons = (m == MATCH_YES);
7491   if (seen_attrs && !seen_colons)
7492     {
7493       gfc_error ("Expected '::' after binding-attributes at %C");
7494       return MATCH_ERROR;
7495     }
7496
7497   /* Match the binding name.  */ 
7498   m = gfc_match_name (name);
7499   if (m == MATCH_ERROR)
7500     return m;
7501   if (m == MATCH_NO)
7502     {
7503       gfc_error ("Expected binding name at %C");
7504       return MATCH_ERROR;
7505     }
7506
7507   /* Try to match the '=> target', if it's there.  */
7508   m = gfc_match (" =>");
7509   if (m == MATCH_ERROR)
7510     return m;
7511   if (m == MATCH_YES)
7512     {
7513       if (tb->deferred)
7514         {
7515           gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7516           return MATCH_ERROR;
7517         }
7518
7519       if (!seen_colons)
7520         {
7521           gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7522                      " at %C");
7523           return MATCH_ERROR;
7524         }
7525
7526       m = gfc_match_name (target_buf);
7527       if (m == MATCH_ERROR)
7528         return m;
7529       if (m == MATCH_NO)
7530         {
7531           gfc_error ("Expected binding target after '=>' at %C");
7532           return MATCH_ERROR;
7533         }
7534       target = target_buf;
7535     }
7536
7537   /* Now we should have the end.  */
7538   m = gfc_match_eos ();
7539   if (m == MATCH_ERROR)
7540     return m;
7541   if (m == MATCH_NO)
7542     {
7543       gfc_error ("Junk after PROCEDURE declaration at %C");
7544       return MATCH_ERROR;
7545     }
7546
7547   /* If no target was found, it has the same name as the binding.  */
7548   if (!target)
7549     target = name;
7550
7551   /* Get the namespace to insert the symbols into.  */
7552   ns = block->f2k_derived;
7553   gcc_assert (ns);
7554
7555   /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
7556   if (tb->deferred && !block->attr.abstract)
7557     {
7558       gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7559                  block->name);
7560       return MATCH_ERROR;
7561     }
7562
7563   /* See if we already have a binding with this name in the symtree which would
7564      be an error.  If a GENERIC already targetted this binding, it may be
7565      already there but then typebound is still NULL.  */
7566   stree = gfc_find_symtree (ns->tb_sym_root, name);
7567   if (stree && stree->n.tb)
7568     {
7569       gfc_error ("There's already a procedure with binding name '%s' for the"
7570                  " derived type '%s' at %C", name, block->name);
7571       return MATCH_ERROR;
7572     }
7573
7574   /* Insert it and set attributes.  */
7575
7576   if (!stree)
7577     {
7578       stree = gfc_new_symtree (&ns->tb_sym_root, name);
7579       gcc_assert (stree);
7580     }
7581   stree->n.tb = tb;
7582
7583   if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific, false))
7584     return MATCH_ERROR;
7585   gfc_set_sym_referenced (tb->u.specific->n.sym);
7586
7587   return MATCH_YES;
7588 }
7589
7590
7591 /* Match a GENERIC procedure binding inside a derived type.  */
7592
7593 match
7594 gfc_match_generic (void)
7595 {
7596   char name[GFC_MAX_SYMBOL_LEN + 1];
7597   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
7598   gfc_symbol* block;
7599   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
7600   gfc_typebound_proc* tb;
7601   gfc_namespace* ns;
7602   interface_type op_type;
7603   gfc_intrinsic_op op;
7604   match m;
7605
7606   /* Check current state.  */
7607   if (gfc_current_state () == COMP_DERIVED)
7608     {
7609       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7610       return MATCH_ERROR;
7611     }
7612   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7613     return MATCH_NO;
7614   block = gfc_state_stack->previous->sym;
7615   ns = block->f2k_derived;
7616   gcc_assert (block && ns);
7617
7618   /* See if we get an access-specifier.  */
7619   m = match_binding_attributes (&tbattr, true, false);
7620   if (m == MATCH_ERROR)
7621     goto error;
7622
7623   /* Now the colons, those are required.  */
7624   if (gfc_match (" ::") != MATCH_YES)
7625     {
7626       gfc_error ("Expected '::' at %C");
7627       goto error;
7628     }
7629
7630   /* Match the binding name; depending on type (operator / generic) format
7631      it for future error messages into bind_name.  */
7632  
7633   m = gfc_match_generic_spec (&op_type, name, &op);
7634   if (m == MATCH_ERROR)
7635     return MATCH_ERROR;
7636   if (m == MATCH_NO)
7637     {
7638       gfc_error ("Expected generic name or operator descriptor at %C");
7639       goto error;
7640     }
7641
7642   switch (op_type)
7643     {
7644     case INTERFACE_GENERIC:
7645       snprintf (bind_name, sizeof (bind_name), "%s", name);
7646       break;
7647  
7648     case INTERFACE_USER_OP:
7649       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
7650       break;
7651  
7652     case INTERFACE_INTRINSIC_OP:
7653       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
7654                 gfc_op2string (op));
7655       break;
7656
7657     default:
7658       gcc_unreachable ();
7659     }
7660
7661   /* Match the required =>.  */
7662   if (gfc_match (" =>") != MATCH_YES)
7663     {
7664       gfc_error ("Expected '=>' at %C");
7665       goto error;
7666     }
7667   
7668   /* Try to find existing GENERIC binding with this name / for this operator;
7669      if there is something, check that it is another GENERIC and then extend
7670      it rather than building a new node.  Otherwise, create it and put it
7671      at the right position.  */
7672
7673   switch (op_type)
7674     {
7675     case INTERFACE_USER_OP:
7676     case INTERFACE_GENERIC:
7677       {
7678         const bool is_op = (op_type == INTERFACE_USER_OP);
7679         gfc_symtree* st;
7680
7681         st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
7682         if (st)
7683           {
7684             tb = st->n.tb;
7685             gcc_assert (tb);
7686           }
7687         else
7688           tb = NULL;
7689
7690         break;
7691       }
7692
7693     case INTERFACE_INTRINSIC_OP:
7694       tb = ns->tb_op[op];
7695       break;
7696
7697     default:
7698       gcc_unreachable ();
7699     }
7700
7701   if (tb)
7702     {
7703       if (!tb->is_generic)
7704         {
7705           gcc_assert (op_type == INTERFACE_GENERIC);
7706           gfc_error ("There's already a non-generic procedure with binding name"
7707                      " '%s' for the derived type '%s' at %C",
7708                      bind_name, block->name);
7709           goto error;
7710         }
7711
7712       if (tb->access != tbattr.access)
7713         {
7714           gfc_error ("Binding at %C must have the same access as already"
7715                      " defined binding '%s'", bind_name);
7716           goto error;
7717         }
7718     }
7719   else
7720     {
7721       tb = gfc_get_typebound_proc ();
7722       tb->where = gfc_current_locus;
7723       tb->access = tbattr.access;
7724       tb->is_generic = 1;
7725       tb->u.generic = NULL;
7726
7727       switch (op_type)
7728         {
7729         case INTERFACE_GENERIC:
7730         case INTERFACE_USER_OP:
7731           {
7732             const bool is_op = (op_type == INTERFACE_USER_OP);
7733             gfc_symtree* st;
7734
7735             st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
7736                                   name);
7737             gcc_assert (st);
7738             st->n.tb = tb;
7739
7740             break;
7741           }
7742           
7743         case INTERFACE_INTRINSIC_OP:
7744           ns->tb_op[op] = tb;
7745           break;
7746
7747         default:
7748           gcc_unreachable ();
7749         }
7750     }
7751
7752   /* Now, match all following names as specific targets.  */
7753   do
7754     {
7755       gfc_symtree* target_st;
7756       gfc_tbp_generic* target;
7757
7758       m = gfc_match_name (name);
7759       if (m == MATCH_ERROR)
7760         goto error;
7761       if (m == MATCH_NO)
7762         {
7763           gfc_error ("Expected specific binding name at %C");
7764           goto error;
7765         }
7766
7767       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
7768
7769       /* See if this is a duplicate specification.  */
7770       for (target = tb->u.generic; target; target = target->next)
7771         if (target_st == target->specific_st)
7772           {
7773             gfc_error ("'%s' already defined as specific binding for the"
7774                        " generic '%s' at %C", name, bind_name);
7775             goto error;
7776           }
7777
7778       target = gfc_get_tbp_generic ();
7779       target->specific_st = target_st;
7780       target->specific = NULL;
7781       target->next = tb->u.generic;
7782       tb->u.generic = target;
7783     }
7784   while (gfc_match (" ,") == MATCH_YES);
7785
7786   /* Here should be the end.  */
7787   if (gfc_match_eos () != MATCH_YES)
7788     {
7789       gfc_error ("Junk after GENERIC binding at %C");
7790       goto error;
7791     }
7792
7793   return MATCH_YES;
7794
7795 error:
7796   return MATCH_ERROR;
7797 }
7798
7799
7800 /* Match a FINAL declaration inside a derived type.  */
7801
7802 match
7803 gfc_match_final_decl (void)
7804 {
7805   char name[GFC_MAX_SYMBOL_LEN + 1];
7806   gfc_symbol* sym;
7807   match m;
7808   gfc_namespace* module_ns;
7809   bool first, last;
7810   gfc_symbol* block;
7811
7812   if (gfc_current_form == FORM_FREE)
7813     {
7814       char c = gfc_peek_ascii_char ();
7815       if (!gfc_is_whitespace (c) && c != ':')
7816         return MATCH_NO;
7817     }
7818   
7819   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
7820     {
7821       if (gfc_current_form == FORM_FIXED)
7822         return MATCH_NO;
7823
7824       gfc_error ("FINAL declaration at %C must be inside a derived type "
7825                  "CONTAINS section");
7826       return MATCH_ERROR;
7827     }
7828
7829   block = gfc_state_stack->previous->sym;
7830   gcc_assert (block);
7831
7832   if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7833       || gfc_state_stack->previous->previous->state != COMP_MODULE)
7834     {
7835       gfc_error ("Derived type declaration with FINAL at %C must be in the"
7836                  " specification part of a MODULE");
7837       return MATCH_ERROR;
7838     }
7839
7840   module_ns = gfc_current_ns;
7841   gcc_assert (module_ns);
7842   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7843
7844   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
7845   if (gfc_match (" ::") == MATCH_ERROR)
7846     return MATCH_ERROR;
7847
7848   /* Match the sequence of procedure names.  */
7849   first = true;
7850   last = false;
7851   do
7852     {
7853       gfc_finalizer* f;
7854
7855       if (first && gfc_match_eos () == MATCH_YES)
7856         {
7857           gfc_error ("Empty FINAL at %C");
7858           return MATCH_ERROR;
7859         }
7860
7861       m = gfc_match_name (name);
7862       if (m == MATCH_NO)
7863         {
7864           gfc_error ("Expected module procedure name at %C");
7865           return MATCH_ERROR;
7866         }
7867       else if (m != MATCH_YES)
7868         return MATCH_ERROR;
7869
7870       if (gfc_match_eos () == MATCH_YES)
7871         last = true;
7872       if (!last && gfc_match_char (',') != MATCH_YES)
7873         {
7874           gfc_error ("Expected ',' at %C");
7875           return MATCH_ERROR;
7876         }
7877
7878       if (gfc_get_symbol (name, module_ns, &sym))
7879         {
7880           gfc_error ("Unknown procedure name \"%s\" at %C", name);
7881           return MATCH_ERROR;
7882         }
7883
7884       /* Mark the symbol as module procedure.  */
7885       if (sym->attr.proc != PROC_MODULE
7886           && gfc_add_procedure (&sym->attr, PROC_MODULE,
7887                                 sym->name, NULL) == FAILURE)
7888         return MATCH_ERROR;
7889
7890       /* Check if we already have this symbol in the list, this is an error.  */
7891       for (f = block->f2k_derived->finalizers; f; f = f->next)
7892         if (f->proc_sym == sym)
7893           {
7894             gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7895                        name);
7896             return MATCH_ERROR;
7897           }
7898
7899       /* Add this symbol to the list of finalizers.  */
7900       gcc_assert (block->f2k_derived);
7901       ++sym->refs;
7902       f = XCNEW (gfc_finalizer);
7903       f->proc_sym = sym;
7904       f->proc_tree = NULL;
7905       f->where = gfc_current_locus;
7906       f->next = block->f2k_derived->finalizers;
7907       block->f2k_derived->finalizers = f;
7908
7909       first = false;
7910     }
7911   while (!last);
7912
7913   return MATCH_YES;
7914 }
7915
7916
7917 const ext_attr_t ext_attr_list[] = {
7918   { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
7919   { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
7920   { "cdecl",     EXT_ATTR_CDECL,     "cdecl"     },
7921   { "stdcall",   EXT_ATTR_STDCALL,   "stdcall"   },
7922   { "fastcall",  EXT_ATTR_FASTCALL,  "fastcall"  },
7923   { NULL,        EXT_ATTR_LAST,      NULL        }
7924 };
7925
7926 /* Match a !GCC$ ATTRIBUTES statement of the form:
7927       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
7928    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
7929
7930    TODO: We should support all GCC attributes using the same syntax for
7931    the attribute list, i.e. the list in C
7932       __attributes(( attribute-list ))
7933    matches then
7934       !GCC$ ATTRIBUTES attribute-list ::
7935    Cf. c-parser.c's c_parser_attributes; the data can then directly be
7936    saved into a TREE.
7937
7938    As there is absolutely no risk of confusion, we should never return
7939    MATCH_NO.  */
7940 match
7941 gfc_match_gcc_attributes (void)
7942
7943   symbol_attribute attr;
7944   char name[GFC_MAX_SYMBOL_LEN + 1];
7945   unsigned id;
7946   gfc_symbol *sym;
7947   match m;
7948
7949   gfc_clear_attr (&attr);
7950   for(;;)
7951     {
7952       char ch;
7953
7954       if (gfc_match_name (name) != MATCH_YES)
7955         return MATCH_ERROR;
7956
7957       for (id = 0; id < EXT_ATTR_LAST; id++)
7958         if (strcmp (name, ext_attr_list[id].name) == 0)
7959           break;
7960
7961       if (id == EXT_ATTR_LAST)
7962         {
7963           gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
7964           return MATCH_ERROR;
7965         }
7966
7967       if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
7968           == FAILURE)
7969         return MATCH_ERROR;
7970
7971       gfc_gobble_whitespace ();
7972       ch = gfc_next_ascii_char ();
7973       if (ch == ':')
7974         {
7975           /* This is the successful exit condition for the loop.  */
7976           if (gfc_next_ascii_char () == ':')
7977             break;
7978         }
7979
7980       if (ch == ',')
7981         continue;
7982
7983       goto syntax;
7984     }
7985
7986   if (gfc_match_eos () == MATCH_YES)
7987     goto syntax;
7988
7989   for(;;)
7990     {
7991       m = gfc_match_name (name);
7992       if (m != MATCH_YES)
7993         return m;
7994
7995       if (find_special (name, &sym, true))
7996         return MATCH_ERROR;
7997       
7998       sym->attr.ext_attr |= attr.ext_attr;
7999
8000       if (gfc_match_eos () == MATCH_YES)
8001         break;
8002
8003       if (gfc_match_char (',') != MATCH_YES)
8004         goto syntax;
8005     }
8006
8007   return MATCH_YES;
8008
8009 syntax:
8010   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8011   return MATCH_ERROR;
8012 }