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