OSDN Git Service

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