OSDN Git Service

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